[llvm] [VPlan] Support multiple F(Max|Min)Num reductions. (PR #161735)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 5 09:20:58 PST 2025


================
@@ -879,52 +873,71 @@ bool VPlanTransforms::handleMaxMinNumReductions(VPlan &Plan) {
   }
 
   VPBasicBlock *LatchVPBB = LoopRegion->getExitingBasicBlock();
-  VPBuilder Builder(LatchVPBB->getTerminator());
-  auto *LatchExitingBranch = cast<VPInstruction>(LatchVPBB->getTerminator());
-  assert(LatchExitingBranch->getOpcode() == VPInstruction::BranchOnCount &&
+  VPBuilder LatchBuilder(LatchVPBB->getTerminator());
+  VPValue *IsNaNLane = nullptr;
+  SmallPtrSet<VPValue *, 2> RdxResults;
+  for (const auto &[RedPhiR, MinMaxOp] : MinMaxNumReductionsToHandle) {
+    assert(RecurrenceDescriptor::isFPMinMaxNumRecurrenceKind(
+               RedPhiR->getRecurrenceKind()) &&
+           "unsupported reduction");
+
+    VPValue *IsNaN =
+        LatchBuilder.createFCmp(CmpInst::FCMP_UNO, MinMaxOp, MinMaxOp);
+    IsNaNLane = IsNaNLane ? LatchBuilder.createOr(IsNaNLane, IsNaN) : IsNaN;
+  }
+
+  VPValue *AnyNaNLane =
+      LatchBuilder.createNaryOp(VPInstruction::AnyOf, {IsNaNLane});
+  VPBasicBlock *MiddleVPBB = Plan.getMiddleBlock();
+  VPBuilder MiddleBuilder(MiddleVPBB, MiddleVPBB->begin());
+  for (const auto &[RedPhiR, MinMaxOp] : MinMaxNumReductionsToHandle) {
+    // If we exit early due to NaNs, compute the final reduction result based
+    // on the reduction phi at the beginning of the last vector iteration.
+    auto *RdxResult = find_singleton<VPSingleDefRecipe>(
+        RedPhiR->getBackedgeValue()->users(),
+        [RedPhiR = RedPhiR](VPUser *U, bool) -> VPSingleDefRecipe * {
+          auto *VPI = dyn_cast<VPInstruction>(U);
+          if (VPI && VPI->getOpcode() == VPInstruction::ComputeReductionResult)
----------------
fhahn wrote:

Yep, will introcuce a matcher and will also update this here, together with changing to iterating over the backedge value.

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


More information about the llvm-commits mailing list