Skip to content
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

Expose groupshared memory in such a way that aliasing of variables is possible #334

Open
AdamJMiles opened this issue Oct 10, 2024 · 2 comments
Labels
enhancement New feature or request Theme:Gaps Issues related to feature gaps
Milestone

Comments

@AdamJMiles
Copy link
Member

Today, variables (usually arrays) placed into groupshared memory are simply marked with 'groupshared' and the compiler sees fit where to place those variables in the available groupshared memory.

However, it's often the case that the lifetime of two different variables/arrays are non-overlapping which is not detected by the compiler (in my experience). The total amount of groupshared memory used by a shader is simply the sum of all referenced variables in groupshared memory.

This lack of aliasing leads to two distinct problems with undesirable workarounds:

  1. More groupshared memory is used than is strictly necessary.
  2. It's very difficult to access the same region of groupshared memory through two different types. For example, you might want to store an array of uint4s, but then index into that array as if they were float16_t2.

I can see two potential solutions, with no strong feelings yet on which one I prefer.

  1. Allow a constant-buffer style "offset(X)" semantic on "groupshared" variables to explicitly set their relative offset in the groupshared allocation, thereby allowing aliasing of said variables.
  2. Expose groupshared memory like a RWByteAddressBuffer that just happens to be backed by groupshared memory. The contents start off undefined and a shader can Load and Store to it for the lifetime of the shader at whatever byte address they see fit. This takes care of aliasing by allowing a shader author to "Store" while also "Load" from the same byte address. This paradigm already exists for UAVs, so it would be fairly natural to think of a RWByteAddressBuffer spanning a user-declared amount of groupshared memory.

Both approaches would probably need to be paired with a new entry point attribute akin to

[groupsharedmemoryrequired(4096)] // 4KB of groupshared memory required

Since the compiler is no longer summing the total of all groupshared variables to determine how much groupshared memory a shader needs.

Thanks,

Adam

@llvm-beanz
Copy link
Collaborator

Related #333

@devshgraphicsprogramming

Technically should be doable with current state of Inline SPIR-V, here's my attempt

https://godbolt.org/z/bbT3E8cqz

@damyanp damyanp added this to the HLSL Backlog milestone Oct 16, 2024
@damyanp damyanp added Theme:Gaps Issues related to feature gaps and removed needs-triage labels Oct 16, 2024
@damyanp damyanp moved this to Triaged in HLSL Triage Oct 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Theme:Gaps Issues related to feature gaps
Projects
Status: Triaged
Development

No branches or pull requests

4 participants