Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a proposal for how to explicitly specify struct layouts #171
base: main
Are you sure you want to change the base?
Add a proposal for how to explicitly specify struct layouts #171
Changes from 1 commit
e785c3c
29fd8d3
f442a3c
ddfd3d6
53c33d4
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From my perspective, I think it is intuitive for users to have the order of the members in the struct match their order in memory. We'd have to see what kind of usage there is.
On the other hand, this is not a fundamental problem for SPIR-V. In https://docs.vulkan.org/spec/latest/chapters/interfaces.html#interfaces-resources-layout, there is an explicit note:
We would not have to jump through too many hoops to implement this. The deciding factor should be what is best for users.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both DXC and FXC support this and it seems to be fairly well defined in the language. I think we'll want to treat the current situation as a bug in DXC's SPIR-V backend and support it in clang.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there are other open question. How and when do you convert from the
spirv.Layout
ordx.Layout
types to the type without the offset, and how could it interact with optimizations. Consider a structured buffer access like https://godbolt.org/z/z49rEWe58. I'm guessing this would change by replacing%struct.T
withtarget("dx.Layout", 16, 0, 8)
in thedx.RawBuffer
type.But we still have the question of what should the type of the store. How should the GEP look?
At first the store becomes:
Note that the GEPs currently act on the struct type. We cannot change the GEPs to use the target extension type, because they are not allowed in GEPs. We could leave it implicit in some ways, but then the optimizer will assume it knows the layout and optimize accordingly:
Note that the opimizer modified the GEPs assuming it knows the offsets, even though it does not. How will you stop the optimizer from making assumptions about the layout of the struct, since we are representing it differently than llvm-ir usually expects?
This is less of a problem for cbuffers because we expect all access to the cbuffer to be through an intrinsic. There are a few options for structrued buffers:
I have a couple ideas on how to fix this, but only one that seems reasonable. Add an intrinsic that does a GEP of the
dx.Layout
type. This should hide everything from the optimizer.We might also need an intrinsic that will do a
memcpy
between a type with a layout and a type that does not. Consider this example: https://godbolt.org/z/rh5dvd3E7. Thememcpy
copies the buffer contents to a variable forFoo
which expects the struct without the layout information.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
memcpy
type intrinsic could correspond to OpCopyLogical in SPIR-V.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could also expand the
memcpy
to copy the individual member one at a time, if we want to expose more to the optimizer. If we use an intrinsic, copy propagation will not work well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be hard to allow target types into a GEP instruction? Because I feel this would be the best option vs adding another intrinsic:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding target type handling to the GEP instruction sounds nicer than needing a parallel set of operations, but that's definitely a larger change to LLVM. I'll add some notes to the open questions to capture these ideas - we'll need to answer this in some satisfying way in order to handle vk::offset properly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just to be pedantic,
memcpy
is astd::bit_cast
/OpBitCast
equivalent, theOpCopyLogical
is more of astd::copy
- a magical member by member logical copyY'all already know all that, its just that if the meaning
memcpy
starts getting overloaded like that it will lead to horrible head-scratching for outside/new contributors.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we try to use a target type in the existing GEP instruction, I believe we will have many places we will have to add special cases. Any code that tries to optimize a GEP will have have a special case for the target extension type, even if it does nothing with it. That defeats the purpose of using an opaque type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added some text to try to capture the questions here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like I indeed missed an important aspect (Thanks DevSH)
If we use the target type in the GEP, then do a
load
/store
, we are saying we do amemcpy
. And if the layout is different, that would be wrong.Maybe in such case we should emit an intrinsic to have carry the semantic of the
OpCopyLogical
, but not amemcpy
?(This doesn't solve the issue of GEP being allowed to use target types still)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another open issue. How will member functions work? This is related to how the
this
pointer will be handled in member functions.