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