diff --git a/llvm/include/llvm/IR/ModuleSummaryIndex.h b/llvm/include/llvm/IR/ModuleSummaryIndex.h index 396a3fddf4f6b..2650456c49841 100644 --- a/llvm/include/llvm/IR/ModuleSummaryIndex.h +++ b/llvm/include/llvm/IR/ModuleSummaryIndex.h @@ -1291,52 +1291,69 @@ struct TypeIdSummary { }; class CfiFunctionIndex { - std::set> Index; + DenseMap>> Index; + using IndexIterator = + DenseMap>>::const_iterator; + using NestedIterator = std::set>::const_iterator; public: - class GUIDIterator - : public iterator_adaptor_base< - GUIDIterator, std::set>::const_iterator, - std::forward_iterator_tag, GlobalValue::GUID> { - using base = iterator_adaptor_base< - GUIDIterator, std::set>::const_iterator, - std::forward_iterator_tag, GlobalValue::GUID>; + // Iterates keys of the DenseMap. + class GUIDIterator : public iterator_adaptor_base { + using base = GUIDIterator::iterator_adaptor_base; public: GUIDIterator() = default; - explicit GUIDIterator(std::set>::const_iterator I) - : base(std::move(I)) {} + explicit GUIDIterator(IndexIterator I) : base(I) {} - GlobalValue::GUID operator*() const { - return GlobalValue::getGUID( - GlobalValue::dropLLVMManglingEscape(*this->wrapped())); - } + GlobalValue::GUID operator*() const { return this->wrapped()->first; } }; CfiFunctionIndex() = default; - template CfiFunctionIndex(It B, It E) : Index(B, E) {} - - std::set>::const_iterator begin() const { - return Index.begin(); + template CfiFunctionIndex(It B, It E) { + for (; B != E; ++B) + emplace(*B); } - std::set>::const_iterator end() const { - return Index.end(); + std::vector symbols() const { + std::vector Symbols; + for (auto &[GUID, Syms] : Index) + Symbols.insert(Symbols.end(), Syms.begin(), Syms.end()); + return Symbols; } - std::vector symbols() const { return {begin(), end()}; } - GUIDIterator guid_begin() const { return GUIDIterator(Index.begin()); } GUIDIterator guid_end() const { return GUIDIterator(Index.end()); } iterator_range guids() const { return make_range(guid_begin(), guid_end()); } + iterator_range forGuid(GlobalValue::GUID GUID) const { + auto I = Index.find(GUID); + if (I == Index.end()) + return make_range(NestedIterator{}, NestedIterator{}); + return make_range(I->second.begin(), I->second.end()); + } + template void emplace(Args &&...A) { - Index.emplace(std::forward(A)...); + StringRef S(std::forward(A)...); + GlobalValue::GUID GUID = + GlobalValue::getGUID(GlobalValue::dropLLVMManglingEscape(S)); + Index[GUID].emplace(S); + } + + size_t count(StringRef S) const { + GlobalValue::GUID GUID = + GlobalValue::getGUID(GlobalValue::dropLLVMManglingEscape(S)); + auto I = Index.find(GUID); + if (I == Index.end()) + return 0; + return I->second.count(S); } - size_t count(StringRef S) const { return Index.count(S); } + bool empty() const { return Index.empty(); } }; /// 160 bits SHA1 diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp index 2f4802ca2d404..f6510a7a1549d 100644 --- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -5064,19 +5064,25 @@ void IndexBitcodeWriter::writeCombinedGlobalValueSummary() { getReferencedTypeIds(FS, ReferencedTypeIds); } + SmallVector Functions; auto EmitCfiFunctions = [&](const CfiFunctionIndex &CfiIndex, bitc::GlobalValueSummarySymtabCodes Code) { - for (auto &S : CfiIndex) { - if (DefOrUseGUIDs.contains( - GlobalValue::getGUID(GlobalValue::dropLLVMManglingEscape(S)))) { - NameVals.push_back(StrtabBuilder.add(S)); - NameVals.push_back(S.size()); - } + if (CfiIndex.empty()) + return; + for (GlobalValue::GUID GUID : DefOrUseGUIDs) { + auto Defs = CfiIndex.forGuid(GUID); + Functions.insert(Functions.end(), Defs.begin(), Defs.end()); } - if (!NameVals.empty()) { - Stream.EmitRecord(Code, NameVals); - NameVals.clear(); + if (Functions.empty()) + return; + llvm::sort(Functions); + for (const auto &S : Functions) { + NameVals.push_back(StrtabBuilder.add(S)); + NameVals.push_back(S.size()); } + Stream.EmitRecord(Code, NameVals); + NameVals.clear(); + Functions.clear(); }; EmitCfiFunctions(Index.cfiFunctionDefs(), bitc::FS_CFI_FUNCTION_DEFS);