[Mlir-commits] [mlir] [mlir] Check for int limits when converting gpu dims (PR #140747)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Tue May 20 08:09:30 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir-gpu

Author: None (Max191)

<details>
<summary>Changes</summary>

When the upper_bound of a gpu dim op (like `gpu.block_dim`) is the maximum i32 integer value, the op conversion for it causes overflow by adding 1 to convert the bound from closed to open. This fixes the bug by clamping the open bound to the MAX_INT value.

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


2 Files Affected:

- (modified) mlir/lib/Conversion/GPUCommon/IndexIntrinsicsOpLowering.h (+3-1) 
- (modified) mlir/test/Conversion/GPUToROCDL/gpu-to-rocdl.mlir (+13) 


``````````diff
diff --git a/mlir/lib/Conversion/GPUCommon/IndexIntrinsicsOpLowering.h b/mlir/lib/Conversion/GPUCommon/IndexIntrinsicsOpLowering.h
index 1f158b271e5c6..fa16deae7a93a 100644
--- a/mlir/lib/Conversion/GPUCommon/IndexIntrinsicsOpLowering.h
+++ b/mlir/lib/Conversion/GPUCommon/IndexIntrinsicsOpLowering.h
@@ -116,7 +116,9 @@ struct OpLowering : public ConvertOpToLLVMPattern<Op> {
 
     if (upperBound && intrType != IntrType::None) {
       int32_t min = (intrType == IntrType::Dim ? 1 : 0);
-      int32_t max = *upperBound + (intrType == IntrType::Id ? 0 : 1);
+      int32_t max = *upperBound == INT_MAX
+                        ? *upperBound
+                        : *upperBound + (intrType == IntrType::Id ? 0 : 1);
       newOp->setAttr("range", LLVM::ConstantRangeAttr::get(
                                   rewriter.getContext(), 32, min, max));
     }
diff --git a/mlir/test/Conversion/GPUToROCDL/gpu-to-rocdl.mlir b/mlir/test/Conversion/GPUToROCDL/gpu-to-rocdl.mlir
index d28aa9e34c22a..e57763c037341 100644
--- a/mlir/test/Conversion/GPUToROCDL/gpu-to-rocdl.mlir
+++ b/mlir/test/Conversion/GPUToROCDL/gpu-to-rocdl.mlir
@@ -763,3 +763,16 @@ gpu.module @test_module {
 gpu.module @test_custom_data_layout attributes {llvm.data_layout = "e"} {
 
 }
+
+// -----
+
+gpu.module @test_module {
+  // CHECK32-LABEL: func @gpu_dim_int_max_upper_bound()
+  func.func @gpu_dim_int_max_upper_bound()
+      -> (index) {
+
+    // CHECK32: rocdl.workgroup.dim.x range <i32, 1, 2147483647> : i32
+    %bDimX = gpu.block_dim x upper_bound 2147483647
+    func.return %bDimX : index
+  }
+}

``````````

</details>


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


More information about the Mlir-commits mailing list