Index: lib/Driver/ToolChains/CommonArgs.cpp =================================================================== --- lib/Driver/ToolChains/CommonArgs.cpp +++ lib/Driver/ToolChains/CommonArgs.cpp @@ -366,12 +366,17 @@ if (Arg *A = Args.getLastArg(options::OPT_O_Group)) { StringRef OOpt; if (A->getOption().matches(options::OPT_O4) || - A->getOption().matches(options::OPT_Ofast)) + 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(); + // Do not pass optimization levels pertaining to code size to the plugin. + // They are captured by corresponding function attributes. + if (!OptLevel.equals("s") && !OptLevel.equals("z")) + 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,9 @@ // 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: %clang -### %t.o -flto -Os 2>&1 \ +// RUN: | FileCheck %s --implicit-check-not "-plugin-opt=Oz"