LCOV - code coverage report
Current view: top level - lib/LTO - ThinLTOCodeGenerator.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 347 403 86.1 %
Date: 2018-07-13 00:08:38 Functions: 40 46 87.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13