diff options
author | Mingming Liu <mingmingl@google.com> | 2024-07-08 22:20:33 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-08 22:20:33 -0700 |
commit | 50fea9943fa59ed1aeb4538e0f715cc01db58a9a (patch) | |
tree | 9f052b740a327d2f6a10b1448e79ccbff2591d81 /llvm/lib/Bitcode/Writer/BitcodeWriter.cpp | |
parent | 84741940f2d9cb858ecb29ce5197714a45e7e67a (diff) | |
download | llvm-50fea9943fa59ed1aeb4538e0f715cc01db58a9a.zip llvm-50fea9943fa59ed1aeb4538e0f715cc01db58a9a.tar.gz llvm-50fea9943fa59ed1aeb4538e0f715cc01db58a9a.tar.bz2 |
Reland "[ThinLTO][Bitcode] Generate import type in bitcode" (#97253)
https://github.com/llvm/llvm-project/pull/87600 was reverted in order to
revert
https://github.com/llvm/llvm-project/commit/6262763341fcd71a2b0708cf7485f9abd1d26ba8.
Now https://github.com/llvm/llvm-project/pull/95482 is fix forward for
https://github.com/llvm/llvm-project/commit/6262763341fcd71a2b0708cf7485f9abd1d26ba8.
This patch is a reland for
https://github.com/llvm/llvm-project/pull/87600
**Changes on top of original patch**
In `llvm/include/llvm/IR/ModuleSummaryIndex.h`, make the type of
`GVSummaryPtrSet` an `unordered_set` which is more memory efficient when
the number of elements is smaller than 128 [1]
**Original commit message**
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.
[1]
https://github.com/llvm/llvm-project/blob/393eff4e02e7ab3d234d246a8d6912c8e745e6f9/llvm/lib/Support/SmallPtrSet.cpp#L43
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 7a228fb..3378931 100644 --- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -425,6 +425,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; @@ -453,11 +458,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 DecSummaries 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) { // See if the StackIdIndex was already added to the StackId map and @@ -1226,7 +1236,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 @@ -1241,7 +1252,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; } @@ -4568,6 +4580,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->count(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; @@ -4678,7 +4696,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()); @@ -4727,7 +4746,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); @@ -5068,8 +5088,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(); } @@ -5124,12 +5145,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()); |