Skip to content

[MLIR][Utils] Fix the overflow issue in computeSuffixProductImpl for 32-bit system. #140567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2025

Conversation

chencha3
Copy link
Contributor

@chencha3 chencha3 commented May 19, 2025

In int64_t r = strides.size() - 2, it may cause overflow on 32-bit system when strides.size() is 1, because strides.size() is a size_t which is a unsigned int (32-bit), leading a wrong computation of 1 - 2 = 4294967295 on unsigned int after casting to int64_t.

@chencha3 chencha3 requested a review from Mogball as a code owner May 19, 2025 16:11
@llvmbot llvmbot added the mlir label May 19, 2025
@llvmbot
Copy link
Member

llvmbot commented May 19, 2025

@llvm/pr-subscribers-mlir

Author: Chao Chen (chencha3)

Changes

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).


Full diff: https://github.com/llvm/llvm-project/pull/140567.diff

1 Files Affected:

  • (modified) mlir/lib/Dialect/Utils/IndexingUtils.cpp (+1-1)
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;
 }

@mgorny
Copy link
Member

mgorny commented May 19, 2025

Thanks. I've started the test run.

Copy link
Member

@mgorny mgorny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot! I can confirm that this fixes both the regression, and a bunch of other test failures we've seen earlier. Also, I'm sorry — this wasn't your fault after all.

@chencha3
Copy link
Contributor Author

Thanks a lot! I can confirm that this fixes both the regression, and a bunch of other test failures we've seen earlier. Also, I'm sorry — this wasn't your fault after all.

No worries. Happy to contribute my effort.

@chencha3 chencha3 merged commit 99720bb into main May 19, 2025
13 checks passed
@chencha3 chencha3 deleted the users/chencha3/indexUtil/fix_overflow_issue branch May 19, 2025 18:27
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Jun 3, 2025
…32-bit system. (llvm#140567)

In `int64_t r = strides.size() - 2`, it may cause overflow on 32-bit
system when strides.size() is 1, because `strides.size()` is defined 
as `unsigned int`
ajaden-codes pushed a commit to Jaddyen/llvm-project that referenced this pull request Jun 6, 2025
…32-bit system. (llvm#140567)

In `int64_t r = strides.size() - 2`, it may cause overflow on 32-bit
system when strides.size() is 1, because `strides.size()` is defined 
as `unsigned int`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants