1 //===-LTOBackend.cpp - LLVM Link Time Optimizer Backend -------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the "backend" phase of LTO, i.e. it performs
10 // optimization and code generation on a loaded module. It is generally used
11 // internally by the LTO class but can also be used independently, for example
12 // to implement a standalone ThinLTO backend.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "llvm/LTO/LTOBackend.h"
17 #include "llvm/Analysis/AliasAnalysis.h"
18 #include "llvm/Analysis/CGSCCPassManager.h"
19 #include "llvm/Analysis/ModuleSummaryAnalysis.h"
20 #include "llvm/Analysis/TargetLibraryInfo.h"
21 #include "llvm/Bitcode/BitcodeReader.h"
22 #include "llvm/Bitcode/BitcodeWriter.h"
23 #include "llvm/IR/LLVMRemarkStreamer.h"
24 #include "llvm/IR/LegacyPassManager.h"
25 #include "llvm/IR/PassManager.h"
26 #include "llvm/IR/Verifier.h"
27 #include "llvm/LTO/LTO.h"
28 #include "llvm/MC/SubtargetFeature.h"
29 #include "llvm/MC/TargetRegistry.h"
30 #include "llvm/Object/ModuleSymbolTable.h"
31 #include "llvm/Passes/PassBuilder.h"
32 #include "llvm/Passes/PassPlugin.h"
33 #include "llvm/Passes/StandardInstrumentations.h"
34 #include "llvm/Support/Error.h"
35 #include "llvm/Support/FileSystem.h"
36 #include "llvm/Support/MemoryBuffer.h"
37 #include "llvm/Support/Path.h"
38 #include "llvm/Support/Program.h"
39 #include "llvm/Support/ThreadPool.h"
40 #include "llvm/Support/ToolOutputFile.h"
41 #include "llvm/Support/raw_ostream.h"
42 #include "llvm/Target/TargetMachine.h"
43 #include "llvm/Transforms/Scalar/LoopPassManager.h"
44 #include "llvm/Transforms/Utils/FunctionImportUtils.h"
45 #include "llvm/Transforms/Utils/SplitModule.h"
46 
47 using namespace llvm;
48 using namespace lto;
49 
50 #define DEBUG_TYPE "lto-backend"
51 
52 enum class LTOBitcodeEmbedding {
53   DoNotEmbed = 0,
54   EmbedOptimized = 1,
55   EmbedPostMergePreOptimized = 2
56 };
57 
58 static cl::opt<LTOBitcodeEmbedding> EmbedBitcode(
59     "lto-embed-bitcode", cl::init(LTOBitcodeEmbedding::DoNotEmbed),
60     cl::values(clEnumValN(LTOBitcodeEmbedding::DoNotEmbed, "none",
61                           "Do not embed"),
62                clEnumValN(LTOBitcodeEmbedding::EmbedOptimized, "optimized",
63                           "Embed after all optimization passes"),
64                clEnumValN(LTOBitcodeEmbedding::EmbedPostMergePreOptimized,
65                           "post-merge-pre-opt",
66                           "Embed post merge, but before optimizations")),
67     cl::desc("Embed LLVM bitcode in object files produced by LTO"));
68 
69 static cl::opt<bool> ThinLTOAssumeMerged(
70     "thinlto-assume-merged", cl::init(false),
71     cl::desc("Assume the input has already undergone ThinLTO function "
72              "importing and the other pre-optimization pipeline changes."));
73 
74 namespace llvm {
75 extern cl::opt<bool> NoPGOWarnMismatch;
76 }
77 
78 [[noreturn]] static void reportOpenError(StringRef Path, Twine Msg) {
79   errs() << "failed to open " << Path << ": " << Msg << '\n';
80   errs().flush();
81   exit(1);
82 }
83 
84 Error Config::addSaveTemps(std::string OutputFileName,
85                            bool UseInputModulePath) {
86   ShouldDiscardValueNames = false;
87 
88   std::error_code EC;
89   ResolutionFile =
90       std::make_unique<raw_fd_ostream>(OutputFileName + "resolution.txt", EC,
91                                        sys::fs::OpenFlags::OF_TextWithCRLF);
92   if (EC) {
93     ResolutionFile.reset();
94     return errorCodeToError(EC);
95   }
96 
97   auto setHook = [&](std::string PathSuffix, ModuleHookFn &Hook) {
98     // Keep track of the hook provided by the linker, which also needs to run.
99     ModuleHookFn LinkerHook = Hook;
100     Hook = [=](unsigned Task, const Module &M) {
101       // If the linker's hook returned false, we need to pass that result
102       // through.
103       if (LinkerHook && !LinkerHook(Task, M))
104         return false;
105 
106       std::string PathPrefix;
107       // If this is the combined module (not a ThinLTO backend compile) or the
108       // user hasn't requested using the input module's path, emit to a file
109       // named from the provided OutputFileName with the Task ID appended.
110       if (M.getModuleIdentifier() == "ld-temp.o" || !UseInputModulePath) {
111         PathPrefix = OutputFileName;
112         if (Task != (unsigned)-1)
113           PathPrefix += utostr(Task) + ".";
114       } else
115         PathPrefix = M.getModuleIdentifier() + ".";
116       std::string Path = PathPrefix + PathSuffix + ".bc";
117       std::error_code EC;
118       raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::OF_None);
119       // Because -save-temps is a debugging feature, we report the error
120       // directly and exit.
121       if (EC)
122         reportOpenError(Path, EC.message());
123       WriteBitcodeToFile(M, OS, /*ShouldPreserveUseListOrder=*/false);
124       return true;
125     };
126   };
127 
128   setHook("0.preopt", PreOptModuleHook);
129   setHook("1.promote", PostPromoteModuleHook);
130   setHook("2.internalize", PostInternalizeModuleHook);
131   setHook("3.import", PostImportModuleHook);
132   setHook("4.opt", PostOptModuleHook);
133   setHook("5.precodegen", PreCodeGenModuleHook);
134 
135   CombinedIndexHook =
136       [=](const ModuleSummaryIndex &Index,
137           const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
138         std::string Path = OutputFileName + "index.bc";
139         std::error_code EC;
140         raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::OF_None);
141         // Because -save-temps is a debugging feature, we report the error
142         // directly and exit.
143         if (EC)
144           reportOpenError(Path, EC.message());
145         writeIndexToFile(Index, OS);
146 
147         Path = OutputFileName + "index.dot";
148         raw_fd_ostream OSDot(Path, EC, sys::fs::OpenFlags::OF_None);
149         if (EC)
150           reportOpenError(Path, EC.message());
151         Index.exportToDot(OSDot, GUIDPreservedSymbols);
152         return true;
153       };
154 
155   return Error::success();
156 }
157 
158 #define HANDLE_EXTENSION(Ext)                                                  \
159   llvm::PassPluginLibraryInfo get##Ext##PluginInfo();
160 #include "llvm/Support/Extension.def"
161 
162 static void RegisterPassPlugins(ArrayRef<std::string> PassPlugins,
163                                 PassBuilder &PB) {
164 #define HANDLE_EXTENSION(Ext)                                                  \
165   get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB);
166 #include "llvm/Support/Extension.def"
167 
168   // Load requested pass plugins and let them register pass builder callbacks
169   for (auto &PluginFN : PassPlugins) {
170     auto PassPlugin = PassPlugin::Load(PluginFN);
171     if (!PassPlugin) {
172       errs() << "Failed to load passes from '" << PluginFN
173              << "'. Request ignored.\n";
174       continue;
175     }
176 
177     PassPlugin->registerPassBuilderCallbacks(PB);
178   }
179 }
180 
181 static std::unique_ptr<TargetMachine>
182 createTargetMachine(const Config &Conf, const Target *TheTarget, Module &M) {
183   StringRef TheTriple = M.getTargetTriple();
184   SubtargetFeatures Features;
185   Features.getDefaultSubtargetFeatures(Triple(TheTriple));
186   for (const std::string &A : Conf.MAttrs)
187     Features.AddFeature(A);
188 
189   Optional<Reloc::Model> RelocModel = None;
190   if (Conf.RelocModel)
191     RelocModel = *Conf.RelocModel;
192   else if (M.getModuleFlag("PIC Level"))
193     RelocModel =
194         M.getPICLevel() == PICLevel::NotPIC ? Reloc::Static : Reloc::PIC_;
195 
196   Optional<CodeModel::Model> CodeModel;
197   if (Conf.CodeModel)
198     CodeModel = *Conf.CodeModel;
199   else
200     CodeModel = M.getCodeModel();
201 
202   std::unique_ptr<TargetMachine> TM(TheTarget->createTargetMachine(
203       TheTriple, Conf.CPU, Features.getString(), Conf.Options, RelocModel,
204       CodeModel, Conf.CGOptLevel));
205   assert(TM && "Failed to create target machine");
206   return TM;
207 }
208 
209 static void runNewPMPasses(const Config &Conf, Module &Mod, TargetMachine *TM,
210                            unsigned OptLevel, bool IsThinLTO,
211                            ModuleSummaryIndex *ExportSummary,
212                            const ModuleSummaryIndex *ImportSummary) {
213   Optional<PGOOptions> PGOOpt;
214   if (!Conf.SampleProfile.empty())
215     PGOOpt = PGOOptions(Conf.SampleProfile, "", Conf.ProfileRemapping,
216                         PGOOptions::SampleUse, PGOOptions::NoCSAction, true);
217   else if (Conf.RunCSIRInstr) {
218     PGOOpt = PGOOptions("", Conf.CSIRProfile, Conf.ProfileRemapping,
219                         PGOOptions::IRUse, PGOOptions::CSIRInstr,
220                         Conf.AddFSDiscriminator);
221   } else if (!Conf.CSIRProfile.empty()) {
222     PGOOpt = PGOOptions(Conf.CSIRProfile, "", Conf.ProfileRemapping,
223                         PGOOptions::IRUse, PGOOptions::CSIRUse,
224                         Conf.AddFSDiscriminator);
225     NoPGOWarnMismatch = !Conf.PGOWarnMismatch;
226   } else if (Conf.AddFSDiscriminator) {
227     PGOOpt = PGOOptions("", "", "", PGOOptions::NoAction,
228                         PGOOptions::NoCSAction, true);
229   }
230   TM->setPGOOption(PGOOpt);
231 
232   LoopAnalysisManager LAM;
233   FunctionAnalysisManager FAM;
234   CGSCCAnalysisManager CGAM;
235   ModuleAnalysisManager MAM;
236 
237   PassInstrumentationCallbacks PIC;
238   StandardInstrumentations SI(Conf.DebugPassManager);
239   SI.registerCallbacks(PIC, &FAM);
240   PassBuilder PB(TM, Conf.PTO, PGOOpt, &PIC);
241 
242   RegisterPassPlugins(Conf.PassPlugins, PB);
243 
244   std::unique_ptr<TargetLibraryInfoImpl> TLII(
245       new TargetLibraryInfoImpl(Triple(TM->getTargetTriple())));
246   if (Conf.Freestanding)
247     TLII->disableAllFunctions();
248   FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
249 
250   // Parse a custom AA pipeline if asked to.
251   if (!Conf.AAPipeline.empty()) {
252     AAManager AA;
253     if (auto Err = PB.parseAAPipeline(AA, Conf.AAPipeline)) {
254       report_fatal_error(Twine("unable to parse AA pipeline description '") +
255                          Conf.AAPipeline + "': " + toString(std::move(Err)));
256     }
257     // Register the AA manager first so that our version is the one used.
258     FAM.registerPass([&] { return std::move(AA); });
259   }
260 
261   // Register all the basic analyses with the managers.
262   PB.registerModuleAnalyses(MAM);
263   PB.registerCGSCCAnalyses(CGAM);
264   PB.registerFunctionAnalyses(FAM);
265   PB.registerLoopAnalyses(LAM);
266   PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
267 
268   ModulePassManager MPM;
269 
270   if (!Conf.DisableVerify)
271     MPM.addPass(VerifierPass());
272 
273   OptimizationLevel OL;
274 
275   switch (OptLevel) {
276   default:
277     llvm_unreachable("Invalid optimization level");
278   case 0:
279     OL = OptimizationLevel::O0;
280     break;
281   case 1:
282     OL = OptimizationLevel::O1;
283     break;
284   case 2:
285     OL = OptimizationLevel::O2;
286     break;
287   case 3:
288     OL = OptimizationLevel::O3;
289     break;
290   }
291 
292   // Parse a custom pipeline if asked to.
293   if (!Conf.OptPipeline.empty()) {
294     if (auto Err = PB.parsePassPipeline(MPM, Conf.OptPipeline)) {
295       report_fatal_error(Twine("unable to parse pass pipeline description '") +
296                          Conf.OptPipeline + "': " + toString(std::move(Err)));
297     }
298   } else if (Conf.UseDefaultPipeline) {
299     MPM.addPass(PB.buildPerModuleDefaultPipeline(OL));
300   } else if (IsThinLTO) {
301     MPM.addPass(PB.buildThinLTODefaultPipeline(OL, ImportSummary));
302   } else {
303     MPM.addPass(PB.buildLTODefaultPipeline(OL, ExportSummary));
304   }
305 
306   if (!Conf.DisableVerify)
307     MPM.addPass(VerifierPass());
308 
309   MPM.run(Mod, MAM);
310 }
311 
312 bool lto::opt(const Config &Conf, TargetMachine *TM, unsigned Task, Module &Mod,
313               bool IsThinLTO, ModuleSummaryIndex *ExportSummary,
314               const ModuleSummaryIndex *ImportSummary,
315               const std::vector<uint8_t> &CmdArgs) {
316   if (EmbedBitcode == LTOBitcodeEmbedding::EmbedPostMergePreOptimized) {
317     // FIXME: the motivation for capturing post-merge bitcode and command line
318     // is replicating the compilation environment from bitcode, without needing
319     // to understand the dependencies (the functions to be imported). This
320     // assumes a clang - based invocation, case in which we have the command
321     // line.
322     // It's not very clear how the above motivation would map in the
323     // linker-based case, so we currently don't plumb the command line args in
324     // that case.
325     if (CmdArgs.empty())
326       LLVM_DEBUG(
327           dbgs() << "Post-(Thin)LTO merge bitcode embedding was requested, but "
328                     "command line arguments are not available");
329     llvm::embedBitcodeInModule(Mod, llvm::MemoryBufferRef(),
330                                /*EmbedBitcode*/ true, /*EmbedCmdline*/ true,
331                                /*Cmdline*/ CmdArgs);
332   }
333   // FIXME: Plumb the combined index into the new pass manager.
334   runNewPMPasses(Conf, Mod, TM, Conf.OptLevel, IsThinLTO, ExportSummary,
335                  ImportSummary);
336   return !Conf.PostOptModuleHook || Conf.PostOptModuleHook(Task, Mod);
337 }
338 
339 static void codegen(const Config &Conf, TargetMachine *TM,
340                     AddStreamFn AddStream, unsigned Task, Module &Mod,
341                     const ModuleSummaryIndex &CombinedIndex) {
342   if (Conf.PreCodeGenModuleHook && !Conf.PreCodeGenModuleHook(Task, Mod))
343     return;
344 
345   if (EmbedBitcode == LTOBitcodeEmbedding::EmbedOptimized)
346     llvm::embedBitcodeInModule(Mod, llvm::MemoryBufferRef(),
347                                /*EmbedBitcode*/ true,
348                                /*EmbedCmdline*/ false,
349                                /*CmdArgs*/ std::vector<uint8_t>());
350 
351   std::unique_ptr<ToolOutputFile> DwoOut;
352   SmallString<1024> DwoFile(Conf.SplitDwarfOutput);
353   if (!Conf.DwoDir.empty()) {
354     std::error_code EC;
355     if (auto EC = llvm::sys::fs::create_directories(Conf.DwoDir))
356       report_fatal_error(Twine("Failed to create directory ") + Conf.DwoDir +
357                          ": " + EC.message());
358 
359     DwoFile = Conf.DwoDir;
360     sys::path::append(DwoFile, std::to_string(Task) + ".dwo");
361     TM->Options.MCOptions.SplitDwarfFile = std::string(DwoFile);
362   } else
363     TM->Options.MCOptions.SplitDwarfFile = Conf.SplitDwarfFile;
364 
365   if (!DwoFile.empty()) {
366     std::error_code EC;
367     DwoOut = std::make_unique<ToolOutputFile>(DwoFile, EC, sys::fs::OF_None);
368     if (EC)
369       report_fatal_error(Twine("Failed to open ") + DwoFile + ": " +
370                          EC.message());
371   }
372 
373   Expected<std::unique_ptr<CachedFileStream>> StreamOrErr = AddStream(Task);
374   if (Error Err = StreamOrErr.takeError())
375     report_fatal_error(std::move(Err));
376   std::unique_ptr<CachedFileStream> &Stream = *StreamOrErr;
377   TM->Options.ObjectFilenameForDebug = Stream->ObjectPathName;
378 
379   legacy::PassManager CodeGenPasses;
380   TargetLibraryInfoImpl TLII(Triple(Mod.getTargetTriple()));
381   CodeGenPasses.add(new TargetLibraryInfoWrapperPass(TLII));
382   CodeGenPasses.add(
383       createImmutableModuleSummaryIndexWrapperPass(&CombinedIndex));
384   if (Conf.PreCodeGenPassesHook)
385     Conf.PreCodeGenPassesHook(CodeGenPasses);
386   if (TM->addPassesToEmitFile(CodeGenPasses, *Stream->OS,
387                               DwoOut ? &DwoOut->os() : nullptr,
388                               Conf.CGFileType))
389     report_fatal_error("Failed to setup codegen");
390   CodeGenPasses.run(Mod);
391 
392   if (DwoOut)
393     DwoOut->keep();
394 }
395 
396 static void splitCodeGen(const Config &C, TargetMachine *TM,
397                          AddStreamFn AddStream,
398                          unsigned ParallelCodeGenParallelismLevel, Module &Mod,
399                          const ModuleSummaryIndex &CombinedIndex) {
400   ThreadPool CodegenThreadPool(
401       heavyweight_hardware_concurrency(ParallelCodeGenParallelismLevel));
402   unsigned ThreadCount = 0;
403   const Target *T = &TM->getTarget();
404 
405   SplitModule(
406       Mod, ParallelCodeGenParallelismLevel,
407       [&](std::unique_ptr<Module> MPart) {
408         // We want to clone the module in a new context to multi-thread the
409         // codegen. We do it by serializing partition modules to bitcode
410         // (while still on the main thread, in order to avoid data races) and
411         // spinning up new threads which deserialize the partitions into
412         // separate contexts.
413         // FIXME: Provide a more direct way to do this in LLVM.
414         SmallString<0> BC;
415         raw_svector_ostream BCOS(BC);
416         WriteBitcodeToFile(*MPart, BCOS);
417 
418         // Enqueue the task
419         CodegenThreadPool.async(
420             [&](const SmallString<0> &BC, unsigned ThreadId) {
421               LTOLLVMContext Ctx(C);
422               Expected<std::unique_ptr<Module>> MOrErr = parseBitcodeFile(
423                   MemoryBufferRef(StringRef(BC.data(), BC.size()), "ld-temp.o"),
424                   Ctx);
425               if (!MOrErr)
426                 report_fatal_error("Failed to read bitcode");
427               std::unique_ptr<Module> MPartInCtx = std::move(MOrErr.get());
428 
429               std::unique_ptr<TargetMachine> TM =
430                   createTargetMachine(C, T, *MPartInCtx);
431 
432               codegen(C, TM.get(), AddStream, ThreadId, *MPartInCtx,
433                       CombinedIndex);
434             },
435             // Pass BC using std::move to ensure that it get moved rather than
436             // copied into the thread's context.
437             std::move(BC), ThreadCount++);
438       },
439       false);
440 
441   // Because the inner lambda (which runs in a worker thread) captures our local
442   // variables, we need to wait for the worker threads to terminate before we
443   // can leave the function scope.
444   CodegenThreadPool.wait();
445 }
446 
447 static Expected<const Target *> initAndLookupTarget(const Config &C,
448                                                     Module &Mod) {
449   if (!C.OverrideTriple.empty())
450     Mod.setTargetTriple(C.OverrideTriple);
451   else if (Mod.getTargetTriple().empty())
452     Mod.setTargetTriple(C.DefaultTriple);
453 
454   std::string Msg;
455   const Target *T = TargetRegistry::lookupTarget(Mod.getTargetTriple(), Msg);
456   if (!T)
457     return make_error<StringError>(Msg, inconvertibleErrorCode());
458   return T;
459 }
460 
461 Error lto::finalizeOptimizationRemarks(
462     std::unique_ptr<ToolOutputFile> DiagOutputFile) {
463   // Make sure we flush the diagnostic remarks file in case the linker doesn't
464   // call the global destructors before exiting.
465   if (!DiagOutputFile)
466     return Error::success();
467   DiagOutputFile->keep();
468   DiagOutputFile->os().flush();
469   return Error::success();
470 }
471 
472 Error lto::backend(const Config &C, AddStreamFn AddStream,
473                    unsigned ParallelCodeGenParallelismLevel, Module &Mod,
474                    ModuleSummaryIndex &CombinedIndex) {
475   Expected<const Target *> TOrErr = initAndLookupTarget(C, Mod);
476   if (!TOrErr)
477     return TOrErr.takeError();
478 
479   std::unique_ptr<TargetMachine> TM = createTargetMachine(C, *TOrErr, Mod);
480 
481   if (!C.CodeGenOnly) {
482     if (!opt(C, TM.get(), 0, Mod, /*IsThinLTO=*/false,
483              /*ExportSummary=*/&CombinedIndex, /*ImportSummary=*/nullptr,
484              /*CmdArgs*/ std::vector<uint8_t>()))
485       return Error::success();
486   }
487 
488   if (ParallelCodeGenParallelismLevel == 1) {
489     codegen(C, TM.get(), AddStream, 0, Mod, CombinedIndex);
490   } else {
491     splitCodeGen(C, TM.get(), AddStream, ParallelCodeGenParallelismLevel, Mod,
492                  CombinedIndex);
493   }
494   return Error::success();
495 }
496 
497 static void dropDeadSymbols(Module &Mod, const GVSummaryMapTy &DefinedGlobals,
498                             const ModuleSummaryIndex &Index) {
499   std::vector<GlobalValue*> DeadGVs;
500   for (auto &GV : Mod.global_values())
501     if (GlobalValueSummary *GVS = DefinedGlobals.lookup(GV.getGUID()))
502       if (!Index.isGlobalValueLive(GVS)) {
503         DeadGVs.push_back(&GV);
504         convertToDeclaration(GV);
505       }
506 
507   // Now that all dead bodies have been dropped, delete the actual objects
508   // themselves when possible.
509   for (GlobalValue *GV : DeadGVs) {
510     GV->removeDeadConstantUsers();
511     // Might reference something defined in native object (i.e. dropped a
512     // non-prevailing IR def, but we need to keep the declaration).
513     if (GV->use_empty())
514       GV->eraseFromParent();
515   }
516 }
517 
518 Error lto::thinBackend(const Config &Conf, unsigned Task, AddStreamFn AddStream,
519                        Module &Mod, const ModuleSummaryIndex &CombinedIndex,
520                        const FunctionImporter::ImportMapTy &ImportList,
521                        const GVSummaryMapTy &DefinedGlobals,
522                        MapVector<StringRef, BitcodeModule> *ModuleMap,
523                        const std::vector<uint8_t> &CmdArgs) {
524   Expected<const Target *> TOrErr = initAndLookupTarget(Conf, Mod);
525   if (!TOrErr)
526     return TOrErr.takeError();
527 
528   std::unique_ptr<TargetMachine> TM = createTargetMachine(Conf, *TOrErr, Mod);
529 
530   // Setup optimization remarks.
531   auto DiagFileOrErr = lto::setupLLVMOptimizationRemarks(
532       Mod.getContext(), Conf.RemarksFilename, Conf.RemarksPasses,
533       Conf.RemarksFormat, Conf.RemarksWithHotness, Conf.RemarksHotnessThreshold,
534       Task);
535   if (!DiagFileOrErr)
536     return DiagFileOrErr.takeError();
537   auto DiagnosticOutputFile = std::move(*DiagFileOrErr);
538 
539   // Set the partial sample profile ratio in the profile summary module flag of
540   // the module, if applicable.
541   Mod.setPartialSampleProfileRatio(CombinedIndex);
542 
543   if (Conf.CodeGenOnly) {
544     codegen(Conf, TM.get(), AddStream, Task, Mod, CombinedIndex);
545     return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
546   }
547 
548   if (Conf.PreOptModuleHook && !Conf.PreOptModuleHook(Task, Mod))
549     return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
550 
551   auto OptimizeAndCodegen =
552       [&](Module &Mod, TargetMachine *TM,
553           std::unique_ptr<ToolOutputFile> DiagnosticOutputFile) {
554         if (!opt(Conf, TM, Task, Mod, /*IsThinLTO=*/true,
555                  /*ExportSummary=*/nullptr, /*ImportSummary=*/&CombinedIndex,
556                  CmdArgs))
557           return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
558 
559         codegen(Conf, TM, AddStream, Task, Mod, CombinedIndex);
560         return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
561       };
562 
563   if (ThinLTOAssumeMerged)
564     return OptimizeAndCodegen(Mod, TM.get(), std::move(DiagnosticOutputFile));
565 
566   // When linking an ELF shared object, dso_local should be dropped. We
567   // conservatively do this for -fpic.
568   bool ClearDSOLocalOnDeclarations =
569       TM->getTargetTriple().isOSBinFormatELF() &&
570       TM->getRelocationModel() != Reloc::Static &&
571       Mod.getPIELevel() == PIELevel::Default;
572   renameModuleForThinLTO(Mod, CombinedIndex, ClearDSOLocalOnDeclarations);
573 
574   dropDeadSymbols(Mod, DefinedGlobals, CombinedIndex);
575 
576   thinLTOFinalizeInModule(Mod, DefinedGlobals, /*PropagateAttrs=*/true);
577 
578   if (Conf.PostPromoteModuleHook && !Conf.PostPromoteModuleHook(Task, Mod))
579     return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
580 
581   if (!DefinedGlobals.empty())
582     thinLTOInternalizeModule(Mod, DefinedGlobals);
583 
584   if (Conf.PostInternalizeModuleHook &&
585       !Conf.PostInternalizeModuleHook(Task, Mod))
586     return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
587 
588   auto ModuleLoader = [&](StringRef Identifier) {
589     assert(Mod.getContext().isODRUniquingDebugTypes() &&
590            "ODR Type uniquing should be enabled on the context");
591     if (ModuleMap) {
592       auto I = ModuleMap->find(Identifier);
593       assert(I != ModuleMap->end());
594       return I->second.getLazyModule(Mod.getContext(),
595                                      /*ShouldLazyLoadMetadata=*/true,
596                                      /*IsImporting*/ true);
597     }
598 
599     ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> MBOrErr =
600         llvm::MemoryBuffer::getFile(Identifier);
601     if (!MBOrErr)
602       return Expected<std::unique_ptr<llvm::Module>>(make_error<StringError>(
603           Twine("Error loading imported file ") + Identifier + " : ",
604           MBOrErr.getError()));
605 
606     Expected<BitcodeModule> BMOrErr = findThinLTOModule(**MBOrErr);
607     if (!BMOrErr)
608       return Expected<std::unique_ptr<llvm::Module>>(make_error<StringError>(
609           Twine("Error loading imported file ") + Identifier + " : " +
610               toString(BMOrErr.takeError()),
611           inconvertibleErrorCode()));
612 
613     Expected<std::unique_ptr<Module>> MOrErr =
614         BMOrErr->getLazyModule(Mod.getContext(),
615                                /*ShouldLazyLoadMetadata=*/true,
616                                /*IsImporting*/ true);
617     if (MOrErr)
618       (*MOrErr)->setOwnedMemoryBuffer(std::move(*MBOrErr));
619     return MOrErr;
620   };
621 
622   FunctionImporter Importer(CombinedIndex, ModuleLoader,
623                             ClearDSOLocalOnDeclarations);
624   if (Error Err = Importer.importFunctions(Mod, ImportList).takeError())
625     return Err;
626 
627   if (Conf.PostImportModuleHook && !Conf.PostImportModuleHook(Task, Mod))
628     return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
629 
630   return OptimizeAndCodegen(Mod, TM.get(), std::move(DiagnosticOutputFile));
631 }
632 
633 BitcodeModule *lto::findThinLTOModule(MutableArrayRef<BitcodeModule> BMs) {
634   if (ThinLTOAssumeMerged && BMs.size() == 1)
635     return BMs.begin();
636 
637   for (BitcodeModule &BM : BMs) {
638     Expected<BitcodeLTOInfo> LTOInfo = BM.getLTOInfo();
639     if (LTOInfo && LTOInfo->IsThinLTO)
640       return &BM;
641   }
642   return nullptr;
643 }
644 
645 Expected<BitcodeModule> lto::findThinLTOModule(MemoryBufferRef MBRef) {
646   Expected<std::vector<BitcodeModule>> BMsOrErr = getBitcodeModuleList(MBRef);
647   if (!BMsOrErr)
648     return BMsOrErr.takeError();
649 
650   // The bitcode file may contain multiple modules, we want the one that is
651   // marked as being the ThinLTO module.
652   if (const BitcodeModule *Bm = lto::findThinLTOModule(*BMsOrErr))
653     return *Bm;
654 
655   return make_error<StringError>("Could not find module summary",
656                                  inconvertibleErrorCode());
657 }
658 
659 bool lto::initImportList(const Module &M,
660                          const ModuleSummaryIndex &CombinedIndex,
661                          FunctionImporter::ImportMapTy &ImportList) {
662   if (ThinLTOAssumeMerged)
663     return true;
664   // We can simply import the values mentioned in the combined index, since
665   // we should only invoke this using the individual indexes written out
666   // via a WriteIndexesThinBackend.
667   for (const auto &GlobalList : CombinedIndex) {
668     // Ignore entries for undefined references.
669     if (GlobalList.second.SummaryList.empty())
670       continue;
671 
672     auto GUID = GlobalList.first;
673     for (const auto &Summary : GlobalList.second.SummaryList) {
674       // Skip the summaries for the importing module. These are included to
675       // e.g. record required linkage changes.
676       if (Summary->modulePath() == M.getModuleIdentifier())
677         continue;
678       // Add an entry to provoke importing by thinBackend.
679       ImportList[Summary->modulePath()].insert(GUID);
680     }
681   }
682   return true;
683 }
684