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