microsoft / DirectXShaderCompiler

This repo hosts the source for the DirectX Shader Compiler which is based on LLVM/Clang.
Other
2.98k stars 665 forks source link

[SPIR-V] Invalid SPIR-V operand with isnan() #6712

Open shobomaru opened 1 week ago

shobomaru commented 1 week ago

Description DXC generates an invalid SPIR-V

Steps to Reproduce

dxc -T cs_6_0 -spirv path/to/file.hlsl

HLSL code:

RWStructuredBuffer<float> Out;
[numthreads(1, 1, 1)]
void main(uint id : SV_DispatchThreadID)
{
        bool bar = isnan(Out[id]);
        Out[id] = (float)bar;
}

If we change the code as follows, the issue is not occurred.

        float baz = Out[id];
        Out[id] = (float)isnan(baz);

Actual Behavior

fatal error: generated SPIR-V is invalid: Expected bool scalar or vector type as Result Type: IsNan %23 = OpIsNan %uint %22 note: please file a bug report on https://github.com/Microsoft/DirectXShaderCompiler/issues with source code if possible

Environment

devshgraphicsprogramming commented 1 week ago

If you're interested in a quick fix, you can use inline SPIR-V to use OpIsNan directly

shobomaru commented 1 week ago

Indeed, by defining a new function as follows, we can pass the validation.

[[vk::ext_instruction(156)]]
bool alt_isnan(float v1);
gorrila007 commented 1 week ago

RWStructuredBuffer Out; [numthreads(1, 1, 1)] void main(uint id : SV_DispatchThreadID) { float value = Out[id]; bool bar = isnan(value); // Check if 'value' is NaN Out[id] = bar ? 1.0f : 0.0f; // Store 1.0f if 'value' is NaN, otherwise 0.0f }