Skip to content

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

4 participants