[llvm] e2c43a5 - [VPlan] Add vp_depth_first_deep (NFC)
Florian Hahn via llvm-commits
llvm-commits at lists.llvm.org
Thu Jan 19 12:34:49 PST 2023
Author: Florian Hahn
Date: 2023-01-19T20:34:23Z
New Revision: e2c43a547b311725067e7b56ae935e940e6fc1c3
URL: https://github.com/llvm/llvm-project/commit/e2c43a547b311725067e7b56ae935e940e6fc1c3
DIFF: https://github.com/llvm/llvm-project/commit/e2c43a547b311725067e7b56ae935e940e6fc1c3.diff
LOG: [VPlan] Add vp_depth_first_deep (NFC)
Similar to vp_depth_first_shallow (D140512) add vp_depth_first_deep to
make existing code clearer and more compact.
Reviewed By: Ayal
Differential Revision: https://reviews.llvm.org/D142055
Added:
Modified:
llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
llvm/lib/Transforms/Vectorize/VPlan.cpp
llvm/lib/Transforms/Vectorize/VPlanCFG.h
llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
llvm/lib/Transforms/Vectorize/VPlanVerifier.cpp
llvm/unittests/Transforms/Vectorize/VPlanTest.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index c6b80e7721117..ca13e7984abd9 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -1078,8 +1078,7 @@ void InnerLoopVectorizer::collectPoisonGeneratingRecipes(
// Traverse all the recipes in the VPlan and collect the poison-generating
// recipes in the backward slice starting at the address of a VPWidenRecipe or
// VPInterleaveRecipe.
- auto Iter = depth_first(
- VPBlockRecursiveTraversalWrapper<VPBlockBase *>(State.Plan->getEntry()));
+ auto Iter = vp_depth_first_deep(State.Plan->getEntry());
for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(Iter)) {
for (VPRecipeBase &Recipe : *VPBB) {
if (auto *WidenRec = dyn_cast<VPWidenMemoryInstructionRecipe>(&Recipe)) {
@@ -4161,8 +4160,7 @@ void InnerLoopVectorizer::sinkScalarOperands(Instruction *PredInst) {
void InnerLoopVectorizer::fixNonInductionPHIs(VPlan &Plan,
VPTransformState &State) {
- auto Iter = depth_first(
- VPBlockRecursiveTraversalWrapper<VPBlockBase *>(Plan.getEntry()));
+ auto Iter = vp_depth_first_deep(Plan.getEntry());
for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(Iter)) {
for (VPRecipeBase &P : VPBB->phis()) {
VPWidenPHIRecipe *VPPhi = dyn_cast<VPWidenPHIRecipe>(&P);
diff --git a/llvm/lib/Transforms/Vectorize/VPlan.cpp b/llvm/lib/Transforms/Vectorize/VPlan.cpp
index f026dbe2b2de7..d554f438c8040 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlan.cpp
@@ -1099,10 +1099,8 @@ void VPSlotTracker::assignSlots(const VPlan &Plan) {
if (Plan.BackedgeTakenCount)
assignSlot(Plan.BackedgeTakenCount);
- ReversePostOrderTraversal<
- VPBlockRecursiveTraversalWrapper<const VPBlockBase *>>
- RPOT(VPBlockRecursiveTraversalWrapper<const VPBlockBase *>(
- Plan.getEntry()));
+ ReversePostOrderTraversal<VPBlockDeepTraversalWrapper<const VPBlockBase *>>
+ RPOT(VPBlockDeepTraversalWrapper<const VPBlockBase *>(Plan.getEntry()));
for (const VPBasicBlock *VPBB :
VPBlockUtils::blocksOnly<const VPBasicBlock>(RPOT))
for (const VPRecipeBase &Recipe : *VPBB)
diff --git a/llvm/lib/Transforms/Vectorize/VPlanCFG.h b/llvm/lib/Transforms/Vectorize/VPlanCFG.h
index 8ee949d2f5529..3ca1538dc4619 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanCFG.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanCFG.h
@@ -220,11 +220,11 @@ class VPAllSuccessorsIterator
};
/// Helper for GraphTraits specialization that traverses through VPRegionBlocks.
-template <typename BlockTy> class VPBlockRecursiveTraversalWrapper {
+template <typename BlockTy> class VPBlockDeepTraversalWrapper {
BlockTy Entry;
public:
- VPBlockRecursiveTraversalWrapper(BlockTy Entry) : Entry(Entry) {}
+ VPBlockDeepTraversalWrapper(BlockTy Entry) : Entry(Entry) {}
BlockTy getEntry() { return Entry; }
};
@@ -233,13 +233,11 @@ template <typename BlockTy> class VPBlockRecursiveTraversalWrapper {
/// implicitly have their parent region's successors. This ensures all blocks in
/// a region are visited before any blocks in a successor region when doing a
/// reverse post-order traversal of the graph.
-template <>
-struct GraphTraits<VPBlockRecursiveTraversalWrapper<VPBlockBase *>> {
+template <> struct GraphTraits<VPBlockDeepTraversalWrapper<VPBlockBase *>> {
using NodeRef = VPBlockBase *;
using ChildIteratorType = VPAllSuccessorsIterator<VPBlockBase *>;
- static NodeRef
- getEntryNode(VPBlockRecursiveTraversalWrapper<VPBlockBase *> N) {
+ static NodeRef getEntryNode(VPBlockDeepTraversalWrapper<VPBlockBase *> N) {
return N.getEntry();
}
@@ -253,12 +251,12 @@ struct GraphTraits<VPBlockRecursiveTraversalWrapper<VPBlockBase *>> {
};
template <>
-struct GraphTraits<VPBlockRecursiveTraversalWrapper<const VPBlockBase *>> {
+struct GraphTraits<VPBlockDeepTraversalWrapper<const VPBlockBase *>> {
using NodeRef = const VPBlockBase *;
using ChildIteratorType = VPAllSuccessorsIterator<const VPBlockBase *>;
static NodeRef
- getEntryNode(VPBlockRecursiveTraversalWrapper<const VPBlockBase *> N) {
+ getEntryNode(VPBlockDeepTraversalWrapper<const VPBlockBase *> N) {
return N.getEntry();
}
@@ -330,6 +328,18 @@ vp_depth_first_shallow(const VPBlockBase *G) {
return depth_first(VPBlockShallowTraversalWrapper<const VPBlockBase *>(G));
}
+/// Returns an iterator range to traverse the graph starting at \p G in
+/// depth-first order while traversing through region blocks.
+inline iterator_range<df_iterator<VPBlockDeepTraversalWrapper<VPBlockBase *>>>
+vp_depth_first_deep(VPBlockBase *G) {
+ return depth_first(VPBlockDeepTraversalWrapper<VPBlockBase *>(G));
+}
+inline iterator_range<
+ df_iterator<VPBlockDeepTraversalWrapper<const VPBlockBase *>>>
+vp_depth_first_deep(const VPBlockBase *G) {
+ return depth_first(VPBlockDeepTraversalWrapper<const VPBlockBase *>(G));
+}
+
} // namespace llvm
#endif // LLVM_TRANSFORMS_VECTORIZE_VPLANCFG_H
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index bb9fd47a3e306..429b80fabbda9 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -28,8 +28,8 @@ void VPlanTransforms::VPInstructionsToVPRecipes(
SmallPtrSetImpl<Instruction *> &DeadInstructions, ScalarEvolution &SE,
const TargetLibraryInfo &TLI) {
- ReversePostOrderTraversal<VPBlockRecursiveTraversalWrapper<VPBlockBase *>>
- RPOT(Plan->getEntry());
+ ReversePostOrderTraversal<VPBlockDeepTraversalWrapper<VPBlockBase *>> RPOT(
+ Plan->getEntry());
for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(RPOT)) {
VPRecipeBase *Term = VPBB->getTerminator();
auto EndIter = Term ? Term->getIterator() : VPBB->end();
@@ -107,8 +107,7 @@ void VPlanTransforms::VPInstructionsToVPRecipes(
}
bool VPlanTransforms::sinkScalarOperands(VPlan &Plan) {
- auto Iter = depth_first(
- VPBlockRecursiveTraversalWrapper<VPBlockBase *>(Plan.getEntry()));
+ auto Iter = vp_depth_first_deep(Plan.getEntry());
bool Changed = false;
// First, collect the operands of all predicated replicate recipes as seeds
// for sinking.
@@ -229,9 +228,8 @@ bool VPlanTransforms::mergeReplicateRegionsIntoSuccessors(VPlan &Plan) {
// replicate region with matching masks to process front. This is to avoid
// iterator invalidation issues while merging regions.
SmallVector<VPRegionBlock *, 8> WorkList;
- for (VPRegionBlock *Region1 :
- VPBlockUtils::blocksOnly<VPRegionBlock>(depth_first(
- VPBlockRecursiveTraversalWrapper<VPBlockBase *>(Plan.getEntry())))) {
+ for (VPRegionBlock *Region1 : VPBlockUtils::blocksOnly<VPRegionBlock>(
+ vp_depth_first_deep(Plan.getEntry()))) {
if (!Region1->isReplicator())
continue;
auto *MiddleBasicBlock =
@@ -313,8 +311,8 @@ bool VPlanTransforms::mergeReplicateRegionsIntoSuccessors(VPlan &Plan) {
bool VPlanTransforms::mergeBlocksIntoPredecessors(VPlan &Plan) {
SmallVector<VPBasicBlock *> WorkList;
- for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(depth_first(
- VPBlockRecursiveTraversalWrapper<VPBlockBase *>(Plan.getEntry())))) {
+ for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(
+ vp_depth_first_deep(Plan.getEntry()))) {
auto *PredVPBB =
dyn_cast_or_null<VPBasicBlock>(VPBB->getSinglePredecessor());
if (PredVPBB && PredVPBB->getNumSuccessors() == 1)
@@ -404,8 +402,8 @@ void VPlanTransforms::removeRedundantCanonicalIVs(VPlan &Plan) {
}
void VPlanTransforms::removeDeadRecipes(VPlan &Plan) {
- ReversePostOrderTraversal<VPBlockRecursiveTraversalWrapper<VPBlockBase *>>
- RPOT(Plan.getEntry());
+ ReversePostOrderTraversal<VPBlockDeepTraversalWrapper<VPBlockBase *>> RPOT(
+ Plan.getEntry());
for (VPBasicBlock *VPBB : reverse(VPBlockUtils::blocksOnly<VPBasicBlock>(RPOT))) {
// The recipes in the block are processed in reverse order, to catch chains
diff --git a/llvm/lib/Transforms/Vectorize/VPlanVerifier.cpp b/llvm/lib/Transforms/Vectorize/VPlanVerifier.cpp
index 56a1b563eeeb6..18125cebed339 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanVerifier.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanVerifier.cpp
@@ -252,8 +252,7 @@ verifyVPBasicBlock(const VPBasicBlock *VPBB,
bool VPlanVerifier::verifyPlanIsValid(const VPlan &Plan) {
DenseMap<const VPBlockBase *, unsigned> BlockNumbering;
unsigned Cnt = 0;
- auto Iter = depth_first(
- VPBlockRecursiveTraversalWrapper<const VPBlockBase *>(Plan.getEntry()));
+ auto Iter = vp_depth_first_deep(Plan.getEntry());
for (const VPBlockBase *VPB : Iter) {
BlockNumbering[VPB] = Cnt++;
auto *VPBB = dyn_cast<VPBasicBlock>(VPB);
@@ -298,8 +297,7 @@ bool VPlanVerifier::verifyPlanIsValid(const VPlan &Plan) {
for (const VPRegionBlock *Region :
VPBlockUtils::blocksOnly<const VPRegionBlock>(
- depth_first(VPBlockRecursiveTraversalWrapper<const VPBlockBase *>(
- Plan.getEntry())))) {
+ vp_depth_first_deep(Plan.getEntry()))) {
if (Region->getEntry()->getNumPredecessors() != 0) {
errs() << "region entry block has predecessors\n";
return false;
diff --git a/llvm/unittests/Transforms/Vectorize/VPlanTest.cpp b/llvm/unittests/Transforms/Vectorize/VPlanTest.cpp
index 899539969b73c..495ce1976590e 100644
--- a/llvm/unittests/Transforms/Vectorize/VPlanTest.cpp
+++ b/llvm/unittests/Transforms/Vectorize/VPlanTest.cpp
@@ -346,7 +346,7 @@ TEST(VPBasicBlockTest, TraversingIteratorTest) {
VPBlockUtils::connectBlocks(VPBB2, VPBB4);
VPBlockUtils::connectBlocks(VPBB3, VPBB4);
- VPBlockRecursiveTraversalWrapper<const VPBlockBase *> Start(VPBB1);
+ VPBlockDeepTraversalWrapper<const VPBlockBase *> Start(VPBB1);
SmallVector<const VPBlockBase *> FromIterator(depth_first(Start));
EXPECT_EQ(4u, FromIterator.size());
EXPECT_EQ(VPBB1, FromIterator[0]);
@@ -402,7 +402,7 @@ TEST(VPBasicBlockTest, TraversingIteratorTest) {
EXPECT_EQ(R1BB1, FromIterator[0]);
// Depth-first.
- VPBlockRecursiveTraversalWrapper<VPBlockBase *> Start(R1);
+ VPBlockDeepTraversalWrapper<VPBlockBase *> Start(R1);
FromIterator.clear();
copy(df_begin(Start), df_end(Start), std::back_inserter(FromIterator));
EXPECT_EQ(8u, FromIterator.size());
@@ -500,7 +500,7 @@ TEST(VPBasicBlockTest, TraversingIteratorTest) {
VPBlockUtils::connectBlocks(R1, VPBB2);
// Depth-first.
- VPBlockRecursiveTraversalWrapper<VPBlockBase *> Start(VPBB1);
+ VPBlockDeepTraversalWrapper<VPBlockBase *> Start(VPBB1);
SmallVector<VPBlockBase *> FromIterator(depth_first(Start));
EXPECT_EQ(10u, FromIterator.size());
EXPECT_EQ(VPBB1, FromIterator[0]);
@@ -557,7 +557,7 @@ TEST(VPBasicBlockTest, TraversingIteratorTest) {
VPBlockUtils::connectBlocks(VPBB1, R1);
// Depth-first.
- VPBlockRecursiveTraversalWrapper<VPBlockBase *> Start(VPBB1);
+ VPBlockDeepTraversalWrapper<VPBlockBase *> Start(VPBB1);
SmallVector<VPBlockBase *> FromIterator(depth_first(Start));
EXPECT_EQ(5u, FromIterator.size());
EXPECT_EQ(VPBB1, FromIterator[0]);
@@ -617,7 +617,7 @@ TEST(VPBasicBlockTest, TraversingIteratorTest) {
VPBlockUtils::connectBlocks(R1, VPBB2);
// Depth-first.
- VPBlockRecursiveTraversalWrapper<VPBlockBase *> Start(VPBB1);
+ VPBlockDeepTraversalWrapper<VPBlockBase *> Start(VPBB1);
SmallVector<VPBlockBase *> FromIterator(depth_first(Start));
EXPECT_EQ(7u, FromIterator.size());
EXPECT_EQ(VPBB1, FromIterator[0]);
@@ -649,7 +649,7 @@ TEST(VPBasicBlockTest, TraversingIteratorTest) {
EXPECT_EQ(VPBB1, FromIterator[6]);
// Post-order, const VPRegionBlocks only.
- VPBlockRecursiveTraversalWrapper<const VPBlockBase *> StartConst(VPBB1);
+ VPBlockDeepTraversalWrapper<const VPBlockBase *> StartConst(VPBB1);
SmallVector<const VPRegionBlock *> FromIteratorVPRegion(
VPBlockUtils::blocksOnly<const VPRegionBlock>(post_order(StartConst)));
EXPECT_EQ(3u, FromIteratorVPRegion.size());
More information about the llvm-commits
mailing list