[llvm] b898ae2 - [VPlan] Reject Argmin/Argmax with intermediate ops. (#218944)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 26 11:54:59 PDT 2026
Author: Florian Hahn
Date: 2026-08-26T19:54:54+01:00
New Revision: b898ae258849450e11cad08a8bafe6360242f5df
URL: https://github.com/llvm/llvm-project/commit/b898ae258849450e11cad08a8bafe6360242f5df
DIFF: https://github.com/llvm/llvm-project/commit/b898ae258849450e11cad08a8bafe6360242f5df.diff
LOG: [VPlan] Reject Argmin/Argmax with intermediate ops. (#218944)
handleMultiUseReductions currently only supports a single min/max
reduction step, not multiple intermediate ops.
Bail out instead of crashing in that case.
PR: https://github.com/llvm/llvm-project/pull/218944
Added:
Modified:
llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
llvm/test/Transforms/LoopVectorize/select-smax-last-index.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp b/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
index ad963fe9ff158..459e8f86cb337 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
@@ -2061,9 +2061,16 @@ bool VPlanTransforms::handleMultiUseReductions(VPlan &Plan,
// ComputeReductionResult.
assert(MinOrMaxOp->getNumUsers() == 2 &&
"MinOrMaxOp must have exactly 2 users");
- VPValue *MinOrMaxOpValue = MinOrMaxOp->getOperand(0);
- if (MinOrMaxOpValue == MinOrMaxPhiR)
+ // MinOrMaxOp must combine MinOrMaxPhiR directly with the new element;
+ // reject multi-step min/max chains (e.g. max(l, max(k, phi))), which
+ // this transform does not handle.
+ VPValue *MinOrMaxOpValue;
+ if (MinOrMaxOp->getOperand(0) == MinOrMaxPhiR)
MinOrMaxOpValue = MinOrMaxOp->getOperand(1);
+ else if (MinOrMaxOp->getOperand(1) == MinOrMaxPhiR)
+ MinOrMaxOpValue = MinOrMaxOp->getOperand(0);
+ else
+ return false;
VPValue *CmpOpA;
VPValue *CmpOpB;
@@ -2085,8 +2092,6 @@ bool VPlanTransforms::handleMultiUseReductions(VPlan &Plan,
VPInstruction *MinOrMaxResult =
findUserOf<VPInstruction::ComputeReductionResult>(MinOrMaxOp);
- assert(is_contained(MinOrMaxPhiR->users(), MinOrMaxOp) &&
- "one user must be MinOrMaxOp");
assert(MinOrMaxResult && "MinOrMaxResult must be a user of MinOrMaxOp");
// Cmp must be used by the select of a FindLastIV chain.
diff --git a/llvm/test/Transforms/LoopVectorize/select-smax-last-index.ll b/llvm/test/Transforms/LoopVectorize/select-smax-last-index.ll
index 4477e8c3e0909..3235ab99df7ab 100644
--- a/llvm/test/Transforms/LoopVectorize/select-smax-last-index.ll
+++ b/llvm/test/Transforms/LoopVectorize/select-smax-last-index.ll
@@ -733,3 +733,47 @@ exit:
ret i64 %res
}
+define i64 @test_smax_idx_intermediate_smax_op(ptr %src, i64 %n, i64 %k) {
+; CHECK-LABEL: define i64 @test_smax_idx_intermediate_smax_op(
+; CHECK-SAME: ptr [[SRC:%.*]], i64 [[N:%.*]], i64 [[K:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*]]:
+; CHECK-NEXT: br label %[[LOOP:.*]]
+; CHECK: [[LOOP]]:
+; CHECK-NEXT: [[IV:%.*]] = phi i64 [ 0, %[[ENTRY]] ], [ [[IV_NEXT:%.*]], %[[LOOP]] ]
+; CHECK-NEXT: [[MAX_IDX:%.*]] = phi i64 [ 0, %[[ENTRY]] ], [ [[MAX_IDX_NEXT:%.*]], %[[LOOP]] ]
+; CHECK-NEXT: [[MAX_VAL:%.*]] = phi i64 [ -2147483648, %[[ENTRY]] ], [ [[MAX_VAL_NEXT:%.*]], %[[LOOP]] ]
+; CHECK-NEXT: [[GEP:%.*]] = getelementptr i64, ptr [[SRC]], i64 [[IV]]
+; CHECK-NEXT: [[L:%.*]] = load i64, ptr [[GEP]], align 4
+; CHECK-NEXT: [[TMP:%.*]] = call i64 @llvm.smax.i64(i64 [[K]], i64 [[MAX_VAL]])
+; CHECK-NEXT: [[MAX_VAL_NEXT]] = call i64 @llvm.smax.i64(i64 [[L]], i64 [[TMP]])
+; CHECK-NEXT: [[CMP:%.*]] = icmp sle i64 [[MAX_VAL]], [[L]]
+; CHECK-NEXT: [[MAX_IDX_NEXT]] = select i1 [[CMP]], i64 [[IV]], i64 [[MAX_IDX]]
+; CHECK-NEXT: [[IV_NEXT]] = add nuw nsw i64 [[IV]], 1
+; CHECK-NEXT: [[EXITCOND_NOT:%.*]] = icmp eq i64 [[IV_NEXT]], [[N]]
+; CHECK-NEXT: br i1 [[EXITCOND_NOT]], label %[[EXIT:.*]], label %[[LOOP]]
+; CHECK: [[EXIT]]:
+; CHECK-NEXT: [[RES:%.*]] = phi i64 [ [[MAX_IDX_NEXT]], %[[LOOP]] ]
+; CHECK-NEXT: ret i64 [[RES]]
+;
+entry:
+ br label %loop
+
+loop:
+ %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
+ %max.idx = phi i64 [ 0, %entry ], [ %max.idx.next, %loop ]
+ %max.val = phi i64 [ -2147483648, %entry ], [ %max.val.next, %loop ]
+ %gep = getelementptr i64, ptr %src, i64 %iv
+ %l = load i64, ptr %gep
+ %tmp = call i64 @llvm.smax.i64(i64 %k, i64 %max.val)
+ %max.val.next = call i64 @llvm.smax.i64(i64 %l, i64 %tmp)
+ %cmp = icmp sle i64 %max.val, %l
+ %max.idx.next = select i1 %cmp, i64 %iv, i64 %max.idx
+ %iv.next = add nuw nsw i64 %iv, 1
+ %exitcond.not = icmp eq i64 %iv.next, %n
+ br i1 %exitcond.not, label %exit, label %loop
+
+exit:
+ %res = phi i64 [ %max.idx.next, %loop ]
+ ret i64 %res
+}
+
More information about the llvm-commits
mailing list