Skip to content

Commit 07699e3

Browse files
fhahnpawosm-arm
authored andcommitted
[VPlan] Manage FindLastIV start value in ComputeFindLastIVResult (NFC) (llvm#132690)
Keep the start value as operand of ComputeFindLastIVResult. A follow-up patch will use this to make sure the start value is frozen if needed. Depends on llvm#132689 PR: llvm#132690
1 parent 3675c5f commit 07699e3

File tree

8 files changed

+42
-16
lines changed

8 files changed

+42
-16
lines changed

llvm/include/llvm/Transforms/Utils/LoopUtils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ Value *createAnyOfReduction(IRBuilderBase &B, Value *Src,
422422
/// Create a reduction of the given vector \p Src for a reduction of the
423423
/// kind RecurKind::IFindLastIV or RecurKind::FFindLastIV. The reduction
424424
/// operation is described by \p Desc.
425-
Value *createFindLastIVReduction(IRBuilderBase &B, Value *Src,
425+
Value *createFindLastIVReduction(IRBuilderBase &B, Value *Src, Value *Start,
426426
const RecurrenceDescriptor &Desc);
427427

428428
/// Create an ordered reduction intrinsic using the given recurrence

llvm/lib/Transforms/Utils/LoopUtils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,11 +1209,11 @@ Value *llvm::createAnyOfReduction(IRBuilderBase &Builder, Value *Src,
12091209
}
12101210

12111211
Value *llvm::createFindLastIVReduction(IRBuilderBase &Builder, Value *Src,
1212+
Value *Start,
12121213
const RecurrenceDescriptor &Desc) {
12131214
assert(RecurrenceDescriptor::isFindLastIVRecurrenceKind(
12141215
Desc.getRecurrenceKind()) &&
12151216
"Unexpected reduction kind");
1216-
Value *StartVal = Desc.getRecurrenceStartValue();
12171217
Value *Sentinel = Desc.getSentinelValue();
12181218
Value *MaxRdx = Src->getType()->isVectorTy()
12191219
? Builder.CreateIntMaxReduce(Src, true)
@@ -1222,7 +1222,7 @@ Value *llvm::createFindLastIVReduction(IRBuilderBase &Builder, Value *Src,
12221222
// reduction is sentinel value.
12231223
Value *Cmp =
12241224
Builder.CreateCmp(CmpInst::ICMP_NE, MaxRdx, Sentinel, "rdx.select.cmp");
1225-
return Builder.CreateSelect(Cmp, MaxRdx, StartVal, "rdx.select");
1225+
return Builder.CreateSelect(Cmp, MaxRdx, Start, "rdx.select");
12261226
}
12271227

12281228
Value *llvm::getReductionIdentity(Intrinsic::ID RdxID, Type *Ty,

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9690,14 +9690,19 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
96909690
// bc.merge.rdx phi nodes, hence it needs to be created unconditionally here
96919691
// even for in-loop reductions, until the reduction resume value handling is
96929692
// also modeled in VPlan.
9693+
VPInstruction *FinalReductionResult;
96939694
VPBuilder::InsertPointGuard Guard(Builder);
96949695
Builder.setInsertPoint(MiddleVPBB, IP);
9695-
auto *FinalReductionResult =
9696-
Builder.createNaryOp(RecurrenceDescriptor::isFindLastIVRecurrenceKind(
9697-
RdxDesc.getRecurrenceKind())
9698-
? VPInstruction::ComputeFindLastIVResult
9699-
: VPInstruction::ComputeReductionResult,
9700-
{PhiR, NewExitingVPV}, ExitDL);
9696+
if (RecurrenceDescriptor::isFindLastIVRecurrenceKind(
9697+
RdxDesc.getRecurrenceKind())) {
9698+
VPValue *Start = PhiR->getStartValue();
9699+
FinalReductionResult =
9700+
Builder.createNaryOp(VPInstruction::ComputeFindLastIVResult,
9701+
{PhiR, Start, NewExitingVPV}, ExitDL);
9702+
} else {
9703+
FinalReductionResult = Builder.createNaryOp(
9704+
VPInstruction::ComputeReductionResult, {PhiR, NewExitingVPV}, ExitDL);
9705+
}
97019706
// Update all users outside the vector region.
97029707
OrigExitingVPV->replaceUsesWithIf(
97039708
FinalReductionResult, [FinalReductionResult](VPUser &User, unsigned) {

llvm/lib/Transforms/Vectorize/VPlanAnalysis.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ Type *VPTypeAnalysis::inferScalarTypeForRecipe(const VPInstruction *R) {
5050
return SetResultTyFromOp();
5151

5252
switch (Opcode) {
53+
case Instruction::Freeze:
54+
return inferScalarType(R->getOperand(0));
5355
case Instruction::Select: {
5456
Type *ResTy = inferScalarType(R->getOperand(1));
5557
VPValue *OtherV = R->getOperand(2);

llvm/lib/Transforms/Vectorize/VPlanPatternMatch.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,16 @@ using BinaryVPInstruction_match =
233233
BinaryRecipe_match<Op0_t, Op1_t, Opcode, /*Commutative*/ false,
234234
VPInstruction>;
235235

236+
template <typename Op0_t, typename Op1_t, typename Op2_t, unsigned Opcode,
237+
bool Commutative, typename... RecipeTys>
238+
using TernaryRecipe_match = Recipe_match<std::tuple<Op0_t, Op1_t, Op2_t>,
239+
Opcode, Commutative, RecipeTys...>;
240+
241+
template <typename Op0_t, typename Op1_t, typename Op2_t, unsigned Opcode>
242+
using TernaryVPInstruction_match =
243+
TernaryRecipe_match<Op0_t, Op1_t, Op2_t, Opcode, /*Commutative*/ false,
244+
VPInstruction>;
245+
236246
template <typename Op0_t, typename Op1_t, unsigned Opcode,
237247
bool Commutative = false>
238248
using AllBinaryRecipe_match =
@@ -251,6 +261,13 @@ m_VPInstruction(const Op0_t &Op0, const Op1_t &Op1) {
251261
return BinaryVPInstruction_match<Op0_t, Op1_t, Opcode>(Op0, Op1);
252262
}
253263

264+
template <unsigned Opcode, typename Op0_t, typename Op1_t, typename Op2_t>
265+
inline TernaryVPInstruction_match<Op0_t, Op1_t, Op2_t, Opcode>
266+
m_VPInstruction(const Op0_t &Op0, const Op1_t &Op1, const Op2_t &Op2) {
267+
return TernaryVPInstruction_match<Op0_t, Op1_t, Op2_t, Opcode>(
268+
{Op0, Op1, Op2});
269+
}
270+
254271
template <typename Op0_t>
255272
inline UnaryVPInstruction_match<Op0_t, VPInstruction::Not>
256273
m_Not(const Op0_t &Op0) {

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -589,14 +589,15 @@ Value *VPInstruction::generate(VPTransformState &State) {
589589

590590
// The recipe's operands are the reduction phi, followed by one operand for
591591
// each part of the reduction.
592-
unsigned UF = getNumOperands() - 1;
593-
Value *ReducedPartRdx = State.get(getOperand(1));
592+
unsigned UF = getNumOperands() - 2;
593+
Value *ReducedPartRdx = State.get(getOperand(2));
594594
for (unsigned Part = 1; Part < UF; ++Part) {
595595
ReducedPartRdx = createMinMaxOp(Builder, RecurKind::SMax, ReducedPartRdx,
596-
State.get(getOperand(1 + Part)));
596+
State.get(getOperand(2 + Part)));
597597
}
598598

599-
return createFindLastIVReduction(Builder, ReducedPartRdx, RdxDesc);
599+
return createFindLastIVReduction(Builder, ReducedPartRdx,
600+
State.get(getOperand(1), true), RdxDesc);
600601
}
601602
case VPInstruction::ComputeReductionResult: {
602603
// FIXME: The cross-recipe dependency on VPReductionPHIRecipe is temporary
@@ -857,6 +858,8 @@ bool VPInstruction::onlyFirstLaneUsed(const VPValue *Op) const {
857858
case VPInstruction::BranchOnCond:
858859
case VPInstruction::ResumePhi:
859860
return true;
861+
case VPInstruction::ComputeFindLastIVResult:
862+
return Op == getOperand(1);
860863
};
861864
llvm_unreachable("switch should return");
862865
}
@@ -1483,7 +1486,6 @@ void VPWidenRecipe::execute(VPTransformState &State) {
14831486
}
14841487
case Instruction::Freeze: {
14851488
Value *Op = State.get(getOperand(0));
1486-
14871489
Value *Freeze = Builder.CreateFreeze(Op);
14881490
State.set(this, Freeze);
14891491
break;

llvm/lib/Transforms/Vectorize/VPlanUnroll.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ void UnrollState::unrollBlock(VPBlockBase *VPB) {
350350
if (match(&R, m_VPInstruction<VPInstruction::ComputeReductionResult>(
351351
m_VPValue(), m_VPValue(Op1))) ||
352352
match(&R, m_VPInstruction<VPInstruction::ComputeFindLastIVResult>(
353-
m_VPValue(), m_VPValue(Op1)))) {
353+
m_VPValue(), m_VPValue(), m_VPValue(Op1)))) {
354354
addUniformForAllParts(cast<VPInstruction>(&R));
355355
for (unsigned Part = 1; Part != UF; ++Part)
356356
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
@@ -234,7 +234,7 @@ define i64 @find_last_iv(ptr %a, i64 %n, i64 %start) {
234234
; CHECK-NEXT: Successor(s): middle.block
235235
; CHECK-EMPTY:
236236
; CHECK-NEXT: middle.block:
237-
; CHECK-NEXT: EMIT vp<[[RDX_RES:%.+]]> = compute-find-last-iv-result ir<%rdx>, ir<%cond>
237+
; CHECK-NEXT: EMIT vp<[[RDX_RES:%.+]]> = compute-find-last-iv-result ir<%rdx>, ir<%start>, ir<%cond>
238238
; CHECK-NEXT: EMIT vp<[[EXT:%.+]]> = extract-from-end vp<[[RDX_RES]]>, ir<1>
239239
; CHECK-NEXT: EMIT vp<%cmp.n> = icmp eq ir<%n>, vp<{{.+}}>
240240
; CHECK-NEXT: EMIT branch-on-cond vp<%cmp.n>

0 commit comments

Comments
 (0)