Skip to content

Commit 2b20444

Browse files
fhahnkrishna2803
authored andcommitted
[VPlan] Use scalar VPPhi instead of VPWidenPHIRecipe in createPlainCFG. (llvm#150847)
The initial VPlan closely reflects the original scalar loop, so unsing VPWidenPHIRecipe here is premature. Widened phi recipes should only be introduced together with other widened recipes. PR: llvm#150847
1 parent 40d0012 commit 2b20444

File tree

8 files changed

+46
-32
lines changed

8 files changed

+46
-32
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8301,7 +8301,7 @@ VPRecipeBase *VPRecipeBuilder::tryToCreateWidenRecipe(VPSingleDefRecipe *R,
83018301
VPRecipeBase *Recipe;
83028302
Instruction *Instr = R->getUnderlyingInstr();
83038303
SmallVector<VPValue *, 4> Operands(R->operands());
8304-
if (auto *PhiR = dyn_cast<VPWidenPHIRecipe>(R)) {
8304+
if (auto *PhiR = dyn_cast<VPPhi>(R)) {
83058305
VPBasicBlock *Parent = PhiR->getParent();
83068306
[[maybe_unused]] VPRegionBlock *LoopRegionOf =
83078307
Parent->getEnclosingLoopRegion();
@@ -8339,6 +8339,7 @@ VPRecipeBase *VPRecipeBuilder::tryToCreateWidenRecipe(VPSingleDefRecipe *R,
83398339
PhiRecipe->addOperand(Operands[1]);
83408340
return PhiRecipe;
83418341
}
8342+
assert(!R->isPhi() && "only VPPhi nodes expected at this point");
83428343

83438344
if (isa<TruncInst>(Instr) && (Recipe = tryToOptimizeInductionTruncate(
83448345
cast<TruncInst>(Instr), Operands, Range)))

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,12 +1242,24 @@ struct LLVM_ABI_FOR_TEST VPPhi : public VPInstruction, public VPPhiAccessors {
12421242
: VPInstruction(Instruction::PHI, Operands, DL, Name) {}
12431243

12441244
static inline bool classof(const VPUser *U) {
1245-
auto *R = dyn_cast<VPInstruction>(U);
1246-
return R && R->getOpcode() == Instruction::PHI;
1245+
auto *VPI = dyn_cast<VPInstruction>(U);
1246+
return VPI && VPI->getOpcode() == Instruction::PHI;
1247+
}
1248+
1249+
static inline bool classof(const VPValue *V) {
1250+
auto *VPI = dyn_cast<VPInstruction>(V);
1251+
return VPI && VPI->getOpcode() == Instruction::PHI;
1252+
}
1253+
1254+
static inline bool classof(const VPSingleDefRecipe *SDR) {
1255+
auto *VPI = dyn_cast<VPInstruction>(SDR);
1256+
return VPI && VPI->getOpcode() == Instruction::PHI;
12471257
}
12481258

12491259
VPPhi *clone() override {
1250-
return new VPPhi(operands(), getDebugLoc(), getName());
1260+
auto *PhiR = new VPPhi(operands(), getDebugLoc(), getName());
1261+
PhiR->setUnderlyingValue(getUnderlyingValue());
1262+
return PhiR;
12511263
}
12521264

12531265
void execute(VPTransformState &State) override;

llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,15 @@ void PlainCFGBuilder::fixHeaderPhis() {
9191
for (auto *Phi : PhisToFix) {
9292
assert(IRDef2VPValue.count(Phi) && "Missing VPInstruction for PHINode.");
9393
VPValue *VPVal = IRDef2VPValue[Phi];
94-
assert(isa<VPWidenPHIRecipe>(VPVal) &&
95-
"Expected WidenPHIRecipe for phi node.");
96-
auto *VPPhi = cast<VPWidenPHIRecipe>(VPVal);
97-
assert(VPPhi->getNumOperands() == 0 &&
98-
"Expected VPInstruction with no operands.");
94+
assert(isa<VPPhi>(VPVal) && "Expected VPPhi for phi node.");
95+
auto *PhiR = cast<VPPhi>(VPVal);
96+
assert(PhiR->getNumOperands() == 0 && "Expected VPPhi with no operands.");
9997
assert(isHeaderBB(Phi->getParent(), LI->getLoopFor(Phi->getParent())) &&
10098
"Expected Phi in header block.");
10199
assert(Phi->getNumOperands() == 2 &&
102100
"header phi must have exactly 2 operands");
103101
for (BasicBlock *Pred : predecessors(Phi->getParent()))
104-
VPPhi->addOperand(
102+
PhiR->addOperand(
105103
getOrCreateVPOperand(Phi->getIncomingValueForBlock(Pred)));
106104
}
107105
}
@@ -204,11 +202,11 @@ void PlainCFGBuilder::createVPInstructionsForVPBB(VPBasicBlock *VPBB,
204202

205203
VPSingleDefRecipe *NewR;
206204
if (auto *Phi = dyn_cast<PHINode>(Inst)) {
207-
// Phi node's operands may have not been visited at this point. We create
205+
// Phi node's operands may not have been visited at this point. We create
208206
// an empty VPInstruction that we will fix once the whole plain CFG has
209207
// been built.
210-
NewR = new VPWidenPHIRecipe(Phi, nullptr, Phi->getDebugLoc(), "vec.phi");
211-
VPBB->appendRecipe(NewR);
208+
NewR = VPIRBuilder.createScalarPhi({}, Phi->getDebugLoc(), "vec.phi");
209+
NewR->setUnderlyingValue(Phi);
212210
if (isHeaderBB(Phi->getParent(), LI->getLoopFor(Phi->getParent()))) {
213211
// Header phis need to be fixed after the VPBB for the latch has been
214212
// created.

llvm/lib/Transforms/Vectorize/VPlanPredicator.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,10 @@ void VPPredicator::createSwitchEdgeMasks(VPInstruction *SI) {
227227
}
228228

229229
void VPPredicator::convertPhisToBlends(VPBasicBlock *VPBB) {
230-
SmallVector<VPWidenPHIRecipe *> Phis;
230+
SmallVector<VPPhi *> Phis;
231231
for (VPRecipeBase &R : VPBB->phis())
232-
Phis.push_back(cast<VPWidenPHIRecipe>(&R));
233-
for (VPWidenPHIRecipe *PhiR : Phis) {
232+
Phis.push_back(cast<VPPhi>(&R));
233+
for (VPPhi *PhiR : Phis) {
234234
// The non-header Phi is converted into a Blend recipe below,
235235
// so we don't have to worry about the insertion order and we can just use
236236
// the builder. At this point we generate the predication tree. There may

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,20 @@ bool VPlanTransforms::tryToConvertVPInstructionsToVPRecipes(
6363
Instruction *Inst = cast<Instruction>(VPV->getUnderlyingValue());
6464

6565
VPRecipeBase *NewRecipe = nullptr;
66-
if (auto *VPPhi = dyn_cast<VPWidenPHIRecipe>(&Ingredient)) {
67-
auto *Phi = cast<PHINode>(VPPhi->getUnderlyingValue());
66+
if (auto *PhiR = dyn_cast<VPPhi>(&Ingredient)) {
67+
auto *Phi = cast<PHINode>(PhiR->getUnderlyingValue());
6868
const auto *II = GetIntOrFpInductionDescriptor(Phi);
69-
if (!II)
70-
continue;
71-
72-
VPValue *Start = Plan->getOrAddLiveIn(II->getStartValue());
73-
VPValue *Step =
74-
vputils::getOrCreateVPValueForSCEVExpr(*Plan, II->getStep(), SE);
75-
NewRecipe = new VPWidenIntOrFpInductionRecipe(
76-
Phi, Start, Step, &Plan->getVF(), *II, Ingredient.getDebugLoc());
69+
if (!II) {
70+
NewRecipe = new VPWidenPHIRecipe(Phi, nullptr, PhiR->getDebugLoc());
71+
for (VPValue *Op : PhiR->operands())
72+
NewRecipe->addOperand(Op);
73+
} else {
74+
VPValue *Start = Plan->getOrAddLiveIn(II->getStartValue());
75+
VPValue *Step =
76+
vputils::getOrCreateVPValueForSCEVExpr(*Plan, II->getStep(), SE);
77+
NewRecipe = new VPWidenIntOrFpInductionRecipe(
78+
Phi, Start, Step, &Plan->getVF(), *II, Ingredient.getDebugLoc());
79+
}
7780
} else {
7881
assert(isa<VPInstruction>(&Ingredient) &&
7982
"only VPInstructions expected here");

llvm/test/Transforms/LoopVectorize/outer-loop-vec-phi-predecessor-order.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ define void @test(ptr %src, i64 %n) {
3535
; CHECK-NEXT: [[TMP3:%.*]] = icmp eq <4 x i64> [[TMP2]], [[BROADCAST_SPLAT]]
3636
; CHECK-NEXT: [[TMP4:%.*]] = extractelement <4 x i1> [[TMP3]], i32 0
3737
; CHECK-NEXT: br i1 [[TMP4]], label [[LOOP_2_LATCH4]], label [[LOOP_32]]
38-
; CHECK: loop.2.latch4:
38+
; CHECK: loop.2.latch3:
3939
; CHECK-NEXT: [[TMP5]] = add nuw nsw <4 x i64> [[VEC_PHI]], splat (i64 1)
4040
; CHECK-NEXT: [[TMP6:%.*]] = icmp eq <4 x i64> [[TMP5]], [[BROADCAST_SPLAT]]
4141
; CHECK-NEXT: [[TMP7:%.*]] = extractelement <4 x i1> [[TMP6]], i32 0

llvm/test/Transforms/LoopVectorize/vplan-printing-outer-loop.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ define void @foo(i64 %n) {
1313
; CHECK-NEXT: Successor(s): outer.header
1414
; CHECK-EMPTY:
1515
; CHECK-NEXT: outer.header:
16-
; CHECK-NEXT: WIDEN-PHI ir<%outer.iv> = phi [ ir<%outer.iv.next>, outer.latch ], [ ir<0>, ir-bb<entry> ]
16+
; CHECK-NEXT: EMIT-SCALAR ir<%outer.iv> = phi [ ir<%outer.iv.next>, outer.latch ], [ ir<0>, ir-bb<entry> ]
1717
; CHECK-NEXT: EMIT ir<%gep.1> = getelementptr ir<@arr2>, ir<0>, ir<%outer.iv>
1818
; CHECK-NEXT: EMIT store ir<%outer.iv>, ir<%gep.1>
1919
; CHECK-NEXT: EMIT ir<%add> = add ir<%outer.iv>, ir<%n>
2020
; CHECK-NEXT: Successor(s): inner
2121
; CHECK-EMPTY:
2222
; CHECK-NEXT: inner:
23-
; CHECK-NEXT: WIDEN-PHI ir<%inner.iv> = phi [ ir<%inner.iv.next>, inner ], [ ir<0>, outer.header ]
23+
; CHECK-NEXT: EMIT-SCALAR ir<%inner.iv> = phi [ ir<%inner.iv.next>, inner ], [ ir<0>, outer.header ]
2424
; CHECK-NEXT: EMIT ir<%gep.2> = getelementptr ir<@arr>, ir<0>, ir<%inner.iv>, ir<%outer.iv>
2525
; CHECK-NEXT: EMIT store ir<%add>, ir<%gep.2>
2626
; CHECK-NEXT: EMIT ir<%inner.iv.next> = add ir<%inner.iv>, ir<1>

llvm/unittests/Transforms/Vectorize/VPlanHCFGTest.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ TEST_F(VPlanHCFGTest, testBuildHCFGInnerLoop) {
5959
auto Iter = VecBB->begin();
6060
auto *CanIV = dyn_cast<VPCanonicalIVPHIRecipe>(&*Iter++);
6161
EXPECT_NE(nullptr, CanIV);
62-
VPWidenPHIRecipe *Phi = dyn_cast<VPWidenPHIRecipe>(&*Iter++);
62+
auto *Phi = dyn_cast<VPPhi>(&*Iter++);
6363
EXPECT_NE(nullptr, Phi);
6464

6565
VPInstruction *Idx = dyn_cast<VPInstruction>(&*Iter++);
@@ -138,7 +138,7 @@ compound=true
138138
N4 [label =
139139
"vector.body:\l" +
140140
" EMIT vp\<%2\> = CANONICAL-INDUCTION ir\<0\>, vp\<%index.next\>\l" +
141-
" WIDEN-PHI ir\<%indvars.iv\> = phi [ ir\<0\>, vector.ph ], [ ir\<%indvars.iv.next\>, vector.body ]\l" +
141+
" EMIT-SCALAR ir\<%indvars.iv\> = phi [ ir\<0\>, vector.ph ], [ ir\<%indvars.iv.next\>, vector.body ]\l" +
142142
" EMIT ir\<%arr.idx\> = getelementptr ir\<%A\>, ir\<%indvars.iv\>\l" +
143143
" EMIT ir\<%l1\> = load ir\<%arr.idx\>\l" +
144144
" EMIT ir\<%res\> = add ir\<%l1\>, ir\<10\>\l" +
@@ -304,7 +304,7 @@ compound=true
304304
N4 [label =
305305
"vector.body:\l" +
306306
" EMIT vp\<%2\> = CANONICAL-INDUCTION ir\<0\>, vp\<%index.next\>\l" +
307-
" WIDEN-PHI ir\<%iv\> = phi [ ir\<0\>, vector.ph ], [ ir\<%iv.next\>, loop.latch ]\l" +
307+
" EMIT-SCALAR ir\<%iv\> = phi [ ir\<0\>, vector.ph ], [ ir\<%iv.next\>, loop.latch ]\l" +
308308
" EMIT ir\<%arr.idx\> = getelementptr ir\<%A\>, ir\<%iv\>\l" +
309309
" EMIT ir\<%l1\> = load ir\<%arr.idx\>\l" +
310310
" EMIT ir\<%c\> = icmp ir\<%l1\>, ir\<0\>\l" +

0 commit comments

Comments
 (0)