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