Author: Nick Sarnie
Date: 2026-03-11T13:48:58Z
New Revision: 32f285d3d9786f69c8dddf9c795ffdbaf9a4f7ce

URL: 
https://github.com/llvm/llvm-project/commit/32f285d3d9786f69c8dddf9c795ffdbaf9a4f7ce
DIFF: 
https://github.com/llvm/llvm-project/commit/32f285d3d9786f69c8dddf9c795ffdbaf9a4f7ce.diff

LOG: [clang][SPIRV] Coerce pointer kernel arguments to global AS (#185498)

SPIR-V does not allow pointer kernel arguments to be in the generic
address space. For offload, we already coerece them to the global
address space if not specified.

We are seeing that we need to do the same for SPIR-V directly as some of
the liboffload unit tests are compiled for `spirv64` directly, otherwise
we produce invalid SPIR-V.

---------

Signed-off-by: Nick Sarnie <[email protected]>

Added: 
    clang/test/CodeGenSPIRV/kernel-ptr-arg.c

Modified: 
    clang/lib/CodeGen/Targets/SPIR.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/Targets/SPIR.cpp 
b/clang/lib/CodeGen/Targets/SPIR.cpp
index 52d019b855dbc..4d902fe2d6e3e 100644
--- a/clang/lib/CodeGen/Targets/SPIR.cpp
+++ b/clang/lib/CodeGen/Targets/SPIR.cpp
@@ -146,31 +146,30 @@ void CommonSPIRABIInfo::setCCs() {
 }
 
 ABIArgInfo SPIRVABIInfo::classifyKernelArgumentType(QualType Ty) const {
-  if (getContext().getLangOpts().isTargetDevice()) {
-    // Coerce pointer arguments with default address space to CrossWorkGroup
-    // pointers for target devices as default address space kernel arguments
-    // are not allowed. We use the opencl_global language address space which
-    // always maps to CrossWorkGroup.
-    llvm::Type *LTy = CGT.ConvertType(Ty);
-    auto DefaultAS = getContext().getTargetAddressSpace(LangAS::Default);
-    auto GlobalAS = getContext().getTargetAddressSpace(LangAS::opencl_global);
-    auto *PtrTy = llvm::dyn_cast<llvm::PointerType>(LTy);
-    if (PtrTy && PtrTy->getAddressSpace() == DefaultAS) {
-      LTy = llvm::PointerType::get(PtrTy->getContext(), GlobalAS);
-      return ABIArgInfo::getDirect(LTy, 0, nullptr, false);
-    }
+  // Coerce pointer arguments with default address space to CrossWorkGroup
+  // pointers as default address space kernel
+  // arguments are not allowed. We use the opencl_global language address
+  // space which always maps to CrossWorkGroup.
+  llvm::Type *LTy = CGT.ConvertType(Ty);
+  auto DefaultAS = getContext().getTargetAddressSpace(LangAS::Default);
+  auto GlobalAS = getContext().getTargetAddressSpace(LangAS::opencl_global);
+  auto *PtrTy = llvm::dyn_cast<llvm::PointerType>(LTy);
+  if (PtrTy && PtrTy->getAddressSpace() == DefaultAS) {
+    LTy = llvm::PointerType::get(PtrTy->getContext(), GlobalAS);
+    return ABIArgInfo::getDirect(LTy, 0, nullptr, false);
+  }
 
-    if (isAggregateTypeForABI(Ty)) {
-      // Force copying aggregate type in kernel arguments by value when
-      // compiling CUDA targeting SPIR-V. This is required for the object
-      // copied to be valid on the device.
-      // This behavior follows the CUDA spec
-      // 
https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#global-function-argument-processing,
-      // and matches the NVPTX implementation. TODO: hardcoding to 0 should be
-      // revisited if HIPSPV / byval starts making use of the AS of an indirect
-      // arg.
-      return getNaturalAlignIndirect(Ty, /*AddrSpace=*/0, /*byval=*/true);
-    }
+  if (getContext().getLangOpts().isTargetDevice() &&
+      isAggregateTypeForABI(Ty)) {
+    // Force copying aggregate type in kernel arguments by value when
+    // compiling CUDA targeting SPIR-V. This is required for the object
+    // copied to be valid on the device.
+    // This behavior follows the CUDA spec
+    // 
https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#global-function-argument-processing,
+    // and matches the NVPTX implementation. TODO: hardcoding to 0 should be
+    // revisited if HIPSPV / byval starts making use of the AS of an indirect
+    // arg.
+    return getNaturalAlignIndirect(Ty, /*AddrSpace=*/0, /*byval=*/true);
   }
   return classifyArgumentType(Ty);
 }

diff  --git a/clang/test/CodeGenSPIRV/kernel-ptr-arg.c 
b/clang/test/CodeGenSPIRV/kernel-ptr-arg.c
new file mode 100644
index 0000000000000..8f6dff83fea71
--- /dev/null
+++ b/clang/test/CodeGenSPIRV/kernel-ptr-arg.c
@@ -0,0 +1,18 @@
+// RUN: %clang_cc1 -triple spirv64 %s -emit-llvm -o - | FileCheck %s
+// RUN: %clang_cc1 -triple spirv32 %s -emit-llvm -o - | FileCheck %s
+
+// CHECK: define spir_func void @func(ptr noundef %{{.*}})
+void func(int* arg) {
+}
+
+// CHECK: define spir_kernel void @kernel(ptr addrspace(1) noundef %{{.*}})
+void __attribute__((device_kernel)) kernel(int* arg) {
+// CHECK: call spir_func{{.*}} void @func(ptr noundef %{{.*}})
+  func(arg);
+}
+
+// CHECK: define spir_kernel void @kernel_spec(ptr addrspace(2) noundef 
%{{.*}})
+void __attribute__((device_kernel)) 
kernel_spec(__attribute__((address_space(2))) int* arg) {
+// CHECK: call spir_func{{.*}} void @func(ptr noundef %{{.*}})
+  func((int*)arg);
+}


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to