diff --git a/llvm/include/llvm/IR/ModuleSummaryIndex.h b/llvm/include/llvm/IR/ModuleSummaryIndex.h index 39c60229aa1d8..a4eb75ceb6930 100644 --- a/llvm/include/llvm/IR/ModuleSummaryIndex.h +++ b/llvm/include/llvm/IR/ModuleSummaryIndex.h @@ -1315,7 +1315,7 @@ using GVSummaryPtrSet = std::unordered_set; /// Map of a type GUID to type id string and summary (multimap used /// in case of GUID conflicts). using TypeIdSummaryMapTy = - std::multimap>; + std::multimap>; /// The following data structures summarize type metadata information. /// For type metadata overview see https://llvm.org/docs/TypeMetadata.html. @@ -1351,6 +1351,9 @@ class ModuleSummaryIndex { /// Holds strings for combined index, mapping to the corresponding module ID. ModulePathStringTableTy ModulePathStringTable; + BumpPtrAllocator TypeIdSaverAlloc; + UniqueStringSaver TypeIdSaver; + /// Mapping from type identifier GUIDs to type identifier and its summary /// information. Produced by thin link. TypeIdSummaryMapTy TypeIdMap; @@ -1359,7 +1362,7 @@ class ModuleSummaryIndex { /// with that type identifier's metadata. Produced by per module summary /// analysis and consumed by thin link. For more information, see description /// above where TypeIdCompatibleVtableInfo is defined. - std::map> + std::map> TypeIdCompatibleVtableMap; /// Mapping from original ID to GUID. If original ID can map to multiple @@ -1455,8 +1458,9 @@ class ModuleSummaryIndex { // See HaveGVs variable comment. ModuleSummaryIndex(bool HaveGVs, bool EnableSplitLTOUnit = false, bool UnifiedLTO = false) - : HaveGVs(HaveGVs), EnableSplitLTOUnit(EnableSplitLTOUnit), - UnifiedLTO(UnifiedLTO), Saver(Alloc) {} + : TypeIdSaver(TypeIdSaverAlloc), HaveGVs(HaveGVs), + EnableSplitLTOUnit(EnableSplitLTOUnit), UnifiedLTO(UnifiedLTO), + Saver(Alloc) {} // Current version for the module summary in bitcode files. // The BitcodeSummaryVersion should be bumped whenever we introduce changes @@ -1829,8 +1833,8 @@ class ModuleSummaryIndex { for (auto &[GUID, TypeIdPair] : make_range(TidIter)) if (TypeIdPair.first == TypeId) return TypeIdPair.second; - auto It = TypeIdMap.insert( - {GlobalValue::getGUID(TypeId), {std::string(TypeId), TypeIdSummary()}}); + auto It = TypeIdMap.insert({GlobalValue::getGUID(TypeId), + {TypeIdSaver.save(TypeId), TypeIdSummary()}}); return It->second.second; } @@ -1859,7 +1863,7 @@ class ModuleSummaryIndex { /// the ThinLTO backends. TypeIdCompatibleVtableInfo & getOrInsertTypeIdCompatibleVtableSummary(StringRef TypeId) { - return TypeIdCompatibleVtableMap[std::string(TypeId)]; + return TypeIdCompatibleVtableMap[TypeIdSaver.save(TypeId)]; } /// For the given \p TypeId, this returns the TypeIdCompatibleVtableMap diff --git a/llvm/include/llvm/IR/ModuleSummaryIndexYAML.h b/llvm/include/llvm/IR/ModuleSummaryIndexYAML.h index 7c405025630c9..b23fd4a72c93b 100644 --- a/llvm/include/llvm/IR/ModuleSummaryIndexYAML.h +++ b/llvm/include/llvm/IR/ModuleSummaryIndexYAML.h @@ -313,11 +313,11 @@ template <> struct CustomMappingTraits { static void inputOne(IO &io, StringRef Key, TypeIdSummaryMapTy &V) { TypeIdSummary TId; io.mapRequired(Key.str().c_str(), TId); - V.insert({GlobalValue::getGUID(Key), {std::string(Key), TId}}); + V.insert({GlobalValue::getGUID(Key), {Key, TId}}); } static void output(IO &io, TypeIdSummaryMapTy &V) { for (auto &TidIter : V) - io.mapRequired(TidIter.second.first.c_str(), TidIter.second.second); + io.mapRequired(TidIter.second.first.str().c_str(), TidIter.second.second); } }; @@ -327,7 +327,21 @@ template <> struct MappingTraits { if (!io.outputting()) CustomMappingTraits::fixAliaseeLinks( index.GlobalValueMap); - io.mapOptional("TypeIdMap", index.TypeIdMap); + + if (io.outputting()) { + io.mapOptional("TypeIdMap", index.TypeIdMap); + } else { + TypeIdSummaryMapTy TypeIdMap; + io.mapOptional("TypeIdMap", TypeIdMap); + for (auto &[TypeGUID, TypeIdSummaryMap] : TypeIdMap) { + // Save type id references in index and point TypeIdMap to use the + // references owned by index. + StringRef KeyRef = index.TypeIdSaver.save(TypeIdSummaryMap.first); + index.TypeIdMap.insert( + {TypeGUID, {KeyRef, std::move(TypeIdSummaryMap.second)}}); + } + } + io.mapOptional("WithGlobalValueDeadStripping", index.WithGlobalValueDeadStripping); diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp index 24a4c2e8303d5..6f50930a0fc38 100644 --- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -4165,7 +4165,7 @@ static void writeWholeProgramDevirtResolution( static void writeTypeIdSummaryRecord(SmallVector &NameVals, StringTableBuilder &StrtabBuilder, - const std::string &Id, + StringRef Id, const TypeIdSummary &Summary) { NameVals.push_back(StrtabBuilder.add(Id)); NameVals.push_back(Id.size()); @@ -4184,7 +4184,7 @@ static void writeTypeIdSummaryRecord(SmallVector &NameVals, static void writeTypeIdCompatibleVtableSummaryRecord( SmallVector &NameVals, StringTableBuilder &StrtabBuilder, - const std::string &Id, const TypeIdCompatibleVtableInfo &Summary, + StringRef Id, const TypeIdCompatibleVtableInfo &Summary, ValueEnumerator &VE) { NameVals.push_back(StrtabBuilder.add(Id)); NameVals.push_back(Id.size());