Index: lib/Driver/ToolChains/CommonArgs.cpp =================================================================== --- lib/Driver/ToolChains/CommonArgs.cpp +++ lib/Driver/ToolChains/CommonArgs.cpp @@ -368,9 +368,16 @@ if (A->getOption().matches(options::OPT_O4) || A->getOption().matches(options::OPT_Ofast)) OOpt = "3"; - else if (A->getOption().matches(options::OPT_O)) - OOpt = A->getValue(); - else if (A->getOption().matches(options::OPT_O0)) + else if (A->getOption().matches(options::OPT_O)) { + StringRef OptLevel = A->getValue(); + // -Os and -Oz add corresponding attributes to functions. Just use -O2 + // for these optimization levels. Pass other optimization levels through + // to the plugin. + if (OptLevel == "s" || OptLevel == "z") + OOpt = "2"; + else + OOpt = OptLevel; + } else if (A->getOption().matches(options::OPT_O0)) OOpt = "0"; if (!OOpt.empty()) CmdArgs.push_back(Args.MakeArgString(Twine("-plugin-opt=O") + OOpt)); Index: test/Driver/gold-lto.c =================================================================== --- test/Driver/gold-lto.c +++ test/Driver/gold-lto.c @@ -26,3 +26,12 @@ // RUN: %clang -target i686-linux-android -### %t.o -flto 2>&1 \ // RUN: | FileCheck %s --check-prefix=CHECK-X86-ANDROID // CHECK-X86-ANDROID: "-plugin" "{{.*}}/LLVMgold.so" +// +// Test that -Os and -Oz are not passed to the plugin +// RUN: %clang -### %t.o -flto -Os 2>&1 \ +// RUN: | FileCheck %s --implicit-check-not "-plugin-opt=Os" \ +// RUN: --check-prefix=CHECK-O2 +// RUN: %clang -### %t.o -flto -Oz 2>&1 \ +// RUN: | FileCheck %s --implicit-check-not "-plugin-opt=Oz" \ +// RUN: --check-prefix=CHECK-O2 +// CHECK-O2: "-plugin-opt=O2"