Skip to content

support volatile atomics #978

Open
Open

Description

@dneto0

If WGSL supports atomic operations, it should support volatile atomic operations.

Since we haven't discussed atomic operations yet, it's too early to suggest syntax. We might want to use a 'volatile' keyword or attribute, or embed it in a built-in function name.

Justification:

In C++ much of the 'volatile' functionality has rightly moved into atomics. See P1152R0 Deprecating volatile

However it still has a reason to exist when used with atomics, and that use carries over into the GPU world.

Consider the following C++ program as the action of a single thread:

#include <atomic>

std::atomic<int> flag;
int main() {
  int sub_counter = 0;
  flag.store(0, std::memory_order_relaxed);
  // Wait for another thread to modify 'flag'.
  while(flag.load(std::memory_order_relaxed) == 0) {
    sub_counter++;
  }
  return sub_counter & 1;
}

The implementation is justified in the following transformation:

  • First, constrain and therefore assume a restricted set of schedules of the operations in this thread and the other signaling thread (that we don't see)
  • Therefore, assume that the atomic loads in this thread are scheduled "before" the writes in the other thread. Without progress fairness guarantees, this is justified
  • Therefore, all the atomic loads can be combined, and then moved before the loop.

So we could end up compiling into the equivalent of the following:

#include <atomic>

std::atomic<int> flag;
int main() {
  int sub_counter = 0;
  flag.store(0, std::memory_order_relaxed);
  const int loaded = flag.load(std::memory_order_relaxed);

  while(loaded == 0) {
    sub_counter++;
  }
  return sub_counter & 1;
}

This is probably not what the programmer wanted.

To the fix this, the programmer should use 'volatile' to indicate that the atomic loads on 'flag' are not to be combined with other memory operations.

Currently this is denoted in C++ by marking 'flag' itself with 'volatile'. If there were no legacy here we'd make the operation volatile, not the object itself. In SPIR-V with the Vulkan memory model, use the Volatile memory semantics bit (on atomic operations and fences/memory barriers), Volatile memory operand (on Load and Store), and VolatileTexel (on image operations).

More background:

(Actually, the signed integer overflow on sub_counter in the example is undefined behaviour and the compiler could delete the entire program or your hard drive. Changing sub_counter to unsigned fixes that issue, I think.)

Activity

  1. added
    wgsl
    on Aug 5, 2020
  2. added this to the MVP milestone on Aug 5, 2020
  3. kdashg commented on Feb 8, 2022

    @kdashg
    Contributor

    @dneto0 This looks "done" or "post-v1" to me. What do you think?

  4. litherum commented on May 10, 2022

    @litherum
    Contributor

    We agree with the ideas in the No Sane Compiler Would Optimize Atomics talk, and feel like all atomics should be treated as volatile.

  5. dneto0 commented on May 10, 2022

    @dneto0
    ContributorAuthor

    In the discussion for the original atomics proposal ( #1360 ), consensus was that the builtins should behave as atomic.

    But right now I don't see how that's said in the spec.

    I also can't think of what it would say, short of introducing an execution model.
    I looked briefly at C++14 and it says that one can assume that a thread will eventually do one of a handful of things in the abstract machine, including accessing a volatile object. I think that's pulling on a thread we don't want to pull on right now.

    Suggest pushing this to post-v1.

  6. kdashg commented on May 11, 2022

    @kdashg
    Contributor
    WGSL meeting minutes 2022-05-10
    • DN: Looked again today, in discussion for atomics agreed that the atomic ops should act as though they’re volatile but decinded ot not add volatile. Don’t see that intent in text in spec but don’t know what to add to spec to codify that in a satisfying way without detailing execution model. Want to take KG suggestion and post-v1. Have understanding when doing translation that if underlying thing can have volatile that’s what we mean
    • AB: In my opinion implemetnation detail.
    • KG: As long as no-one defects on implementation. MM opinion is no sane compile would defect?
    • AB: Think talk was opposite conclusion and they would optimize atomics.
    • DN: At best add conformance test with while loop and atomic load and say that thing will terminate in 2 threads
    • AB: Worry about requiring forward progress. Don’t know if you can loop and trust result and separate thing to get into that model
    • DN: Push post-v1 at best or close.
    • KG: Anyone want to do more here? Or are we satisfied. We can always re-open later.
    • DN: Other thing, have no idea hwo we’d enforce this in HLSL for example. Would have to look, but guess is not necessarily stable. Don’t want to promise in spec and then test and can’t follow through.
    • KG: Would be OK, then would realize we’d fallen short and can adjust language. Don’t have to skip straight to right answer. Would like to record intent, even if no text in spec, intent is behaves as if volatile and we have forward progress. Having intent and can lookup issue later and we fall afoul we can lookup and have on record.
  7. modified the milestones: V1.0, post-V1 on May 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    wgsl

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions