Index: llvm/include/llvm/LTO/LTO.h =================================================================== --- llvm/include/llvm/LTO/LTO.h +++ llvm/include/llvm/LTO/LTO.h @@ -213,11 +213,11 @@ /// OnWrite is callback which receives module identifier and notifies LTO user /// that index file for the module (and optionally imports file) was created. using IndexWriteCallback = std::function; -ThinBackend createWriteIndexesThinBackend(std::string OldPrefix, - std::string NewPrefix, - bool ShouldEmitImportsFiles, - std::string LinkedObjectsFile, - IndexWriteCallback OnWrite); +ThinBackend +createWriteIndexesThinBackend(std::string OldPrefix, std::string NewPrefix, + bool ShouldEmitImportsFiles, + std::unique_ptr LinkedObjectsFile, + IndexWriteCallback OnWrite); /// This class implements a resolution-based interface to LLVM's LTO /// functionality. It supports regular LTO, parallel LTO code generation and Index: llvm/lib/LTO/LTO.cpp =================================================================== --- llvm/lib/LTO/LTO.cpp +++ llvm/lib/LTO/LTO.cpp @@ -1034,10 +1034,7 @@ class WriteIndexesThinBackend : public ThinBackendProc { std::string OldPrefix, NewPrefix; bool ShouldEmitImportsFiles; - - std::string LinkedObjectsFileName; - std::unique_ptr LinkedObjectsFile; - + std::shared_ptr LinkedObjectsFile; lto::IndexWriteCallback OnWrite; public: @@ -1045,11 +1042,12 @@ Config &Conf, ModuleSummaryIndex &CombinedIndex, const StringMap &ModuleToDefinedGVSummaries, std::string OldPrefix, std::string NewPrefix, bool ShouldEmitImportsFiles, - std::string LinkedObjectsFileName, lto::IndexWriteCallback OnWrite) + std::shared_ptr LinkedObjectsFile, + lto::IndexWriteCallback OnWrite) : ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries), OldPrefix(OldPrefix), NewPrefix(NewPrefix), ShouldEmitImportsFiles(ShouldEmitImportsFiles), - LinkedObjectsFileName(LinkedObjectsFileName), OnWrite(OnWrite) {} + LinkedObjectsFile(std::move(LinkedObjectsFile)), OnWrite(OnWrite) {} Error start( unsigned Task, BitcodeModule BM, @@ -1061,21 +1059,14 @@ std::string NewModulePath = getThinLTOOutputFile(ModulePath, OldPrefix, NewPrefix); - std::error_code EC; - if (!LinkedObjectsFileName.empty()) { - if (!LinkedObjectsFile) { - LinkedObjectsFile = llvm::make_unique( - LinkedObjectsFileName, EC, sys::fs::OpenFlags::F_None); - if (EC) - return errorCodeToError(EC); - } + if (LinkedObjectsFile) *LinkedObjectsFile << NewModulePath << '\n'; - } std::map ModuleToSummariesForIndex; gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries, ImportList, ModuleToSummariesForIndex); + std::error_code EC; raw_fd_ostream OS(NewModulePath + ".thinlto.bc", EC, sys::fs::OpenFlags::F_None); if (EC) @@ -1097,17 +1088,18 @@ }; } // end anonymous namespace -ThinBackend lto::createWriteIndexesThinBackend(std::string OldPrefix, - std::string NewPrefix, - bool ShouldEmitImportsFiles, - std::string LinkedObjectsFile, - IndexWriteCallback OnWrite) { +ThinBackend lto::createWriteIndexesThinBackend( + std::string OldPrefix, std::string NewPrefix, bool ShouldEmitImportsFiles, + std::unique_ptr LinkedObjectsFile, + IndexWriteCallback OnWrite) { + std::shared_ptr LinkedObjectsFileShared = + std::move(LinkedObjectsFile); return [=](Config &Conf, ModuleSummaryIndex &CombinedIndex, const StringMap &ModuleToDefinedGVSummaries, AddStreamFn AddStream, NativeObjectCache Cache) { return llvm::make_unique( Conf, CombinedIndex, ModuleToDefinedGVSummaries, OldPrefix, NewPrefix, - ShouldEmitImportsFiles, LinkedObjectsFile, OnWrite); + ShouldEmitImportsFiles, LinkedObjectsFileShared, OnWrite); }; } Index: llvm/test/tools/gold/X86/thinlto_no_objects.ll =================================================================== --- /dev/null +++ llvm/test/tools/gold/X86/thinlto_no_objects.ll @@ -0,0 +1,18 @@ +; Check that thinlto-index-only= always creates linked objects file, even +; if nothing to add there. + +; Non-ThinLTO file should not get into list of linked objects. +; RUN: opt %s -o %t.o + +; RUN: rm -f %t3 +; RUN: %gold -plugin %llvmshlibdir/LLVMgold%shlibext \ +; RUN: --plugin-opt=thinlto \ +; RUN: --plugin-opt=thinlto-index-only=%t3 \ +; RUN: -o %t5 \ +; RUN: %t.o + +; RUN: cat %t3 | count 0 + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + Index: llvm/tools/gold/gold-plugin.cpp =================================================================== --- llvm/tools/gold/gold-plugin.cpp +++ llvm/tools/gold/gold-plugin.cpp @@ -736,6 +736,22 @@ std::tie(OldPrefix, NewPrefix) = PrefixReplace.split(';'); } +// Creates and return output stream with a list of object files for final +// linking of distributed ThinLTO. +static std::unique_ptr CreateLinkedObjectsFile() { + if (!options::thinlto_linked_objects_file.empty()) { + std::error_code EC; + auto LinkedObjectsFile = llvm::make_unique( + options::thinlto_linked_objects_file, EC, sys::fs::OpenFlags::F_None); + if (EC) + message(LDPL_FATAL, "Failed to create '%s': %s", + options::thinlto_linked_objects_file.c_str(), + EC.message().c_str()); + return LinkedObjectsFile; + } + return nullptr; +} + static std::unique_ptr createLTO(IndexWriteCallback OnIndexWrite) { Config Conf; ThinBackend Backend; @@ -762,7 +778,7 @@ getThinLTOOldAndNewPrefix(OldPrefix, NewPrefix); Backend = createWriteIndexesThinBackend( OldPrefix, NewPrefix, options::thinlto_emit_imports_files, - options::thinlto_linked_objects_file, OnIndexWrite); + CreateLinkedObjectsFile(), OnIndexWrite); } Conf.OverrideTriple = options::triple; Index: llvm/tools/llvm-lto2/llvm-lto2.cpp =================================================================== --- llvm/tools/llvm-lto2/llvm-lto2.cpp +++ llvm/tools/llvm-lto2/llvm-lto2.cpp @@ -243,7 +243,11 @@ ThinBackend Backend; if (ThinLTODistributedIndexes) - Backend = createWriteIndexesThinBackend("", "", true, "", {}); + Backend = createWriteIndexesThinBackend(/* OldPrefix */ "", + /* NewPrefix */ "", + /* ShouldEmitImportsFiles */ true, + /* LinkedObjectsFile */ nullptr, + /* OnWrite */ {}); else Backend = createInProcessThinBackend(Threads); LTO Lto(std::move(Conf), std::move(Backend));