[llvm] [LTO] Don't make unnecessary copies of ImportIDTable (PR #106998)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 2 09:47:02 PDT 2024


https://github.com/kazutakahirata updated https://github.com/llvm/llvm-project/pull/106998

>From da0e3e743d8c115214a200804a836ded68b6aa2b Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Mon, 2 Sep 2024 01:06:43 -0700
Subject: [PATCH 1/2] [LTO] Don't make unnecessary copies of ImportIDTable

Without this patch, {ImportMapTy,SortedImportList}::{begin,end} make
unnecessary copies of ImportIDTable via:

  map_iterator(Imports.begin(), IDs);

The second parameter, IDs, is passed by value, so we make a copy of
MapVector inside ImportIDTable every time we call begin and end.
These begin and end show up as time-consuming functions in the
performance profile.

This patch fixes the problem by passing IDs by reference with
std::cref.

While we are at it, this patch deletes the copy constructor and
assignment operator.  I cannot think of any legitimate need reason to
make a copy of the deduplication table.
---
 .../llvm/Transforms/IPO/FunctionImport.h      | 21 +++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/llvm/include/llvm/Transforms/IPO/FunctionImport.h b/llvm/include/llvm/Transforms/IPO/FunctionImport.h
index b5b328d96b2737..000bd9273d9116 100644
--- a/llvm/include/llvm/Transforms/IPO/FunctionImport.h
+++ b/llvm/include/llvm/Transforms/IPO/FunctionImport.h
@@ -113,6 +113,13 @@ class FunctionImporter {
   public:
     using ImportIDTy = uint32_t;
 
+    ImportIDTable() = default;
+
+    // Three is something wrong with the application logic if we need to make a
+    // copy of this and potentially make a fork.
+    ImportIDTable(const ImportIDTable &) = delete;
+    ImportIDTable &operator=(const ImportIDTable &) = delete;
+
     // Create a pair of import IDs [Def, Decl] for a given pair of FromModule
     // and GUID.
     std::pair<ImportIDTy, ImportIDTy> createImportIDs(StringRef FromModule,
@@ -218,9 +225,10 @@ class FunctionImporter {
     getImportType(StringRef FromModule, GlobalValue::GUID GUID) const;
 
     // Iterate over the import list.  The caller gets tuples of FromModule,
-    // GUID, and ImportKind instead of import IDs.
-    auto begin() const { return map_iterator(Imports.begin(), IDs); }
-    auto end() const { return map_iterator(Imports.end(), IDs); }
+    // GUID, and ImportKind instead of import IDs.  std::cref below prevents
+    // map_iterator from deep-copying IDs.
+    auto begin() const { return map_iterator(Imports.begin(), std::cref(IDs)); }
+    auto end() const { return map_iterator(Imports.end(), std::cref(IDs)); }
 
     friend class SortedImportList;
 
@@ -251,9 +259,10 @@ class FunctionImporter {
     }
 
     // Iterate over the import list.  The caller gets tuples of FromModule,
-    // GUID, and ImportKind instead of import IDs.
-    auto begin() const { return map_iterator(Imports.begin(), IDs); }
-    auto end() const { return map_iterator(Imports.end(), IDs); }
+    // GUID, and ImportKind instead of import IDs.  std::cref below prevents
+    // map_iterator from deep-copying IDs.
+    auto begin() const { return map_iterator(Imports.begin(), std::cref(IDs)); }
+    auto end() const { return map_iterator(Imports.end(), std::cref(IDs)); }
 
   private:
     const ImportIDTable &IDs;

>From e42dbbac9de094b127369a47f2edaf8bf0af76fa Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Mon, 2 Sep 2024 09:46:35 -0700
Subject: [PATCH 2/2] Revise a comment.

---
 llvm/include/llvm/Transforms/IPO/FunctionImport.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/include/llvm/Transforms/IPO/FunctionImport.h b/llvm/include/llvm/Transforms/IPO/FunctionImport.h
index 000bd9273d9116..0a6cc5951b706a 100644
--- a/llvm/include/llvm/Transforms/IPO/FunctionImport.h
+++ b/llvm/include/llvm/Transforms/IPO/FunctionImport.h
@@ -115,8 +115,8 @@ class FunctionImporter {
 
     ImportIDTable() = default;
 
-    // Three is something wrong with the application logic if we need to make a
-    // copy of this and potentially make a fork.
+    // Something is wrong with the application logic if we need to make a copy
+    // of this and potentially make a fork.
     ImportIDTable(const ImportIDTable &) = delete;
     ImportIDTable &operator=(const ImportIDTable &) = delete;
 



More information about the llvm-commits mailing list