[Mlir-commits] [mlir] [MLIR][Utils] Fix the overflow issue in computeSuffixProductImpl for 32-bit system. (PR #140567)

Chao Chen llvmlistbot at llvm.org
Mon May 19 09:11:30 PDT 2025


https://github.com/chencha3 created https://github.com/llvm/llvm-project/pull/140567

In `int64_t r = strides.size() - 2`, it may cause overflow on 32-bit system when strides.size() is 1. be cause `strides.size()` is a `size_t` which is a `unsigned int` (32-bit), leading 1 - 2 = 4294967295 (on unsigned computing). 

>From 3807eeaf672c17b77b2b2fe8733709aab3f52842 Mon Sep 17 00:00:00 2001
From: Chao Chen <chao.chen at intel.com>
Date: Mon, 19 May 2025 16:06:03 +0000
Subject: [PATCH] fix overflow

---
 mlir/lib/Dialect/Utils/IndexingUtils.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mlir/lib/Dialect/Utils/IndexingUtils.cpp b/mlir/lib/Dialect/Utils/IndexingUtils.cpp
index d9edabef6693d..8de77e2c3cb08 100644
--- a/mlir/lib/Dialect/Utils/IndexingUtils.cpp
+++ b/mlir/lib/Dialect/Utils/IndexingUtils.cpp
@@ -24,7 +24,7 @@ SmallVector<ExprType> computeSuffixProductImpl(ArrayRef<ExprType> sizes,
   if (sizes.empty())
     return {};
   SmallVector<ExprType> strides(sizes.size(), unit);
-  for (int64_t r = strides.size() - 2; r >= 0; --r)
+  for (int64_t r = static_cast<int64_t>(strides.size()) - 2; r >= 0; --r)
     strides[r] = strides[r + 1] * sizes[r + 1];
   return strides;
 }



More information about the Mlir-commits mailing list