fatal error: generated SPIR-V is invalid: Invalid word count: OpConstant starting at word 108 says it has 4 words, but found 5 words instead.
$ cat repro.hlsl
RWByteAddressBuffer outp : register(u1);
[numthreads(1, 1, 1)]
void entrypoint()
{
uint val = 0;
uint64_t res = val ? 1 : 0;
outp.Store(0, res);
}
$ dxc -fspv-target-env=vulkan1.1 -spirv -E entrypoint -T cs_6_2 -no-warnings repro.hlsl
fatal error: generated SPIR-V is invalid: Invalid word count: OpConstant starting at word 108 says it has 4 words, but found 5 words instead.
note: please file a bug report on https://github.com/Microsoft/DirectXShaderCompiler/issues with source code if possible
Probably related to #5318.
Bisects to #4935.
I think this is a small issue. From the two issues mentioned, it seems like the operand count in the spir-v instruction for 64-bit integer constants is wrong. I'll try tracking down where we create that instruction.
The problem is that the SPIR-V backend tries to treat the int and float literals as if they do not have a size. I believe this was to try to avoid extra conversions. If you have short s = 1, DXC wants to generate a short OpConstant in spir-v and not have to convert. However, in this case, we end up making two conflicting decisions. The temporary variable to hold the result of the select is set to be 32-bits. Then when the value is stored to the original variable, it is assumed that the literal can be the same size as result type, so there is no conversion from 32-bits to 64-bits. This causes a type mismatch.
This also fails with double and float in the select:
void foo(out double value, uint x) {
value = x < 64 ? 1.0 : 0.0;
}
void main() {
double value;
foo(value, 2);
}
and with 16-bit ints:
void foo(out uint16_t value, uint x) {
value = x < 64 ? 1 : 0;
}
void main() {
uint16_t value;
foo(value, 2);
}
Although #5318 is very similar, note that it has a different error message: "Expected input to have the same total bit width as Result Type: Bitcast"
Although #5318 is very similar, note that it has a different error message: "Expected input to have the same total bit width as Result Type: Bitcast"
That is because you have "-Od" in #5318. DXC generates a bad bitcast. When the optimizer folds it, it creates a bad OpConstant. Which error you see depends on whether or not optimizations are enabled.
This has not been fixed for the case where we do the short circuit evaluation of the ternary operator.