[llvm] [NVPTX] Add `sharedshortptr` ABI (PR #215422)

Justin Fargnoli via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 10 16:58:11 PDT 2026


https://github.com/justinfargnoli updated https://github.com/llvm/llvm-project/pull/215422

>From 5eedb078cb416f8415d4757af4181793ccf8125f Mon Sep 17 00:00:00 2001
From: Justin Fargnoli <jfargnoli at nvidia.com>
Date: Wed, 5 Aug 2026 01:36:25 +0000
Subject: [PATCH] [NVPTX] Add "sharedshortptr" ABI

---
 llvm/lib/TargetParser/TargetDataLayout.cpp    | 31 +++++++++++++------
 .../NVPTX/distributed-shared-cluster.ll       |  4 +++
 .../CodeGen/NVPTX/lower-args-gridconstant.ll  |  1 +
 llvm/test/CodeGen/NVPTX/short-ptr.ll          | 10 ++++++
 llvm/unittests/TargetParser/TripleTest.cpp    |  6 ++++
 5 files changed, 42 insertions(+), 10 deletions(-)

diff --git a/llvm/lib/TargetParser/TargetDataLayout.cpp b/llvm/lib/TargetParser/TargetDataLayout.cpp
index 8b6f46642e4fa..75594590eaf60 100644
--- a/llvm/lib/TargetParser/TargetDataLayout.cpp
+++ b/llvm/lib/TargetParser/TargetDataLayout.cpp
@@ -456,32 +456,43 @@ static std::string computeX86DataLayout(const Triple &TT) {
 }
 
 static std::string computeNVPTXDataLayout(const Triple &T, StringRef ABIName) {
+  assert((ABIName.empty() || ABIName == "shortptr" ||
+          ABIName == "sharedshortptr") &&
+         "Unsupported NVPTX ABI");
+
   const bool Is32Bit = T.getArch() == Triple::nvptx;
   const bool IsShortPtr = ABIName == "shortptr";
+  const bool IsSharedShortPtr = ABIName == "sharedshortptr";
   std::string Ret = "e";
 
   if (Is32Bit) {
     Ret += "-p:32:32";
   } else {
     // Keep the pointer specifications sorted by address space.
-    //
-    // In shortptr mode, specify the following address spaces as 32-bits:
-    // - shared (addrspace:3)
-    // - constant (addrspace:4)
-    // - local (addrspace:5)
-    // - shared cluster (addrspace:7)
-    // - entry parameter (addrspace:101)
+    // shared (addrspace:3)
+    if (IsShortPtr || IsSharedShortPtr)
+      Ret += "-p3:32:32";
+
+    // constant (addrspace:4)
+    // local (addrspace:5)
     if (IsShortPtr)
-      Ret += "-p3:32:32-p4:32:32-p5:32:32";
+      Ret += "-p4:32:32-p5:32:32";
 
     // Tensor Memory (addrspace:6) is always 32-bits.
     Ret += "-p6:32:32";
 
+    // shared cluster (addrspace:7)
+    if (IsShortPtr || IsSharedShortPtr)
+      Ret += "-p7:32:32";
+
+    // entry parameter (addrspace:101)
     if (IsShortPtr)
-      Ret += "-p7:32:32-p101:32:32";
+      Ret += "-p101:32:32";
   }
 
-  Ret += "-i64:64-i128:128-i256:256-v16:16-v32:32-n16:32:64";
+  Ret += "-i64:64-i128:128-i256:256";
+  Ret += "-v16:16-v32:32";
+  Ret += "-n16:32:64";
 
   return Ret;
 }
diff --git a/llvm/test/CodeGen/NVPTX/distributed-shared-cluster.ll b/llvm/test/CodeGen/NVPTX/distributed-shared-cluster.ll
index 932f8addfaf38..27a4d570202bd 100644
--- a/llvm/test/CodeGen/NVPTX/distributed-shared-cluster.ll
+++ b/llvm/test/CodeGen/NVPTX/distributed-shared-cluster.ll
@@ -1,5 +1,6 @@
 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
 ; RUN: llc < %s -o - -mcpu=sm_90 -mattr=+ptx78 | FileCheck %s
+; RUN: llc < %s -o - -mcpu=sm_90 -mattr=+ptx78 -target-abi=sharedshortptr | FileCheck %s --check-prefix=CHECK-SHARED-SHORT
 ; RUN: %if ptxas-sm_90 && ptxas-isa-7.8 %{ llc < %s -mcpu=sm_90 -mattr=+ptx78| %ptxas-verify -arch=sm_90 %}
 
 target triple = "nvptx64-nvidia-cuda"
@@ -11,6 +12,9 @@ declare ptr @llvm.nvvm.mapa(ptr, i32)
 
 ; Common setup for distributed shared memory cluster addressing
 define i32 @test_distributed_shared_cluster_common(ptr %ptr, ptr addrspace(3) %smem_ptr) local_unnamed_addr {
+; CHECK-SHARED-SHORT-LABEL: test_distributed_shared_cluster_common(
+; CHECK-SHARED-SHORT:         .param .b32 test_distributed_shared_cluster_common_param_1
+; CHECK-SHARED-SHORT:         mapa.shared::cluster.u32
 ; CHECK-LABEL: test_distributed_shared_cluster_common(
 ; CHECK:       {
 ; CHECK-NEXT:    .reg .pred %p<3>;
diff --git a/llvm/test/CodeGen/NVPTX/lower-args-gridconstant.ll b/llvm/test/CodeGen/NVPTX/lower-args-gridconstant.ll
index 5b23f009fea6c..634e0937a575f 100644
--- a/llvm/test/CodeGen/NVPTX/lower-args-gridconstant.ll
+++ b/llvm/test/CodeGen/NVPTX/lower-args-gridconstant.ll
@@ -1,6 +1,7 @@
 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
 ; RUN: opt < %s -S -nvptx-lower-args -mcpu=sm_70 -mattr=+ptx77 | FileCheck %s --check-prefixes OPT
 ; RUN: llc < %s -mcpu=sm_70 -mattr=+ptx77 -O1 | FileCheck %s --check-prefixes PTX,PTX-DEFAULT
+; RUN: llc < %s -mcpu=sm_70 -mattr=+ptx77 -O1 -target-abi=sharedshortptr | FileCheck %s --check-prefixes PTX,PTX-DEFAULT
 ; RUN: llc < %s -mcpu=sm_70 -mattr=+ptx77 -O1 -target-abi=shortptr | FileCheck %s --check-prefixes PTX,PTX-SHORT-PTR
 
 target triple = "nvptx64-nvidia-cuda"
diff --git a/llvm/test/CodeGen/NVPTX/short-ptr.ll b/llvm/test/CodeGen/NVPTX/short-ptr.ll
index e7bcc6cbeb9ee..9b62ad44c42a5 100644
--- a/llvm/test/CodeGen/NVPTX/short-ptr.ll
+++ b/llvm/test/CodeGen/NVPTX/short-ptr.ll
@@ -1,6 +1,7 @@
 ; RUN: llc < %s -mtriple=nvptx64 -mcpu=sm_20 | FileCheck %s --check-prefix CHECK-DEFAULT
 ; RUN: llc < %s -mtriple=nvptx -mcpu=sm_20 | FileCheck %s --check-prefix CHECK-DEFAULT-32
 ; RUN: llc < %s -mtriple=nvptx64 -mcpu=sm_20 -target-abi=shortptr | FileCheck %s --check-prefixes CHECK-SHORT-SHARED,CHECK-SHORT-CONST,CHECK-SHORT-LOCAL
+; RUN: llc < %s -mtriple=nvptx64 -mcpu=sm_20 -target-abi=sharedshortptr | FileCheck %s --check-prefix CHECK-SHARED-SHORT
 
 ; RUN: %if ptxas-ptr32 %{ llc < %s -mtriple=nvptx -mcpu=sm_20 | %ptxas-verify %}
 ; RUN: %if ptxas %{ llc < %s -mtriple=nvptx64 -mcpu=sm_20 | %ptxas-verify %}
@@ -9,11 +10,13 @@
 ; CHECK-DEFAULT: .visible .shared .align 8 .u64 s
 ; CHECK-DEFAULT-32: .visible .shared .align 8 .u32 s
 ; CHECK-SHORT-SHARED: .visible .shared .align 8 .u32 s
+; CHECK-SHARED-SHORT: .visible .shared .align 8 .u32 s
 @s = local_unnamed_addr addrspace(3) global ptr addrspace(3) null, align 8
 
 ; CHECK-DEFAULT: .visible .const .align 8 .u64 c
 ; CHECK-DEFAULT-32: .visible .const .align 8 .u32 c
 ; CHECK-SHORT-CONST: .visible .const .align 8 .u32 c
+; CHECK-SHARED-SHORT: .visible .const .align 8 .u64 c
 @c = local_unnamed_addr addrspace(4) global ptr addrspace(4) null, align 8
 
 declare void @use(i8 %arg);
@@ -21,10 +24,12 @@ declare void @use(i8 %arg);
 ; CHECK-DEFAULT: .param .b64 test1_param_0
 ; CHECK-DEFAULT-32: .param .b32 test1_param_0
 ; CHECK-SHORT-LOCAL: .param .b32 test1_param_0
+; CHECK-SHARED-SHORT: .param .b64 test1_param_0
 define void @test1(ptr addrspace(5) %local) {
   ; CHECK-DEFAULT: ld.param.b64 %rd{{.*}}, [test1_param_0];
   ; CHECK-DEFAULT-32:  ld.param.b32 %r{{.*}}, [test1_param_0];
   ; CHECK-SHORT-LOCAL: ld.param.b32 %r{{.*}}, [test1_param_0];
+  ; CHECK-SHARED-SHORT: ld.param.b64 %rd{{.*}}, [test1_param_0];
   %v = load i8, ptr addrspace(5) %local
   call void @use(i8 %v)
   ret void
@@ -43,6 +48,9 @@ define void @test2() {
   ; CHECK-SHORT-LOCAL: .param .b32 param0;
   ; CHECK-SHORT-LOCAL: add.u32 %r{{.*}}, %SPL, 0;
   ; CHECK-SHORT-LOCAL: st.param.b32
+  ; CHECK-SHARED-SHORT: .param .b64 param0;
+  ; CHECK-SHARED-SHORT: add.u64 %rd{{.*}}, %SPL, 0;
+  ; CHECK-SHARED-SHORT: st.param.b64
   call void @test1(ptr addrspace(5) %cast)
   ret void
 }
@@ -56,6 +64,8 @@ define <4 x float> @test3(<4 x float> %v, i32 %i, float %x) {
   ; CHECK-SHORT-LOCAL: mov.b32 %SPL, __local_depot2;
   ; CHECK-SHORT-LOCAL-NEXT: cvt.u64.u32 %SP, %SPL;
   ; CHECK-SHORT-LOCAL-NEXT: cvta.local.u64 %SP, %SP;
+  ; CHECK-SHARED-SHORT: mov.b64 %SPL, __local_depot2;
+  ; CHECK-SHARED-SHORT-NEXT: cvta.local.u64 %SP, %SPL;
   %r = insertelement <4 x float> %v, float %x, i32 %i
   ret <4 x float> %r
 }
diff --git a/llvm/unittests/TargetParser/TripleTest.cpp b/llvm/unittests/TargetParser/TripleTest.cpp
index 219e3ce9c0baf..f2bad881e5fc1 100644
--- a/llvm/unittests/TargetParser/TripleTest.cpp
+++ b/llvm/unittests/TargetParser/TripleTest.cpp
@@ -4002,6 +4002,8 @@ TEST(DataLayoutTest, NVPTX) {
               testing::ElementsAre("p:32:32"));
   EXPECT_THAT(PointerLayoutSpecs(TT32.computeDataLayout("shortptr")),
               testing::ElementsAre("p:32:32"));
+  EXPECT_THAT(PointerLayoutSpecs(TT32.computeDataLayout("sharedshortptr")),
+              testing::ElementsAre("p:32:32"));
 
   // The default 64-bit target only shrinks Tensor Memory (addrspace:6).
   EXPECT_THAT(PointerLayoutSpecs(TT64.computeDataLayout("")),
@@ -4012,6 +4014,10 @@ TEST(DataLayoutTest, NVPTX) {
   EXPECT_THAT(PointerLayoutSpecs(TT64.computeDataLayout("shortptr")),
               testing::ElementsAre("p3:32:32", "p4:32:32", "p5:32:32",
                                    "p6:32:32", "p7:32:32", "p101:32:32"));
+
+  // The sharedshortptr ABI only shrinks shared and shared cluster pointers.
+  EXPECT_THAT(PointerLayoutSpecs(TT64.computeDataLayout("sharedshortptr")),
+              testing::ElementsAre("p3:32:32", "p6:32:32", "p7:32:32"));
 }
 
 TEST(DataLayoutTest, CheriRISCV32) {



More information about the llvm-commits mailing list