[llvm] 2be4a9b - [LV] Add predicated early-exit tests showing poison prop issue. (NFC)
Florian Hahn via llvm-commits
llvm-commits at lists.llvm.org
Wed Mar 18 13:01:03 PDT 2026
Author: Florian Hahn
Date: 2026-03-18T20:00:45Z
New Revision: 2be4a9b1b2089e5752382c70046b0a8c1901ab85
URL: https://github.com/llvm/llvm-project/commit/2be4a9b1b2089e5752382c70046b0a8c1901ab85
DIFF: https://github.com/llvm/llvm-project/commit/2be4a9b1b2089e5752382c70046b0a8c1901ab85.diff
LOG: [LV] Add predicated early-exit tests showing poison prop issue. (NFC)
Add tests showing incorrect poison propagation from
https://github.com/llvm/llvm-project/issues/187061.
Added:
Modified:
llvm/test/Transforms/LoopVectorize/predicated-multiple-exits.ll
Removed:
################################################################################
diff --git a/llvm/test/Transforms/LoopVectorize/predicated-multiple-exits.ll b/llvm/test/Transforms/LoopVectorize/predicated-multiple-exits.ll
index e15d4fde3d70e..f54f6ec768bc4 100644
--- a/llvm/test/Transforms/LoopVectorize/predicated-multiple-exits.ll
+++ b/llvm/test/Transforms/LoopVectorize/predicated-multiple-exits.ll
@@ -713,3 +713,138 @@ loop.end:
%retval = phi i64 [ 1, %block.a ], [ 2, %block.c ], [ 0, %loop.latch ]
ret i64 %retval
}
+
+; When the else branch is speculatively executed for iv < 2, `sub nuw` wraps
+; producing poison. This poison condition is processed first in RPO.
+; Test for https://github.com/llvm/llvm-project/issues/187061.
+define i32 @diamond_exit_poison_from_speculated_branch() {
+; CHECK-LABEL: define i32 @diamond_exit_poison_from_speculated_branch() {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: br label %[[VECTOR_PH:.*]]
+; CHECK: [[VECTOR_PH]]:
+; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
+; CHECK: [[VECTOR_BODY]]:
+; CHECK-NEXT: [[TMP0:%.*]] = freeze <4 x i1> <i1 poison, i1 poison, i1 true, i1 true>
+; CHECK-NEXT: [[TMP1:%.*]] = call i1 @llvm.vector.reduce.or.v4i1(<4 x i1> [[TMP0]])
+; CHECK-NEXT: br i1 [[TMP1]], label %[[VECTOR_EARLY_EXIT_CHECK:.*]], label %[[MIDDLE_BLOCK:.*]]
+; CHECK: [[MIDDLE_BLOCK]]:
+; CHECK-NEXT: br label %[[LOOP_END:.*]]
+; CHECK: [[VECTOR_EARLY_EXIT_CHECK]]:
+; CHECK-NEXT: [[FIRST_ACTIVE_LANE:%.*]] = call i64 @llvm.experimental.cttz.elts.i64.v4i1(<4 x i1> <i1 poison, i1 poison, i1 true, i1 true>, i1 false)
+; CHECK-NEXT: [[TMP2:%.*]] = extractelement <4 x i1> <i1 poison, i1 poison, i1 false, i1 false>, i64 [[FIRST_ACTIVE_LANE]]
+; CHECK-NEXT: br i1 [[TMP2]], label %[[VECTOR_EARLY_EXIT_0:.*]], label %[[VECTOR_EARLY_EXIT_1:.*]]
+; CHECK: [[VECTOR_EARLY_EXIT_1]]:
+; CHECK-NEXT: [[TMP3:%.*]] = extractelement <4 x i32> <i32 10, i32 11, i32 1, i32 2>, i64 [[FIRST_ACTIVE_LANE]]
+; CHECK-NEXT: br label %[[LOOP_END]]
+; CHECK: [[VECTOR_EARLY_EXIT_0]]:
+; CHECK-NEXT: br label %[[UNREACHABLE_EXIT:.*]]
+; CHECK: [[UNREACHABLE_EXIT]]:
+; CHECK-NEXT: call void @llvm.trap()
+; CHECK-NEXT: unreachable
+; CHECK: [[LOOP_END]]:
+; CHECK-NEXT: [[RETVAL:%.*]] = phi i32 [ [[TMP3]], %[[VECTOR_EARLY_EXIT_1]] ], [ -1, %[[MIDDLE_BLOCK]] ]
+; CHECK-NEXT: ret i32 [[RETVAL]]
+;
+entry:
+ br label %loop.header
+
+loop.header:
+ %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop.latch ]
+ %cmp = icmp ult i32 %iv, 2
+ br i1 %cmp, label %then, label %else
+
+then:
+ %lo.val = add i32 %iv, 10
+ br label %loop.exiting
+
+else:
+ %sub = sub nuw i32 %iv, 2
+ %shl = shl nuw i32 1, %sub
+ %trap.cond = icmp eq i32 %shl, 999
+ br i1 %trap.cond, label %unreachable.exit, label %loop.exiting
+
+unreachable.exit:
+ call void @llvm.trap()
+ unreachable
+
+loop.exiting:
+ %val = phi i32 [ %lo.val, %then ], [ %shl, %else ]
+ %found.cond = icmp ult i32 %val, 12
+ br i1 %found.cond, label %loop.end, label %loop.latch
+
+loop.latch:
+ %iv.next = add nuw nsw i32 %iv, 1
+ %done = icmp eq i32 %iv.next, 4
+ br i1 %done, label %loop.end, label %loop.header
+
+loop.end:
+ %retval = phi i32 [ %val, %loop.exiting ], [ -1, %loop.latch ]
+ ret i32 %retval
+}
+
+; Same as above but the poison exit condition (trap.cond from the speculated
+; else branch) comes second in RPO. The first processed exit is from
+; the then branch (RPO-before else).
+define i32 @diamond_exit_poison_cond_second() {
+; CHECK-LABEL: define i32 @diamond_exit_poison_cond_second() {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: br label %[[VECTOR_PH:.*]]
+; CHECK: [[VECTOR_PH]]:
+; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
+; CHECK: [[VECTOR_BODY]]:
+; CHECK-NEXT: [[TMP0:%.*]] = freeze <4 x i1> <i1 poison, i1 poison, i1 false, i1 false>
+; CHECK-NEXT: [[TMP1:%.*]] = call i1 @llvm.vector.reduce.or.v4i1(<4 x i1> [[TMP0]])
+; CHECK-NEXT: br i1 [[TMP1]], label %[[VECTOR_EARLY_EXIT_CHECK:.*]], label %[[MIDDLE_BLOCK:.*]]
+; CHECK: [[MIDDLE_BLOCK]]:
+; CHECK-NEXT: br label %[[LOOP_END:.*]]
+; CHECK: [[VECTOR_EARLY_EXIT_CHECK]]:
+; CHECK-NEXT: [[FIRST_ACTIVE_LANE:%.*]] = call i64 @llvm.experimental.cttz.elts.i64.v4i1(<4 x i1> <i1 poison, i1 poison, i1 false, i1 false>, i1 false)
+; CHECK-NEXT: [[TMP2:%.*]] = extractelement <4 x i1> <i1 poison, i1 poison, i1 false, i1 false>, i64 [[FIRST_ACTIVE_LANE]]
+; CHECK-NEXT: br i1 [[TMP2]], label %[[VECTOR_EARLY_EXIT_0:.*]], label %[[VECTOR_EARLY_EXIT_1:.*]]
+; CHECK: [[VECTOR_EARLY_EXIT_1]]:
+; CHECK-NEXT: [[TMP3:%.*]] = extractelement <4 x i32> <i32 10, i32 11, i32 12, i32 13>, i64 [[FIRST_ACTIVE_LANE]]
+; CHECK-NEXT: br label %[[LOOP_END]]
+; CHECK: [[VECTOR_EARLY_EXIT_0]]:
+; CHECK-NEXT: br label %[[UNREACHABLE_EXIT:.*]]
+; CHECK: [[UNREACHABLE_EXIT]]:
+; CHECK-NEXT: call void @llvm.trap()
+; CHECK-NEXT: unreachable
+; CHECK: [[LOOP_END]]:
+; CHECK-NEXT: [[RETVAL:%.*]] = phi i32 [ [[TMP3]], %[[VECTOR_EARLY_EXIT_1]] ], [ -1, %[[MIDDLE_BLOCK]] ]
+; CHECK-NEXT: ret i32 [[RETVAL]]
+;
+entry:
+ br label %loop.header
+
+loop.header:
+ %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop.latch ]
+ %cmp = icmp ult i32 %iv, 2
+ br i1 %cmp, label %then, label %else
+
+then:
+ %val = add i32 %iv, 10
+ %found.cond = icmp ult i32 %val, 11
+ br i1 %found.cond, label %loop.end, label %loop.latch
+
+else:
+ ; sub nuw produces poison when speculatively executed for iv < 2.
+ %sub = sub nuw i32 %iv, 2
+ %shl = shl nuw i32 1, %sub
+ %trap.cond = icmp eq i32 %shl, 999
+ br i1 %trap.cond, label %unreachable.exit, label %loop.latch
+
+unreachable.exit:
+ call void @llvm.trap()
+ unreachable
+
+loop.latch:
+ %iv.next = add nuw nsw i32 %iv, 1
+ %done = icmp eq i32 %iv.next, 4
+ br i1 %done, label %loop.end, label %loop.header
+
+loop.end:
+ %retval = phi i32 [ %val, %then ], [ -1, %loop.latch ]
+ ret i32 %retval
+}
+
+declare void @llvm.trap()
More information about the llvm-commits
mailing list