[llvm] [LV] Vectorize selecting last IV of min/max element. (PR #141431)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Nov 23 12:58:47 PST 2025
================
@@ -992,3 +996,98 @@ bool VPlanTransforms::handleMaxMinNumReductions(VPlan &Plan) {
MiddleTerm->setOperand(0, NewCond);
return true;
}
+
+bool VPlanTransforms::handleMultiUseReductions(VPlan &Plan) {
+ for (auto &PhiR : make_early_inc_range(
+ Plan.getVectorLoopRegion()->getEntryBasicBlock()->phis())) {
+ auto *MinMaxPhiR = dyn_cast<VPReductionPHIRecipe>(&PhiR);
+ // TODO: check for multi-uses in VPlan directly.
+ if (!MinMaxPhiR || !MinMaxPhiR->hasLoopUsesOutsideReductionChain())
+ continue;
+
+ RecurKind RdxKind = MinMaxPhiR->getRecurrenceKind();
+ assert(
+ RecurrenceDescriptor::isIntMinMaxRecurrenceKind(RdxKind) &&
+ "only min/max recurrences support users outside the reduction chain");
+
+ // One user of MinMaxPhiR is MinMaxOp, the other user must be a compare
+ // that's part of a FindLastIV chain.
+ auto *MinMaxOp =
+ dyn_cast<VPRecipeWithIRFlags>(MinMaxPhiR->getBackedgeValue());
+ if (!MinMaxOp || MinMaxOp->getNumUsers() != 2)
+ return false;
+
+ assert((isa<VPWidenIntrinsicRecipe>(MinMaxOp) ||
+ (isa<VPReplicateRecipe>(MinMaxOp) &&
+ isa<IntrinsicInst>(
+ cast<VPReplicateRecipe>(MinMaxOp)->getUnderlyingValue()))) &&
+ "MinMaxOp must be a wide or scalar intrinsic");
+ VPValue *MinMaxOpA = MinMaxOp->getOperand(0);
+ VPValue *MinMaxOpB = MinMaxOp->getOperand(1);
+ if (MinMaxOpA != MinMaxPhiR)
+ std::swap(MinMaxOpA, MinMaxOpB);
+ if (MinMaxOpA != MinMaxPhiR)
+ return false;
+
+ VPValue *CmpOpA;
+ VPValue *CmpOpB;
+ CmpPredicate Pred;
+ auto *Cmp = dyn_cast_or_null<VPRecipeWithIRFlags>(findUserOf(
+ MinMaxPhiR, m_Cmp(Pred, m_VPValue(CmpOpA), m_VPValue(CmpOpB))));
+ if (!Cmp || Cmp->getNumUsers() != 1 ||
+ (CmpOpA != MinMaxOpB && CmpOpB != MinMaxOpB))
+ return false;
+
+ // TODO: Strict predicates need to find the first IV value for which the
+ // predicate holds, not the last.
+ if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_NE ||
+ ICmpInst::isLT(Pred) || ICmpInst::isGT(Pred))
+ return false;
+
+ // Normalize the predicate so MinMaxPhiR is on the right side.
+ if (CmpOpA == MinMaxPhiR)
+ Pred = CmpInst::getSwappedPredicate(Pred);
+
+ // Cmp must be used by the select of a FindLastIV chain.
+ VPValue *Sel = dyn_cast<VPSingleDefRecipe>(Cmp->getSingleUser());
+ VPValue *IVOp, *FindIV;
+ if (!Sel ||
+ !match(Sel,
+ m_Select(m_Specific(Cmp), m_VPValue(IVOp), m_VPValue(FindIV))) ||
+ Sel->getNumUsers() != 2)
+ return false;
+
+ auto *FindIVPhiR = dyn_cast<VPReductionPHIRecipe>(FindIV);
+ if (!FindIVPhiR || !RecurrenceDescriptor::isFindLastIVRecurrenceKind(
+ FindIVPhiR->getRecurrenceKind()))
+ return false;
+
+ assert(isa<VPWidenIntOrFpInductionRecipe>(IVOp) &&
+ "IVOp must be a wide induction");
+ assert(!FindIVPhiR->isInLoop() && !FindIVPhiR->isOrdered() &&
+ "cannot handle inloop/ordered reductions yet");
+
+ // The reduction using MinMaxPhiR needs adjusting to compute the correct
+ // result:
+ // 1. We need to find the last IV for which the condition based on the
+ // min/max recurrence is true,
+ // 2. Compare the partial min/max reduction result to its final value and,
+ // 3. Select the lanes of the partial FindLastIV reductions which
+ // correspond to the lanes matching the min/max reduction result.
+ VPInstruction *FindIVResult =
+ findUserOf<VPInstruction::ComputeFindIVResult>(FindIVPhiR);
+ VPInstruction *MinMaxResult =
+ findUserOf<VPInstruction::ComputeReductionResult>(MinMaxPhiR);
+ MinMaxResult->moveBefore(*FindIVResult->getParent(),
+ FindIVResult->getIterator());
+
+ VPBuilder B(FindIVResult);
+ auto *FinalMinMaxCmp = B.createICmp(
+ CmpInst::ICMP_EQ, MinMaxResult->getOperand(1), MinMaxResult);
----------------
ayalz wrote:
Would be good to explicitly Broadcast MinMaxResult before feeding it to compare with its operand(1)?
https://github.com/llvm/llvm-project/pull/141431
More information about the llvm-commits
mailing list