Skip to content

[SPIR-V] DXC generates invalid SPIR-V when using RawBufferLoad with a struct. #5327

Closed

Description

From the SPIR-V spec:

Composite objects in the StorageBuffer, PhysicalStorageBuffer, Uniform, and PushConstant Storage Classes must be explicitly laid out. The following apply to all the aggregate and matrix types describing such an object, recursively through their nested types:

  • Each structure-type member must have an Offset decoration.

SPIRV-Tools started to validate this rule, meaning DXC now fails hard.
Seems like we simply don't generate the Offset member decoration when the offset is not explicit.

Looking at the code, I see 2 places to handle that:

  • LowerTypeVisitor.cpp: only add the offset decoration when required.
  • EmitVisitor.cpp: always add the offset decoration.

Method 1:
To fix this in the LowerTypeVisitor.cpp, we should not only pass the layout, but also the storage class where the type is used (which here is what's changing the offsets/decorations).
Then, depending on the storage class, the offset will be set in the type or not, the the rest of the code should handle it.
This seems to be the clean method, but requires some larger changes to the lowerTypeVisitor.

Method 2:
The second method is a bit more basic: always add the decoration.
I don't think the spec is against always specifying the Offset member decoration.
This would allow us to have a simple fix.

  • Bitfields should be handled correctly since the fields are already squashed.
  • Specific offsets (layout rule by ex), the offset is pre-computed, so we don't have to compute it. Otherwise we have the full-type, so we should always have the correct size.

Repro steps:

struct S {
 uint f1;
};

uint64_t Address;

[numthreads(64, 1, 1)]
void main(uint3 id : SV_DispatchThreadID) {
 uint tmp = vk::RawBufferLoad<S>(Address).f1;
}
dxc -spirv -T cs_6_0 -E main -HV 2021 repro.hlsl

Activity

  1. added
    bug
    spirv
    on Jun 27, 2023
  2. changed the title [-]DXC generates invalid SPIR-V when using RawBufferLoad with a struct.[/-] [+][SPIR-V] DXC generates invalid SPIR-V when using RawBufferLoad with a struct.[/+] on Jun 27, 2023
  3. Keenuts commented on Jun 27, 2023

    @Keenuts
    CollaboratorAuthor

    Initial exploration shows the method 2 won't be that trivial:

    • some edge cases needs to be handled, like a RuntimeArray in the struct. I believe it's not legal to add any member after if it's a VLA, but unsure.
    • Some internal structures layouts, like SparseResidencyStruct. Handling those would mean computing the correct size for all those specific types.

    From the amount of special cases I need to handle just by poking at it, it seems the "this method is simpler" argument is not valid anymore.
    I'll fallback on the method 1, which seems safer than always setting the Offset decoration.

  4. s-perron commented on Jun 28, 2023

    @s-perron
    Collaborator

    Will a fix for this issue also fix #5122?

  5. Keenuts commented on Jun 28, 2023

    @Keenuts
    CollaboratorAuthor

    Not sure, but I guess I'll figure out once solved?

  6. added a commit that references this issue on Jul 6, 2023
    c17a556
  7. added a commit that references this issue on Jul 7, 2023
    a46f491
  8. added a commit that references this issue on Jul 7, 2023
    6289ba3
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