[llvm] [LV] Support argmin/argmax with strict predicates. (PR #170223)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Dec 23 07:24:51 PST 2025
================
@@ -1120,6 +1122,129 @@ bool VPlanTransforms::handleMaxMinNumReductions(VPlan &Plan) {
return true;
}
+/// For argmin/argmax reductions with strict predicates, convert the existing
+/// FindLastIV reduction to a new UMin reduction of a wide canonical IV. If the
+/// original IV was not canonical, a new canonical wide IV is added, and the
+/// final result is scaled back to the original IV.
+static bool handleFirstArgMinArgMax(VPlan &Plan,
+ VPReductionPHIRecipe *MinMaxPhiR,
+ VPReductionPHIRecipe *FindIVPhiR,
+ VPWidenIntOrFpInductionRecipe *WideIV,
+ VPInstruction *MinMaxResult) {
+ Type *Ty = Plan.getVectorLoopRegion()->getCanonicalIVType();
+ // TODO: Support different IV types.
+ if (Ty != VPTypeAnalysis(Plan).inferScalarType(FindIVPhiR))
+ return false;
+
+ // If the original wide IV is not canonical, create a new one. The wide IV is
+ // guaranteed to not wrap for all lanes that are active in the vector loop.
+ if (!WideIV->isCanonical()) {
+ VPValue *Zero = Plan.getConstantInt(Ty, 0);
+ VPValue *One = Plan.getConstantInt(Ty, 1);
+ auto *WidenCanIV = new VPWidenIntOrFpInductionRecipe(
+ nullptr, Zero, One, WideIV->getVFValue(),
+ WideIV->getInductionDescriptor(),
+ VPIRFlags::WrapFlagsTy(/*HasNUW=*/true, /*HasNSW=*/false),
+ WideIV->getDebugLoc());
+ WidenCanIV->insertBefore(WideIV);
+
+ // Update the select to use the wide canonical IV.
+ auto *SelectR = cast<VPSingleDefRecipe>(
+ FindIVPhiR->getBackedgeValue()->getDefiningRecipe());
+ assert(match(SelectR, m_Select(m_VPValue(), m_VPValue(), m_VPValue())) &&
+ "backedge value must be a select");
+ WideIV->replaceUsesWithIf(WidenCanIV, [SelectR](const VPUser &U, unsigned) {
+ return SelectR == &U;
+ });
+ }
+
+ // Create the new UMin reduction recipe to track the minimum index.
----------------
ayalz wrote:
Creating a UMin reduction may be confusing - as it conceptually suggests recording the first iteration that updates the running min/max. The header phi itself remains the same, vectorizing Find[Last]IV, albeit one with a simpler sentinel-less final reduction (more below).
https://github.com/llvm/llvm-project/pull/170223
More information about the llvm-commits
mailing list