[llvm] d0f122b - [LV] Update incoming blocks in VPWidenPHIRecipe in reassociateBlocks (#125481)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Feb 6 16:50:39 PST 2025
Author: Luke Lau
Date: 2025-02-07T08:50:35+08:00
New Revision: d0f122b9c5a5c6d26971d7ed57aeed8606da1ade
URL: https://github.com/llvm/llvm-project/commit/d0f122b9c5a5c6d26971d7ed57aeed8606da1ade
DIFF: https://github.com/llvm/llvm-project/commit/d0f122b9c5a5c6d26971d7ed57aeed8606da1ade.diff
LOG: [LV] Update incoming blocks in VPWidenPHIRecipe in reassociateBlocks (#125481)
This is extracted from #118638
After c7ebe4f we will crash in fixNonInductionPHIs if we use a
VPWidenPHIRecipe with the vector preheader as an incoming block, because
the phi will reference the old non-IRBB vector preheader.
This fixes this by updating VPBlockUtils::reassociateBlocks to update
any VPWidenPHIRecipes's incoming blocks.
This assumes that if the VPWidenPHIRecipe is in a VPRegionBlock, it's in
the entry block, and that we are replacing a VPBasicBlock with another
VPBasicBlock.
Added:
Modified:
llvm/lib/Transforms/Vectorize/VPlan.h
llvm/lib/Transforms/Vectorize/VPlanUtils.h
llvm/unittests/Transforms/Vectorize/VPlanTest.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Vectorize/VPlan.h b/llvm/lib/Transforms/Vectorize/VPlan.h
index 5b9dcf68a62bec..9988e03e9fdca1 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.h
+++ b/llvm/lib/Transforms/Vectorize/VPlan.h
@@ -2000,6 +2000,11 @@ class VPWidenPHIRecipe : public VPSingleDefRecipe {
/// Returns the \p I th incoming VPBasicBlock.
VPBasicBlock *getIncomingBlock(unsigned I) { return IncomingBlocks[I]; }
+ /// Set the \p I th incoming VPBasicBlock to \p IncomingBlock.
+ void setIncomingBlock(unsigned I, VPBasicBlock *IncomingBlock) {
+ IncomingBlocks[I] = IncomingBlock;
+ }
+
/// Returns the \p I th incoming VPValue.
VPValue *getIncomingValue(unsigned I) { return getOperand(I); }
};
diff --git a/llvm/lib/Transforms/Vectorize/VPlanUtils.h b/llvm/lib/Transforms/Vectorize/VPlanUtils.h
index 6ddb88308955f1..ac5e1978fcfbe0 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanUtils.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanUtils.h
@@ -169,8 +169,16 @@ class VPBlockUtils {
static void reassociateBlocks(VPBlockBase *Old, VPBlockBase *New) {
for (auto *Pred : to_vector(Old->getPredecessors()))
Pred->replaceSuccessor(Old, New);
- for (auto *Succ : to_vector(Old->getSuccessors()))
+ for (auto *Succ : to_vector(Old->getSuccessors())) {
Succ->replacePredecessor(Old, New);
+
+ // Replace any references to Old in widened phi incoming blocks.
+ for (auto &R : Succ->getEntryBasicBlock()->phis())
+ if (auto *WidenPhiR = dyn_cast<VPWidenPHIRecipe>(&R))
+ for (unsigned I = 0; I < WidenPhiR->getNumOperands(); I++)
+ if (WidenPhiR->getIncomingBlock(I) == Old)
+ WidenPhiR->setIncomingBlock(I, cast<VPBasicBlock>(New));
+ }
New->setPredecessors(Old->getPredecessors());
New->setSuccessors(Old->getSuccessors());
Old->clearPredecessors();
diff --git a/llvm/unittests/Transforms/Vectorize/VPlanTest.cpp b/llvm/unittests/Transforms/Vectorize/VPlanTest.cpp
index 23ecffa2db3b73..2f37c08bd9f117 100644
--- a/llvm/unittests/Transforms/Vectorize/VPlanTest.cpp
+++ b/llvm/unittests/Transforms/Vectorize/VPlanTest.cpp
@@ -660,6 +660,49 @@ TEST_F(VPBasicBlockTest, TraversingIteratorTest) {
}
}
+TEST_F(VPBasicBlockTest, reassociateBlocks) {
+ {
+ // Ensure that when we reassociate a basic block, we make sure to update any
+ // references to it in VPWidenPHIRecipes' incoming blocks.
+ VPlan &Plan = getPlan();
+ VPBasicBlock *VPBB1 = Plan.createVPBasicBlock("VPBB1");
+ VPBasicBlock *VPBB2 = Plan.createVPBasicBlock("VPBB2");
+ VPBlockUtils::connectBlocks(VPBB1, VPBB2);
+
+ auto *WidenPhi = new VPWidenPHIRecipe(nullptr);
+ IntegerType *Int32 = IntegerType::get(C, 32);
+ VPValue *Val = Plan.getOrAddLiveIn(ConstantInt::get(Int32, 1));
+ WidenPhi->addIncoming(Val, VPBB1);
+ VPBB2->appendRecipe(WidenPhi);
+
+ VPBasicBlock *VPBBNew = Plan.createVPBasicBlock("VPBBNew");
+ VPBlockUtils::reassociateBlocks(VPBB1, VPBBNew);
+ EXPECT_EQ(VPBB2->getSinglePredecessor(), VPBBNew);
+ EXPECT_EQ(WidenPhi->getIncomingBlock(0), VPBBNew);
+ }
+
+ {
+ // Ensure that we update VPWidenPHIRecipes that are nested inside a
+ // VPRegionBlock.
+ VPlan &Plan = getPlan();
+ VPBasicBlock *VPBB1 = Plan.createVPBasicBlock("VPBB1");
+ VPBasicBlock *VPBB2 = Plan.createVPBasicBlock("VPBB2");
+ VPRegionBlock *R1 = Plan.createVPRegionBlock(VPBB2, VPBB2, "R1");
+ VPBlockUtils::connectBlocks(VPBB1, R1);
+
+ auto *WidenPhi = new VPWidenPHIRecipe(nullptr);
+ IntegerType *Int32 = IntegerType::get(C, 32);
+ VPValue *Val = Plan.getOrAddLiveIn(ConstantInt::get(Int32, 1));
+ WidenPhi->addIncoming(Val, VPBB1);
+ VPBB2->appendRecipe(WidenPhi);
+
+ VPBasicBlock *VPBBNew = Plan.createVPBasicBlock("VPBBNew");
+ VPBlockUtils::reassociateBlocks(VPBB1, VPBBNew);
+ EXPECT_EQ(R1->getSinglePredecessor(), VPBBNew);
+ EXPECT_EQ(WidenPhi->getIncomingBlock(0), VPBBNew);
+ }
+}
+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
TEST_F(VPBasicBlockTest, print) {
VPInstruction *TC = new VPInstruction(Instruction::Add, {});
More information about the llvm-commits
mailing list