[llvm] c941d92 - [MachineCycle][NFC] add a cache for block and its top level cycle
Chen Zheng via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 20 22:31:21 PDT 2022
Author: Chen Zheng
Date: 2022-09-21T01:31:04-04:00
New Revision: c941d925b0e47ec166364178edac75cf1cb1ee1a
URL: https://github.com/llvm/llvm-project/commit/c941d925b0e47ec166364178edac75cf1cb1ee1a
DIFF: https://github.com/llvm/llvm-project/commit/c941d925b0e47ec166364178edac75cf1cb1ee1a.diff
LOG: [MachineCycle][NFC] add a cache for block and its top level cycle
This solves https://github.com/llvm/llvm-project/issues/57664
Reviewed By: sameerds
Differential Revision: https://reviews.llvm.org/D134019
Added:
Modified:
llvm/include/llvm/ADT/GenericCycleImpl.h
llvm/include/llvm/ADT/GenericCycleInfo.h
Removed:
################################################################################
diff --git a/llvm/include/llvm/ADT/GenericCycleImpl.h b/llvm/include/llvm/ADT/GenericCycleImpl.h
index ea2847f8c8ee6..07ac1768ea278 100644
--- a/llvm/include/llvm/ADT/GenericCycleImpl.h
+++ b/llvm/include/llvm/ADT/GenericCycleImpl.h
@@ -144,8 +144,12 @@ template <typename ContextT> class GenericCycleInfoCompute {
};
template <typename ContextT>
-auto GenericCycleInfo<ContextT>::getTopLevelParentCycle(
- const BlockT *Block) const -> CycleT * {
+auto GenericCycleInfo<ContextT>::getTopLevelParentCycle(BlockT *Block)
+ -> CycleT * {
+ auto Cycle = BlockMapTopLevel.find(Block);
+ if (Cycle != BlockMapTopLevel.end())
+ return Cycle->second;
+
auto MapIt = BlockMap.find(Block);
if (MapIt == BlockMap.end())
return nullptr;
@@ -153,12 +157,15 @@ auto GenericCycleInfo<ContextT>::getTopLevelParentCycle(
auto *C = MapIt->second;
while (C->ParentCycle)
C = C->ParentCycle;
+ BlockMapTopLevel.try_emplace(Block, C);
return C;
}
template <typename ContextT>
-void GenericCycleInfo<ContextT>::moveToNewParent(CycleT *NewParent,
- CycleT *Child) {
+void GenericCycleInfo<ContextT>::moveTopLevelCycleToNewParent(CycleT *NewParent,
+ CycleT *Child) {
+ assert((!Child->ParentCycle && !NewParent->ParentCycle) &&
+ "NewParent and Child must be both top level cycle!\n");
auto &CurrentContainer =
Child->ParentCycle ? Child->ParentCycle->Children : TopLevelCycles;
auto Pos = llvm::find_if(CurrentContainer, [=](const auto &Ptr) -> bool {
@@ -169,6 +176,13 @@ void GenericCycleInfo<ContextT>::moveToNewParent(CycleT *NewParent,
*Pos = std::move(CurrentContainer.back());
CurrentContainer.pop_back();
Child->ParentCycle = NewParent;
+
+ NewParent->Blocks.insert(NewParent->Blocks.end(), Child->block_begin(),
+ Child->block_end());
+
+ for (auto &It : BlockMapTopLevel)
+ if (It.second == Child)
+ It.second = NewParent;
}
/// \brief Main function of the cycle info computations.
@@ -240,10 +254,7 @@ void GenericCycleInfoCompute<ContextT>::run(BlockT *EntryBlock) {
<< "discovered child cycle "
<< Info.Context.print(BlockParent->getHeader()) << "\n");
// Make BlockParent the child of NewCycle.
- Info.moveToNewParent(NewCycle.get(), BlockParent);
- NewCycle->Blocks.insert(NewCycle->Blocks.end(),
- BlockParent->block_begin(),
- BlockParent->block_end());
+ Info.moveTopLevelCycleToNewParent(NewCycle.get(), BlockParent);
for (auto *ChildEntry : BlockParent->entries())
ProcessPredecessors(ChildEntry);
@@ -257,6 +268,7 @@ void GenericCycleInfoCompute<ContextT>::run(BlockT *EntryBlock) {
assert(!is_contained(NewCycle->Blocks, Block));
NewCycle->Blocks.push_back(Block);
ProcessPredecessors(Block);
+ Info.BlockMapTopLevel.try_emplace(Block, NewCycle.get());
}
} while (!Worklist.empty());
@@ -336,6 +348,7 @@ void GenericCycleInfoCompute<ContextT>::dfs(BlockT *EntryBlock) {
template <typename ContextT> void GenericCycleInfo<ContextT>::clear() {
TopLevelCycles.clear();
BlockMap.clear();
+ BlockMapTopLevel.clear();
}
/// \brief Compute the cycle info for a function.
diff --git a/llvm/include/llvm/ADT/GenericCycleInfo.h b/llvm/include/llvm/ADT/GenericCycleInfo.h
index 9fbcb5525230e..1b987d8d3ac6b 100644
--- a/llvm/include/llvm/ADT/GenericCycleInfo.h
+++ b/llvm/include/llvm/ADT/GenericCycleInfo.h
@@ -232,15 +232,24 @@ template <typename ContextT> class GenericCycleInfo {
private:
ContextT Context;
- /// Map basic blocks to their inner-most containing loop.
+ /// Map basic blocks to their inner-most containing cycle.
DenseMap<BlockT *, CycleT *> BlockMap;
+ /// Map basic blocks to their top level containing cycle.
+ DenseMap<BlockT *, CycleT *> BlockMapTopLevel;
+
/// Top-level cycles discovered by any DFS.
///
/// Note: The implementation treats the nullptr as the parent of
/// every top-level cycle. See \ref contains for an example.
std::vector<std::unique_ptr<CycleT>> TopLevelCycles;
+ /// Move \p Child to \p NewParent by manipulating Children vectors.
+ ///
+ /// Note: This is an incomplete operation that does not update the depth of
+ /// the subtree.
+ void moveTopLevelCycleToNewParent(CycleT *NewParent, CycleT *Child);
+
public:
GenericCycleInfo() = default;
GenericCycleInfo(GenericCycleInfo &&) = default;
@@ -254,13 +263,7 @@ template <typename ContextT> class GenericCycleInfo {
CycleT *getCycle(const BlockT *Block) const;
unsigned getCycleDepth(const BlockT *Block) const;
- CycleT *getTopLevelParentCycle(const BlockT *Block) const;
-
- /// Move \p Child to \p NewParent by manipulating Children vectors.
- ///
- /// Note: This is an incomplete operation that does not update the
- /// list of blocks in the new parent or the depth of the subtree.
- void moveToNewParent(CycleT *NewParent, CycleT *Child);
+ CycleT *getTopLevelParentCycle(BlockT *Block);
/// Methods for debug and self-test.
//@{
More information about the llvm-commits
mailing list