llvm.org GIT mirror llvm / 55523de
[ThinLTO] Always create linked objects file for --thinlto-index-only= Summary: ThinLTO indexing may decide to skip all objects. If we don't write something to the list build system may consider this as failure or linker can reuse a file from the previews build. Reviewers: pcc, tejohnson Subscribers: mehdi_amini, inglorion, eraman, hiraditya, llvm-commits Differential Revision: https://reviews.llvm.org/D43415 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325819 91177308-0d34-0410-b5e6-96231b3b80d8 Vitaly Buka 2 years ago
5 changed file(s) with 63 addition(s) and 29 deletion(s). Raw diff Collapse all Expand all
209209 /// appends ".thinlto.bc" and writes the index to that path. If
210210 /// ShouldEmitImportsFiles is true it also writes a list of imported files to a
211211 /// similar path with ".imports" appended instead.
212 /// LinkedObjectsFile is an output stream to write the list of object files for
213 /// the final ThinLTO linking. Can be nullptr.
212214 /// OnWrite is callback which receives module identifier and notifies LTO user
213215 /// that index file for the module (and optionally imports file) was created.
214216 using IndexWriteCallback = std::function;
215217 ThinBackend createWriteIndexesThinBackend(std::string OldPrefix,
216218 std::string NewPrefix,
217219 bool ShouldEmitImportsFiles,
218 std::string LinkedObjectsFile,
220 raw_fd_ostream *LinkedObjectsFile,
219221 IndexWriteCallback OnWrite);
220222
221223 /// This class implements a resolution-based interface to LLVM's LTO
10371037 class WriteIndexesThinBackend : public ThinBackendProc {
10381038 std::string OldPrefix, NewPrefix;
10391039 bool ShouldEmitImportsFiles;
1040
1041 std::string LinkedObjectsFileName;
1042 std::unique_ptr LinkedObjectsFile;
1043
1040 raw_fd_ostream *LinkedObjectsFile;
10441041 lto::IndexWriteCallback OnWrite;
10451042
10461043 public:
10481045 Config &Conf, ModuleSummaryIndex &CombinedIndex,
10491046 const StringMap &ModuleToDefinedGVSummaries,
10501047 std::string OldPrefix, std::string NewPrefix, bool ShouldEmitImportsFiles,
1051 std::string LinkedObjectsFileName, lto::IndexWriteCallback OnWrite)
1048 raw_fd_ostream *LinkedObjectsFile, lto::IndexWriteCallback OnWrite)
10521049 : ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries),
10531050 OldPrefix(OldPrefix), NewPrefix(NewPrefix),
10541051 ShouldEmitImportsFiles(ShouldEmitImportsFiles),
1055 LinkedObjectsFileName(LinkedObjectsFileName), OnWrite(OnWrite) {}
1052 LinkedObjectsFile(LinkedObjectsFile), OnWrite(OnWrite) {}
10561053
10571054 Error start(
10581055 unsigned Task, BitcodeModule BM,
10641061 std::string NewModulePath =
10651062 getThinLTOOutputFile(ModulePath, OldPrefix, NewPrefix);
10661063
1067 std::error_code EC;
1068 if (!LinkedObjectsFileName.empty()) {
1069 if (!LinkedObjectsFile) {
1070 LinkedObjectsFile = llvm::make_unique(
1071 LinkedObjectsFileName, EC, sys::fs::OpenFlags::F_None);
1072 if (EC)
1073 return errorCodeToError(EC);
1074 }
1064 if (LinkedObjectsFile)
10751065 *LinkedObjectsFile << NewModulePath << '\n';
1076 }
10771066
10781067 std::map ModuleToSummariesForIndex;
10791068 gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries,
10801069 ImportList, ModuleToSummariesForIndex);
10811070
1071 std::error_code EC;
10821072 raw_fd_ostream OS(NewModulePath + ".thinlto.bc", EC,
10831073 sys::fs::OpenFlags::F_None);
10841074 if (EC)
11001090 };
11011091 } // end anonymous namespace
11021092
1103 ThinBackend lto::createWriteIndexesThinBackend(std::string OldPrefix,
1104 std::string NewPrefix,
1105 bool ShouldEmitImportsFiles,
1106 std::string LinkedObjectsFile,
1107 IndexWriteCallback OnWrite) {
1093 ThinBackend lto::createWriteIndexesThinBackend(
1094 std::string OldPrefix, std::string NewPrefix, bool ShouldEmitImportsFiles,
1095 raw_fd_ostream *LinkedObjectsFile, IndexWriteCallback OnWrite) {
11081096 return [=](Config &Conf, ModuleSummaryIndex &CombinedIndex,
11091097 const StringMap &ModuleToDefinedGVSummaries,
11101098 AddStreamFn AddStream, NativeObjectCache Cache) {
0 ; Check that thinlto-index-only= always creates linked objects file, even
1 ; if nothing to add there.
2
3 ; Non-ThinLTO file should not get into list of linked objects.
4 ; RUN: opt %s -o %t.o
5
6 ; RUN: rm -f %t3
7 ; RUN: %gold -plugin %llvmshlibdir/LLVMgold%shlibext \
8 ; RUN: --plugin-opt=thinlto \
9 ; RUN: --plugin-opt=thinlto-index-only=%t3 \
10 ; RUN: -o %t5 \
11 ; RUN: %t.o
12
13 ; RUN: cat %t3 | count 0
14
15 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
16 target triple = "x86_64-unknown-linux-gnu"
17
735735 std::tie(OldPrefix, NewPrefix) = PrefixReplace.split(';');
736736 }
737737
738 static std::unique_ptr createLTO(IndexWriteCallback OnIndexWrite) {
738 /// Creates instance of LTO.
739 /// OnIndexWrite is callback to let caller know when LTO writes index files.
740 /// LinkedObjectsFile is an output stream to write the list of object files for
741 /// the final ThinLTO linking. Can be nullptr.
742 static std::unique_ptr createLTO(IndexWriteCallback OnIndexWrite,
743 raw_fd_ostream *LinkedObjectsFile) {
739744 Config Conf;
740745 ThinBackend Backend;
741746
759764 if (options::thinlto_index_only) {
760765 std::string OldPrefix, NewPrefix;
761766 getThinLTOOldAndNewPrefix(OldPrefix, NewPrefix);
762 Backend = createWriteIndexesThinBackend(
763 OldPrefix, NewPrefix, options::thinlto_emit_imports_files,
764 options::thinlto_linked_objects_file, OnIndexWrite);
767 Backend = createWriteIndexesThinBackend(OldPrefix, NewPrefix,
768 options::thinlto_emit_imports_files,
769 LinkedObjectsFile, OnIndexWrite);
765770 }
766771
767772 Conf.OverrideTriple = options::triple;
843848 }
844849 }
845850
851 // Creates and returns output stream with a list of object files for final
852 // linking of distributed ThinLTO.
853 static std::unique_ptr CreateLinkedObjectsFile() {
854 if (options::thinlto_linked_objects_file.empty())
855 return nullptr;
856 assert(options::thinlto_index_only);
857 std::error_code EC;
858 auto LinkedObjectsFile = llvm::make_unique(
859 options::thinlto_linked_objects_file, EC, sys::fs::OpenFlags::F_None);
860 if (EC)
861 message(LDPL_FATAL, "Failed to create '%s': %s",
862 options::thinlto_linked_objects_file.c_str(), EC.message().c_str());
863 return LinkedObjectsFile;
864 }
865
846866 /// Runs LTO and return a list of pairs .
847867 static std::vector, bool>> runLTO() {
848868 // Map to own RAII objects that manage the file opening and releasing
855875 // Owns string objects and tells if index file was already created.
856876 StringMap ObjectToIndexFileState;
857877
858 std::unique_ptr Lto =
859 createLTO([&ObjectToIndexFileState](const std::string &Identifier) {
878 std::unique_ptr LinkedObjects = CreateLinkedObjectsFile();
879 std::unique_ptr Lto = createLTO(
880 [&ObjectToIndexFileState](const std::string &Identifier) {
860881 ObjectToIndexFileState[Identifier] = true;
861 });
882 },
883 LinkedObjects.get());
862884
863885 std::string OldPrefix, NewPrefix;
864886 if (options::thinlto_index_only)
242242
243243 ThinBackend Backend;
244244 if (ThinLTODistributedIndexes)
245 Backend = createWriteIndexesThinBackend("", "", true, "", {});
245 Backend = createWriteIndexesThinBackend(/* OldPrefix */ "",
246 /* NewPrefix */ "",
247 /* ShouldEmitImportsFiles */ true,
248 /* LinkedObjectsFile */ nullptr,
249 /* OnWrite */ {});
246250 else
247251 Backend = createInProcessThinBackend(Threads);
248252 LTO Lto(std::move(Conf), std::move(Backend));