[llvm] [GenericCycle] Add a Cache for getExitBlocks in GenericCycle (PR #112290)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Oct 14 17:49:29 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-adt
Author: Chengjun (Chengjunp)
<details>
<summary>Changes</summary>
In `UniformityAnalysis`, we need to get the exit blocks of cycles in the `DivergencePropagator` and currently, we have to do a search for the exit blocks every time. In this change, we add a cache of the results in the `GenericCycle` so that it can save the compile time. By testing, for some large cases, this can save about 60% compile time in the `UniformityAnalysis`.
---
Full diff: https://github.com/llvm/llvm-project/pull/112290.diff
2 Files Affected:
- (modified) llvm/include/llvm/ADT/GenericCycleImpl.h (+6)
- (modified) llvm/include/llvm/ADT/GenericCycleInfo.h (+6-1)
``````````diff
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; }
``````````
</details>
https://github.com/llvm/llvm-project/pull/112290
More information about the llvm-commits
mailing list