Skip to content

[SPIR-V] fatal error: generated SPIR-V is invalid: Expected total number of given components to be equal to the size of Result Type vector #4906

Closed

Description

@lukedan

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

  1. Keenuts commented on Jan 3, 2023

    @Keenuts
    Collaborator

    Hi! Thanks for the report!

    Was able to simplify the bug, rulling out a packing issue on the struct and HLSL2021-sepecific bug:

    struct my_struct {
      float3 my_field;
    };
    
    StructuredBuffer<float4> input2;
    RWStructuredBuffer<float> output;
    
    [numthreads(64, 1, 1)]
    void main_cs(uint id : SV_DispatchThreadID) {
      my_struct in2 = (my_struct)input2[id];
      output[id] = dot(in2.my_field, in2.my_field);
    }

    commit: a0b6fc1

    ./build/bin/dxc ./repro.hlsl -T cs_6_6 -E main_cs -spirv -Od
    

    Same error with and without optimizing the code:

             %34 = OpLoad %uint %id
             %36 = OpAccessChain %_ptr_Uniform_v4float %input2 %int_0 %34
             %37 = OpLoad %v4float %36
             %38 = OpCompositeConstruct %v3float %37 %37 %37
  2. added
    bug
    spirv
    on Jan 3, 2023
  3. Keenuts commented on Jan 5, 2023

    @Keenuts
    Collaborator

    With the repo above, the AST already seems wrong:

    | | | `-VarDecl 0x5619b8fedbd0 <col:3, col:39> col:13 used in2 'my_struct' cinit
    | | |   `-CStyleCastExpr 0x5619b8fee0d0 <col:19, col:39> 'my_struct' <NoOp>
    | | |     `-ImplicitCastExpr 0x5619b8fee0b8 <col:30, col:39> 'my_struct' <FlatConversion>
    | | |       `-ImplicitCastExpr 0x5619b8fee0a0 <col:30, col:39> 'vector<float, 4>' <LValueToRValue>
    | | |         `-CXXOperatorCallExpr 0x5619b8fee030 <col:30, col:39> 'const vector<float, 4>' lvalue
    

    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).

  4. lukedan commented on Feb 10, 2023

    @lukedan
    Author

    Any updates?

  5. added 2 commits that reference this issue on Oct 2, 2023
    5a3d3e7
    350f7ba
  6. s-perron commented on Oct 2, 2023

    @s-perron
    Collaborator

    The PR above fixes the first issue, but there is another issue once that is fixed.

    » 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                                                                                                                                     1 ↵ stevenperron@stevenperron-cloudtop
    fatal error: generated SPIR-V is invalid: GLSL.std.450 Fma: expected types of all operands to be equal to Result Type
      %39 = OpExtInst %float %1 Fma %27 %33 %38
    
    note: please file a bug report on https://github.com/Microsoft/DirectXShaderCompiler/issues with source code if possible
    
  7. s-perron commented on Oct 2, 2023

    @s-perron
    Collaborator

    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:

    namespace sh {
     struct sh2 {
      float b0;
      float3 b1;
     };
    }
    
    StructuredBuffer<float4> input;
    RWStructuredBuffer<float4> output;
    
    [numthreads(64, 1, 1)]
    void main_cs(uint id : SV_DispatchThreadID) {
    // The first element of input[id] ends up in in1.b0, and the remaining 3 in in1.b1.
     sh::sh2 in1 = (sh::sh2)input[id];
    
    // This does the same type of cast, but it is reversed.
     output[id] = (float4)in1;
    }
    
  8. s-perron commented on Oct 3, 2023

    @s-perron
    Collaborator

    We need to implement something similar to what the DXIL code does:

    // It is possible to implement EmitHLSLAggregateCopy, the same way. But split
    // value to scalar will generate many instruction when src type is same as
    // dest type.
    SmallVector<Value *, 4> GEPIdxStack;
    SmallVector<Value *, 4> SrcPtrs;
    SmallVector<QualType, 4> SrcQualTys;
    FlattenAggregatePtrToGepList(CGF, SrcPtr, GEPIdxStack, SrcTy,
    SrcPtr->getType(), SrcPtrs, SrcQualTys);
    SmallVector<Value *, 4> SrcVals;
    LoadElements(CGF, SrcPtrs, SrcQualTys, SrcVals);
    GEPIdxStack.clear();
    SmallVector<Value *, 4> DstPtrs;
    SmallVector<QualType, 4> DstQualTys;
    FlattenAggregatePtrToGepList(CGF, DestPtr, GEPIdxStack, DestTy,
    DestPtr->getType(), DstPtrs, DstQualTys);
    ConvertAndStoreElements(CGF, SrcVals, SrcQualTys, DstPtrs, DstQualTys);

  9. s-perron commented on Oct 3, 2023

    @s-perron
    Collaborator

    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.

  10. s-perron commented on Oct 3, 2023

    @s-perron
    Collaborator

    This is a bigger change, and I will not get back to it for a while.

  11. s-perron commented on Jan 18, 2024

    @s-perron
    Collaborator

    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.

  12. added 2 commits that reference this issue on Jan 23, 2024
    6b63c3d
    1d271cd
  13. added a commit that references this issue on Jan 30, 2024
    8019c71
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugspirv

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions