naga
naga copied to clipboard
spv-out: Don't emit cast when types are the same
While working with some shaders with wgpu & naga on android I noticed random driver crashes with the following logs:
07-25 13:17:37.578 15860 15900 I ____ : [2022-07-25 13:17:37.578] [GFX.Context] [info] Skip function Some("entryPoint_vertex_7")
07-25 13:17:37.578 15860 15900 I ____ : [2022-07-25 13:17:37.578] [GFX.Context] [info] Skip function Some("entryPoint_vertex_8")
07-25 13:17:37.587 15860 15900 I AdrenoVK-0: Shader compilation failed for shaderType: 1
07-25 13:17:37.587 15860 15900 W ____ : [2022-07-25 13:17:37.587] [GFX.Context] [warning] Unrecognized device error ERROR_UNKNOWN
07-25 13:17:37.587 15860 15900 E ____ : [2022-07-25 13:17:37.587] [GFX.Context] [error] [context.cpp:415] Device(Lost) (3)
--------- beginning of crash
After digging around I was able to isolate it to the usage of OpBitcast on a value that is already the correct type (in my case casting float to float)
struct Fragment_globals_t {
n_dot_l: f32,
};
var<private> p_Fragment_globals: Fragment_globals_t;
fn entryPoint_fragment_1() {
// %float_0 = OpConstant %float 0
// ...
// This breaks
// %179 = OpBitcast %float %float_0
// %180 = OpAccessChain %_ptr_Private_float %p_Fragment_globals %uint_5
// OpStore %180 %179
p_Fragment_globals.n_dot_l = f32(0.000000);
// This doesn't
// %179 = OpAccessChain %_ptr_Private_float %p_Fragment_globals %uint_5
// OpStore %179 %float_0
p_Fragment_globals.n_dot_l = 0.000000;
}
I tried to make a change that doesn't emit the casting instruction when the input and output types are the same, which In my case fixes this issue.
Would it be possible to merge something like this to avoid this driver issue?
I'm not too familiar with SPIR-V or this codebase so let me know If I missed something.