You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[SPIR-V] [Proposal 0011] SPIR-V Type for a PhysicalStorageBuffer class OpTypePointer fails to compile because of missing offsets for a struct #6541
Description
This was already solved for vk::RawBufferLoad<Composite> however it rears its head again when we start using vk::SpirvOpaqueType to declare the same poitner type RawBufferLoad uses under the hood.
gets instantiated where T is a composite, we get the following error
fatal error: generated SPIR-V is invalid: Structure id 7 decorated as Block for variable in PhysicalStorageBuffer storage class must follow relaxed storage buffer layout rules: member 0 is missing an Offset decoration
Its been over a year since I've checkd, but seems that still, contrary to the docs
struct Test
{
// The vk::ext_decorate don't seem to work or emit `OpMemberDecorate` at all
[[vk::ext_decorate(/*Offset*/ 35,0)]] float4 mem1;
[[vk::ext_decorate(/*Offset*/ 35,16)]] float mem2;
[[vk::ext_decorate(/*Offset*/ 35,20)]] int mem3;
};
There are no tests for this functionality in the DXC repo at all
The vk::ext_decorate seems really flaky, I'm trying to decorate my pointer variable as AliasedPointer and seems like some SPIR-V Optimization rips it away
I'm starting to understand the problem, and I'll need to think about how to fix it. The problem in the smaller example is that the SPIR-V backend does not know how to propagate the layout rule for the vk::ext_instruction functions.
In general, the layout rule for a return value in a function is Void. When DXC generates the callee, it will make sure the return value has layout rule Void. In this case, the load function is a single OpLoad instruction, which will result in an object of type T that has a different layout rule.
There are a few options to fix this. In the expansion of the load function, DXC could add code to always rebuild the type with the correct layout rule. This is the best option if it can work.
Other option involve fixing up the code after wards, either in DXC or spirv-opt.
Opcopylogical only helps a little. It would not translate the data pointed to by a pointer. You would need a load. It also doesn't help with the result type for a vk::ext_instruction function. The result type is invalid even if an opcopylogical is applied afterwards.
Its been over a year since I've checkd, but seems that still, contrary to the docs
struct Test
{
// The vk::ext_decorate don't seem to work or emit `OpMemberDecorate` at all
[[vk::ext_decorate(/*Offset*/ 35,0)]] float4 mem1;
[[vk::ext_decorate(/*Offset*/ 35,16)]] float mem2;
[[vk::ext_decorate(/*Offset*/ 35,20)]] int mem3;
};
There are no tests for this functionality in the DXC repo at all
I'm starting to understand the problem, and I'll need to think about how to fix it. The problem in the smaller example is that the SPIR-V backend does not know how to propagate the layout rule for the vk::ext_instruction functions.
In general, the layout rule for a return value in a function is Void. When DXC generates the callee, it will make sure the return value has layout rule Void. In this case, the load function is a single OpLoad instruction, which will result in an object of type T that has a different layout rule.
There are a few options to fix this. In the expansion of the load function, DXC could add code to always rebuild the type with the correct layout rule. This is the best option if it can work.
Other option involve fixing up the code after wards, either in DXC or spirv-opt.
Shouldn't this PR merge #6873 eliminate the need for you to pass the -Vd commandline option?
We are able to deal with workgroup pointers, and we are adding this to the HLSL standard headers: #6873.
I am coming to believe that the layout issue with storage classes that require a layout is insurmountable. We might be able to do some targeted fixes for pointer. If we have a SpirvOpqaueType tha that is a pointer, then try to deduce the layout based on the storage class. However, it would still fail for instructions that do a load or store type instruction using the pointers.
There is no way to specify that the result of an vk::ext_instruction function must have a particular layout. We may need to add an attribute to indicate the layout of the result. I am not planning on doing that until it becomes something we need for the HLSL standard headers that we writing.
I will close this issue, and open a new one in HLSL spec when we decide that we need the layout attribute.
doesn't the -fvk-use-scalar-layout option turn EVERY struct into one that has a scalar layout instead of the std140 and std430 shenanigans? (SSBO, UBO and BDA)
No it does not. In SPIR-V there are certain storage class that never have a data layout (workgroup, private, function). If you do an OpLoad the layout of the result type has to match the layout of the pointer type. In the normal case, this is easy. DXC knows when it is generating an OpLoad, so it looks at the pointer type to see which layout the result needs.
The problem in your case is that the instruction that does the load is opaque to the code generator, which decides the layout. So the code generator does not know how to deduce the layout type. It just opts for no layout. It works for some cases, and not for others.
It is a big change to fix the way that DXC handles layouts in general. This is too big of a change to go into DXC at this point.
Yes, there is a hole. What should the layout be for a return value from a function? We chose to make it no "no layout". That is the calling convention. However, if the function call represents a single SPIR-V instruction like an OpLoad, then there is no fixed layout. The layout depends on the input. This ends up breaking the calling convention. Any solution to this is very invasive.
It might also be DXC specific because I'm not sure how we will deal with this in Clang.
Post C++11, don't functions always return r-value references when returning structs by value? so one could "walk" eventually find the layout?
Second question, more to the point, this maybe would be solved if the layout was inherently baked into the type.
For every offset decoration combo I can see that DXC codegen emits a separate SPIR-V struct declaration.
Would treating a plain T as scalar-layout (with explicit offset attributes overriding) as the canonical layout and then letting private, groupshared, function be actually separate types ___layoutless<T> with implicit conversions to/from T work?
Taking a step back, the whole problem for my BDA use case seems to stem from the fact that my vk::SpirvType for a pointer doesn't know which concrete SPIR-V struct declaration to hash-cons to. I'd argue that its not really the OpLoad or any other intrinsic that needs to know anything about layouts, its simply my vk::SpirvType<32,... .
So maybe the only thing that needs to be fixed is how vk::SpirvType and vk::SpirvOpaqueType handles template parameters which are types and not vk::integral_constant, maybe vk::laid_out<T,enum layout> ?
I can't use vk::RawBufferLoad on the Meta Quest 2 or 3 because the Adreno doesn't have the shaderInt64 feature and I need it implemented though uvec2 casting, so Inline SPIR-V is my only way to use BDA in HLSL on this GPU
I'm just not getting the picture why its difficult with scalar layout. I do understand the difficulty with multiple layouts (std140, std430, and scalar all being used at the same time), because the layout and storage class don't map unambiguously.
I know that you have to generate multiple OpStruct for the same HLSL struct with different member offset decorations and the codegen has to work out which one to use from the context.
The problem in your case is that the instruction that does the load is opaque to the code generator, which decides the layout. So the code generator does not know how to deduce the layout type. It just opts for no layout. It works for some cases, and not for others.
The problem in the smaller example is that the SPIR-V backend does not know how to propagate the layout rule for the vk::ext_instruction functions.
In general, the layout rule for a return value in a function is Void. When DXC generates the callee, it will make sure the return value has layout rule Void. In this case, the load function is a single OpLoad instruction, which will result in an object of type T that has a different layout rule.
As I have thought about this more, DXC does not know the correct layout for the result of a vk::ext_instruction function. It cannot generate code that does to or from the correct type without knowing about the instruction. This is only a problem if the result type is a struct or array, and the instruction is dealing with externally visible memory.
I am coming to believe that the layout issue with storage classes that require a layout is insurmountable. We might be able to do some targeted fixes for pointer. If we have a SpirvOpqaueType tha that is a pointer, then try to deduce the layout based on the storage class. However, it would still fail for instructions that do a load or store type instruction using the pointers.
Steven Perron (@s-perron) then let me annotate the layout of a SPIR-V vk::ext_instruction return type (and maybe even the parameters) either via a template vk::SpirvLaidout<T,LayoutEnum> or an attribute [[vk::ext_layout(LayoutEnum)]] ?
Its your own idea
There is no way to specify that the result of an vk::ext_instruction function must have a particular layout. We may need to add an attribute to indicate the layout of the result. I am not planning on doing that until it becomes something we need for the HLSL standard headers that we writing.
I will close this issue, and open a new one in HLSL spec when we decide that we need the layout attribute.
Its a simple solution that I could live with and is probably not insurmountable on your end.
P.S. You say the OpStruct needs to match on both side of an instruction (lets say OpLoad), I could even write the _static_cast between the two different layout Ts to do the conversions
Its been over a year since I've checkd, but seems that still, contrary to the docs
struct Test
{
// The vk::ext_decorate don't seem to work or emit `OpMemberDecorate` at all
[[vk::ext_decorate(/*Offset*/ 35,0)]] float4 mem1;
[[vk::ext_decorate(/*Offset*/ 35,16)]] float mem2;
[[vk::ext_decorate(/*Offset*/ 35,20)]] int mem3;
};
There are no tests for this functionality in the DXC repo at all
fatal error: generated SPIR-V is invalid: Structure id 6 decorated as Block for variable in PhysicalStorageBuffer storage class must follow scalar storage buffer layout rules: member 0 is missing an Offset decoration
%Test = OpTypeStruct %v4float %float %int
I don't get whats wrong, because the emitted SPIR-V seems identical (albeit in slightly different order) if I disable the spir-v validation with -Vd.
Maybe something invalid gets created before optimization? I didn't try with -O0 flag.
That is expected. DXC does not know what the decoration attribute it. It just passes is along. If DXC would normally add the offset decoration itself it will add it along with the one you told it to add.
That is expected. DXC does not know what the decoration attribute it. It just passes is along. If DXC would normally add the offset decoration itself it will add it along with the one you told it to add.
that's incredibly strange, how can godbolt still produce the error from before but show new --version when you recompile?
Activity
devshgraphicsprogramming commentedon Apr 18, 2024
Its been over a year since I've checkd, but seems that still, contrary to the docs
There are no tests for this functionality in the DXC repo at all
https://godbolt.org/z/x7sx4Po9Y
sudonatalie commentedon May 1, 2024
Thanks for reporting Mateusz Kielan (@devshgraphicsprogramming). You've piqued my interest with #6489 (comment) so we'll try to prioritize this.
devshgraphicsprogramming commentedon May 2, 2024
I can't conform to Proposal 0010 100% but I can get something that has all the functionality.
One reason is that there's no reflection neither in C++ or HLSL so for structs I'd be basically defining them throuhg a macro.
sudonatalie commentedon May 2, 2024
We'll want a dedicated implementation anyways, but I'm still keen to see full features done successfully in inline SPIR-V.
devshgraphicsprogramming commentedon May 2, 2024
Thevk::ext_decorateseems really flaky, I'm trying to decorate my pointer variable asAliasedPointerand seems like some SPIR-V Optimization rips it awayhttps://godbolt.org/z/9szf1nYq5NVM: got sniped by KhronosGroup/SPIRV-Registry#140 (comment)
s-perron commentedon Jun 17, 2024
This seems like an issue with the layout rules not getting passed around correctly for the vk::SpirvType. Here is another case that fails. It fails differently, but it probably a related issues: https://godbolt.org/z/3vq3xWY7v.
s-perron commentedon Jun 17, 2024
I'm starting to understand the problem, and I'll need to think about how to fix it. The problem in the smaller example is that the SPIR-V backend does not know how to propagate the layout rule for the
vk::ext_instructionfunctions.In general, the layout rule for a return value in a function is
Void. When DXC generates the callee, it will make sure the return value has layout ruleVoid. In this case, theloadfunction is a singleOpLoadinstruction, which will result in an object of type T that has a different layout rule.There are a few options to fix this. In the expansion of the
loadfunction, DXC could add code to always rebuild the type with the correct layout rule. This is the best option if it can work.Other option involve fixing up the code after wards, either in DXC or spirv-opt.
7 remaining items
s-perron commentedon Aug 23, 2024
Opcopylogical only helps a little. It would not translate the data pointed to by a pointer. You would need a load. It also doesn't help with the result type for a vk::ext_instruction function. The result type is invalid even if an opcopylogical is applied afterwards.
s-perron commentedon Sep 3, 2024
We have #4195 to cover this issue.
devshgraphicsprogramming commentedon Oct 3, 2024
Shouldn't this PR merge #6873 eliminate the need for you to pass the
-Vdcommandline option?devshgraphicsprogramming commentedon Oct 3, 2024
doesn't the
-fvk-use-scalar-layoutoption turn EVERY struct into one that has a scalar layout instead of the std140 and std430 shenanigans? (SSBO, UBO and BDA)s-perron commentedon Oct 3, 2024
No it does not. In SPIR-V there are certain storage class that never have a data layout (workgroup, private, function). If you do an OpLoad the layout of the result type has to match the layout of the pointer type. In the normal case, this is easy. DXC knows when it is generating an OpLoad, so it looks at the pointer type to see which layout the result needs.
The problem in your case is that the instruction that does the load is opaque to the code generator, which decides the layout. So the code generator does not know how to deduce the layout type. It just opts for no layout. It works for some cases, and not for others.
It is a big change to fix the way that DXC handles layouts in general. This is too big of a change to go into DXC at this point.
devshgraphicsprogramming commentedon Oct 4, 2024
Are there any holes in:
for your layout resolution?
This is pure unambiguous mapping of Storage Class to Layout, DXC never respected the attribute offset annotation decoration for inline spirv.
s-perron commentedon Oct 4, 2024
Yes, there is a hole. What should the layout be for a return value from a function? We chose to make it no "no layout". That is the calling convention. However, if the function call represents a single SPIR-V instruction like an OpLoad, then there is no fixed layout. The layout depends on the input. This ends up breaking the calling convention. Any solution to this is very invasive.
It might also be DXC specific because I'm not sure how we will deal with this in Clang.
devshgraphicsprogramming commentedon Oct 5, 2024
Post C++11, don't functions always return r-value references when returning structs by value? so one could "walk" eventually find the layout?
Second question, more to the point, this maybe would be solved if the layout was inherently baked into the type.
For every offset decoration combo I can see that DXC codegen emits a separate SPIR-V struct declaration.
Would treating a plain
Tas scalar-layout (with explicit offset attributes overriding) as the canonical layout and then letting private, groupshared, function be actually separate types___layoutless<T>with implicit conversions to/fromTwork?Taking a step back, the whole problem for my BDA use case seems to stem from the fact that my
vk::SpirvTypefor a pointer doesn't know which concrete SPIR-V struct declaration to hash-cons to. I'd argue that its not really theOpLoador any other intrinsic that needs to know anything about layouts, its simply myvk::SpirvType<32,....So maybe the only thing that needs to be fixed is how
vk::SpirvTypeandvk::SpirvOpaqueTypehandles template parameters which are types and notvk::integral_constant, maybevk::laid_out<T,enum layout>?P.S. are you sure
groupsharedhas no memory layout, there's theKHR_workgroup_explicit_layoutextension.https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VK_KHR_workgroup_memory_explicit_layout.html
https://htmlpreview.github.io/?https://github.com/KhronosGroup/SPIRV-Registry/blob/main/extensions/KHR/SPV_KHR_workgroup_memory_explicit_layout.html
SamCZ commentedon Feb 6, 2025
I can't use vk::RawBufferLoad on the Meta Quest 2 or 3 because the Adreno doesn't have the shaderInt64 feature and I need it implemented though uvec2 casting, so Inline SPIR-V is my only way to use BDA in HLSL on this GPU
devshgraphicsprogramming commentedon Feb 8, 2025
I'm just not getting the picture why its difficult with scalar layout. I do understand the difficulty with multiple layouts (std140, std430, and scalar all being used at the same time), because the layout and storage class don't map unambiguously.I know that you have to generate multiple OpStruct for the same HLSL
structwith different member offset decorations and the codegen has to work out which one to use from the context.Steven Perron (@s-perron) then let me annotate the layout of a SPIR-V
vk::ext_instructionreturn type (and maybe even the parameters) either via a templatevk::SpirvLaidout<T,LayoutEnum>or an attribute[[vk::ext_layout(LayoutEnum)]]?Its your own idea
Its a simple solution that I could live with and is probably not insurmountable on your end.
P.S. You say the OpStruct needs to match on both side of an instruction (lets say
OpLoad), I could even write the_static_castbetween the two different layoutTs to do the conversionsIn GLSL https://godbolt.org/z/7fcW9nsjq if I make
I get
where
And if I target SPIR-V 1.4 I get
OpCopyLogical.devshgraphicsprogramming commentedon Feb 10, 2025
I'll write one up there then.
devshgraphicsprogramming commentedon Mar 1, 2025
Steven Perron (@s-perron) after your #7137 PR merge it now emits
but still complains about
I don't get whats wrong, because the emitted SPIR-V seems identical (albeit in slightly different order) if I disable the spir-v validation with
-Vd.Maybe something invalid gets created before optimization? I didn't try with
-O0flag.P.S. Also I found a fun bug, if I shove a manually decorated
Teststruct intoRawBufferLoadit will decorate the offset on the member twice leading to a validation error https://godbolt.org/z/TxjT358cKs-perron commentedon Mar 4, 2025
When I try the link https://godbolt.org/z/x7sx4Po9Y it does not have a validation failure.
That is expected. DXC does not know what the decoration attribute it. It just passes is along. If DXC would normally add the offset decoration itself it will add it along with the one you told it to add.
devshgraphicsprogramming commentedon Mar 5, 2025
that's incredibly strange, how can godbolt still produce the error from before but show new
--versionwhen you recompile?