Skip to content

shader programming model: permitted memory orders on control barriers #232

Closed

Description

@dneto0

Discussed with @RobinMorisset at San Diego F2F

Proposal: Control barriers should use AcquireRelease memory semantics.

Rationale:

  • MSL control barriers can't be Release-only or Acquire-only
  • Vulkan does not have SequentiallyConsistent

Activity

  1. RobinMorisset commented on Mar 8, 2019

    @RobinMorisset
    Contributor

    I agree that we must offer ReleaseAcquire, and not offer SequentiallyConsistent.

    There are a few different choices beyond that:

    • We could also offer "pure" control barriers, with no memory model implications. My understanding is that they are supported both by MSL through threadgroup_barrier(mem_none) and by Vulkan through OpControlBarrier(Workgroup, ignored, None). They also match barrier() from GLSL.
    • Even though they are not directly supported by MSL, we could in theory have Release or Acquire barriers, and treat them as ReleaseAcquire when compiling to MSL. However Myles told me that HLSL and GLSL also only have ReleaseAcquire, so it is unlikely that many shaders in the wild use pure release or acquire fences.
    • Similarily, do we want only control barriers, or do we also want "pure" memory fences? They would have to be in uniform control-flow anyway because they would be compiled to the same threadgroup_barrier on MSL, but pure memory fences (OpMemoryBarrier in SPIRV; GroupMemoryBarrier, DeviceMemoryBarrier and AllMemoryBarrier in HLSL; memoryBarrier, memoryBarrierBuffer and memoryBarrierShared) could be cheaper on other platforms.

    It would be fairly easy to offer all of these with a fairly fine degree of granularity, but I don't know how useful they would be. For example, do shaders often use pure control or memory barriers, or do they always combine them?

    There are several other dimensions to the design of barriers. It is a bit off-topic for the title of this issue, but I'd like to keep the discussion of everything related to barriers in one place. OpControlBarrier in SPIRV/Vulkan varies along 5 dimensions:

    • the scope of its control-barrier effect. Because MSL only support threadgroup_barrier and subgroup_barrier (and we are currently proposing not supporting subgroups in the MVP of webgpu), I would recommend only allowing threadgroup (Workgroup in Vulkan)
    • the scope of its memory-barrier effect (if the semantics is not None). I think it should be linked to the storage class being affected (see below), with only two possibilities: threadgroup/Workgroup and device/QueueFamilyKHR (I originally thought Device was the right Vulkan choice, but https://github.com/Microsoft/DirectXShaderCompiler/pull/1738/files and our email discussion suggest that QueueFamilyKHR may be more appropriate).
    • the memory order: Release/Acquire/AcquireRelease/SequentiallyConsistent. see discussion above.
    • the storage class it affects: any number among UniformMemory / SubgroupMemory / WorkgroupMemory / CrossWorkgroupMemory / AtomicCounterMemory / ImageMemory. The equivalent in MSL is the choice between mem_none, mem_threadgroup, mem_device and (mem_threadgroup|mem_device). Similarily, HLSL has the distinction between GroupMemoryBarrier/DeviceMemoryBarrier/AllMemoryBarrier. I recommend providing the same choices as HLSL/MSL as they seem sufficient and fairly standard. threadgroup/Group clearly matches WorkgroupMemory. From https://github.com/Microsoft/DirectXShaderCompiler/pull/1738/files, it seems that Image|Uniform is the best match for device.
    • finally it can be marked MakeAvailableKHR (if Release) and/or MakeVisibleKHR (if Acquire). How we deal with this will depend on how we support available/visible more generally. I hope that either all release/acquire barriers will be MakeAvailableKHR/MakeVisibleKHR or none will be (to spare shader authors from having to learn about this quite subtle concept, that does not seem to have a direct equivalent on other platforms).
  2. dneto0 commented on Apr 5, 2019

    @dneto0
    ContributorAuthor

    Small correction: barrier() in GLSL means different things in tessellation control and compute shaders. Only compute shaders are relevant to WebGPU. In a compute shader, barrier is actually an AcquireRelease on workgroup memory, with workgroup execution and memory scope. It's explained better here: https://github.com/KhronosGroup/GLSL/blob/master/extensions/khr/GL_KHR_memory_scope_semantics.txt#L447
    That references the Vulkan memory model extension to GLSL, but plain barrier() is meant to have the same semantics for regular GLSL.

  3. dneto0 commented on Apr 5, 2019

    @dneto0
    ContributorAuthor

    I think Release-only and Acquire-only barriers are interesting, but not widely used.

    Generally, I would say that anything beyond basics is not going to have good portable support. A major motivation for defining the Vulkan memory model was to put memory semantics on a solid footing, properly describing it and testing it. Even so, the Vulkan memory model is a provisional spec, not required to be supported by anything yet.

    My guess is that we'd have to stick to fairly vanilla things: GLSL compute shader barrier() with AcquireRelease, GLSL memory barrier (fence) with AcquireRelease.

    I agree with @RobinMorisset's other recommendations. We should make the possible choices explicit, e.g. with a table somewhere.

  4. grorg commented on Apr 8, 2019

    @grorg
    Contributor

    Discussed at the 8 April 2019 meeting

  5. dneto0 commented on Apr 16, 2019

    @dneto0
    ContributorAuthor

    Decision from 2019-04-08 is that control barriers use AcquireRelease ordering.

  6. dneto0 commented on Apr 17, 2019

    @dneto0
    ContributorAuthor

    Reviewing the discussion from 2019-04-08 we didn't really discuss whether to have a memory fence that is not a control barrier.

    First kind:

    • GLSL has memoryBarrier():

        void memoryBarrier()
        // equivalent to:
        // memoryBarrier(gl_ScopeQueueFamily,
        //               gl_StorageSemanticsBuffer |
        //               gl_StorageSemanticsShared |
        //               gl_StorageSemanticsImage,
        //               gl_SemanticsAcquireRelease)
      
    • MSL can emulate that with a control barrier, as long as it's in uniform control flow.

    • HLSL shader model 5 has AllMemoryBarrier() which is the same as GLSL memoryBarrier()

    • OpenCL C has mem_fence which is AcquireRelease, read_mem_fence "only orders loads", write_mem_fence "only orders writes".

    The question to me is whether to have this at all in WebGPU MVP. I lean against having it because it people will learn that this fence will need to be in uniform control flow. Then it will be hard to shake that impression if/when MSL gets an upgraded memory fence, and then we can relax the rule. If it really is just an emulation to control-barrier then make WebGPU coders write that in the first place. When the new thing is introduced then add it as purely new functionality without confusion.

    Second kind: image fence

    OpenCL 2.x has a general atomic_work_item_fence which can be used on images (separately from shared and device storage). In the image case it orders accesses within one invocation to an image.

    MSL has the same image functionality in the fence() texture operation.

    GLSL 4.4 (maybe earlier) has memoryBarrierImage()

    ESSL 3.1 has memoryBarrierImage()

    I don't think HLSL has this specifically. It can be emulated with DeviceMemoryBarrier or AllMemoryBarrier .(See the mappings for the SPIR-V backend on DXC: https://github.com/Microsoft/DirectXShaderCompiler/blob/master/docs/SPIR-V.rst#synchronization-intrinsics )

    These would map to SPIR-V OpMemoryBarrier with Invocation scope and Image memory semantics (it's Relaxed ordering)

    I don't strongly I feel about this, but I propose that WebGPU MVP not have this as a builtin. The impact is really whether images can be read-write. As an example, OpenCL only got read-write images in OpenCL 2.0, which is pretty bells-and-whistles....?

  7. dneto0 commented on May 10, 2019

    @dneto0
    ContributorAuthor

    Regarding image fence: I think we only need this if writable images are supported.

    The draft of WebGPU doesn't mention a storage-image GPUBindingType, but that enum does have TODOs. See https://gpuweb.github.io/gpuweb/spec/#binding

  8. dneto0 commented on May 10, 2019

    @dneto0
    ContributorAuthor

    Correction, I think it's read-write images that introduce the need for image fences.

  9. dneto0 commented on May 16, 2019

    @dneto0
    ContributorAuthor

    Discussed in WebGPU F2F 2019-05-16:

    • Decided not to add pure memory barrier for MVP
    • Since read-write images will be included, we need the image fence with invocation scope.
  10. devshgraphicsprogramming commented on Aug 7, 2019

    @devshgraphicsprogramming

    @dneto0 from my reading of the "Memory Model" Appendix of the Vulkan specification it was apparent to me that:

    • memory barrier only ensures visibility and availability of memory writes for reads
    • memory barrier is only there to create a memory dependency
    • memory dependency requires an execution dependency (execution barrier)

    So for a Thread A Write to location M visible to thread B read from location M one always needs an execution barrier.

    The only reason to place a memoryBarrier of any kind (with any suffix) not immediately after a barrier, like so

    barrier();
    memoryBarrier();

    would be to allow a "split barrier". Basically you ensure that writes occur before an execution barrier, but don't care about them propagating to caches and being visible for reading until later, or if you only need one cache type to be primed (Image Barrier) or effects only from a certain subset of writers to be seen (groupMemoryBarrier).

    Basically speaking, an execution + widest most encompassing in-shader memory barrier with acquire-release semantics can replace separate execution and memory barriers. However at a small cost to performance (waay more sync and less flexibility with memory loads).

    AFAIK, no Vulkan device actually supports the whole range of memory storage/load semantics on its barriers and atomic ops, I think the VK spec even requires that certain semantics are not present in accepted SPIR-V at all. I believe that to be the case because SPIR-V is written for representing OpenCL as well, so there are more barrier types, C++11 like memory semantics than are actually available for use in graphics API shaders.

  11. added
    wgsl
    on Oct 12, 2020
  12. grorg commented on Oct 19, 2020

    @grorg
    Contributor

    Discussed at the 2020-10-19 Virtual F2F.

  13. Kangz commented on Feb 2, 2022

    @Kangz
    Contributor

    Barriers have been added to WGSL partly through this investigation. Tentatively closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

programming modelwgsl

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions