@@ -7485,6 +7485,13 @@ static void addRuntimeUnrollDisableMetaData(Loop *L) {
7485
7485
}
7486
7486
}
7487
7487
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
+
7488
7495
// If \p R is a ComputeReductionResult when vectorizing the epilog loop,
7489
7496
// fix the reduction's scalar PHI node by adding the incoming value from the
7490
7497
// main vector loop.
@@ -7493,7 +7500,8 @@ static void fixReductionScalarResumeWhenVectorizingEpilog(
7493
7500
BasicBlock *BypassBlock) {
7494
7501
auto *EpiRedResult = dyn_cast<VPInstruction>(R);
7495
7502
if (!EpiRedResult ||
7496
- (EpiRedResult->getOpcode () != VPInstruction::ComputeReductionResult &&
7503
+ (EpiRedResult->getOpcode () != VPInstruction::ComputeAnyOfResult &&
7504
+ EpiRedResult->getOpcode () != VPInstruction::ComputeReductionResult &&
7497
7505
EpiRedResult->getOpcode () != VPInstruction::ComputeFindLastIVResult))
7498
7506
return ;
7499
7507
@@ -7505,15 +7513,19 @@ static void fixReductionScalarResumeWhenVectorizingEpilog(
7505
7513
EpiRedHeaderPhi->getStartValue ()->getUnderlyingValue ();
7506
7514
if (RecurrenceDescriptor::isAnyOfRecurrenceKind (
7507
7515
RdxDesc.getRecurrenceKind ())) {
7516
+ Value *StartV = EpiRedResult->getOperand (1 )->getLiveInIRValue ();
7517
+ (void )StartV;
7508
7518
auto *Cmp = cast<ICmpInst>(MainResumeValue);
7509
7519
assert (Cmp->getPredicate () == CmpInst::ICMP_NE &&
7510
7520
" AnyOf expected to start with ICMP_NE" );
7511
- assert (Cmp->getOperand (1 ) == RdxDesc. getRecurrenceStartValue () &&
7521
+ assert (Cmp->getOperand (1 ) == StartV &&
7512
7522
" AnyOf expected to start by comparing main resume value to original "
7513
7523
" start value" );
7514
7524
MainResumeValue = Cmp->getOperand (0 );
7515
7525
} else if (RecurrenceDescriptor::isFindLastIVRecurrenceKind (
7516
7526
RdxDesc.getRecurrenceKind ())) {
7527
+ Value *StartV = getStartValueFromReductionResult (EpiRedResult);
7528
+ (void )StartV;
7517
7529
using namespace llvm ::PatternMatch;
7518
7530
Value *Cmp, *OrigResumeV, *CmpOp;
7519
7531
bool IsExpectedPattern =
@@ -7522,10 +7534,7 @@ static void fixReductionScalarResumeWhenVectorizingEpilog(
7522
7534
m_Value (OrigResumeV))) &&
7523
7535
(match (Cmp, m_SpecificICmp (ICmpInst::ICMP_EQ, m_Specific (OrigResumeV),
7524
7536
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))));
7529
7538
assert (IsExpectedPattern && " Unexpected reduction resume pattern" );
7530
7539
(void )IsExpectedPattern;
7531
7540
MainResumeValue = OrigResumeV;
@@ -9467,7 +9476,10 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
9467
9476
OrigExitingVPV->replaceUsesWithIf (NewExitingVPV, [](VPUser &U, unsigned ) {
9468
9477
return isa<VPInstruction>(&U) &&
9469
9478
(cast<VPInstruction>(&U)->getOpcode () ==
9479
+ VPInstruction::ComputeAnyOfResult ||
9480
+ cast<VPInstruction>(&U)->getOpcode () ==
9470
9481
VPInstruction::ComputeReductionResult ||
9482
+
9471
9483
cast<VPInstruction>(&U)->getOpcode () ==
9472
9484
VPInstruction::ComputeFindLastIVResult);
9473
9485
});
@@ -9497,6 +9509,12 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
9497
9509
FinalReductionResult =
9498
9510
Builder.createNaryOp (VPInstruction::ComputeFindLastIVResult,
9499
9511
{PhiR, Start, NewExitingVPV}, ExitDL);
9512
+ } else if (RecurrenceDescriptor::isAnyOfRecurrenceKind (
9513
+ RdxDesc.getRecurrenceKind ())) {
9514
+ VPValue *Start = PhiR->getStartValue ();
9515
+ FinalReductionResult =
9516
+ Builder.createNaryOp (VPInstruction::ComputeAnyOfResult,
9517
+ {PhiR, Start, NewExitingVPV}, ExitDL);
9500
9518
} else {
9501
9519
VPIRFlags Flags = RecurrenceDescriptor::isFloatingPointRecurrenceKind (
9502
9520
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