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