Skip to content

Commit 0359b9a

Browse files
[LTO] Introduce a helper function collectImportStatistics (NFC) (#106179)
This patch introduces a helper function collectImportStatistics. The new function computes statistics of imports for ComputeCrossModuleImport and dumpImportListForModule with no functional change. The background is as follows. I'm planning to reduce the memory footprint of ThinLTO indexing by changing ImportMapTy, the data structure used for an import list. The new list will be a hash set of tuples (SourceModule, GUID, ImportType) represented in a space efficient manner. That means that obtaining statistics like the number of definitions per source module requires us to go through the entire import list (for a given destination module). Introducing a helper function now makes the callers more independent of the underlying data structures used in ImportMapT.
1 parent 6e44cb3 commit 0359b9a

File tree

1 file changed

+37
-31
lines changed

1 file changed

+37
-31
lines changed

llvm/lib/Transforms/IPO/FunctionImport.cpp

+37-31
Original file line numberDiff line numberDiff line change
@@ -1082,21 +1082,29 @@ numGlobalVarSummaries(const ModuleSummaryIndex &Index,
10821082
return NumGVS;
10831083
}
10841084

1085-
// Given ImportMap, return the number of global variable summaries and record
1086-
// the number of defined function summaries as output parameter.
1087-
static unsigned
1088-
numGlobalVarSummaries(const ModuleSummaryIndex &Index,
1089-
const FunctionImporter::FunctionsToImportTy &ImportMap,
1090-
unsigned &DefinedFS) {
1085+
struct ImportStatistics {
10911086
unsigned NumGVS = 0;
1092-
DefinedFS = 0;
1093-
for (auto &[GUID, Type] : ImportMap) {
1094-
if (isGlobalVarSummary(Index, GUID))
1095-
++NumGVS;
1096-
else if (Type == GlobalValueSummary::Definition)
1097-
++DefinedFS;
1087+
unsigned DefinedFS = 0;
1088+
unsigned Count = 0;
1089+
};
1090+
1091+
// Compute import statistics for each source module in ImportList.
1092+
static DenseMap<StringRef, ImportStatistics>
1093+
collectImportStatistics(const ModuleSummaryIndex &Index,
1094+
const FunctionImporter::ImportMapTy &ImportList) {
1095+
DenseMap<StringRef, ImportStatistics> Histogram;
1096+
1097+
for (const auto &[FromModule, GUIDs] : ImportList.getImportMap()) {
1098+
ImportStatistics &Entry = Histogram[FromModule];
1099+
for (const auto &[GUID, Type] : GUIDs) {
1100+
++Entry.Count;
1101+
if (isGlobalVarSummary(Index, GUID))
1102+
++Entry.NumGVS;
1103+
else if (Type == GlobalValueSummary::Definition)
1104+
++Entry.DefinedFS;
1105+
}
10981106
}
1099-
return NumGVS;
1107+
return Histogram;
11001108
}
11011109
#endif
11021110

@@ -1217,21 +1225,19 @@ void llvm::ComputeCrossModuleImport(
12171225
auto ModName = ModuleImports.first;
12181226
auto &Exports = ExportLists[ModName];
12191227
unsigned NumGVS = numGlobalVarSummaries(Index, Exports);
1228+
DenseMap<StringRef, ImportStatistics> Histogram =
1229+
collectImportStatistics(Index, ModuleImports.second);
12201230
LLVM_DEBUG(dbgs() << "* Module " << ModName << " exports "
12211231
<< Exports.size() - NumGVS << " functions and " << NumGVS
1222-
<< " vars. Imports from "
1223-
<< ModuleImports.second.getImportMap().size()
1232+
<< " vars. Imports from " << Histogram.size()
12241233
<< " modules.\n");
1225-
for (const auto &Src : ModuleImports.second.getImportMap()) {
1226-
auto SrcModName = Src.first;
1227-
unsigned DefinedFS = 0;
1228-
unsigned NumGVSPerMod =
1229-
numGlobalVarSummaries(Index, Src.second, DefinedFS);
1230-
LLVM_DEBUG(dbgs() << " - " << DefinedFS << " function definitions and "
1231-
<< Src.second.size() - NumGVSPerMod - DefinedFS
1234+
for (const auto &[SrcModName, Stats] : Histogram) {
1235+
LLVM_DEBUG(dbgs() << " - " << Stats.DefinedFS
1236+
<< " function definitions and "
1237+
<< Stats.Count - Stats.NumGVS - Stats.DefinedFS
12321238
<< " function declarations imported from " << SrcModName
12331239
<< "\n");
1234-
LLVM_DEBUG(dbgs() << " - " << NumGVSPerMod
1240+
LLVM_DEBUG(dbgs() << " - " << Stats.NumGVS
12351241
<< " global vars imported from " << SrcModName << "\n");
12361242
}
12371243
}
@@ -1242,17 +1248,17 @@ void llvm::ComputeCrossModuleImport(
12421248
static void dumpImportListForModule(const ModuleSummaryIndex &Index,
12431249
StringRef ModulePath,
12441250
FunctionImporter::ImportMapTy &ImportList) {
1251+
DenseMap<StringRef, ImportStatistics> Histogram =
1252+
collectImportStatistics(Index, ImportList);
12451253
LLVM_DEBUG(dbgs() << "* Module " << ModulePath << " imports from "
1246-
<< ImportList.getImportMap().size() << " modules.\n");
1247-
for (const auto &Src : ImportList.getImportMap()) {
1248-
auto SrcModName = Src.first;
1249-
unsigned DefinedFS = 0;
1250-
unsigned NumGVSPerMod = numGlobalVarSummaries(Index, Src.second, DefinedFS);
1251-
LLVM_DEBUG(dbgs() << " - " << DefinedFS << " function definitions and "
1252-
<< Src.second.size() - DefinedFS - NumGVSPerMod
1254+
<< Histogram.size() << " modules.\n");
1255+
for (const auto &[SrcModName, Stats] : Histogram) {
1256+
LLVM_DEBUG(dbgs() << " - " << Stats.DefinedFS
1257+
<< " function definitions and "
1258+
<< Stats.Count - Stats.DefinedFS - Stats.NumGVS
12531259
<< " function declarations imported from " << SrcModName
12541260
<< "\n");
1255-
LLVM_DEBUG(dbgs() << " - " << NumGVSPerMod << " vars imported from "
1261+
LLVM_DEBUG(dbgs() << " - " << Stats.NumGVS << " vars imported from "
12561262
<< SrcModName << "\n");
12571263
}
12581264
}

0 commit comments

Comments
 (0)