Skip to content

Commit 1c7a325

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

File tree

6 files changed

+75
-22
lines changed

6 files changed

+75
-22
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

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

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

@@ -7504,15 +7512,19 @@ static void fixReductionScalarResumeWhenVectorizingEpilog(
75047512
EpiRedHeaderPhi->getStartValue()->getUnderlyingValue();
75057513
if (RecurrenceDescriptor::isAnyOfRecurrenceKind(
75067514
RdxDesc.getRecurrenceKind())) {
7515+
Value *StartV = EpiRedResult->getOperand(1)->getLiveInIRValue();
7516+
(void)StartV;
75077517
auto *Cmp = cast<ICmpInst>(MainResumeValue);
75087518
assert(Cmp->getPredicate() == CmpInst::ICMP_NE &&
75097519
"AnyOf expected to start with ICMP_NE");
7510-
assert(Cmp->getOperand(1) == RdxDesc.getRecurrenceStartValue() &&
7520+
assert(Cmp->getOperand(1) == StartV &&
75117521
"AnyOf expected to start by comparing main resume value to original "
75127522
"start value");
75137523
MainResumeValue = Cmp->getOperand(0);
75147524
} else if (RecurrenceDescriptor::isFindLastIVRecurrenceKind(
75157525
RdxDesc.getRecurrenceKind())) {
7526+
Value *StartV = getStartValueFromReductionResult(EpiRedResult);
7527+
(void)StartV;
75167528
using namespace llvm::PatternMatch;
75177529
Value *Cmp, *OrigResumeV, *CmpOp;
75187530
bool IsExpectedPattern =
@@ -7521,10 +7533,7 @@ static void fixReductionScalarResumeWhenVectorizingEpilog(
75217533
m_Value(OrigResumeV))) &&
75227534
(match(Cmp, m_SpecificICmp(ICmpInst::ICMP_EQ, m_Specific(OrigResumeV),
75237535
m_Value(CmpOp))) &&
7524-
(match(CmpOp,
7525-
m_Freeze(m_Specific(RdxDesc.getRecurrenceStartValue()))) ||
7526-
(CmpOp == RdxDesc.getRecurrenceStartValue() &&
7527-
isGuaranteedNotToBeUndefOrPoison(CmpOp))));
7536+
((CmpOp == StartV && isGuaranteedNotToBeUndefOrPoison(CmpOp))));
75287537
assert(IsExpectedPattern && "Unexpected reduction resume pattern");
75297538
(void)IsExpectedPattern;
75307539
MainResumeValue = OrigResumeV;
@@ -9466,7 +9475,10 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
94669475
OrigExitingVPV->replaceUsesWithIf(NewExitingVPV, [](VPUser &U, unsigned) {
94679476
return isa<VPInstruction>(&U) &&
94689477
(cast<VPInstruction>(&U)->getOpcode() ==
9478+
VPInstruction::ComputeAnyOfResult ||
9479+
cast<VPInstruction>(&U)->getOpcode() ==
94699480
VPInstruction::ComputeReductionResult ||
9481+
94709482
cast<VPInstruction>(&U)->getOpcode() ==
94719483
VPInstruction::ComputeFindLastIVResult);
94729484
});
@@ -9519,6 +9531,12 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
95199531
FinalReductionResult =
95209532
Builder.createNaryOp(VPInstruction::ComputeFindLastIVResult,
95219533
{PhiR, Start, NewExitingVPV}, ExitDL);
9534+
} else if (RecurrenceDescriptor::isAnyOfRecurrenceKind(
9535+
RdxDesc.getRecurrenceKind())) {
9536+
VPValue *Start = PhiR->getStartValue();
9537+
FinalReductionResult =
9538+
Builder.createNaryOp(VPInstruction::ComputeAnyOfResult,
9539+
{PhiR, Start, NewExitingVPV}, ExitDL);
95229540
} else {
95239541
VPIRFlags Flags = RecurrenceDescriptor::isFloatingPointRecurrenceKind(
95249542
RdxDesc.getRecurrenceKind())
@@ -10050,23 +10068,36 @@ preparePlanForEpilogueVectorLoop(VPlan &Plan, Loop *L,
1005010068
Value *ResumeV = nullptr;
1005110069
// TODO: Move setting of resume values to prepareToExecute.
1005210070
if (auto *ReductionPhi = dyn_cast<VPReductionPHIRecipe>(&R)) {
10071+
auto *RdxResult =
10072+
cast<VPInstruction>(*find_if(ReductionPhi->users(), [](VPUser *U) {
10073+
auto *VPI = dyn_cast<VPInstruction>(U);
10074+
return VPI &&
10075+
(VPI->getOpcode() == VPInstruction::ComputeReductionResult ||
10076+
VPI->getOpcode() == VPInstruction::ComputeFindLastIVResult);
10077+
}));
1005310078
ResumeV = cast<PHINode>(ReductionPhi->getUnderlyingInstr())
1005410079
->getIncomingValueForBlock(L->getLoopPreheader());
1005510080
const RecurrenceDescriptor &RdxDesc =
1005610081
ReductionPhi->getRecurrenceDescriptor();
1005710082
RecurKind RK = RdxDesc.getRecurrenceKind();
1005810083
if (RecurrenceDescriptor::isAnyOfRecurrenceKind(RK)) {
10084+
Value *StartV = RdxResult->getOperand(1)->getLiveInIRValue();
10085+
assert(RdxDesc.getRecurrenceStartValue() == StartV &&
10086+
"start value from ComputeAnyOfResult must match");
10087+
1005910088
// VPReductionPHIRecipes for AnyOf reductions expect a boolean as
1006010089
// start value; compare the final value from the main vector loop
1006110090
// to the start value.
1006210091
BasicBlock *PBB = cast<Instruction>(ResumeV)->getParent();
1006310092
IRBuilder<> Builder(PBB, PBB->getFirstNonPHIIt());
10064-
ResumeV =
10065-
Builder.CreateICmpNE(ResumeV, RdxDesc.getRecurrenceStartValue());
10093+
ResumeV = Builder.CreateICmpNE(ResumeV, StartV);
1006610094
} else if (RecurrenceDescriptor::isFindLastIVRecurrenceKind(RK)) {
10067-
ToFrozen[RdxDesc.getRecurrenceStartValue()] =
10068-
cast<PHINode>(ResumeV)->getIncomingValueForBlock(
10069-
EPI.MainLoopIterationCountCheck);
10095+
Value *StartV = getStartValueFromReductionResult(RdxResult);
10096+
assert(RdxDesc.getRecurrenceStartValue() == StartV &&
10097+
"start value from ComputeFindLastIVResult must match");
10098+
10099+
ToFrozen[StartV] = cast<PHINode>(ResumeV)->getIncomingValueForBlock(
10100+
EPI.MainLoopIterationCountCheck);
1007010101

1007110102
// VPReductionPHIRecipe for FindLastIV reductions requires an adjustment
1007210103
// to the resume value. The resume value is adjusted to the sentinel
@@ -10076,8 +10107,7 @@ preparePlanForEpilogueVectorLoop(VPlan &Plan, Loop *L,
1007610107
// variable.
1007710108
BasicBlock *ResumeBB = cast<Instruction>(ResumeV)->getParent();
1007810109
IRBuilder<> Builder(ResumeBB, ResumeBB->getFirstNonPHIIt());
10079-
Value *Cmp = Builder.CreateICmpEQ(
10080-
ResumeV, ToFrozen[RdxDesc.getRecurrenceStartValue()]);
10110+
Value *Cmp = Builder.CreateICmpEQ(ResumeV, ToFrozen[StartV]);
1008110111
ResumeV =
1008210112
Builder.CreateSelect(Cmp, RdxDesc.getSentinelValue(), ResumeV);
1008310113
}

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,7 @@ class VPInstruction : public VPRecipeWithIRFlags,
898898
BranchOnCount,
899899
BranchOnCond,
900900
Broadcast,
901+
ComputeAnyOfResult,
901902
ComputeFindLastIVResult,
902903
ComputeReductionResult,
903904
// 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: 20 additions & 8 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,17 +695,10 @@ 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
695702
ReducedPartRdx = createSimpleReduction(Builder, ReducedPartRdx, RK);
696703

697704
// If the reduction can be performed in a smaller type, we need to extend
@@ -841,6 +848,7 @@ bool VPInstruction::isVectorToScalar() const {
841848
getOpcode() == VPInstruction::ExtractPenultimateElement ||
842849
getOpcode() == Instruction::ExtractElement ||
843850
getOpcode() == VPInstruction::FirstActiveLane ||
851+
getOpcode() == VPInstruction::ComputeAnyOfResult ||
844852
getOpcode() == VPInstruction::ComputeFindLastIVResult ||
845853
getOpcode() == VPInstruction::ComputeReductionResult ||
846854
getOpcode() == VPInstruction::AnyOf;
@@ -938,6 +946,7 @@ bool VPInstruction::onlyFirstLaneUsed(const VPValue *Op) const {
938946
return true;
939947
case VPInstruction::PtrAdd:
940948
return Op == getOperand(0) || vputils::onlyFirstLaneUsed(this);
949+
case VPInstruction::ComputeAnyOfResult:
941950
case VPInstruction::ComputeFindLastIVResult:
942951
return Op == getOperand(1);
943952
};
@@ -1021,6 +1030,9 @@ void VPInstruction::print(raw_ostream &O, const Twine &Indent,
10211030
case VPInstruction::ExtractPenultimateElement:
10221031
O << "extract-penultimate-element";
10231032
break;
1033+
case VPInstruction::ComputeAnyOfResult:
1034+
O << "compute-anyof-result";
1035+
break;
10241036
case VPInstruction::ComputeFindLastIVResult:
10251037
O << "compute-find-last-iv-result";
10261038
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)