KhronosGroup / KhronosGroup/SPIRV-LLVM-Translator
SPIR-V fails to validate with: Initializer type must match the type pointed to by the Result Type
- Dominant language
- LLVM
- Stars
- 625
- Forks
- 279
- Avg merge
- 3d 5h
- Merged PRs (30d)
- 34
Description
while trying to run OpenCL-CTS with SPIR-V offload compilation, this source:
```
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
#pragma OPENCL EXTENSION cl_khr_int64_base_atomics : enable
#pragma OPENCL EXTENSION cl_khr_int64_extended_atomics : enable
ulong from_buf(ulong a) { return a; }
ulong to_buf(ulong a) { return a; }
#define INIT_VAR(a) ATOMIC_VAR_INIT(a)
volatile atomic_ptrdiff_t var = INIT_VAR(0);
global volatile atomic_ptrdiff_t g_var = INIT_VAR(1);
volatile atomic_ptrdiff_t a_var[2] = { INIT_VAR(1), INIT_VAR(1) };
global volatile atomic_ptrdiff_t* p_var = &a_var[1];
kernel void writer( global ulong* src, uint idx ) {
atomic_store( &var, from_buf(src[0]) );
atomic_store( &g_var, from_buf(src[1]) );
atomic_store( &a_var[0], from_buf(src[2]) );
atomic_store( &a_var[1], from_buf(src[3]) );
p_var = a_var + idx;
}
kernel void reader( global ulong* dest, ulong ptr_write_val ) {
atomic_store( p_var, from_buf(ptr_write_val) );
dest[0] = to_buf( atomic_load( &var ) );
dest[1] = to_buf( atomic_load( &g_var ) );
dest[2] = to_buf( atomic_load( &a_var[0] ) );
dest[3] = to_buf( atomic_load( &a_var[1] ) );
}
```
compiled with Clang 16.0.4 && llvm-spirv branch `llvm_release_160`:
```
$CLANG --target=spir64-unknown-unknown -x cl -cl-std=CL3.0 -D__OPENCL_VERSION__=300 -D__OPENCL_C_VERSION__=300 -D__ENDIAN_LITTLE__=1 -D__IMAGE_SUPPORT__=1 -D__opencl_c_named_address_space_builtins=1 -D__opencl_c_int64=1 -D__opencl_c_atomic_order_acq_rel=1 -D__opencl_c_atomic_order_seq_cst=1 -D__opencl_c_atomic_scope_device=1 -D__opencl_c_program_scope_global_variables=1 -D__opencl_c_generic_address_space=1 -Dcl_khr_byte_addressable_store -Dcl_khr_global_int32_base_atomics -Dcl_khr_global_int32_extended_atomics -Dcl_khr_local_int32_base_atomics -Dcl_khr_local_int32_extended_atomics -Dcl_khr_3d_image_writes -D__opencl_c_3d_image_writes=1 -Dcl_khr_command_buffer -Dcl_khr_subgroups -Dcl_intel_unified_shared_memory -Dcl_khr_spir -Dcl_khr_il_program -Dcl_khr_fp64 -D__opencl_c_fp64=1 -Dcl_khr_int64_base_atomics -Dcl_khr_int64_extended_atomics -Xclang -cl-ext=-all,+__opencl_c_images,+__opencl_c_named_address_space_builtins,+__opencl_c_int64,+__opencl_c_atomic_order_acq_rel,+__opencl_c_atomic_order_seq_cst,+__opencl_c_atomic_scope_device,+__opencl_c_program_scope_global_variables,+__opencl_c_generic_address_space,+cl_khr_byte_addressable_store,+cl_khr_global_int32_base_atomics,+cl_khr_global_int32_extended_atomics,+cl_khr_local_int32_base_atomics,+cl_khr_local_int32_extended_atomics,+cl_khr_3d_image_writes,+__opencl_c_3d_image_writes,+cl_khr_command_buffer,+cl_khr_subgroups,+cl_intel_unified_shared_memory,+cl_khr_spir,+cl_khr_il_program,+cl_khr_fp64,+__opencl_c_fp64,+cl_khr_int64_base_atomics,+cl_khr_int64_extended_atomics -o test_llvm16.bc -emit-llvm -c source.cl
$LLVM_SPIRV --spirv-gen-kernel-arg-name-md --spirv-max-version=1.2 -o test_llvm16.spv test_llvm16.bc
```
results in a SPIR-V which fails to validate:
```
spirv-val test_llvm16.spv
error: line 66: Initializer type must match the type pointed to by the Result Type
%p_var = OpVariable %_ptr_CrossWorkgroup__ptr_CrossWorkgroup_uchar CrossWorkgroup %13
```
When using Clang-15 & llvm-spirv-15 *without opaque pointers* the resulting SPIR-V passes validation, but Clang-15 *with opaque pointers* also fails validation.
Contributor guide
Research direction
Start with the OpenCL reproducer in source.cl and run the shown Clang, llvm-spirv, and spirv-val commands to reproduce the invalid initializer type in test_llvm16.spv. Trace the opaque-pointer translation responsible for %p_var and add or update coverage so the generated SPIR-V passes validation for this case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100