Skip to content

Commit

Permalink
[GenericCycle] Add a Cache for getExitBlocks in GenericCycle (llvm#11…
Browse files Browse the repository at this point in the history
…2290)

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`.
  • Loading branch information
Chengjunp authored Oct 29, 2024
1 parent 1549a0c commit 6233346
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
9 changes: 9 additions & 0 deletions llvm/include/llvm/ADT/GenericCycleImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -65,6 +70,7 @@ void GenericCycle<ContextT>::getExitBlocks(

TmpStorage.resize(NumExitBlocks);
}
ExitBlocksCache.append(TmpStorage.begin(), TmpStorage.end());
}

template <typename ContextT>
Expand Down Expand Up @@ -298,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>
Expand All @@ -316,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.
Expand Down
21 changes: 19 additions & 2 deletions llvm/include/llvm/ADT/GenericCycleInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,27 @@ template <typename ContextT> class GenericCycle {
/// always have the same depth.
unsigned Depth = 0;

/// Cache for the results of GetExitBlocks
mutable SmallVector<BlockT *, 4> ExitBlocksCache;

void clear() {
Entries.clear();
Children.clear();
Blocks.clear();
Depth = 0;
ParentCycle = nullptr;
clearCache();
}

void appendEntry(BlockT *Block) {
Entries.push_back(Block);
clearCache();
}

void appendEntry(BlockT *Block) { Entries.push_back(Block); }
void appendBlock(BlockT *Block) { Blocks.insert(Block); }
void appendBlock(BlockT *Block) {
Blocks.insert(Block);
clearCache();
}

GenericCycle(const GenericCycle &) = delete;
GenericCycle &operator=(const GenericCycle &) = delete;
Expand All @@ -102,6 +113,11 @@ 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);
Expand All @@ -112,6 +128,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.
Expand Down

0 comments on commit 6233346

Please sign in to comment.