Skip to content

Commit c551166

Browse files
committed
[VPlan] Manage FindLastIV start value in ComputeFindLastIVResult (NFC).
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.
1 parent 3e42683 commit c551166

File tree

6 files changed

+23
-15
lines changed

6 files changed

+23
-15
lines changed

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

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

429429
/// 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
@@ -1233,11 +1233,11 @@ Value *llvm::createAnyOfReduction(IRBuilderBase &Builder, Value *Src,
12331233
}
12341234

12351235
Value *llvm::createFindLastIVReduction(IRBuilderBase &Builder, Value *Src,
1236+
Value *Start,
12361237
const RecurrenceDescriptor &Desc) {
12371238
assert(RecurrenceDescriptor::isFindLastIVRecurrenceKind(
12381239
Desc.getRecurrenceKind()) &&
12391240
"Unexpected reduction kind");
1240-
Value *StartVal = Desc.getRecurrenceStartValue();
12411241
Value *Sentinel = Desc.getSentinelValue();
12421242
Value *MaxRdx = Src->getType()->isVectorTy()
12431243
? Builder.CreateIntMaxReduce(Src, true)
@@ -1246,7 +1246,7 @@ Value *llvm::createFindLastIVReduction(IRBuilderBase &Builder, Value *Src,
12461246
// reduction is sentinel value.
12471247
Value *Cmp =
12481248
Builder.CreateCmp(CmpInst::ICMP_NE, MaxRdx, Sentinel, "rdx.select.cmp");
1249-
return Builder.CreateSelect(Cmp, MaxRdx, StartVal, "rdx.select");
1249+
return Builder.CreateSelect(Cmp, MaxRdx, Start, "rdx.select");
12501250
}
12511251

12521252
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
@@ -9864,14 +9864,19 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
98649864
// bc.merge.rdx phi nodes, hence it needs to be created unconditionally here
98659865
// even for in-loop reductions, until the reduction resume value handling is
98669866
// also modeled in VPlan.
9867+
VPInstruction *FinalReductionResult;
98679868
VPBuilder::InsertPointGuard Guard(Builder);
98689869
Builder.setInsertPoint(MiddleVPBB, IP);
9869-
auto *FinalReductionResult =
9870-
Builder.createNaryOp(RecurrenceDescriptor::isFindLastIVRecurrenceKind(
9871-
RdxDesc.getRecurrenceKind())
9872-
? VPInstruction::ComputeFindLastIVResult
9873-
: VPInstruction::ComputeReductionResult,
9874-
{PhiR, NewExitingVPV}, ExitDL);
9870+
if (RecurrenceDescriptor::isFindLastIVRecurrenceKind(
9871+
RdxDesc.getRecurrenceKind())) {
9872+
VPValue *Start = PhiR->getStartValue();
9873+
FinalReductionResult =
9874+
Builder.createNaryOp(VPInstruction::ComputeFindLastIVResult,
9875+
{PhiR, Start, NewExitingVPV}, ExitDL);
9876+
} else {
9877+
FinalReductionResult = Builder.createNaryOp(
9878+
VPInstruction::ComputeReductionResult, {PhiR, NewExitingVPV}, ExitDL);
9879+
}
98759880
// Update all users outside the vector region.
98769881
OrigExitingVPV->replaceUsesWithIf(
98779882
FinalReductionResult, [FinalReductionResult](VPUser &User, unsigned) {

llvm/lib/Transforms/Vectorize/VPlanAnalysis.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Type *VPTypeAnalysis::inferScalarTypeForRecipe(const VPInstruction *R) {
5151

5252
switch (Opcode) {
5353
case Instruction::ExtractElement:
54+
case Instruction::Freeze:
5455
return inferScalarType(R->getOperand(0));
5556
case Instruction::Select: {
5657
Type *ResTy = inferScalarType(R->getOperand(1));

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -617,11 +617,11 @@ Value *VPInstruction::generate(VPTransformState &State) {
617617
case VPInstruction::ComputeFindLastIVResult: {
618618
// The recipe's operands are the reduction phi, followed by one operand for
619619
// each part of the reduction.
620-
unsigned UF = getNumOperands() - 1;
621-
Value *ReducedPartRdx = State.get(getOperand(1));
620+
unsigned UF = getNumOperands() - 2;
621+
Value *ReducedPartRdx = State.get(getOperand(2));
622622
for (unsigned Part = 1; Part < UF; ++Part) {
623623
ReducedPartRdx = createMinMaxOp(Builder, RecurKind::SMax, ReducedPartRdx,
624-
State.get(getOperand(1 + Part)));
624+
State.get(getOperand(2 + Part)));
625625
}
626626

627627
// FIXME: The cross-recipe dependency on VPReductionPHIRecipe is temporary
@@ -633,7 +633,8 @@ Value *VPInstruction::generate(VPTransformState &State) {
633633

634634
assert(RecurrenceDescriptor::isFindLastIVRecurrenceKind(RK));
635635
assert(!PhiR->isInLoop());
636-
return createFindLastIVReduction(Builder, ReducedPartRdx, RdxDesc);
636+
return createFindLastIVReduction(Builder, ReducedPartRdx,
637+
State.get(getOperand(1), true), RdxDesc);
637638
}
638639
case VPInstruction::ComputeReductionResult: {
639640
// FIXME: The cross-recipe dependency on VPReductionPHIRecipe is temporary
@@ -950,6 +951,8 @@ bool VPInstruction::onlyFirstLaneUsed(const VPValue *Op) const {
950951
return true;
951952
case VPInstruction::PtrAdd:
952953
return Op == getOperand(0) || vputils::onlyFirstLaneUsed(this);
954+
case VPInstruction::ComputeFindLastIVResult:
955+
return Op == getOperand(1);
953956
};
954957
llvm_unreachable("switch should return");
955958
}
@@ -1591,7 +1594,6 @@ void VPWidenRecipe::execute(VPTransformState &State) {
15911594
}
15921595
case Instruction::Freeze: {
15931596
Value *Op = State.get(getOperand(0));
1594-
15951597
Value *Freeze = Builder.CreateFreeze(Op);
15961598
State.set(this, Freeze);
15971599
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));

0 commit comments

Comments
 (0)