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