1 //===-ThinLTOCodeGenerator.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 the Thin Link Time Optimization library. This library is
10 // intended to be used by linker to optimize code at link time.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/LTO/legacy/ThinLTOCodeGenerator.h"
15 #include "llvm/Support/CommandLine.h"
16 
17 #include "llvm/ADT/ScopeExit.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/Analysis/AliasAnalysis.h"
21 #include "llvm/Analysis/ModuleSummaryAnalysis.h"
22 #include "llvm/Analysis/ProfileSummaryInfo.h"
23 #include "llvm/Analysis/TargetLibraryInfo.h"
24 #include "llvm/Bitcode/BitcodeReader.h"
25 #include "llvm/Bitcode/BitcodeWriter.h"
26 #include "llvm/Bitcode/BitcodeWriterPass.h"
27 #include "llvm/Config/llvm-config.h"
28 #include "llvm/IR/DebugInfo.h"
29 #include "llvm/IR/DiagnosticPrinter.h"
30 #include "llvm/IR/LegacyPassManager.h"
31 #include "llvm/IR/LLVMContext.h"
32 #include "llvm/IR/LLVMRemarkStreamer.h"
33 #include "llvm/IR/Mangler.h"
34 #include "llvm/IR/PassTimingInfo.h"
35 #include "llvm/IR/Verifier.h"
36 #include "llvm/IRReader/IRReader.h"
37 #include "llvm/LTO/LTO.h"
38 #include "llvm/LTO/SummaryBasedOptimizations.h"
39 #include "llvm/MC/TargetRegistry.h"
40 #include "llvm/Object/IRObjectFile.h"
41 #include "llvm/Passes/PassBuilder.h"
42 #include "llvm/Passes/StandardInstrumentations.h"
43 #include "llvm/Remarks/HotnessThresholdParser.h"
44 #include "llvm/Support/CachePruning.h"
45 #include "llvm/Support/Debug.h"
46 #include "llvm/Support/Error.h"
47 #include "llvm/Support/FileSystem.h"
48 #include "llvm/Support/Path.h"
49 #include "llvm/Support/SHA1.h"
50 #include "llvm/Support/SmallVectorMemoryBuffer.h"
51 #include "llvm/Support/ThreadPool.h"
52 #include "llvm/Support/Threading.h"
53 #include "llvm/Support/ToolOutputFile.h"
54 #include "llvm/Support/raw_ostream.h"
55 #include "llvm/Target/TargetMachine.h"
56 #include "llvm/TargetParser/SubtargetFeature.h"
57 #include "llvm/Transforms/IPO/FunctionAttrs.h"
58 #include "llvm/Transforms/IPO/FunctionImport.h"
59 #include "llvm/Transforms/IPO/Internalize.h"
60 #include "llvm/Transforms/IPO/WholeProgramDevirt.h"
61 #include "llvm/Transforms/ObjCARC.h"
62 #include "llvm/Transforms/Utils/FunctionImportUtils.h"
63 
64 #include <numeric>
65 
66 #if !defined(_MSC_VER) && !defined(__MINGW32__)
67 #include <unistd.h>
68 #else
69 #include <io.h>
70 #endif
71 
72 using namespace llvm;
73 
74 #define DEBUG_TYPE "thinlto"
75 
76 namespace llvm {
77 // Flags -discard-value-names, defined in LTOCodeGenerator.cpp
78 extern cl::opt<bool> LTODiscardValueNames;
79 extern cl::opt<std::string> RemarksFilename;
80 extern cl::opt<std::string> RemarksPasses;
81 extern cl::opt<bool> RemarksWithHotness;
82 extern cl::opt<std::optional<uint64_t>, false, remarks::HotnessThresholdParser>
83     RemarksHotnessThreshold;
84 extern cl::opt<std::string> RemarksFormat;
85 }
86 
87 namespace {
88 
89 // Default to using all available threads in the system, but using only one
90 // thred per core, as indicated by the usage of
91 // heavyweight_hardware_concurrency() below.
92 static cl::opt<int> ThreadCount("threads", cl::init(0));
93 
94 // Simple helper to save temporary files for debug.
95 static void saveTempBitcode(const Module &TheModule, StringRef TempDir,
96                             unsigned count, StringRef Suffix) {
97   if (TempDir.empty())
98     return;
99   // User asked to save temps, let dump the bitcode file after import.
100   std::string SaveTempPath = (TempDir + llvm::Twine(count) + Suffix).str();
101   std::error_code EC;
102   raw_fd_ostream OS(SaveTempPath, EC, sys::fs::OF_None);
103   if (EC)
104     report_fatal_error(Twine("Failed to open ") + SaveTempPath +
105                        " to save optimized bitcode\n");
106   WriteBitcodeToFile(TheModule, OS, /* ShouldPreserveUseListOrder */ true);
107 }
108 
109 static const GlobalValueSummary *
110 getFirstDefinitionForLinker(const GlobalValueSummaryList &GVSummaryList) {
111   // If there is any strong definition anywhere, get it.
112   auto StrongDefForLinker = llvm::find_if(
113       GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
114         auto Linkage = Summary->linkage();
115         return !GlobalValue::isAvailableExternallyLinkage(Linkage) &&
116                !GlobalValue::isWeakForLinker(Linkage);
117       });
118   if (StrongDefForLinker != GVSummaryList.end())
119     return StrongDefForLinker->get();
120   // Get the first *linker visible* definition for this global in the summary
121   // list.
122   auto FirstDefForLinker = llvm::find_if(
123       GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
124         auto Linkage = Summary->linkage();
125         return !GlobalValue::isAvailableExternallyLinkage(Linkage);
126       });
127   // Extern templates can be emitted as available_externally.
128   if (FirstDefForLinker == GVSummaryList.end())
129     return nullptr;
130   return FirstDefForLinker->get();
131 }
132 
133 // Populate map of GUID to the prevailing copy for any multiply defined
134 // symbols. Currently assume first copy is prevailing, or any strong
135 // definition. Can be refined with Linker information in the future.
136 static void computePrevailingCopies(
137     const ModuleSummaryIndex &Index,
138     DenseMap<GlobalValue::GUID, const GlobalValueSummary *> &PrevailingCopy) {
139   auto HasMultipleCopies = [&](const GlobalValueSummaryList &GVSummaryList) {
140     return GVSummaryList.size() > 1;
141   };
142 
143   for (auto &I : Index) {
144     if (HasMultipleCopies(I.second.SummaryList))
145       PrevailingCopy[I.first] =
146           getFirstDefinitionForLinker(I.second.SummaryList);
147   }
148 }
149 
150 static StringMap<lto::InputFile *>
151 generateModuleMap(std::vector<std::unique_ptr<lto::InputFile>> &Modules) {
152   StringMap<lto::InputFile *> ModuleMap;
153   for (auto &M : Modules) {
154     assert(!ModuleMap.contains(M->getName()) &&
155            "Expect unique Buffer Identifier");
156     ModuleMap[M->getName()] = M.get();
157   }
158   return ModuleMap;
159 }
160 
161 static void promoteModule(Module &TheModule, const ModuleSummaryIndex &Index,
162                           bool ClearDSOLocalOnDeclarations) {
163   if (renameModuleForThinLTO(TheModule, Index, ClearDSOLocalOnDeclarations))
164     report_fatal_error("renameModuleForThinLTO failed");
165 }
166 
167 namespace {
168 class ThinLTODiagnosticInfo : public DiagnosticInfo {
169   const Twine &Msg;
170 public:
171   ThinLTODiagnosticInfo(const Twine &DiagMsg,
172                         DiagnosticSeverity Severity = DS_Error)
173       : DiagnosticInfo(DK_Linker, Severity), Msg(DiagMsg) {}
174   void print(DiagnosticPrinter &DP) const override { DP << Msg; }
175 };
176 }
177 
178 /// Verify the module and strip broken debug info.
179 static void verifyLoadedModule(Module &TheModule) {
180   bool BrokenDebugInfo = false;
181   if (verifyModule(TheModule, &dbgs(), &BrokenDebugInfo))
182     report_fatal_error("Broken module found, compilation aborted!");
183   if (BrokenDebugInfo) {
184     TheModule.getContext().diagnose(ThinLTODiagnosticInfo(
185         "Invalid debug info found, debug info will be stripped", DS_Warning));
186     StripDebugInfo(TheModule);
187   }
188 }
189 
190 static std::unique_ptr<Module> loadModuleFromInput(lto::InputFile *Input,
191                                                    LLVMContext &Context,
192                                                    bool Lazy,
193                                                    bool IsImporting) {
194   auto &Mod = Input->getSingleBitcodeModule();
195   SMDiagnostic Err;
196   Expected<std::unique_ptr<Module>> ModuleOrErr =
197       Lazy ? Mod.getLazyModule(Context,
198                                /* ShouldLazyLoadMetadata */ true, IsImporting)
199            : Mod.parseModule(Context);
200   if (!ModuleOrErr) {
201     handleAllErrors(ModuleOrErr.takeError(), [&](ErrorInfoBase &EIB) {
202       SMDiagnostic Err = SMDiagnostic(Mod.getModuleIdentifier(),
203                                       SourceMgr::DK_Error, EIB.message());
204       Err.print("ThinLTO", errs());
205     });
206     report_fatal_error("Can't load module, abort.");
207   }
208   if (!Lazy)
209     verifyLoadedModule(*ModuleOrErr.get());
210   return std::move(*ModuleOrErr);
211 }
212 
213 static void
214 crossImportIntoModule(Module &TheModule, const ModuleSummaryIndex &Index,
215                       StringMap<lto::InputFile *> &ModuleMap,
216                       const FunctionImporter::ImportMapTy &ImportList,
217                       bool ClearDSOLocalOnDeclarations) {
218   auto Loader = [&](StringRef Identifier) {
219     auto &Input = ModuleMap[Identifier];
220     return loadModuleFromInput(Input, TheModule.getContext(),
221                                /*Lazy=*/true, /*IsImporting*/ true);
222   };
223 
224   FunctionImporter Importer(Index, Loader, ClearDSOLocalOnDeclarations);
225   Expected<bool> Result = Importer.importFunctions(TheModule, ImportList);
226   if (!Result) {
227     handleAllErrors(Result.takeError(), [&](ErrorInfoBase &EIB) {
228       SMDiagnostic Err = SMDiagnostic(TheModule.getModuleIdentifier(),
229                                       SourceMgr::DK_Error, EIB.message());
230       Err.print("ThinLTO", errs());
231     });
232     report_fatal_error("importFunctions failed");
233   }
234   // Verify again after cross-importing.
235   verifyLoadedModule(TheModule);
236 }
237 
238 static void optimizeModule(Module &TheModule, TargetMachine &TM,
239                            unsigned OptLevel, bool Freestanding,
240                            bool DebugPassManager, ModuleSummaryIndex *Index) {
241   std::optional<PGOOptions> PGOOpt;
242   LoopAnalysisManager LAM;
243   FunctionAnalysisManager FAM;
244   CGSCCAnalysisManager CGAM;
245   ModuleAnalysisManager MAM;
246 
247   PassInstrumentationCallbacks PIC;
248   StandardInstrumentations SI(TheModule.getContext(), DebugPassManager);
249   SI.registerCallbacks(PIC, &MAM);
250   PipelineTuningOptions PTO;
251   PTO.LoopVectorization = true;
252   PTO.SLPVectorization = true;
253   PassBuilder PB(&TM, PTO, PGOOpt, &PIC);
254 
255   std::unique_ptr<TargetLibraryInfoImpl> TLII(
256       new TargetLibraryInfoImpl(Triple(TM.getTargetTriple())));
257   if (Freestanding)
258     TLII->disableAllFunctions();
259   FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
260 
261   // Register all the basic analyses with the managers.
262   PB.registerModuleAnalyses(MAM);
263   PB.registerCGSCCAnalyses(CGAM);
264   PB.registerFunctionAnalyses(FAM);
265   PB.registerLoopAnalyses(LAM);
266   PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
267 
268   ModulePassManager MPM;
269 
270   OptimizationLevel OL;
271 
272   switch (OptLevel) {
273   default:
274     llvm_unreachable("Invalid optimization level");
275   case 0:
276     OL = OptimizationLevel::O0;
277     break;
278   case 1:
279     OL = OptimizationLevel::O1;
280     break;
281   case 2:
282     OL = OptimizationLevel::O2;
283     break;
284   case 3:
285     OL = OptimizationLevel::O3;
286     break;
287   }
288 
289   MPM.addPass(PB.buildThinLTODefaultPipeline(OL, Index));
290 
291   MPM.run(TheModule, MAM);
292 }
293 
294 static void
295 addUsedSymbolToPreservedGUID(const lto::InputFile &File,
296                              DenseSet<GlobalValue::GUID> &PreservedGUID) {
297   for (const auto &Sym : File.symbols()) {
298     if (Sym.isUsed())
299       PreservedGUID.insert(GlobalValue::getGUID(Sym.getIRName()));
300   }
301 }
302 
303 // Convert the PreservedSymbols map from "Name" based to "GUID" based.
304 static void computeGUIDPreservedSymbols(const lto::InputFile &File,
305                                         const StringSet<> &PreservedSymbols,
306                                         const Triple &TheTriple,
307                                         DenseSet<GlobalValue::GUID> &GUIDs) {
308   // Iterate the symbols in the input file and if the input has preserved symbol
309   // compute the GUID for the symbol.
310   for (const auto &Sym : File.symbols()) {
311     if (PreservedSymbols.count(Sym.getName()) && !Sym.getIRName().empty())
312       GUIDs.insert(GlobalValue::getGUID(GlobalValue::getGlobalIdentifier(
313           Sym.getIRName(), GlobalValue::ExternalLinkage, "")));
314   }
315 }
316 
317 static DenseSet<GlobalValue::GUID>
318 computeGUIDPreservedSymbols(const lto::InputFile &File,
319                             const StringSet<> &PreservedSymbols,
320                             const Triple &TheTriple) {
321   DenseSet<GlobalValue::GUID> GUIDPreservedSymbols(PreservedSymbols.size());
322   computeGUIDPreservedSymbols(File, PreservedSymbols, TheTriple,
323                               GUIDPreservedSymbols);
324   return GUIDPreservedSymbols;
325 }
326 
327 std::unique_ptr<MemoryBuffer> codegenModule(Module &TheModule,
328                                             TargetMachine &TM) {
329   SmallVector<char, 128> OutputBuffer;
330 
331   // CodeGen
332   {
333     raw_svector_ostream OS(OutputBuffer);
334     legacy::PassManager PM;
335 
336     // If the bitcode files contain ARC code and were compiled with optimization,
337     // the ObjCARCContractPass must be run, so do it unconditionally here.
338     PM.add(createObjCARCContractPass());
339 
340     // Setup the codegen now.
341     if (TM.addPassesToEmitFile(PM, OS, nullptr, CGFT_ObjectFile,
342                                /* DisableVerify */ true))
343       report_fatal_error("Failed to setup codegen");
344 
345     // Run codegen now. resulting binary is in OutputBuffer.
346     PM.run(TheModule);
347   }
348   return std::make_unique<SmallVectorMemoryBuffer>(
349       std::move(OutputBuffer), /*RequiresNullTerminator=*/false);
350 }
351 
352 /// Manage caching for a single Module.
353 class ModuleCacheEntry {
354   SmallString<128> EntryPath;
355 
356 public:
357   // Create a cache entry. This compute a unique hash for the Module considering
358   // the current list of export/import, and offer an interface to query to
359   // access the content in the cache.
360   ModuleCacheEntry(
361       StringRef CachePath, const ModuleSummaryIndex &Index, StringRef ModuleID,
362       const FunctionImporter::ImportMapTy &ImportList,
363       const FunctionImporter::ExportSetTy &ExportList,
364       const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
365       const GVSummaryMapTy &DefinedGVSummaries, unsigned OptLevel,
366       bool Freestanding, const TargetMachineBuilder &TMBuilder) {
367     if (CachePath.empty())
368       return;
369 
370     if (!Index.modulePaths().count(ModuleID))
371       // The module does not have an entry, it can't have a hash at all
372       return;
373 
374     if (all_of(Index.getModuleHash(ModuleID),
375                [](uint32_t V) { return V == 0; }))
376       // No hash entry, no caching!
377       return;
378 
379     llvm::lto::Config Conf;
380     Conf.OptLevel = OptLevel;
381     Conf.Options = TMBuilder.Options;
382     Conf.CPU = TMBuilder.MCpu;
383     Conf.MAttrs.push_back(TMBuilder.MAttr);
384     Conf.RelocModel = TMBuilder.RelocModel;
385     Conf.CGOptLevel = TMBuilder.CGOptLevel;
386     Conf.Freestanding = Freestanding;
387     SmallString<40> Key;
388     computeLTOCacheKey(Key, Conf, Index, ModuleID, ImportList, ExportList,
389                        ResolvedODR, DefinedGVSummaries);
390 
391     // This choice of file name allows the cache to be pruned (see pruneCache()
392     // in include/llvm/Support/CachePruning.h).
393     sys::path::append(EntryPath, CachePath, "llvmcache-" + Key);
394   }
395 
396   // Access the path to this entry in the cache.
397   StringRef getEntryPath() { return EntryPath; }
398 
399   // Try loading the buffer for this cache entry.
400   ErrorOr<std::unique_ptr<MemoryBuffer>> tryLoadingBuffer() {
401     if (EntryPath.empty())
402       return std::error_code();
403     SmallString<64> ResultPath;
404     Expected<sys::fs::file_t> FDOrErr = sys::fs::openNativeFileForRead(
405         Twine(EntryPath), sys::fs::OF_UpdateAtime, &ResultPath);
406     if (!FDOrErr)
407       return errorToErrorCode(FDOrErr.takeError());
408     ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getOpenFile(
409         *FDOrErr, EntryPath, /*FileSize=*/-1, /*RequiresNullTerminator=*/false);
410     sys::fs::closeFile(*FDOrErr);
411     return MBOrErr;
412   }
413 
414   // Cache the Produced object file
415   void write(const MemoryBuffer &OutputBuffer) {
416     if (EntryPath.empty())
417       return;
418 
419     if (auto Err = llvm::writeToOutput(
420             EntryPath, [&OutputBuffer](llvm::raw_ostream &OS) -> llvm::Error {
421               OS << OutputBuffer.getBuffer();
422               return llvm::Error::success();
423             }))
424       report_fatal_error(llvm::formatv("ThinLTO: Can't write file {0}: {1}",
425                                        EntryPath,
426                                        toString(std::move(Err)).c_str()));
427   }
428 };
429 
430 static std::unique_ptr<MemoryBuffer>
431 ProcessThinLTOModule(Module &TheModule, ModuleSummaryIndex &Index,
432                      StringMap<lto::InputFile *> &ModuleMap, TargetMachine &TM,
433                      const FunctionImporter::ImportMapTy &ImportList,
434                      const FunctionImporter::ExportSetTy &ExportList,
435                      const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
436                      const GVSummaryMapTy &DefinedGlobals,
437                      const ThinLTOCodeGenerator::CachingOptions &CacheOptions,
438                      bool DisableCodeGen, StringRef SaveTempsDir,
439                      bool Freestanding, unsigned OptLevel, unsigned count,
440                      bool DebugPassManager) {
441   // "Benchmark"-like optimization: single-source case
442   bool SingleModule = (ModuleMap.size() == 1);
443 
444   // When linking an ELF shared object, dso_local should be dropped. We
445   // conservatively do this for -fpic.
446   bool ClearDSOLocalOnDeclarations =
447       TM.getTargetTriple().isOSBinFormatELF() &&
448       TM.getRelocationModel() != Reloc::Static &&
449       TheModule.getPIELevel() == PIELevel::Default;
450 
451   if (!SingleModule) {
452     promoteModule(TheModule, Index, ClearDSOLocalOnDeclarations);
453 
454     // Apply summary-based prevailing-symbol resolution decisions.
455     thinLTOFinalizeInModule(TheModule, DefinedGlobals, /*PropagateAttrs=*/true);
456 
457     // Save temps: after promotion.
458     saveTempBitcode(TheModule, SaveTempsDir, count, ".1.promoted.bc");
459   }
460 
461   // Be friendly and don't nuke totally the module when the client didn't
462   // supply anything to preserve.
463   if (!ExportList.empty() || !GUIDPreservedSymbols.empty()) {
464     // Apply summary-based internalization decisions.
465     thinLTOInternalizeModule(TheModule, DefinedGlobals);
466   }
467 
468   // Save internalized bitcode
469   saveTempBitcode(TheModule, SaveTempsDir, count, ".2.internalized.bc");
470 
471   if (!SingleModule)
472     crossImportIntoModule(TheModule, Index, ModuleMap, ImportList,
473                           ClearDSOLocalOnDeclarations);
474 
475   // Do this after any importing so that imported code is updated.
476   // See comment at call to updateVCallVisibilityInIndex() for why
477   // WholeProgramVisibilityEnabledInLTO is false.
478   updatePublicTypeTestCalls(TheModule,
479                             /* WholeProgramVisibilityEnabledInLTO */ false);
480 
481   // Save temps: after cross-module import.
482   saveTempBitcode(TheModule, SaveTempsDir, count, ".3.imported.bc");
483 
484   optimizeModule(TheModule, TM, OptLevel, Freestanding, DebugPassManager,
485                  &Index);
486 
487   saveTempBitcode(TheModule, SaveTempsDir, count, ".4.opt.bc");
488 
489   if (DisableCodeGen) {
490     // Configured to stop before CodeGen, serialize the bitcode and return.
491     SmallVector<char, 128> OutputBuffer;
492     {
493       raw_svector_ostream OS(OutputBuffer);
494       ProfileSummaryInfo PSI(TheModule);
495       auto Index = buildModuleSummaryIndex(TheModule, nullptr, &PSI);
496       WriteBitcodeToFile(TheModule, OS, true, &Index);
497     }
498     return std::make_unique<SmallVectorMemoryBuffer>(
499         std::move(OutputBuffer), /*RequiresNullTerminator=*/false);
500   }
501 
502   return codegenModule(TheModule, TM);
503 }
504 
505 /// Resolve prevailing symbols. Record resolutions in the \p ResolvedODR map
506 /// for caching, and in the \p Index for application during the ThinLTO
507 /// backends. This is needed for correctness for exported symbols (ensure
508 /// at least one copy kept) and a compile-time optimization (to drop duplicate
509 /// copies when possible).
510 static void resolvePrevailingInIndex(
511     ModuleSummaryIndex &Index,
512     StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>>
513         &ResolvedODR,
514     const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
515     const DenseMap<GlobalValue::GUID, const GlobalValueSummary *>
516         &PrevailingCopy) {
517 
518   auto isPrevailing = [&](GlobalValue::GUID GUID, const GlobalValueSummary *S) {
519     const auto &Prevailing = PrevailingCopy.find(GUID);
520     // Not in map means that there was only one copy, which must be prevailing.
521     if (Prevailing == PrevailingCopy.end())
522       return true;
523     return Prevailing->second == S;
524   };
525 
526   auto recordNewLinkage = [&](StringRef ModuleIdentifier,
527                               GlobalValue::GUID GUID,
528                               GlobalValue::LinkageTypes NewLinkage) {
529     ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
530   };
531 
532   // TODO Conf.VisibilityScheme can be lto::Config::ELF for ELF.
533   lto::Config Conf;
534   thinLTOResolvePrevailingInIndex(Conf, Index, isPrevailing, recordNewLinkage,
535                                   GUIDPreservedSymbols);
536 }
537 
538 // Initialize the TargetMachine builder for a given Triple
539 static void initTMBuilder(TargetMachineBuilder &TMBuilder,
540                           const Triple &TheTriple) {
541   // Set a default CPU for Darwin triples (copied from LTOCodeGenerator).
542   // FIXME this looks pretty terrible...
543   if (TMBuilder.MCpu.empty() && TheTriple.isOSDarwin()) {
544     if (TheTriple.getArch() == llvm::Triple::x86_64)
545       TMBuilder.MCpu = "core2";
546     else if (TheTriple.getArch() == llvm::Triple::x86)
547       TMBuilder.MCpu = "yonah";
548     else if (TheTriple.getArch() == llvm::Triple::aarch64 ||
549              TheTriple.getArch() == llvm::Triple::aarch64_32)
550       TMBuilder.MCpu = "cyclone";
551   }
552   TMBuilder.TheTriple = std::move(TheTriple);
553 }
554 
555 } // end anonymous namespace
556 
557 void ThinLTOCodeGenerator::addModule(StringRef Identifier, StringRef Data) {
558   MemoryBufferRef Buffer(Data, Identifier);
559 
560   auto InputOrError = lto::InputFile::create(Buffer);
561   if (!InputOrError)
562     report_fatal_error(Twine("ThinLTO cannot create input file: ") +
563                        toString(InputOrError.takeError()));
564 
565   auto TripleStr = (*InputOrError)->getTargetTriple();
566   Triple TheTriple(TripleStr);
567 
568   if (Modules.empty())
569     initTMBuilder(TMBuilder, Triple(TheTriple));
570   else if (TMBuilder.TheTriple != TheTriple) {
571     if (!TMBuilder.TheTriple.isCompatibleWith(TheTriple))
572       report_fatal_error("ThinLTO modules with incompatible triples not "
573                          "supported");
574     initTMBuilder(TMBuilder, Triple(TMBuilder.TheTriple.merge(TheTriple)));
575   }
576 
577   Modules.emplace_back(std::move(*InputOrError));
578 }
579 
580 void ThinLTOCodeGenerator::preserveSymbol(StringRef Name) {
581   PreservedSymbols.insert(Name);
582 }
583 
584 void ThinLTOCodeGenerator::crossReferenceSymbol(StringRef Name) {
585   // FIXME: At the moment, we don't take advantage of this extra information,
586   // we're conservatively considering cross-references as preserved.
587   //  CrossReferencedSymbols.insert(Name);
588   PreservedSymbols.insert(Name);
589 }
590 
591 // TargetMachine factory
592 std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
593   std::string ErrMsg;
594   const Target *TheTarget =
595       TargetRegistry::lookupTarget(TheTriple.str(), ErrMsg);
596   if (!TheTarget) {
597     report_fatal_error(Twine("Can't load target for this Triple: ") + ErrMsg);
598   }
599 
600   // Use MAttr as the default set of features.
601   SubtargetFeatures Features(MAttr);
602   Features.getDefaultSubtargetFeatures(TheTriple);
603   std::string FeatureStr = Features.getString();
604 
605   std::unique_ptr<TargetMachine> TM(
606       TheTarget->createTargetMachine(TheTriple.str(), MCpu, FeatureStr, Options,
607                                      RelocModel, std::nullopt, CGOptLevel));
608   assert(TM && "Cannot create target machine");
609 
610   return TM;
611 }
612 
613 /**
614  * Produce the combined summary index from all the bitcode files:
615  * "thin-link".
616  */
617 std::unique_ptr<ModuleSummaryIndex> ThinLTOCodeGenerator::linkCombinedIndex() {
618   std::unique_ptr<ModuleSummaryIndex> CombinedIndex =
619       std::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false);
620   uint64_t NextModuleId = 0;
621   for (auto &Mod : Modules) {
622     auto &M = Mod->getSingleBitcodeModule();
623     if (Error Err =
624             M.readSummary(*CombinedIndex, Mod->getName(), NextModuleId++)) {
625       // FIXME diagnose
626       logAllUnhandledErrors(
627           std::move(Err), errs(),
628           "error: can't create module summary index for buffer: ");
629       return nullptr;
630     }
631   }
632   return CombinedIndex;
633 }
634 
635 namespace {
636 struct IsExported {
637   const StringMap<FunctionImporter::ExportSetTy> &ExportLists;
638   const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols;
639 
640   IsExported(const StringMap<FunctionImporter::ExportSetTy> &ExportLists,
641              const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols)
642       : ExportLists(ExportLists), GUIDPreservedSymbols(GUIDPreservedSymbols) {}
643 
644   bool operator()(StringRef ModuleIdentifier, ValueInfo VI) const {
645     const auto &ExportList = ExportLists.find(ModuleIdentifier);
646     return (ExportList != ExportLists.end() && ExportList->second.count(VI)) ||
647            GUIDPreservedSymbols.count(VI.getGUID());
648   }
649 };
650 
651 struct IsPrevailing {
652   const DenseMap<GlobalValue::GUID, const GlobalValueSummary *> &PrevailingCopy;
653   IsPrevailing(const DenseMap<GlobalValue::GUID, const GlobalValueSummary *>
654                    &PrevailingCopy)
655       : PrevailingCopy(PrevailingCopy) {}
656 
657   bool operator()(GlobalValue::GUID GUID, const GlobalValueSummary *S) const {
658     const auto &Prevailing = PrevailingCopy.find(GUID);
659     // Not in map means that there was only one copy, which must be prevailing.
660     if (Prevailing == PrevailingCopy.end())
661       return true;
662     return Prevailing->second == S;
663   };
664 };
665 } // namespace
666 
667 static void computeDeadSymbolsInIndex(
668     ModuleSummaryIndex &Index,
669     const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
670   // We have no symbols resolution available. And can't do any better now in the
671   // case where the prevailing symbol is in a native object. It can be refined
672   // with linker information in the future.
673   auto isPrevailing = [&](GlobalValue::GUID G) {
674     return PrevailingType::Unknown;
675   };
676   computeDeadSymbolsWithConstProp(Index, GUIDPreservedSymbols, isPrevailing,
677                                   /* ImportEnabled = */ true);
678 }
679 
680 /**
681  * Perform promotion and renaming of exported internal functions.
682  * Index is updated to reflect linkage changes from weak resolution.
683  */
684 void ThinLTOCodeGenerator::promote(Module &TheModule, ModuleSummaryIndex &Index,
685                                    const lto::InputFile &File) {
686   auto ModuleCount = Index.modulePaths().size();
687   auto ModuleIdentifier = TheModule.getModuleIdentifier();
688 
689   // Collect for each module the list of function it defines (GUID -> Summary).
690   StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries;
691   Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
692 
693   // Convert the preserved symbols set from string to GUID
694   auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
695       File, PreservedSymbols, Triple(TheModule.getTargetTriple()));
696 
697   // Add used symbol to the preserved symbols.
698   addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
699 
700   // Compute "dead" symbols, we don't want to import/export these!
701   computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
702 
703   // Compute prevailing symbols
704   DenseMap<GlobalValue::GUID, const GlobalValueSummary *> PrevailingCopy;
705   computePrevailingCopies(Index, PrevailingCopy);
706 
707   // Generate import/export list
708   StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
709   StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
710   ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries,
711                            IsPrevailing(PrevailingCopy), ImportLists,
712                            ExportLists);
713 
714   // Resolve prevailing symbols
715   StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
716   resolvePrevailingInIndex(Index, ResolvedODR, GUIDPreservedSymbols,
717                            PrevailingCopy);
718 
719   thinLTOFinalizeInModule(TheModule,
720                           ModuleToDefinedGVSummaries[ModuleIdentifier],
721                           /*PropagateAttrs=*/false);
722 
723   // Promote the exported values in the index, so that they are promoted
724   // in the module.
725   thinLTOInternalizeAndPromoteInIndex(
726       Index, IsExported(ExportLists, GUIDPreservedSymbols),
727       IsPrevailing(PrevailingCopy));
728 
729   // FIXME Set ClearDSOLocalOnDeclarations.
730   promoteModule(TheModule, Index, /*ClearDSOLocalOnDeclarations=*/false);
731 }
732 
733 /**
734  * Perform cross-module importing for the module identified by ModuleIdentifier.
735  */
736 void ThinLTOCodeGenerator::crossModuleImport(Module &TheModule,
737                                              ModuleSummaryIndex &Index,
738                                              const lto::InputFile &File) {
739   auto ModuleMap = generateModuleMap(Modules);
740   auto ModuleCount = Index.modulePaths().size();
741 
742   // Collect for each module the list of function it defines (GUID -> Summary).
743   StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
744   Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
745 
746   // Convert the preserved symbols set from string to GUID
747   auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
748       File, PreservedSymbols, Triple(TheModule.getTargetTriple()));
749 
750   addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
751 
752   // Compute "dead" symbols, we don't want to import/export these!
753   computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
754 
755   // Compute prevailing symbols
756   DenseMap<GlobalValue::GUID, const GlobalValueSummary *> PrevailingCopy;
757   computePrevailingCopies(Index, PrevailingCopy);
758 
759   // Generate import/export list
760   StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
761   StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
762   ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries,
763                            IsPrevailing(PrevailingCopy), ImportLists,
764                            ExportLists);
765   auto &ImportList = ImportLists[TheModule.getModuleIdentifier()];
766 
767   // FIXME Set ClearDSOLocalOnDeclarations.
768   crossImportIntoModule(TheModule, Index, ModuleMap, ImportList,
769                         /*ClearDSOLocalOnDeclarations=*/false);
770 }
771 
772 /**
773  * Compute the list of summaries needed for importing into module.
774  */
775 void ThinLTOCodeGenerator::gatherImportedSummariesForModule(
776     Module &TheModule, ModuleSummaryIndex &Index,
777     std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex,
778     const lto::InputFile &File) {
779   auto ModuleCount = Index.modulePaths().size();
780   auto ModuleIdentifier = TheModule.getModuleIdentifier();
781 
782   // Collect for each module the list of function it defines (GUID -> Summary).
783   StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
784   Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
785 
786   // Convert the preserved symbols set from string to GUID
787   auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
788       File, PreservedSymbols, Triple(TheModule.getTargetTriple()));
789 
790   addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
791 
792   // Compute "dead" symbols, we don't want to import/export these!
793   computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
794 
795   // Compute prevailing symbols
796   DenseMap<GlobalValue::GUID, const GlobalValueSummary *> PrevailingCopy;
797   computePrevailingCopies(Index, PrevailingCopy);
798 
799   // Generate import/export list
800   StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
801   StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
802   ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries,
803                            IsPrevailing(PrevailingCopy), ImportLists,
804                            ExportLists);
805 
806   llvm::gatherImportedSummariesForModule(
807       ModuleIdentifier, ModuleToDefinedGVSummaries,
808       ImportLists[ModuleIdentifier], ModuleToSummariesForIndex);
809 }
810 
811 /**
812  * Emit the list of files needed for importing into module.
813  */
814 void ThinLTOCodeGenerator::emitImports(Module &TheModule, StringRef OutputName,
815                                        ModuleSummaryIndex &Index,
816                                        const lto::InputFile &File) {
817   auto ModuleCount = Index.modulePaths().size();
818   auto ModuleIdentifier = TheModule.getModuleIdentifier();
819 
820   // Collect for each module the list of function it defines (GUID -> Summary).
821   StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
822   Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
823 
824   // Convert the preserved symbols set from string to GUID
825   auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
826       File, PreservedSymbols, Triple(TheModule.getTargetTriple()));
827 
828   addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
829 
830   // Compute "dead" symbols, we don't want to import/export these!
831   computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
832 
833   // Compute prevailing symbols
834   DenseMap<GlobalValue::GUID, const GlobalValueSummary *> PrevailingCopy;
835   computePrevailingCopies(Index, PrevailingCopy);
836 
837   // Generate import/export list
838   StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
839   StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
840   ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries,
841                            IsPrevailing(PrevailingCopy), ImportLists,
842                            ExportLists);
843 
844   std::map<std::string, GVSummaryMapTy> ModuleToSummariesForIndex;
845   llvm::gatherImportedSummariesForModule(
846       ModuleIdentifier, ModuleToDefinedGVSummaries,
847       ImportLists[ModuleIdentifier], ModuleToSummariesForIndex);
848 
849   std::error_code EC;
850   if ((EC = EmitImportsFiles(ModuleIdentifier, OutputName,
851                              ModuleToSummariesForIndex)))
852     report_fatal_error(Twine("Failed to open ") + OutputName +
853                        " to save imports lists\n");
854 }
855 
856 /**
857  * Perform internalization. Runs promote and internalization together.
858  * Index is updated to reflect linkage changes.
859  */
860 void ThinLTOCodeGenerator::internalize(Module &TheModule,
861                                        ModuleSummaryIndex &Index,
862                                        const lto::InputFile &File) {
863   initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
864   auto ModuleCount = Index.modulePaths().size();
865   auto ModuleIdentifier = TheModule.getModuleIdentifier();
866 
867   // Convert the preserved symbols set from string to GUID
868   auto GUIDPreservedSymbols =
869       computeGUIDPreservedSymbols(File, PreservedSymbols, TMBuilder.TheTriple);
870 
871   addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
872 
873   // Collect for each module the list of function it defines (GUID -> Summary).
874   StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
875   Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
876 
877   // Compute "dead" symbols, we don't want to import/export these!
878   computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
879 
880   // Compute prevailing symbols
881   DenseMap<GlobalValue::GUID, const GlobalValueSummary *> PrevailingCopy;
882   computePrevailingCopies(Index, PrevailingCopy);
883 
884   // Generate import/export list
885   StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
886   StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
887   ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries,
888                            IsPrevailing(PrevailingCopy), ImportLists,
889                            ExportLists);
890   auto &ExportList = ExportLists[ModuleIdentifier];
891 
892   // Be friendly and don't nuke totally the module when the client didn't
893   // supply anything to preserve.
894   if (ExportList.empty() && GUIDPreservedSymbols.empty())
895     return;
896 
897   // Resolve prevailing symbols
898   StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
899   resolvePrevailingInIndex(Index, ResolvedODR, GUIDPreservedSymbols,
900                            PrevailingCopy);
901 
902   // Promote the exported values in the index, so that they are promoted
903   // in the module.
904   thinLTOInternalizeAndPromoteInIndex(
905       Index, IsExported(ExportLists, GUIDPreservedSymbols),
906       IsPrevailing(PrevailingCopy));
907 
908   // FIXME Set ClearDSOLocalOnDeclarations.
909   promoteModule(TheModule, Index, /*ClearDSOLocalOnDeclarations=*/false);
910 
911   // Internalization
912   thinLTOFinalizeInModule(TheModule,
913                           ModuleToDefinedGVSummaries[ModuleIdentifier],
914                           /*PropagateAttrs=*/false);
915 
916   thinLTOInternalizeModule(TheModule,
917                            ModuleToDefinedGVSummaries[ModuleIdentifier]);
918 }
919 
920 /**
921  * Perform post-importing ThinLTO optimizations.
922  */
923 void ThinLTOCodeGenerator::optimize(Module &TheModule) {
924   initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
925 
926   // Optimize now
927   optimizeModule(TheModule, *TMBuilder.create(), OptLevel, Freestanding,
928                  DebugPassManager, nullptr);
929 }
930 
931 /// Write out the generated object file, either from CacheEntryPath or from
932 /// OutputBuffer, preferring hard-link when possible.
933 /// Returns the path to the generated file in SavedObjectsDirectoryPath.
934 std::string
935 ThinLTOCodeGenerator::writeGeneratedObject(int count, StringRef CacheEntryPath,
936                                            const MemoryBuffer &OutputBuffer) {
937   auto ArchName = TMBuilder.TheTriple.getArchName();
938   SmallString<128> OutputPath(SavedObjectsDirectoryPath);
939   llvm::sys::path::append(OutputPath,
940                           Twine(count) + "." + ArchName + ".thinlto.o");
941   OutputPath.c_str(); // Ensure the string is null terminated.
942   if (sys::fs::exists(OutputPath))
943     sys::fs::remove(OutputPath);
944 
945   // We don't return a memory buffer to the linker, just a list of files.
946   if (!CacheEntryPath.empty()) {
947     // Cache is enabled, hard-link the entry (or copy if hard-link fails).
948     auto Err = sys::fs::create_hard_link(CacheEntryPath, OutputPath);
949     if (!Err)
950       return std::string(OutputPath.str());
951     // Hard linking failed, try to copy.
952     Err = sys::fs::copy_file(CacheEntryPath, OutputPath);
953     if (!Err)
954       return std::string(OutputPath.str());
955     // Copy failed (could be because the CacheEntry was removed from the cache
956     // in the meantime by another process), fall back and try to write down the
957     // buffer to the output.
958     errs() << "remark: can't link or copy from cached entry '" << CacheEntryPath
959            << "' to '" << OutputPath << "'\n";
960   }
961   // No cache entry, just write out the buffer.
962   std::error_code Err;
963   raw_fd_ostream OS(OutputPath, Err, sys::fs::OF_None);
964   if (Err)
965     report_fatal_error(Twine("Can't open output '") + OutputPath + "'\n");
966   OS << OutputBuffer.getBuffer();
967   return std::string(OutputPath.str());
968 }
969 
970 // Main entry point for the ThinLTO processing
971 void ThinLTOCodeGenerator::run() {
972   timeTraceProfilerBegin("ThinLink", StringRef(""));
973   auto TimeTraceScopeExit = llvm::make_scope_exit([]() {
974     if (llvm::timeTraceProfilerEnabled())
975       llvm::timeTraceProfilerEnd();
976   });
977   // Prepare the resulting object vector
978   assert(ProducedBinaries.empty() && "The generator should not be reused");
979   if (SavedObjectsDirectoryPath.empty())
980     ProducedBinaries.resize(Modules.size());
981   else {
982     sys::fs::create_directories(SavedObjectsDirectoryPath);
983     bool IsDir;
984     sys::fs::is_directory(SavedObjectsDirectoryPath, IsDir);
985     if (!IsDir)
986       report_fatal_error(Twine("Unexistent dir: '") + SavedObjectsDirectoryPath + "'");
987     ProducedBinaryFiles.resize(Modules.size());
988   }
989 
990   if (CodeGenOnly) {
991     // Perform only parallel codegen and return.
992     ThreadPool Pool;
993     int count = 0;
994     for (auto &Mod : Modules) {
995       Pool.async([&](int count) {
996         LLVMContext Context;
997         Context.setDiscardValueNames(LTODiscardValueNames);
998 
999         // Parse module now
1000         auto TheModule = loadModuleFromInput(Mod.get(), Context, false,
1001                                              /*IsImporting*/ false);
1002 
1003         // CodeGen
1004         auto OutputBuffer = codegenModule(*TheModule, *TMBuilder.create());
1005         if (SavedObjectsDirectoryPath.empty())
1006           ProducedBinaries[count] = std::move(OutputBuffer);
1007         else
1008           ProducedBinaryFiles[count] =
1009               writeGeneratedObject(count, "", *OutputBuffer);
1010       }, count++);
1011     }
1012 
1013     return;
1014   }
1015 
1016   // Sequential linking phase
1017   auto Index = linkCombinedIndex();
1018 
1019   // Save temps: index.
1020   if (!SaveTempsDir.empty()) {
1021     auto SaveTempPath = SaveTempsDir + "index.bc";
1022     std::error_code EC;
1023     raw_fd_ostream OS(SaveTempPath, EC, sys::fs::OF_None);
1024     if (EC)
1025       report_fatal_error(Twine("Failed to open ") + SaveTempPath +
1026                          " to save optimized bitcode\n");
1027     writeIndexToFile(*Index, OS);
1028   }
1029 
1030 
1031   // Prepare the module map.
1032   auto ModuleMap = generateModuleMap(Modules);
1033   auto ModuleCount = Modules.size();
1034 
1035   // Collect for each module the list of function it defines (GUID -> Summary).
1036   StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
1037   Index->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
1038 
1039   // Convert the preserved symbols set from string to GUID, this is needed for
1040   // computing the caching hash and the internalization.
1041   DenseSet<GlobalValue::GUID> GUIDPreservedSymbols;
1042   for (const auto &M : Modules)
1043     computeGUIDPreservedSymbols(*M, PreservedSymbols, TMBuilder.TheTriple,
1044                                 GUIDPreservedSymbols);
1045 
1046   // Add used symbol from inputs to the preserved symbols.
1047   for (const auto &M : Modules)
1048     addUsedSymbolToPreservedGUID(*M, GUIDPreservedSymbols);
1049 
1050   // Compute "dead" symbols, we don't want to import/export these!
1051   computeDeadSymbolsInIndex(*Index, GUIDPreservedSymbols);
1052 
1053   // Synthesize entry counts for functions in the combined index.
1054   computeSyntheticCounts(*Index);
1055 
1056   // Currently there is no support for enabling whole program visibility via a
1057   // linker option in the old LTO API, but this call allows it to be specified
1058   // via the internal option. Must be done before WPD below.
1059   if (hasWholeProgramVisibility(/* WholeProgramVisibilityEnabledInLTO */ false))
1060     Index->setWithWholeProgramVisibility();
1061   updateVCallVisibilityInIndex(*Index,
1062                                /* WholeProgramVisibilityEnabledInLTO */ false,
1063                                // FIXME: This needs linker information via a
1064                                // TBD new interface.
1065                                /* DynamicExportSymbols */ {});
1066 
1067   // Perform index-based WPD. This will return immediately if there are
1068   // no index entries in the typeIdMetadata map (e.g. if we are instead
1069   // performing IR-based WPD in hybrid regular/thin LTO mode).
1070   std::map<ValueInfo, std::vector<VTableSlotSummary>> LocalWPDTargetsMap;
1071   std::set<GlobalValue::GUID> ExportedGUIDs;
1072   runWholeProgramDevirtOnIndex(*Index, ExportedGUIDs, LocalWPDTargetsMap);
1073   for (auto GUID : ExportedGUIDs)
1074     GUIDPreservedSymbols.insert(GUID);
1075 
1076   // Compute prevailing symbols
1077   DenseMap<GlobalValue::GUID, const GlobalValueSummary *> PrevailingCopy;
1078   computePrevailingCopies(*Index, PrevailingCopy);
1079 
1080   // Collect the import/export lists for all modules from the call-graph in the
1081   // combined index.
1082   StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
1083   StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
1084   ComputeCrossModuleImport(*Index, ModuleToDefinedGVSummaries,
1085                            IsPrevailing(PrevailingCopy), ImportLists,
1086                            ExportLists);
1087 
1088   // We use a std::map here to be able to have a defined ordering when
1089   // producing a hash for the cache entry.
1090   // FIXME: we should be able to compute the caching hash for the entry based
1091   // on the index, and nuke this map.
1092   StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
1093 
1094   // Resolve prevailing symbols, this has to be computed early because it
1095   // impacts the caching.
1096   resolvePrevailingInIndex(*Index, ResolvedODR, GUIDPreservedSymbols,
1097                            PrevailingCopy);
1098 
1099   // Use global summary-based analysis to identify symbols that can be
1100   // internalized (because they aren't exported or preserved as per callback).
1101   // Changes are made in the index, consumed in the ThinLTO backends.
1102   updateIndexWPDForExports(*Index,
1103                            IsExported(ExportLists, GUIDPreservedSymbols),
1104                            LocalWPDTargetsMap);
1105   thinLTOInternalizeAndPromoteInIndex(
1106       *Index, IsExported(ExportLists, GUIDPreservedSymbols),
1107       IsPrevailing(PrevailingCopy));
1108 
1109   thinLTOPropagateFunctionAttrs(*Index, IsPrevailing(PrevailingCopy));
1110 
1111   // Make sure that every module has an entry in the ExportLists, ImportList,
1112   // GVSummary and ResolvedODR maps to enable threaded access to these maps
1113   // below.
1114   for (auto &Module : Modules) {
1115     auto ModuleIdentifier = Module->getName();
1116     ExportLists[ModuleIdentifier];
1117     ImportLists[ModuleIdentifier];
1118     ResolvedODR[ModuleIdentifier];
1119     ModuleToDefinedGVSummaries[ModuleIdentifier];
1120   }
1121 
1122   std::vector<BitcodeModule *> ModulesVec;
1123   ModulesVec.reserve(Modules.size());
1124   for (auto &Mod : Modules)
1125     ModulesVec.push_back(&Mod->getSingleBitcodeModule());
1126   std::vector<int> ModulesOrdering = lto::generateModulesOrdering(ModulesVec);
1127 
1128   if (llvm::timeTraceProfilerEnabled())
1129     llvm::timeTraceProfilerEnd();
1130 
1131   TimeTraceScopeExit.release();
1132 
1133   // Parallel optimizer + codegen
1134   {
1135     ThreadPool Pool(heavyweight_hardware_concurrency(ThreadCount));
1136     for (auto IndexCount : ModulesOrdering) {
1137       auto &Mod = Modules[IndexCount];
1138       Pool.async([&](int count) {
1139         auto ModuleIdentifier = Mod->getName();
1140         auto &ExportList = ExportLists[ModuleIdentifier];
1141 
1142         auto &DefinedGVSummaries = ModuleToDefinedGVSummaries[ModuleIdentifier];
1143 
1144         // The module may be cached, this helps handling it.
1145         ModuleCacheEntry CacheEntry(CacheOptions.Path, *Index, ModuleIdentifier,
1146                                     ImportLists[ModuleIdentifier], ExportList,
1147                                     ResolvedODR[ModuleIdentifier],
1148                                     DefinedGVSummaries, OptLevel, Freestanding,
1149                                     TMBuilder);
1150         auto CacheEntryPath = CacheEntry.getEntryPath();
1151 
1152         {
1153           auto ErrOrBuffer = CacheEntry.tryLoadingBuffer();
1154           LLVM_DEBUG(dbgs() << "Cache " << (ErrOrBuffer ? "hit" : "miss")
1155                             << " '" << CacheEntryPath << "' for buffer "
1156                             << count << " " << ModuleIdentifier << "\n");
1157 
1158           if (ErrOrBuffer) {
1159             // Cache Hit!
1160             if (SavedObjectsDirectoryPath.empty())
1161               ProducedBinaries[count] = std::move(ErrOrBuffer.get());
1162             else
1163               ProducedBinaryFiles[count] = writeGeneratedObject(
1164                   count, CacheEntryPath, *ErrOrBuffer.get());
1165             return;
1166           }
1167         }
1168 
1169         LLVMContext Context;
1170         Context.setDiscardValueNames(LTODiscardValueNames);
1171         Context.enableDebugTypeODRUniquing();
1172         auto DiagFileOrErr = lto::setupLLVMOptimizationRemarks(
1173             Context, RemarksFilename, RemarksPasses, RemarksFormat,
1174             RemarksWithHotness, RemarksHotnessThreshold, count);
1175         if (!DiagFileOrErr) {
1176           errs() << "Error: " << toString(DiagFileOrErr.takeError()) << "\n";
1177           report_fatal_error("ThinLTO: Can't get an output file for the "
1178                              "remarks");
1179         }
1180 
1181         // Parse module now
1182         auto TheModule = loadModuleFromInput(Mod.get(), Context, false,
1183                                              /*IsImporting*/ false);
1184 
1185         // Save temps: original file.
1186         saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");
1187 
1188         auto &ImportList = ImportLists[ModuleIdentifier];
1189         // Run the main process now, and generates a binary
1190         auto OutputBuffer = ProcessThinLTOModule(
1191             *TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
1192             ExportList, GUIDPreservedSymbols,
1193             ModuleToDefinedGVSummaries[ModuleIdentifier], CacheOptions,
1194             DisableCodeGen, SaveTempsDir, Freestanding, OptLevel, count,
1195             DebugPassManager);
1196 
1197         // Commit to the cache (if enabled)
1198         CacheEntry.write(*OutputBuffer);
1199 
1200         if (SavedObjectsDirectoryPath.empty()) {
1201           // We need to generated a memory buffer for the linker.
1202           if (!CacheEntryPath.empty()) {
1203             // When cache is enabled, reload from the cache if possible.
1204             // Releasing the buffer from the heap and reloading it from the
1205             // cache file with mmap helps us to lower memory pressure.
1206             // The freed memory can be used for the next input file.
1207             // The final binary link will read from the VFS cache (hopefully!)
1208             // or from disk (if the memory pressure was too high).
1209             auto ReloadedBufferOrErr = CacheEntry.tryLoadingBuffer();
1210             if (auto EC = ReloadedBufferOrErr.getError()) {
1211               // On error, keep the preexisting buffer and print a diagnostic.
1212               errs() << "remark: can't reload cached file '" << CacheEntryPath
1213                      << "': " << EC.message() << "\n";
1214             } else {
1215               OutputBuffer = std::move(*ReloadedBufferOrErr);
1216             }
1217           }
1218           ProducedBinaries[count] = std::move(OutputBuffer);
1219           return;
1220         }
1221         ProducedBinaryFiles[count] = writeGeneratedObject(
1222             count, CacheEntryPath, *OutputBuffer);
1223       }, IndexCount);
1224     }
1225   }
1226 
1227   pruneCache(CacheOptions.Path, CacheOptions.Policy, ProducedBinaries);
1228 
1229   // If statistics were requested, print them out now.
1230   if (llvm::AreStatisticsEnabled())
1231     llvm::PrintStatistics();
1232   reportAndResetTimings();
1233 }
1234