[Mlir-commits] [mlir] [mlir][scf] Fix trip count signedness and overflow in SCF Utils (PR #178782)

Matthias Springer llvmlistbot at llvm.org
Fri Jan 30 09:30:31 PST 2026


================
@@ -397,14 +397,26 @@ FailureOr<UnrolledLoopInfo> mlir::loopUnrollByFactor(
       return UnrolledLoopInfo{forOp, std::nullopt};
     }
 
-    // TODO(#178506): This may overflow for large trip counts. Should use
-    // uint64_t.
-    int64_t tripCountEvenMultiple =
-        constTripCount->getZExtValue() -
-        (constTripCount->getZExtValue() % unrollFactor);
-    // TODO(#178506): This may overflow when computing upperBoundUnrolledCst.
-    int64_t upperBoundUnrolledCst = lbCst + tripCountEvenMultiple * stepCst;
-    int64_t stepUnrolledCst = stepCst * unrollFactor;
+    // Check for overflow before extracting trip count.
+    if (constTripCount->getActiveBits() > 64)
+      return failure();
+    uint64_t tripCountValue = constTripCount->getZExtValue();
----------------
matthias-springer wrote:

I think these could also all be `APInt`, then we don't need any bitwidth checks. In the end, we create an `arith::ConstantOp` with the same type as the `scf.for` (which has the same bitwidth as the `APInt`, so that works out nicely).

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


More information about the Mlir-commits mailing list