DirectXShaderCompiler icon indicating copy to clipboard operation
DirectXShaderCompiler copied to clipboard

[SPIR-V] Internal compiler error when attempting to use vk::BufferPointer without uint64_t

Open deprilula28 opened this issue 1 year ago • 5 comments

Description Internal compiler error when attempting to use vk::BufferPointer without uint64_t

Steps to Reproduce

https://godbolt.org/z/7b1PhWGb9

Actual Behavior

Message:

fatal error: generated SPIR-V is invalid: OpLoad Result Type <id> '8[%StaticMeshVertex]' does not match Pointer <id> '52[%52]'s type.
  %53 = OpLoad %StaticMeshVertex %52 Aligned 16

note: please file a bug report on https://github.com/Microsoft/DirectXShaderCompiler/issues with source code if possible

Compiler returned: 5

Environment

  • libdxcompiler.so: 1.9(dev;1-adffd31e); libdxil.so: 1.9
  • Windows 11

deprilula28 avatar May 19 '25 16:05 deprilula28

The problem seems to stem from the fact that DXC is adamant on generating two OpStruct of the same type

%StaticMeshVertex = OpTypeStruct %v3float %uint
%StaticMeshVertex_0 = OpTypeStruct %v3float %uint
%_ptr_PhysicalStorageBuffer_StaticMeshVertex_0 = OpTypePointer PhysicalStorageBuffer %StaticMeshVertex_0
...
         %51 = OpLoad %StaticMeshVertex %50 Aligned 16

vk::BufferPointer<StaticMeshVertex,Alignment>::Get() is declared with %StaticMeshVertex_0 while the Get attempts to OpLoad a %StaticMeshVertex

@danbrown-amd I see another bug, from #7459 which is the alignment on the Op-Load is completely arbitrary, even with -fvk-use-scalar-alignment

I see why two distinct SPIR-V types are getting created. dxc is creating a temporary variable for the result of calling VertexFactory_GetVertex() so it can use OpAccessChain from there to get to the location of Position. Temporaries get created with a void layout (comment: "We are creating a temporary variable in the Function storage class here, which means it has void layout rule."), meaning there are no offsets defined for the fields. However, the buffer contents' layout rule is that indicated by SpirvCodeGenOptions::sBufferLayoutRule. ("The pointee of a vk::BufferPointer is considered to be a buffer and will be laid out as the user directs all buffers to be laid out through the dxc compiler.") Fields with offsets and fields without offsets compare as non-equal, so the structs as a whole lower to different SPIR-V types.

What I don't yet understand is why other code in the test suite featuring structs with defined layouts doesn't also fail to compile due to this stipulation for temporary variables. So I'm still trying to figure out how best to fix the issue without breaking anything else. (@s-perron, @Keenuts: do you have any advice to give here?)

danbrown-amd avatar May 29 '25 02:05 danbrown-amd

normally I'd suspect the spir-v intrinsics causing a problem, but here it evidently shouldn't vecause the uint64_t is created from a temporary.

If we remove all the intrinsics you still get a problem https://godbolt.org/z/K7GoEM6oq

Sorry I'm late to this. I'll make some guess at to what is going on.

The problem instruction (when compiled with -fcgl to avoid optimization) are:

         %80 = OpConvertUToPtr %_ptr_PhysicalStorageBuffer_StaticMeshVertex_0 %77
         %81 = OpLoad %StaticMeshVertex %80 Aligned 16
               OpReturnValue %81

I believe this comes from the call to get(). The pointer type for the convert is correct. The spir-v spec says that the type pointer to by a pointer in the physical storage buffer storage class must have an explicit layout. This means the load has the wrong type.

This usually happens because the load does not have the correct layout type. When the load is generated, SetLayoutType should be called and it set it correctly. I would look for the place that load is generated, and make sure the layout is set correctly.

The return in the next instruction require a value without an explicit layout. That should be taken care of when processing the return instruction. It will recognize that the layout of the load does not match the expected layout of the return instruction and it will add code to convert the type. You should not have to do anything.

s-perron avatar May 29 '25 17:05 s-perron

FWIW, if the function is inlined, the type is not duplicated, and the compilation succeeds.

danbrown-amd avatar May 29 '25 19:05 danbrown-amd