[Mlir-commits] [mlir] [mlir][scf] Fix unsigned narrow type trip count calculation (PR #178060)

Jhalak Patel llvmlistbot at llvm.org
Tue Jan 27 12:00:35 PST 2026


================
@@ -336,7 +336,10 @@ std::optional<APInt> constantTripCount(
       // case applies, so the static trip count is unknown.
       return std::nullopt;
     }
-    if (stepCst.isNegative())
+    // For unsigned values, negative step is impossible; for signed, check the
+    // sign bit properly using APSInt.
+    APSInt stepSInt(stepCst, /*isUnsigned=*/!isSigned);
+    if (stepSInt.isNegative())
       return APInt(bitwidth, 0);
----------------
jhalakpatel wrote:

Good point. IIUC, we are encoding `for (i = lb; i < ub; i += step)` where `lb < ub` and a positive `step` is needed for a finite iteration. For cases, where `lb > ub`, it should be a 0 trip count irrespective of step value.

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


More information about the Mlir-commits mailing list