[llvm] [LTO] Introduce a helper function collectImportStatistics (NFC) (PR #106179)
Kazu Hirata via llvm-commits
llvm-commits at lists.llvm.org
Tue Aug 27 08:18:55 PDT 2024
https://github.com/kazutakahirata updated https://github.com/llvm/llvm-project/pull/106179
>From a7d32d9806fca8c6685a2691d2e375299821d0b5 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Mon, 26 Aug 2024 18:44:42 -0700
Subject: [PATCH 1/3] [LTO] Introduce a helper function summarizeImports (NFC)
This patch introduces a helper function summarizeImports. The new
function computes a summary 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 space efficient
manner. That means that obtaining a summary 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.
---
llvm/lib/Transforms/IPO/FunctionImport.cpp | 68 ++++++++++++----------
1 file changed, 37 insertions(+), 31 deletions(-)
diff --git a/llvm/lib/Transforms/IPO/FunctionImport.cpp b/llvm/lib/Transforms/IPO/FunctionImport.cpp
index dd01d143b066b9..9553d0a8e5ee3d 100644
--- a/llvm/lib/Transforms/IPO/FunctionImport.cpp
+++ b/llvm/lib/Transforms/IPO/FunctionImport.cpp
@@ -1082,21 +1082,29 @@ numGlobalVarSummaries(const ModuleSummaryIndex &Index,
return NumGVS;
}
-// Given ImportMap, return the number of global variable summaries and record
-// the number of defined function summaries as output parameter.
-static unsigned
-numGlobalVarSummaries(const ModuleSummaryIndex &Index,
- const FunctionImporter::FunctionsToImportTy &ImportMap,
- unsigned &DefinedFS) {
+struct ImportSummary {
unsigned NumGVS = 0;
- DefinedFS = 0;
- for (auto &[GUID, Type] : ImportMap) {
- if (isGlobalVarSummary(Index, GUID))
- ++NumGVS;
- else if (Type == GlobalValueSummary::Definition)
- ++DefinedFS;
+ unsigned DefinedFS = 0;
+ unsigned Count = 0;
+};
+
+// Compute import summary for each source module in ImportList.
+static DenseMap<StringRef, ImportSummary>
+summarizeImports(const ModuleSummaryIndex &Index,
+ const FunctionImporter::ImportMapTy &ImportList) {
+ DenseMap<StringRef, ImportSummary> Histogram;
+
+ for (const auto &[FromModule, GUIDs] : ImportList.getImportMap()) {
+ for (const auto &[GUID, Type] : GUIDs) {
+ ImportSummary &Entry = Histogram[FromModule];
+ ++Entry.Count;
+ if (isGlobalVarSummary(Index, GUID))
+ ++Entry.NumGVS;
+ else if (Type == GlobalValueSummary::Definition)
+ ++Entry.DefinedFS;
+ }
}
- return NumGVS;
+ return Histogram;
}
#endif
@@ -1217,21 +1225,19 @@ void llvm::ComputeCrossModuleImport(
auto ModName = ModuleImports.first;
auto &Exports = ExportLists[ModName];
unsigned NumGVS = numGlobalVarSummaries(Index, Exports);
+ DenseMap<StringRef, ImportSummary> Histogram =
+ summarizeImports(Index, ModuleImports.second);
LLVM_DEBUG(dbgs() << "* Module " << ModName << " exports "
<< Exports.size() - NumGVS << " functions and " << NumGVS
- << " vars. Imports from "
- << ModuleImports.second.getImportMap().size()
+ << " vars. Imports from " << Histogram.size()
<< " modules.\n");
- for (const auto &Src : ModuleImports.second.getImportMap()) {
- auto SrcModName = Src.first;
- unsigned DefinedFS = 0;
- unsigned NumGVSPerMod =
- numGlobalVarSummaries(Index, Src.second, DefinedFS);
- LLVM_DEBUG(dbgs() << " - " << DefinedFS << " function definitions and "
- << Src.second.size() - NumGVSPerMod - DefinedFS
+ for (const auto &[SrcModName, Summary] : Histogram) {
+ LLVM_DEBUG(dbgs() << " - " << Summary.DefinedFS
+ << " function definitions and "
+ << Summary.Count - Summary.NumGVS - Summary.DefinedFS
<< " function declarations imported from " << SrcModName
<< "\n");
- LLVM_DEBUG(dbgs() << " - " << NumGVSPerMod
+ LLVM_DEBUG(dbgs() << " - " << Summary.NumGVS
<< " global vars imported from " << SrcModName << "\n");
}
}
@@ -1242,17 +1248,17 @@ void llvm::ComputeCrossModuleImport(
static void dumpImportListForModule(const ModuleSummaryIndex &Index,
StringRef ModulePath,
FunctionImporter::ImportMapTy &ImportList) {
+ DenseMap<StringRef, ImportSummary> Histogram =
+ summarizeImports(Index, ImportList);
LLVM_DEBUG(dbgs() << "* Module " << ModulePath << " imports from "
- << ImportList.getImportMap().size() << " modules.\n");
- for (const auto &Src : ImportList.getImportMap()) {
- auto SrcModName = Src.first;
- unsigned DefinedFS = 0;
- unsigned NumGVSPerMod = numGlobalVarSummaries(Index, Src.second, DefinedFS);
- LLVM_DEBUG(dbgs() << " - " << DefinedFS << " function definitions and "
- << Src.second.size() - DefinedFS - NumGVSPerMod
+ << Histogram.size() << " modules.\n");
+ for (const auto &[SrcModName, Summary] : Histogram) {
+ LLVM_DEBUG(dbgs() << " - " << Summary.DefinedFS
+ << " function definitions and "
+ << Summary.Count - Summary.DefinedFS - Summary.NumGVS
<< " function declarations imported from " << SrcModName
<< "\n");
- LLVM_DEBUG(dbgs() << " - " << NumGVSPerMod << " vars imported from "
+ LLVM_DEBUG(dbgs() << " - " << Summary.NumGVS << " vars imported from "
<< SrcModName << "\n");
}
}
>From 685a6f81592ee8b426baf43ce694c1d931c11f8d Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Tue, 27 Aug 2024 07:46:47 -0700
Subject: [PATCH 2/3] Rename ImportSummary to ImportStatistics.
Rename summarizeImports to collectImportStatistics.
Move a map lookup outside the inner loop.
---
llvm/lib/Transforms/IPO/FunctionImport.cpp | 20 ++++++++++----------
1 file changed, 10 insertions(+), 10 deletions(-)
diff --git a/llvm/lib/Transforms/IPO/FunctionImport.cpp b/llvm/lib/Transforms/IPO/FunctionImport.cpp
index 9553d0a8e5ee3d..53cb66537aa62a 100644
--- a/llvm/lib/Transforms/IPO/FunctionImport.cpp
+++ b/llvm/lib/Transforms/IPO/FunctionImport.cpp
@@ -1082,21 +1082,21 @@ numGlobalVarSummaries(const ModuleSummaryIndex &Index,
return NumGVS;
}
-struct ImportSummary {
+struct ImportStatistics {
unsigned NumGVS = 0;
unsigned DefinedFS = 0;
unsigned Count = 0;
};
// Compute import summary for each source module in ImportList.
-static DenseMap<StringRef, ImportSummary>
-summarizeImports(const ModuleSummaryIndex &Index,
- const FunctionImporter::ImportMapTy &ImportList) {
- DenseMap<StringRef, ImportSummary> Histogram;
+static DenseMap<StringRef, ImportStatistics>
+collectImportStatistics(const ModuleSummaryIndex &Index,
+ const FunctionImporter::ImportMapTy &ImportList) {
+ DenseMap<StringRef, ImportStatistics> Histogram;
for (const auto &[FromModule, GUIDs] : ImportList.getImportMap()) {
+ ImportStatistics &Entry = Histogram[FromModule];
for (const auto &[GUID, Type] : GUIDs) {
- ImportSummary &Entry = Histogram[FromModule];
++Entry.Count;
if (isGlobalVarSummary(Index, GUID))
++Entry.NumGVS;
@@ -1225,8 +1225,8 @@ void llvm::ComputeCrossModuleImport(
auto ModName = ModuleImports.first;
auto &Exports = ExportLists[ModName];
unsigned NumGVS = numGlobalVarSummaries(Index, Exports);
- DenseMap<StringRef, ImportSummary> Histogram =
- summarizeImports(Index, ModuleImports.second);
+ DenseMap<StringRef, ImportStatistics> Histogram =
+ collectImportStatistics(Index, ModuleImports.second);
LLVM_DEBUG(dbgs() << "* Module " << ModName << " exports "
<< Exports.size() - NumGVS << " functions and " << NumGVS
<< " vars. Imports from " << Histogram.size()
@@ -1248,8 +1248,8 @@ void llvm::ComputeCrossModuleImport(
static void dumpImportListForModule(const ModuleSummaryIndex &Index,
StringRef ModulePath,
FunctionImporter::ImportMapTy &ImportList) {
- DenseMap<StringRef, ImportSummary> Histogram =
- summarizeImports(Index, ImportList);
+ DenseMap<StringRef, ImportStatistics> Histogram =
+ collectImportStatistics(Index, ImportList);
LLVM_DEBUG(dbgs() << "* Module " << ModulePath << " imports from "
<< Histogram.size() << " modules.\n");
for (const auto &[SrcModName, Summary] : Histogram) {
>From 6215c18eccaf71f7eb7f9d0ada8a3751a2e22135 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Tue, 27 Aug 2024 08:18:02 -0700
Subject: [PATCH 3/3] Rename Summary to Stats.
---
llvm/lib/Transforms/IPO/FunctionImport.cpp | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)
diff --git a/llvm/lib/Transforms/IPO/FunctionImport.cpp b/llvm/lib/Transforms/IPO/FunctionImport.cpp
index 53cb66537aa62a..34274ab0e75964 100644
--- a/llvm/lib/Transforms/IPO/FunctionImport.cpp
+++ b/llvm/lib/Transforms/IPO/FunctionImport.cpp
@@ -1231,13 +1231,13 @@ void llvm::ComputeCrossModuleImport(
<< Exports.size() - NumGVS << " functions and " << NumGVS
<< " vars. Imports from " << Histogram.size()
<< " modules.\n");
- for (const auto &[SrcModName, Summary] : Histogram) {
- LLVM_DEBUG(dbgs() << " - " << Summary.DefinedFS
+ for (const auto &[SrcModName, Stats] : Histogram) {
+ LLVM_DEBUG(dbgs() << " - " << Stats.DefinedFS
<< " function definitions and "
- << Summary.Count - Summary.NumGVS - Summary.DefinedFS
+ << Stats.Count - Stats.NumGVS - Stats.DefinedFS
<< " function declarations imported from " << SrcModName
<< "\n");
- LLVM_DEBUG(dbgs() << " - " << Summary.NumGVS
+ LLVM_DEBUG(dbgs() << " - " << Stats.NumGVS
<< " global vars imported from " << SrcModName << "\n");
}
}
@@ -1252,13 +1252,13 @@ static void dumpImportListForModule(const ModuleSummaryIndex &Index,
collectImportStatistics(Index, ImportList);
LLVM_DEBUG(dbgs() << "* Module " << ModulePath << " imports from "
<< Histogram.size() << " modules.\n");
- for (const auto &[SrcModName, Summary] : Histogram) {
- LLVM_DEBUG(dbgs() << " - " << Summary.DefinedFS
+ for (const auto &[SrcModName, Stats] : Histogram) {
+ LLVM_DEBUG(dbgs() << " - " << Stats.DefinedFS
<< " function definitions and "
- << Summary.Count - Summary.DefinedFS - Summary.NumGVS
+ << Stats.Count - Stats.DefinedFS - Stats.NumGVS
<< " function declarations imported from " << SrcModName
<< "\n");
- LLVM_DEBUG(dbgs() << " - " << Summary.NumGVS << " vars imported from "
+ LLVM_DEBUG(dbgs() << " - " << Stats.NumGVS << " vars imported from "
<< SrcModName << "\n");
}
}
More information about the llvm-commits
mailing list