llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: unamedkr (unamedkr)

<details>
<summary>Changes</summary>

When HIP is compiled for SPIR-V other than AMDGCN-flavoured SPIR-V (HIPSPV, 
e.g. `--offload=spirv64`), Sema gives every `__global__` function the 
`device_kernel` calling convention in its declared type. Host code in the 
device-side compilation then cannot convert a kernel to an ordinary function 
pointer:

```c++
#define __global__ __attribute__((global))
typedef void (*kernel_ptr)(float *);
__global__ void k(float *d) { *d = 1; }
void host() { kernel_ptr p = k; }   // error on spirv64; fine on amdgcn, nvptx, 
spirv64-amd-amdhsa
```

```
error: cannot initialize a variable of type 'kernel_ptr' (aka 'void (*)(float 
*)')
       with an lvalue of type 'void (float *) __attribute__((device_kernel))'
```

With a kernel template (`p = k&lt;N&gt;;`) the error is the less telling 
`assigning to 'kernel_ptr' from incompatible type '&lt;overloaded function 
type&gt;'`.

This is a common pattern. llama.cpp's FlashAttention dispatch 
(`fattn-mma-f16.cuh`, `fattn-tile.cuh`, `fattn-vec.cuh`) stores the chosen 
kernel template instance in a `fattn_kernel_t` pointer, so none of its 
FlashAttention kernels build for HIPSPV. #<!-- -->110447 removed the same 
convention from the type for AMDGCN-flavoured SPIR-V because it "breaks 
overload resolution"; HIPSPV still has it. It reproduces with clang 20.1.8, 
22.1.8 and 23.1.6.

For HIP the convention in the type is not needed: CodeGen already applies it to 
HIP kernels on SPIR-V 
(`SPIRVTargetCodeGenInfo::setCUDAKernelCallingConvention`, and its CIR 
counterpart), as it does on AMDGPU. This patch keeps the Sema rule for CUDA 
compiled to SPIR-V (`CodeGenCUDASPIRV/kernel-cc.cu` relies on it) and leaves 
HIP to CodeGen. HIP kernels are still emitted as `spir_kernel`.

**Testing**
- New `clang/test/CodeGenHIP/hipspv-kernel-address.cpp`: a kernel, a kernel 
template and a static kernel template whose addresses host code stores in a 
plain function pointer, checked to be emitted as `spir_kernel` for `spirv64` 
and `spirv64-amd-amdhsa`. It fails without the patch and passes with it.
- `llvm-lit 
clang/test/{CodeGenHIP,CodeGenCUDA,CodeGenCUDASPIRV,SemaCUDA,CodeGenOpenCL,SemaOpenCL,CodeGenSPIRV}`
 on main (f2cf1ce7c, assertions on, targets X86;SPIRV;AMDGPU;NVPTX): all pass 
with the patch.
- Downstream, with the same change on release/22.x: llama.cpp's CUDA sources 
with FlashAttention build for SPIR-V through a CUDA-on-OpenCL layer 
([OpenCuda](https://github.com/ailab-kr/opencuda)) and run on an Intel Arc Pro 
B70; `test-backend-ops -o FLASH_ATTN_EXT` passes 4178 of 4370 cases against the 
CPU backend (the 192 others, all with head size 64 and a KV length of 113, are 
not traced yet). Before the change, all 39 FlashAttention source files had no 
device code there.

This change was prepared with the help of an AI assistant (Claude); I have 
reviewed it and the tests above were run as described.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_01TraZ37D76NnXSnpSN7QrSA


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


2 Files Affected:

- (modified) clang/lib/Sema/SemaType.cpp (+8-4) 
- (added) clang/test/CodeGenHIP/hipspv-kernel-address.cpp (+40) 


``````````diff
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index 0e49975e4bed04..1a2c04014511b4 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -3894,11 +3894,15 @@ static CallingConv getCCForDeclaratorChunk(
   CallingConv CC = S.Context.getDefaultCallingConvention(FTI.isVariadic,
                                                          IsCXXInstanceMethod);
 
-  if (S.getLangOpts().CUDA) {
-    // If we're compiling CUDA/HIP code and targeting HIPSPV we need to make
-    // sure the kernels will be marked with the right calling convention so 
that
+  if (S.getLangOpts().CUDA && !S.getLangOpts().HIP) {
+    // If we're compiling CUDA code and targeting SPIR-V we need to make sure
+    // the kernels will be marked with the right calling convention so that
     // they will be visible by the APIs that ingest SPIR-V. We do not do this
-    // when targeting AMDGCNSPIRV, as it does not rely on OpenCL.
+    // when targeting AMDGCNSPIRV, as it does not rely on OpenCL. HIP kernels
+    // get the kernel calling convention in CodeGen instead
+    // (setCUDAKernelCallingConvention), as on AMDGPU: in the declared type it
+    // would keep host code from converting a kernel to an ordinary function
+    // pointer.
     llvm::Triple Triple = S.Context.getTargetInfo().getTriple();
     if (Triple.isSPIRV() && Triple.getVendor() != llvm::Triple::AMD) {
       for (const ParsedAttr &AL : D.getDeclSpec().getAttributes()) {
diff --git a/clang/test/CodeGenHIP/hipspv-kernel-address.cpp 
b/clang/test/CodeGenHIP/hipspv-kernel-address.cpp
new file mode 100644
index 00000000000000..43675549653cdd
--- /dev/null
+++ b/clang/test/CodeGenHIP/hipspv-kernel-address.cpp
@@ -0,0 +1,40 @@
+// RUN: %clang_cc1 -triple spirv64 -x hip -emit-llvm -fcuda-is-device \
+// RUN:   -o - %s | FileCheck %s
+// RUN: %clang_cc1 -triple spirv64-amd-amdhsa -x hip -emit-llvm \
+// RUN:   -fcuda-is-device -o - %s | FileCheck %s
+
+// Host code may take a kernel's address into an ordinary function pointer,
+// as with other targets (for example a table of kernels or a launcher that
+// picks one). On HIPSPV the kernel calling convention is applied in CodeGen,
+// so the kernels are still emitted as SPIR-V kernels.
+
+#define __global__ __attribute__((global))
+
+typedef void (*kernel_ptr)(float *, float);
+
+// CHECK: define {{.*}}spir_kernel void @_Z3fooPff(
+__global__ void foo(float *a, float b) { *a = b; }
+
+// CHECK: define {{.*}}spir_kernel void @_Z3barILi2EEvPff(
+template <int N> __global__ void bar(float *a, float b) { *a = b * N; }
+
+// CHECK: define {{.*}}spir_kernel void @_ZL3bazILb1EEvPff(
+template <bool B> static __global__ void baz(float *a, float b) {
+  *a = B ? b : -b;
+}
+
+kernel_ptr pick(int i) {
+  kernel_ptr k = foo;
+  if (i == 1)
+    k = bar<2>;
+  else if (i == 2)
+    k = &baz<true>;
+  return k;
+}
+
+template <int N> kernel_ptr pick_t() {
+  kernel_ptr k;
+  k = bar<N>;
+  return k;
+}
+kernel_ptr p2 = pick_t<2>();

``````````

</details>


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

Reply via email to