Skip to content

Commit 8cfe2f2

Browse files
committed
[LV] Update incoming blocks in VPWidenPHIRecipe in reassociateBlocks
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.
1 parent 3ae0f30 commit 8cfe2f2

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2347,6 +2347,11 @@ class VPWidenPHIRecipe : public VPSingleDefRecipe {
23472347
/// Returns the \p I th incoming VPBasicBlock.
23482348
VPBasicBlock *getIncomingBlock(unsigned I) { return IncomingBlocks[I]; }
23492349

2350+
/// Set the \p I th incoming VPBasicBlock to \p IncomingBlock.
2351+
void setIncomingBlock(unsigned I, VPBasicBlock *IncomingBlock) {
2352+
IncomingBlocks[I] = IncomingBlock;
2353+
}
2354+
23502355
/// Returns the \p I th incoming VPValue.
23512356
VPValue *getIncomingValue(unsigned I) { return getOperand(I); }
23522357
};

llvm/lib/Transforms/Vectorize/VPlanUtils.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,19 @@ class VPBlockUtils {
169169
static void reassociateBlocks(VPBlockBase *Old, VPBlockBase *New) {
170170
for (auto *Pred : to_vector(Old->getPredecessors()))
171171
Pred->replaceSuccessor(Old, New);
172-
for (auto *Succ : to_vector(Old->getSuccessors()))
172+
for (auto *Succ : to_vector(Old->getSuccessors())) {
173173
Succ->replacePredecessor(Old, New);
174+
175+
// Replace any references to Old in widened phi incoming blocks.
176+
while (auto *Region = dyn_cast<VPRegionBlock>(Succ))
177+
Succ = Region->getEntry();
178+
179+
for (auto &R : *cast<VPBasicBlock>(Succ))
180+
if (auto *WidenPhiR = dyn_cast<VPWidenPHIRecipe>(&R))
181+
for (unsigned I = 0; I < WidenPhiR->getNumOperands(); I++)
182+
if (WidenPhiR->getIncomingBlock(I) == Old)
183+
WidenPhiR->setIncomingBlock(I, cast<VPBasicBlock>(New));
184+
}
174185
New->setPredecessors(Old->getPredecessors());
175186
New->setSuccessors(Old->getSuccessors());
176187
Old->clearPredecessors();

llvm/unittests/Transforms/Vectorize/VPlanTest.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,45 @@ TEST_F(VPBasicBlockTest, TraversingIteratorTest) {
659659
}
660660
}
661661

662+
TEST_F(VPBasicBlockTest, reassociateBlocks) {
663+
{
664+
VPlan &Plan = getPlan();
665+
VPBasicBlock *VPBB1 = Plan.createVPBasicBlock("VPBB1");
666+
VPBasicBlock *VPBB2 = Plan.createVPBasicBlock("VPBB2");
667+
VPBlockUtils::connectBlocks(VPBB1, VPBB2);
668+
669+
auto *WidenPhi = new VPWidenPHIRecipe(nullptr);
670+
IntegerType *Int32 = IntegerType::get(C, 32);
671+
VPValue *Val = Plan.getOrAddLiveIn(ConstantInt::get(Int32, 1));
672+
WidenPhi->addIncoming(Val, VPBB1);
673+
VPBB2->appendRecipe(WidenPhi);
674+
675+
VPBasicBlock *VPBBNew = Plan.createVPBasicBlock("VPBBNew");
676+
VPBlockUtils::reassociateBlocks(VPBB1, VPBBNew);
677+
EXPECT_EQ(VPBB2->getSinglePredecessor(), VPBBNew);
678+
EXPECT_EQ(WidenPhi->getIncomingBlock(0), VPBBNew);
679+
}
680+
681+
{
682+
VPlan &Plan = getPlan();
683+
VPBasicBlock *VPBB1 = Plan.createVPBasicBlock("VPBB1");
684+
VPBasicBlock *VPBB2 = Plan.createVPBasicBlock("VPBB2");
685+
VPRegionBlock *R1 = Plan.createVPRegionBlock(VPBB2, VPBB2, "R1");
686+
VPBlockUtils::connectBlocks(VPBB1, R1);
687+
688+
auto *WidenPhi = new VPWidenPHIRecipe(nullptr);
689+
IntegerType *Int32 = IntegerType::get(C, 32);
690+
VPValue *Val = Plan.getOrAddLiveIn(ConstantInt::get(Int32, 1));
691+
WidenPhi->addIncoming(Val, VPBB1);
692+
VPBB2->appendRecipe(WidenPhi);
693+
694+
VPBasicBlock *VPBBNew = Plan.createVPBasicBlock("VPBBNew");
695+
VPBlockUtils::reassociateBlocks(VPBB1, VPBBNew);
696+
EXPECT_EQ(R1->getSinglePredecessor(), VPBBNew);
697+
EXPECT_EQ(WidenPhi->getIncomingBlock(0), VPBBNew);
698+
}
699+
}
700+
662701
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
663702
TEST_F(VPBasicBlockTest, print) {
664703
VPInstruction *TC = new VPInstruction(Instruction::Add, {});

0 commit comments

Comments
 (0)