Skip to content

Commit 29bb523

Browse files
[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.
1 parent 2bdc0da commit 29bb523

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

llvm/include/llvm/IR/ModuleSummaryIndex.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1293,7 +1293,8 @@ using GVSummaryMapTy = DenseMap<GlobalValue::GUID, GlobalValueSummary *>;
12931293

12941294
/// Map of a module name to the GUIDs and summaries we will import from that
12951295
/// module.
1296-
using ModuleToSummariesForIndexTy = std::map<std::string, GVSummaryMapTy>;
1296+
using ModuleToSummariesForIndexTy =
1297+
std::map<std::string, GVSummaryMapTy, std::less<>>;
12971298

12981299
/// A set of global value summary pointers.
12991300
using GVSummaryPtrSet = std::unordered_set<GlobalValueSummary *>;

llvm/lib/Transforms/IPO/FunctionImport.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1503,9 +1503,25 @@ void llvm::gatherImportedSummariesForModule(
15031503
// Include all summaries from the importing module.
15041504
ModuleToSummariesForIndex[std::string(ModulePath)] =
15051505
ModuleToDefinedGVSummaries.lookup(ModulePath);
1506+
1507+
// Forward port the heterogeneous std::map::operator[]() from C++26, which
1508+
// lets us look up the map without allocating an instance of std::string when
1509+
// the key-value pair exists in the map.
1510+
// TODO: Remove this in favor of the heterogenous std::map::operator[]() from
1511+
// C++26 when it becomes available for our codebase.
1512+
auto LookupOrCreate = [](ModuleToSummariesForIndexTy &Map,
1513+
StringRef Key) -> GVSummaryMapTy & {
1514+
auto It = Map.find(Key);
1515+
if (It == Map.end())
1516+
std::tie(It, std::ignore) =
1517+
Map.try_emplace(std::string(Key), GVSummaryMapTy());
1518+
return It->second;
1519+
};
1520+
15061521
// Include summaries for imports.
15071522
for (const auto &ILI : ImportList.getImportMap()) {
1508-
auto &SummariesForIndex = ModuleToSummariesForIndex[std::string(ILI.first)];
1523+
auto &SummariesForIndex =
1524+
LookupOrCreate(ModuleToSummariesForIndex, ILI.first);
15091525

15101526
const auto &DefinedGVSummaries =
15111527
ModuleToDefinedGVSummaries.lookup(ILI.first);

0 commit comments

Comments
 (0)