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