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

Generated by: LCOV version 1.13