[llvm] [LTO] Turn ImportListsTy into a proper class (NFC) (PR #106427)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 28 10:56:05 PDT 2024


https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/106427

This patch turns ImportListsTy into a class that wraps
DenseMap<StringRef, ImportMapTy>.

Here is the background.  I'm planning to reduce the memory footprint
of ThinLTO indexing.  Specifically, ImportMapTy, the list of imports
for a given destination module, will be a hash set of integer IDs
indexing into a deduplication table of pairs (SourceModule, GUID),
which is a lot like string interning.  I'm planning to put this
deduplication table as part of ImportListsTy and have each instance of
ImportMapTy hold a reference to the deduplication table.

Another reason to wrap the DenseMap is that I need to intercept
operator[]() so that I can construct an instance of ImportMapTy with a
reference to the deduplication table.  Note that the default
implementation of operator[]() would default-construct ImportMapTy,
which I am going to disable.


>From ea241f3347a1e0b32a4160f53e2f189897b6ba59 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Wed, 28 Aug 2024 10:05:11 -0700
Subject: [PATCH] [LTO] Turn ImportListsTy into a proper class (NFC)

This patch turns ImportListsTy into a class that wraps
DenseMap<StringRef, ImportMapTy>.

Here is the background.  I'm planning to reduce the memory footprint
of ThinLTO indexing.  Specifically, ImportMapTy, the list of imports
for a given destination module, will be a hash set of integer IDs
indexing into a deduplication table of pairs (SourceModule, GUID),
which is a lot like string interning.  I'm planning to put this
deduplication table as part of ImportListsTy and have each instance of
ImportMapTy hold a reference to the deduplication table.

Another reason to wrap the DenseMap is that I need to intercept
operator[]() so that I can construct an instance of ImportMapTy with a
reference to the deduplication table.  Note that the default
implementation of operator[]() would default-construct ImportMapTy,
which I am going to disable.
---
 .../llvm/Transforms/IPO/FunctionImport.h      | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/llvm/include/llvm/Transforms/IPO/FunctionImport.h b/llvm/include/llvm/Transforms/IPO/FunctionImport.h
index c06d96bbe62e22..78932c12e76ff8 100644
--- a/llvm/include/llvm/Transforms/IPO/FunctionImport.h
+++ b/llvm/include/llvm/Transforms/IPO/FunctionImport.h
@@ -150,7 +150,24 @@ class FunctionImporter {
   };
 
   // A map from destination modules to lists of imports.
-  using ImportListsTy = DenseMap<StringRef, ImportMapTy>;
+  class ImportListsTy {
+  public:
+    ImportListsTy() = default;
+    ImportListsTy(size_t Size) : ListsImpl(Size) {}
+
+    ImportMapTy &operator[](StringRef DestMod) {
+      return ListsImpl.try_emplace(DestMod).first->second;
+    }
+
+    size_t size() const { return ListsImpl.size(); }
+
+    using const_iterator = DenseMap<StringRef, ImportMapTy>::const_iterator;
+    const_iterator begin() const { return ListsImpl.begin(); }
+    const_iterator end() const { return ListsImpl.end(); }
+
+  private:
+    DenseMap<StringRef, ImportMapTy> ListsImpl;
+  };
 
   /// The set contains an entry for every global value that the module exports.
   /// Depending on the user context, this container is allowed to contain



More information about the llvm-commits mailing list