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