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