[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:08:52 PDT 2025


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

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.

>From 6c9885ea3540adffa8f714b2e9d63808cc47b14b Mon Sep 17 00:00:00 2001
From: Max Dawkins <max.dawkins at gmail.com>
Date: Thu, 15 May 2025 18:55:52 +0000
Subject: [PATCH] [mlir] Check for int limits when converting gpu dims

Signed-off-by: Max Dawkins <max.dawkins at gmail.com>
---
 .../GPUCommon/IndexIntrinsicsOpLowering.h           |  4 +++-
 mlir/test/Conversion/GPUToROCDL/gpu-to-rocdl.mlir   | 13 +++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)

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
+  }
+}



More information about the Mlir-commits mailing list