| Issue |
242543
|
| Summary |
[SPIR-V][clang] Inconsistent default address space mapping makes C/C++ and SYCL device code ABI-incompatible
|
| Labels |
clang
|
| Assignees |
|
| Reporter |
YuriPlyakhin
|
### Summary
When clang targets `spirv64`, the address space it uses for unqualified pointers depends on the source language and on the vendor component of the target triple. As a result, code built as C/C++ for `spirv64-unknown-unknown` can't safely be linked with SYCL device code built for the same triple. Any function whose signature involves an unqualified pointer — directly as a parameter or return type, or nested inside a struct — ends up with a different LLVM IR signature on each side of the language boundary (`ptr` vs. `ptr addrspace(4)`).
This currently prevents the GPU builds of LLVM's own runtime libraries (compiler-rt builtins, libc) from being used by SYCL device code on SPIR-V.
### Reproducer
```c++
// t.cpp
#ifdef __SYCL_DEVICE_ONLY__
[[clang::sycl_external]]
#endif
void foo(int *p) {}
```
```console
$ clang -cc1 -x c++ -triple spirv64-unknown-unknown -emit-llvm -o - t.cpp | grep define
define spir_func void @_Z3fooPi(ptr noundef %p) #0 {
$ clang -cc1 -fsycl-is-device -triple spirv64-unknown-unknown -emit-llvm -o - t.cpp | grep define
define spir_func void @_Z3fooPi(ptr addrspace(4) noundef %p) #0 {
```
Both builds produce the same mangled symbol, but the parameter types are different.
### How the address space map is selected
`BaseSPIRTargetInfo::adjust()` in `clang/lib/Basic/Targets/SPIR.h` ([source](https://github.com/llvm/llvm-project/blob/13e02397ce93/clang/lib/Basic/Targets/SPIR.h#L195-L214)):
```c++
setAddressSpaceMap(
/*DefaultIsGeneric=*/Opts.SYCLIsDevice ||
(getTriple().isSPIRV() &&
(Opts.CUDAIsDevice ||
getTriple().getVendor() == llvm::Triple::Intel)));
```
- `SPIRDefIsPrivMap`: unqualified pointers are `ptr` (addrspace 0, SPIR-V `Function` storage class).
- `SPIRDefIsGenMap`: unqualified pointers are `ptr addrspace(4)` (SPIR-V `Generic` storage class).
### Impact
- **libc on SPIR-V GPUs.** Any libc entry point with an unqualified pointer parameter is affected, e.g. `__assert_fail(const char *, const char *, unsigned, const char *)`. This blocks device-side `assert` for SYCL (#221297).
- **compiler-rt builtins already enabled for SYCL upstream.** Any builtin with an unqualified pointer in its signature has the same mismatch when compiler-rt is built as C for `spirv64-unknown-unknown`. <!-- TODO: add a concrete builtin + failing reproducer. Note: __divsc3/__mulsc3 are declared identically from C++ and SYCL in a quick check (only sret pointer, AS0 on both sides), so don't cite them without a confirmed failure. -->
- More generally, any shared device library written in C/C++ has to be compiled once per language's address space mapping, or kept downstream.
### References
- RFC (no conclusion reached): https://discourse.llvm.org/t/rfc-the-spir-v-backend-should-change-its-address-space-mappings/82640
- #241095 – `[clang] Add option to use 'generic' as default address space for SPIR-V`
- Blocks: #221297 – `[SYCL] Feature request: support device code assertions`
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs