DirectXShaderCompiler icon indicating copy to clipboard operation
DirectXShaderCompiler copied to clipboard

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

Open shobomaru opened this issue 1 year ago • 3 comments

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

  • DXC version: libdxcompiler.dylib: 1.8(dev;4640-45018c75)
  • Host Operating System: Windows 11 23H2 and macOS 14.5

shobomaru avatar Jun 20 '24 19:06 shobomaru

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

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

[[vk::ext_instruction(156)]]
bool alt_isnan(float v1);

shobomaru avatar Jun 21 '24 18:06 shobomaru

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 }

gorrila007 avatar Jun 22 '24 00:06 gorrila007

Thanks for the issue! Sent a PR 😊 Most of the team being OOO, there might be some delays, but it's on its way.

Keenuts avatar Jul 09 '24 13:07 Keenuts