Skip to content

Commit b709387

Browse files
committed
[VPlan] Add start VPV to compute-reduction-result.
1 parent b68565b commit b709387

File tree

6 files changed

+76
-23
lines changed

6 files changed

+76
-23
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7485,6 +7485,13 @@ static void addRuntimeUnrollDisableMetaData(Loop *L) {
74857485
}
74867486
}
74877487

7488+
static Value *getStartValueFromReductionResult(VPInstruction *RdxResult) {
7489+
using namespace VPlanPatternMatch;
7490+
VPValue *StartVPV = RdxResult->getOperand(1);
7491+
match(StartVPV, m_Freeze(m_VPValue(StartVPV)));
7492+
return StartVPV->getLiveInIRValue();
7493+
}
7494+
74887495
// If \p R is a ComputeReductionResult when vectorizing the epilog loop,
74897496
// fix the reduction's scalar PHI node by adding the incoming value from the
74907497
// main vector loop.
@@ -7493,7 +7500,8 @@ static void fixReductionScalarResumeWhenVectorizingEpilog(
74937500
BasicBlock *BypassBlock) {
74947501
auto *EpiRedResult = dyn_cast<VPInstruction>(R);
74957502
if (!EpiRedResult ||
7496-
(EpiRedResult->getOpcode() != VPInstruction::ComputeReductionResult &&
7503+
(EpiRedResult->getOpcode() != VPInstruction::ComputeAnyOfResult &&
7504+
EpiRedResult->getOpcode() != VPInstruction::ComputeReductionResult &&
74977505
EpiRedResult->getOpcode() != VPInstruction::ComputeFindLastIVResult))
74987506
return;
74997507

@@ -7505,15 +7513,19 @@ static void fixReductionScalarResumeWhenVectorizingEpilog(
75057513
EpiRedHeaderPhi->getStartValue()->getUnderlyingValue();
75067514
if (RecurrenceDescriptor::isAnyOfRecurrenceKind(
75077515
RdxDesc.getRecurrenceKind())) {
7516+
Value *StartV = EpiRedResult->getOperand(1)->getLiveInIRValue();
7517+
(void)StartV;
75087518
auto *Cmp = cast<ICmpInst>(MainResumeValue);
75097519
assert(Cmp->getPredicate() == CmpInst::ICMP_NE &&
75107520
"AnyOf expected to start with ICMP_NE");
7511-
assert(Cmp->getOperand(1) == RdxDesc.getRecurrenceStartValue() &&
7521+
assert(Cmp->getOperand(1) == StartV &&
75127522
"AnyOf expected to start by comparing main resume value to original "
75137523
"start value");
75147524
MainResumeValue = Cmp->getOperand(0);
75157525
} else if (RecurrenceDescriptor::isFindLastIVRecurrenceKind(
75167526
RdxDesc.getRecurrenceKind())) {
7527+
Value *StartV = getStartValueFromReductionResult(EpiRedResult);
7528+
(void)StartV;
75177529
using namespace llvm::PatternMatch;
75187530
Value *Cmp, *OrigResumeV, *CmpOp;
75197531
bool IsExpectedPattern =
@@ -7522,10 +7534,7 @@ static void fixReductionScalarResumeWhenVectorizingEpilog(
75227534
m_Value(OrigResumeV))) &&
75237535
(match(Cmp, m_SpecificICmp(ICmpInst::ICMP_EQ, m_Specific(OrigResumeV),
75247536
m_Value(CmpOp))) &&
7525-
(match(CmpOp,
7526-
m_Freeze(m_Specific(RdxDesc.getRecurrenceStartValue()))) ||
7527-
(CmpOp == RdxDesc.getRecurrenceStartValue() &&
7528-
isGuaranteedNotToBeUndefOrPoison(CmpOp))));
7537+
((CmpOp == StartV && isGuaranteedNotToBeUndefOrPoison(CmpOp))));
75297538
assert(IsExpectedPattern && "Unexpected reduction resume pattern");
75307539
(void)IsExpectedPattern;
75317540
MainResumeValue = OrigResumeV;
@@ -9460,7 +9469,10 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
94609469
OrigExitingVPV->replaceUsesWithIf(NewExitingVPV, [](VPUser &U, unsigned) {
94619470
return isa<VPInstruction>(&U) &&
94629471
(cast<VPInstruction>(&U)->getOpcode() ==
9472+
VPInstruction::ComputeAnyOfResult ||
9473+
cast<VPInstruction>(&U)->getOpcode() ==
94639474
VPInstruction::ComputeReductionResult ||
9475+
94649476
cast<VPInstruction>(&U)->getOpcode() ==
94659477
VPInstruction::ComputeFindLastIVResult);
94669478
});
@@ -9512,6 +9524,12 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
95129524
FinalReductionResult =
95139525
Builder.createNaryOp(VPInstruction::ComputeFindLastIVResult,
95149526
{PhiR, Start, NewExitingVPV}, ExitDL);
9527+
} else if (RecurrenceDescriptor::isAnyOfRecurrenceKind(
9528+
RdxDesc.getRecurrenceKind())) {
9529+
VPValue *Start = PhiR->getStartValue();
9530+
FinalReductionResult =
9531+
Builder.createNaryOp(VPInstruction::ComputeAnyOfResult,
9532+
{PhiR, Start, NewExitingVPV}, ExitDL);
95159533
} else {
95169534
VPIRFlags Flags = RecurrenceDescriptor::isFloatingPointRecurrenceKind(
95179535
RdxDesc.getRecurrenceKind())
@@ -10040,23 +10058,36 @@ preparePlanForEpilogueVectorLoop(VPlan &Plan, Loop *L,
1004010058
Value *ResumeV = nullptr;
1004110059
// TODO: Move setting of resume values to prepareToExecute.
1004210060
if (auto *ReductionPhi = dyn_cast<VPReductionPHIRecipe>(&R)) {
10061+
auto *RdxResult =
10062+
cast<VPInstruction>(*find_if(ReductionPhi->users(), [](VPUser *U) {
10063+
auto *VPI = dyn_cast<VPInstruction>(U);
10064+
return VPI &&
10065+
(VPI->getOpcode() == VPInstruction::ComputeReductionResult ||
10066+
VPI->getOpcode() == VPInstruction::ComputeFindLastIVResult);
10067+
}));
1004310068
ResumeV = cast<PHINode>(ReductionPhi->getUnderlyingInstr())
1004410069
->getIncomingValueForBlock(L->getLoopPreheader());
1004510070
const RecurrenceDescriptor &RdxDesc =
1004610071
ReductionPhi->getRecurrenceDescriptor();
1004710072
RecurKind RK = RdxDesc.getRecurrenceKind();
1004810073
if (RecurrenceDescriptor::isAnyOfRecurrenceKind(RK)) {
10074+
Value *StartV = RdxResult->getOperand(1)->getLiveInIRValue();
10075+
assert(RdxDesc.getRecurrenceStartValue() == StartV &&
10076+
"start value from ComputeAnyOfResult must match");
10077+
1004910078
// VPReductionPHIRecipes for AnyOf reductions expect a boolean as
1005010079
// start value; compare the final value from the main vector loop
1005110080
// to the start value.
1005210081
BasicBlock *PBB = cast<Instruction>(ResumeV)->getParent();
1005310082
IRBuilder<> Builder(PBB, PBB->getFirstNonPHIIt());
10054-
ResumeV =
10055-
Builder.CreateICmpNE(ResumeV, RdxDesc.getRecurrenceStartValue());
10083+
ResumeV = Builder.CreateICmpNE(ResumeV, StartV);
1005610084
} else if (RecurrenceDescriptor::isFindLastIVRecurrenceKind(RK)) {
10057-
ToFrozen[RdxDesc.getRecurrenceStartValue()] =
10058-
cast<PHINode>(ResumeV)->getIncomingValueForBlock(
10059-
EPI.MainLoopIterationCountCheck);
10085+
Value *StartV = getStartValueFromReductionResult(RdxResult);
10086+
assert(RdxDesc.getRecurrenceStartValue() == StartV &&
10087+
"start value from ComputeFindLastIVResult must match");
10088+
10089+
ToFrozen[StartV] = cast<PHINode>(ResumeV)->getIncomingValueForBlock(
10090+
EPI.MainLoopIterationCountCheck);
1006010091

1006110092
// VPReductionPHIRecipe for FindLastIV reductions requires an adjustment
1006210093
// to the resume value. The resume value is adjusted to the sentinel
@@ -10066,8 +10097,7 @@ preparePlanForEpilogueVectorLoop(VPlan &Plan, Loop *L,
1006610097
// variable.
1006710098
BasicBlock *ResumeBB = cast<Instruction>(ResumeV)->getParent();
1006810099
IRBuilder<> Builder(ResumeBB, ResumeBB->getFirstNonPHIIt());
10069-
Value *Cmp = Builder.CreateICmpEQ(
10070-
ResumeV, ToFrozen[RdxDesc.getRecurrenceStartValue()]);
10100+
Value *Cmp = Builder.CreateICmpEQ(ResumeV, ToFrozen[StartV]);
1007110101
ResumeV =
1007210102
Builder.CreateSelect(Cmp, RdxDesc.getSentinelValue(), ResumeV);
1007310103
}

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,7 @@ class VPInstruction : public VPRecipeWithIRFlags,
907907
BranchOnCount,
908908
BranchOnCond,
909909
Broadcast,
910+
ComputeAnyOfResult,
910911
ComputeFindLastIVResult,
911912
ComputeReductionResult,
912913
// Extracts the last lane from its operand if it is a vector, or the last

llvm/lib/Transforms/Vectorize/VPlanAnalysis.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ Type *VPTypeAnalysis::inferScalarTypeForRecipe(const VPInstruction *R) {
8787
inferScalarType(R->getOperand(1)) &&
8888
"different types inferred for different operands");
8989
return IntegerType::get(Ctx, 1);
90+
case VPInstruction::ComputeAnyOfResult:
91+
return inferScalarType(R->getOperand(1));
9092
case VPInstruction::ComputeFindLastIVResult:
9193
case VPInstruction::ComputeReductionResult: {
9294
auto *PhiR = cast<VPReductionPHIRecipe>(R->getOperand(0));

llvm/lib/Transforms/Vectorize/VPlanPatternMatch.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,12 @@ 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>
322+
inline UnaryVPInstruction_match<Op0_t, Instruction::Freeze>
323+
m_Freeze(const Op0_t &Op0) {
324+
return m_VPInstruction<Instruction::Freeze>(Op0);
325+
}
326+
321327
template <typename Op0_t>
322328
inline UnaryVPInstruction_match<Op0_t, VPInstruction::Not>
323329
m_Not(const Op0_t &Op0) {

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,20 @@ Value *VPInstruction::generate(VPTransformState &State) {
604604
return Builder.CreateVectorSplat(
605605
State.VF, State.get(getOperand(0), /*IsScalar*/ true), "broadcast");
606606
}
607+
case VPInstruction::ComputeAnyOfResult: {
608+
// FIXME: The cross-recipe dependency on VPReductionPHIRecipe is temporary
609+
// and will be removed by breaking up the recipe further.
610+
auto *PhiR = cast<VPReductionPHIRecipe>(getOperand(0));
611+
auto *OrigPhi = cast<PHINode>(PhiR->getUnderlyingValue());
612+
Value *ReducedPartRdx = State.get(getOperand(2));
613+
for (unsigned Idx = 3; Idx < getNumOperands(); ++Idx)
614+
ReducedPartRdx = Builder.CreateBinOp(
615+
(Instruction::BinaryOps)RecurrenceDescriptor::getOpcode(
616+
RecurKind::AnyOf),
617+
State.get(getOperand(Idx)), ReducedPartRdx, "bin.rdx");
618+
return createAnyOfReduction(Builder, ReducedPartRdx,
619+
State.get(getOperand(1), VPLane(0)), OrigPhi);
620+
}
607621
case VPInstruction::ComputeFindLastIVResult: {
608622
// FIXME: The cross-recipe dependency on VPReductionPHIRecipe is temporary
609623
// and will be removed by breaking up the recipe further.
@@ -681,18 +695,11 @@ Value *VPInstruction::generate(VPTransformState &State) {
681695

682696
// Create the reduction after the loop. Note that inloop reductions create
683697
// the target reduction in the loop using a Reduction recipe.
684-
if ((State.VF.isVector() ||
685-
RecurrenceDescriptor::isAnyOfRecurrenceKind(RK)) &&
686-
!PhiR->isInLoop()) {
698+
if (State.VF.isVector() && !PhiR->isInLoop()) {
687699
// TODO: Support in-order reductions based on the recurrence descriptor.
688700
// All ops in the reduction inherit fast-math-flags from the recurrence
689701
// descriptor.
690-
if (RecurrenceDescriptor::isAnyOfRecurrenceKind(RK))
691-
ReducedPartRdx =
692-
createAnyOfReduction(Builder, ReducedPartRdx,
693-
RdxDesc.getRecurrenceStartValue(), OrigPhi);
694-
else
695-
ReducedPartRdx = createSimpleReduction(Builder, ReducedPartRdx, RK);
702+
ReducedPartRdx = createSimpleReduction(Builder, ReducedPartRdx, RK);
696703

697704
// If the reduction can be performed in a smaller type, we need to extend
698705
// the reduction to the wider type before we branch to the original loop.
@@ -830,6 +837,7 @@ bool VPInstruction::isVectorToScalar() const {
830837
getOpcode() == VPInstruction::ExtractPenultimateElement ||
831838
getOpcode() == Instruction::ExtractElement ||
832839
getOpcode() == VPInstruction::FirstActiveLane ||
840+
getOpcode() == VPInstruction::ComputeAnyOfResult ||
833841
getOpcode() == VPInstruction::ComputeFindLastIVResult ||
834842
getOpcode() == VPInstruction::ComputeReductionResult ||
835843
getOpcode() == VPInstruction::AnyOf;
@@ -925,6 +933,7 @@ bool VPInstruction::onlyFirstLaneUsed(const VPValue *Op) const {
925933
return true;
926934
case VPInstruction::PtrAdd:
927935
return Op == getOperand(0) || vputils::onlyFirstLaneUsed(this);
936+
case VPInstruction::ComputeAnyOfResult:
928937
case VPInstruction::ComputeFindLastIVResult:
929938
return Op == getOperand(1);
930939
};
@@ -1005,6 +1014,9 @@ void VPInstruction::print(raw_ostream &O, const Twine &Indent,
10051014
case VPInstruction::ExtractPenultimateElement:
10061015
O << "extract-penultimate-element";
10071016
break;
1017+
case VPInstruction::ComputeAnyOfResult:
1018+
O << "compute-anyof-result";
1019+
break;
10081020
case VPInstruction::ComputeFindLastIVResult:
10091021
O << "compute-find-last-iv-result";
10101022
break;

llvm/lib/Transforms/Vectorize/VPlanUnroll.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,9 @@ void UnrollState::unrollBlock(VPBlockBase *VPB) {
327327
// Add all VPValues for all parts to ComputeReductionResult which combines
328328
// the parts to compute the final reduction value.
329329
VPValue *Op1;
330-
if (match(&R, m_VPInstruction<VPInstruction::ComputeReductionResult>(
330+
if (match(&R, m_VPInstruction<VPInstruction::ComputeAnyOfResult>(
331+
m_VPValue(), m_VPValue(), m_VPValue(Op1))) ||
332+
match(&R, m_VPInstruction<VPInstruction::ComputeReductionResult>(
331333
m_VPValue(), m_VPValue(Op1))) ||
332334
match(&R, m_VPInstruction<VPInstruction::ComputeFindLastIVResult>(
333335
m_VPValue(), m_VPValue(), m_VPValue(Op1)))) {

0 commit comments

Comments
 (0)