xref: /llvm-project-15.0.7/llvm/lib/LTO/LTO.cpp (revision e396d8e2)
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 Config &Conf, const ModuleSummaryIndex &Index,
54     StringRef ModuleID, 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 parts of the LTO configuration that affect code generation.
71   auto AddString = [&](StringRef Str) {
72     Hasher.update(Str);
73     Hasher.update(ArrayRef<uint8_t>{0});
74   };
75   auto AddUnsigned = [&](unsigned I) {
76     uint8_t Data[4];
77     Data[0] = I;
78     Data[1] = I >> 8;
79     Data[2] = I >> 16;
80     Data[3] = I >> 24;
81     Hasher.update(ArrayRef<uint8_t>{Data, 4});
82   };
83   AddString(Conf.CPU);
84   // FIXME: Hash more of Options. For now all clients initialize Options from
85   // command-line flags (which is unsupported in production), but may set
86   // RelaxELFRelocations. The clang driver can also pass FunctionSections,
87   // DataSections and DebuggerTuning via command line flags.
88   AddUnsigned(Conf.Options.RelaxELFRelocations);
89   AddUnsigned(Conf.Options.FunctionSections);
90   AddUnsigned(Conf.Options.DataSections);
91   AddUnsigned((unsigned)Conf.Options.DebuggerTuning);
92   for (auto &A : Conf.MAttrs)
93     AddString(A);
94   AddUnsigned(Conf.RelocModel);
95   AddUnsigned(Conf.CodeModel);
96   AddUnsigned(Conf.CGOptLevel);
97   AddUnsigned(Conf.OptLevel);
98   AddString(Conf.OptPipeline);
99   AddString(Conf.AAPipeline);
100   AddString(Conf.OverrideTriple);
101   AddString(Conf.DefaultTriple);
102 
103   // Include the hash for the current module
104   auto ModHash = Index.getModuleHash(ModuleID);
105   Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
106   for (auto F : ExportList)
107     // The export list can impact the internalization, be conservative here
108     Hasher.update(ArrayRef<uint8_t>((uint8_t *)&F, sizeof(F)));
109 
110   // Include the hash for every module we import functions from
111   for (auto &Entry : ImportList) {
112     auto ModHash = Index.getModuleHash(Entry.first());
113     Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
114   }
115 
116   // Include the hash for the resolved ODR.
117   for (auto &Entry : ResolvedODR) {
118     Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.first,
119                                     sizeof(GlobalValue::GUID)));
120     Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.second,
121                                     sizeof(GlobalValue::LinkageTypes)));
122   }
123 
124   // Include the hash for the linkage type to reflect internalization and weak
125   // resolution.
126   for (auto &GS : DefinedGlobals) {
127     GlobalValue::LinkageTypes Linkage = GS.second->linkage();
128     Hasher.update(
129         ArrayRef<uint8_t>((const uint8_t *)&Linkage, sizeof(Linkage)));
130   }
131 
132   if (!Conf.SampleProfile.empty()) {
133     auto FileOrErr = MemoryBuffer::getFile(Conf.SampleProfile);
134     if (FileOrErr)
135       Hasher.update(FileOrErr.get()->getBuffer());
136   }
137 
138   Key = toHex(Hasher.result());
139 }
140 
141 static void thinLTOResolveWeakForLinkerGUID(
142     GlobalValueSummaryList &GVSummaryList, GlobalValue::GUID GUID,
143     DenseSet<GlobalValueSummary *> &GlobalInvolvedWithAlias,
144     function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
145         isPrevailing,
146     function_ref<void(StringRef, GlobalValue::GUID, GlobalValue::LinkageTypes)>
147         recordNewLinkage) {
148   for (auto &S : GVSummaryList) {
149     GlobalValue::LinkageTypes OriginalLinkage = S->linkage();
150     if (!GlobalValue::isWeakForLinker(OriginalLinkage))
151       continue;
152     // We need to emit only one of these. The prevailing module will keep it,
153     // but turned into a weak, while the others will drop it when possible.
154     // This is both a compile-time optimization and a correctness
155     // transformation. This is necessary for correctness when we have exported
156     // a reference - we need to convert the linkonce to weak to
157     // ensure a copy is kept to satisfy the exported reference.
158     // FIXME: We may want to split the compile time and correctness
159     // aspects into separate routines.
160     if (isPrevailing(GUID, S.get())) {
161       if (GlobalValue::isLinkOnceLinkage(OriginalLinkage))
162         S->setLinkage(GlobalValue::getWeakLinkage(
163             GlobalValue::isLinkOnceODRLinkage(OriginalLinkage)));
164     }
165     // Alias and aliasee can't be turned into available_externally.
166     else if (!isa<AliasSummary>(S.get()) &&
167              !GlobalInvolvedWithAlias.count(S.get()))
168       S->setLinkage(GlobalValue::AvailableExternallyLinkage);
169     if (S->linkage() != OriginalLinkage)
170       recordNewLinkage(S->modulePath(), GUID, S->linkage());
171   }
172 }
173 
174 // Resolve Weak and LinkOnce values in the \p Index.
175 //
176 // We'd like to drop these functions if they are no longer referenced in the
177 // current module. However there is a chance that another module is still
178 // referencing them because of the import. We make sure we always emit at least
179 // one copy.
180 void llvm::thinLTOResolveWeakForLinkerInIndex(
181     ModuleSummaryIndex &Index,
182     function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
183         isPrevailing,
184     function_ref<void(StringRef, GlobalValue::GUID, GlobalValue::LinkageTypes)>
185         recordNewLinkage) {
186   // We won't optimize the globals that are referenced by an alias for now
187   // Ideally we should turn the alias into a global and duplicate the definition
188   // when needed.
189   DenseSet<GlobalValueSummary *> GlobalInvolvedWithAlias;
190   for (auto &I : Index)
191     for (auto &S : I.second)
192       if (auto AS = dyn_cast<AliasSummary>(S.get()))
193         GlobalInvolvedWithAlias.insert(&AS->getAliasee());
194 
195   for (auto &I : Index)
196     thinLTOResolveWeakForLinkerGUID(I.second, I.first, GlobalInvolvedWithAlias,
197                                     isPrevailing, recordNewLinkage);
198 }
199 
200 static void thinLTOInternalizeAndPromoteGUID(
201     GlobalValueSummaryList &GVSummaryList, GlobalValue::GUID GUID,
202     function_ref<bool(StringRef, GlobalValue::GUID)> isExported) {
203   for (auto &S : GVSummaryList) {
204     if (isExported(S->modulePath(), GUID)) {
205       if (GlobalValue::isLocalLinkage(S->linkage()))
206         S->setLinkage(GlobalValue::ExternalLinkage);
207     } else if (!GlobalValue::isLocalLinkage(S->linkage()))
208       S->setLinkage(GlobalValue::InternalLinkage);
209   }
210 }
211 
212 // Update the linkages in the given \p Index to mark exported values
213 // as external and non-exported values as internal.
214 void llvm::thinLTOInternalizeAndPromoteInIndex(
215     ModuleSummaryIndex &Index,
216     function_ref<bool(StringRef, GlobalValue::GUID)> isExported) {
217   for (auto &I : Index)
218     thinLTOInternalizeAndPromoteGUID(I.second, I.first, isExported);
219 }
220 
221 struct InputFile::InputModule {
222   BitcodeModule BM;
223   std::unique_ptr<Module> Mod;
224 
225   // The range of ModuleSymbolTable entries for this input module.
226   size_t SymBegin, SymEnd;
227 };
228 
229 // Requires a destructor for std::vector<InputModule>.
230 InputFile::~InputFile() = default;
231 
232 Expected<std::unique_ptr<InputFile>> InputFile::create(MemoryBufferRef Object) {
233   std::unique_ptr<InputFile> File(new InputFile);
234 
235   ErrorOr<MemoryBufferRef> BCOrErr =
236       IRObjectFile::findBitcodeInMemBuffer(Object);
237   if (!BCOrErr)
238     return errorCodeToError(BCOrErr.getError());
239 
240   Expected<std::vector<BitcodeModule>> BMsOrErr =
241       getBitcodeModuleList(*BCOrErr);
242   if (!BMsOrErr)
243     return BMsOrErr.takeError();
244 
245   if (BMsOrErr->empty())
246     return make_error<StringError>("Bitcode file does not contain any modules",
247                                    inconvertibleErrorCode());
248 
249   // Create an InputModule for each module in the InputFile, and add it to the
250   // ModuleSymbolTable.
251   for (auto BM : *BMsOrErr) {
252     Expected<std::unique_ptr<Module>> MOrErr =
253         BM.getLazyModule(File->Ctx, /*ShouldLazyLoadMetadata*/ true,
254                          /*IsImporting*/ false);
255     if (!MOrErr)
256       return MOrErr.takeError();
257 
258     size_t SymBegin = File->SymTab.symbols().size();
259     File->SymTab.addModule(MOrErr->get());
260     size_t SymEnd = File->SymTab.symbols().size();
261 
262     for (const auto &C : (*MOrErr)->getComdatSymbolTable()) {
263       auto P = File->ComdatMap.insert(
264           std::make_pair(&C.second, File->Comdats.size()));
265       assert(P.second);
266       (void)P;
267       File->Comdats.push_back(C.first());
268     }
269 
270     File->Mods.push_back({BM, std::move(*MOrErr), SymBegin, SymEnd});
271   }
272 
273   return std::move(File);
274 }
275 
276 Expected<int> InputFile::Symbol::getComdatIndex() const {
277   if (!isGV())
278     return -1;
279   const GlobalObject *GO = getGV()->getBaseObject();
280   if (!GO)
281     return make_error<StringError>("Unable to determine comdat of alias!",
282                                    inconvertibleErrorCode());
283   if (const Comdat *C = GO->getComdat()) {
284     auto I = File->ComdatMap.find(C);
285     assert(I != File->ComdatMap.end());
286     return I->second;
287   }
288   return -1;
289 }
290 
291 StringRef InputFile::getName() const {
292   return Mods[0].BM.getModuleIdentifier();
293 }
294 
295 StringRef InputFile::getSourceFileName() const {
296   return Mods[0].Mod->getSourceFileName();
297 }
298 
299 iterator_range<InputFile::symbol_iterator>
300 InputFile::module_symbols(InputModule &IM) {
301   return llvm::make_range(
302       symbol_iterator(SymTab.symbols().data() + IM.SymBegin, SymTab, this),
303       symbol_iterator(SymTab.symbols().data() + IM.SymEnd, SymTab, this));
304 }
305 
306 LTO::RegularLTOState::RegularLTOState(unsigned ParallelCodeGenParallelismLevel,
307                                       Config &Conf)
308     : ParallelCodeGenParallelismLevel(ParallelCodeGenParallelismLevel),
309       Ctx(Conf) {}
310 
311 LTO::ThinLTOState::ThinLTOState(ThinBackend Backend) : Backend(Backend) {
312   if (!Backend)
313     this->Backend =
314         createInProcessThinBackend(llvm::heavyweight_hardware_concurrency());
315 }
316 
317 LTO::LTO(Config Conf, ThinBackend Backend,
318          unsigned ParallelCodeGenParallelismLevel)
319     : Conf(std::move(Conf)),
320       RegularLTO(ParallelCodeGenParallelismLevel, this->Conf),
321       ThinLTO(std::move(Backend)) {}
322 
323 // Requires a destructor for MapVector<BitcodeModule>.
324 LTO::~LTO() = default;
325 
326 // Add the given symbol to the GlobalResolutions map, and resolve its partition.
327 void LTO::addSymbolToGlobalRes(SmallPtrSet<GlobalValue *, 8> &Used,
328                                const InputFile::Symbol &Sym,
329                                SymbolResolution Res, unsigned Partition) {
330   GlobalValue *GV = Sym.isGV() ? Sym.getGV() : nullptr;
331 
332   auto &GlobalRes = GlobalResolutions[Sym.getName()];
333   if (GV) {
334     GlobalRes.UnnamedAddr &= GV->hasGlobalUnnamedAddr();
335     if (Res.Prevailing)
336       GlobalRes.IRName = GV->getName();
337   }
338   // Set the partition to external if we know it is used elsewhere, e.g.
339   // it is visible to a regular object, is referenced from llvm.compiler_used,
340   // or was already recorded as being referenced from a different partition.
341   if (Res.VisibleToRegularObj || (GV && Used.count(GV)) ||
342       (GlobalRes.Partition != GlobalResolution::Unknown &&
343        GlobalRes.Partition != Partition)) {
344     GlobalRes.Partition = GlobalResolution::External;
345   } else
346     // First recorded reference, save the current partition.
347     GlobalRes.Partition = Partition;
348 
349   // Flag as visible outside of ThinLTO if visible from a regular object or
350   // if this is a reference in the regular LTO partition.
351   GlobalRes.VisibleOutsideThinLTO |=
352       (Res.VisibleToRegularObj || (Partition == GlobalResolution::RegularLTO));
353 }
354 
355 static void writeToResolutionFile(raw_ostream &OS, InputFile *Input,
356                                   ArrayRef<SymbolResolution> Res) {
357   StringRef Path = Input->getName();
358   OS << Path << '\n';
359   auto ResI = Res.begin();
360   for (const InputFile::Symbol &Sym : Input->symbols()) {
361     assert(ResI != Res.end());
362     SymbolResolution Res = *ResI++;
363 
364     OS << "-r=" << Path << ',' << Sym.getName() << ',';
365     if (Res.Prevailing)
366       OS << 'p';
367     if (Res.FinalDefinitionInLinkageUnit)
368       OS << 'l';
369     if (Res.VisibleToRegularObj)
370       OS << 'x';
371     OS << '\n';
372   }
373   OS.flush();
374   assert(ResI == Res.end());
375 }
376 
377 Error LTO::add(std::unique_ptr<InputFile> Input,
378                ArrayRef<SymbolResolution> Res) {
379   assert(!CalledGetMaxTasks);
380 
381   if (Conf.ResolutionFile)
382     writeToResolutionFile(*Conf.ResolutionFile, Input.get(), Res);
383 
384   const SymbolResolution *ResI = Res.begin();
385   for (InputFile::InputModule &IM : Input->Mods)
386     if (Error Err = addModule(*Input, IM, ResI, Res.end()))
387       return Err;
388 
389   assert(ResI == Res.end());
390   return Error::success();
391 }
392 
393 Error LTO::addModule(InputFile &Input, InputFile::InputModule &IM,
394                      const SymbolResolution *&ResI,
395                      const SymbolResolution *ResE) {
396   // FIXME: move to backend
397   Module &M = *IM.Mod;
398 
399   if (M.getDataLayoutStr().empty())
400     return make_error<StringError>("input module has no datalayout",
401                                     inconvertibleErrorCode());
402 
403   if (!Conf.OverrideTriple.empty())
404     M.setTargetTriple(Conf.OverrideTriple);
405   else if (M.getTargetTriple().empty())
406     M.setTargetTriple(Conf.DefaultTriple);
407 
408   Expected<bool> HasThinLTOSummary = IM.BM.hasSummary();
409   if (!HasThinLTOSummary)
410     return HasThinLTOSummary.takeError();
411 
412   if (*HasThinLTOSummary)
413     return addThinLTO(IM.BM, M, Input.module_symbols(IM), ResI, ResE);
414   else
415     return addRegularLTO(IM.BM, ResI, ResE);
416 }
417 
418 // Add a regular LTO object to the link.
419 Error LTO::addRegularLTO(BitcodeModule BM, const SymbolResolution *&ResI,
420                          const SymbolResolution *ResE) {
421   if (!RegularLTO.CombinedModule) {
422     RegularLTO.CombinedModule =
423         llvm::make_unique<Module>("ld-temp.o", RegularLTO.Ctx);
424     RegularLTO.Mover = llvm::make_unique<IRMover>(*RegularLTO.CombinedModule);
425   }
426   Expected<std::unique_ptr<Module>> MOrErr =
427       BM.getLazyModule(RegularLTO.Ctx, /*ShouldLazyLoadMetadata*/ true,
428                        /*IsImporting*/ false);
429   if (!MOrErr)
430     return MOrErr.takeError();
431 
432   Module &M = **MOrErr;
433   if (Error Err = M.materializeMetadata())
434     return Err;
435   UpgradeDebugInfo(M);
436 
437   ModuleSymbolTable SymTab;
438   SymTab.addModule(&M);
439 
440   SmallPtrSet<GlobalValue *, 8> Used;
441   collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false);
442 
443   std::vector<GlobalValue *> Keep;
444 
445   for (GlobalVariable &GV : M.globals())
446     if (GV.hasAppendingLinkage())
447       Keep.push_back(&GV);
448 
449   DenseSet<GlobalObject *> AliasedGlobals;
450   for (auto &GA : M.aliases())
451     if (GlobalObject *GO = GA.getBaseObject())
452       AliasedGlobals.insert(GO);
453 
454   for (const InputFile::Symbol &Sym :
455        make_range(InputFile::symbol_iterator(SymTab.symbols().begin(), SymTab,
456                                              nullptr),
457                   InputFile::symbol_iterator(SymTab.symbols().end(), SymTab,
458                                              nullptr))) {
459     assert(ResI != ResE);
460     SymbolResolution Res = *ResI++;
461     addSymbolToGlobalRes(Used, Sym, Res, 0);
462 
463     if (Sym.isGV()) {
464       GlobalValue *GV = Sym.getGV();
465       if (Res.Prevailing) {
466         if (Sym.getFlags() & object::BasicSymbolRef::SF_Undefined)
467           continue;
468         Keep.push_back(GV);
469         switch (GV->getLinkage()) {
470         default:
471           break;
472         case GlobalValue::LinkOnceAnyLinkage:
473           GV->setLinkage(GlobalValue::WeakAnyLinkage);
474           break;
475         case GlobalValue::LinkOnceODRLinkage:
476           GV->setLinkage(GlobalValue::WeakODRLinkage);
477           break;
478         }
479       } else if (isa<GlobalObject>(GV) &&
480                  (GV->hasLinkOnceODRLinkage() || GV->hasWeakODRLinkage() ||
481                   GV->hasAvailableExternallyLinkage()) &&
482                  !AliasedGlobals.count(cast<GlobalObject>(GV))) {
483         // Either of the above three types of linkage indicates that the
484         // chosen prevailing symbol will have the same semantics as this copy of
485         // the symbol, so we can link it with available_externally linkage. We
486         // only need to do this if the symbol is undefined.
487         GlobalValue *CombinedGV =
488             RegularLTO.CombinedModule->getNamedValue(GV->getName());
489         if (!CombinedGV || CombinedGV->isDeclaration()) {
490           Keep.push_back(GV);
491           GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
492           cast<GlobalObject>(GV)->setComdat(nullptr);
493         }
494       }
495     }
496     // Common resolution: collect the maximum size/alignment over all commons.
497     // We also record if we see an instance of a common as prevailing, so that
498     // if none is prevailing we can ignore it later.
499     if (Sym.getFlags() & object::BasicSymbolRef::SF_Common) {
500       // FIXME: We should figure out what to do about commons defined by asm.
501       // For now they aren't reported correctly by ModuleSymbolTable.
502       auto &CommonRes = RegularLTO.Commons[Sym.getGV()->getName()];
503       CommonRes.Size = std::max(CommonRes.Size, Sym.getCommonSize());
504       CommonRes.Align = std::max(CommonRes.Align, Sym.getCommonAlignment());
505       CommonRes.Prevailing |= Res.Prevailing;
506     }
507 
508     // FIXME: use proposed local attribute for FinalDefinitionInLinkageUnit.
509   }
510 
511   return RegularLTO.Mover->move(std::move(*MOrErr), Keep,
512                                 [](GlobalValue &, IRMover::ValueAdder) {},
513                                 /* LinkModuleInlineAsm */ true,
514                                 /* IsPerformingImport */ false);
515 }
516 
517 // Add a ThinLTO object to the link.
518 // FIXME: This function should not need to take as many parameters once we have
519 // a bitcode symbol table.
520 Error LTO::addThinLTO(BitcodeModule BM, Module &M,
521                       iterator_range<InputFile::symbol_iterator> Syms,
522                       const SymbolResolution *&ResI,
523                       const SymbolResolution *ResE) {
524   SmallPtrSet<GlobalValue *, 8> Used;
525   collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false);
526 
527   Expected<std::unique_ptr<ModuleSummaryIndex>> SummaryOrErr = BM.getSummary();
528   if (!SummaryOrErr)
529     return SummaryOrErr.takeError();
530   ThinLTO.CombinedIndex.mergeFrom(std::move(*SummaryOrErr),
531                                   ThinLTO.ModuleMap.size());
532 
533   for (const InputFile::Symbol &Sym : Syms) {
534     assert(ResI != ResE);
535     SymbolResolution Res = *ResI++;
536     addSymbolToGlobalRes(Used, Sym, Res, ThinLTO.ModuleMap.size() + 1);
537 
538     if (Res.Prevailing && Sym.isGV())
539       ThinLTO.PrevailingModuleForGUID[Sym.getGV()->getGUID()] =
540           BM.getModuleIdentifier();
541   }
542 
543   if (!ThinLTO.ModuleMap.insert({BM.getModuleIdentifier(), BM}).second)
544     return make_error<StringError>(
545         "Expected at most one ThinLTO module per bitcode file",
546         inconvertibleErrorCode());
547 
548   return Error::success();
549 }
550 
551 unsigned LTO::getMaxTasks() const {
552   CalledGetMaxTasks = true;
553   return RegularLTO.ParallelCodeGenParallelismLevel + ThinLTO.ModuleMap.size();
554 }
555 
556 Error LTO::run(AddStreamFn AddStream, NativeObjectCache Cache) {
557   // Save the status of having a regularLTO combined module, as
558   // this is needed for generating the ThinLTO Task ID, and
559   // the CombinedModule will be moved at the end of runRegularLTO.
560   bool HasRegularLTO = RegularLTO.CombinedModule != nullptr;
561   // Invoke regular LTO if there was a regular LTO module to start with.
562   if (HasRegularLTO)
563     if (auto E = runRegularLTO(AddStream))
564       return E;
565   return runThinLTO(AddStream, Cache, HasRegularLTO);
566 }
567 
568 Error LTO::runRegularLTO(AddStreamFn AddStream) {
569   // Make sure commons have the right size/alignment: we kept the largest from
570   // all the prevailing when adding the inputs, and we apply it here.
571   const DataLayout &DL = RegularLTO.CombinedModule->getDataLayout();
572   for (auto &I : RegularLTO.Commons) {
573     if (!I.second.Prevailing)
574       // Don't do anything if no instance of this common was prevailing.
575       continue;
576     GlobalVariable *OldGV = RegularLTO.CombinedModule->getNamedGlobal(I.first);
577     if (OldGV && DL.getTypeAllocSize(OldGV->getValueType()) == I.second.Size) {
578       // Don't create a new global if the type is already correct, just make
579       // sure the alignment is correct.
580       OldGV->setAlignment(I.second.Align);
581       continue;
582     }
583     ArrayType *Ty =
584         ArrayType::get(Type::getInt8Ty(RegularLTO.Ctx), I.second.Size);
585     auto *GV = new GlobalVariable(*RegularLTO.CombinedModule, Ty, false,
586                                   GlobalValue::CommonLinkage,
587                                   ConstantAggregateZero::get(Ty), "");
588     GV->setAlignment(I.second.Align);
589     if (OldGV) {
590       OldGV->replaceAllUsesWith(ConstantExpr::getBitCast(GV, OldGV->getType()));
591       GV->takeName(OldGV);
592       OldGV->eraseFromParent();
593     } else {
594       GV->setName(I.first);
595     }
596   }
597 
598   if (Conf.PreOptModuleHook &&
599       !Conf.PreOptModuleHook(0, *RegularLTO.CombinedModule))
600     return Error::success();
601 
602   if (!Conf.CodeGenOnly) {
603     for (const auto &R : GlobalResolutions) {
604       if (R.second.IRName.empty())
605         continue;
606       if (R.second.Partition != 0 &&
607           R.second.Partition != GlobalResolution::External)
608         continue;
609 
610       GlobalValue *GV =
611           RegularLTO.CombinedModule->getNamedValue(R.second.IRName);
612       // Ignore symbols defined in other partitions.
613       if (!GV || GV->hasLocalLinkage())
614         continue;
615       GV->setUnnamedAddr(R.second.UnnamedAddr ? GlobalValue::UnnamedAddr::Global
616                                               : GlobalValue::UnnamedAddr::None);
617       if (R.second.Partition == 0)
618         GV->setLinkage(GlobalValue::InternalLinkage);
619     }
620 
621     if (Conf.PostInternalizeModuleHook &&
622         !Conf.PostInternalizeModuleHook(0, *RegularLTO.CombinedModule))
623       return Error::success();
624   }
625   return backend(Conf, AddStream, RegularLTO.ParallelCodeGenParallelismLevel,
626                  std::move(RegularLTO.CombinedModule), ThinLTO.CombinedIndex);
627 }
628 
629 /// This class defines the interface to the ThinLTO backend.
630 class lto::ThinBackendProc {
631 protected:
632   Config &Conf;
633   ModuleSummaryIndex &CombinedIndex;
634   const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries;
635 
636 public:
637   ThinBackendProc(Config &Conf, ModuleSummaryIndex &CombinedIndex,
638                   const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries)
639       : Conf(Conf), CombinedIndex(CombinedIndex),
640         ModuleToDefinedGVSummaries(ModuleToDefinedGVSummaries) {}
641 
642   virtual ~ThinBackendProc() {}
643   virtual Error start(
644       unsigned Task, BitcodeModule BM,
645       const FunctionImporter::ImportMapTy &ImportList,
646       const FunctionImporter::ExportSetTy &ExportList,
647       const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
648       MapVector<StringRef, BitcodeModule> &ModuleMap) = 0;
649   virtual Error wait() = 0;
650 };
651 
652 namespace {
653 class InProcessThinBackend : public ThinBackendProc {
654   ThreadPool BackendThreadPool;
655   AddStreamFn AddStream;
656   NativeObjectCache Cache;
657 
658   Optional<Error> Err;
659   std::mutex ErrMu;
660 
661 public:
662   InProcessThinBackend(
663       Config &Conf, ModuleSummaryIndex &CombinedIndex,
664       unsigned ThinLTOParallelismLevel,
665       const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
666       AddStreamFn AddStream, NativeObjectCache Cache)
667       : ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries),
668         BackendThreadPool(ThinLTOParallelismLevel),
669         AddStream(std::move(AddStream)), Cache(std::move(Cache)) {}
670 
671   Error runThinLTOBackendThread(
672       AddStreamFn AddStream, NativeObjectCache Cache, unsigned Task,
673       BitcodeModule BM, ModuleSummaryIndex &CombinedIndex,
674       const FunctionImporter::ImportMapTy &ImportList,
675       const FunctionImporter::ExportSetTy &ExportList,
676       const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
677       const GVSummaryMapTy &DefinedGlobals,
678       MapVector<StringRef, BitcodeModule> &ModuleMap) {
679     auto RunThinBackend = [&](AddStreamFn AddStream) {
680       LTOLLVMContext BackendContext(Conf);
681       Expected<std::unique_ptr<Module>> MOrErr = BM.parseModule(BackendContext);
682       if (!MOrErr)
683         return MOrErr.takeError();
684 
685       return thinBackend(Conf, Task, AddStream, **MOrErr, CombinedIndex,
686                          ImportList, DefinedGlobals, ModuleMap);
687     };
688 
689     auto ModuleID = BM.getModuleIdentifier();
690 
691     if (!Cache || !CombinedIndex.modulePaths().count(ModuleID) ||
692         all_of(CombinedIndex.getModuleHash(ModuleID),
693                [](uint32_t V) { return V == 0; }))
694       // Cache disabled or no entry for this module in the combined index or
695       // no module hash.
696       return RunThinBackend(AddStream);
697 
698     SmallString<40> Key;
699     // The module may be cached, this helps handling it.
700     computeCacheKey(Key, Conf, CombinedIndex, ModuleID, ImportList, ExportList,
701                     ResolvedODR, DefinedGlobals);
702     if (AddStreamFn CacheAddStream = Cache(Task, Key))
703       return RunThinBackend(CacheAddStream);
704 
705     return Error::success();
706   }
707 
708   Error start(
709       unsigned Task, BitcodeModule BM,
710       const FunctionImporter::ImportMapTy &ImportList,
711       const FunctionImporter::ExportSetTy &ExportList,
712       const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
713       MapVector<StringRef, BitcodeModule> &ModuleMap) override {
714     StringRef ModulePath = BM.getModuleIdentifier();
715     assert(ModuleToDefinedGVSummaries.count(ModulePath));
716     const GVSummaryMapTy &DefinedGlobals =
717         ModuleToDefinedGVSummaries.find(ModulePath)->second;
718     BackendThreadPool.async(
719         [=](BitcodeModule BM, ModuleSummaryIndex &CombinedIndex,
720             const FunctionImporter::ImportMapTy &ImportList,
721             const FunctionImporter::ExportSetTy &ExportList,
722             const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>
723                 &ResolvedODR,
724             const GVSummaryMapTy &DefinedGlobals,
725             MapVector<StringRef, BitcodeModule> &ModuleMap) {
726           Error E = runThinLTOBackendThread(
727               AddStream, Cache, Task, BM, CombinedIndex, ImportList,
728               ExportList, ResolvedODR, DefinedGlobals, ModuleMap);
729           if (E) {
730             std::unique_lock<std::mutex> L(ErrMu);
731             if (Err)
732               Err = joinErrors(std::move(*Err), std::move(E));
733             else
734               Err = std::move(E);
735           }
736         },
737         BM, std::ref(CombinedIndex), std::ref(ImportList),
738         std::ref(ExportList), std::ref(ResolvedODR), std::ref(DefinedGlobals),
739         std::ref(ModuleMap));
740     return Error::success();
741   }
742 
743   Error wait() override {
744     BackendThreadPool.wait();
745     if (Err)
746       return std::move(*Err);
747     else
748       return Error::success();
749   }
750 };
751 } // end anonymous namespace
752 
753 ThinBackend lto::createInProcessThinBackend(unsigned ParallelismLevel) {
754   return [=](Config &Conf, ModuleSummaryIndex &CombinedIndex,
755              const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
756              AddStreamFn AddStream, NativeObjectCache Cache) {
757     return llvm::make_unique<InProcessThinBackend>(
758         Conf, CombinedIndex, ParallelismLevel, ModuleToDefinedGVSummaries,
759         AddStream, Cache);
760   };
761 }
762 
763 // Given the original \p Path to an output file, replace any path
764 // prefix matching \p OldPrefix with \p NewPrefix. Also, create the
765 // resulting directory if it does not yet exist.
766 std::string lto::getThinLTOOutputFile(const std::string &Path,
767                                       const std::string &OldPrefix,
768                                       const std::string &NewPrefix) {
769   if (OldPrefix.empty() && NewPrefix.empty())
770     return Path;
771   SmallString<128> NewPath(Path);
772   llvm::sys::path::replace_path_prefix(NewPath, OldPrefix, NewPrefix);
773   StringRef ParentPath = llvm::sys::path::parent_path(NewPath.str());
774   if (!ParentPath.empty()) {
775     // Make sure the new directory exists, creating it if necessary.
776     if (std::error_code EC = llvm::sys::fs::create_directories(ParentPath))
777       llvm::errs() << "warning: could not create directory '" << ParentPath
778                    << "': " << EC.message() << '\n';
779   }
780   return NewPath.str();
781 }
782 
783 namespace {
784 class WriteIndexesThinBackend : public ThinBackendProc {
785   std::string OldPrefix, NewPrefix;
786   bool ShouldEmitImportsFiles;
787 
788   std::string LinkedObjectsFileName;
789   std::unique_ptr<llvm::raw_fd_ostream> LinkedObjectsFile;
790 
791 public:
792   WriteIndexesThinBackend(
793       Config &Conf, ModuleSummaryIndex &CombinedIndex,
794       const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
795       std::string OldPrefix, std::string NewPrefix, bool ShouldEmitImportsFiles,
796       std::string LinkedObjectsFileName)
797       : ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries),
798         OldPrefix(OldPrefix), NewPrefix(NewPrefix),
799         ShouldEmitImportsFiles(ShouldEmitImportsFiles),
800         LinkedObjectsFileName(LinkedObjectsFileName) {}
801 
802   Error start(
803       unsigned Task, BitcodeModule BM,
804       const FunctionImporter::ImportMapTy &ImportList,
805       const FunctionImporter::ExportSetTy &ExportList,
806       const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
807       MapVector<StringRef, BitcodeModule> &ModuleMap) override {
808     StringRef ModulePath = BM.getModuleIdentifier();
809     std::string NewModulePath =
810         getThinLTOOutputFile(ModulePath, OldPrefix, NewPrefix);
811 
812     std::error_code EC;
813     if (!LinkedObjectsFileName.empty()) {
814       if (!LinkedObjectsFile) {
815         LinkedObjectsFile = llvm::make_unique<raw_fd_ostream>(
816             LinkedObjectsFileName, EC, sys::fs::OpenFlags::F_None);
817         if (EC)
818           return errorCodeToError(EC);
819       }
820       *LinkedObjectsFile << NewModulePath << '\n';
821     }
822 
823     std::map<std::string, GVSummaryMapTy> ModuleToSummariesForIndex;
824     gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries,
825                                      ImportList, ModuleToSummariesForIndex);
826 
827     raw_fd_ostream OS(NewModulePath + ".thinlto.bc", EC,
828                       sys::fs::OpenFlags::F_None);
829     if (EC)
830       return errorCodeToError(EC);
831     WriteIndexToFile(CombinedIndex, OS, &ModuleToSummariesForIndex);
832 
833     if (ShouldEmitImportsFiles)
834       return errorCodeToError(
835           EmitImportsFiles(ModulePath, NewModulePath + ".imports", ImportList));
836     return Error::success();
837   }
838 
839   Error wait() override { return Error::success(); }
840 };
841 } // end anonymous namespace
842 
843 ThinBackend lto::createWriteIndexesThinBackend(std::string OldPrefix,
844                                                std::string NewPrefix,
845                                                bool ShouldEmitImportsFiles,
846                                                std::string LinkedObjectsFile) {
847   return [=](Config &Conf, ModuleSummaryIndex &CombinedIndex,
848              const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
849              AddStreamFn AddStream, NativeObjectCache Cache) {
850     return llvm::make_unique<WriteIndexesThinBackend>(
851         Conf, CombinedIndex, ModuleToDefinedGVSummaries, OldPrefix, NewPrefix,
852         ShouldEmitImportsFiles, LinkedObjectsFile);
853   };
854 }
855 
856 Error LTO::runThinLTO(AddStreamFn AddStream, NativeObjectCache Cache,
857                       bool HasRegularLTO) {
858   if (ThinLTO.ModuleMap.empty())
859     return Error::success();
860 
861   if (Conf.CombinedIndexHook && !Conf.CombinedIndexHook(ThinLTO.CombinedIndex))
862     return Error::success();
863 
864   // Collect for each module the list of function it defines (GUID ->
865   // Summary).
866   StringMap<std::map<GlobalValue::GUID, GlobalValueSummary *>>
867       ModuleToDefinedGVSummaries(ThinLTO.ModuleMap.size());
868   ThinLTO.CombinedIndex.collectDefinedGVSummariesPerModule(
869       ModuleToDefinedGVSummaries);
870   // Create entries for any modules that didn't have any GV summaries
871   // (either they didn't have any GVs to start with, or we suppressed
872   // generation of the summaries because they e.g. had inline assembly
873   // uses that couldn't be promoted/renamed on export). This is so
874   // InProcessThinBackend::start can still launch a backend thread, which
875   // is passed the map of summaries for the module, without any special
876   // handling for this case.
877   for (auto &Mod : ThinLTO.ModuleMap)
878     if (!ModuleToDefinedGVSummaries.count(Mod.first))
879       ModuleToDefinedGVSummaries.try_emplace(Mod.first);
880 
881   StringMap<FunctionImporter::ImportMapTy> ImportLists(
882       ThinLTO.ModuleMap.size());
883   StringMap<FunctionImporter::ExportSetTy> ExportLists(
884       ThinLTO.ModuleMap.size());
885   StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
886 
887   if (Conf.OptLevel > 0) {
888     // Compute "dead" symbols, we don't want to import/export these!
889     DenseSet<GlobalValue::GUID> GUIDPreservedSymbols;
890     for (auto &Res : GlobalResolutions) {
891       if (Res.second.VisibleOutsideThinLTO &&
892           // IRName will be defined if we have seen the prevailing copy of
893           // this value. If not, no need to preserve any ThinLTO copies.
894           !Res.second.IRName.empty())
895         GUIDPreservedSymbols.insert(GlobalValue::getGUID(Res.second.IRName));
896     }
897 
898     auto DeadSymbols =
899         computeDeadSymbols(ThinLTO.CombinedIndex, GUIDPreservedSymbols);
900 
901     ComputeCrossModuleImport(ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
902                              ImportLists, ExportLists, &DeadSymbols);
903 
904     std::set<GlobalValue::GUID> ExportedGUIDs;
905     for (auto &Res : GlobalResolutions) {
906       // First check if the symbol was flagged as having external references.
907       if (Res.second.Partition != GlobalResolution::External)
908         continue;
909       // IRName will be defined if we have seen the prevailing copy of
910       // this value. If not, no need to mark as exported from a ThinLTO
911       // partition (and we can't get the GUID).
912       if (Res.second.IRName.empty())
913         continue;
914       auto GUID = GlobalValue::getGUID(Res.second.IRName);
915       // Mark exported unless index-based analysis determined it to be dead.
916       if (!DeadSymbols.count(GUID))
917         ExportedGUIDs.insert(GlobalValue::getGUID(Res.second.IRName));
918     }
919 
920     auto isPrevailing = [&](GlobalValue::GUID GUID,
921                             const GlobalValueSummary *S) {
922       return ThinLTO.PrevailingModuleForGUID[GUID] == S->modulePath();
923     };
924     auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) {
925       const auto &ExportList = ExportLists.find(ModuleIdentifier);
926       return (ExportList != ExportLists.end() &&
927               ExportList->second.count(GUID)) ||
928              ExportedGUIDs.count(GUID);
929     };
930     thinLTOInternalizeAndPromoteInIndex(ThinLTO.CombinedIndex, isExported);
931 
932     auto recordNewLinkage = [&](StringRef ModuleIdentifier,
933                                 GlobalValue::GUID GUID,
934                                 GlobalValue::LinkageTypes NewLinkage) {
935       ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
936     };
937 
938     thinLTOResolveWeakForLinkerInIndex(ThinLTO.CombinedIndex, isPrevailing,
939                                        recordNewLinkage);
940   }
941 
942   std::unique_ptr<ThinBackendProc> BackendProc =
943       ThinLTO.Backend(Conf, ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
944                       AddStream, Cache);
945 
946   // Task numbers start at ParallelCodeGenParallelismLevel if an LTO
947   // module is present, as tasks 0 through ParallelCodeGenParallelismLevel-1
948   // are reserved for parallel code generation partitions.
949   unsigned Task =
950       HasRegularLTO ? RegularLTO.ParallelCodeGenParallelismLevel : 0;
951   for (auto &Mod : ThinLTO.ModuleMap) {
952     if (Error E = BackendProc->start(Task, Mod.second, ImportLists[Mod.first],
953                                      ExportLists[Mod.first],
954                                      ResolvedODR[Mod.first], ThinLTO.ModuleMap))
955       return E;
956     ++Task;
957   }
958 
959   return BackendProc->wait();
960 }
961