You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
shader programming model: permitted memory orders on control barriers #232
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 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).
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.
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.
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....?
@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.
Activity
RobinMorisset commentedon Mar 8, 2019
I agree that we must offer ReleaseAcquire, and not offer SequentiallyConsistent.
There are a few different choices beyond that:
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:
dneto0 commentedon Apr 5, 2019
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.dneto0 commentedon Apr 5, 2019
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.
grorg commentedon Apr 8, 2019
Discussed at the 8 April 2019 meeting
dneto0 commentedon Apr 16, 2019
Decision from 2019-04-08 is that control barriers use AcquireRelease ordering.
ControlBarrier and MemoryBarrier use AcquireRelease
ControlBarrier and MemoryBarrier use AcquireRelease
ControlBarrier and MemoryBarrier use AcquireRelease (#24)
dneto0 commentedon Apr 17, 2019
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():
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....?
dneto0 commentedon May 10, 2019
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
dneto0 commentedon May 10, 2019
Correction, I think it's read-write images that introduce the need for image fences.
dneto0 commentedon May 16, 2019
Discussed in WebGPU F2F 2019-05-16:
devshgraphicsprogramming commentedon Aug 7, 2019
@dneto0 from my reading of the "Memory Model" Appendix of the Vulkan specification it was apparent to me that:
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
memoryBarrierof any kind (with any suffix) not immediately after abarrier, like sowould 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.
grorg commentedon Oct 19, 2020
Discussed at the 2020-10-19 Virtual F2F.
Kangz commentedon Feb 2, 2022
Barriers have been added to WGSL partly through this investigation. Tentatively closing.