[llvm] 29bb523 - [LTO] Introduce a helper lambda in gatherImportedSummariesForModule (NFC) (#106251)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Aug 27 12:43:10 PDT 2024
Author: Kazu Hirata
Date: 2024-08-27T12:43:07-07:00
New Revision: 29bb523b7c3502b47737ced2a8b20678809ec9de
URL: https://github.com/llvm/llvm-project/commit/29bb523b7c3502b47737ced2a8b20678809ec9de
DIFF: https://github.com/llvm/llvm-project/commit/29bb523b7c3502b47737ced2a8b20678809ec9de.diff
LOG: [LTO] Introduce a helper lambda in gatherImportedSummariesForModule (NFC) (#106251)
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.
Added:
Modified:
llvm/include/llvm/IR/ModuleSummaryIndex.h
llvm/lib/Transforms/IPO/FunctionImport.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/IR/ModuleSummaryIndex.h b/llvm/include/llvm/IR/ModuleSummaryIndex.h
index f94c2dfd0e67bd..6d39150a03bfd5 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);
More information about the llvm-commits
mailing list