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