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