Skip to content

Commit 97b2903

Browse files
[NFCI][WPD]Use unique string saver to store type id (llvm#106932)
Currently, both [TypeIdMap](https://github.com/llvm/llvm-project/blob/67a1fdb014790a38a205d28e1748634de34471dd/llvm/include/llvm/IR/ModuleSummaryIndex.h#L1356) and [TypeIdCompatibleVtableMap](https://github.com/llvm/llvm-project/blob/67a1fdb014790a38a205d28e1748634de34471dd/llvm/include/llvm/IR/ModuleSummaryIndex.h#L1363) keep type-id as `std::string` in the combined index for LTO indexing analysis. With this change, index uses a unique-string-saver to own the string copies and two maps above can use string references to save some memory. This shows a 3% memory reduction (from 8.2GiB to 7.9GiB) in an internal binary with high indexing memory usage.
1 parent a6fefc8 commit 97b2903

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

llvm/include/llvm/IR/ModuleSummaryIndex.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,7 +1315,7 @@ using GVSummaryPtrSet = std::unordered_set<GlobalValueSummary *>;
13151315
/// Map of a type GUID to type id string and summary (multimap used
13161316
/// in case of GUID conflicts).
13171317
using TypeIdSummaryMapTy =
1318-
std::multimap<GlobalValue::GUID, std::pair<std::string, TypeIdSummary>>;
1318+
std::multimap<GlobalValue::GUID, std::pair<StringRef, TypeIdSummary>>;
13191319

13201320
/// The following data structures summarize type metadata information.
13211321
/// For type metadata overview see https://llvm.org/docs/TypeMetadata.html.
@@ -1351,6 +1351,9 @@ class ModuleSummaryIndex {
13511351
/// Holds strings for combined index, mapping to the corresponding module ID.
13521352
ModulePathStringTableTy ModulePathStringTable;
13531353

1354+
BumpPtrAllocator TypeIdSaverAlloc;
1355+
UniqueStringSaver TypeIdSaver;
1356+
13541357
/// Mapping from type identifier GUIDs to type identifier and its summary
13551358
/// information. Produced by thin link.
13561359
TypeIdSummaryMapTy TypeIdMap;
@@ -1359,7 +1362,7 @@ class ModuleSummaryIndex {
13591362
/// with that type identifier's metadata. Produced by per module summary
13601363
/// analysis and consumed by thin link. For more information, see description
13611364
/// above where TypeIdCompatibleVtableInfo is defined.
1362-
std::map<std::string, TypeIdCompatibleVtableInfo, std::less<>>
1365+
std::map<StringRef, TypeIdCompatibleVtableInfo, std::less<>>
13631366
TypeIdCompatibleVtableMap;
13641367

13651368
/// Mapping from original ID to GUID. If original ID can map to multiple
@@ -1455,8 +1458,9 @@ class ModuleSummaryIndex {
14551458
// See HaveGVs variable comment.
14561459
ModuleSummaryIndex(bool HaveGVs, bool EnableSplitLTOUnit = false,
14571460
bool UnifiedLTO = false)
1458-
: HaveGVs(HaveGVs), EnableSplitLTOUnit(EnableSplitLTOUnit),
1459-
UnifiedLTO(UnifiedLTO), Saver(Alloc) {}
1461+
: TypeIdSaver(TypeIdSaverAlloc), HaveGVs(HaveGVs),
1462+
EnableSplitLTOUnit(EnableSplitLTOUnit), UnifiedLTO(UnifiedLTO),
1463+
Saver(Alloc) {}
14601464

14611465
// Current version for the module summary in bitcode files.
14621466
// The BitcodeSummaryVersion should be bumped whenever we introduce changes
@@ -1829,8 +1833,8 @@ class ModuleSummaryIndex {
18291833
for (auto &[GUID, TypeIdPair] : make_range(TidIter))
18301834
if (TypeIdPair.first == TypeId)
18311835
return TypeIdPair.second;
1832-
auto It = TypeIdMap.insert(
1833-
{GlobalValue::getGUID(TypeId), {std::string(TypeId), TypeIdSummary()}});
1836+
auto It = TypeIdMap.insert({GlobalValue::getGUID(TypeId),
1837+
{TypeIdSaver.save(TypeId), TypeIdSummary()}});
18341838
return It->second.second;
18351839
}
18361840

@@ -1859,7 +1863,7 @@ class ModuleSummaryIndex {
18591863
/// the ThinLTO backends.
18601864
TypeIdCompatibleVtableInfo &
18611865
getOrInsertTypeIdCompatibleVtableSummary(StringRef TypeId) {
1862-
return TypeIdCompatibleVtableMap[std::string(TypeId)];
1866+
return TypeIdCompatibleVtableMap[TypeIdSaver.save(TypeId)];
18631867
}
18641868

18651869
/// For the given \p TypeId, this returns the TypeIdCompatibleVtableMap

llvm/include/llvm/IR/ModuleSummaryIndexYAML.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,11 +313,11 @@ template <> struct CustomMappingTraits<TypeIdSummaryMapTy> {
313313
static void inputOne(IO &io, StringRef Key, TypeIdSummaryMapTy &V) {
314314
TypeIdSummary TId;
315315
io.mapRequired(Key.str().c_str(), TId);
316-
V.insert({GlobalValue::getGUID(Key), {std::string(Key), TId}});
316+
V.insert({GlobalValue::getGUID(Key), {Key, TId}});
317317
}
318318
static void output(IO &io, TypeIdSummaryMapTy &V) {
319319
for (auto &TidIter : V)
320-
io.mapRequired(TidIter.second.first.c_str(), TidIter.second.second);
320+
io.mapRequired(TidIter.second.first.str().c_str(), TidIter.second.second);
321321
}
322322
};
323323

@@ -327,7 +327,21 @@ template <> struct MappingTraits<ModuleSummaryIndex> {
327327
if (!io.outputting())
328328
CustomMappingTraits<GlobalValueSummaryMapTy>::fixAliaseeLinks(
329329
index.GlobalValueMap);
330-
io.mapOptional("TypeIdMap", index.TypeIdMap);
330+
331+
if (io.outputting()) {
332+
io.mapOptional("TypeIdMap", index.TypeIdMap);
333+
} else {
334+
TypeIdSummaryMapTy TypeIdMap;
335+
io.mapOptional("TypeIdMap", TypeIdMap);
336+
for (auto &[TypeGUID, TypeIdSummaryMap] : TypeIdMap) {
337+
// Save type id references in index and point TypeIdMap to use the
338+
// references owned by index.
339+
StringRef KeyRef = index.TypeIdSaver.save(TypeIdSummaryMap.first);
340+
index.TypeIdMap.insert(
341+
{TypeGUID, {KeyRef, std::move(TypeIdSummaryMap.second)}});
342+
}
343+
}
344+
331345
io.mapOptional("WithGlobalValueDeadStripping",
332346
index.WithGlobalValueDeadStripping);
333347

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4165,7 +4165,7 @@ static void writeWholeProgramDevirtResolution(
41654165

41664166
static void writeTypeIdSummaryRecord(SmallVector<uint64_t, 64> &NameVals,
41674167
StringTableBuilder &StrtabBuilder,
4168-
const std::string &Id,
4168+
StringRef Id,
41694169
const TypeIdSummary &Summary) {
41704170
NameVals.push_back(StrtabBuilder.add(Id));
41714171
NameVals.push_back(Id.size());
@@ -4184,7 +4184,7 @@ static void writeTypeIdSummaryRecord(SmallVector<uint64_t, 64> &NameVals,
41844184

41854185
static void writeTypeIdCompatibleVtableSummaryRecord(
41864186
SmallVector<uint64_t, 64> &NameVals, StringTableBuilder &StrtabBuilder,
4187-
const std::string &Id, const TypeIdCompatibleVtableInfo &Summary,
4187+
StringRef Id, const TypeIdCompatibleVtableInfo &Summary,
41884188
ValueEnumerator &VE) {
41894189
NameVals.push_back(StrtabBuilder.add(Id));
41904190
NameVals.push_back(Id.size());

0 commit comments

Comments
 (0)