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

Generated by: LCOV version 1.13