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

via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 14 17:58:23 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/2] 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/2] 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; }



More information about the llvm-commits mailing list