[llvm] [SPIRV] Add legalization pass for zero-size arrays (PR #172367)
Nick Sarnie via llvm-commits
llvm-commits at lists.llvm.org
Tue Dec 16 08:26:49 PST 2025
sarnex wrote:
> Hello, Just to get the full picture, what is the code that generates a `[0 x T] alloca`? What does it represent?
>
> Could you share a simple source code and compile command to play with?
Thanks for the review (thanks Marcos as well)! I haven't worked much with legalization before so I really appreciate the feedback.
The real-world use case for this is for generating OpenMP offloading code targeting SPIR-V. The OpenMP frontend may generate a 0-element array in some cases. The case here is an array with captured variables but the user has none captured. I originally [tried](https://github.com/llvm/llvm-project/pull/169723) to change that behavior in the frontend but it was recommended I fix it in the backend.
Here's a repro for the case, you'll need to have compiled with the `offload`, `openmp` and `libc` runtimes enabled.
```
bin/clang++ -fopenmp -fopenmp-targets=spirv64-intel -nogpulib ../offload/test/libc/puts.c -nogpulib
```
where `pwd` is the root of the `llvm` source directory. If you have trouble reproducing let me know.
You should get:
```
fatal error: error in backend: Runtime arrays are not allowed in non-shader SPIR-V modules
```
and if you look at the IR, we see:
```
%5 = alloca [0 x ptr addrspace(4)], align 8
...
%8 = addrspacecast ptr %5 to ptr addrspace(4)
...
call spir_func addrspace(9) void @__kmpc_parallel_60(..., ptr addrspace(4) %8, ...)
..
https://github.com/llvm/llvm-project/pull/172367
More information about the llvm-commits
mailing list