diff options
author | Jin Xin Ng <njx@google.com> | 2022-06-01 10:49:36 -0700 |
---|---|---|
committer | Jin Xin Ng <njx@google.com> | 2022-06-23 12:35:42 -0700 |
commit | 22f1273357cfe1d7d6e395c447d1be10360cffaa (patch) | |
tree | 8fa49b5b9dc2f37b68de42d97cc86831600868a1 /llvm/lib/LTO/LTO.cpp | |
parent | 8c6da76483935d172c34e04e6c0106e33d803c61 (diff) | |
download | llvm-22f1273357cfe1d7d6e395c447d1be10360cffaa.zip llvm-22f1273357cfe1d7d6e395c447d1be10360cffaa.tar.gz llvm-22f1273357cfe1d7d6e395c447d1be10360cffaa.tar.bz2 |
[ThinLTO][ELF] Add --thinlto-emit-index-files option
Allows ThinLTO indices to be written to disk on-the-fly/as-part-of “normal” linker execution. Previously ThinLTO indices could be written via --thinlto-index-only but that would cause the linker to exit early. For MLGO specifically, this enables saving the ThinLTO index files without having to restart the linker to collect data only available at later stages (i.e. output of --save-temps) of the linker's execution.
Note, this option does not currently work with:
--thinlto-object-suffix-replace, as this is intended to be used to consume minimized IR bitcode files while --thinlto-emit-index-files is intended to be run together with InProcessThinLTO (which cannot parse minimized IR).
--thinlto-prefix-replace support is left unimplemented but can be implemented if needed
Differential Revision: https://reviews.llvm.org/D127777
Diffstat (limited to 'llvm/lib/LTO/LTO.cpp')
-rw-r--r-- | llvm/lib/LTO/LTO.cpp | 84 |
1 files changed, 55 insertions, 29 deletions
diff --git a/llvm/lib/LTO/LTO.cpp b/llvm/lib/LTO/LTO.cpp index 8d1570f..a9e04ba 100644 --- a/llvm/lib/LTO/LTO.cpp +++ b/llvm/lib/LTO/LTO.cpp @@ -1161,12 +1161,16 @@ protected: const Config &Conf; ModuleSummaryIndex &CombinedIndex; const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries; + lto::IndexWriteCallback OnWrite; + bool ShouldEmitImportsFiles; public: ThinBackendProc(const Config &Conf, ModuleSummaryIndex &CombinedIndex, - const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries) + const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries, + lto::IndexWriteCallback OnWrite, bool ShouldEmitImportsFiles) : Conf(Conf), CombinedIndex(CombinedIndex), - ModuleToDefinedGVSummaries(ModuleToDefinedGVSummaries) {} + ModuleToDefinedGVSummaries(ModuleToDefinedGVSummaries), + OnWrite(OnWrite), ShouldEmitImportsFiles(ShouldEmitImportsFiles) {} virtual ~ThinBackendProc() = default; virtual Error start( @@ -1177,6 +1181,30 @@ public: MapVector<StringRef, BitcodeModule> &ModuleMap) = 0; virtual Error wait() = 0; virtual unsigned getThreadCount() = 0; + + // Write sharded indices and (optionally) imports to disk + Error emitFiles(const FunctionImporter::ImportMapTy &ImportList, + llvm::StringRef ModulePath, + const std::string &NewModulePath) { + std::map<std::string, GVSummaryMapTy> ModuleToSummariesForIndex; + std::error_code EC; + gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries, + ImportList, ModuleToSummariesForIndex); + + raw_fd_ostream OS(NewModulePath + ".thinlto.bc", EC, + sys::fs::OpenFlags::OF_None); + if (EC) + return errorCodeToError(EC); + writeIndexToFile(CombinedIndex, OS, &ModuleToSummariesForIndex); + + if (ShouldEmitImportsFiles) { + EC = EmitImportsFiles(ModulePath, NewModulePath + ".imports", + ModuleToSummariesForIndex); + if (EC) + return errorCodeToError(EC); + } + return Error::success(); + } }; namespace { @@ -1190,15 +1218,19 @@ class InProcessThinBackend : public ThinBackendProc { Optional<Error> Err; std::mutex ErrMu; + bool ShouldEmitIndexFiles; + public: InProcessThinBackend( const Config &Conf, ModuleSummaryIndex &CombinedIndex, ThreadPoolStrategy ThinLTOParallelism, const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries, - AddStreamFn AddStream, FileCache Cache) - : ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries), + AddStreamFn AddStream, FileCache Cache, lto::IndexWriteCallback OnWrite, + bool ShouldEmitIndexFiles, bool ShouldEmitImportsFiles) + : ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries, + OnWrite, ShouldEmitImportsFiles), BackendThreadPool(ThinLTOParallelism), AddStream(std::move(AddStream)), - Cache(std::move(Cache)) { + Cache(std::move(Cache)), ShouldEmitIndexFiles(ShouldEmitIndexFiles) { for (auto &Name : CombinedIndex.cfiFunctionDefs()) CfiFunctionDefs.insert( GlobalValue::getGUID(GlobalValue::dropLLVMManglingEscape(Name))); @@ -1227,6 +1259,11 @@ public: auto ModuleID = BM.getModuleIdentifier(); + if (ShouldEmitIndexFiles) { + if (auto E = emitFiles(ImportList, ModuleID, ModuleID.str())) + return E; + } + if (!Cache || !CombinedIndex.modulePaths().count(ModuleID) || all_of(CombinedIndex.getModuleHash(ModuleID), [](uint32_t V) { return V == 0; })) @@ -1285,6 +1322,9 @@ public: }, BM, std::ref(CombinedIndex), std::ref(ImportList), std::ref(ExportList), std::ref(ResolvedODR), std::ref(DefinedGlobals), std::ref(ModuleMap)); + + if (OnWrite) + OnWrite(std::string(ModulePath)); return Error::success(); } @@ -1302,13 +1342,16 @@ public: }; } // end anonymous namespace -ThinBackend lto::createInProcessThinBackend(ThreadPoolStrategy Parallelism) { +ThinBackend lto::createInProcessThinBackend(ThreadPoolStrategy Parallelism, + lto::IndexWriteCallback OnWrite, + bool ShouldEmitIndexFiles, + bool ShouldEmitImportsFiles) { return [=](const Config &Conf, ModuleSummaryIndex &CombinedIndex, const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries, AddStreamFn AddStream, FileCache Cache) { return std::make_unique<InProcessThinBackend>( Conf, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries, AddStream, - Cache); + Cache, OnWrite, ShouldEmitIndexFiles, ShouldEmitImportsFiles); }; } @@ -1335,9 +1378,7 @@ std::string lto::getThinLTOOutputFile(const std::string &Path, namespace { class WriteIndexesThinBackend : public ThinBackendProc { std::string OldPrefix, NewPrefix; - bool ShouldEmitImportsFiles; raw_fd_ostream *LinkedObjectsFile; - lto::IndexWriteCallback OnWrite; public: WriteIndexesThinBackend( @@ -1345,10 +1386,10 @@ public: const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries, std::string OldPrefix, std::string NewPrefix, bool ShouldEmitImportsFiles, raw_fd_ostream *LinkedObjectsFile, lto::IndexWriteCallback OnWrite) - : ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries), + : ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries, + OnWrite, ShouldEmitImportsFiles), OldPrefix(OldPrefix), NewPrefix(NewPrefix), - ShouldEmitImportsFiles(ShouldEmitImportsFiles), - LinkedObjectsFile(LinkedObjectsFile), OnWrite(OnWrite) {} + LinkedObjectsFile(LinkedObjectsFile) {} Error start( unsigned Task, BitcodeModule BM, @@ -1363,23 +1404,8 @@ public: if (LinkedObjectsFile) *LinkedObjectsFile << NewModulePath << '\n'; - std::map<std::string, GVSummaryMapTy> ModuleToSummariesForIndex; - gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries, - ImportList, ModuleToSummariesForIndex); - - std::error_code EC; - raw_fd_ostream OS(NewModulePath + ".thinlto.bc", EC, - sys::fs::OpenFlags::OF_None); - if (EC) - return errorCodeToError(EC); - writeIndexToFile(CombinedIndex, OS, &ModuleToSummariesForIndex); - - if (ShouldEmitImportsFiles) { - EC = EmitImportsFiles(ModulePath, NewModulePath + ".imports", - ModuleToSummariesForIndex); - if (EC) - return errorCodeToError(EC); - } + if (auto E = emitFiles(ImportList, ModulePath, NewModulePath)) + return E; if (OnWrite) OnWrite(std::string(ModulePath)); |