Index: include/clang/Driver/Options.td =================================================================== --- include/clang/Driver/Options.td +++ include/clang/Driver/Options.td @@ -812,6 +812,11 @@ HelpText<"Enable LTO in 'full' mode">; def fno_lto : Flag<["-"], "fno-lto">, Group<f_Group>, HelpText<"Disable LTO mode (default)">; +def flto_jobs_EQ : Joined<["-"], "flto-jobs=">, + Flags<[CC1Option]>, Group<f_Group>, + HelpText<"Controls the backend parallelism of -flto=thin (default " + "of 0 means the number of threads will be derived from " + "the number of CPUs detected)">; def fthinlto_index_EQ : Joined<["-"], "fthinlto-index=">, Flags<[CC1Option]>, Group<f_Group>, HelpText<"Perform ThinLTO importing using provided function summary index">; Index: lib/Driver/Tools.cpp =================================================================== --- lib/Driver/Tools.cpp +++ lib/Driver/Tools.cpp @@ -1998,8 +1998,19 @@ } } +static unsigned getLTOParallelism(const ArgList &Args, const Driver &D) { + unsigned Parallelism = 0; + Arg *LtoJobsArg = Args.getLastArg(options::OPT_flto_jobs_EQ); + if (LtoJobsArg && + StringRef(LtoJobsArg->getValue()).getAsInteger(10, Parallelism)) + D.Diag(diag::err_drv_invalid_int_value) << LtoJobsArg->getAsString(Args) + << LtoJobsArg->getValue(); + return Parallelism; +} + static void AddGoldPlugin(const ToolChain &ToolChain, const ArgList &Args, - ArgStringList &CmdArgs, bool IsThinLTO) { + ArgStringList &CmdArgs, bool IsThinLTO, + const Driver &D) { // Tell the linker to load the plugin. This has to come before AddLinkerInputs // as gold requires -plugin to come before any -plugin-opt that -Wl might // forward. @@ -2032,6 +2043,10 @@ if (IsThinLTO) CmdArgs.push_back("-plugin-opt=thinlto"); + if (unsigned Parallelism = getLTOParallelism(Args, D)) + CmdArgs.push_back(Args.MakeArgString(Twine("-plugin-opt=jobs=") + + std::to_string(Parallelism))); + // If an explicit debugger tuning argument appeared, pass it along. if (Arg *A = Args.getLastArg(options::OPT_gTune_Group, options::OPT_ggdbN_Group)) { @@ -7639,7 +7654,7 @@ options::OPT_t, options::OPT_Z_Flag, options::OPT_r}); if (D.isUsingLTO()) - AddGoldPlugin(ToolChain, Args, CmdArgs, D.getLTOMode() == LTOK_Thin); + AddGoldPlugin(ToolChain, Args, CmdArgs, D.getLTOMode() == LTOK_Thin, D); AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs); @@ -8060,6 +8075,13 @@ getMachOToolChain().addProfileRTLibs(Args, CmdArgs); + if (unsigned Parallelism = + getLTOParallelism(Args, getToolChain().getDriver())) { + CmdArgs.push_back("-mllvm"); + CmdArgs.push_back( + Args.MakeArgString(Twine("-threads=") + std::to_string(Parallelism))); + } + if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) { if (getToolChain().getDriver().CCCIsCXX()) getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs); @@ -8790,7 +8812,7 @@ Args.AddAllArgs(CmdArgs, options::OPT_r); if (D.isUsingLTO()) - AddGoldPlugin(ToolChain, Args, CmdArgs, D.getLTOMode() == LTOK_Thin); + AddGoldPlugin(ToolChain, Args, CmdArgs, D.getLTOMode() == LTOK_Thin, D); bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs); AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs); @@ -9623,7 +9645,7 @@ ToolChain.AddFilePathLibArgs(Args, CmdArgs); if (D.isUsingLTO()) - AddGoldPlugin(ToolChain, Args, CmdArgs, D.getLTOMode() == LTOK_Thin); + AddGoldPlugin(ToolChain, Args, CmdArgs, D.getLTOMode() == LTOK_Thin, D); if (Args.hasArg(options::OPT_Z_Xlinker__no_demangle)) CmdArgs.push_back("--no-demangle"); Index: test/Driver/lto-jobs.c =================================================================== --- /dev/null +++ test/Driver/lto-jobs.c @@ -0,0 +1,11 @@ +// Confirm that -flto-jobs=N is passed to linker + +// RUN: %clang -target x86_64-unknown-linux -### %s -flto=thin -flto-jobs=5 2> %t +// RUN: FileCheck -check-prefix=CHECK-LINK-THIN-JOBS-ACTION < %t %s +// +// CHECK-LINK-THIN-JOBS-ACTION: "-plugin-opt=jobs=5" + +// RUN: %clang -target x86_64-apple-darwin13.3.0 -### %s -flto=thin -flto-jobs=5 2> %t +// RUN: FileCheck -check-prefix=CHECK-LINK-THIN-JOBS2-ACTION < %t %s +// +// CHECK-LINK-THIN-JOBS2-ACTION: "-mllvm" "-threads=5"