Repository navigation
Closed
Description
fatal error: generated SPIR-V is invalid: Expected total number of given components to be equal to the size of Result Type vector
This error occurs when compiling for SPIR-V, but does not occur for DXIL.
Functional impact
Cannot compile shaders for SPIR-V.
Minimal repro steps
test.i
#line 1 "test.hlsl"
#line 1 "./sh.hlsli"
#line 1 "./common.hlsli"
static const float pi = 3.141592653589793;
static const float sqrt_pi = 1.772453850905516;
static const float sqrt_2 = 1.414213562373095;
static const float sqrt_3 = 1.732050807568877;
static const int max_int_v = 0x7FFFFFFF;
static const int min_int_v = -0x80000000;
static const uint max_uint_v = 0xFFFFFFFFu;
static const float max_float_v = 3.4028234664e+38f;
float squared(float x) {
return x * x;
}
#line 4 "./sh.hlsli"
namespace zh {
typedef float2 zh2;
typedef float3 zh3;
}
namespace sh {
namespace constants {
static const float band0_factor = 0.5f / sqrt_pi;
static const float band1_factor = 0.5f * sqrt_3 / sqrt_pi;
static const float zh0_rotation_factor = 2.0f * sqrt_pi;
static const float zh1_rotation_factor = 2.0f * sqrt_pi / sqrt_3;
}
typedef float band0;
typedef float3 band1;
band0 coefficients_band0(float3 dir) {
return constants::band0_factor;
}
band1 coefficients_band1(float3 dir) {
return constants::band1_factor * float3(-dir.y, dir.z, -dir.x);
}
float integrate(band0 lhs, band0 rhs) {
return lhs * rhs;
}
float integrate(band1 lhs, band1 rhs) {
return dot(lhs, rhs);
}
band0 rotate_zh_band0(float zh_v, float3 z) {
return constants::zh0_rotation_factor * zh_v * coefficients_band0(z);
}
band1 rotate_zh_band1(float zh_v, float3 z) {
return constants::zh1_rotation_factor * zh_v * coefficients_band1(z);
}
struct sh2 {
band0 b0;
band1 b1;
sh2 operator+(sh2 rhs) {
sh2 result;
result.b0 = b0 + rhs.b0;
result.b1 = b1 + rhs.b1;
return result;
}
sh2 operator-(sh2 rhs) {
sh2 result;
result.b0 = b0 - rhs.b0;
result.b1 = b1 - rhs.b1;
return result;
}
sh2 operator*(float rhs) {
sh2 result;
result.b0 = b0 * rhs;
result.b1 = b1 * rhs;
return result;
}
};
sh2 coefficients_sh2(float3 dir) {
return (sh2)float4(coefficients_band0(dir), coefficients_band1(dir));
}
sh2 rotate_zh(zh::zh2 v, float3 z) {
return (sh2)float4(rotate_zh_band0(v.x, z), rotate_zh_band1(v.y, z));
}
float integrate(sh2 lhs, sh2 rhs) {
return integrate(lhs.b0, rhs.b0) + integrate(lhs.b1, rhs.b1);
}
namespace clamped_cosine {
static const float band0_zh = 0.5f * sqrt_pi;
static const float band1_zh = sqrt_pi / sqrt_3;
band0 eval_band0(float3 v) {
return rotate_zh_band0(band0_zh, v);
}
band1 eval_band1(float3 v) {
return rotate_zh_band1(band1_zh, v);
}
sh2 eval_sh2(float3 v) {
return (sh2)float4(eval_band0(v), eval_band1(v));
}
}
namespace impulse {
static const float band0_zh = 0.5f / sqrt_pi;
static const float band1_zh = 0.5f * sqrt_3 / sqrt_pi;
band0 eval_band0(float3 v) {
return rotate_zh_band0(band0_zh, v);
}
band1 eval_band1(float3 v) {
return rotate_zh_band1(band1_zh, v);
}
sh2 eval_sh2(float3 v) {
return (sh2)float4(eval_band0(v), eval_band1(v));
}
}
}
#line 1 "test.hlsl"
StructuredBuffer<float4> input1;
StructuredBuffer<float4> input2;
RWStructuredBuffer<float4> output;
[numthreads(64, 1, 1)]
void main_cs(uint id : SV_DispatchThreadID) {
sh::sh2 in1 = (sh::sh2)input1[id];
sh::sh2 in2 = (sh::sh2)input2[id];
output[id] = (float4)sh::integrate(in1, in2);
}
Compile with dxc test.i -HV 2021 -T cs_6_5 -E main_cs and dxc test.i -HV 2021 -T cs_6_5 -E main_cs -spirv.
Expected result
The shader should compile.
Actual result
The shader compiles for the first command line, but fails for the second with the given error:
fatal error: generated SPIR-V is invalid: Expected total number of given components to be equal to the size of Result Type vector
%28 = OpCompositeConstruct %v3float %27 %27 %27
note: please file a bug report on https://github.com/Microsoft/DirectXShaderCompiler/issues with source code if possible
Further technical details
This comes from the version of DXC bundled with Vulkan SDK 1.3.236.0.
dxcompiler.dll: 1.7 - 1.7.0.3738 (cafbbed4d)
Activity
Keenuts commentedon Jan 3, 2023
Hi! Thanks for the report!
Was able to simplify the bug, rulling out a packing issue on the struct and HLSL2021-sepecific bug:
commit: a0b6fc1
Same error with and without optimizing the code:
Keenuts commentedon Jan 5, 2023
With the repo above, the AST already seems wrong:
Doing a similar kind of cast in clang is forbidden, at least with what I tried. Here for some reason, DXC doesn't complains when the larger item is stored into the smaller struct. I suppose the FlatConversion should complain that the parent and children have different sizes.
Gregory Roth (@pow2clk): do you know if this should be allowed in HLSL? I'd say no, but since we are dealing with float3/float4, maybe it's a language feature (since implicit truncation is only a warning).
lukedan commentedon Feb 10, 2023
Any updates?
Handle vector cast in flat conversion
Handle vector cast in flat conversion
s-perron commentedon Oct 2, 2023
The PR above fixes the first issue, but there is another issue once that is fixed.
s-perron commentedon Oct 2, 2023
The problem is more complicated than the case that Nathan mentioned. If there are multiple vectors in the struct, then the input vector for the case needs to be spread out over them.
Here is another reduced test case:
s-perron commentedon Oct 3, 2023
We need to implement something similar to what the DXIL code does:
DirectXShaderCompiler/tools/clang/lib/CodeGen/CGHLSLMS.cpp
Lines 5769 to 5787 in 517e702
s-perron commentedon Oct 3, 2023
The current algorithm to cast to and from structs is to have a 1-to-1 correspondence between the nth element of the source with the nth element of the destination. However that is not correct, as the example I have above shows. The algorithm needs to flow into a few steps that corresponds to what the DXIL code generation does above.
s-perron commentedon Oct 3, 2023
This is a bigger change, and I will not get back to it for a while.
s-perron commentedon Jan 18, 2024
As I look into it more, the flat conversion code in SPIR-V gets many cases wrong:
struct-to-struct when members do not match: https://godbolt.org/z/MqGzGdPPT
struct with inheritance: https://godbolt.org/z/dWWr9ffh1
The code also has many special cases in the code, but they should mostly fall out of the simple algorithm used on the DXIL path.
[SPIRV] Reimplement flat conversion.
[SPIRV] Reimplement flat conversion.
[SPIRV] Reimplement flat conversion. (#6189)