[llvm] [LoopVectorize] Don't speculate an early-exit trip count that may cause UB (PR #219893)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Aug 30 23:43:41 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-vectorizers
@llvm/pr-subscribers-llvm-transforms
Author: Madhur Amilkanthwar (madhur13490)
<details>
<summary>Changes</summary>
For a loop with an uncountable early exit, the trip count is derived from the latch exit and expanded in the preheader, above the early exit. If it contains a udiv whose divisor may be zero or poison, that speculates UB.
Bail out in this case, reusing ScalarEvolution::isGuaranteedNotToCauseUB (made public here). Loops whose divisor is known non-zero and not poison are still vectorized.
Fixes https://github.com/llvm/llvm-project/issues/219371.
---
Full diff: https://github.com/llvm/llvm-project/pull/219893.diff
3 Files Affected:
- (modified) llvm/include/llvm/Analysis/ScalarEvolution.h (+5-3)
- (modified) llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp (+19-2)
- (modified) llvm/test/Transforms/LoopVectorize/early-exit-trip-count-may-cause-ub.ll (+9-37)
``````````diff
diff --git a/llvm/include/llvm/Analysis/ScalarEvolution.h b/llvm/include/llvm/Analysis/ScalarEvolution.h
index 385a8e3f0d3bb..594fc3c478a76 100644
--- a/llvm/include/llvm/Analysis/ScalarEvolution.h
+++ b/llvm/include/llvm/Analysis/ScalarEvolution.h
@@ -1248,6 +1248,11 @@ class ScalarEvolution {
/// Returns true if \p Op is guaranteed to not be poison.
LLVM_ABI static bool isGuaranteedNotToBePoison(const SCEV *Op);
+ /// Returns true if \p Op is guaranteed not to cause immediate UB. Use this
+ /// to check whether \p Op can be expanded at a point it may not have been
+ /// evaluated at in the original program.
+ LLVM_ABI bool isGuaranteedNotToCauseUB(const SCEV *Op);
+
/// Test if the given expression is known to be a power of 2. OrNegative
/// allows matching negative power of 2s, and OrZero allows matching 0.
LLVM_ABI bool isKnownToBeAPowerOfTwo(const SCEV *S, bool OrZero = false,
@@ -2454,9 +2459,6 @@ class ScalarEvolution {
bool isGuaranteedToTransferExecutionTo(const Instruction *A,
const Instruction *B);
- /// Returns true if \p Op is guaranteed not to cause immediate UB.
- bool isGuaranteedNotToCauseUB(const SCEV *Op);
-
/// Return true if the SCEV corresponding to \p I is never poison. Proving
/// this is more complex than proving that just \p I is never poison, since
/// SCEV commons expressions across control flow, and you can have cases
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
index f1d785b571367..f8ba9cb520cbe 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
@@ -1787,12 +1787,29 @@ bool LoopVectorizationLegality::isVectorizableEarlyExitLoop() {
}
}
- [[maybe_unused]] const SCEV *SymbolicMaxBTC =
- PSE.getSymbolicMaxBackedgeTakenCount();
+ const SCEV *SymbolicMaxBTC = PSE.getSymbolicMaxBackedgeTakenCount();
// Since we have an exact exit count for the latch and the early exit
// dominates the latch, then this should guarantee a computed SCEV value.
assert(!isa<SCEVCouldNotCompute>(SymbolicMaxBTC) &&
"Failed to get symbolic expression for backedge taken count");
+
+ // The symbolic max backedge-taken count is built from the countable exits
+ // only, which the original loop reaches only if no uncountable exit is taken
+ // first. Expanding it in the preheader therefore speculates it above those
+ // uncountable exits, so it must not cause UB on its own. Note that the
+ // sequential umin ScalarEvolution uses to keep the exit counts of multiple
+ // countable exits from short-circuiting does not help here, as uncountable
+ // exits contribute no operand to it.
+ // TODO: Rather than giving up, expand a divisor that may be zero or poison
+ // as freeze + umax(_, 1), like SCEVExpander does for the operands of a
+ // sequential umin.
+ if (!PSE.getSE()->isGuaranteedNotToCauseUB(SymbolicMaxBTC)) {
+ reportVectorizationFailure(
+ "Backedge taken count may cause UB when expanded into the preheader",
+ "PotentiallyUBBackedgeTakenCountEarlyExitLoop", ORE, TheLoop);
+ return false;
+ }
+
LLVM_DEBUG(dbgs() << "LV: Found an early exit loop with symbolic max "
"backedge taken count: "
<< *SymbolicMaxBTC << '\n');
diff --git a/llvm/test/Transforms/LoopVectorize/early-exit-trip-count-may-cause-ub.ll b/llvm/test/Transforms/LoopVectorize/early-exit-trip-count-may-cause-ub.ll
index 2f9a37918c65b..d521d766cd017 100644
--- a/llvm/test/Transforms/LoopVectorize/early-exit-trip-count-may-cause-ub.ll
+++ b/llvm/test/Transforms/LoopVectorize/early-exit-trip-count-may-cause-ub.ll
@@ -10,53 +10,25 @@ declare i32 @llvm.cttz.i32(i32, i1 immarg)
; well defined on that path must not be computed unconditionally in the
; preheader.
-; FIXME: %ct is poison when %x is 0, so %d may be poison. The udiv is currently
-; speculated into the preheader, where it can divide by poison, which is UB. The
-; original loop returns 0 without evaluating %d when %skip is true, so this is a
-; miscompile and the loop must not be vectorized.
+; %ct is poison when %x is 0, so %d may be poison and speculating (63 /u %d)
+; would divide by poison, which is UB. The original loop returns 0 without ever
+; evaluating %d when %skip is true, so do not vectorize.
define i32 @udiv_by_poison_step(i32 %x, i1 %skip) {
; CHECK-LABEL: define i32 @udiv_by_poison_step(
; CHECK-SAME: i32 [[X:%.*]], i1 [[SKIP:%.*]]) {
; CHECK-NEXT: [[ENTRY:.*]]:
; CHECK-NEXT: [[CT:%.*]] = call i32 @llvm.cttz.i32(i32 [[X]], i1 true)
; CHECK-NEXT: [[D:%.*]] = add nuw nsw i32 [[CT]], 1
-; CHECK-NEXT: [[TMP0:%.*]] = udiv i32 63, [[D]]
-; CHECK-NEXT: [[TMP1:%.*]] = add nuw nsw i32 [[TMP0]], 1
-; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i32 [[TMP1]], 4
-; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
-; CHECK: [[VECTOR_PH]]:
-; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP1]], 3
-; CHECK-NEXT: [[N_VEC:%.*]] = sub i32 [[TMP1]], [[TMP2]]
-; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x i1> poison, i1 [[SKIP]], i64 0
-; CHECK-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x i1> [[BROADCAST_SPLATINSERT]], <4 x i1> poison, <4 x i32> zeroinitializer
-; CHECK-NEXT: [[TMP3:%.*]] = mul i32 [[N_VEC]], [[D]]
-; CHECK-NEXT: [[TMP4:%.*]] = freeze <4 x i1> [[BROADCAST_SPLAT]]
-; CHECK-NEXT: [[TMP5:%.*]] = call i1 @llvm.vector.reduce.or.v4i1(<4 x i1> [[TMP4]])
-; CHECK-NEXT: br label %[[LOOP:.*]]
-; CHECK: [[LOOP]]:
-; CHECK-NEXT: [[INDEX:%.*]] = phi i32 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY_INTERIM:.*]] ]
-; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i32 [[INDEX]], 4
-; CHECK-NEXT: [[TMP6:%.*]] = icmp eq i32 [[INDEX_NEXT]], [[N_VEC]]
-; CHECK-NEXT: br i1 [[TMP5]], label %[[VECTOR_EARLY_EXIT:.*]], label %[[VECTOR_BODY_INTERIM]]
-; CHECK: [[VECTOR_BODY_INTERIM]]:
-; CHECK-NEXT: br i1 [[TMP6]], label %[[MIDDLE_BLOCK:.*]], label %[[LOOP]], !llvm.loop [[LOOP0:![0-9]+]]
-; CHECK: [[MIDDLE_BLOCK]]:
-; CHECK-NEXT: [[CMP_N:%.*]] = icmp eq i32 [[TMP1]], [[N_VEC]]
-; CHECK-NEXT: br i1 [[CMP_N]], label %[[EXIT:.*]], label %[[SCALAR_PH]]
-; CHECK: [[VECTOR_EARLY_EXIT]]:
-; CHECK-NEXT: br label %[[EXIT]]
-; CHECK: [[SCALAR_PH]]:
-; CHECK-NEXT: [[BC_RESUME_VAL:%.*]] = phi i32 [ [[TMP3]], %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
; CHECK-NEXT: br label %[[LOOP1:.*]]
; CHECK: [[LOOP1]]:
-; CHECK-NEXT: [[I:%.*]] = phi i32 [ [[BC_RESUME_VAL]], %[[SCALAR_PH]] ], [ [[INC:%.*]], %[[LATCH:.*]] ]
-; CHECK-NEXT: br i1 [[SKIP]], label %[[EXIT]], label %[[LATCH]]
+; CHECK-NEXT: [[I:%.*]] = phi i32 [ 0, %[[ENTRY]] ], [ [[INC:%.*]], %[[LATCH:.*]] ]
+; CHECK-NEXT: br i1 [[SKIP]], label %[[EXIT:.*]], label %[[LATCH]]
; CHECK: [[LATCH]]:
; CHECK-NEXT: [[INC]] = add nuw nsw i32 [[I]], [[D]]
; CHECK-NEXT: [[DONE:%.*]] = icmp uge i32 [[INC]], 64
-; CHECK-NEXT: br i1 [[DONE]], label %[[EXIT]], label %[[LOOP1]], !llvm.loop [[LOOP3:![0-9]+]]
+; CHECK-NEXT: br i1 [[DONE]], label %[[EXIT]], label %[[LOOP1]], !llvm.loop [[LOOP0:![0-9]+]]
; CHECK: [[EXIT]]:
-; CHECK-NEXT: [[R:%.*]] = phi i32 [ 0, %[[LOOP1]] ], [ [[INC]], %[[LATCH]] ], [ [[TMP3]], %[[MIDDLE_BLOCK]] ], [ 0, %[[VECTOR_EARLY_EXIT]] ]
+; CHECK-NEXT: [[R:%.*]] = phi i32 [ 0, %[[LOOP1]] ], [ [[INC]], %[[LATCH]] ]
; CHECK-NEXT: ret i32 [[R]]
;
entry:
@@ -106,7 +78,7 @@ define i32 @udiv_by_nonzero_nonpoison_step(i32 noundef %x, i1 %skip) {
; CHECK-NEXT: [[TMP6:%.*]] = icmp eq i32 [[INDEX_NEXT]], [[N_VEC]]
; CHECK-NEXT: br i1 [[TMP5]], label %[[VECTOR_EARLY_EXIT:.*]], label %[[VECTOR_BODY_INTERIM]]
; CHECK: [[VECTOR_BODY_INTERIM]]:
-; CHECK-NEXT: br i1 [[TMP6]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP4:![0-9]+]]
+; CHECK-NEXT: br i1 [[TMP6]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP3:![0-9]+]]
; CHECK: [[MIDDLE_BLOCK]]:
; CHECK-NEXT: [[CMP_N:%.*]] = icmp eq i32 [[TMP1]], [[N_VEC]]
; CHECK-NEXT: br i1 [[CMP_N]], label %[[EXIT:.*]], label %[[SCALAR_PH]]
@@ -121,7 +93,7 @@ define i32 @udiv_by_nonzero_nonpoison_step(i32 noundef %x, i1 %skip) {
; CHECK: [[LATCH]]:
; CHECK-NEXT: [[INC]] = add nuw nsw i32 [[I]], [[D]]
; CHECK-NEXT: [[DONE:%.*]] = icmp uge i32 [[INC]], 64
-; CHECK-NEXT: br i1 [[DONE]], label %[[EXIT]], label %[[LOOP]], !llvm.loop [[LOOP5:![0-9]+]]
+; CHECK-NEXT: br i1 [[DONE]], label %[[EXIT]], label %[[LOOP]], !llvm.loop [[LOOP6:![0-9]+]]
; CHECK: [[EXIT]]:
; CHECK-NEXT: [[R:%.*]] = phi i32 [ 0, %[[LOOP]] ], [ [[INC]], %[[LATCH]] ], [ [[TMP3]], %[[MIDDLE_BLOCK]] ], [ 0, %[[VECTOR_EARLY_EXIT]] ]
; CHECK-NEXT: ret i32 [[R]]
``````````
</details>
https://github.com/llvm/llvm-project/pull/219893
More information about the llvm-commits
mailing list