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/ScopeExit.h"
15 #include "llvm/ADT/SmallSet.h"
16 #include "llvm/ADT/Statistic.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
19 #include "llvm/Analysis/StackSafetyAnalysis.h"
20 #include "llvm/Analysis/TargetLibraryInfo.h"
21 #include "llvm/Analysis/TargetTransformInfo.h"
22 #include "llvm/Bitcode/BitcodeReader.h"
23 #include "llvm/Bitcode/BitcodeWriter.h"
24 #include "llvm/CodeGen/Analysis.h"
25 #include "llvm/Config/llvm-config.h"
26 #include "llvm/IR/AutoUpgrade.h"
27 #include "llvm/IR/DiagnosticPrinter.h"
28 #include "llvm/IR/Intrinsics.h"
29 #include "llvm/IR/LLVMRemarkStreamer.h"
30 #include "llvm/IR/LegacyPassManager.h"
31 #include "llvm/IR/Mangler.h"
32 #include "llvm/IR/Metadata.h"
33 #include "llvm/LTO/LTOBackend.h"
34 #include "llvm/LTO/SummaryBasedOptimizations.h"
35 #include "llvm/Linker/IRMover.h"
36 #include "llvm/MC/TargetRegistry.h"
37 #include "llvm/Object/IRObjectFile.h"
38 #include "llvm/Support/CommandLine.h"
39 #include "llvm/Support/Error.h"
40 #include "llvm/Support/FileSystem.h"
41 #include "llvm/Support/ManagedStatic.h"
42 #include "llvm/Support/MemoryBuffer.h"
43 #include "llvm/Support/Path.h"
44 #include "llvm/Support/SHA1.h"
45 #include "llvm/Support/SourceMgr.h"
46 #include "llvm/Support/ThreadPool.h"
47 #include "llvm/Support/Threading.h"
48 #include "llvm/Support/TimeProfiler.h"
49 #include "llvm/Support/ToolOutputFile.h"
50 #include "llvm/Support/VCSRevision.h"
51 #include "llvm/Support/raw_ostream.h"
52 #include "llvm/Target/TargetOptions.h"
53 #include "llvm/Transforms/IPO.h"
54 #include "llvm/Transforms/IPO/WholeProgramDevirt.h"
55 #include "llvm/Transforms/Utils/FunctionImportUtils.h"
56 #include "llvm/Transforms/Utils/SplitModule.h"
57 
58 #include <optional>
59 #include <set>
60 
61 using namespace llvm;
62 using namespace lto;
63 using namespace object;
64 
65 #define DEBUG_TYPE "lto"
66 
67 static cl::opt<bool>
68     DumpThinCGSCCs("dump-thin-cg-sccs", cl::init(false), cl::Hidden,
69                    cl::desc("Dump the SCCs in the ThinLTO index's callgraph"));
70 
71 namespace llvm {
72 /// Enable global value internalization in LTO.
73 cl::opt<bool> EnableLTOInternalization(
74     "enable-lto-internalization", cl::init(true), cl::Hidden,
75     cl::desc("Enable global value internalization in LTO"));
76 }
77 
78 // Computes a unique hash for the Module considering the current list of
79 // export/import and other global analysis results.
80 // The hash is produced in \p Key.
81 void llvm::computeLTOCacheKey(
82     SmallString<40> &Key, const Config &Conf, const ModuleSummaryIndex &Index,
83     StringRef ModuleID, const FunctionImporter::ImportMapTy &ImportList,
84     const FunctionImporter::ExportSetTy &ExportList,
85     const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
86     const GVSummaryMapTy &DefinedGlobals,
87     const std::set<GlobalValue::GUID> &CfiFunctionDefs,
88     const std::set<GlobalValue::GUID> &CfiFunctionDecls) {
89   // Compute the unique hash for this entry.
90   // This is based on the current compiler version, the module itself, the
91   // export list, the hash for every single module in the import list, the
92   // list of ResolvedODR for the module, and the list of preserved symbols.
93   SHA1 Hasher;
94 
95   // Start with the compiler revision
96   Hasher.update(LLVM_VERSION_STRING);
97 #ifdef LLVM_REVISION
98   Hasher.update(LLVM_REVISION);
99 #endif
100 
101   // Include the parts of the LTO configuration that affect code generation.
102   auto AddString = [&](StringRef Str) {
103     Hasher.update(Str);
104     Hasher.update(ArrayRef<uint8_t>{0});
105   };
106   auto AddUnsigned = [&](unsigned I) {
107     uint8_t Data[4];
108     support::endian::write32le(Data, I);
109     Hasher.update(ArrayRef<uint8_t>{Data, 4});
110   };
111   auto AddUint64 = [&](uint64_t I) {
112     uint8_t Data[8];
113     support::endian::write64le(Data, I);
114     Hasher.update(ArrayRef<uint8_t>{Data, 8});
115   };
116   AddString(Conf.CPU);
117   // FIXME: Hash more of Options. For now all clients initialize Options from
118   // command-line flags (which is unsupported in production), but may set
119   // RelaxELFRelocations. The clang driver can also pass FunctionSections,
120   // DataSections and DebuggerTuning via command line flags.
121   AddUnsigned(Conf.Options.RelaxELFRelocations);
122   AddUnsigned(Conf.Options.FunctionSections);
123   AddUnsigned(Conf.Options.DataSections);
124   AddUnsigned((unsigned)Conf.Options.DebuggerTuning);
125   for (auto &A : Conf.MAttrs)
126     AddString(A);
127   if (Conf.RelocModel)
128     AddUnsigned(*Conf.RelocModel);
129   else
130     AddUnsigned(-1);
131   if (Conf.CodeModel)
132     AddUnsigned(*Conf.CodeModel);
133   else
134     AddUnsigned(-1);
135   for (const auto &S : Conf.MllvmArgs)
136     AddString(S);
137   AddUnsigned(Conf.CGOptLevel);
138   AddUnsigned(Conf.CGFileType);
139   AddUnsigned(Conf.OptLevel);
140   AddUnsigned(Conf.Freestanding);
141   AddString(Conf.OptPipeline);
142   AddString(Conf.AAPipeline);
143   AddString(Conf.OverrideTriple);
144   AddString(Conf.DefaultTriple);
145   AddString(Conf.DwoDir);
146 
147   // Include the hash for the current module
148   auto ModHash = Index.getModuleHash(ModuleID);
149   Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
150 
151   std::vector<uint64_t> ExportsGUID;
152   ExportsGUID.reserve(ExportList.size());
153   for (const auto &VI : ExportList) {
154     auto GUID = VI.getGUID();
155     ExportsGUID.push_back(GUID);
156   }
157 
158   // Sort the export list elements GUIDs.
159   llvm::sort(ExportsGUID);
160   for (uint64_t GUID : ExportsGUID) {
161     // The export list can impact the internalization, be conservative here
162     Hasher.update(ArrayRef<uint8_t>((uint8_t *)&GUID, sizeof(GUID)));
163   }
164 
165   // Include the hash for every module we import functions from. The set of
166   // imported symbols for each module may affect code generation and is
167   // sensitive to link order, so include that as well.
168   using ImportMapIteratorTy = FunctionImporter::ImportMapTy::const_iterator;
169   std::vector<ImportMapIteratorTy> ImportModulesVector;
170   ImportModulesVector.reserve(ImportList.size());
171 
172   for (ImportMapIteratorTy It = ImportList.begin(); It != ImportList.end();
173        ++It) {
174     ImportModulesVector.push_back(It);
175   }
176   llvm::sort(ImportModulesVector,
177              [](const ImportMapIteratorTy &Lhs, const ImportMapIteratorTy &Rhs)
178                  -> bool { return Lhs->getKey() < Rhs->getKey(); });
179   for (const ImportMapIteratorTy &EntryIt : ImportModulesVector) {
180     auto ModHash = Index.getModuleHash(EntryIt->first());
181     Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
182 
183     AddUint64(EntryIt->second.size());
184     for (auto &Fn : EntryIt->second)
185       AddUint64(Fn);
186   }
187 
188   // Include the hash for the resolved ODR.
189   for (auto &Entry : ResolvedODR) {
190     Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.first,
191                                     sizeof(GlobalValue::GUID)));
192     Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.second,
193                                     sizeof(GlobalValue::LinkageTypes)));
194   }
195 
196   // Members of CfiFunctionDefs and CfiFunctionDecls that are referenced or
197   // defined in this module.
198   std::set<GlobalValue::GUID> UsedCfiDefs;
199   std::set<GlobalValue::GUID> UsedCfiDecls;
200 
201   // Typeids used in this module.
202   std::set<GlobalValue::GUID> UsedTypeIds;
203 
204   auto AddUsedCfiGlobal = [&](GlobalValue::GUID ValueGUID) {
205     if (CfiFunctionDefs.count(ValueGUID))
206       UsedCfiDefs.insert(ValueGUID);
207     if (CfiFunctionDecls.count(ValueGUID))
208       UsedCfiDecls.insert(ValueGUID);
209   };
210 
211   auto AddUsedThings = [&](GlobalValueSummary *GS) {
212     if (!GS) return;
213     AddUnsigned(GS->getVisibility());
214     AddUnsigned(GS->isLive());
215     AddUnsigned(GS->canAutoHide());
216     for (const ValueInfo &VI : GS->refs()) {
217       AddUnsigned(VI.isDSOLocal(Index.withDSOLocalPropagation()));
218       AddUsedCfiGlobal(VI.getGUID());
219     }
220     if (auto *GVS = dyn_cast<GlobalVarSummary>(GS)) {
221       AddUnsigned(GVS->maybeReadOnly());
222       AddUnsigned(GVS->maybeWriteOnly());
223     }
224     if (auto *FS = dyn_cast<FunctionSummary>(GS)) {
225       for (auto &TT : FS->type_tests())
226         UsedTypeIds.insert(TT);
227       for (auto &TT : FS->type_test_assume_vcalls())
228         UsedTypeIds.insert(TT.GUID);
229       for (auto &TT : FS->type_checked_load_vcalls())
230         UsedTypeIds.insert(TT.GUID);
231       for (auto &TT : FS->type_test_assume_const_vcalls())
232         UsedTypeIds.insert(TT.VFunc.GUID);
233       for (auto &TT : FS->type_checked_load_const_vcalls())
234         UsedTypeIds.insert(TT.VFunc.GUID);
235       for (auto &ET : FS->calls()) {
236         AddUnsigned(ET.first.isDSOLocal(Index.withDSOLocalPropagation()));
237         AddUsedCfiGlobal(ET.first.getGUID());
238       }
239     }
240   };
241 
242   // Include the hash for the linkage type to reflect internalization and weak
243   // resolution, and collect any used type identifier resolutions.
244   for (auto &GS : DefinedGlobals) {
245     GlobalValue::LinkageTypes Linkage = GS.second->linkage();
246     Hasher.update(
247         ArrayRef<uint8_t>((const uint8_t *)&Linkage, sizeof(Linkage)));
248     AddUsedCfiGlobal(GS.first);
249     AddUsedThings(GS.second);
250   }
251 
252   // Imported functions may introduce new uses of type identifier resolutions,
253   // so we need to collect their used resolutions as well.
254   for (auto &ImpM : ImportList)
255     for (auto &ImpF : ImpM.second) {
256       GlobalValueSummary *S = Index.findSummaryInModule(ImpF, ImpM.first());
257       AddUsedThings(S);
258       // If this is an alias, we also care about any types/etc. that the aliasee
259       // may reference.
260       if (auto *AS = dyn_cast_or_null<AliasSummary>(S))
261         AddUsedThings(AS->getBaseObject());
262     }
263 
264   auto AddTypeIdSummary = [&](StringRef TId, const TypeIdSummary &S) {
265     AddString(TId);
266 
267     AddUnsigned(S.TTRes.TheKind);
268     AddUnsigned(S.TTRes.SizeM1BitWidth);
269 
270     AddUint64(S.TTRes.AlignLog2);
271     AddUint64(S.TTRes.SizeM1);
272     AddUint64(S.TTRes.BitMask);
273     AddUint64(S.TTRes.InlineBits);
274 
275     AddUint64(S.WPDRes.size());
276     for (auto &WPD : S.WPDRes) {
277       AddUnsigned(WPD.first);
278       AddUnsigned(WPD.second.TheKind);
279       AddString(WPD.second.SingleImplName);
280 
281       AddUint64(WPD.second.ResByArg.size());
282       for (auto &ByArg : WPD.second.ResByArg) {
283         AddUint64(ByArg.first.size());
284         for (uint64_t Arg : ByArg.first)
285           AddUint64(Arg);
286         AddUnsigned(ByArg.second.TheKind);
287         AddUint64(ByArg.second.Info);
288         AddUnsigned(ByArg.second.Byte);
289         AddUnsigned(ByArg.second.Bit);
290       }
291     }
292   };
293 
294   // Include the hash for all type identifiers used by this module.
295   for (GlobalValue::GUID TId : UsedTypeIds) {
296     auto TidIter = Index.typeIds().equal_range(TId);
297     for (auto It = TidIter.first; It != TidIter.second; ++It)
298       AddTypeIdSummary(It->second.first, It->second.second);
299   }
300 
301   AddUnsigned(UsedCfiDefs.size());
302   for (auto &V : UsedCfiDefs)
303     AddUint64(V);
304 
305   AddUnsigned(UsedCfiDecls.size());
306   for (auto &V : UsedCfiDecls)
307     AddUint64(V);
308 
309   if (!Conf.SampleProfile.empty()) {
310     auto FileOrErr = MemoryBuffer::getFile(Conf.SampleProfile);
311     if (FileOrErr) {
312       Hasher.update(FileOrErr.get()->getBuffer());
313 
314       if (!Conf.ProfileRemapping.empty()) {
315         FileOrErr = MemoryBuffer::getFile(Conf.ProfileRemapping);
316         if (FileOrErr)
317           Hasher.update(FileOrErr.get()->getBuffer());
318       }
319     }
320   }
321 
322   Key = toHex(Hasher.result());
323 }
324 
325 static void thinLTOResolvePrevailingGUID(
326     const Config &C, ValueInfo VI,
327     DenseSet<GlobalValueSummary *> &GlobalInvolvedWithAlias,
328     function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
329         isPrevailing,
330     function_ref<void(StringRef, GlobalValue::GUID, GlobalValue::LinkageTypes)>
331         recordNewLinkage,
332     const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
333   GlobalValue::VisibilityTypes Visibility =
334       C.VisibilityScheme == Config::ELF ? VI.getELFVisibility()
335                                         : GlobalValue::DefaultVisibility;
336   for (auto &S : VI.getSummaryList()) {
337     GlobalValue::LinkageTypes OriginalLinkage = S->linkage();
338     // Ignore local and appending linkage values since the linker
339     // doesn't resolve them.
340     if (GlobalValue::isLocalLinkage(OriginalLinkage) ||
341         GlobalValue::isAppendingLinkage(S->linkage()))
342       continue;
343     // We need to emit only one of these. The prevailing module will keep it,
344     // but turned into a weak, while the others will drop it when possible.
345     // This is both a compile-time optimization and a correctness
346     // transformation. This is necessary for correctness when we have exported
347     // a reference - we need to convert the linkonce to weak to
348     // ensure a copy is kept to satisfy the exported reference.
349     // FIXME: We may want to split the compile time and correctness
350     // aspects into separate routines.
351     if (isPrevailing(VI.getGUID(), S.get())) {
352       if (GlobalValue::isLinkOnceLinkage(OriginalLinkage)) {
353         S->setLinkage(GlobalValue::getWeakLinkage(
354             GlobalValue::isLinkOnceODRLinkage(OriginalLinkage)));
355         // The kept copy is eligible for auto-hiding (hidden visibility) if all
356         // copies were (i.e. they were all linkonce_odr global unnamed addr).
357         // If any copy is not (e.g. it was originally weak_odr), then the symbol
358         // must remain externally available (e.g. a weak_odr from an explicitly
359         // instantiated template). Additionally, if it is in the
360         // GUIDPreservedSymbols set, that means that it is visibile outside
361         // the summary (e.g. in a native object or a bitcode file without
362         // summary), and in that case we cannot hide it as it isn't possible to
363         // check all copies.
364         S->setCanAutoHide(VI.canAutoHide() &&
365                           !GUIDPreservedSymbols.count(VI.getGUID()));
366       }
367       if (C.VisibilityScheme == Config::FromPrevailing)
368         Visibility = S->getVisibility();
369     }
370     // Alias and aliasee can't be turned into available_externally.
371     else if (!isa<AliasSummary>(S.get()) &&
372              !GlobalInvolvedWithAlias.count(S.get()))
373       S->setLinkage(GlobalValue::AvailableExternallyLinkage);
374 
375     // For ELF, set visibility to the computed visibility from summaries. We
376     // don't track visibility from declarations so this may be more relaxed than
377     // the most constraining one.
378     if (C.VisibilityScheme == Config::ELF)
379       S->setVisibility(Visibility);
380 
381     if (S->linkage() != OriginalLinkage)
382       recordNewLinkage(S->modulePath(), VI.getGUID(), S->linkage());
383   }
384 
385   if (C.VisibilityScheme == Config::FromPrevailing) {
386     for (auto &S : VI.getSummaryList()) {
387       GlobalValue::LinkageTypes OriginalLinkage = S->linkage();
388       if (GlobalValue::isLocalLinkage(OriginalLinkage) ||
389           GlobalValue::isAppendingLinkage(S->linkage()))
390         continue;
391       S->setVisibility(Visibility);
392     }
393   }
394 }
395 
396 /// Resolve linkage for prevailing symbols in the \p Index.
397 //
398 // We'd like to drop these functions if they are no longer referenced in the
399 // current module. However there is a chance that another module is still
400 // referencing them because of the import. We make sure we always emit at least
401 // one copy.
402 void llvm::thinLTOResolvePrevailingInIndex(
403     const Config &C, ModuleSummaryIndex &Index,
404     function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
405         isPrevailing,
406     function_ref<void(StringRef, GlobalValue::GUID, GlobalValue::LinkageTypes)>
407         recordNewLinkage,
408     const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
409   // We won't optimize the globals that are referenced by an alias for now
410   // Ideally we should turn the alias into a global and duplicate the definition
411   // when needed.
412   DenseSet<GlobalValueSummary *> GlobalInvolvedWithAlias;
413   for (auto &I : Index)
414     for (auto &S : I.second.SummaryList)
415       if (auto AS = dyn_cast<AliasSummary>(S.get()))
416         GlobalInvolvedWithAlias.insert(&AS->getAliasee());
417 
418   for (auto &I : Index)
419     thinLTOResolvePrevailingGUID(C, Index.getValueInfo(I),
420                                  GlobalInvolvedWithAlias, isPrevailing,
421                                  recordNewLinkage, GUIDPreservedSymbols);
422 }
423 
424 static bool isWeakObjectWithRWAccess(GlobalValueSummary *GVS) {
425   if (auto *VarSummary = dyn_cast<GlobalVarSummary>(GVS->getBaseObject()))
426     return !VarSummary->maybeReadOnly() && !VarSummary->maybeWriteOnly() &&
427            (VarSummary->linkage() == GlobalValue::WeakODRLinkage ||
428             VarSummary->linkage() == GlobalValue::LinkOnceODRLinkage);
429   return false;
430 }
431 
432 static void thinLTOInternalizeAndPromoteGUID(
433     ValueInfo VI, function_ref<bool(StringRef, ValueInfo)> isExported,
434     function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
435         isPrevailing) {
436   for (auto &S : VI.getSummaryList()) {
437     if (isExported(S->modulePath(), VI)) {
438       if (GlobalValue::isLocalLinkage(S->linkage()))
439         S->setLinkage(GlobalValue::ExternalLinkage);
440     } else if (EnableLTOInternalization &&
441                // Ignore local and appending linkage values since the linker
442                // doesn't resolve them.
443                !GlobalValue::isLocalLinkage(S->linkage()) &&
444                (!GlobalValue::isInterposableLinkage(S->linkage()) ||
445                 isPrevailing(VI.getGUID(), S.get())) &&
446                S->linkage() != GlobalValue::AppendingLinkage &&
447                // We can't internalize available_externally globals because this
448                // can break function pointer equality.
449                S->linkage() != GlobalValue::AvailableExternallyLinkage &&
450                // Functions and read-only variables with linkonce_odr and
451                // weak_odr linkage can be internalized. We can't internalize
452                // linkonce_odr and weak_odr variables which are both modified
453                // and read somewhere in the program because reads and writes
454                // will become inconsistent.
455                !isWeakObjectWithRWAccess(S.get()))
456       S->setLinkage(GlobalValue::InternalLinkage);
457   }
458 }
459 
460 // Update the linkages in the given \p Index to mark exported values
461 // as external and non-exported values as internal.
462 void llvm::thinLTOInternalizeAndPromoteInIndex(
463     ModuleSummaryIndex &Index,
464     function_ref<bool(StringRef, ValueInfo)> isExported,
465     function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
466         isPrevailing) {
467   for (auto &I : Index)
468     thinLTOInternalizeAndPromoteGUID(Index.getValueInfo(I), isExported,
469                                      isPrevailing);
470 }
471 
472 // Requires a destructor for std::vector<InputModule>.
473 InputFile::~InputFile() = default;
474 
475 Expected<std::unique_ptr<InputFile>> InputFile::create(MemoryBufferRef Object) {
476   std::unique_ptr<InputFile> File(new InputFile);
477 
478   Expected<IRSymtabFile> FOrErr = readIRSymtab(Object);
479   if (!FOrErr)
480     return FOrErr.takeError();
481 
482   File->TargetTriple = FOrErr->TheReader.getTargetTriple();
483   File->SourceFileName = FOrErr->TheReader.getSourceFileName();
484   File->COFFLinkerOpts = FOrErr->TheReader.getCOFFLinkerOpts();
485   File->DependentLibraries = FOrErr->TheReader.getDependentLibraries();
486   File->ComdatTable = FOrErr->TheReader.getComdatTable();
487 
488   for (unsigned I = 0; I != FOrErr->Mods.size(); ++I) {
489     size_t Begin = File->Symbols.size();
490     for (const irsymtab::Reader::SymbolRef &Sym :
491          FOrErr->TheReader.module_symbols(I))
492       // Skip symbols that are irrelevant to LTO. Note that this condition needs
493       // to match the one in Skip() in LTO::addRegularLTO().
494       if (Sym.isGlobal() && !Sym.isFormatSpecific())
495         File->Symbols.push_back(Sym);
496     File->ModuleSymIndices.push_back({Begin, File->Symbols.size()});
497   }
498 
499   File->Mods = FOrErr->Mods;
500   File->Strtab = std::move(FOrErr->Strtab);
501   return std::move(File);
502 }
503 
504 StringRef InputFile::getName() const {
505   return Mods[0].getModuleIdentifier();
506 }
507 
508 BitcodeModule &InputFile::getSingleBitcodeModule() {
509   assert(Mods.size() == 1 && "Expect only one bitcode module");
510   return Mods[0];
511 }
512 
513 LTO::RegularLTOState::RegularLTOState(unsigned ParallelCodeGenParallelismLevel,
514                                       const Config &Conf)
515     : ParallelCodeGenParallelismLevel(ParallelCodeGenParallelismLevel),
516       Ctx(Conf), CombinedModule(std::make_unique<Module>("ld-temp.o", Ctx)),
517       Mover(std::make_unique<IRMover>(*CombinedModule)) {}
518 
519 LTO::ThinLTOState::ThinLTOState(ThinBackend Backend)
520     : Backend(Backend), CombinedIndex(/*HaveGVs*/ false) {
521   if (!Backend)
522     this->Backend =
523         createInProcessThinBackend(llvm::heavyweight_hardware_concurrency());
524 }
525 
526 LTO::LTO(Config Conf, ThinBackend Backend,
527          unsigned ParallelCodeGenParallelismLevel)
528     : Conf(std::move(Conf)),
529       RegularLTO(ParallelCodeGenParallelismLevel, this->Conf),
530       ThinLTO(std::move(Backend)) {}
531 
532 // Requires a destructor for MapVector<BitcodeModule>.
533 LTO::~LTO() = default;
534 
535 // Add the symbols in the given module to the GlobalResolutions map, and resolve
536 // their partitions.
537 void LTO::addModuleToGlobalRes(ArrayRef<InputFile::Symbol> Syms,
538                                ArrayRef<SymbolResolution> Res,
539                                unsigned Partition, bool InSummary) {
540   auto *ResI = Res.begin();
541   auto *ResE = Res.end();
542   (void)ResE;
543   const Triple TT(RegularLTO.CombinedModule->getTargetTriple());
544   for (const InputFile::Symbol &Sym : Syms) {
545     assert(ResI != ResE);
546     SymbolResolution Res = *ResI++;
547 
548     StringRef Name = Sym.getName();
549     // Strip the __imp_ prefix from COFF dllimport symbols (similar to the
550     // way they are handled by lld), otherwise we can end up with two
551     // global resolutions (one with and one for a copy of the symbol without).
552     if (TT.isOSBinFormatCOFF() && Name.startswith("__imp_"))
553       Name = Name.substr(strlen("__imp_"));
554     auto &GlobalRes = GlobalResolutions[Name];
555     GlobalRes.UnnamedAddr &= Sym.isUnnamedAddr();
556     if (Res.Prevailing) {
557       assert(!GlobalRes.Prevailing &&
558              "Multiple prevailing defs are not allowed");
559       GlobalRes.Prevailing = true;
560       GlobalRes.IRName = std::string(Sym.getIRName());
561     } else if (!GlobalRes.Prevailing && GlobalRes.IRName.empty()) {
562       // Sometimes it can be two copies of symbol in a module and prevailing
563       // symbol can have no IR name. That might happen if symbol is defined in
564       // module level inline asm block. In case we have multiple modules with
565       // the same symbol we want to use IR name of the prevailing symbol.
566       // Otherwise, if we haven't seen a prevailing symbol, set the name so that
567       // we can later use it to check if there is any prevailing copy in IR.
568       GlobalRes.IRName = std::string(Sym.getIRName());
569     }
570 
571     // In rare occasion, the symbol used to initialize GlobalRes has a different
572     // IRName from the inspected Symbol. This can happen on macOS + iOS, when a
573     // symbol is referenced through its mangled name, say @"\01_symbol" while
574     // the IRName is @symbol (the prefix underscore comes from MachO mangling).
575     // In that case, we have the same actual Symbol that can get two different
576     // GUID, leading to some invalid internalization. Workaround this by marking
577     // the GlobalRes external.
578 
579     // FIXME: instead of this check, it would be desirable to compute GUIDs
580     // based on mangled name, but this requires an access to the Target Triple
581     // and would be relatively invasive on the codebase.
582     if (GlobalRes.IRName != Sym.getIRName()) {
583       GlobalRes.Partition = GlobalResolution::External;
584       GlobalRes.VisibleOutsideSummary = true;
585     }
586 
587     // Set the partition to external if we know it is re-defined by the linker
588     // with -defsym or -wrap options, used elsewhere, e.g. it is visible to a
589     // regular object, is referenced from llvm.compiler.used/llvm.used, or was
590     // already recorded as being referenced from a different partition.
591     if (Res.LinkerRedefined || Res.VisibleToRegularObj || Sym.isUsed() ||
592         (GlobalRes.Partition != GlobalResolution::Unknown &&
593          GlobalRes.Partition != Partition)) {
594       GlobalRes.Partition = GlobalResolution::External;
595     } else
596       // First recorded reference, save the current partition.
597       GlobalRes.Partition = Partition;
598 
599     // Flag as visible outside of summary if visible from a regular object or
600     // from a module that does not have a summary.
601     GlobalRes.VisibleOutsideSummary |=
602         (Res.VisibleToRegularObj || Sym.isUsed() || !InSummary);
603 
604     GlobalRes.ExportDynamic |= Res.ExportDynamic;
605   }
606 }
607 
608 static void writeToResolutionFile(raw_ostream &OS, InputFile *Input,
609                                   ArrayRef<SymbolResolution> Res) {
610   StringRef Path = Input->getName();
611   OS << Path << '\n';
612   auto ResI = Res.begin();
613   for (const InputFile::Symbol &Sym : Input->symbols()) {
614     assert(ResI != Res.end());
615     SymbolResolution Res = *ResI++;
616 
617     OS << "-r=" << Path << ',' << Sym.getName() << ',';
618     if (Res.Prevailing)
619       OS << 'p';
620     if (Res.FinalDefinitionInLinkageUnit)
621       OS << 'l';
622     if (Res.VisibleToRegularObj)
623       OS << 'x';
624     if (Res.LinkerRedefined)
625       OS << 'r';
626     OS << '\n';
627   }
628   OS.flush();
629   assert(ResI == Res.end());
630 }
631 
632 Error LTO::add(std::unique_ptr<InputFile> Input,
633                ArrayRef<SymbolResolution> Res) {
634   assert(!CalledGetMaxTasks);
635 
636   if (Conf.ResolutionFile)
637     writeToResolutionFile(*Conf.ResolutionFile, Input.get(), Res);
638 
639   if (RegularLTO.CombinedModule->getTargetTriple().empty()) {
640     RegularLTO.CombinedModule->setTargetTriple(Input->getTargetTriple());
641     if (Triple(Input->getTargetTriple()).isOSBinFormatELF())
642       Conf.VisibilityScheme = Config::ELF;
643   }
644 
645   const SymbolResolution *ResI = Res.begin();
646   for (unsigned I = 0; I != Input->Mods.size(); ++I)
647     if (Error Err = addModule(*Input, I, ResI, Res.end()))
648       return Err;
649 
650   assert(ResI == Res.end());
651   return Error::success();
652 }
653 
654 Error LTO::addModule(InputFile &Input, unsigned ModI,
655                      const SymbolResolution *&ResI,
656                      const SymbolResolution *ResE) {
657   Expected<BitcodeLTOInfo> LTOInfo = Input.Mods[ModI].getLTOInfo();
658   if (!LTOInfo)
659     return LTOInfo.takeError();
660 
661   if (EnableSplitLTOUnit) {
662     // If only some modules were split, flag this in the index so that
663     // we can skip or error on optimizations that need consistently split
664     // modules (whole program devirt and lower type tests).
665     if (*EnableSplitLTOUnit != LTOInfo->EnableSplitLTOUnit)
666       ThinLTO.CombinedIndex.setPartiallySplitLTOUnits();
667   } else
668     EnableSplitLTOUnit = LTOInfo->EnableSplitLTOUnit;
669 
670   BitcodeModule BM = Input.Mods[ModI];
671   auto ModSyms = Input.module_symbols(ModI);
672   addModuleToGlobalRes(ModSyms, {ResI, ResE},
673                        LTOInfo->IsThinLTO ? ThinLTO.ModuleMap.size() + 1 : 0,
674                        LTOInfo->HasSummary);
675 
676   if (LTOInfo->IsThinLTO)
677     return addThinLTO(BM, ModSyms, ResI, ResE);
678 
679   RegularLTO.EmptyCombinedModule = false;
680   Expected<RegularLTOState::AddedModule> ModOrErr =
681       addRegularLTO(BM, ModSyms, ResI, ResE);
682   if (!ModOrErr)
683     return ModOrErr.takeError();
684 
685   if (!LTOInfo->HasSummary)
686     return linkRegularLTO(std::move(*ModOrErr), /*LivenessFromIndex=*/false);
687 
688   // Regular LTO module summaries are added to a dummy module that represents
689   // the combined regular LTO module.
690   if (Error Err = BM.readSummary(ThinLTO.CombinedIndex, "", -1ull))
691     return Err;
692   RegularLTO.ModsWithSummaries.push_back(std::move(*ModOrErr));
693   return Error::success();
694 }
695 
696 // Checks whether the given global value is in a non-prevailing comdat
697 // (comdat containing values the linker indicated were not prevailing,
698 // which we then dropped to available_externally), and if so, removes
699 // it from the comdat. This is called for all global values to ensure the
700 // comdat is empty rather than leaving an incomplete comdat. It is needed for
701 // regular LTO modules, in case we are in a mixed-LTO mode (both regular
702 // and thin LTO modules) compilation. Since the regular LTO module will be
703 // linked first in the final native link, we want to make sure the linker
704 // doesn't select any of these incomplete comdats that would be left
705 // in the regular LTO module without this cleanup.
706 static void
707 handleNonPrevailingComdat(GlobalValue &GV,
708                           std::set<const Comdat *> &NonPrevailingComdats) {
709   Comdat *C = GV.getComdat();
710   if (!C)
711     return;
712 
713   if (!NonPrevailingComdats.count(C))
714     return;
715 
716   // Additionally need to drop all global values from the comdat to
717   // available_externally, to satisfy the COMDAT requirement that all members
718   // are discarded as a unit. The non-local linkage global values avoid
719   // duplicate definition linker errors.
720   GV.setLinkage(GlobalValue::AvailableExternallyLinkage);
721 
722   if (auto GO = dyn_cast<GlobalObject>(&GV))
723     GO->setComdat(nullptr);
724 }
725 
726 // Add a regular LTO object to the link.
727 // The resulting module needs to be linked into the combined LTO module with
728 // linkRegularLTO.
729 Expected<LTO::RegularLTOState::AddedModule>
730 LTO::addRegularLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
731                    const SymbolResolution *&ResI,
732                    const SymbolResolution *ResE) {
733   RegularLTOState::AddedModule Mod;
734   Expected<std::unique_ptr<Module>> MOrErr =
735       BM.getLazyModule(RegularLTO.Ctx, /*ShouldLazyLoadMetadata*/ true,
736                        /*IsImporting*/ false);
737   if (!MOrErr)
738     return MOrErr.takeError();
739   Module &M = **MOrErr;
740   Mod.M = std::move(*MOrErr);
741 
742   if (Error Err = M.materializeMetadata())
743     return std::move(Err);
744   UpgradeDebugInfo(M);
745 
746   ModuleSymbolTable SymTab;
747   SymTab.addModule(&M);
748 
749   for (GlobalVariable &GV : M.globals())
750     if (GV.hasAppendingLinkage())
751       Mod.Keep.push_back(&GV);
752 
753   DenseSet<GlobalObject *> AliasedGlobals;
754   for (auto &GA : M.aliases())
755     if (GlobalObject *GO = GA.getAliaseeObject())
756       AliasedGlobals.insert(GO);
757 
758   // In this function we need IR GlobalValues matching the symbols in Syms
759   // (which is not backed by a module), so we need to enumerate them in the same
760   // order. The symbol enumeration order of a ModuleSymbolTable intentionally
761   // matches the order of an irsymtab, but when we read the irsymtab in
762   // InputFile::create we omit some symbols that are irrelevant to LTO. The
763   // Skip() function skips the same symbols from the module as InputFile does
764   // from the symbol table.
765   auto MsymI = SymTab.symbols().begin(), MsymE = SymTab.symbols().end();
766   auto Skip = [&]() {
767     while (MsymI != MsymE) {
768       auto Flags = SymTab.getSymbolFlags(*MsymI);
769       if ((Flags & object::BasicSymbolRef::SF_Global) &&
770           !(Flags & object::BasicSymbolRef::SF_FormatSpecific))
771         return;
772       ++MsymI;
773     }
774   };
775   Skip();
776 
777   std::set<const Comdat *> NonPrevailingComdats;
778   SmallSet<StringRef, 2> NonPrevailingAsmSymbols;
779   for (const InputFile::Symbol &Sym : Syms) {
780     assert(ResI != ResE);
781     SymbolResolution Res = *ResI++;
782 
783     assert(MsymI != MsymE);
784     ModuleSymbolTable::Symbol Msym = *MsymI++;
785     Skip();
786 
787     if (GlobalValue *GV = Msym.dyn_cast<GlobalValue *>()) {
788       if (Res.Prevailing) {
789         if (Sym.isUndefined())
790           continue;
791         Mod.Keep.push_back(GV);
792         // For symbols re-defined with linker -wrap and -defsym options,
793         // set the linkage to weak to inhibit IPO. The linkage will be
794         // restored by the linker.
795         if (Res.LinkerRedefined)
796           GV->setLinkage(GlobalValue::WeakAnyLinkage);
797 
798         GlobalValue::LinkageTypes OriginalLinkage = GV->getLinkage();
799         if (GlobalValue::isLinkOnceLinkage(OriginalLinkage))
800           GV->setLinkage(GlobalValue::getWeakLinkage(
801               GlobalValue::isLinkOnceODRLinkage(OriginalLinkage)));
802       } else if (isa<GlobalObject>(GV) &&
803                  (GV->hasLinkOnceODRLinkage() || GV->hasWeakODRLinkage() ||
804                   GV->hasAvailableExternallyLinkage()) &&
805                  !AliasedGlobals.count(cast<GlobalObject>(GV))) {
806         // Any of the above three types of linkage indicates that the
807         // chosen prevailing symbol will have the same semantics as this copy of
808         // the symbol, so we may be able to link it with available_externally
809         // linkage. We will decide later whether to do that when we link this
810         // module (in linkRegularLTO), based on whether it is undefined.
811         Mod.Keep.push_back(GV);
812         GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
813         if (GV->hasComdat())
814           NonPrevailingComdats.insert(GV->getComdat());
815         cast<GlobalObject>(GV)->setComdat(nullptr);
816       }
817 
818       // Set the 'local' flag based on the linker resolution for this symbol.
819       if (Res.FinalDefinitionInLinkageUnit) {
820         GV->setDSOLocal(true);
821         if (GV->hasDLLImportStorageClass())
822           GV->setDLLStorageClass(GlobalValue::DLLStorageClassTypes::
823                                  DefaultStorageClass);
824       }
825     } else if (auto *AS = Msym.dyn_cast<ModuleSymbolTable::AsmSymbol *>()) {
826       // Collect non-prevailing symbols.
827       if (!Res.Prevailing)
828         NonPrevailingAsmSymbols.insert(AS->first);
829     } else {
830       llvm_unreachable("unknown symbol type");
831     }
832 
833     // Common resolution: collect the maximum size/alignment over all commons.
834     // We also record if we see an instance of a common as prevailing, so that
835     // if none is prevailing we can ignore it later.
836     if (Sym.isCommon()) {
837       // FIXME: We should figure out what to do about commons defined by asm.
838       // For now they aren't reported correctly by ModuleSymbolTable.
839       auto &CommonRes = RegularLTO.Commons[std::string(Sym.getIRName())];
840       CommonRes.Size = std::max(CommonRes.Size, Sym.getCommonSize());
841       if (uint32_t SymAlignValue = Sym.getCommonAlignment()) {
842         const Align SymAlign(SymAlignValue);
843         CommonRes.Align = std::max(SymAlign, CommonRes.Align.valueOrOne());
844       }
845       CommonRes.Prevailing |= Res.Prevailing;
846     }
847   }
848 
849   if (!M.getComdatSymbolTable().empty())
850     for (GlobalValue &GV : M.global_values())
851       handleNonPrevailingComdat(GV, NonPrevailingComdats);
852 
853   // Prepend ".lto_discard <sym>, <sym>*" directive to each module inline asm
854   // block.
855   if (!M.getModuleInlineAsm().empty()) {
856     std::string NewIA = ".lto_discard";
857     if (!NonPrevailingAsmSymbols.empty()) {
858       // Don't dicard a symbol if there is a live .symver for it.
859       ModuleSymbolTable::CollectAsmSymvers(
860           M, [&](StringRef Name, StringRef Alias) {
861             if (!NonPrevailingAsmSymbols.count(Alias))
862               NonPrevailingAsmSymbols.erase(Name);
863           });
864       NewIA += " " + llvm::join(NonPrevailingAsmSymbols, ", ");
865     }
866     NewIA += "\n";
867     M.setModuleInlineAsm(NewIA + M.getModuleInlineAsm());
868   }
869 
870   assert(MsymI == MsymE);
871   return std::move(Mod);
872 }
873 
874 Error LTO::linkRegularLTO(RegularLTOState::AddedModule Mod,
875                           bool LivenessFromIndex) {
876   std::vector<GlobalValue *> Keep;
877   for (GlobalValue *GV : Mod.Keep) {
878     if (LivenessFromIndex && !ThinLTO.CombinedIndex.isGUIDLive(GV->getGUID())) {
879       if (Function *F = dyn_cast<Function>(GV)) {
880         if (DiagnosticOutputFile) {
881           if (Error Err = F->materialize())
882             return Err;
883           OptimizationRemarkEmitter ORE(F, nullptr);
884           ORE.emit(OptimizationRemark(DEBUG_TYPE, "deadfunction", F)
885                    << ore::NV("Function", F)
886                    << " not added to the combined module ");
887         }
888       }
889       continue;
890     }
891 
892     if (!GV->hasAvailableExternallyLinkage()) {
893       Keep.push_back(GV);
894       continue;
895     }
896 
897     // Only link available_externally definitions if we don't already have a
898     // definition.
899     GlobalValue *CombinedGV =
900         RegularLTO.CombinedModule->getNamedValue(GV->getName());
901     if (CombinedGV && !CombinedGV->isDeclaration())
902       continue;
903 
904     Keep.push_back(GV);
905   }
906 
907   return RegularLTO.Mover->move(std::move(Mod.M), Keep, nullptr,
908                                 /* IsPerformingImport */ false);
909 }
910 
911 // Add a ThinLTO module to the link.
912 Error LTO::addThinLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
913                       const SymbolResolution *&ResI,
914                       const SymbolResolution *ResE) {
915   const SymbolResolution *ResITmp = ResI;
916   for (const InputFile::Symbol &Sym : Syms) {
917     assert(ResITmp != ResE);
918     SymbolResolution Res = *ResITmp++;
919 
920     if (!Sym.getIRName().empty()) {
921       auto GUID = GlobalValue::getGUID(GlobalValue::getGlobalIdentifier(
922           Sym.getIRName(), GlobalValue::ExternalLinkage, ""));
923       if (Res.Prevailing)
924         ThinLTO.PrevailingModuleForGUID[GUID] = BM.getModuleIdentifier();
925     }
926   }
927 
928   if (Error Err =
929           BM.readSummary(ThinLTO.CombinedIndex, BM.getModuleIdentifier(),
930                          ThinLTO.ModuleMap.size(), [&](GlobalValue::GUID GUID) {
931                            return ThinLTO.PrevailingModuleForGUID[GUID] ==
932                                   BM.getModuleIdentifier();
933                          }))
934     return Err;
935 
936   for (const InputFile::Symbol &Sym : Syms) {
937     assert(ResI != ResE);
938     SymbolResolution Res = *ResI++;
939 
940     if (!Sym.getIRName().empty()) {
941       auto GUID = GlobalValue::getGUID(GlobalValue::getGlobalIdentifier(
942           Sym.getIRName(), GlobalValue::ExternalLinkage, ""));
943       if (Res.Prevailing) {
944         assert(ThinLTO.PrevailingModuleForGUID[GUID] ==
945                BM.getModuleIdentifier());
946 
947         // For linker redefined symbols (via --wrap or --defsym) we want to
948         // switch the linkage to `weak` to prevent IPOs from happening.
949         // Find the summary in the module for this very GV and record the new
950         // linkage so that we can switch it when we import the GV.
951         if (Res.LinkerRedefined)
952           if (auto S = ThinLTO.CombinedIndex.findSummaryInModule(
953                   GUID, BM.getModuleIdentifier()))
954             S->setLinkage(GlobalValue::WeakAnyLinkage);
955       }
956 
957       // If the linker resolved the symbol to a local definition then mark it
958       // as local in the summary for the module we are adding.
959       if (Res.FinalDefinitionInLinkageUnit) {
960         if (auto S = ThinLTO.CombinedIndex.findSummaryInModule(
961                 GUID, BM.getModuleIdentifier())) {
962           S->setDSOLocal(true);
963         }
964       }
965     }
966   }
967 
968   if (!ThinLTO.ModuleMap.insert({BM.getModuleIdentifier(), BM}).second)
969     return make_error<StringError>(
970         "Expected at most one ThinLTO module per bitcode file",
971         inconvertibleErrorCode());
972 
973   if (!Conf.ThinLTOModulesToCompile.empty()) {
974     if (!ThinLTO.ModulesToCompile)
975       ThinLTO.ModulesToCompile = ModuleMapType();
976     // This is a fuzzy name matching where only modules with name containing the
977     // specified switch values are going to be compiled.
978     for (const std::string &Name : Conf.ThinLTOModulesToCompile) {
979       if (BM.getModuleIdentifier().contains(Name)) {
980         ThinLTO.ModulesToCompile->insert({BM.getModuleIdentifier(), BM});
981         llvm::errs() << "[ThinLTO] Selecting " << BM.getModuleIdentifier()
982                      << " to compile\n";
983       }
984     }
985   }
986 
987   return Error::success();
988 }
989 
990 unsigned LTO::getMaxTasks() const {
991   CalledGetMaxTasks = true;
992   auto ModuleCount = ThinLTO.ModulesToCompile ? ThinLTO.ModulesToCompile->size()
993                                               : ThinLTO.ModuleMap.size();
994   return RegularLTO.ParallelCodeGenParallelismLevel + ModuleCount;
995 }
996 
997 // If only some of the modules were split, we cannot correctly handle
998 // code that contains type tests or type checked loads.
999 Error LTO::checkPartiallySplit() {
1000   if (!ThinLTO.CombinedIndex.partiallySplitLTOUnits())
1001     return Error::success();
1002 
1003   Function *TypeTestFunc = RegularLTO.CombinedModule->getFunction(
1004       Intrinsic::getName(Intrinsic::type_test));
1005   Function *TypeCheckedLoadFunc = RegularLTO.CombinedModule->getFunction(
1006       Intrinsic::getName(Intrinsic::type_checked_load));
1007 
1008   // First check if there are type tests / type checked loads in the
1009   // merged regular LTO module IR.
1010   if ((TypeTestFunc && !TypeTestFunc->use_empty()) ||
1011       (TypeCheckedLoadFunc && !TypeCheckedLoadFunc->use_empty()))
1012     return make_error<StringError>(
1013         "inconsistent LTO Unit splitting (recompile with -fsplit-lto-unit)",
1014         inconvertibleErrorCode());
1015 
1016   // Otherwise check if there are any recorded in the combined summary from the
1017   // ThinLTO modules.
1018   for (auto &P : ThinLTO.CombinedIndex) {
1019     for (auto &S : P.second.SummaryList) {
1020       auto *FS = dyn_cast<FunctionSummary>(S.get());
1021       if (!FS)
1022         continue;
1023       if (!FS->type_test_assume_vcalls().empty() ||
1024           !FS->type_checked_load_vcalls().empty() ||
1025           !FS->type_test_assume_const_vcalls().empty() ||
1026           !FS->type_checked_load_const_vcalls().empty() ||
1027           !FS->type_tests().empty())
1028         return make_error<StringError>(
1029             "inconsistent LTO Unit splitting (recompile with -fsplit-lto-unit)",
1030             inconvertibleErrorCode());
1031     }
1032   }
1033   return Error::success();
1034 }
1035 
1036 Error LTO::run(AddStreamFn AddStream, FileCache Cache) {
1037   // Compute "dead" symbols, we don't want to import/export these!
1038   DenseSet<GlobalValue::GUID> GUIDPreservedSymbols;
1039   DenseMap<GlobalValue::GUID, PrevailingType> GUIDPrevailingResolutions;
1040   for (auto &Res : GlobalResolutions) {
1041     // Normally resolution have IR name of symbol. We can do nothing here
1042     // otherwise. See comments in GlobalResolution struct for more details.
1043     if (Res.second.IRName.empty())
1044       continue;
1045 
1046     GlobalValue::GUID GUID = GlobalValue::getGUID(
1047         GlobalValue::dropLLVMManglingEscape(Res.second.IRName));
1048 
1049     if (Res.second.VisibleOutsideSummary && Res.second.Prevailing)
1050       GUIDPreservedSymbols.insert(GUID);
1051 
1052     if (Res.second.ExportDynamic)
1053       DynamicExportSymbols.insert(GUID);
1054 
1055     GUIDPrevailingResolutions[GUID] =
1056         Res.second.Prevailing ? PrevailingType::Yes : PrevailingType::No;
1057   }
1058 
1059   auto isPrevailing = [&](GlobalValue::GUID G) {
1060     auto It = GUIDPrevailingResolutions.find(G);
1061     if (It == GUIDPrevailingResolutions.end())
1062       return PrevailingType::Unknown;
1063     return It->second;
1064   };
1065   computeDeadSymbolsWithConstProp(ThinLTO.CombinedIndex, GUIDPreservedSymbols,
1066                                   isPrevailing, Conf.OptLevel > 0);
1067 
1068   // Setup output file to emit statistics.
1069   auto StatsFileOrErr = setupStatsFile(Conf.StatsFile);
1070   if (!StatsFileOrErr)
1071     return StatsFileOrErr.takeError();
1072   std::unique_ptr<ToolOutputFile> StatsFile = std::move(StatsFileOrErr.get());
1073 
1074   Error Result = runRegularLTO(AddStream);
1075   if (!Result)
1076     Result = runThinLTO(AddStream, Cache, GUIDPreservedSymbols);
1077 
1078   if (StatsFile)
1079     PrintStatisticsJSON(StatsFile->os());
1080 
1081   return Result;
1082 }
1083 
1084 Error LTO::runRegularLTO(AddStreamFn AddStream) {
1085   // Setup optimization remarks.
1086   auto DiagFileOrErr = lto::setupLLVMOptimizationRemarks(
1087       RegularLTO.CombinedModule->getContext(), Conf.RemarksFilename,
1088       Conf.RemarksPasses, Conf.RemarksFormat, Conf.RemarksWithHotness,
1089       Conf.RemarksHotnessThreshold);
1090   if (!DiagFileOrErr)
1091     return DiagFileOrErr.takeError();
1092   DiagnosticOutputFile = std::move(*DiagFileOrErr);
1093 
1094   // Finalize linking of regular LTO modules containing summaries now that
1095   // we have computed liveness information.
1096   for (auto &M : RegularLTO.ModsWithSummaries)
1097     if (Error Err = linkRegularLTO(std::move(M),
1098                                    /*LivenessFromIndex=*/true))
1099       return Err;
1100 
1101   // Ensure we don't have inconsistently split LTO units with type tests.
1102   // FIXME: this checks both LTO and ThinLTO. It happens to work as we take
1103   // this path both cases but eventually this should be split into two and
1104   // do the ThinLTO checks in `runThinLTO`.
1105   if (Error Err = checkPartiallySplit())
1106     return Err;
1107 
1108   // Make sure commons have the right size/alignment: we kept the largest from
1109   // all the prevailing when adding the inputs, and we apply it here.
1110   const DataLayout &DL = RegularLTO.CombinedModule->getDataLayout();
1111   for (auto &I : RegularLTO.Commons) {
1112     if (!I.second.Prevailing)
1113       // Don't do anything if no instance of this common was prevailing.
1114       continue;
1115     GlobalVariable *OldGV = RegularLTO.CombinedModule->getNamedGlobal(I.first);
1116     if (OldGV && DL.getTypeAllocSize(OldGV->getValueType()) == I.second.Size) {
1117       // Don't create a new global if the type is already correct, just make
1118       // sure the alignment is correct.
1119       OldGV->setAlignment(I.second.Align);
1120       continue;
1121     }
1122     ArrayType *Ty =
1123         ArrayType::get(Type::getInt8Ty(RegularLTO.Ctx), I.second.Size);
1124     auto *GV = new GlobalVariable(*RegularLTO.CombinedModule, Ty, false,
1125                                   GlobalValue::CommonLinkage,
1126                                   ConstantAggregateZero::get(Ty), "");
1127     GV->setAlignment(I.second.Align);
1128     if (OldGV) {
1129       OldGV->replaceAllUsesWith(ConstantExpr::getBitCast(GV, OldGV->getType()));
1130       GV->takeName(OldGV);
1131       OldGV->eraseFromParent();
1132     } else {
1133       GV->setName(I.first);
1134     }
1135   }
1136 
1137   // If allowed, upgrade public vcall visibility metadata to linkage unit
1138   // visibility before whole program devirtualization in the optimizer.
1139   updateVCallVisibilityInModule(*RegularLTO.CombinedModule,
1140                                 Conf.HasWholeProgramVisibility,
1141                                 DynamicExportSymbols);
1142   updatePublicTypeTestCalls(*RegularLTO.CombinedModule,
1143                             Conf.HasWholeProgramVisibility);
1144 
1145   if (Conf.PreOptModuleHook &&
1146       !Conf.PreOptModuleHook(0, *RegularLTO.CombinedModule))
1147     return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
1148 
1149   if (!Conf.CodeGenOnly) {
1150     for (const auto &R : GlobalResolutions) {
1151       if (!R.second.isPrevailingIRSymbol())
1152         continue;
1153       if (R.second.Partition != 0 &&
1154           R.second.Partition != GlobalResolution::External)
1155         continue;
1156 
1157       GlobalValue *GV =
1158           RegularLTO.CombinedModule->getNamedValue(R.second.IRName);
1159       // Ignore symbols defined in other partitions.
1160       // Also skip declarations, which are not allowed to have internal linkage.
1161       if (!GV || GV->hasLocalLinkage() || GV->isDeclaration())
1162         continue;
1163       GV->setUnnamedAddr(R.second.UnnamedAddr ? GlobalValue::UnnamedAddr::Global
1164                                               : GlobalValue::UnnamedAddr::None);
1165       if (EnableLTOInternalization && R.second.Partition == 0)
1166         GV->setLinkage(GlobalValue::InternalLinkage);
1167     }
1168 
1169     RegularLTO.CombinedModule->addModuleFlag(Module::Error, "LTOPostLink", 1);
1170 
1171     if (Conf.PostInternalizeModuleHook &&
1172         !Conf.PostInternalizeModuleHook(0, *RegularLTO.CombinedModule))
1173       return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
1174   }
1175 
1176   if (!RegularLTO.EmptyCombinedModule || Conf.AlwaysEmitRegularLTOObj) {
1177     if (Error Err =
1178             backend(Conf, AddStream, RegularLTO.ParallelCodeGenParallelismLevel,
1179                     *RegularLTO.CombinedModule, ThinLTO.CombinedIndex))
1180       return Err;
1181   }
1182 
1183   return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
1184 }
1185 
1186 static const char *libcallRoutineNames[] = {
1187 #define HANDLE_LIBCALL(code, name) name,
1188 #include "llvm/IR/RuntimeLibcalls.def"
1189 #undef HANDLE_LIBCALL
1190 };
1191 
1192 ArrayRef<const char*> LTO::getRuntimeLibcallSymbols() {
1193   return ArrayRef(libcallRoutineNames);
1194 }
1195 
1196 /// This class defines the interface to the ThinLTO backend.
1197 class lto::ThinBackendProc {
1198 protected:
1199   const Config &Conf;
1200   ModuleSummaryIndex &CombinedIndex;
1201   const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries;
1202   lto::IndexWriteCallback OnWrite;
1203   bool ShouldEmitImportsFiles;
1204 
1205 public:
1206   ThinBackendProc(const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1207                   const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1208                   lto::IndexWriteCallback OnWrite, bool ShouldEmitImportsFiles)
1209       : Conf(Conf), CombinedIndex(CombinedIndex),
1210         ModuleToDefinedGVSummaries(ModuleToDefinedGVSummaries),
1211         OnWrite(OnWrite), ShouldEmitImportsFiles(ShouldEmitImportsFiles) {}
1212 
1213   virtual ~ThinBackendProc() = default;
1214   virtual Error start(
1215       unsigned Task, BitcodeModule BM,
1216       const FunctionImporter::ImportMapTy &ImportList,
1217       const FunctionImporter::ExportSetTy &ExportList,
1218       const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1219       MapVector<StringRef, BitcodeModule> &ModuleMap) = 0;
1220   virtual Error wait() = 0;
1221   virtual unsigned getThreadCount() = 0;
1222 
1223   // Write sharded indices and (optionally) imports to disk
1224   Error emitFiles(const FunctionImporter::ImportMapTy &ImportList,
1225                   llvm::StringRef ModulePath,
1226                   const std::string &NewModulePath) {
1227     std::map<std::string, GVSummaryMapTy> ModuleToSummariesForIndex;
1228     std::error_code EC;
1229     gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries,
1230                                      ImportList, ModuleToSummariesForIndex);
1231 
1232     raw_fd_ostream OS(NewModulePath + ".thinlto.bc", EC,
1233                       sys::fs::OpenFlags::OF_None);
1234     if (EC)
1235       return errorCodeToError(EC);
1236     writeIndexToFile(CombinedIndex, OS, &ModuleToSummariesForIndex);
1237 
1238     if (ShouldEmitImportsFiles) {
1239       EC = EmitImportsFiles(ModulePath, NewModulePath + ".imports",
1240                             ModuleToSummariesForIndex);
1241       if (EC)
1242         return errorCodeToError(EC);
1243     }
1244     return Error::success();
1245   }
1246 };
1247 
1248 namespace {
1249 class InProcessThinBackend : public ThinBackendProc {
1250   ThreadPool BackendThreadPool;
1251   AddStreamFn AddStream;
1252   FileCache Cache;
1253   std::set<GlobalValue::GUID> CfiFunctionDefs;
1254   std::set<GlobalValue::GUID> CfiFunctionDecls;
1255 
1256   std::optional<Error> Err;
1257   std::mutex ErrMu;
1258 
1259   bool ShouldEmitIndexFiles;
1260 
1261 public:
1262   InProcessThinBackend(
1263       const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1264       ThreadPoolStrategy ThinLTOParallelism,
1265       const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1266       AddStreamFn AddStream, FileCache Cache, lto::IndexWriteCallback OnWrite,
1267       bool ShouldEmitIndexFiles, bool ShouldEmitImportsFiles)
1268       : ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries,
1269                         OnWrite, ShouldEmitImportsFiles),
1270         BackendThreadPool(ThinLTOParallelism), AddStream(std::move(AddStream)),
1271         Cache(std::move(Cache)), ShouldEmitIndexFiles(ShouldEmitIndexFiles) {
1272     for (auto &Name : CombinedIndex.cfiFunctionDefs())
1273       CfiFunctionDefs.insert(
1274           GlobalValue::getGUID(GlobalValue::dropLLVMManglingEscape(Name)));
1275     for (auto &Name : CombinedIndex.cfiFunctionDecls())
1276       CfiFunctionDecls.insert(
1277           GlobalValue::getGUID(GlobalValue::dropLLVMManglingEscape(Name)));
1278   }
1279 
1280   Error runThinLTOBackendThread(
1281       AddStreamFn AddStream, FileCache Cache, unsigned Task, BitcodeModule BM,
1282       ModuleSummaryIndex &CombinedIndex,
1283       const FunctionImporter::ImportMapTy &ImportList,
1284       const FunctionImporter::ExportSetTy &ExportList,
1285       const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1286       const GVSummaryMapTy &DefinedGlobals,
1287       MapVector<StringRef, BitcodeModule> &ModuleMap) {
1288     auto RunThinBackend = [&](AddStreamFn AddStream) {
1289       LTOLLVMContext BackendContext(Conf);
1290       Expected<std::unique_ptr<Module>> MOrErr = BM.parseModule(BackendContext);
1291       if (!MOrErr)
1292         return MOrErr.takeError();
1293 
1294       return thinBackend(Conf, Task, AddStream, **MOrErr, CombinedIndex,
1295                          ImportList, DefinedGlobals, &ModuleMap);
1296     };
1297 
1298     auto ModuleID = BM.getModuleIdentifier();
1299 
1300     if (ShouldEmitIndexFiles) {
1301       if (auto E = emitFiles(ImportList, ModuleID, ModuleID.str()))
1302         return E;
1303     }
1304 
1305     if (!Cache || !CombinedIndex.modulePaths().count(ModuleID) ||
1306         all_of(CombinedIndex.getModuleHash(ModuleID),
1307                [](uint32_t V) { return V == 0; }))
1308       // Cache disabled or no entry for this module in the combined index or
1309       // no module hash.
1310       return RunThinBackend(AddStream);
1311 
1312     SmallString<40> Key;
1313     // The module may be cached, this helps handling it.
1314     computeLTOCacheKey(Key, Conf, CombinedIndex, ModuleID, ImportList,
1315                        ExportList, ResolvedODR, DefinedGlobals, CfiFunctionDefs,
1316                        CfiFunctionDecls);
1317     Expected<AddStreamFn> CacheAddStreamOrErr = Cache(Task, Key, ModuleID);
1318     if (Error Err = CacheAddStreamOrErr.takeError())
1319       return Err;
1320     AddStreamFn &CacheAddStream = *CacheAddStreamOrErr;
1321     if (CacheAddStream)
1322       return RunThinBackend(CacheAddStream);
1323 
1324     return Error::success();
1325   }
1326 
1327   Error start(
1328       unsigned Task, BitcodeModule BM,
1329       const FunctionImporter::ImportMapTy &ImportList,
1330       const FunctionImporter::ExportSetTy &ExportList,
1331       const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1332       MapVector<StringRef, BitcodeModule> &ModuleMap) override {
1333     StringRef ModulePath = BM.getModuleIdentifier();
1334     assert(ModuleToDefinedGVSummaries.count(ModulePath));
1335     const GVSummaryMapTy &DefinedGlobals =
1336         ModuleToDefinedGVSummaries.find(ModulePath)->second;
1337     BackendThreadPool.async(
1338         [=](BitcodeModule BM, ModuleSummaryIndex &CombinedIndex,
1339             const FunctionImporter::ImportMapTy &ImportList,
1340             const FunctionImporter::ExportSetTy &ExportList,
1341             const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>
1342                 &ResolvedODR,
1343             const GVSummaryMapTy &DefinedGlobals,
1344             MapVector<StringRef, BitcodeModule> &ModuleMap) {
1345           if (LLVM_ENABLE_THREADS && Conf.TimeTraceEnabled)
1346             timeTraceProfilerInitialize(Conf.TimeTraceGranularity,
1347                                         "thin backend");
1348           Error E = runThinLTOBackendThread(
1349               AddStream, Cache, Task, BM, CombinedIndex, ImportList, ExportList,
1350               ResolvedODR, DefinedGlobals, ModuleMap);
1351           if (E) {
1352             std::unique_lock<std::mutex> L(ErrMu);
1353             if (Err)
1354               Err = joinErrors(std::move(*Err), std::move(E));
1355             else
1356               Err = std::move(E);
1357           }
1358           if (LLVM_ENABLE_THREADS && Conf.TimeTraceEnabled)
1359             timeTraceProfilerFinishThread();
1360         },
1361         BM, std::ref(CombinedIndex), std::ref(ImportList), std::ref(ExportList),
1362         std::ref(ResolvedODR), std::ref(DefinedGlobals), std::ref(ModuleMap));
1363 
1364     if (OnWrite)
1365       OnWrite(std::string(ModulePath));
1366     return Error::success();
1367   }
1368 
1369   Error wait() override {
1370     BackendThreadPool.wait();
1371     if (Err)
1372       return std::move(*Err);
1373     else
1374       return Error::success();
1375   }
1376 
1377   unsigned getThreadCount() override {
1378     return BackendThreadPool.getThreadCount();
1379   }
1380 };
1381 } // end anonymous namespace
1382 
1383 ThinBackend lto::createInProcessThinBackend(ThreadPoolStrategy Parallelism,
1384                                             lto::IndexWriteCallback OnWrite,
1385                                             bool ShouldEmitIndexFiles,
1386                                             bool ShouldEmitImportsFiles) {
1387   return [=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1388              const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1389              AddStreamFn AddStream, FileCache Cache) {
1390     return std::make_unique<InProcessThinBackend>(
1391         Conf, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries, AddStream,
1392         Cache, OnWrite, ShouldEmitIndexFiles, ShouldEmitImportsFiles);
1393   };
1394 }
1395 
1396 // Given the original \p Path to an output file, replace any path
1397 // prefix matching \p OldPrefix with \p NewPrefix. Also, create the
1398 // resulting directory if it does not yet exist.
1399 std::string lto::getThinLTOOutputFile(const std::string &Path,
1400                                       const std::string &OldPrefix,
1401                                       const std::string &NewPrefix) {
1402   if (OldPrefix.empty() && NewPrefix.empty())
1403     return Path;
1404   SmallString<128> NewPath(Path);
1405   llvm::sys::path::replace_path_prefix(NewPath, OldPrefix, NewPrefix);
1406   StringRef ParentPath = llvm::sys::path::parent_path(NewPath.str());
1407   if (!ParentPath.empty()) {
1408     // Make sure the new directory exists, creating it if necessary.
1409     if (std::error_code EC = llvm::sys::fs::create_directories(ParentPath))
1410       llvm::errs() << "warning: could not create directory '" << ParentPath
1411                    << "': " << EC.message() << '\n';
1412   }
1413   return std::string(NewPath.str());
1414 }
1415 
1416 namespace {
1417 class WriteIndexesThinBackend : public ThinBackendProc {
1418   std::string OldPrefix, NewPrefix;
1419   raw_fd_ostream *LinkedObjectsFile;
1420 
1421 public:
1422   WriteIndexesThinBackend(
1423       const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1424       const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1425       std::string OldPrefix, std::string NewPrefix, bool ShouldEmitImportsFiles,
1426       raw_fd_ostream *LinkedObjectsFile, lto::IndexWriteCallback OnWrite)
1427       : ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries,
1428                         OnWrite, ShouldEmitImportsFiles),
1429         OldPrefix(OldPrefix), NewPrefix(NewPrefix),
1430         LinkedObjectsFile(LinkedObjectsFile) {}
1431 
1432   Error start(
1433       unsigned Task, BitcodeModule BM,
1434       const FunctionImporter::ImportMapTy &ImportList,
1435       const FunctionImporter::ExportSetTy &ExportList,
1436       const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
1437       MapVector<StringRef, BitcodeModule> &ModuleMap) override {
1438     StringRef ModulePath = BM.getModuleIdentifier();
1439     std::string NewModulePath =
1440         getThinLTOOutputFile(std::string(ModulePath), OldPrefix, NewPrefix);
1441 
1442     if (LinkedObjectsFile)
1443       *LinkedObjectsFile << NewModulePath << '\n';
1444 
1445     if (auto E = emitFiles(ImportList, ModulePath, NewModulePath))
1446       return E;
1447 
1448     if (OnWrite)
1449       OnWrite(std::string(ModulePath));
1450     return Error::success();
1451   }
1452 
1453   Error wait() override { return Error::success(); }
1454 
1455   // WriteIndexesThinBackend should always return 1 to prevent module
1456   // re-ordering and avoid non-determinism in the final link.
1457   unsigned getThreadCount() override { return 1; }
1458 };
1459 } // end anonymous namespace
1460 
1461 ThinBackend lto::createWriteIndexesThinBackend(
1462     std::string OldPrefix, std::string NewPrefix, bool ShouldEmitImportsFiles,
1463     raw_fd_ostream *LinkedObjectsFile, IndexWriteCallback OnWrite) {
1464   return [=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1465              const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1466              AddStreamFn AddStream, FileCache Cache) {
1467     return std::make_unique<WriteIndexesThinBackend>(
1468         Conf, CombinedIndex, ModuleToDefinedGVSummaries, OldPrefix, NewPrefix,
1469         ShouldEmitImportsFiles, LinkedObjectsFile, OnWrite);
1470   };
1471 }
1472 
1473 Error LTO::runThinLTO(AddStreamFn AddStream, FileCache Cache,
1474                       const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
1475   ThinLTO.CombinedIndex.releaseTemporaryMemory();
1476   timeTraceProfilerBegin("ThinLink", StringRef(""));
1477   auto TimeTraceScopeExit = llvm::make_scope_exit([]() {
1478     if (llvm::timeTraceProfilerEnabled())
1479       llvm::timeTraceProfilerEnd();
1480   });
1481   if (ThinLTO.ModuleMap.empty())
1482     return Error::success();
1483 
1484   if (ThinLTO.ModulesToCompile && ThinLTO.ModulesToCompile->empty()) {
1485     llvm::errs() << "warning: [ThinLTO] No module compiled\n";
1486     return Error::success();
1487   }
1488 
1489   if (Conf.CombinedIndexHook &&
1490       !Conf.CombinedIndexHook(ThinLTO.CombinedIndex, GUIDPreservedSymbols))
1491     return Error::success();
1492 
1493   // Collect for each module the list of function it defines (GUID ->
1494   // Summary).
1495   StringMap<GVSummaryMapTy>
1496       ModuleToDefinedGVSummaries(ThinLTO.ModuleMap.size());
1497   ThinLTO.CombinedIndex.collectDefinedGVSummariesPerModule(
1498       ModuleToDefinedGVSummaries);
1499   // Create entries for any modules that didn't have any GV summaries
1500   // (either they didn't have any GVs to start with, or we suppressed
1501   // generation of the summaries because they e.g. had inline assembly
1502   // uses that couldn't be promoted/renamed on export). This is so
1503   // InProcessThinBackend::start can still launch a backend thread, which
1504   // is passed the map of summaries for the module, without any special
1505   // handling for this case.
1506   for (auto &Mod : ThinLTO.ModuleMap)
1507     if (!ModuleToDefinedGVSummaries.count(Mod.first))
1508       ModuleToDefinedGVSummaries.try_emplace(Mod.first);
1509 
1510   // Synthesize entry counts for functions in the CombinedIndex.
1511   computeSyntheticCounts(ThinLTO.CombinedIndex);
1512 
1513   StringMap<FunctionImporter::ImportMapTy> ImportLists(
1514       ThinLTO.ModuleMap.size());
1515   StringMap<FunctionImporter::ExportSetTy> ExportLists(
1516       ThinLTO.ModuleMap.size());
1517   StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
1518 
1519   if (DumpThinCGSCCs)
1520     ThinLTO.CombinedIndex.dumpSCCs(outs());
1521 
1522   std::set<GlobalValue::GUID> ExportedGUIDs;
1523 
1524   if (hasWholeProgramVisibility(Conf.HasWholeProgramVisibility))
1525     ThinLTO.CombinedIndex.setWithWholeProgramVisibility();
1526   // If allowed, upgrade public vcall visibility to linkage unit visibility in
1527   // the summaries before whole program devirtualization below.
1528   updateVCallVisibilityInIndex(ThinLTO.CombinedIndex,
1529                                Conf.HasWholeProgramVisibility,
1530                                DynamicExportSymbols);
1531 
1532   // Perform index-based WPD. This will return immediately if there are
1533   // no index entries in the typeIdMetadata map (e.g. if we are instead
1534   // performing IR-based WPD in hybrid regular/thin LTO mode).
1535   std::map<ValueInfo, std::vector<VTableSlotSummary>> LocalWPDTargetsMap;
1536   runWholeProgramDevirtOnIndex(ThinLTO.CombinedIndex, ExportedGUIDs,
1537                                LocalWPDTargetsMap);
1538 
1539   if (Conf.OptLevel > 0)
1540     ComputeCrossModuleImport(ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
1541                              ImportLists, ExportLists);
1542 
1543   // Figure out which symbols need to be internalized. This also needs to happen
1544   // at -O0 because summary-based DCE is implemented using internalization, and
1545   // we must apply DCE consistently with the full LTO module in order to avoid
1546   // undefined references during the final link.
1547   for (auto &Res : GlobalResolutions) {
1548     // If the symbol does not have external references or it is not prevailing,
1549     // then not need to mark it as exported from a ThinLTO partition.
1550     if (Res.second.Partition != GlobalResolution::External ||
1551         !Res.second.isPrevailingIRSymbol())
1552       continue;
1553     auto GUID = GlobalValue::getGUID(
1554         GlobalValue::dropLLVMManglingEscape(Res.second.IRName));
1555     // Mark exported unless index-based analysis determined it to be dead.
1556     if (ThinLTO.CombinedIndex.isGUIDLive(GUID))
1557       ExportedGUIDs.insert(GUID);
1558   }
1559 
1560   // Any functions referenced by the jump table in the regular LTO object must
1561   // be exported.
1562   for (auto &Def : ThinLTO.CombinedIndex.cfiFunctionDefs())
1563     ExportedGUIDs.insert(
1564         GlobalValue::getGUID(GlobalValue::dropLLVMManglingEscape(Def)));
1565   for (auto &Decl : ThinLTO.CombinedIndex.cfiFunctionDecls())
1566     ExportedGUIDs.insert(
1567         GlobalValue::getGUID(GlobalValue::dropLLVMManglingEscape(Decl)));
1568 
1569   auto isExported = [&](StringRef ModuleIdentifier, ValueInfo VI) {
1570     const auto &ExportList = ExportLists.find(ModuleIdentifier);
1571     return (ExportList != ExportLists.end() && ExportList->second.count(VI)) ||
1572            ExportedGUIDs.count(VI.getGUID());
1573   };
1574 
1575   // Update local devirtualized targets that were exported by cross-module
1576   // importing or by other devirtualizations marked in the ExportedGUIDs set.
1577   updateIndexWPDForExports(ThinLTO.CombinedIndex, isExported,
1578                            LocalWPDTargetsMap);
1579 
1580   auto isPrevailing = [&](GlobalValue::GUID GUID,
1581                           const GlobalValueSummary *S) {
1582     return ThinLTO.PrevailingModuleForGUID[GUID] == S->modulePath();
1583   };
1584   thinLTOInternalizeAndPromoteInIndex(ThinLTO.CombinedIndex, isExported,
1585                                       isPrevailing);
1586 
1587   auto recordNewLinkage = [&](StringRef ModuleIdentifier,
1588                               GlobalValue::GUID GUID,
1589                               GlobalValue::LinkageTypes NewLinkage) {
1590     ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
1591   };
1592   thinLTOResolvePrevailingInIndex(Conf, ThinLTO.CombinedIndex, isPrevailing,
1593                                   recordNewLinkage, GUIDPreservedSymbols);
1594 
1595   thinLTOPropagateFunctionAttrs(ThinLTO.CombinedIndex, isPrevailing);
1596 
1597   generateParamAccessSummary(ThinLTO.CombinedIndex);
1598 
1599   if (llvm::timeTraceProfilerEnabled())
1600     llvm::timeTraceProfilerEnd();
1601 
1602   TimeTraceScopeExit.release();
1603 
1604   std::unique_ptr<ThinBackendProc> BackendProc =
1605       ThinLTO.Backend(Conf, ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
1606                       AddStream, Cache);
1607 
1608   auto &ModuleMap =
1609       ThinLTO.ModulesToCompile ? *ThinLTO.ModulesToCompile : ThinLTO.ModuleMap;
1610 
1611   auto ProcessOneModule = [&](int I) -> Error {
1612     auto &Mod = *(ModuleMap.begin() + I);
1613     // Tasks 0 through ParallelCodeGenParallelismLevel-1 are reserved for
1614     // combined module and parallel code generation partitions.
1615     return BackendProc->start(RegularLTO.ParallelCodeGenParallelismLevel + I,
1616                               Mod.second, ImportLists[Mod.first],
1617                               ExportLists[Mod.first], ResolvedODR[Mod.first],
1618                               ThinLTO.ModuleMap);
1619   };
1620 
1621   if (BackendProc->getThreadCount() == 1) {
1622     // Process the modules in the order they were provided on the command-line.
1623     // It is important for this codepath to be used for WriteIndexesThinBackend,
1624     // to ensure the emitted LinkedObjectsFile lists ThinLTO objects in the same
1625     // order as the inputs, which otherwise would affect the final link order.
1626     for (int I = 0, E = ModuleMap.size(); I != E; ++I)
1627       if (Error E = ProcessOneModule(I))
1628         return E;
1629   } else {
1630     // When executing in parallel, process largest bitsize modules first to
1631     // improve parallelism, and avoid starving the thread pool near the end.
1632     // This saves about 15 sec on a 36-core machine while link `clang.exe` (out
1633     // of 100 sec).
1634     std::vector<BitcodeModule *> ModulesVec;
1635     ModulesVec.reserve(ModuleMap.size());
1636     for (auto &Mod : ModuleMap)
1637       ModulesVec.push_back(&Mod.second);
1638     for (int I : generateModulesOrdering(ModulesVec))
1639       if (Error E = ProcessOneModule(I))
1640         return E;
1641   }
1642   return BackendProc->wait();
1643 }
1644 
1645 Expected<std::unique_ptr<ToolOutputFile>> lto::setupLLVMOptimizationRemarks(
1646     LLVMContext &Context, StringRef RemarksFilename, StringRef RemarksPasses,
1647     StringRef RemarksFormat, bool RemarksWithHotness,
1648     std::optional<uint64_t> RemarksHotnessThreshold, int Count) {
1649   std::string Filename = std::string(RemarksFilename);
1650   // For ThinLTO, file.opt.<format> becomes
1651   // file.opt.<format>.thin.<num>.<format>.
1652   if (!Filename.empty() && Count != -1)
1653     Filename =
1654         (Twine(Filename) + ".thin." + llvm::utostr(Count) + "." + RemarksFormat)
1655             .str();
1656 
1657   auto ResultOrErr = llvm::setupLLVMOptimizationRemarks(
1658       Context, Filename, RemarksPasses, RemarksFormat, RemarksWithHotness,
1659       RemarksHotnessThreshold);
1660   if (Error E = ResultOrErr.takeError())
1661     return std::move(E);
1662 
1663   if (*ResultOrErr)
1664     (*ResultOrErr)->keep();
1665 
1666   return ResultOrErr;
1667 }
1668 
1669 Expected<std::unique_ptr<ToolOutputFile>>
1670 lto::setupStatsFile(StringRef StatsFilename) {
1671   // Setup output file to emit statistics.
1672   if (StatsFilename.empty())
1673     return nullptr;
1674 
1675   llvm::EnableStatistics(false);
1676   std::error_code EC;
1677   auto StatsFile =
1678       std::make_unique<ToolOutputFile>(StatsFilename, EC, sys::fs::OF_None);
1679   if (EC)
1680     return errorCodeToError(EC);
1681 
1682   StatsFile->keep();
1683   return std::move(StatsFile);
1684 }
1685 
1686 // Compute the ordering we will process the inputs: the rough heuristic here
1687 // is to sort them per size so that the largest module get schedule as soon as
1688 // possible. This is purely a compile-time optimization.
1689 std::vector<int> lto::generateModulesOrdering(ArrayRef<BitcodeModule *> R) {
1690   auto Seq = llvm::seq<int>(0, R.size());
1691   std::vector<int> ModulesOrdering(Seq.begin(), Seq.end());
1692   llvm::sort(ModulesOrdering, [&](int LeftIndex, int RightIndex) {
1693     auto LSize = R[LeftIndex]->getBuffer().size();
1694     auto RSize = R[RightIndex]->getBuffer().size();
1695     return LSize > RSize;
1696   });
1697   return ModulesOrdering;
1698 }
1699