clspv icon indicating copy to clipboard operation
clspv copied to clipboard

(More) Invalid SPIR-V generated from Darktable / Geekbench 5 kernels

Open kpet opened this issue 2 years ago • 2 comments

https://github.com/google/clspv/pull/1102 made it possible for many more Darktable kernel source files to be compiled but not all of them yet.

The attached source, compiled with the following options:

clspv source.cl  -w -cl-fast-relaxed-math  -DNVIDIA=1 -I"/usr/share/darktable/kernels" -cl-single-precision-constant  -cl-kernel-arg-info   -fp16=0  -rewrite-packed-structs  -std430-ubo-layout  -decorate-nonuniform    -arch=spir  --use-native-builtins=acos,acosh,acospi,asin,asinh,asinpi,atan,atan2,atan2pi,atanh,atanpi,ceil,copysign,fabs,fdim,floor,fma,fmax,fmin,frexp,half_rsqrt,half_sqrt,isequal,isfinite,isgreater,isgreaterequal,isinf,isless,islessequal,islessgreater,isnan,isnormal,isnotequal,isordered,isunordered,ldexp,mad,rint,round,rsqrt,signbit,sqrt,tanh,trunc,  -spv-version=1.6  -max-pushconstant-size=256  -max-ubo-size=65536  -global-offset  -long-vector  -module-constants-in-storage-buffer  -cl-arm-non-uniform-work-group-size     -o compiled.spv

produces a SPIR-V module that fails validation with the following message:

error: 736: AtomicOr: expected Pointer to point to a value of type Result Type
  %520 = OpAtomicOr %uint %516 %uint_1 %uint_80 %uint_0

source.txt

kpet avatar May 12 '23 17:05 kpet

This is tricky. Our fake pointer casting doesn't work with atomics. So we'd have to prefer the uint type for inference. That's a reasonably large change. This isn't the only time being greedy is detrimental though so it might be worth investigating.

Another alternative would be for darktable to use cl_khr_float_atomics and implement support for the various SPIR-V extensions like SPV_KHR_shader_atomic_float_add, but that requires changing darktable. Another problem with that route (beyond extension support) is that LLVM apppears to have "optimized" to atomic or which isn't supported with float. Maybe that wouldn't happen if it was all done in floats, but SPIR-V only supports add, min, and max.

alan-baker avatar May 12 '23 19:05 alan-baker

I'm seeing a similar validation error with a kernel from Geekbench 5 too (attached):

error: 823: AtomicIAdd: expected Pointer to point to a value of type Result Type
  %622 = OpAtomicIAdd %uint %621 %uint_1 %uint_80 %619

Build options:

clspv source.cl  -cl-single-precision-constant  -cl-kernel-arg-info   -fp16=0  -rewrite-packed-structs  -std430-ubo-layout  -decorate-nonuniform    -arch=spir  --use-native-builtins=acos,acosh,acospi,asin,asinh,asinpi,atan,atan2,atan2pi,atanh,atanpi,ceil,copysign,fabs,fdim,floor,fma,fmax,fmin,frexp,half_rsqrt,half_sqrt,isequal,isfinite,isgreater,isgreaterequal,isinf,isless,islessequal,islessgreater,isnan,isnormal,isnotequal,isordered,isunordered,ldexp,mad,rint,round,rsqrt,signbit,sqrt,tanh,trunc,  -spv-version=1.6  -max-pushconstant-size=256  -max-ubo-size=65536  -global-offset  -long-vector  -module-constants-in-storage-buffer  -cl-arm-non-uniform-work-group-size     -o compiled.spv

source.txt

kpet avatar May 16 '23 19:05 kpet