[llvm] [LV] Support argmin/argmax with strict predicates. (PR #170223)

via llvm-commits llvm-commits at lists.llvm.org
Sat Jan 24 14:35:52 PST 2026


================
@@ -1403,7 +1405,139 @@ bool VPlanTransforms::handleFindLastReductions(VPlan &Plan) {
   return true;
 }
 
-bool VPlanTransforms::handleMultiUseReductions(VPlan &Plan) {
+/// For an argmin/argmax reduction with strict predicate \p MinOrMaxPhiR,
+/// convert the FindLastIV reduction \p FindLastIVPhiR of induction \p WideIV 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. \p MinOrMaxResult computes the final value of \p
+/// MinOrMaxPhiR and \p FindIVSelect, \p FindIVCmp, \p FindIVRdxResult compute
+/// the final value of \p FindLastIVPhiR.
+static bool handleFirstArgMinOrMax(
+    VPlan &Plan, VPReductionPHIRecipe *MinOrMaxPhiR,
+    VPReductionPHIRecipe *FindLastIVPhiR, VPWidenIntOrFpInductionRecipe *WideIV,
+    VPInstruction *MinOrMaxResult, VPInstruction *FindIVSelect,
+    VPRecipeBase *FindIVCmp, VPInstruction *FindIVRdxResult) {
+  Type *Ty = Plan.getVectorLoopRegion()->getCanonicalIVType();
+  // TODO: Support non (i.e., narrower than) canonical IV types.
+  if (Ty != VPTypeAnalysis(Plan).inferScalarType(WideIV))
+    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.
+  auto *FindIVSelectR = cast<VPSingleDefRecipe>(
+      FindLastIVPhiR->getBackedgeValue()->getDefiningRecipe());
+  assert(
+      match(FindIVSelectR, m_Select(m_VPValue(), m_VPValue(), m_VPValue())) &&
+      "backedge value must be a select");
+  if (!WideIV->isCanonical()) {
+    VPIRValue *Zero = Plan.getConstantInt(Ty, 0);
+    VPIRValue *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.
+    FindIVSelectR->setOperand(FindIVSelectR->getOperand(1) == WideIV ? 1 : 2,
+                              WidenCanIV);
+  }
+
+  // Create the new UMin reduction recipe to track the minimum index.
----------------
ayalz wrote:

```
  // Create the new UMin reduction recipe to track the minimum index.
```
Minimum index should continue to be tracked via FindLastIV reduction chain, new UMin reduction is needed only to ComputeReductionResult?

https://github.com/llvm/llvm-project/pull/170223


More information about the llvm-commits mailing list