Skip to content

Commit 2c86301

Browse files
committed
[VPlan] Manage Sentinel value for FindLastIV in VPlan.
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.
1 parent 4a6d31f commit 2c86301

File tree

5 files changed

+38
-14
lines changed

5 files changed

+38
-14
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7262,9 +7262,11 @@ static void fixReductionScalarResumeWhenVectorizingEpilog(
72627262
using namespace llvm::PatternMatch;
72637263
Value *Cmp, *OrigResumeV, *CmpOp;
72647264
bool IsExpectedPattern =
7265-
match(MainResumeValue, m_Select(m_OneUse(m_Value(Cmp)),
7266-
m_Specific(RdxDesc.getSentinelValue()),
7267-
m_Value(OrigResumeV))) &&
7265+
match(MainResumeValue,
7266+
m_Select(
7267+
m_OneUse(m_Value(Cmp)),
7268+
m_Specific(EpiRedResult->getOperand(2)->getLiveInIRValue()),
7269+
m_Value(OrigResumeV))) &&
72687270
(match(Cmp, m_SpecificICmp(ICmpInst::ICMP_EQ, m_Specific(OrigResumeV),
72697271
m_Value(CmpOp))) &&
72707272
((CmpOp == StartV && isGuaranteedNotToBeUndefOrPoison(CmpOp))));
@@ -9242,9 +9244,11 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
92429244
if (RecurrenceDescriptor::isFindLastIVRecurrenceKind(
92439245
RdxDesc.getRecurrenceKind())) {
92449246
VPValue *Start = PhiR->getStartValue();
9245-
FinalReductionResult =
9246-
Builder.createNaryOp(VPInstruction::ComputeFindLastIVResult,
9247-
{PhiR, Start, NewExitingVPV}, ExitDL);
9247+
FinalReductionResult = Builder.createNaryOp(
9248+
VPInstruction::ComputeFindLastIVResult,
9249+
{PhiR, Start, Plan->getOrAddLiveIn(RdxDesc.getSentinelValue()),
9250+
NewExitingVPV},
9251+
ExitDL);
92489252
} else if (RecurrenceDescriptor::isAnyOfRecurrenceKind(
92499253
RdxDesc.getRecurrenceKind())) {
92509254
VPValue *Start = PhiR->getStartValue();
@@ -9832,8 +9836,9 @@ preparePlanForEpilogueVectorLoop(VPlan &Plan, Loop *L,
98329836
BasicBlock *ResumeBB = cast<Instruction>(ResumeV)->getParent();
98339837
IRBuilder<> Builder(ResumeBB, ResumeBB->getFirstNonPHIIt());
98349838
Value *Cmp = Builder.CreateICmpEQ(ResumeV, ToFrozen[StartV]);
9835-
ResumeV =
9836-
Builder.CreateSelect(Cmp, RdxDesc.getSentinelValue(), ResumeV);
9839+
ResumeV = Builder.CreateSelect(
9840+
Cmp, RdxResult->getOperand(2)->getLiveInIRValue(), ResumeV);
9841+
98379842
} else {
98389843
VPValue *StartVal = Plan.getOrAddLiveIn(ResumeV);
98399844
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
@@ -646,16 +646,16 @@ Value *VPInstruction::generate(VPTransformState &State) {
646646

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

656656
return createFindLastIVReduction(Builder, ReducedPartRdx,
657657
State.get(getOperand(1), true),
658-
RdxDesc.getSentinelValue());
658+
getOperand(2)->getLiveInIRValue());
659659
}
660660
case VPInstruction::ComputeReductionResult: {
661661
// 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)