xref: /llvm-project-15.0.7/llvm/lib/LTO/LTO.cpp (revision ee6d3fa0)
1 //===-LTO.cpp - LLVM Link Time Optimizer ----------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements functions and classes used to support LTO.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/LTO/LTO.h"
15 #include "llvm/Analysis/TargetLibraryInfo.h"
16 #include "llvm/Analysis/TargetTransformInfo.h"
17 #include "llvm/Bitcode/BitcodeReader.h"
18 #include "llvm/Bitcode/BitcodeWriter.h"
19 #include "llvm/CodeGen/Analysis.h"
20 #include "llvm/IR/AutoUpgrade.h"
21 #include "llvm/IR/DiagnosticPrinter.h"
22 #include "llvm/IR/LegacyPassManager.h"
23 #include "llvm/LTO/LTOBackend.h"
24 #include "llvm/Linker/IRMover.h"
25 #include "llvm/Object/ModuleSummaryIndexObjectFile.h"
26 #include "llvm/Support/ManagedStatic.h"
27 #include "llvm/Support/MemoryBuffer.h"
28 #include "llvm/Support/Path.h"
29 #include "llvm/Support/SHA1.h"
30 #include "llvm/Support/SourceMgr.h"
31 #include "llvm/Support/TargetRegistry.h"
32 #include "llvm/Support/ThreadPool.h"
33 #include "llvm/Support/Threading.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include "llvm/Target/TargetMachine.h"
36 #include "llvm/Target/TargetOptions.h"
37 #include "llvm/Transforms/IPO.h"
38 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
39 #include "llvm/Transforms/Utils/SplitModule.h"
40 
41 #include <set>
42 
43 using namespace llvm;
44 using namespace lto;
45 using namespace object;
46 
47 #define DEBUG_TYPE "lto"
48 
49 // Returns a unique hash for the Module considering the current list of
50 // export/import and other global analysis results.
51 // The hash is produced in \p Key.
52 static void computeCacheKey(
53     SmallString<40> &Key, const ModuleSummaryIndex &Index, StringRef ModuleID,
54     const FunctionImporter::ImportMapTy &ImportList,
55     const FunctionImporter::ExportSetTy &ExportList,
56     const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
57     const GVSummaryMapTy &DefinedGlobals) {
58   // Compute the unique hash for this entry.
59   // This is based on the current compiler version, the module itself, the
60   // export list, the hash for every single module in the import list, the
61   // list of ResolvedODR for the module, and the list of preserved symbols.
62   SHA1 Hasher;
63 
64   // Start with the compiler revision
65   Hasher.update(LLVM_VERSION_STRING);
66 #ifdef HAVE_LLVM_REVISION
67   Hasher.update(LLVM_REVISION);
68 #endif
69 
70   // Include the hash for the current module
71   auto ModHash = Index.getModuleHash(ModuleID);
72   Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
73   for (auto F : ExportList)
74     // The export list can impact the internalization, be conservative here
75     Hasher.update(ArrayRef<uint8_t>((uint8_t *)&F, sizeof(F)));
76 
77   // Include the hash for every module we import functions from
78   for (auto &Entry : ImportList) {
79     auto ModHash = Index.getModuleHash(Entry.first());
80     Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
81   }
82 
83   // Include the hash for the resolved ODR.
84   for (auto &Entry : ResolvedODR) {
85     Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.first,
86                                     sizeof(GlobalValue::GUID)));
87     Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.second,
88                                     sizeof(GlobalValue::LinkageTypes)));
89   }
90 
91   // Include the hash for the linkage type to reflect internalization and weak
92   // resolution.
93   for (auto &GS : DefinedGlobals) {
94     GlobalValue::LinkageTypes Linkage = GS.second->linkage();
95     Hasher.update(
96         ArrayRef<uint8_t>((const uint8_t *)&Linkage, sizeof(Linkage)));
97   }
98 
99   Key = toHex(Hasher.result());
100 }
101 
102 // Simple helper to load a module from bitcode
103 std::unique_ptr<Module>
104 llvm::loadModuleFromBuffer(const MemoryBufferRef &Buffer, LLVMContext &Context,
105                            bool Lazy) {
106   SMDiagnostic Err;
107   Expected<std::unique_ptr<Module>> ModuleOrErr =
108       Lazy ? getLazyBitcodeModule(Buffer, Context,
109                                   /* ShouldLazyLoadMetadata */ true)
110            : parseBitcodeFile(Buffer, Context);
111   if (!ModuleOrErr) {
112     handleAllErrors(ModuleOrErr.takeError(), [&](ErrorInfoBase &EIB) {
113       SMDiagnostic Err = SMDiagnostic(Buffer.getBufferIdentifier(),
114                                       SourceMgr::DK_Error, EIB.message());
115       Err.print("ThinLTO", errs());
116     });
117     report_fatal_error("Can't load module, abort.");
118   }
119   return std::move(ModuleOrErr.get());
120 }
121 
122 static void thinLTOResolveWeakForLinkerGUID(
123     GlobalValueSummaryList &GVSummaryList, GlobalValue::GUID GUID,
124     DenseSet<GlobalValueSummary *> &GlobalInvolvedWithAlias,
125     function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
126         isPrevailing,
127     function_ref<void(StringRef, GlobalValue::GUID, GlobalValue::LinkageTypes)>
128         recordNewLinkage) {
129   for (auto &S : GVSummaryList) {
130     GlobalValue::LinkageTypes OriginalLinkage = S->linkage();
131     if (!GlobalValue::isWeakForLinker(OriginalLinkage))
132       continue;
133     // We need to emit only one of these. The prevailing module will keep it,
134     // but turned into a weak, while the others will drop it when possible.
135     // This is both a compile-time optimization and a correctness
136     // transformation. This is necessary for correctness when we have exported
137     // a reference - we need to convert the linkonce to weak to
138     // ensure a copy is kept to satisfy the exported reference.
139     // FIXME: We may want to split the compile time and correctness
140     // aspects into separate routines.
141     if (isPrevailing(GUID, S.get())) {
142       if (GlobalValue::isLinkOnceLinkage(OriginalLinkage))
143         S->setLinkage(GlobalValue::getWeakLinkage(
144             GlobalValue::isLinkOnceODRLinkage(OriginalLinkage)));
145     }
146     // Alias and aliasee can't be turned into available_externally.
147     else if (!isa<AliasSummary>(S.get()) &&
148              !GlobalInvolvedWithAlias.count(S.get()) &&
149              (GlobalValue::isLinkOnceODRLinkage(OriginalLinkage) ||
150               GlobalValue::isWeakODRLinkage(OriginalLinkage)))
151       S->setLinkage(GlobalValue::AvailableExternallyLinkage);
152     if (S->linkage() != OriginalLinkage)
153       recordNewLinkage(S->modulePath(), GUID, S->linkage());
154   }
155 }
156 
157 // Resolve Weak and LinkOnce values in the \p Index.
158 //
159 // We'd like to drop these functions if they are no longer referenced in the
160 // current module. However there is a chance that another module is still
161 // referencing them because of the import. We make sure we always emit at least
162 // one copy.
163 void llvm::thinLTOResolveWeakForLinkerInIndex(
164     ModuleSummaryIndex &Index,
165     function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
166         isPrevailing,
167     function_ref<void(StringRef, GlobalValue::GUID, GlobalValue::LinkageTypes)>
168         recordNewLinkage) {
169   // We won't optimize the globals that are referenced by an alias for now
170   // Ideally we should turn the alias into a global and duplicate the definition
171   // when needed.
172   DenseSet<GlobalValueSummary *> GlobalInvolvedWithAlias;
173   for (auto &I : Index)
174     for (auto &S : I.second)
175       if (auto AS = dyn_cast<AliasSummary>(S.get()))
176         GlobalInvolvedWithAlias.insert(&AS->getAliasee());
177 
178   for (auto &I : Index)
179     thinLTOResolveWeakForLinkerGUID(I.second, I.first, GlobalInvolvedWithAlias,
180                                     isPrevailing, recordNewLinkage);
181 }
182 
183 static void thinLTOInternalizeAndPromoteGUID(
184     GlobalValueSummaryList &GVSummaryList, GlobalValue::GUID GUID,
185     function_ref<bool(StringRef, GlobalValue::GUID)> isExported) {
186   for (auto &S : GVSummaryList) {
187     if (isExported(S->modulePath(), GUID)) {
188       if (GlobalValue::isLocalLinkage(S->linkage()))
189         S->setLinkage(GlobalValue::ExternalLinkage);
190     } else if (!GlobalValue::isLocalLinkage(S->linkage()))
191       S->setLinkage(GlobalValue::InternalLinkage);
192   }
193 }
194 
195 // Update the linkages in the given \p Index to mark exported values
196 // as external and non-exported values as internal.
197 void llvm::thinLTOInternalizeAndPromoteInIndex(
198     ModuleSummaryIndex &Index,
199     function_ref<bool(StringRef, GlobalValue::GUID)> isExported) {
200   for (auto &I : Index)
201     thinLTOInternalizeAndPromoteGUID(I.second, I.first, isExported);
202 }
203 
204 Expected<std::unique_ptr<InputFile>> InputFile::create(MemoryBufferRef Object) {
205   std::unique_ptr<InputFile> File(new InputFile);
206 
207   Expected<std::unique_ptr<object::IRObjectFile>> IRObj =
208       IRObjectFile::create(Object, File->Ctx);
209   if (!IRObj)
210     return IRObj.takeError();
211   File->Obj = std::move(*IRObj);
212 
213   for (const auto &C : File->Obj->getModule().getComdatSymbolTable()) {
214     auto P =
215         File->ComdatMap.insert(std::make_pair(&C.second, File->Comdats.size()));
216     assert(P.second);
217     (void)P;
218     File->Comdats.push_back(C.first());
219   }
220 
221   return std::move(File);
222 }
223 
224 Expected<int> InputFile::Symbol::getComdatIndex() const {
225   if (!GV)
226     return -1;
227   const GlobalObject *GO;
228   if (auto *GA = dyn_cast<GlobalAlias>(GV)) {
229     GO = GA->getBaseObject();
230     if (!GO)
231       return make_error<StringError>("Unable to determine comdat of alias!",
232                                      inconvertibleErrorCode());
233   } else {
234     GO = cast<GlobalObject>(GV);
235   }
236   if (const Comdat *C = GO->getComdat()) {
237     auto I = File->ComdatMap.find(C);
238     assert(I != File->ComdatMap.end());
239     return I->second;
240   }
241   return -1;
242 }
243 
244 LTO::RegularLTOState::RegularLTOState(unsigned ParallelCodeGenParallelismLevel,
245                                       Config &Conf)
246     : ParallelCodeGenParallelismLevel(ParallelCodeGenParallelismLevel),
247       Ctx(Conf) {}
248 
249 LTO::ThinLTOState::ThinLTOState(ThinBackend Backend) : Backend(Backend) {
250   if (!Backend)
251     this->Backend =
252         createInProcessThinBackend(llvm::heavyweight_hardware_concurrency());
253 }
254 
255 LTO::LTO(Config Conf, ThinBackend Backend,
256          unsigned ParallelCodeGenParallelismLevel)
257     : Conf(std::move(Conf)),
258       RegularLTO(ParallelCodeGenParallelismLevel, this->Conf),
259       ThinLTO(std::move(Backend)) {}
260 
261 // Add the given symbol to the GlobalResolutions map, and resolve its partition.
262 void LTO::addSymbolToGlobalRes(IRObjectFile *Obj,
263                                SmallPtrSet<GlobalValue *, 8> &Used,
264                                const InputFile::Symbol &Sym,
265                                SymbolResolution Res, unsigned Partition) {
266   GlobalValue *GV = Obj->getSymbolGV(Sym.I->getRawDataRefImpl());
267 
268   auto &GlobalRes = GlobalResolutions[Sym.getName()];
269   if (GV) {
270     GlobalRes.UnnamedAddr &= GV->hasGlobalUnnamedAddr();
271     if (Res.Prevailing)
272       GlobalRes.IRName = GV->getName();
273   }
274   if (Res.VisibleToRegularObj || (GV && Used.count(GV)) ||
275       (GlobalRes.Partition != GlobalResolution::Unknown &&
276        GlobalRes.Partition != Partition))
277     GlobalRes.Partition = GlobalResolution::External;
278   else
279     GlobalRes.Partition = Partition;
280 }
281 
282 static void writeToResolutionFile(raw_ostream &OS, InputFile *Input,
283                                   ArrayRef<SymbolResolution> Res) {
284   StringRef Path = Input->getMemoryBufferRef().getBufferIdentifier();
285   OS << Path << '\n';
286   auto ResI = Res.begin();
287   for (const InputFile::Symbol &Sym : Input->symbols()) {
288     assert(ResI != Res.end());
289     SymbolResolution Res = *ResI++;
290 
291     OS << "-r=" << Path << ',' << Sym.getName() << ',';
292     if (Res.Prevailing)
293       OS << 'p';
294     if (Res.FinalDefinitionInLinkageUnit)
295       OS << 'l';
296     if (Res.VisibleToRegularObj)
297       OS << 'x';
298     OS << '\n';
299   }
300   assert(ResI == Res.end());
301 }
302 
303 Error LTO::add(std::unique_ptr<InputFile> Input,
304                ArrayRef<SymbolResolution> Res) {
305   assert(!CalledGetMaxTasks);
306 
307   if (Conf.ResolutionFile)
308     writeToResolutionFile(*Conf.ResolutionFile, Input.get(), Res);
309 
310   // FIXME: move to backend
311   Module &M = Input->Obj->getModule();
312   if (!Conf.OverrideTriple.empty())
313     M.setTargetTriple(Conf.OverrideTriple);
314   else if (M.getTargetTriple().empty())
315     M.setTargetTriple(Conf.DefaultTriple);
316 
317   MemoryBufferRef MBRef = Input->Obj->getMemoryBufferRef();
318   Expected<bool> HasThinLTOSummary = hasGlobalValueSummary(MBRef);
319   if (!HasThinLTOSummary)
320     return HasThinLTOSummary.takeError();
321 
322   if (*HasThinLTOSummary)
323     return addThinLTO(std::move(Input), Res);
324   else
325     return addRegularLTO(std::move(Input), Res);
326 }
327 
328 // Add a regular LTO object to the link.
329 Error LTO::addRegularLTO(std::unique_ptr<InputFile> Input,
330                          ArrayRef<SymbolResolution> Res) {
331   if (!RegularLTO.CombinedModule) {
332     RegularLTO.CombinedModule =
333         llvm::make_unique<Module>("ld-temp.o", RegularLTO.Ctx);
334     RegularLTO.Mover = llvm::make_unique<IRMover>(*RegularLTO.CombinedModule);
335   }
336   Expected<std::unique_ptr<object::IRObjectFile>> ObjOrErr =
337       IRObjectFile::create(Input->Obj->getMemoryBufferRef(), RegularLTO.Ctx);
338   if (!ObjOrErr)
339     return ObjOrErr.takeError();
340   std::unique_ptr<object::IRObjectFile> Obj = std::move(*ObjOrErr);
341 
342   Module &M = Obj->getModule();
343   if (Error Err = M.materializeMetadata())
344     return Err;
345   UpgradeDebugInfo(M);
346 
347   SmallPtrSet<GlobalValue *, 8> Used;
348   collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false);
349 
350   std::vector<GlobalValue *> Keep;
351 
352   for (GlobalVariable &GV : M.globals())
353     if (GV.hasAppendingLinkage())
354       Keep.push_back(&GV);
355 
356   auto ResI = Res.begin();
357   for (const InputFile::Symbol &Sym :
358        make_range(InputFile::symbol_iterator(Obj->symbol_begin(), nullptr),
359                   InputFile::symbol_iterator(Obj->symbol_end(), nullptr))) {
360     assert(ResI != Res.end());
361     SymbolResolution Res = *ResI++;
362     addSymbolToGlobalRes(Obj.get(), Used, Sym, Res, 0);
363 
364     GlobalValue *GV = Obj->getSymbolGV(Sym.I->getRawDataRefImpl());
365     if (Sym.getFlags() & object::BasicSymbolRef::SF_Undefined)
366       continue;
367     if (Res.Prevailing && GV) {
368       Keep.push_back(GV);
369       switch (GV->getLinkage()) {
370       default:
371         break;
372       case GlobalValue::LinkOnceAnyLinkage:
373         GV->setLinkage(GlobalValue::WeakAnyLinkage);
374         break;
375       case GlobalValue::LinkOnceODRLinkage:
376         GV->setLinkage(GlobalValue::WeakODRLinkage);
377         break;
378       }
379     }
380     // Common resolution: collect the maximum size/alignment over all commons.
381     // We also record if we see an instance of a common as prevailing, so that
382     // if none is prevailing we can ignore it later.
383     if (Sym.getFlags() & object::BasicSymbolRef::SF_Common) {
384       auto &CommonRes = RegularLTO.Commons[Sym.getIRName()];
385       CommonRes.Size = std::max(CommonRes.Size, Sym.getCommonSize());
386       CommonRes.Align = std::max(CommonRes.Align, Sym.getCommonAlignment());
387       CommonRes.Prevailing |= Res.Prevailing;
388     }
389 
390     // FIXME: use proposed local attribute for FinalDefinitionInLinkageUnit.
391   }
392   assert(ResI == Res.end());
393 
394   return RegularLTO.Mover->move(Obj->takeModule(), Keep,
395                                 [](GlobalValue &, IRMover::ValueAdder) {},
396                                 /* LinkModuleInlineAsm */ true);
397 }
398 
399 // Add a ThinLTO object to the link.
400 Error LTO::addThinLTO(std::unique_ptr<InputFile> Input,
401                       ArrayRef<SymbolResolution> Res) {
402   Module &M = Input->Obj->getModule();
403   SmallPtrSet<GlobalValue *, 8> Used;
404   collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false);
405 
406   MemoryBufferRef MBRef = Input->Obj->getMemoryBufferRef();
407   Expected<std::unique_ptr<object::ModuleSummaryIndexObjectFile>>
408       SummaryObjOrErr = object::ModuleSummaryIndexObjectFile::create(MBRef);
409   if (!SummaryObjOrErr)
410     return SummaryObjOrErr.takeError();
411   ThinLTO.CombinedIndex.mergeFrom((*SummaryObjOrErr)->takeIndex(),
412                                   ThinLTO.ModuleMap.size());
413 
414   auto ResI = Res.begin();
415   for (const InputFile::Symbol &Sym : Input->symbols()) {
416     assert(ResI != Res.end());
417     SymbolResolution Res = *ResI++;
418     addSymbolToGlobalRes(Input->Obj.get(), Used, Sym, Res,
419                          ThinLTO.ModuleMap.size() + 1);
420 
421     GlobalValue *GV = Input->Obj->getSymbolGV(Sym.I->getRawDataRefImpl());
422     if (Res.Prevailing && GV)
423       ThinLTO.PrevailingModuleForGUID[GV->getGUID()] =
424           MBRef.getBufferIdentifier();
425   }
426   assert(ResI == Res.end());
427 
428   ThinLTO.ModuleMap[MBRef.getBufferIdentifier()] = MBRef;
429   return Error::success();
430 }
431 
432 unsigned LTO::getMaxTasks() const {
433   CalledGetMaxTasks = true;
434   return RegularLTO.ParallelCodeGenParallelismLevel + ThinLTO.ModuleMap.size();
435 }
436 
437 Error LTO::run(AddStreamFn AddStream, NativeObjectCache Cache) {
438   // Save the status of having a regularLTO combined module, as
439   // this is needed for generating the ThinLTO Task ID, and
440   // the CombinedModule will be moved at the end of runRegularLTO.
441   bool HasRegularLTO = RegularLTO.CombinedModule != nullptr;
442   // Invoke regular LTO if there was a regular LTO module to start with.
443   if (HasRegularLTO)
444     if (auto E = runRegularLTO(AddStream))
445       return E;
446   return runThinLTO(AddStream, Cache, HasRegularLTO);
447 }
448 
449 Error LTO::runRegularLTO(AddStreamFn AddStream) {
450   // Make sure commons have the right size/alignment: we kept the largest from
451   // all the prevailing when adding the inputs, and we apply it here.
452   const DataLayout &DL = RegularLTO.CombinedModule->getDataLayout();
453   for (auto &I : RegularLTO.Commons) {
454     if (!I.second.Prevailing)
455       // Don't do anything if no instance of this common was prevailing.
456       continue;
457     GlobalVariable *OldGV = RegularLTO.CombinedModule->getNamedGlobal(I.first);
458     if (OldGV && DL.getTypeAllocSize(OldGV->getValueType()) == I.second.Size) {
459       // Don't create a new global if the type is already correct, just make
460       // sure the alignment is correct.
461       OldGV->setAlignment(I.second.Align);
462       continue;
463     }
464     ArrayType *Ty =
465         ArrayType::get(Type::getInt8Ty(RegularLTO.Ctx), I.second.Size);
466     auto *GV = new GlobalVariable(*RegularLTO.CombinedModule, Ty, false,
467                                   GlobalValue::CommonLinkage,
468                                   ConstantAggregateZero::get(Ty), "");
469     GV->setAlignment(I.second.Align);
470     if (OldGV) {
471       OldGV->replaceAllUsesWith(ConstantExpr::getBitCast(GV, OldGV->getType()));
472       GV->takeName(OldGV);
473       OldGV->eraseFromParent();
474     } else {
475       GV->setName(I.first);
476     }
477   }
478 
479   if (Conf.PreOptModuleHook &&
480       !Conf.PreOptModuleHook(0, *RegularLTO.CombinedModule))
481     return Error::success();
482 
483   if (!Conf.CodeGenOnly) {
484     for (const auto &R : GlobalResolutions) {
485       if (R.second.IRName.empty())
486         continue;
487       if (R.second.Partition != 0 &&
488           R.second.Partition != GlobalResolution::External)
489         continue;
490 
491       GlobalValue *GV =
492           RegularLTO.CombinedModule->getNamedValue(R.second.IRName);
493       // Ignore symbols defined in other partitions.
494       if (!GV || GV->hasLocalLinkage())
495         continue;
496       GV->setUnnamedAddr(R.second.UnnamedAddr ? GlobalValue::UnnamedAddr::Global
497                                               : GlobalValue::UnnamedAddr::None);
498       if (R.second.Partition == 0)
499         GV->setLinkage(GlobalValue::InternalLinkage);
500     }
501 
502     if (Conf.PostInternalizeModuleHook &&
503         !Conf.PostInternalizeModuleHook(0, *RegularLTO.CombinedModule))
504       return Error::success();
505   }
506   return backend(Conf, AddStream, RegularLTO.ParallelCodeGenParallelismLevel,
507                  std::move(RegularLTO.CombinedModule));
508 }
509 
510 /// This class defines the interface to the ThinLTO backend.
511 class lto::ThinBackendProc {
512 protected:
513   Config &Conf;
514   ModuleSummaryIndex &CombinedIndex;
515   const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries;
516 
517 public:
518   ThinBackendProc(Config &Conf, ModuleSummaryIndex &CombinedIndex,
519                   const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries)
520       : Conf(Conf), CombinedIndex(CombinedIndex),
521         ModuleToDefinedGVSummaries(ModuleToDefinedGVSummaries) {}
522 
523   virtual ~ThinBackendProc() {}
524   virtual Error start(
525       unsigned Task, MemoryBufferRef MBRef,
526       const FunctionImporter::ImportMapTy &ImportList,
527       const FunctionImporter::ExportSetTy &ExportList,
528       const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
529       MapVector<StringRef, MemoryBufferRef> &ModuleMap) = 0;
530   virtual Error wait() = 0;
531 };
532 
533 class InProcessThinBackend : public ThinBackendProc {
534   ThreadPool BackendThreadPool;
535   AddStreamFn AddStream;
536   NativeObjectCache Cache;
537 
538   Optional<Error> Err;
539   std::mutex ErrMu;
540 
541 public:
542   InProcessThinBackend(
543       Config &Conf, ModuleSummaryIndex &CombinedIndex,
544       unsigned ThinLTOParallelismLevel,
545       const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
546       AddStreamFn AddStream, NativeObjectCache Cache)
547       : ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries),
548         BackendThreadPool(ThinLTOParallelismLevel),
549         AddStream(std::move(AddStream)), Cache(std::move(Cache)) {}
550 
551   Error runThinLTOBackendThread(
552       AddStreamFn AddStream, NativeObjectCache Cache, unsigned Task,
553       MemoryBufferRef MBRef, ModuleSummaryIndex &CombinedIndex,
554       const FunctionImporter::ImportMapTy &ImportList,
555       const FunctionImporter::ExportSetTy &ExportList,
556       const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
557       const GVSummaryMapTy &DefinedGlobals,
558       MapVector<StringRef, MemoryBufferRef> &ModuleMap) {
559     auto RunThinBackend = [&](AddStreamFn AddStream) {
560       LTOLLVMContext BackendContext(Conf);
561       Expected<std::unique_ptr<Module>> MOrErr =
562           parseBitcodeFile(MBRef, BackendContext);
563       if (!MOrErr)
564         return MOrErr.takeError();
565 
566       return thinBackend(Conf, Task, AddStream, **MOrErr, CombinedIndex,
567                          ImportList, DefinedGlobals, ModuleMap);
568     };
569 
570     auto ModuleID = MBRef.getBufferIdentifier();
571 
572     if (!Cache || !CombinedIndex.modulePaths().count(ModuleID) ||
573         all_of(CombinedIndex.getModuleHash(ModuleID),
574                [](uint32_t V) { return V == 0; }))
575       // Cache disabled or no entry for this module in the combined index or
576       // no module hash.
577       return RunThinBackend(AddStream);
578 
579     SmallString<40> Key;
580     // The module may be cached, this helps handling it.
581     computeCacheKey(Key, CombinedIndex, ModuleID, ImportList, ExportList,
582                     ResolvedODR, DefinedGlobals);
583     if (AddStreamFn CacheAddStream = Cache(Task, Key))
584       return RunThinBackend(CacheAddStream);
585 
586     return Error::success();
587   }
588 
589   Error start(
590       unsigned Task, MemoryBufferRef MBRef,
591       const FunctionImporter::ImportMapTy &ImportList,
592       const FunctionImporter::ExportSetTy &ExportList,
593       const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
594       MapVector<StringRef, MemoryBufferRef> &ModuleMap) override {
595     StringRef ModulePath = MBRef.getBufferIdentifier();
596     assert(ModuleToDefinedGVSummaries.count(ModulePath));
597     const GVSummaryMapTy &DefinedGlobals =
598         ModuleToDefinedGVSummaries.find(ModulePath)->second;
599     BackendThreadPool.async(
600         [=](MemoryBufferRef MBRef, ModuleSummaryIndex &CombinedIndex,
601             const FunctionImporter::ImportMapTy &ImportList,
602             const FunctionImporter::ExportSetTy &ExportList,
603             const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>
604                 &ResolvedODR,
605             const GVSummaryMapTy &DefinedGlobals,
606             MapVector<StringRef, MemoryBufferRef> &ModuleMap) {
607           Error E = runThinLTOBackendThread(
608               AddStream, Cache, Task, MBRef, CombinedIndex, ImportList,
609               ExportList, ResolvedODR, DefinedGlobals, ModuleMap);
610           if (E) {
611             std::unique_lock<std::mutex> L(ErrMu);
612             if (Err)
613               Err = joinErrors(std::move(*Err), std::move(E));
614             else
615               Err = std::move(E);
616           }
617         },
618         MBRef, std::ref(CombinedIndex), std::ref(ImportList),
619         std::ref(ExportList), std::ref(ResolvedODR), std::ref(DefinedGlobals),
620         std::ref(ModuleMap));
621     return Error::success();
622   }
623 
624   Error wait() override {
625     BackendThreadPool.wait();
626     if (Err)
627       return std::move(*Err);
628     else
629       return Error::success();
630   }
631 };
632 
633 ThinBackend lto::createInProcessThinBackend(unsigned ParallelismLevel) {
634   return [=](Config &Conf, ModuleSummaryIndex &CombinedIndex,
635              const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
636              AddStreamFn AddStream, NativeObjectCache Cache) {
637     return llvm::make_unique<InProcessThinBackend>(
638         Conf, CombinedIndex, ParallelismLevel, ModuleToDefinedGVSummaries,
639         AddStream, Cache);
640   };
641 }
642 
643 // Given the original \p Path to an output file, replace any path
644 // prefix matching \p OldPrefix with \p NewPrefix. Also, create the
645 // resulting directory if it does not yet exist.
646 std::string lto::getThinLTOOutputFile(const std::string &Path,
647                                       const std::string &OldPrefix,
648                                       const std::string &NewPrefix) {
649   if (OldPrefix.empty() && NewPrefix.empty())
650     return Path;
651   SmallString<128> NewPath(Path);
652   llvm::sys::path::replace_path_prefix(NewPath, OldPrefix, NewPrefix);
653   StringRef ParentPath = llvm::sys::path::parent_path(NewPath.str());
654   if (!ParentPath.empty()) {
655     // Make sure the new directory exists, creating it if necessary.
656     if (std::error_code EC = llvm::sys::fs::create_directories(ParentPath))
657       llvm::errs() << "warning: could not create directory '" << ParentPath
658                    << "': " << EC.message() << '\n';
659   }
660   return NewPath.str();
661 }
662 
663 class WriteIndexesThinBackend : public ThinBackendProc {
664   std::string OldPrefix, NewPrefix;
665   bool ShouldEmitImportsFiles;
666 
667   std::string LinkedObjectsFileName;
668   std::unique_ptr<llvm::raw_fd_ostream> LinkedObjectsFile;
669 
670 public:
671   WriteIndexesThinBackend(
672       Config &Conf, ModuleSummaryIndex &CombinedIndex,
673       const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
674       std::string OldPrefix, std::string NewPrefix, bool ShouldEmitImportsFiles,
675       std::string LinkedObjectsFileName)
676       : ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries),
677         OldPrefix(OldPrefix), NewPrefix(NewPrefix),
678         ShouldEmitImportsFiles(ShouldEmitImportsFiles),
679         LinkedObjectsFileName(LinkedObjectsFileName) {}
680 
681   Error start(
682       unsigned Task, MemoryBufferRef MBRef,
683       const FunctionImporter::ImportMapTy &ImportList,
684       const FunctionImporter::ExportSetTy &ExportList,
685       const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
686       MapVector<StringRef, MemoryBufferRef> &ModuleMap) override {
687     StringRef ModulePath = MBRef.getBufferIdentifier();
688     std::string NewModulePath =
689         getThinLTOOutputFile(ModulePath, OldPrefix, NewPrefix);
690 
691     std::error_code EC;
692     if (!LinkedObjectsFileName.empty()) {
693       if (!LinkedObjectsFile) {
694         LinkedObjectsFile = llvm::make_unique<raw_fd_ostream>(
695             LinkedObjectsFileName, EC, sys::fs::OpenFlags::F_None);
696         if (EC)
697           return errorCodeToError(EC);
698       }
699       *LinkedObjectsFile << NewModulePath << '\n';
700     }
701 
702     std::map<std::string, GVSummaryMapTy> ModuleToSummariesForIndex;
703     gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries,
704                                      ImportList, ModuleToSummariesForIndex);
705 
706     raw_fd_ostream OS(NewModulePath + ".thinlto.bc", EC,
707                       sys::fs::OpenFlags::F_None);
708     if (EC)
709       return errorCodeToError(EC);
710     WriteIndexToFile(CombinedIndex, OS, &ModuleToSummariesForIndex);
711 
712     if (ShouldEmitImportsFiles)
713       return errorCodeToError(
714           EmitImportsFiles(ModulePath, NewModulePath + ".imports", ImportList));
715     return Error::success();
716   }
717 
718   Error wait() override { return Error::success(); }
719 };
720 
721 ThinBackend lto::createWriteIndexesThinBackend(std::string OldPrefix,
722                                                std::string NewPrefix,
723                                                bool ShouldEmitImportsFiles,
724                                                std::string LinkedObjectsFile) {
725   return [=](Config &Conf, ModuleSummaryIndex &CombinedIndex,
726              const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
727              AddStreamFn AddStream, NativeObjectCache Cache) {
728     return llvm::make_unique<WriteIndexesThinBackend>(
729         Conf, CombinedIndex, ModuleToDefinedGVSummaries, OldPrefix, NewPrefix,
730         ShouldEmitImportsFiles, LinkedObjectsFile);
731   };
732 }
733 
734 Error LTO::runThinLTO(AddStreamFn AddStream, NativeObjectCache Cache,
735                       bool HasRegularLTO) {
736   if (ThinLTO.ModuleMap.empty())
737     return Error::success();
738 
739   if (Conf.CombinedIndexHook && !Conf.CombinedIndexHook(ThinLTO.CombinedIndex))
740     return Error::success();
741 
742   // Collect for each module the list of function it defines (GUID ->
743   // Summary).
744   StringMap<std::map<GlobalValue::GUID, GlobalValueSummary *>>
745       ModuleToDefinedGVSummaries(ThinLTO.ModuleMap.size());
746   ThinLTO.CombinedIndex.collectDefinedGVSummariesPerModule(
747       ModuleToDefinedGVSummaries);
748   // Create entries for any modules that didn't have any GV summaries
749   // (either they didn't have any GVs to start with, or we suppressed
750   // generation of the summaries because they e.g. had inline assembly
751   // uses that couldn't be promoted/renamed on export). This is so
752   // InProcessThinBackend::start can still launch a backend thread, which
753   // is passed the map of summaries for the module, without any special
754   // handling for this case.
755   for (auto &Mod : ThinLTO.ModuleMap)
756     if (!ModuleToDefinedGVSummaries.count(Mod.first))
757       ModuleToDefinedGVSummaries.try_emplace(Mod.first);
758 
759   StringMap<FunctionImporter::ImportMapTy> ImportLists(
760       ThinLTO.ModuleMap.size());
761   StringMap<FunctionImporter::ExportSetTy> ExportLists(
762       ThinLTO.ModuleMap.size());
763   StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
764 
765   if (Conf.OptLevel > 0) {
766     ComputeCrossModuleImport(ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
767                              ImportLists, ExportLists);
768 
769     std::set<GlobalValue::GUID> ExportedGUIDs;
770     for (auto &Res : GlobalResolutions) {
771       if (!Res.second.IRName.empty() &&
772           Res.second.Partition == GlobalResolution::External)
773         ExportedGUIDs.insert(GlobalValue::getGUID(Res.second.IRName));
774     }
775 
776     auto isPrevailing = [&](GlobalValue::GUID GUID,
777                             const GlobalValueSummary *S) {
778       return ThinLTO.PrevailingModuleForGUID[GUID] == S->modulePath();
779     };
780     auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) {
781       const auto &ExportList = ExportLists.find(ModuleIdentifier);
782       return (ExportList != ExportLists.end() &&
783               ExportList->second.count(GUID)) ||
784              ExportedGUIDs.count(GUID);
785     };
786     thinLTOInternalizeAndPromoteInIndex(ThinLTO.CombinedIndex, isExported);
787 
788     auto recordNewLinkage = [&](StringRef ModuleIdentifier,
789                                 GlobalValue::GUID GUID,
790                                 GlobalValue::LinkageTypes NewLinkage) {
791       ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
792     };
793 
794     thinLTOResolveWeakForLinkerInIndex(ThinLTO.CombinedIndex, isPrevailing,
795                                        recordNewLinkage);
796   }
797 
798   std::unique_ptr<ThinBackendProc> BackendProc =
799       ThinLTO.Backend(Conf, ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
800                       AddStream, Cache);
801 
802   // Partition numbers for ThinLTO jobs start at 1 (see comments for
803   // GlobalResolution in LTO.h). Task numbers, however, start at
804   // ParallelCodeGenParallelismLevel if an LTO module is present, as tasks 0
805   // through ParallelCodeGenParallelismLevel-1 are reserved for parallel code
806   // generation partitions.
807   unsigned Task =
808       HasRegularLTO ? RegularLTO.ParallelCodeGenParallelismLevel : 0;
809   unsigned Partition = 1;
810 
811   for (auto &Mod : ThinLTO.ModuleMap) {
812     if (Error E = BackendProc->start(Task, Mod.second, ImportLists[Mod.first],
813                                      ExportLists[Mod.first],
814                                      ResolvedODR[Mod.first], ThinLTO.ModuleMap))
815       return E;
816 
817     ++Task;
818     ++Partition;
819   }
820 
821   return BackendProc->wait();
822 }
823