Skip to content

Commit f688480

Browse files
authored
[VPlan] Manage Sentinel value for FindLastIV in VPlan. (#142291)
Similar to modeling the start value as operand, also model the sentinel value as operand explicitly. This makes all require information for code-gen available directly in VPlan. PR: #142291
1 parent 59388fb commit f688480

File tree

5 files changed

+37
-14
lines changed

5 files changed

+37
-14
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7266,9 +7266,11 @@ static void fixReductionScalarResumeWhenVectorizingEpilog(
72667266
using namespace llvm::PatternMatch;
72677267
Value *Cmp, *OrigResumeV, *CmpOp;
72687268
bool IsExpectedPattern =
7269-
match(MainResumeValue, m_Select(m_OneUse(m_Value(Cmp)),
7270-
m_Specific(RdxDesc.getSentinelValue()),
7271-
m_Value(OrigResumeV))) &&
7269+
match(MainResumeValue,
7270+
m_Select(
7271+
m_OneUse(m_Value(Cmp)),
7272+
m_Specific(EpiRedResult->getOperand(2)->getLiveInIRValue()),
7273+
m_Value(OrigResumeV))) &&
72727274
(match(Cmp, m_SpecificICmp(ICmpInst::ICMP_EQ, m_Specific(OrigResumeV),
72737275
m_Value(CmpOp))) &&
72747276
((CmpOp == StartV && isGuaranteedNotToBeUndefOrPoison(CmpOp))));
@@ -9235,9 +9237,11 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
92359237
if (RecurrenceDescriptor::isFindLastIVRecurrenceKind(
92369238
RdxDesc.getRecurrenceKind())) {
92379239
VPValue *Start = PhiR->getStartValue();
9238-
FinalReductionResult =
9239-
Builder.createNaryOp(VPInstruction::ComputeFindLastIVResult,
9240-
{PhiR, Start, NewExitingVPV}, ExitDL);
9240+
FinalReductionResult = Builder.createNaryOp(
9241+
VPInstruction::ComputeFindLastIVResult,
9242+
{PhiR, Start, Plan->getOrAddLiveIn(RdxDesc.getSentinelValue()),
9243+
NewExitingVPV},
9244+
ExitDL);
92419245
} else if (RecurrenceDescriptor::isAnyOfRecurrenceKind(
92429246
RdxDesc.getRecurrenceKind())) {
92439247
VPValue *Start = PhiR->getStartValue();
@@ -9825,8 +9829,8 @@ preparePlanForEpilogueVectorLoop(VPlan &Plan, Loop *L,
98259829
BasicBlock *ResumeBB = cast<Instruction>(ResumeV)->getParent();
98269830
IRBuilder<> Builder(ResumeBB, ResumeBB->getFirstNonPHIIt());
98279831
Value *Cmp = Builder.CreateICmpEQ(ResumeV, ToFrozen[StartV]);
9828-
ResumeV =
9829-
Builder.CreateSelect(Cmp, RdxDesc.getSentinelValue(), ResumeV);
9832+
ResumeV = Builder.CreateSelect(
9833+
Cmp, RdxResult->getOperand(2)->getLiveInIRValue(), ResumeV);
98309834
} else {
98319835
VPValue *StartVal = Plan.getOrAddLiveIn(ResumeV);
98329836
auto *PhiR = dyn_cast<VPReductionPHIRecipe>(&R);

llvm/lib/Transforms/Vectorize/VPlanPatternMatch.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,25 @@ m_VPInstruction(const Op0_t &Op0, const Op1_t &Op1, const Op2_t &Op2) {
318318
{Op0, Op1, Op2});
319319
}
320320

321+
template <typename Op0_t, typename Op1_t, typename Op2_t, typename Op3_t,
322+
unsigned Opcode, bool Commutative, typename... RecipeTys>
323+
using Recipe4Op_match = Recipe_match<std::tuple<Op0_t, Op1_t, Op2_t, Op3_t>,
324+
Opcode, Commutative, RecipeTys...>;
325+
326+
template <typename Op0_t, typename Op1_t, typename Op2_t, typename Op3_t,
327+
unsigned Opcode>
328+
using VPInstruction4Op_match =
329+
Recipe4Op_match<Op0_t, Op1_t, Op2_t, Op3_t, Opcode, /*Commutative*/ false,
330+
VPInstruction>;
331+
332+
template <unsigned Opcode, typename Op0_t, typename Op1_t, typename Op2_t,
333+
typename Op3_t>
334+
inline VPInstruction4Op_match<Op0_t, Op1_t, Op2_t, Op3_t, Opcode>
335+
m_VPInstruction(const Op0_t &Op0, const Op1_t &Op1, const Op2_t &Op2,
336+
const Op3_t &Op3) {
337+
return VPInstruction4Op_match<Op0_t, Op1_t, Op2_t, Op3_t, Opcode>(
338+
{Op0, Op1, Op2, Op3});
339+
}
321340
template <typename Op0_t>
322341
inline UnaryVPInstruction_match<Op0_t, Instruction::Freeze>
323342
m_Freeze(const Op0_t &Op0) {

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -645,16 +645,16 @@ Value *VPInstruction::generate(VPTransformState &State) {
645645

646646
// The recipe's operands are the reduction phi, followed by one operand for
647647
// each part of the reduction.
648-
unsigned UF = getNumOperands() - 2;
649-
Value *ReducedPartRdx = State.get(getOperand(2));
648+
unsigned UF = getNumOperands() - 3;
649+
Value *ReducedPartRdx = State.get(getOperand(3));
650650
for (unsigned Part = 1; Part < UF; ++Part) {
651651
ReducedPartRdx = createMinMaxOp(Builder, RecurKind::SMax, ReducedPartRdx,
652-
State.get(getOperand(2 + Part)));
652+
State.get(getOperand(3 + Part)));
653653
}
654654

655655
return createFindLastIVReduction(Builder, ReducedPartRdx,
656656
State.get(getOperand(1), true),
657-
RdxDesc.getSentinelValue());
657+
getOperand(2)->getLiveInIRValue());
658658
}
659659
case VPInstruction::ComputeReductionResult: {
660660
// FIXME: The cross-recipe dependency on VPReductionPHIRecipe is temporary

llvm/lib/Transforms/Vectorize/VPlanUnroll.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ void UnrollState::unrollBlock(VPBlockBase *VPB) {
347347
match(&R, m_VPInstruction<VPInstruction::ComputeReductionResult>(
348348
m_VPValue(), m_VPValue(Op1))) ||
349349
match(&R, m_VPInstruction<VPInstruction::ComputeFindLastIVResult>(
350-
m_VPValue(), m_VPValue(), m_VPValue(Op1)))) {
350+
m_VPValue(), m_VPValue(), m_VPValue(), m_VPValue(Op1)))) {
351351
addUniformForAllParts(cast<VPInstruction>(&R));
352352
for (unsigned Part = 1; Part != UF; ++Part)
353353
R.addOperand(getValueForPart(Op1, Part));

llvm/test/Transforms/LoopVectorize/vplan-printing-reductions.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ define i64 @find_last_iv(ptr %a, i64 %n, i64 %start) {
240240
; CHECK-NEXT: Successor(s): middle.block
241241
; CHECK-EMPTY:
242242
; CHECK-NEXT: middle.block:
243-
; CHECK-NEXT: EMIT vp<[[RDX_RES:%.+]]> = compute-find-last-iv-result ir<%rdx>, ir<%start>, ir<%cond>
243+
; CHECK-NEXT: EMIT vp<[[RDX_RES:%.+]]> = compute-find-last-iv-result ir<%rdx>, ir<%start>, ir<-9223372036854775808>, ir<%cond>
244244
; CHECK-NEXT: EMIT vp<[[EXT:%.+]]> = extract-last-element vp<[[RDX_RES]]>
245245
; CHECK-NEXT: EMIT vp<%cmp.n> = icmp eq ir<%n>, vp<{{.+}}>
246246
; CHECK-NEXT: EMIT branch-on-cond vp<%cmp.n>

0 commit comments

Comments
 (0)