From 21eddfac3d75879b3e0b09c5bc848526dcab6ab0 Mon Sep 17 00:00:00 2001 From: Kazu Hirata Date: Wed, 28 Aug 2024 12:50:44 -0700 Subject: [PATCH] [LTO] Turn ImportListsTy into a proper class (NFC) (#106427) This patch turns ImportListsTy into a class that wraps DenseMap. 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 c06d96bbe62e22a..78932c12e76ff82 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; + 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::const_iterator; + const_iterator begin() const { return ListsImpl.begin(); } + const_iterator end() const { return ListsImpl.end(); } + + private: + DenseMap 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