[llvm] [VPlan] Bail out on chained reductions in handleMaxMinNumReductions. (PR #223521)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 14 13:46:36 PDT 2026


https://github.com/fhahn created https://github.com/llvm/llvm-project/pull/223521

handleMaxMinNumReductions does not support reduction chains, as the NaN check directly checks the operand of the single min/max reduction.

Bail out for now to fix a crash.

Fixes https://github.com/llvm/llvm-project/issues/223430.

>From ca2bc7430575b1f3d28348c00784b0d0faa380b6 Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Mon, 14 Sep 2026 21:42:20 +0100
Subject: [PATCH] [VPlan] Bail out on chained reductions in
 handleMaxMinNumReductions.

handleMaxMinNumReductions does not support reduction chains, as the NaN
check directly checks the operand of the single min/max reduction.

Bail out for now to fix a crash.

Fixes https://github.com/llvm/llvm-project/issues/223430.
---
 .../Vectorize/VPlanConstruction.cpp           | 11 ++++--
 .../fmax-without-fast-math-flags.ll           | 38 +++++++++++++++++++
 .../fmin-without-fast-math-flags.ll           | 38 +++++++++++++++++++
 3 files changed, 83 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp b/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
index e42261ff50735..4cd455adb7a90 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
@@ -1622,12 +1622,15 @@ bool VPlanTransforms::handleMaxMinNumReductions(VPlan &Plan) {
     if (!match(MinOrMaxR, m_Intrinsic(ExpectedIntrinsicID)))
       return nullptr;
 
+    // MinOrMaxR must combine RedPhiR directly with the new element, as the NaN
+    // check added below only covers the other operand.
+    // TODO: Support multi-step min/max chains (e.g. maxnum(l, maxnum(k, phi)))
+    // by checking all operands feeding the chain for NaNs.
     if (MinOrMaxR->getOperand(0) == RedPhiR)
       return MinOrMaxR->getOperand(1);
-
-    assert(MinOrMaxR->getOperand(1) == RedPhiR &&
-           "Reduction phi operand expected");
-    return MinOrMaxR->getOperand(0);
+    if (MinOrMaxR->getOperand(1) == RedPhiR)
+      return MinOrMaxR->getOperand(0);
+    return nullptr;
   };
 
   VPRegionBlock *LoopRegion = Plan.getVectorLoopRegion();
diff --git a/llvm/test/Transforms/LoopVectorize/fmax-without-fast-math-flags.ll b/llvm/test/Transforms/LoopVectorize/fmax-without-fast-math-flags.ll
index d81b88af2cd32..b0dd95735c5f3 100644
--- a/llvm/test/Transforms/LoopVectorize/fmax-without-fast-math-flags.ll
+++ b/llvm/test/Transforms/LoopVectorize/fmax-without-fast-math-flags.ll
@@ -862,4 +862,42 @@ exit:
   ret float %max.next
 }
 
+; The reduction chain contains an intermediate maxnum.
+define float @fmaxnum_intermediate_maxnum_op(ptr %src, i64 %n, float %k) {
+; CHECK-LABEL: define float @fmaxnum_intermediate_maxnum_op(
+; CHECK-SAME: ptr [[SRC:%.*]], i64 [[N:%.*]], float [[K:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*]]:
+; CHECK-NEXT:    br label %[[LOOP:.*]]
+; CHECK:       [[LOOP]]:
+; CHECK-NEXT:    [[IV:%.*]] = phi i64 [ 0, %[[ENTRY]] ], [ [[IV_NEXT:%.*]], %[[LOOP]] ]
+; CHECK-NEXT:    [[MAX:%.*]] = phi float [ -1.000000e+07, %[[ENTRY]] ], [ [[MAX_NEXT:%.*]], %[[LOOP]] ]
+; CHECK-NEXT:    [[GEP_SRC:%.*]] = getelementptr inbounds nuw float, ptr [[SRC]], i64 [[IV]]
+; CHECK-NEXT:    [[L:%.*]] = load float, ptr [[GEP_SRC]], align 4
+; CHECK-NEXT:    [[TMP:%.*]] = call float @llvm.maxnum.f32(float [[K]], float [[MAX]])
+; CHECK-NEXT:    [[MAX_NEXT]] = call nnan nsz float @llvm.maxnum.f32(float [[L]], float [[TMP]])
+; CHECK-NEXT:    [[IV_NEXT]] = add nuw nsw i64 [[IV]], 1
+; CHECK-NEXT:    [[EC:%.*]] = icmp eq i64 [[IV_NEXT]], [[N]]
+; CHECK-NEXT:    br i1 [[EC]], label %[[EXIT:.*]], label %[[LOOP]]
+; CHECK:       [[EXIT]]:
+; CHECK-NEXT:    [[MAX_NEXT_LCSSA:%.*]] = phi float [ [[MAX_NEXT]], %[[LOOP]] ]
+; CHECK-NEXT:    ret float [[MAX_NEXT_LCSSA]]
+;
+entry:
+  br label %loop
+
+loop:
+  %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
+  %max = phi float [ -1.000000e+07, %entry ], [ %max.next, %loop ]
+  %gep.src = getelementptr inbounds nuw float, ptr %src, i64 %iv
+  %l = load float, ptr %gep.src, align 4
+  %tmp = call float @llvm.maxnum.f32(float %k, float %max)
+  %max.next = call nnan nsz float @llvm.maxnum.f32(float %l, float %tmp)
+  %iv.next = add nuw nsw i64 %iv, 1
+  %ec = icmp eq i64 %iv.next, %n
+  br i1 %ec, label %exit, label %loop
+
+exit:
+  ret float %max.next
+}
+
 attributes #0 = { optsize }
diff --git a/llvm/test/Transforms/LoopVectorize/fmin-without-fast-math-flags.ll b/llvm/test/Transforms/LoopVectorize/fmin-without-fast-math-flags.ll
index 90a433c2594ac..24999536d936c 100644
--- a/llvm/test/Transforms/LoopVectorize/fmin-without-fast-math-flags.ll
+++ b/llvm/test/Transforms/LoopVectorize/fmin-without-fast-math-flags.ll
@@ -493,3 +493,41 @@ loop:
 exit:
   ret float %min.next
 }
+
+; The reduction chain contains an intermediate minnum.
+define float @fminnum_intermediate_minnum_op(ptr %src, i64 %n, float %k) {
+; CHECK-LABEL: define float @fminnum_intermediate_minnum_op(
+; CHECK-SAME: ptr [[SRC:%.*]], i64 [[N:%.*]], float [[K:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*]]:
+; CHECK-NEXT:    br label %[[LOOP:.*]]
+; CHECK:       [[LOOP]]:
+; CHECK-NEXT:    [[IV:%.*]] = phi i64 [ 0, %[[ENTRY]] ], [ [[IV_NEXT:%.*]], %[[LOOP]] ]
+; CHECK-NEXT:    [[MIN:%.*]] = phi float [ 1.000000e+07, %[[ENTRY]] ], [ [[MIN_NEXT:%.*]], %[[LOOP]] ]
+; CHECK-NEXT:    [[GEP_SRC:%.*]] = getelementptr inbounds nuw float, ptr [[SRC]], i64 [[IV]]
+; CHECK-NEXT:    [[L:%.*]] = load float, ptr [[GEP_SRC]], align 4
+; CHECK-NEXT:    [[TMP:%.*]] = call float @llvm.minnum.f32(float [[K]], float [[MIN]])
+; CHECK-NEXT:    [[MIN_NEXT]] = call nnan nsz float @llvm.minnum.f32(float [[L]], float [[TMP]])
+; CHECK-NEXT:    [[IV_NEXT]] = add nuw nsw i64 [[IV]], 1
+; CHECK-NEXT:    [[EC:%.*]] = icmp eq i64 [[IV_NEXT]], [[N]]
+; CHECK-NEXT:    br i1 [[EC]], label %[[EXIT:.*]], label %[[LOOP]]
+; CHECK:       [[EXIT]]:
+; CHECK-NEXT:    [[MIN_NEXT_LCSSA:%.*]] = phi float [ [[MIN_NEXT]], %[[LOOP]] ]
+; CHECK-NEXT:    ret float [[MIN_NEXT_LCSSA]]
+;
+entry:
+  br label %loop
+
+loop:
+  %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
+  %min = phi float [ 1.000000e+07, %entry ], [ %min.next, %loop ]
+  %gep.src = getelementptr inbounds nuw float, ptr %src, i64 %iv
+  %l = load float, ptr %gep.src, align 4
+  %tmp = call float @llvm.minnum.f32(float %k, float %min)
+  %min.next = call nnan nsz float @llvm.minnum.f32(float %l, float %tmp)
+  %iv.next = add nuw nsw i64 %iv, 1
+  %ec = icmp eq i64 %iv.next, %n
+  br i1 %ec, label %exit, label %loop
+
+exit:
+  ret float %min.next
+}



More information about the llvm-commits mailing list