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