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

[Feature Request] Allow readonly usage of atomics #4574

Open
stefnotch opened this issue Apr 12, 2024 · 2 comments
Open

[Feature Request] Allow readonly usage of atomics #4574

stefnotch opened this issue Apr 12, 2024 · 2 comments
Labels
wgsl WebGPU Shading Language Issues
Milestone

Comments

@stefnotch
Copy link

stefnotch commented Apr 12, 2024

Problem

Currently, when one defines a struct with an atomic, one has to use it as a read_write.

struct MiscData {
    num_fine_shapes: atomic<i32>,
    ...
};
@group(0) @binding(0) var<storage, read_write> buffer : MiscData;

If one tries to use it as a read only, which could make sense when passing the storage buffer to subsequent shaders, then one gets an error, due to how WGSL validation works at the moment.

// This is currently not possible, since it leads to an error
@group(0) @binding(0) var<storage, read> buffer : MiscData;
Error while parsing WGSL: :26:42 error: atomic variables in <storage> address space must have read_write access mode
@group(0) @binding(0) var<storage, read> buffer : MiscData;
                                         ^^^^^^^^^^^^^

:16:1 note: atomic sub-type of 'MiscData' is declared here
num_fine_shapes: atomic<i32>,
^^^^^

Current workaround

This means, if one wants to run multiple compute shaders that only read from an atomic variable in parallel, then one has to resort to creating an extra struct, with exactly the same layout and alignment, but a non-atomic variable.

struct MiscData {
    num_fine_shapes: i32, // I believe the spec says that this is valid
    ...
};
@group(0) @binding(0) var<storage, read> buffer : MiscData;

That is inconvenient, and could easily lead to either bugs (developer defined the secondary struct incorrectly) or performance left on the table (developer just used read_write).

Request

You could open an issue for that as a feature request. The validation on atomic built-in functions could be changed to have the access restrictions instead of the type. Only atomicLoad would be allowed in the read-only case. Hopefully, drivers would just optimize them to regular loads.
quote from alan-baker

Would it be possible to change the validation on atomic built-in functions to allow for atomicLoad in the read-only case?

@kainino0x kainino0x added the wgsl WebGPU Shading Language Issues label Apr 12, 2024
@alan-baker
Copy link
Contributor

Worth noting that atomicLoad is emulated on D3D12. The good news is that HLSL doesn't use an explicit atomic type, but a WGSL compiler would likely need to specialize load function call tree for the read vs read/write cases.

@kdashg kdashg added this to the Milestone 3+ milestone Apr 30, 2024
@kdashg
Copy link
Contributor

kdashg commented May 14, 2024

WGSL 2024-04-30 Minutes
  • KG: A struct has an atomic in it. We can put that in read-write storage, but you’re not allowed to put it in read-only storage, because our rules don’t permit it.
  • AB: This came out of a discussion where the author wanted to re-use structures, but wanted to remove the atomicness in one case, wanted to reuse the atomics in read-only storage.
  • AB: This would take some work in our backends, so we would like this not to be a priority.
  • JB: That’s Mozilla’s opinion too. Do it later if we want it later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wgsl WebGPU Shading Language Issues
Projects
None yet
Development

No branches or pull requests

4 participants