@@ -7484,6 +7484,13 @@ static void addRuntimeUnrollDisableMetaData(Loop *L) {
7484
7484
}
7485
7485
}
7486
7486
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
+
7487
7494
// If \p R is a ComputeReductionResult when vectorizing the epilog loop,
7488
7495
// fix the reduction's scalar PHI node by adding the incoming value from the
7489
7496
// main vector loop.
@@ -7492,7 +7499,8 @@ static void fixReductionScalarResumeWhenVectorizingEpilog(
7492
7499
BasicBlock *BypassBlock) {
7493
7500
auto *EpiRedResult = dyn_cast<VPInstruction>(R);
7494
7501
if (!EpiRedResult ||
7495
- (EpiRedResult->getOpcode () != VPInstruction::ComputeReductionResult &&
7502
+ (EpiRedResult->getOpcode () != VPInstruction::ComputeAnyOfResult &&
7503
+ EpiRedResult->getOpcode () != VPInstruction::ComputeReductionResult &&
7496
7504
EpiRedResult->getOpcode () != VPInstruction::ComputeFindLastIVResult))
7497
7505
return ;
7498
7506
@@ -7504,15 +7512,19 @@ static void fixReductionScalarResumeWhenVectorizingEpilog(
7504
7512
EpiRedHeaderPhi->getStartValue ()->getUnderlyingValue ();
7505
7513
if (RecurrenceDescriptor::isAnyOfRecurrenceKind (
7506
7514
RdxDesc.getRecurrenceKind ())) {
7515
+ Value *StartV = EpiRedResult->getOperand (1 )->getLiveInIRValue ();
7516
+ (void )StartV;
7507
7517
auto *Cmp = cast<ICmpInst>(MainResumeValue);
7508
7518
assert (Cmp->getPredicate () == CmpInst::ICMP_NE &&
7509
7519
" AnyOf expected to start with ICMP_NE" );
7510
- assert (Cmp->getOperand (1 ) == RdxDesc. getRecurrenceStartValue () &&
7520
+ assert (Cmp->getOperand (1 ) == StartV &&
7511
7521
" AnyOf expected to start by comparing main resume value to original "
7512
7522
" start value" );
7513
7523
MainResumeValue = Cmp->getOperand (0 );
7514
7524
} else if (RecurrenceDescriptor::isFindLastIVRecurrenceKind (
7515
7525
RdxDesc.getRecurrenceKind ())) {
7526
+ Value *StartV = getStartValueFromReductionResult (EpiRedResult);
7527
+ (void )StartV;
7516
7528
using namespace llvm ::PatternMatch;
7517
7529
Value *Cmp, *OrigResumeV, *CmpOp;
7518
7530
bool IsExpectedPattern =
@@ -7521,10 +7533,7 @@ static void fixReductionScalarResumeWhenVectorizingEpilog(
7521
7533
m_Value (OrigResumeV))) &&
7522
7534
(match (Cmp, m_SpecificICmp (ICmpInst::ICMP_EQ, m_Specific (OrigResumeV),
7523
7535
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))));
7528
7537
assert (IsExpectedPattern && " Unexpected reduction resume pattern" );
7529
7538
(void )IsExpectedPattern;
7530
7539
MainResumeValue = OrigResumeV;
@@ -9466,7 +9475,10 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
9466
9475
OrigExitingVPV->replaceUsesWithIf (NewExitingVPV, [](VPUser &U, unsigned ) {
9467
9476
return isa<VPInstruction>(&U) &&
9468
9477
(cast<VPInstruction>(&U)->getOpcode () ==
9478
+ VPInstruction::ComputeAnyOfResult ||
9479
+ cast<VPInstruction>(&U)->getOpcode () ==
9469
9480
VPInstruction::ComputeReductionResult ||
9481
+
9470
9482
cast<VPInstruction>(&U)->getOpcode () ==
9471
9483
VPInstruction::ComputeFindLastIVResult);
9472
9484
});
@@ -9519,6 +9531,12 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
9519
9531
FinalReductionResult =
9520
9532
Builder.createNaryOp (VPInstruction::ComputeFindLastIVResult,
9521
9533
{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);
9522
9540
} else {
9523
9541
VPIRFlags Flags = RecurrenceDescriptor::isFloatingPointRecurrenceKind (
9524
9542
RdxDesc.getRecurrenceKind ())
@@ -10050,23 +10068,36 @@ preparePlanForEpilogueVectorLoop(VPlan &Plan, Loop *L,
10050
10068
Value *ResumeV = nullptr ;
10051
10069
// TODO: Move setting of resume values to prepareToExecute.
10052
10070
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
+ }));
10053
10078
ResumeV = cast<PHINode>(ReductionPhi->getUnderlyingInstr ())
10054
10079
->getIncomingValueForBlock (L->getLoopPreheader ());
10055
10080
const RecurrenceDescriptor &RdxDesc =
10056
10081
ReductionPhi->getRecurrenceDescriptor ();
10057
10082
RecurKind RK = RdxDesc.getRecurrenceKind ();
10058
10083
if (RecurrenceDescriptor::isAnyOfRecurrenceKind (RK)) {
10084
+ Value *StartV = RdxResult->getOperand (1 )->getLiveInIRValue ();
10085
+ assert (RdxDesc.getRecurrenceStartValue () == StartV &&
10086
+ " start value from ComputeAnyOfResult must match" );
10087
+
10059
10088
// VPReductionPHIRecipes for AnyOf reductions expect a boolean as
10060
10089
// start value; compare the final value from the main vector loop
10061
10090
// to the start value.
10062
10091
BasicBlock *PBB = cast<Instruction>(ResumeV)->getParent ();
10063
10092
IRBuilder<> Builder (PBB, PBB->getFirstNonPHIIt ());
10064
- ResumeV =
10065
- Builder.CreateICmpNE (ResumeV, RdxDesc.getRecurrenceStartValue ());
10093
+ ResumeV = Builder.CreateICmpNE (ResumeV, StartV);
10066
10094
} 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 );
10070
10101
10071
10102
// VPReductionPHIRecipe for FindLastIV reductions requires an adjustment
10072
10103
// to the resume value. The resume value is adjusted to the sentinel
@@ -10076,8 +10107,7 @@ preparePlanForEpilogueVectorLoop(VPlan &Plan, Loop *L,
10076
10107
// variable.
10077
10108
BasicBlock *ResumeBB = cast<Instruction>(ResumeV)->getParent ();
10078
10109
IRBuilder<> Builder (ResumeBB, ResumeBB->getFirstNonPHIIt ());
10079
- Value *Cmp = Builder.CreateICmpEQ (
10080
- ResumeV, ToFrozen[RdxDesc.getRecurrenceStartValue ()]);
10110
+ Value *Cmp = Builder.CreateICmpEQ (ResumeV, ToFrozen[StartV]);
10081
10111
ResumeV =
10082
10112
Builder.CreateSelect (Cmp, RdxDesc.getSentinelValue (), ResumeV);
10083
10113
}
0 commit comments