[llvm] [NVPTX] NVVMIntrRange: Handle maxntid > INT32_MAX. (PR #201245)

via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 2 17:58:51 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-nvptx

Author: Justin Lebar (jlebar)

<details>
<summary>Changes</summary>

Previously we computed the overall maxntid and downcast it to unsigned
int.  This is not correct; it can be larger than UINT32_MAX.

This would cause reads of tid.xyz and ntid.xyz to have incorrect range
information.  Also if maxntid was an exact multiple of 2^32, we'd get an
ICE (because we'd incorrectly think that maxntid is 0).


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


2 Files Affected:

- (modified) llvm/lib/Target/NVPTX/NVVMIntrRange.cpp (+7-6) 
- (modified) llvm/test/CodeGen/NVPTX/intr-range.ll (+47-1) 


``````````diff
diff --git a/llvm/lib/Target/NVPTX/NVVMIntrRange.cpp b/llvm/lib/Target/NVPTX/NVVMIntrRange.cpp
index ed1d880f612c1..a00bb85a19c73 100644
--- a/llvm/lib/Target/NVPTX/NVVMIntrRange.cpp
+++ b/llvm/lib/Target/NVPTX/NVVMIntrRange.cpp
@@ -67,17 +67,17 @@ static bool runNVVMIntrRange(Function &F) {
     return false;
 
   auto ReqNTID = getReqNTID(F);
-  const auto OverallMaxNTID = getOverallMaxNTID(F);
+  const std::optional<uint64_t> OverallMaxNTID = getOverallMaxNTID(F);
   auto ClusterDim = getClusterDim(F);
-  const auto MaxClusterRank = getMaxClusterRank(F);
+  const std::optional<unsigned> MaxClusterRank = getMaxClusterRank(F);
 
   // If this function lacks any range information, do nothing.
   if (ReqNTID.empty() && !OverallMaxNTID && ClusterDim.empty() &&
       !MaxClusterRank)
     return false;
 
-  const unsigned MaxNTID =
-      OverallMaxNTID.value_or(std::numeric_limits<unsigned>::max());
+  const uint64_t MaxNTID =
+      OverallMaxNTID.value_or(std::numeric_limits<uint64_t>::max());
 
   // When reqntid is specified, block dimensions are exact compile-time
   // constants. Otherwise, use maxntid (capped at hardware limits) as upper
@@ -88,8 +88,9 @@ static bool runNVVMIntrRange(Function &F) {
     MinBlockDim = MaxBlockDim = {ReqNTID[0], ReqNTID[1], ReqNTID[2]};
   } else {
     MinBlockDim = {1, 1, 1};
-    MaxBlockDim = {std::min(1024u, MaxNTID), std::min(1024u, MaxNTID),
-                   std::min(64u, MaxNTID)};
+    MaxBlockDim = {static_cast<unsigned>(std::min(uint64_t{1024}, MaxNTID)),
+                   static_cast<unsigned>(std::min(uint64_t{1024}, MaxNTID)),
+                   static_cast<unsigned>(std::min(uint64_t{64}, MaxNTID))};
   }
 
   const bool HasClusterInfo = !ClusterDim.empty() || MaxClusterRank;
diff --git a/llvm/test/CodeGen/NVPTX/intr-range.ll b/llvm/test/CodeGen/NVPTX/intr-range.ll
index cd35fd1ddfc0c..1fb43dc038f25 100644
--- a/llvm/test/CodeGen/NVPTX/intr-range.ll
+++ b/llvm/test/CodeGen/NVPTX/intr-range.ll
@@ -1,4 +1,4 @@
-; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-attributes --version 5
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-attributes --check-globals all --version 5
 ; RUN: opt < %s -S -mtriple=nvptx-nvidia-cuda -mcpu=sm_20 -passes=nvvm-intr-range | FileCheck %s
 ; RUN: llvm-as < %s | llvm-dis | FileCheck %s --check-prefix=DEFAULT
 
@@ -221,3 +221,49 @@ declare i32 @llvm.nvvm.read.ptx.sreg.cluster.nctaid.x()
 declare i32 @llvm.nvvm.read.ptx.sreg.cluster.nctaid.y()
 declare i32 @llvm.nvvm.read.ptx.sreg.cluster.nctaid.z()
 declare i32 @llvm.nvvm.read.ptx.sreg.cluster.nctaid.w()
+
+
+; The product of all maxntid dims can exceed INT32_MAX.  Make sure we still get
+; correct ranges on (n)tid.(x|y).
+define ptx_kernel i32 @test_maxntid_product_overflow() "nvvm.maxntid"="641,6700417" {
+; CHECK-LABEL: define ptx_kernel i32 @test_maxntid_product_overflow(
+; CHECK-SAME: ) #[[ATTR6:[0-9]+]] {
+; CHECK-NEXT:    [[TMP1:%.*]] = call range(i32 0, 1024) i32 @llvm.nvvm.read.ptx.sreg.tid.x()
+; CHECK-NEXT:    [[TMP2:%.*]] = call range(i32 1, 1025) i32 @llvm.nvvm.read.ptx.sreg.ntid.x()
+; CHECK-NEXT:    [[TMP3:%.*]] = call range(i32 0, 1024) i32 @llvm.nvvm.read.ptx.sreg.tid.y()
+; CHECK-NEXT:    [[TMP4:%.*]] = call range(i32 1, 1025) i32 @llvm.nvvm.read.ptx.sreg.ntid.y()
+; CHECK-NEXT:    [[TMP5:%.*]] = add i32 [[TMP1]], [[TMP2]]
+; CHECK-NEXT:    [[TMP6:%.*]] = add i32 [[TMP5]], [[TMP3]]
+; CHECK-NEXT:    [[TMP7:%.*]] = add i32 [[TMP6]], [[TMP4]]
+; CHECK-NEXT:    ret i32 [[TMP7]]
+;
+  %1 = call i32 @llvm.nvvm.read.ptx.sreg.tid.x()
+  %2 = call i32 @llvm.nvvm.read.ptx.sreg.ntid.x()
+  %3 = call i32 @llvm.nvvm.read.ptx.sreg.tid.y()
+  %4 = call i32 @llvm.nvvm.read.ptx.sreg.ntid.y()
+  %5 = add i32 %1, %2
+  %6 = add i32 %5, %3
+  %7 = add i32 %6, %4
+  ret i32 %7
+}
+
+; Try a maxntid product that is an exact multiple of 2^32 (65536*65536*1).
+define ptx_kernel i32 @test_maxntid_product_2pow32() "nvvm.maxntid"="65536,65536,1" {
+; CHECK-LABEL: define ptx_kernel i32 @test_maxntid_product_2pow32(
+; CHECK-SAME: ) #[[ATTR7:[0-9]+]] {
+; CHECK-NEXT:    [[TMP1:%.*]] = call range(i32 1, 1025) i32 @llvm.nvvm.read.ptx.sreg.ntid.x()
+; CHECK-NEXT:    ret i32 [[TMP1]]
+;
+  %1 = call i32 @llvm.nvvm.read.ptx.sreg.ntid.x()
+  ret i32 %1
+}
+;.
+; CHECK: attributes #[[ATTR0]] = { "nvvm.maxntid"="32,1,3" "target-cpu"="sm_20" }
+; CHECK: attributes #[[ATTR1]] = { "nvvm.reqntid"="20" "target-cpu"="sm_20" }
+; CHECK: attributes #[[ATTR2]] = { "nvvm.maxntid"="4" "target-cpu"="sm_20" }
+; CHECK: attributes #[[ATTR3]] = { "nvvm.maxclusterrank"="8" "target-cpu"="sm_20" }
+; CHECK: attributes #[[ATTR4]] = { "nvvm.cluster_dim"="4,4,1" "target-cpu"="sm_20" }
+; CHECK: attributes #[[ATTR5:[0-9]+]] = { nocallback nofree nosync nounwind speculatable willreturn memory(none) "target-cpu"="sm_20" }
+; CHECK: attributes #[[ATTR6]] = { "nvvm.maxntid"="641,6700417" "target-cpu"="sm_20" }
+; CHECK: attributes #[[ATTR7]] = { "nvvm.maxntid"="65536,65536,1" "target-cpu"="sm_20" }
+;.

``````````

</details>


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


More information about the llvm-commits mailing list