-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[LTO] Introduce a helper lambda in gatherImportedSummariesForModule (NFC) #106251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[LTO] Introduce a helper lambda in gatherImportedSummariesForModule (NFC) #106251
Conversation
…NFC) This patch forward ports the heterogeneous std::map::operator[]() from C++26 so that we can look up the map without allocating an instance of std::string when the key-value pair exists in the map. 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 as we iterate over the hash set, we encounter SourceModule as many times as GUID. We don't want to create a temporary instance of std::string every time we look up ModuleToSummariesForIndex like: auto &SummariesForIndex = ModuleToSummariesForIndex[std::string(ILI.first)]; This patch removes the need to create the temporaries by enabling the hetegeneous lookup with std::set<K, V, std::less<>> and forward porting std::map::operator[]() from C++26.
@llvm/pr-subscribers-llvm-ir @llvm/pr-subscribers-llvm-transforms Author: Kazu Hirata (kazutakahirata) ChangesThis patch forward ports the heterogeneous std::map::operator from The background is as follows. I'm planning to reduce the memory auto &SummariesForIndex = ModuleToSummariesForIndex[std::string(ILI.first)]; This patch removes the need to create the temporaries by enabling the Full diff: https://github.com/llvm/llvm-project/pull/106251.diff 2 Files Affected:
diff --git a/llvm/include/llvm/IR/ModuleSummaryIndex.h b/llvm/include/llvm/IR/ModuleSummaryIndex.h
index b16596e454bdcc..af73b95c0f71db 100644
--- a/llvm/include/llvm/IR/ModuleSummaryIndex.h
+++ b/llvm/include/llvm/IR/ModuleSummaryIndex.h
@@ -1293,7 +1293,8 @@ using GVSummaryMapTy = DenseMap<GlobalValue::GUID, GlobalValueSummary *>;
/// Map of a module name to the GUIDs and summaries we will import from that
/// module.
-using ModuleToSummariesForIndexTy = std::map<std::string, GVSummaryMapTy>;
+using ModuleToSummariesForIndexTy =
+ std::map<std::string, GVSummaryMapTy, std::less<>>;
/// A set of global value summary pointers.
using GVSummaryPtrSet = std::unordered_set<GlobalValueSummary *>;
diff --git a/llvm/lib/Transforms/IPO/FunctionImport.cpp b/llvm/lib/Transforms/IPO/FunctionImport.cpp
index a80e6d3f411dde..512f771a873aa1 100644
--- a/llvm/lib/Transforms/IPO/FunctionImport.cpp
+++ b/llvm/lib/Transforms/IPO/FunctionImport.cpp
@@ -1503,9 +1503,25 @@ void llvm::gatherImportedSummariesForModule(
// Include all summaries from the importing module.
ModuleToSummariesForIndex[std::string(ModulePath)] =
ModuleToDefinedGVSummaries.lookup(ModulePath);
+
+ // Forward port the heterogeneous std::map::operator[]() from C++26, which
+ // lets us look up the map without allocating an instance of std::string when
+ // the key-value pair exists in the map.
+ // TODO: Remove this in favor of the heterogenous std::map::operator[]() from
+ // C++26 when it becomes available for our codebase.
+ auto LookupOrCreate = [](ModuleToSummariesForIndexTy &Map,
+ StringRef Key) -> GVSummaryMapTy & {
+ auto It = Map.find(Key);
+ if (It == Map.end())
+ std::tie(It, std::ignore) =
+ Map.try_emplace(std::string(Key), GVSummaryMapTy());
+ return It->second;
+ };
+
// Include summaries for imports.
for (const auto &ILI : ImportList.getImportMap()) {
- auto &SummariesForIndex = ModuleToSummariesForIndex[std::string(ILI.first)];
+ auto &SummariesForIndex =
+ LookupOrCreate(ModuleToSummariesForIndex, ILI.first);
const auto &DefinedGVSummaries =
ModuleToDefinedGVSummaries.lookup(ILI.first);
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm with a question below
This patch forward ports the heterogeneous std::map::operator from
C++26 so that we can look up the map without allocating an instance of
std::string when the key-value pair exists in the map.
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 as we iterate over the hash set, we
encounter SourceModule as many times as GUID. We don't want to create
a temporary instance of std::string every time we look up
ModuleToSummariesForIndex like:
auto &SummariesForIndex = ModuleToSummariesForIndex[std::string(ILI.first)];
This patch removes the need to create the temporaries by enabling the
hetegeneous lookup with std::set<K, V, std::less<>> and forward
porting std::map::operator from C++26.