diff options
author | Mingming Liu <mingmingl@google.com> | 2024-05-22 09:52:54 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-22 09:52:54 -0700 |
commit | 6262763341fcd71a2b0708cf7485f9abd1d26ba8 (patch) | |
tree | 5a574bad6a6bd1170907647932b78abc837c41ce /llvm/lib/Bitcode/Writer/BitcodeWriter.cpp | |
parent | 271eb0686b7b0f9d2e016751399b948ccbbe7925 (diff) | |
download | llvm-6262763341fcd71a2b0708cf7485f9abd1d26ba8.zip llvm-6262763341fcd71a2b0708cf7485f9abd1d26ba8.tar.gz llvm-6262763341fcd71a2b0708cf7485f9abd1d26ba8.tar.bz2 |
[ThinLTO][Bitcode] Generate import type in bitcode (#87600)
For distributed ThinLTO, the LTO indexing step generates combined
summary for each module, and postlink pipeline reads the combined
summary which stores the information for link-time optimization.
This patch populates the 'import type' of a summary in bitcode, and
updates bitcode reader to parse the bit correctly.
Diffstat (limited to 'llvm/lib/Bitcode/Writer/BitcodeWriter.cpp')
-rw-r--r-- | llvm/lib/Bitcode/Writer/BitcodeWriter.cpp | 38 |
1 files changed, 30 insertions, 8 deletions
diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp index c4cea3d..c5fdd111 100644 --- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -428,6 +428,11 @@ class IndexBitcodeWriter : public BitcodeWriterBase { /// The combined index to write to bitcode. const ModuleSummaryIndex &Index; + /// When writing combined summaries, provides the set of global value + /// summaries for which the value (function, function alias, etc) should be + /// imported as a declaration. + const GVSummaryPtrSet *DecSummaries = nullptr; + /// When writing a subset of the index for distributed backends, client /// provides a map of modules to the corresponding GUIDs/summaries to write. const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex; @@ -452,11 +457,16 @@ public: /// Constructs a IndexBitcodeWriter object for the given combined index, /// writing to the provided \p Buffer. When writing a subset of the index /// for a distributed backend, provide a \p ModuleToSummariesForIndex map. + /// If provided, \p ModuleToDecSummaries specifies the set of summaries for + /// which the corresponding functions or aliased functions should be imported + /// as a declaration (but not definition) for each module. IndexBitcodeWriter(BitstreamWriter &Stream, StringTableBuilder &StrtabBuilder, const ModuleSummaryIndex &Index, + const GVSummaryPtrSet *DecSummaries = nullptr, const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex = nullptr) : BitcodeWriterBase(Stream, StrtabBuilder), Index(Index), + DecSummaries(DecSummaries), ModuleToSummariesForIndex(ModuleToSummariesForIndex) { // Assign unique value ids to all summaries to be written, for use // in writing out the call graph edges. Save the mapping from GUID @@ -1202,7 +1212,8 @@ static uint64_t getEncodedFFlags(FunctionSummary::FFlags Flags) { // Decode the flags for GlobalValue in the summary. See getDecodedGVSummaryFlags // in BitcodeReader.cpp. -static uint64_t getEncodedGVSummaryFlags(GlobalValueSummary::GVFlags Flags) { +static uint64_t getEncodedGVSummaryFlags(GlobalValueSummary::GVFlags Flags, + bool ImportAsDecl = false) { uint64_t RawFlags = 0; RawFlags |= Flags.NotEligibleToImport; // bool @@ -1217,7 +1228,8 @@ static uint64_t getEncodedGVSummaryFlags(GlobalValueSummary::GVFlags Flags) { RawFlags |= (Flags.Visibility << 8); // 2 bits - RawFlags |= (Flags.ImportType << 10); // 1 bit + unsigned ImportType = Flags.ImportType | ImportAsDecl; + RawFlags |= (ImportType << 10); // 1 bit return RawFlags; } @@ -4543,6 +4555,12 @@ void IndexBitcodeWriter::writeCombinedGlobalValueSummary() { Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); unsigned AllocAbbrev = Stream.EmitAbbrev(std::move(Abbv)); + auto shouldImportValueAsDecl = [&](GlobalValueSummary *GVS) -> bool { + if (DecSummaries == nullptr) + return false; + return DecSummaries->contains(GVS); + }; + // The aliases are emitted as a post-pass, and will point to the value // id of the aliasee. Save them in a vector for post-processing. SmallVector<AliasSummary *, 64> Aliases; @@ -4653,7 +4671,8 @@ void IndexBitcodeWriter::writeCombinedGlobalValueSummary() { NameVals.push_back(*ValueId); assert(ModuleIdMap.count(FS->modulePath())); NameVals.push_back(ModuleIdMap[FS->modulePath()]); - NameVals.push_back(getEncodedGVSummaryFlags(FS->flags())); + NameVals.push_back( + getEncodedGVSummaryFlags(FS->flags(), shouldImportValueAsDecl(FS))); NameVals.push_back(FS->instCount()); NameVals.push_back(getEncodedFFlags(FS->fflags())); NameVals.push_back(FS->entryCount()); @@ -4702,7 +4721,8 @@ void IndexBitcodeWriter::writeCombinedGlobalValueSummary() { NameVals.push_back(AliasValueId); assert(ModuleIdMap.count(AS->modulePath())); NameVals.push_back(ModuleIdMap[AS->modulePath()]); - NameVals.push_back(getEncodedGVSummaryFlags(AS->flags())); + NameVals.push_back( + getEncodedGVSummaryFlags(AS->flags(), shouldImportValueAsDecl(AS))); auto AliaseeValueId = SummaryToValueIdMap[&AS->getAliasee()]; assert(AliaseeValueId); NameVals.push_back(AliaseeValueId); @@ -5036,8 +5056,9 @@ void BitcodeWriter::writeModule(const Module &M, void BitcodeWriter::writeIndex( const ModuleSummaryIndex *Index, - const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex) { - IndexBitcodeWriter IndexWriter(*Stream, StrtabBuilder, *Index, + const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex, + const GVSummaryPtrSet *DecSummaries) { + IndexBitcodeWriter IndexWriter(*Stream, StrtabBuilder, *Index, DecSummaries, ModuleToSummariesForIndex); IndexWriter.write(); } @@ -5090,12 +5111,13 @@ void IndexBitcodeWriter::write() { // index for a distributed backend, provide a \p ModuleToSummariesForIndex map. void llvm::writeIndexToFile( const ModuleSummaryIndex &Index, raw_ostream &Out, - const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex) { + const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex, + const GVSummaryPtrSet *DecSummaries) { SmallVector<char, 0> Buffer; Buffer.reserve(256 * 1024); BitcodeWriter Writer(Buffer); - Writer.writeIndex(&Index, ModuleToSummariesForIndex); + Writer.writeIndex(&Index, ModuleToSummariesForIndex, DecSummaries); Writer.writeStrtab(); Out.write((char *)&Buffer.front(), Buffer.size()); |