[clang] [CUDA][HIP] Mangle language address spaces by name on the device (PR #245364)

Shilei Tian via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 23 12:06:34 PST 2026


https://github.com/shiltian created https://github.com/llvm/llvm-project/pull/245364

For a kernel that takes a pointer in a language address space, such as
`__attribute__((opencl_global)) int *`, the device mangles the address space by
number (`_Z6kernelPU3AS1i`) but the host mangles it by name
(`_Z6kernelPU8CLglobali`). The host registers the kernel with its own name, so
the kernel can't be found at launch. The host can't use the numbers instead,
since it doesn't know how the device maps them, e.g. SPIR-V chooses its map
based on the device's language options.

I understand this is not a typical CUDA code, because CUDA doesn't have the
concept of address spaces because of the "unified pointer" design, but I still
think the host and device code should be able to use the same mangling for the
same kernel.

This PR was assisted by AI but I reviewed all the changes.


>From 54491f954a84f6603e6d1234d94b1af04e735d59 Mon Sep 17 00:00:00 2001
From: Shilei Tian <i at tianshilei.me>
Date: Mon, 23 Nov 2026 14:21:08 -0500
Subject: [PATCH] [CUDA][HIP] Mangle language address spaces by name on the
 device

For a kernel that takes a pointer in a language address space, such as
`__attribute__((opencl_global)) int *`, the device mangles the address space by
number (`_Z6kernelPU3AS1i`) but the host mangles it by name
(`_Z6kernelPU8CLglobali`). The host registers the kernel with its own name, so
the kernel can't be found at launch. The host can't use the numbers instead,
since it doesn't know how the device maps them, e.g. SPIR-V chooses its map
based on the device's language options.

I understand this is not a typical CUDA code, because CUDA doesn't have the
concept of address spaces because of the "unified pointer" design, but I still
think the host and device code should be able to use the same mangling for the
same kernel.

This PR was assisted by AI but I reviewed all the changes.
---
 clang/lib/AST/ASTContext.cpp                  |  6 ++++-
 .../CodeGenHIP/spirv-constant-addrspace.hip   |  2 +-
 .../CodeGenCUDA/device-name-address-space.cu  | 22 +++++++++++++++++++
 3 files changed, 28 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/CodeGenCUDA/device-name-address-space.cu

diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 7d5fd98b348941..074a8ce0f1be14 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -925,7 +925,11 @@ static bool isAddrSpaceMapManglingEnabled(const TargetInfo &TI,
                                           const LangOptions &LangOpts) {
   switch (LangOpts.getAddressSpaceMapMangling()) {
   case LangOptions::ASMM_Target:
-    return TI.useAddressSpaceMapMangling();
+    // In CUDA and HIP, the host registers kernels with their device names, so
+    // the host and the device must mangle them in the same way. The host
+    // doesn't know which numbers the device maps language address spaces to,
+    // so both sides mangle them by name.
+    return !LangOpts.CUDA && TI.useAddressSpaceMapMangling();
   case LangOptions::ASMM_On:
     return true;
   case LangOptions::ASMM_Off:
diff --git a/clang/test/CIR/CodeGenHIP/spirv-constant-addrspace.hip b/clang/test/CIR/CodeGenHIP/spirv-constant-addrspace.hip
index fdf8d95fec734e..018b6af65a4bef 100644
--- a/clang/test/CIR/CodeGenHIP/spirv-constant-addrspace.hip
+++ b/clang/test/CIR/CodeGenHIP/spirv-constant-addrspace.hip
@@ -60,7 +60,7 @@ __device__ int get_shared() {
 }
 
 // An explicit opencl_constant qualifier keeps UniformConstant.
-// LLVM-LABEL: define{{.*}} i32 @_Z6get_ocPU3AS2i(ptr addrspace(2)
+// LLVM-LABEL: define{{.*}} i32 @_Z6get_ocPU10CLconstanti(ptr addrspace(2)
 __device__ int get_oc(__attribute__((opencl_constant)) int *p) { return *p; }
 
 // Function addresses are in the program address space 4.
diff --git a/clang/test/CodeGenCUDA/device-name-address-space.cu b/clang/test/CodeGenCUDA/device-name-address-space.cu
new file mode 100644
index 00000000000000..c81bf97633a8a1
--- /dev/null
+++ b/clang/test/CodeGenCUDA/device-name-address-space.cu
@@ -0,0 +1,22 @@
+// RUN: %clang_cc1 -triple amdgpu9.00-amd-amdhsa -x hip -fcuda-is-device -emit-llvm -o - %s | FileCheck --check-prefix=DEV %s
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -aux-triple amdgpu9.00-amd-amdhsa -x hip -emit-llvm -o - %s | FileCheck --check-prefix=HOST %s
+// RUN: %clang_cc1 -triple x86_64-pc-windows-msvc -aux-triple amdgpu9.00-amd-amdhsa -x hip -emit-llvm -o - %s | FileCheck --check-prefix=HOST %s
+// RUN: %clang_cc1 -triple spirv64-amd-amdhsa -x hip -fcuda-is-device -emit-llvm -o - %s | FileCheck --check-prefix=DEV %s
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -aux-triple spirv64-amd-amdhsa -x hip -emit-llvm -o - %s | FileCheck --check-prefix=HOST %s
+// RUN: %clang_cc1 -triple nvptx64-nvidia-cuda -fcuda-is-device -emit-llvm -o - %s | FileCheck --check-prefix=DEV %s
+// RUN: echo -n "GPU binary would be here." > %t
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -aux-triple nvptx64-nvidia-cuda -fcuda-include-gpubinary %t -emit-llvm -o - %s | FileCheck --check-prefix=HOST %s
+
+// The host registers a kernel with its device name, so both sides must mangle
+// it in the same way. A language address space is mangled by its name, and a
+// numeric one by its number.
+
+#define __global__ __attribute__((global))
+
+// DEV: define {{.*}}@_Z6kernelPU8CLglobali(
+// HOST: c"_Z6kernelPU8CLglobali\00"
+__global__ void kernel(__attribute__((opencl_global)) int *p) {}
+
+// DEV: define {{.*}}@_Z7nkernelPU3AS1i(
+// HOST: c"_Z7nkernelPU3AS1i\00"
+__global__ void nkernel(__attribute__((address_space(1))) int *p) {}



More information about the cfe-commits mailing list