Skip to content

Commit 9da6627

Browse files
committed
[VPlan] Create resume phis in scalar preheader early. (NFC)
Create phi recipes for scalar resume value up front in addInitialSkeleton during initial construction. This will allow moving the remaining code dealing with resume values to VPlan transforms/construction.
1 parent 9762c91 commit 9da6627

File tree

5 files changed

+33
-26
lines changed

5 files changed

+33
-26
lines changed

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,7 @@ class LLVM_ABI_FOR_TEST VPInstruction : public VPRecipeWithIRFlags,
10841084
OpcodeTy Opcode;
10851085

10861086
/// An optional name that can be used for the generated IR instruction.
1087-
const std::string Name;
1087+
std::string Name;
10881088

10891089
/// Returns true if we can generate a scalar for the first lane only if
10901090
/// needed.
@@ -1183,6 +1183,8 @@ class LLVM_ABI_FOR_TEST VPInstruction : public VPRecipeWithIRFlags,
11831183

11841184
/// Returns the symbolic name assigned to the VPInstruction.
11851185
StringRef getName() const { return Name; }
1186+
1187+
void setName(StringRef NewName) { Name = NewName.str(); }
11861188
};
11871189

11881190
/// A specialization of VPInstruction augmenting it with a dedicated result

llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,15 @@ static void addInitialSkeleton(VPlan &Plan, Type *InductionTy, DebugLoc IVDL,
533533
Plan.getEntry()->swapSuccessors();
534534

535535
createExtractsForLiveOuts(Plan, MiddleVPBB);
536+
537+
VPBuilder ScalarPHBuilder(ScalarPH);
538+
for (const auto &[PhiR, ScalarPhiR] : zip_equal(
539+
drop_begin(HeaderVPBB->phis()), Plan.getScalarHeader()->phis())) {
540+
auto *VectorPhiR = cast<VPPhi>(&PhiR);
541+
auto *ResumePhiR = ScalarPHBuilder.createScalarPhi(
542+
{VectorPhiR, VectorPhiR->getOperand(0)}, VectorPhiR->getDebugLoc());
543+
cast<VPIRPhi>(&ScalarPhiR)->addOperand(ResumePhiR);
544+
}
536545
}
537546

538547
std::unique_ptr<VPlan>

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4459,9 +4459,10 @@ void VPlanTransforms::addBranchWeightToMiddleTerminator(
44594459
/// Create and return a ResumePhi for \p WideIV, unless it is truncated. If the
44604460
/// induction recipe is not canonical, creates a VPDerivedIVRecipe to compute
44614461
/// the end value of the induction.
4462-
static VPInstruction *addResumePhiRecipeForInduction(
4463-
VPWidenInductionRecipe *WideIV, VPBuilder &VectorPHBuilder,
4464-
VPBuilder &ScalarPHBuilder, VPTypeAnalysis &TypeInfo, VPValue *VectorTC) {
4462+
static VPValue *addResumePhiRecipeForInduction(VPWidenInductionRecipe *WideIV,
4463+
VPBuilder &VectorPHBuilder,
4464+
VPTypeAnalysis &TypeInfo,
4465+
VPValue *VectorTC) {
44654466
auto *WideIntOrFp = dyn_cast<VPWidenIntOrFpInductionRecipe>(WideIV);
44664467
// Truncated wide inductions resume from the last lane of their vector value
44674468
// in the last vector iteration which is handled elsewhere.
@@ -4487,9 +4488,7 @@ static VPInstruction *addResumePhiRecipeForInduction(
44874488
WideIV->getDebugLoc());
44884489
}
44894490

4490-
auto *ResumePhiRecipe = ScalarPHBuilder.createScalarPhi(
4491-
{EndValue, Start}, WideIV->getDebugLoc(), "bc.resume.val");
4492-
return ResumePhiRecipe;
4491+
return EndValue;
44934492
}
44944493

44954494
void VPlanTransforms::addScalarResumePhis(
@@ -4502,21 +4501,18 @@ void VPlanTransforms::addScalarResumePhis(
45024501
VPBuilder VectorPHBuilder(
45034502
cast<VPBasicBlock>(VectorRegion->getSinglePredecessor()));
45044503
VPBuilder MiddleBuilder(MiddleVPBB, MiddleVPBB->getFirstNonPhi());
4505-
VPBuilder ScalarPHBuilder(ScalarPH);
4506-
for (VPRecipeBase &ScalarPhiR : Plan.getScalarHeader()->phis()) {
4507-
auto *ScalarPhiIRI = cast<VPIRPhi>(&ScalarPhiR);
4504+
for (VPRecipeBase &PhiR : Plan.getScalarPreheader()->phis()) {
4505+
auto *ResumePhiR = cast<VPPhi>(&PhiR);
45084506

45094507
// TODO: Extract final value from induction recipe initially, optimize to
45104508
// pre-computed end value together in optimizeInductionExitUsers.
4511-
auto *VectorPhiR =
4512-
cast<VPHeaderPHIRecipe>(Builder.getRecipe(&ScalarPhiIRI->getIRPhi()));
4509+
auto *VectorPhiR = cast<VPHeaderPHIRecipe>(ResumePhiR->getOperand(0));
45134510
if (auto *WideIVR = dyn_cast<VPWidenInductionRecipe>(VectorPhiR)) {
4514-
if (VPInstruction *ResumePhi = addResumePhiRecipeForInduction(
4515-
WideIVR, VectorPHBuilder, ScalarPHBuilder, TypeInfo,
4516-
&Plan.getVectorTripCount())) {
4517-
assert(isa<VPPhi>(ResumePhi) && "Expected a phi");
4518-
IVEndValues[WideIVR] = ResumePhi->getOperand(0);
4519-
ScalarPhiIRI->addOperand(ResumePhi);
4511+
if (VPValue *ResumeV = addResumePhiRecipeForInduction(
4512+
WideIVR, VectorPHBuilder, TypeInfo, &Plan.getVectorTripCount())) {
4513+
IVEndValues[WideIVR] = ResumeV;
4514+
ResumePhiR->setOperand(0, ResumeV);
4515+
ResumePhiR->setName("bc.resume.val");
45204516
continue;
45214517
}
45224518
// TODO: Also handle truncated inductions here. Computing end-values
@@ -4538,10 +4534,8 @@ void VPlanTransforms::addScalarResumePhis(
45384534
ResumeFromVectorLoop = MiddleBuilder.createNaryOp(
45394535
VPInstruction::ExtractLastElement, {ResumeFromVectorLoop}, {},
45404536
"vector.recur.extract");
4541-
StringRef Name = IsFOR ? "scalar.recur.init" : "bc.merge.rdx";
4542-
auto *ResumePhiR = ScalarPHBuilder.createScalarPhi(
4543-
{ResumeFromVectorLoop, VectorPhiR->getStartValue()}, {}, Name);
4544-
ScalarPhiIRI->addOperand(ResumePhiR);
4537+
ResumePhiR->setName(IsFOR ? "scalar.recur.init" : "bc.merge.rdx");
4538+
ResumePhiR->setOperand(0, ResumeFromVectorLoop);
45454539
}
45464540
}
45474541

llvm/unittests/Transforms/Vectorize/VPlanHCFGTest.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,13 @@ compound=true
113113
N0 -> N2 [ label="F"]
114114
N1 [label =
115115
"scalar.ph:\l" +
116+
" EMIT-SCALAR vp\<%6\> = phi [ ir\<%indvars.iv\>, middle.block ], [ ir\<0\>, ir-bb\<entry\> ]\l" +
116117
"Successor(s): ir-bb\<for.body\>\l"
117118
]
118119
N1 -> N3 [ label=""]
119120
N3 [label =
120121
"ir-bb\<for.body\>:\l" +
121-
" IR %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]\l" +
122+
" IR %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ] (extra operand: vp\<%6\> from scalar.ph)\l" +
122123
" IR %arr.idx = getelementptr inbounds i32, ptr %A, i64 %indvars.iv\l" +
123124
" IR %l1 = load i32, ptr %arr.idx, align 4\l" +
124125
" IR %res = add i32 %l1, 10\l" +
@@ -282,12 +283,13 @@ compound=true
282283
N0 -> N2 [ label="F"]
283284
N1 [label =
284285
"scalar.ph:\l" +
286+
" EMIT-SCALAR vp\<%6\> = phi [ ir\<%iv\>, middle.block ], [ ir\<0\>, ir-bb\<entry\> ]\l" +
285287
"Successor(s): ir-bb\<loop.header\>\l"
286288
]
287289
N1 -> N3 [ label=""]
288290
N3 [label =
289291
"ir-bb\<loop.header\>:\l" +
290-
" IR %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop.latch ]\l" +
292+
" IR %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop.latch ] (extra operand: vp\<%6\> from scalar.ph)\l" +
291293
" IR %arr.idx = getelementptr inbounds i32, ptr %A, i64 %iv\l" +
292294
" IR %l1 = load i32, ptr %arr.idx, align 4\l" +
293295
" IR %c = icmp eq i32 %l1, 0\l" +

llvm/unittests/Transforms/Vectorize/VPlanVerifierTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,8 @@ TEST_F(VPIRVerifierTest, testVerifyIRPhiInScalarHeaderVPIRBB) {
346346
Function *F = M.getFunction("f");
347347
BasicBlock *LoopHeader = F->getEntryBlock().getSingleSuccessor();
348348
auto Plan = buildVPlan(LoopHeader);
349+
VPValue *Zero = Plan->getConstantInt(32, 0);
350+
Plan->getScalarHeader()->front().addOperand(Zero);
349351

350352
#if GTEST_HAS_STREAM_REDIRECTION
351353
::testing::internal::CaptureStderr();
@@ -387,8 +389,6 @@ TEST_F(VPIRVerifierTest, testVerifyIRPhiInExitVPIRBB) {
387389
{HeaderBlock->front().getVPSingleValue()});
388390
DefI->insertBefore(Plan->getMiddleBlock()->getTerminator());
389391
Plan->getExitBlocks()[0]->front().addOperand(DefI);
390-
VPValue *Zero = Plan->getConstantInt(32, 0);
391-
Plan->getScalarHeader()->front().addOperand(Zero);
392392

393393
#if GTEST_HAS_STREAM_REDIRECTION
394394
::testing::internal::CaptureStderr();

0 commit comments

Comments
 (0)