Repository navigation
Description
Atomic Type
Add a new type, atomic<T>, to WGSL. For MVP, T must be either u32 or i32. Atomics may only be placed in variables in the workgroup or storage storage classes. Atomics may only be operated on by atomic builtin functions.
Translation
In both HLSL and SPIR-V, atomic<T> should be translated the same as T. In MSL, atomic<T> should translate as volatile atomic_uint or volatile atomic_int.
Atomic Functions
Add the following builtin functions:
atomicAdd(atomic_ptr : ptr<SC, atomic<T>>, value : T) -> TatomicMax(atomic_ptr : ptr<SC, atomic<T>>, value : T) -> TatomicMin(atomic_ptr : ptr<SC, atomic<T>>, value : T) -> TatomicAnd(atomic_ptr : ptr<SC, atomic<T>>, value : T) -> TatomicOr(atomic_ptr : ptr<SC, atomic<T>>, value : T) -> TatomicXor(atomic_ptr : ptr<SC, atomic<T>>, value : T) -> TatomicExchange(atomic_ptr : ptr<SC, atomic<T>>, value : T) -> T
In all the above cases, the return value is the original value in the atomic prior to the operation.
Also add compare exchange:
atomicCompareExchangeWeak(atomic_ptr : ptr<SC, atomic<T>>, expected : T, value : T) -> vec2<T>
Compare exchange returns a vec2 where the first element is the original value in the atomic and the second value is whether or not the comparison was successful (1 for success, 0 otherwise). This uses the weak version of compare exchange which means that the comparison can fail spuriously (i.e. even if expected matches the original value).
The memory ordering for all atomic functions is relaxed (see section 29.3 of the C++14 specification). This means that no synchronization or ordering guarantees are made on non-atomic memory accesses, nor on atomic memory accesses operating on different memory addresses (i.e. unless they are mutually ordered atomics).
For all atomic functions, the storage class of atomic_ptr must be workgroup or storage. Atomics on storage storage class are device atomics (e.g. Device memory scope in SPIR-V), while atomics in workgroup storage class are workgroup atomics (e.g. shared variable atomics in HLSL).
Atomic functions must not be used in vertex shaders.
Example:
[[block]] struct A {
atomic<u32> a;
array<u32> rta;
};
var<storage> myVar : A;
// ...
const old : u32 = atomicAdd(myVar.a, 1);
Translation
| WGSL | SPIR-V | HLSL | MSL | GLES |
|---|---|---|---|---|
atomicAdd |
OpAtomicIAdd |
InterlockedAdd |
atomic_fetch_add_explicit |
atomicAdd |
atomicMax |
OpAtomic[S|U]Max1 |
InterlockedMax |
atomic_fetch_max_explicit |
atomicMax |
atomicMin |
OpAtomic[S|U]Min1 |
InterlockedMin |
atomic_fetch_min_explicit |
atomicMin |
atomicAnd |
OpAtomicAnd |
InterlockedAnd |
atomic_fetch_and_explicit |
atomicAnd |
atomicOr |
OpAtomicOr |
InterlockedOr |
atomic_fetch_or_explicit |
atomicOr |
atomicXor |
OpAtomicXor |
InterlockedXor |
atomic_fetch_xor_explicit |
atomicXor |
atomicExchange |
OpAtomicExchange |
InterlockedExchange |
atomic_exchange_explicit |
atomicExchange |
atomicCompareExchangeWeak |
OpAtomicCompareExchange2 |
InterlockedCompareExchange3 |
atomic_compare_exchange_weak_explicit |
atomicCompSwap |
- Signedness is based on
T. - SPIR-V only provides the strong compare exchange.
- If used in a loop, the loop must be annotated with the attribute
[allow_uav_condition]
Considerations
The new type favours translations from WGSL to other dialects (as opposed to translation to WGSL). MSL requires C++ style atomic types. If just i32 or u32 were used, then the declaration of the variable would need to be modified by its usage, which should generally be avoided. One implication of this choice is that not all HLSL or SPIR-V programs will be convertible to WGSL as those languages support atomic operations on regular memory addresses (no special types). One example is if the SPIR-V or HLSL atomic operates on 32-bit integer in a vector. Translating to WGSL (from SPIR-V and HLSL) has the problem avoided in avoid translating from WGSL to MSL (the use must modify the declaration).
Survey of Implementations
Metal (MSL)
Provides atomic_int and atomic_uint types, so 32-bit integer atomics only. Atomic functions only support memory_order_relaxed (no synchronization with other memory operations). Atomic functions are defined in <metal_atomic>. These functions are not limited by the shader stage.
Metal 1.0 supports the following functions for volatile threadgroup (workgroup) and device atomic memory objects:
- Atomic_store_explicit
- Atomic_load_explicit
- Atomic_exchange_explicit
- Atomic_compare_exchange_weak_explicit
- Atomic_fetch_key_explicit
- And, or, add, max, min, sub, xor
Signedness based on memory type
Signed versions use two’s complement with silent wrap around (i.e. same as SPIR-V)
Metal 2.0 added support for non-volatile versions of the atomic functions.
The biggest difference from SPIR-V is that atomic types are required. Atomic operations cannot be performed on any memory object. MSL also does not support atomic image operations (i.e. no atomics through OpImageTexelPointer).
DirectX (HLSL)
HLSL supports interlocked (atomic) functions on int and uint data types since Shader Model 5. HLSL also supports atomic exchanges on any scalar data type. There are no restrictions on shader stage for any of the functions. Atomics can be performed on shared (workgroup) or resource (storage and texture) variables. All atomic operations use relaxed memory ordering.
The following functions are supported:
- InterlockedAdd
- InterlockedAnd
- InterlockedCompareExchange
- When used in a loop, the loop must be annotated with [allow_uav_condition] (in compute shaders)
- Unclear if this is a strong or weak compare exchange
- InterlockedCompareStore
- Could be mapped to compare exchange
- InterlockedExchange
- InterlockedMax
- InterlockedMin
- InterlockedOr
- InterlockedXor
Vulkan (SPIR-V)
Vulkan supports 32-bit integer atomics in Vulkan 1.0. There are extensions to support 64-bit integer atomics and float/half/double atomics (load, store, exchange, min, max, add). Vulkan also supports integer atomic operations on texel components (via OpImageTexelPointer). The memory scope for atomics must be Workgroup or Device (QueueFamily). The storage class of pointers must be: Workgroup, StorageBuffer (and PhysicalStorageBuffer), Uniform (BufferBlock) or Image. Vulkan supports atomics in compute shaders unconditionally, but requires feature bits (fragmentStoresAndAtomics and vertexStoresAndAtomics) to work in Fragment and Vertex shaders. WebGPU currently forbids vertexStoresAndAtomics.
Vulkan supports the following operations:
- OpAtomicLoad
- OpAtomicStore
- OpAtomicExchange
- OpAtomicCompareExchange
- Strong version
- OpAtomicIIncrement
- OpAtomicIDecrement
- OpAtomic[I|F]Add
- OpAtomicISub
- OpAtomic[U|S|F]Min
- OpAtomic[U|S|F]Max
- OpAtomicAnd
- OpAtomicOr
- OpAtomicXor
Weak Compare Exchange
The weak version of atomic compare exchange may fail spuriously. That is, the comparison may fail even if the expected value matches the value in the atomic memory location. The weak version should generally be used when the cost of failure is low and be put into a loop until it succeeds. The cost of the strong version is much higher than the weak version on some systems and should be used for single tests.
Activity
dneto0 commentedon Jan 21, 2021
In #978 I argued that WGSL should have volatile atomics. I presume the proposed builtins would all be the volatile versions.
alan-baker commentedon Jan 21, 2021
It should likely depend on whether WGSL adopts
volatileas a modifier or not.dneto0 commentedon Jan 21, 2021
I mean that WGSL should specify their behavior as if they are volatile, whether or not the keyword exists: each apparent access specified in the program source should be executed, i.e. you can't optimize them away assuming some constraints on the scheduling of other threads. (or something like that. See the issue).
alan-baker commentedon Jan 21, 2021
I understand, I was saying if there is a volatile modifier then you don't have to say that atomics are inherently volatile, but if that modifier isn't added I agree it's best to just say they are volatile.
dneto0 commentedon Jan 22, 2021
It's too bad atomic-load and atomic-store are not supported.
How about emulating them on HLSL with atomic-or with a 0 value?
dneto0 commentedon Jan 22, 2021
Or rather:
litherum commentedon Feb 16, 2021
We had a discussion with the Metal team about whether atomic types are really necessary to expose in the language, or whether we could have WGSL work like HLSL and just make the implementation of the atomic functions in MSL internally reinterpret_cast their parameters to their corresponding atomic types. However, the Metal team says this would be no good very bad, and an atomic type really needs to be part of WGSL (the reinterpret_cast trick would not work in general).
litherum commentedon Feb 16, 2021
Presumably atomic operations shouldn’t need to be inside uniform control flow.
litherum commentedon Feb 16, 2021
A few thoughts:
atomic a;
…
atomicAdd(myVar.a, 1);
I think we’re still in discussions about how pointers will work, so that pointers discussion needs to be concluded before these signatures are finalized.
litherum commentedon Feb 16, 2021
@alan-baker this is a wonderfully researched and presented proposal! Thank you so much!!!
alan-baker commentedon Feb 16, 2021
Thanks, I've fixed the signatures.
I agree. The most important thing in my opinion is facilitating the syntax in the example for programmer ease-of-use. So whether the signature retains the
ptror just usesatomicdirectly can be resolved with the pointer discussion.The allow_uav_condition attribute is also only necessary in compute shaders according to the HLSL documentation. Unfortunately, I don't see an alternative to program analysis unless we want to expose a similar attribute in WGSL.
pow2clk commentedon Feb 16, 2021
The
[allow_uav_condition]requirement is somewhat contorted by the documentation linked above. for loop docs give a more nuanced picture.At any rate, this is a relic of FXC that is not relevant to DXC. Additionally, the only way of generating HLSL from WGSL that I'm aware of will make use of SPIRV-Cross, which doesn't seem to support this. So if this is a requirement, it will be a requirement for spirv-cross.
litherum commentedon Feb 16, 2021
I believe both Google and Mozilla have publicly stated at the beginning of the last F2F that their long term plans include compiling directly from WGSL to HLSL/MSL without going through SPIR-V. (Please correct me if I am mistaken.)
dj2 commentedon Feb 16, 2021
Tint has backends for both MSL and HLSL now, they are not enabled in Chromium yet, but will be used for Origin Trials.
pow2clk commentedon Feb 17, 2021
I've completed an invigorating code archeology expedition which has revealed that at least since 2016 (and possibly much sooner), FXC has also ignored the
allow_uav_conditionattribute. The documentation forInterlockedCompareExchangeis clearly inaccurate and also out of date. We'll address that.Regardless of how the HLSL is generated, if the FXC version used to compile it to DXBC is at least more recent then 2016, the attribute can be left out entirely. If earlier versions of FXC need to be used, then we can look closer at exactly when this change was made.
Add atomics to WGSL
kdashg commentedon Mar 5, 2021
WGSL meeting minutes 2021-02-16
DN: Listed in the for statement as a qualifier on something. Loop shader termination condition to be based off of …. (see link)
AB: Workgroup and storage.
MM: In metal you can, you can call functions on them
Add atomics to WGSL
dneto0 commentedon Mar 13, 2021
My summary of the issues from the 02-16 meeting:
dneto0 commentedon Mar 31, 2021
In prep for next week's meeting, "atomics in a pointer world", I think this is the relevant example:
Let's assume we have #1569.
Example in this context:
Should we have:
Usage:
let newValue = atomicAdd(&(buf.a), 1);or this, if we also allow function parameters with ref type, at least for built-in functions.
Usage
let newValue = atomicAdd(buf.a, 1);kvark commentedon Apr 1, 2021
When you have
refin the type system, it's tempting to start exposing it out like this...But there is no need here, really. @litherum pointed out that MSL atomics work on a pointer argument, so we should just follow, instead of trying to invent new grounds here.
kdashg commentedon Apr 7, 2021
WGSL meeting minutes 2021-04-06
dneto0 commentedon Apr 7, 2021
The resolution was that the atomic built-ins act on a pointer-to-the-atomic.
Add atomics to WGSL
Add atomics to WGSL (#1448)
wgsl: Stub tests for the dpdy builtin (gpuweb#1360)