[llvm] [GenericCycle] Add a Cache for getExitBlocks in GenericCycle (PR #112290)

via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 22 15:04:19 PDT 2024


https://github.com/Chengjunp updated https://github.com/llvm/llvm-project/pull/112290

>From 08ce9cda6c231dd0ca80985bfc2ea92442fd6e25 Mon Sep 17 00:00:00 2001
From: chengjunp <chengjunp at nvidia.com>
Date: Mon, 14 Oct 2024 23:40:38 +0000
Subject: [PATCH 1/5] Add cache in GenericCycleInfo

---
 llvm/include/llvm/ADT/GenericCycleImpl.h | 6 ++++++
 llvm/include/llvm/ADT/GenericCycleInfo.h | 7 ++++++-
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/llvm/include/llvm/ADT/GenericCycleImpl.h b/llvm/include/llvm/ADT/GenericCycleImpl.h
index 3d2c5f42883558..ee7296043dce73 100644
--- a/llvm/include/llvm/ADT/GenericCycleImpl.h
+++ b/llvm/include/llvm/ADT/GenericCycleImpl.h
@@ -47,6 +47,11 @@ bool GenericCycle<ContextT>::contains(const GenericCycle *C) const {
 template <typename ContextT>
 void GenericCycle<ContextT>::getExitBlocks(
     SmallVectorImpl<BlockT *> &TmpStorage) const {
+  if (!ExitBlocksCache->empty()) {
+    TmpStorage = *ExitBlocksCache;
+    return;
+  }
+
   TmpStorage.clear();
 
   size_t NumExitBlocks = 0;
@@ -65,6 +70,7 @@ void GenericCycle<ContextT>::getExitBlocks(
 
     TmpStorage.resize(NumExitBlocks);
   }
+  ExitBlocksCache->append(TmpStorage.begin(), TmpStorage.end());
 }
 
 template <typename ContextT>
diff --git a/llvm/include/llvm/ADT/GenericCycleInfo.h b/llvm/include/llvm/ADT/GenericCycleInfo.h
index 8c2fa0490e638a..98d935975cc373 100644
--- a/llvm/include/llvm/ADT/GenericCycleInfo.h
+++ b/llvm/include/llvm/ADT/GenericCycleInfo.h
@@ -74,12 +74,16 @@ template <typename ContextT> class GenericCycle {
   ///       always have the same depth.
   unsigned Depth = 0;
 
+  /// Cache for the results of GetExitBlocks
+  std::unique_ptr<SmallVector<BlockT *, 4>> ExitBlocksCache;
+
   void clear() {
     Entries.clear();
     Children.clear();
     Blocks.clear();
     Depth = 0;
     ParentCycle = nullptr;
+    ExitBlocksCache->clear();
   }
 
   void appendEntry(BlockT *Block) { Entries.push_back(Block); }
@@ -91,7 +95,8 @@ template <typename ContextT> class GenericCycle {
   GenericCycle &operator=(GenericCycle &&Rhs) = delete;
 
 public:
-  GenericCycle() = default;
+  GenericCycle()
+      : ExitBlocksCache(std::make_unique<SmallVector<BlockT *, 4>>()){};
 
   /// \brief Whether the cycle is a natural loop.
   bool isReducible() const { return Entries.size() == 1; }

>From dd45e4bf139a109579461740aa0edc124cad4ed5 Mon Sep 17 00:00:00 2001
From: chengjunp <chengjunp at nvidia.com>
Date: Tue, 15 Oct 2024 01:01:55 +0000
Subject: [PATCH 2/5] Format

---
 llvm/include/llvm/ADT/GenericCycleInfo.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/include/llvm/ADT/GenericCycleInfo.h b/llvm/include/llvm/ADT/GenericCycleInfo.h
index 98d935975cc373..f3f1a0c45aaa8c 100644
--- a/llvm/include/llvm/ADT/GenericCycleInfo.h
+++ b/llvm/include/llvm/ADT/GenericCycleInfo.h
@@ -96,7 +96,7 @@ template <typename ContextT> class GenericCycle {
 
 public:
   GenericCycle()
-      : ExitBlocksCache(std::make_unique<SmallVector<BlockT *, 4>>()){};
+      : ExitBlocksCache(std::make_unique<SmallVector<BlockT *, 4>>()) {};
 
   /// \brief Whether the cycle is a natural loop.
   bool isReducible() const { return Entries.size() == 1; }

>From 80792adace3210618aad63e46ac4e557b8392dd5 Mon Sep 17 00:00:00 2001
From: chengjunp <chengjunp at nvidia.com>
Date: Wed, 16 Oct 2024 20:01:08 +0000
Subject: [PATCH 3/5] Update cache and clear the cache in non-const func

---
 llvm/include/llvm/ADT/GenericCycleImpl.h |  9 ++++---
 llvm/include/llvm/ADT/GenericCycleInfo.h | 33 +++++++++++++++++++-----
 2 files changed, 32 insertions(+), 10 deletions(-)

diff --git a/llvm/include/llvm/ADT/GenericCycleImpl.h b/llvm/include/llvm/ADT/GenericCycleImpl.h
index ee7296043dce73..41ba8bf8fde14b 100644
--- a/llvm/include/llvm/ADT/GenericCycleImpl.h
+++ b/llvm/include/llvm/ADT/GenericCycleImpl.h
@@ -47,8 +47,8 @@ bool GenericCycle<ContextT>::contains(const GenericCycle *C) const {
 template <typename ContextT>
 void GenericCycle<ContextT>::getExitBlocks(
     SmallVectorImpl<BlockT *> &TmpStorage) const {
-  if (!ExitBlocksCache->empty()) {
-    TmpStorage = *ExitBlocksCache;
+  if (!ExitBlocksCache.empty()) {
+    TmpStorage = ExitBlocksCache;
     return;
   }
 
@@ -70,7 +70,7 @@ void GenericCycle<ContextT>::getExitBlocks(
 
     TmpStorage.resize(NumExitBlocks);
   }
-  ExitBlocksCache->append(TmpStorage.begin(), TmpStorage.end());
+  ExitBlocksCache.append(TmpStorage.begin(), TmpStorage.end());
 }
 
 template <typename ContextT>
@@ -304,6 +304,8 @@ void GenericCycleInfo<ContextT>::moveTopLevelCycleToNewParent(CycleT *NewParent,
   for (auto &It : BlockMapTopLevel)
     if (It.second == Child)
       It.second = NewParent;
+  NewParent->clearCache();
+  Child->clearCache();
 }
 
 template <typename ContextT>
@@ -322,6 +324,7 @@ void GenericCycleInfo<ContextT>::addBlockToCycle(BlockT *Block, CycleT *Cycle) {
   }
 
   BlockMapTopLevel.try_emplace(Block, Cycle);
+  Cycle->clearCache();
 }
 
 /// \brief Main function of the cycle info computations.
diff --git a/llvm/include/llvm/ADT/GenericCycleInfo.h b/llvm/include/llvm/ADT/GenericCycleInfo.h
index f3f1a0c45aaa8c..bb2a7557548a0b 100644
--- a/llvm/include/llvm/ADT/GenericCycleInfo.h
+++ b/llvm/include/llvm/ADT/GenericCycleInfo.h
@@ -75,7 +75,7 @@ template <typename ContextT> class GenericCycle {
   unsigned Depth = 0;
 
   /// Cache for the results of GetExitBlocks
-  std::unique_ptr<SmallVector<BlockT *, 4>> ExitBlocksCache;
+  mutable SmallVector<BlockT *, 4> ExitBlocksCache;
 
   void clear() {
     Entries.clear();
@@ -83,11 +83,18 @@ template <typename ContextT> class GenericCycle {
     Blocks.clear();
     Depth = 0;
     ParentCycle = nullptr;
-    ExitBlocksCache->clear();
+    clearCache();
   }
 
-  void appendEntry(BlockT *Block) { Entries.push_back(Block); }
-  void appendBlock(BlockT *Block) { Blocks.insert(Block); }
+  void appendEntry(BlockT *Block) {
+    Entries.push_back(Block);
+    clearCache();
+  }
+
+  void appendBlock(BlockT *Block) {
+    Blocks.insert(Block);
+    clearCache();
+  }
 
   GenericCycle(const GenericCycle &) = delete;
   GenericCycle &operator=(const GenericCycle &) = delete;
@@ -95,8 +102,7 @@ template <typename ContextT> class GenericCycle {
   GenericCycle &operator=(GenericCycle &&Rhs) = delete;
 
 public:
-  GenericCycle()
-      : ExitBlocksCache(std::make_unique<SmallVector<BlockT *, 4>>()) {};
+  GenericCycle() = default;
 
   /// \brief Whether the cycle is a natural loop.
   bool isReducible() const { return Entries.size() == 1; }
@@ -107,6 +113,13 @@ template <typename ContextT> class GenericCycle {
     return Entries;
   }
 
+  /// Clear the cache of the cycle.
+  /// This should be run in all non-const function in GenericCycle
+  /// and GenericCycleInfo.
+  void clearCache() const {
+    ExitBlocksCache.clear();
+  }
+
   /// \brief Return whether \p Block is an entry block of the cycle.
   bool isEntry(const BlockT *Block) const {
     return is_contained(Entries, Block);
@@ -117,6 +130,7 @@ template <typename ContextT> class GenericCycle {
     assert(contains(Block));
     Entries.clear();
     Entries.push_back(Block);
+    clearCache();
   }
 
   /// \brief Return whether \p Block is contained in the cycle.
@@ -129,7 +143,12 @@ template <typename ContextT> class GenericCycle {
   bool contains(const GenericCycle *C) const;
 
   const GenericCycle *getParentCycle() const { return ParentCycle; }
-  GenericCycle *getParentCycle() { return ParentCycle; }
+
+  GenericCycle *getParentCycle() {
+    clearCache();
+    return ParentCycle;
+  }
+
   unsigned getDepth() const { return Depth; }
 
   /// Return all of the successor blocks of this cycle.

>From 75435a8ceff038a5f8af82b68b68df8f64c202d8 Mon Sep 17 00:00:00 2001
From: chengjunp <chengjunp at nvidia.com>
Date: Wed, 16 Oct 2024 20:07:29 +0000
Subject: [PATCH 4/5] Format

---
 llvm/include/llvm/ADT/GenericCycleInfo.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/llvm/include/llvm/ADT/GenericCycleInfo.h b/llvm/include/llvm/ADT/GenericCycleInfo.h
index bb2a7557548a0b..14877bb6051091 100644
--- a/llvm/include/llvm/ADT/GenericCycleInfo.h
+++ b/llvm/include/llvm/ADT/GenericCycleInfo.h
@@ -116,9 +116,7 @@ template <typename ContextT> class GenericCycle {
   /// Clear the cache of the cycle.
   /// This should be run in all non-const function in GenericCycle
   /// and GenericCycleInfo.
-  void clearCache() const {
-    ExitBlocksCache.clear();
-  }
+  void clearCache() const { ExitBlocksCache.clear(); }
 
   /// \brief Return whether \p Block is an entry block of the cycle.
   bool isEntry(const BlockT *Block) const {

>From b56225b1ad1548a25778d49bca37cfe4180adb9a Mon Sep 17 00:00:00 2001
From: chengjunp <chengjunp at nvidia.com>
Date: Tue, 22 Oct 2024 22:06:27 +0000
Subject: [PATCH 5/5] Remove redundant cache clear

---
 llvm/include/llvm/ADT/GenericCycleInfo.h | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)

diff --git a/llvm/include/llvm/ADT/GenericCycleInfo.h b/llvm/include/llvm/ADT/GenericCycleInfo.h
index 14877bb6051091..b8b6e3e9967a4a 100644
--- a/llvm/include/llvm/ADT/GenericCycleInfo.h
+++ b/llvm/include/llvm/ADT/GenericCycleInfo.h
@@ -141,12 +141,7 @@ template <typename ContextT> class GenericCycle {
   bool contains(const GenericCycle *C) const;
 
   const GenericCycle *getParentCycle() const { return ParentCycle; }
-
-  GenericCycle *getParentCycle() {
-    clearCache();
-    return ParentCycle;
-  }
-
+  GenericCycle *getParentCycle() { return ParentCycle; }
   unsigned getDepth() const { return Depth; }
 
   /// Return all of the successor blocks of this cycle.



More information about the llvm-commits mailing list