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/Analysis/TargetTransformInfo.h"
22 #include "llvm/Bitcode/BitcodeReader.h"
23 #include "llvm/Bitcode/BitcodeWriter.h"
24 #include "llvm/IR/LLVMRemarkStreamer.h"
25 #include "llvm/IR/LegacyPassManager.h"
26 #include "llvm/IR/PassManager.h"
27 #include "llvm/IR/Verifier.h"
28 #include "llvm/LTO/LTO.h"
29 #include "llvm/MC/SubtargetFeature.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/SmallVectorMemoryBuffer.h"
40 #include "llvm/Support/TargetRegistry.h"
41 #include "llvm/Support/ThreadPool.h"
42 #include "llvm/Support/raw_ostream.h"
43 #include "llvm/Target/TargetMachine.h"
44 #include "llvm/Transforms/IPO.h"
45 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
46 #include "llvm/Transforms/Scalar/LoopPassManager.h"
47 #include "llvm/Transforms/Utils/FunctionImportUtils.h"
48 #include "llvm/Transforms/Utils/SplitModule.h"
49 
50 using namespace llvm;
51 using namespace lto;
52 
53 LLVM_ATTRIBUTE_NORETURN static void reportOpenError(StringRef Path, Twine Msg) {
54   errs() << "failed to open " << Path << ": " << Msg << '\n';
55   errs().flush();
56   exit(1);
57 }
58 
59 Error Config::addSaveTemps(std::string OutputFileName,
60                            bool UseInputModulePath) {
61   ShouldDiscardValueNames = false;
62 
63   std::error_code EC;
64   ResolutionFile = std::make_unique<raw_fd_ostream>(
65       OutputFileName + "resolution.txt", EC, sys::fs::OpenFlags::OF_Text);
66   if (EC) {
67     ResolutionFile.reset();
68     return errorCodeToError(EC);
69   }
70 
71   auto setHook = [&](std::string PathSuffix, ModuleHookFn &Hook) {
72     // Keep track of the hook provided by the linker, which also needs to run.
73     ModuleHookFn LinkerHook = Hook;
74     Hook = [=](unsigned Task, const Module &M) {
75       // If the linker's hook returned false, we need to pass that result
76       // through.
77       if (LinkerHook && !LinkerHook(Task, M))
78         return false;
79 
80       std::string PathPrefix;
81       // If this is the combined module (not a ThinLTO backend compile) or the
82       // user hasn't requested using the input module's path, emit to a file
83       // named from the provided OutputFileName with the Task ID appended.
84       if (M.getModuleIdentifier() == "ld-temp.o" || !UseInputModulePath) {
85         PathPrefix = OutputFileName;
86         if (Task != (unsigned)-1)
87           PathPrefix += utostr(Task) + ".";
88       } else
89         PathPrefix = M.getModuleIdentifier() + ".";
90       std::string Path = PathPrefix + PathSuffix + ".bc";
91       std::error_code EC;
92       raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::OF_None);
93       // Because -save-temps is a debugging feature, we report the error
94       // directly and exit.
95       if (EC)
96         reportOpenError(Path, EC.message());
97       WriteBitcodeToFile(M, OS, /*ShouldPreserveUseListOrder=*/false);
98       return true;
99     };
100   };
101 
102   setHook("0.preopt", PreOptModuleHook);
103   setHook("1.promote", PostPromoteModuleHook);
104   setHook("2.internalize", PostInternalizeModuleHook);
105   setHook("3.import", PostImportModuleHook);
106   setHook("4.opt", PostOptModuleHook);
107   setHook("5.precodegen", PreCodeGenModuleHook);
108 
109   CombinedIndexHook =
110       [=](const ModuleSummaryIndex &Index,
111           const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
112         std::string Path = OutputFileName + "index.bc";
113         std::error_code EC;
114         raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::OF_None);
115         // Because -save-temps is a debugging feature, we report the error
116         // directly and exit.
117         if (EC)
118           reportOpenError(Path, EC.message());
119         WriteIndexToFile(Index, OS);
120 
121         Path = OutputFileName + "index.dot";
122         raw_fd_ostream OSDot(Path, EC, sys::fs::OpenFlags::OF_None);
123         if (EC)
124           reportOpenError(Path, EC.message());
125         Index.exportToDot(OSDot, GUIDPreservedSymbols);
126         return true;
127       };
128 
129   return Error::success();
130 }
131 
132 #define HANDLE_EXTENSION(Ext)                                                  \
133   llvm::PassPluginLibraryInfo get##Ext##PluginInfo();
134 #include "llvm/Support/Extension.def"
135 
136 static void RegisterPassPlugins(ArrayRef<std::string> PassPlugins,
137                                 PassBuilder &PB) {
138 #define HANDLE_EXTENSION(Ext)                                                  \
139   get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB);
140 #include "llvm/Support/Extension.def"
141 
142   // Load requested pass plugins and let them register pass builder callbacks
143   for (auto &PluginFN : PassPlugins) {
144     auto PassPlugin = PassPlugin::Load(PluginFN);
145     if (!PassPlugin) {
146       errs() << "Failed to load passes from '" << PluginFN
147              << "'. Request ignored.\n";
148       continue;
149     }
150 
151     PassPlugin->registerPassBuilderCallbacks(PB);
152   }
153 }
154 
155 namespace {
156 
157 std::unique_ptr<TargetMachine>
158 createTargetMachine(const Config &Conf, const Target *TheTarget, Module &M) {
159   StringRef TheTriple = M.getTargetTriple();
160   SubtargetFeatures Features;
161   Features.getDefaultSubtargetFeatures(Triple(TheTriple));
162   for (const std::string &A : Conf.MAttrs)
163     Features.AddFeature(A);
164 
165   Reloc::Model RelocModel;
166   if (Conf.RelocModel)
167     RelocModel = *Conf.RelocModel;
168   else
169     RelocModel =
170         M.getPICLevel() == PICLevel::NotPIC ? Reloc::Static : Reloc::PIC_;
171 
172   Optional<CodeModel::Model> CodeModel;
173   if (Conf.CodeModel)
174     CodeModel = *Conf.CodeModel;
175   else
176     CodeModel = M.getCodeModel();
177 
178   return std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(
179       TheTriple, Conf.CPU, Features.getString(), Conf.Options, RelocModel,
180       CodeModel, Conf.CGOptLevel));
181 }
182 
183 static void runNewPMPasses(const Config &Conf, Module &Mod, TargetMachine *TM,
184                            unsigned OptLevel, bool IsThinLTO,
185                            ModuleSummaryIndex *ExportSummary,
186                            const ModuleSummaryIndex *ImportSummary) {
187   Optional<PGOOptions> PGOOpt;
188   if (!Conf.SampleProfile.empty())
189     PGOOpt = PGOOptions(Conf.SampleProfile, "", Conf.ProfileRemapping,
190                         PGOOptions::SampleUse, PGOOptions::NoCSAction, true);
191   else if (Conf.RunCSIRInstr) {
192     PGOOpt = PGOOptions("", Conf.CSIRProfile, Conf.ProfileRemapping,
193                         PGOOptions::IRUse, PGOOptions::CSIRInstr);
194   } else if (!Conf.CSIRProfile.empty()) {
195     PGOOpt = PGOOptions(Conf.CSIRProfile, "", Conf.ProfileRemapping,
196                         PGOOptions::IRUse, PGOOptions::CSIRUse);
197   }
198 
199   PassInstrumentationCallbacks PIC;
200   StandardInstrumentations SI(Conf.DebugPassManager);
201   SI.registerCallbacks(PIC);
202   PassBuilder PB(TM, Conf.PTO, PGOOpt, &PIC);
203   AAManager AA;
204 
205   // Parse a custom AA pipeline if asked to.
206   if (auto Err = PB.parseAAPipeline(AA, "default"))
207     report_fatal_error("Error parsing default AA pipeline");
208 
209   RegisterPassPlugins(Conf.PassPlugins, PB);
210 
211   LoopAnalysisManager LAM(Conf.DebugPassManager);
212   FunctionAnalysisManager FAM(Conf.DebugPassManager);
213   CGSCCAnalysisManager CGAM(Conf.DebugPassManager);
214   ModuleAnalysisManager MAM(Conf.DebugPassManager);
215 
216   // Register the AA manager first so that our version is the one used.
217   FAM.registerPass([&] { return std::move(AA); });
218 
219   // Register all the basic analyses with the managers.
220   PB.registerModuleAnalyses(MAM);
221   PB.registerCGSCCAnalyses(CGAM);
222   PB.registerFunctionAnalyses(FAM);
223   PB.registerLoopAnalyses(LAM);
224   PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
225 
226   ModulePassManager MPM(Conf.DebugPassManager);
227   // FIXME (davide): verify the input.
228 
229   PassBuilder::OptimizationLevel OL;
230 
231   switch (OptLevel) {
232   default:
233     llvm_unreachable("Invalid optimization level");
234   case 0:
235     OL = PassBuilder::OptimizationLevel::O0;
236     break;
237   case 1:
238     OL = PassBuilder::OptimizationLevel::O1;
239     break;
240   case 2:
241     OL = PassBuilder::OptimizationLevel::O2;
242     break;
243   case 3:
244     OL = PassBuilder::OptimizationLevel::O3;
245     break;
246   }
247 
248   if (IsThinLTO)
249     MPM = PB.buildThinLTODefaultPipeline(OL, Conf.DebugPassManager,
250                                          ImportSummary);
251   else
252     MPM = PB.buildLTODefaultPipeline(OL, Conf.DebugPassManager, ExportSummary);
253   MPM.run(Mod, MAM);
254 
255   // FIXME (davide): verify the output.
256 }
257 
258 static void runNewPMCustomPasses(const Config &Conf, Module &Mod,
259                                  TargetMachine *TM, std::string PipelineDesc,
260                                  std::string AAPipelineDesc,
261                                  bool DisableVerify) {
262   PassBuilder PB(TM);
263   AAManager AA;
264 
265   // Parse a custom AA pipeline if asked to.
266   if (!AAPipelineDesc.empty())
267     if (auto Err = PB.parseAAPipeline(AA, AAPipelineDesc))
268       report_fatal_error("unable to parse AA pipeline description '" +
269                          AAPipelineDesc + "': " + toString(std::move(Err)));
270 
271   RegisterPassPlugins(Conf.PassPlugins, PB);
272 
273   LoopAnalysisManager LAM;
274   FunctionAnalysisManager FAM;
275   CGSCCAnalysisManager CGAM;
276   ModuleAnalysisManager MAM;
277 
278   // Register the AA manager first so that our version is the one used.
279   FAM.registerPass([&] { return std::move(AA); });
280 
281   // Register all the basic analyses with the managers.
282   PB.registerModuleAnalyses(MAM);
283   PB.registerCGSCCAnalyses(CGAM);
284   PB.registerFunctionAnalyses(FAM);
285   PB.registerLoopAnalyses(LAM);
286   PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
287 
288   ModulePassManager MPM;
289 
290   // Always verify the input.
291   MPM.addPass(VerifierPass());
292 
293   // Now, add all the passes we've been requested to.
294   if (auto Err = PB.parsePassPipeline(MPM, PipelineDesc))
295     report_fatal_error("unable to parse pass pipeline description '" +
296                        PipelineDesc + "': " + toString(std::move(Err)));
297 
298   if (!DisableVerify)
299     MPM.addPass(VerifierPass());
300   MPM.run(Mod, MAM);
301 }
302 
303 static void runOldPMPasses(const Config &Conf, Module &Mod, TargetMachine *TM,
304                            bool IsThinLTO, ModuleSummaryIndex *ExportSummary,
305                            const ModuleSummaryIndex *ImportSummary) {
306   legacy::PassManager passes;
307   passes.add(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis()));
308 
309   PassManagerBuilder PMB;
310   PMB.LibraryInfo = new TargetLibraryInfoImpl(Triple(TM->getTargetTriple()));
311   PMB.Inliner = createFunctionInliningPass();
312   PMB.ExportSummary = ExportSummary;
313   PMB.ImportSummary = ImportSummary;
314   // Unconditionally verify input since it is not verified before this
315   // point and has unknown origin.
316   PMB.VerifyInput = true;
317   PMB.VerifyOutput = !Conf.DisableVerify;
318   PMB.LoopVectorize = true;
319   PMB.SLPVectorize = true;
320   PMB.OptLevel = Conf.OptLevel;
321   PMB.PGOSampleUse = Conf.SampleProfile;
322   PMB.EnablePGOCSInstrGen = Conf.RunCSIRInstr;
323   if (!Conf.RunCSIRInstr && !Conf.CSIRProfile.empty()) {
324     PMB.EnablePGOCSInstrUse = true;
325     PMB.PGOInstrUse = Conf.CSIRProfile;
326   }
327   if (IsThinLTO)
328     PMB.populateThinLTOPassManager(passes);
329   else
330     PMB.populateLTOPassManager(passes);
331   passes.run(Mod);
332 }
333 
334 bool opt(const Config &Conf, TargetMachine *TM, unsigned Task, Module &Mod,
335          bool IsThinLTO, ModuleSummaryIndex *ExportSummary,
336          const ModuleSummaryIndex *ImportSummary) {
337   // FIXME: Plumb the combined index into the new pass manager.
338   if (!Conf.OptPipeline.empty())
339     runNewPMCustomPasses(Conf, Mod, TM, Conf.OptPipeline, Conf.AAPipeline,
340                          Conf.DisableVerify);
341   else if (Conf.UseNewPM)
342     runNewPMPasses(Conf, Mod, TM, Conf.OptLevel, IsThinLTO, ExportSummary,
343                    ImportSummary);
344   else
345     runOldPMPasses(Conf, Mod, TM, IsThinLTO, ExportSummary, ImportSummary);
346   return !Conf.PostOptModuleHook || Conf.PostOptModuleHook(Task, Mod);
347 }
348 
349 static cl::opt<bool> EmbedBitcode(
350     "lto-embed-bitcode", cl::init(false),
351     cl::desc("Embed LLVM bitcode in object files produced by LTO"));
352 
353 static void EmitBitcodeSection(Module &M) {
354   if (!EmbedBitcode)
355     return;
356   llvm::EmbedBitcodeInModule(M, llvm::MemoryBufferRef(), /*EmbedBitcode*/ true,
357                              /*EmbedMarker*/ false, /*CmdArgs*/ nullptr);
358 }
359 
360 void codegen(const Config &Conf, TargetMachine *TM, AddStreamFn AddStream,
361              unsigned Task, Module &Mod,
362              const ModuleSummaryIndex &CombinedIndex) {
363   if (Conf.PreCodeGenModuleHook && !Conf.PreCodeGenModuleHook(Task, Mod))
364     return;
365 
366   EmitBitcodeSection(Mod);
367 
368   std::unique_ptr<ToolOutputFile> DwoOut;
369   SmallString<1024> DwoFile(Conf.SplitDwarfOutput);
370   if (!Conf.DwoDir.empty()) {
371     std::error_code EC;
372     if (auto EC = llvm::sys::fs::create_directories(Conf.DwoDir))
373       report_fatal_error("Failed to create directory " + Conf.DwoDir + ": " +
374                          EC.message());
375 
376     DwoFile = Conf.DwoDir;
377     sys::path::append(DwoFile, std::to_string(Task) + ".dwo");
378     TM->Options.MCOptions.SplitDwarfFile = std::string(DwoFile);
379   } else
380     TM->Options.MCOptions.SplitDwarfFile = Conf.SplitDwarfFile;
381 
382   if (!DwoFile.empty()) {
383     std::error_code EC;
384     DwoOut = std::make_unique<ToolOutputFile>(DwoFile, EC, sys::fs::OF_None);
385     if (EC)
386       report_fatal_error("Failed to open " + DwoFile + ": " + EC.message());
387   }
388 
389   auto Stream = AddStream(Task);
390   legacy::PassManager CodeGenPasses;
391   CodeGenPasses.add(
392       createImmutableModuleSummaryIndexWrapperPass(&CombinedIndex));
393   if (TM->addPassesToEmitFile(CodeGenPasses, *Stream->OS,
394                               DwoOut ? &DwoOut->os() : nullptr,
395                               Conf.CGFileType))
396     report_fatal_error("Failed to setup codegen");
397   CodeGenPasses.run(Mod);
398 
399   if (DwoOut)
400     DwoOut->keep();
401 }
402 
403 void splitCodeGen(const Config &C, TargetMachine *TM, AddStreamFn AddStream,
404                   unsigned ParallelCodeGenParallelismLevel,
405                   std::unique_ptr<Module> Mod,
406                   const ModuleSummaryIndex &CombinedIndex) {
407   ThreadPool CodegenThreadPool(
408       heavyweight_hardware_concurrency(ParallelCodeGenParallelismLevel));
409   unsigned ThreadCount = 0;
410   const Target *T = &TM->getTarget();
411 
412   SplitModule(
413       std::move(Mod), ParallelCodeGenParallelismLevel,
414       [&](std::unique_ptr<Module> MPart) {
415         // We want to clone the module in a new context to multi-thread the
416         // codegen. We do it by serializing partition modules to bitcode
417         // (while still on the main thread, in order to avoid data races) and
418         // spinning up new threads which deserialize the partitions into
419         // separate contexts.
420         // FIXME: Provide a more direct way to do this in LLVM.
421         SmallString<0> BC;
422         raw_svector_ostream BCOS(BC);
423         WriteBitcodeToFile(*MPart, BCOS);
424 
425         // Enqueue the task
426         CodegenThreadPool.async(
427             [&](const SmallString<0> &BC, unsigned ThreadId) {
428               LTOLLVMContext Ctx(C);
429               Expected<std::unique_ptr<Module>> MOrErr = parseBitcodeFile(
430                   MemoryBufferRef(StringRef(BC.data(), BC.size()), "ld-temp.o"),
431                   Ctx);
432               if (!MOrErr)
433                 report_fatal_error("Failed to read bitcode");
434               std::unique_ptr<Module> MPartInCtx = std::move(MOrErr.get());
435 
436               std::unique_ptr<TargetMachine> TM =
437                   createTargetMachine(C, T, *MPartInCtx);
438 
439               codegen(C, TM.get(), AddStream, ThreadId, *MPartInCtx,
440                       CombinedIndex);
441             },
442             // Pass BC using std::move to ensure that it get moved rather than
443             // copied into the thread's context.
444             std::move(BC), ThreadCount++);
445       },
446       false);
447 
448   // Because the inner lambda (which runs in a worker thread) captures our local
449   // variables, we need to wait for the worker threads to terminate before we
450   // can leave the function scope.
451   CodegenThreadPool.wait();
452 }
453 
454 Expected<const Target *> initAndLookupTarget(const Config &C, Module &Mod) {
455   if (!C.OverrideTriple.empty())
456     Mod.setTargetTriple(C.OverrideTriple);
457   else if (Mod.getTargetTriple().empty())
458     Mod.setTargetTriple(C.DefaultTriple);
459 
460   std::string Msg;
461   const Target *T = TargetRegistry::lookupTarget(Mod.getTargetTriple(), Msg);
462   if (!T)
463     return make_error<StringError>(Msg, inconvertibleErrorCode());
464   return T;
465 }
466 }
467 
468 Error lto::finalizeOptimizationRemarks(
469     std::unique_ptr<ToolOutputFile> DiagOutputFile) {
470   // Make sure we flush the diagnostic remarks file in case the linker doesn't
471   // call the global destructors before exiting.
472   if (!DiagOutputFile)
473     return Error::success();
474   DiagOutputFile->keep();
475   DiagOutputFile->os().flush();
476   return Error::success();
477 }
478 
479 Error lto::backend(const Config &C, AddStreamFn AddStream,
480                    unsigned ParallelCodeGenParallelismLevel,
481                    std::unique_ptr<Module> Mod,
482                    ModuleSummaryIndex &CombinedIndex) {
483   Expected<const Target *> TOrErr = initAndLookupTarget(C, *Mod);
484   if (!TOrErr)
485     return TOrErr.takeError();
486 
487   std::unique_ptr<TargetMachine> TM = createTargetMachine(C, *TOrErr, *Mod);
488 
489   if (!C.CodeGenOnly) {
490     if (!opt(C, TM.get(), 0, *Mod, /*IsThinLTO=*/false,
491              /*ExportSummary=*/&CombinedIndex, /*ImportSummary=*/nullptr))
492       return Error::success();
493   }
494 
495   if (ParallelCodeGenParallelismLevel == 1) {
496     codegen(C, TM.get(), AddStream, 0, *Mod, CombinedIndex);
497   } else {
498     splitCodeGen(C, TM.get(), AddStream, ParallelCodeGenParallelismLevel,
499                  std::move(Mod), CombinedIndex);
500   }
501   return Error::success();
502 }
503 
504 static void dropDeadSymbols(Module &Mod, const GVSummaryMapTy &DefinedGlobals,
505                             const ModuleSummaryIndex &Index) {
506   std::vector<GlobalValue*> DeadGVs;
507   for (auto &GV : Mod.global_values())
508     if (GlobalValueSummary *GVS = DefinedGlobals.lookup(GV.getGUID()))
509       if (!Index.isGlobalValueLive(GVS)) {
510         DeadGVs.push_back(&GV);
511         convertToDeclaration(GV);
512       }
513 
514   // Now that all dead bodies have been dropped, delete the actual objects
515   // themselves when possible.
516   for (GlobalValue *GV : DeadGVs) {
517     GV->removeDeadConstantUsers();
518     // Might reference something defined in native object (i.e. dropped a
519     // non-prevailing IR def, but we need to keep the declaration).
520     if (GV->use_empty())
521       GV->eraseFromParent();
522   }
523 }
524 
525 Error lto::thinBackend(const Config &Conf, unsigned Task, AddStreamFn AddStream,
526                        Module &Mod, const ModuleSummaryIndex &CombinedIndex,
527                        const FunctionImporter::ImportMapTy &ImportList,
528                        const GVSummaryMapTy &DefinedGlobals,
529                        MapVector<StringRef, BitcodeModule> &ModuleMap) {
530   Expected<const Target *> TOrErr = initAndLookupTarget(Conf, Mod);
531   if (!TOrErr)
532     return TOrErr.takeError();
533 
534   std::unique_ptr<TargetMachine> TM = createTargetMachine(Conf, *TOrErr, Mod);
535 
536   // Setup optimization remarks.
537   auto DiagFileOrErr = lto::setupLLVMOptimizationRemarks(
538       Mod.getContext(), Conf.RemarksFilename, Conf.RemarksPasses,
539       Conf.RemarksFormat, Conf.RemarksWithHotness, Task);
540   if (!DiagFileOrErr)
541     return DiagFileOrErr.takeError();
542   auto DiagnosticOutputFile = std::move(*DiagFileOrErr);
543 
544   // Set the partial sample profile ratio in the profile summary module flag of
545   // the module, if applicable.
546   Mod.setPartialSampleProfileRatio(CombinedIndex);
547 
548   if (Conf.CodeGenOnly) {
549     codegen(Conf, TM.get(), AddStream, Task, Mod, CombinedIndex);
550     return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
551   }
552 
553   if (Conf.PreOptModuleHook && !Conf.PreOptModuleHook(Task, Mod))
554     return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
555 
556   // When linking an ELF shared object, dso_local should be dropped. We
557   // conservatively do this for -fpic.
558   bool ClearDSOLocalOnDeclarations =
559       TM->getTargetTriple().isOSBinFormatELF() &&
560       TM->getRelocationModel() != Reloc::Static &&
561       Mod.getPIELevel() == PIELevel::Default;
562   renameModuleForThinLTO(Mod, CombinedIndex, ClearDSOLocalOnDeclarations);
563 
564   dropDeadSymbols(Mod, DefinedGlobals, CombinedIndex);
565 
566   thinLTOResolvePrevailingInModule(Mod, DefinedGlobals);
567 
568   if (Conf.PostPromoteModuleHook && !Conf.PostPromoteModuleHook(Task, Mod))
569     return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
570 
571   if (!DefinedGlobals.empty())
572     thinLTOInternalizeModule(Mod, DefinedGlobals);
573 
574   if (Conf.PostInternalizeModuleHook &&
575       !Conf.PostInternalizeModuleHook(Task, Mod))
576     return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
577 
578   auto ModuleLoader = [&](StringRef Identifier) {
579     assert(Mod.getContext().isODRUniquingDebugTypes() &&
580            "ODR Type uniquing should be enabled on the context");
581     auto I = ModuleMap.find(Identifier);
582     assert(I != ModuleMap.end());
583     return I->second.getLazyModule(Mod.getContext(),
584                                    /*ShouldLazyLoadMetadata=*/true,
585                                    /*IsImporting*/ true);
586   };
587 
588   FunctionImporter Importer(CombinedIndex, ModuleLoader,
589                             ClearDSOLocalOnDeclarations);
590   if (Error Err = Importer.importFunctions(Mod, ImportList).takeError())
591     return Err;
592 
593   if (Conf.PostImportModuleHook && !Conf.PostImportModuleHook(Task, Mod))
594     return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
595 
596   if (!opt(Conf, TM.get(), Task, Mod, /*IsThinLTO=*/true,
597            /*ExportSummary=*/nullptr, /*ImportSummary=*/&CombinedIndex))
598     return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
599 
600   codegen(Conf, TM.get(), AddStream, Task, Mod, CombinedIndex);
601   return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
602 }
603