Skip to content

Commit 8a3982f

Browse files
committed
Adjust reduction phi in transformation stage
1 parent dc2da76 commit 8a3982f

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,17 +1486,6 @@ class LoopVectorizationCostModel {
14861486
return InLoopReductions.contains(Phi);
14871487
}
14881488

1489-
/// Returns true if the predicated reduction select should be used.
1490-
bool usePredicatedReductionSelect(unsigned Opcode, Type *PhiTy) const {
1491-
// Force to use predicated reduction select since the EVL of the
1492-
// second-to-last iteration might not be VF*UF.
1493-
if (foldTailWithEVL())
1494-
return true;
1495-
return PreferPredicatedReductionSelect ||
1496-
TTI.preferPredicatedReductionSelect(
1497-
Opcode, PhiTy, TargetTransformInfo::ReductionFlags());
1498-
}
1499-
15001489
/// Estimate cost of an intrinsic call instruction CI if it were vectorized
15011490
/// with factor VF. Return the cost of the instruction, including
15021491
/// scalarization overhead if it's needed.
@@ -9461,8 +9450,10 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
94619450
cast<VPInstruction>(&U)->getOpcode() ==
94629451
VPInstruction::ComputeReductionResult;
94639452
});
9464-
if (CM.usePredicatedReductionSelect(
9465-
PhiR->getRecurrenceDescriptor().getOpcode(), PhiTy))
9453+
if (PreferPredicatedReductionSelect ||
9454+
TTI.preferPredicatedReductionSelect(
9455+
PhiR->getRecurrenceDescriptor().getOpcode(), PhiTy,
9456+
TargetTransformInfo::ReductionFlags()))
94669457
PhiR->setOperand(1, NewExitingVPV);
94679458
}
94689459

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,6 +1357,30 @@ static void transformRecipestoEVLRecipes(VPlan &Plan, VPValue &EVL) {
13571357
VPTypeAnalysis TypeInfo(CanonicalIVType);
13581358
LLVMContext &Ctx = CanonicalIVType->getContext();
13591359
SmallVector<VPValue *> HeaderMasks = collectAllHeaderMasks(Plan);
1360+
1361+
// Adjust reduction phi recipes
1362+
VPBasicBlock *Header = Plan.getVectorLoopRegion()->getEntryBasicBlock();
1363+
for (VPRecipeBase &R : Header->phis()) {
1364+
auto *PhiR = dyn_cast<VPReductionPHIRecipe>(&R);
1365+
if (!PhiR || PhiR->isInLoop())
1366+
continue;
1367+
VPValue *BackedgeVPV = PhiR->getBackedgeValue();
1368+
bool IsExiting = any_of(BackedgeVPV->users(), [](VPUser *U) {
1369+
return isa<VPInstruction>(U) && cast<VPInstruction>(U)->getOpcode() ==
1370+
VPInstruction::ComputeReductionResult;
1371+
});
1372+
if (IsExiting)
1373+
continue;
1374+
1375+
auto *FoundRdxSelect = find_if(BackedgeVPV->users(), [](VPUser *U) {
1376+
return match(U, m_Select(m_VPValue(), m_VPValue(), m_VPValue()));
1377+
});
1378+
assert(FoundRdxSelect && "Must have a reduction select when out-loop "
1379+
"reduction with tail folding");
1380+
auto *RdxSelect = cast<VPRecipeBase>(*FoundRdxSelect);
1381+
PhiR->setOperand(1, RdxSelect->getVPSingleValue());
1382+
}
1383+
13601384
for (VPValue *HeaderMask : collectAllHeaderMasks(Plan)) {
13611385
for (VPUser *U : collectUsersRecursively(HeaderMask)) {
13621386
auto *CurRecipe = dyn_cast<VPRecipeBase>(U);

0 commit comments

Comments
 (0)