Index: include/clang/Driver/Options.td =================================================================== --- include/clang/Driver/Options.td +++ include/clang/Driver/Options.td @@ -682,6 +682,8 @@ def flto_EQ : Joined<["-"], "flto=">, Group; def flto : Flag<["-"], "flto">, Flags<[CC1Option]>, Group; def fno_lto : Flag<["-"], "fno-lto">, Group; +def fthinlto : Flag<["-"], "fthinlto">, Flags<[CC1Option]>, Group; +def fno_thinlto : Flag<["-"], "fno-thinlto">, Group; def fmacro_backtrace_limit_EQ : Joined<["-"], "fmacro-backtrace-limit=">, Group, Flags<[DriverOption, CoreOption]>; def fmerge_all_constants : Flag<["-"], "fmerge-all-constants">, Group; Index: include/clang/Frontend/CodeGenOptions.def =================================================================== --- include/clang/Frontend/CodeGenOptions.def +++ include/clang/Frontend/CodeGenOptions.def @@ -72,7 +72,9 @@ CODEGENOPT(InstrumentForProfiling , 1, 0) ///< Set when -pg is enabled. CODEGENOPT(LessPreciseFPMAD , 1, 0) ///< Enable less precise MAD instructions to ///< be generated. -CODEGENOPT(PrepareForLTO , 1, 0) ///< Set when -flto is enabled on the +CODEGENOPT(PrepareForLTO , 1, 0) ///< Set when -flto or -fthinlto is enabled + ///< on the compile step. +CODEGENOPT(EmitThinLTOIndex , 1, 0) ///< Set when -fthinlto is enabled on the ///< compile step. CODEGENOPT(MergeAllConstants , 1, 1) ///< Merge identical constants. CODEGENOPT(MergeFunctions , 1, 0) ///< Set when -fmerge-functions is enabled. Index: lib/CodeGen/BackendUtil.cpp =================================================================== --- lib/CodeGen/BackendUtil.cpp +++ lib/CodeGen/BackendUtil.cpp @@ -618,7 +618,8 @@ case Backend_EmitBC: getPerModulePasses()->add( - createBitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists)); + createBitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists, + CodeGenOpts.EmitThinLTOIndex)); break; case Backend_EmitLL: Index: lib/Driver/Driver.cpp =================================================================== --- lib/Driver/Driver.cpp +++ lib/Driver/Driver.cpp @@ -1583,7 +1583,8 @@ } bool Driver::IsUsingLTO(const ArgList &Args) const { - return Args.hasFlag(options::OPT_flto, options::OPT_fno_lto, false); + return Args.hasFlag(options::OPT_flto, options::OPT_fno_lto, false) || + Args.hasFlag(options::OPT_fthinlto, options::OPT_fno_thinlto, false); } void Driver::BuildJobs(Compilation &C) const { Index: lib/Driver/Tools.cpp =================================================================== --- lib/Driver/Tools.cpp +++ lib/Driver/Tools.cpp @@ -1648,6 +1648,9 @@ std::string CPU = getCPUName(Args, ToolChain.getTriple()); if (!CPU.empty()) CmdArgs.push_back(Args.MakeArgString(Twine("-plugin-opt=mcpu=") + CPU)); + + if (Args.hasArg(options::OPT_fthinlto)) + CmdArgs.push_back("-plugin-opt=thinlto"); } /// This is a helper function for validating the optional refinement step @@ -2761,6 +2764,8 @@ // preprocessing, precompiling or assembling. Args.ClaimAllArgs(options::OPT_flto); Args.ClaimAllArgs(options::OPT_fno_lto); + Args.ClaimAllArgs(options::OPT_fthinlto); + Args.ClaimAllArgs(options::OPT_fno_thinlto); } static void appendUserToPath(SmallVectorImpl &Result) { @@ -3027,6 +3032,9 @@ "Invalid action for clang tool."); if (JA.getType() == types::TY_LTO_IR || JA.getType() == types::TY_LTO_BC) { + // Note that both -flto and -fthinlto use the TY_LTO_* types. + // Setting -flto here will enable the PrepareForLTO codegen option, + // which we do want for -fthinlto compiles as well. CmdArgs.push_back("-flto"); } if (JA.getType() == types::TY_Nothing) { @@ -3059,6 +3067,10 @@ // the use-list order, since serialization to bitcode is part of the flow. if (JA.getType() == types::TY_LLVM_BC) CmdArgs.push_back("-emit-llvm-uselists"); + + if (Args.hasArg(options::OPT_fthinlto)) { + CmdArgs.push_back("-fthinlto"); + } } // We normally speed up the clang process a bit by skipping destructors at Index: lib/Frontend/CompilerInvocation.cpp =================================================================== --- lib/Frontend/CompilerInvocation.cpp +++ lib/Frontend/CompilerInvocation.cpp @@ -516,6 +516,7 @@ Opts.MergeFunctions = Args.hasArg(OPT_fmerge_functions); Opts.PrepareForLTO = Args.hasArg(OPT_flto); + Opts.EmitThinLTOIndex = Args.hasArg(OPT_fthinlto); Opts.MSVolatile = Args.hasArg(OPT_fms_volatile);