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

Generated by: LCOV version 1.13