[llvm] [AMDGPU] A Vulkan-style memory model weaker than the LLVM model (PR #191246)

Sameer Sahasrabuddhe via llvm-commits llvm-commits at lists.llvm.org
Thu May 21 04:01:49 PDT 2026


https://github.com/ssahasra updated https://github.com/llvm/llvm-project/pull/191246

>From b1e89f758c74831d1d6eb908a65f45b6331c62b4 Mon Sep 17 00:00:00 2001
From: Sameer Sahasrabuddhe <sameer.sahasrabuddhe at amd.com>
Date: Thu, 9 Apr 2026 19:47:41 +0530
Subject: [PATCH 1/3] [AMDGPU] A Vulkan-style memory model weaker than the LLVM
 model

Add a new AMDGPU memory model specification that is weaker than the LLVM memory
model using Vulkan-style availability/visibility semantics and scoped
operations. The model allows more efficient implementations while maintaining a
safe-by-default mapping to the standard LLVM model.
---
 llvm/docs/AMDGPUMemoryModel.rst | 377 ++++++++++++++++++++++++++++++++
 llvm/docs/UserGuides.rst        |   5 +
 2 files changed, 382 insertions(+)
 create mode 100644 llvm/docs/AMDGPUMemoryModel.rst

diff --git a/llvm/docs/AMDGPUMemoryModel.rst b/llvm/docs/AMDGPUMemoryModel.rst
new file mode 100644
index 0000000000000..2d1eb03fbd98a
--- /dev/null
+++ b/llvm/docs/AMDGPUMemoryModel.rst
@@ -0,0 +1,377 @@
+.. _amdgpu-memmodel:
+
+=================================
+ AMDGPU Memory Model
+=================================
+
+.. contents::
+   :local:
+
+Introduction
+============
+
+The :ref:`LLVM memory model<memmodel>` provides broad guarantees that are
+sufficient to implement inter-thread communication via memory. But in most
+communication patterns, not all memory accesses performed by a thread need to be
+exposed to other threads. Even when they do need to be exposed, not all threads
+may need to observe these memory accesses. This document describes the *AMDGPU
+memory model* that allows the user to control how the side-effects of memory
+accesses are propagated across threads. The programmer expresses this using
+**new intrinsics and metadata** as described below, and the implementation can
+then choose a more efficient mechanism to complete them, such as the cache
+policy bits in an AMDGPU device.
+
+The AMDGPU memory model allows executions that are not allowed by the LLVM
+memory model. At the same time, a simple mapping can be used to implement these
+new intrinsics and metadata using operations defined in the default LLVM memory
+model. Thus, **there exists a safe-by-default implementation** that produces
+executions that are valid in both models.
+
+Terminology
+===========
+
+Memory Accesses
+  Operations that read or write locations in memory are termed as *memory
+  accesses*. Typical examples are ``load``, ``store`` and atomic instructions,
+  as well as many intrinsics.
+
+Synchronization Operations
+  Synchronization operations control how the side-effects of memory accesses are
+  propagated in the system. Typical examples are atomic operations (including
+  fences) with at least ``release`` or ``acquire`` ordering.
+
+Availability and Visibility
+===========================
+
+The AMDGPU memory model is built on top of the :ref:`happens-before<memmodel>`
+order defined by the LLVM memory model. But when one of the new intrinsics or
+metadata is used, **happens-before by itself is not sufficient** to describe its
+observable effects. Instead, the AMDGPU model uses *availability* and
+*visibility* to describe how the side-effects of these operations propagate to
+other threads.
+
+Availability determines how *far* the side-effects of a write have been
+forwarded in the system relative to that write. Visibility determines how
+*close* the side-effects of the same write have reached relative to an observer
+operation (typically a read).
+
+The AMDGPU memory model *does not change the structure of happens-before*, but
+changes the rules that determine how operations may observe the side-effects of
+other operations that happen before them.
+
+Consider a write W that ``happens-before`` a read R to the same address:
+
+- R can potentially observe the side-effects of W **only if W is visible** to R.
+- W can potentially be visible to R **only if W is first made available** to R.
+
+The instructions used in the default LLVM memory model automatically satisfy
+these necessary conditions, and hence they can be explained using the rules from
+either memory model. But the new intrinsics and metadata *opt out* of the LLVM
+memory model, and can only be explained using the AMDGPU memory model.
+
+store-available
+---------------
+
+.. code-block:: llvm
+
+   @llvm.amdgcn.av.global.store.b128(ptr, value, scope)
+   store atomic [syncscope("<target-scope>")]
+   atomicrmw    [syncscope("<target-scope>")]
+   cmpxchg      [syncscope("<target-scope>")]
+
+The ``@llvm.amdgcn.av.global.store.b128`` intrinsic performs a non-atomic
+store-available operation on ``ptr`` whose side-effects are made available to
+:ref:`its instance<amdgpu-scopes>` of ``scope``.
+
+An atomic operation that results in a store operation is a store-available
+operation with scope ``syncscope``.
+
+load-visible
+------------
+
+.. code-block:: llvm
+
+   @llvm.amdgcn.av.global.load.b128(ptr, scope)
+   load atomic  [syncscope("<target-scope>")]
+   atomicrmw    [syncscope("<target-scope>")]
+   cmpxchg      [syncscope("<target-scope>")]
+
+The ``@llvm.amdgcn.av.global.load.b128`` intrinsic performs a non-atomic
+load-visible operation which ensures the visibility of all side-effects on
+``ptr`` available at :ref:`its instance<amdgpu-scopes>` of ``scope``.
+
+An atomic operation that results in a read operation is a load-visible operation
+with scope ``syncscope``.
+
+.. note::
+
+   Metadata cannot be used to model this using ordinary load/store operations,
+   because the scope is necessary for correctness. In a hypothetical operation
+   like this:
+
+   .. code-block:: llvm
+
+      store ptr, data !amdgcn-av:scope
+
+   If the metadata is dropped or ignored, there is no guarantee that the store
+   will become available at the intended scope. In implementation terms, the
+   store may be completed at a nearer cache than the one required for that
+   scope. A corresponding load-visible that does not access the same near cache
+   will fail to observe this store.
+
+MakeAvailable and MakeVisible
+-----------------------------
+
+.. code-block:: llvm
+
+   store atomic [syncscope("<target-scope>")] <ordering> [!amdgcn-av-none]
+   load atomic  [syncscope("<target-scope>")] <ordering> [!amdgcn-av-none]
+   atomicrmw    [syncscope("<target-scope>")] <ordering> [!amdgcn-av-none]
+   cmpxchg      [syncscope("<target-scope>")] <ordering> [!amdgcn-av-none]
+   fence        [syncscope("<target-scope>")] <ordering> [!amdgcn-av-none]
+
+A synchronization operation with at least ``release`` ordering is a
+``MakeAvailable`` operation with scope ``syncscope``, if it does not have the
+``!amdgcn-av-none`` metadata.
+
+A synchronization operation with at least ``acquire`` ordering is a
+``MakeVisible`` operation with scope ``syncscope``, if it does not have the
+``!amdgcn-av-none`` metadata.
+
+These operations include MakeVisible and MakeAvailable operations by default.
+The presence of this metadata removes this ability and essentially creates
+"non-av" ordering operations.
+
+For an atomic operation which itself accesses memory (e.g., ``store atomic``
+or ``load atomic``), the metadata does not affect the availability or the
+visibility of the access performed by the operation itself. It only affects
+the ordering of other memory accesses.
+
+.. code-block:: llvm
+
+   ; This includes a MakeAvailable operation.
+   store atomic [syncscope("<target-scope>")] <ordering>
+
+   ; This **does not** include a MakeAvailable operation.
+   store atomic [syncscope("<target-scope>")] <ordering> !amdgcn-av-none
+
+.. _amdgpu-scopes:
+
+Scopes
+======
+
+A *scope* describes sets of memory accesses and synchronization operations in a
+multi-threaded execution environment. Each such set is called an *instance* of
+that scope, or a *scope instance* for short. Each memory access or
+synchronization operation belongs to at most one instance of every scope defined
+by the target.
+
+A "subscope" is a scope that is a subset of another scope. A "subscope instance"
+is a scope instance that is a subset of another scope instance.
+
+When an operation X specifies a scope S, it indicates the instance of S that
+contains X. This scope instance is also termed as *X's instance of scope S* or
+just "X's scope instance", when S is implied by the context.
+
+When an operation does not specify a scope, it indicates the singleton instance
+of the LLVM system-wide scope defined below.
+
+**Inclusive Scopes**: Two operations X and Y are said to have *inclusive scopes*
+if the scope instance of each operation contains the other operation. In that
+case, the *common scope instance* S' of X and Y is the intersection of their
+scope instances. The scope corresponding to S' is also termed as the *common
+scope* of X and Y. [Informational note -- Since scopes are arranged in a linear
+hierarchy, clearly S' is the smaller of the two scope instances.]
+
+LLVM scopes
+-----------
+
+LLVM defines the following scopes:
+
+- A "singlethread" scope. Each thread has a corresponding "singlethread" scope
+  instance that contains the memory accesses and synchronization operations
+  performed by that thread.
+- An anonymous system-wide scope with a single instance that contains the memory
+  accesses and synchronization operations performed by all threads. This is
+  typically called "system" scope in languages and targets supported by
+  Clang/LLVM.
+
+AMDGPU scopes
+-------------
+
+The AMDGPU backend defines the following scopes:
+
+- "system" (the anonymous system-wide scope in LLVM)
+- "agent" scope
+- "cluster" scope
+- "workgroup" scope
+- "wavefront" scope
+- "singlethread" scope (same as LLVM)
+
+These are arranged from largest scope ("system") to smallest scope
+("singlethread"). Every scope instance (except the singleton "system" scope
+instance) is included in a scope instance at the level above it.
+
+Ordering
+========
+
+.. note::
+
+   **TODO:** These ordering operations affect all address spaces. We need to
+   eventually make that a parameter similar to the storage class parameter on
+   operations and orders in Vulkan.
+
+An operation X is an *availability operation* on a write W if:
+
+- X is W itself, and W is a store-available operation, or,
+- X is a MakeAvailable operation that follows W in program order, or,
+- X is a MakeAvailable operation whose scope instance includes W, and there is
+  an availability operation Z on W such that:
+
+  - Z happens-before X, and,
+  - Z's scope instance includes X.
+
+Then X makes W available in its own scope instance S and every subscope instance
+of S that also includes W.
+
+An operation Y is a *visibility operation* on a write W if:
+
+- Y is a load-visible operation to the same address, or a MakeVisible operation,
+  and,
+- There exists an *availability* or *visibility* operation X on write W such
+  that:
+
+  - X happens-before Y, and,
+  - X and Y specify inclusive scopes.
+
+Then Y makes W visible in the common scope instance S of X and Y, and every
+subscope instance of S that includes Y.
+
+Location Order
+--------------
+
+A write W is location-ordered before an access Y to the same address if W is
+program-ordered before Y.
+
+A write W is location-ordered before a write W1 to the same address if there
+exists an availability operation Z on W such that:
+
+- Z happens-before W1, and,
+- W1 is included in Z's scope instance.
+
+A write W is location-ordered before a read R to the same address if there
+exists a visibility operation Z on write W such that:
+
+- Z is R itself, or,
+- Z precedes R in program order.
+
+The AMDGPU memory model overrides the :ref:`definition of each
+byte<memmodel>` in the LLVM memory model as follows.
+
+Every (defined) read operation R reads a series of bytes written by (defined)
+write operations. Each initialized global is assumed to have an initial write
+operation which is atomic and defined to be location-ordered before any other
+read or write to that same location.
+
+For each byte of a read R, R may see any write to the same byte, except:
+
+- If a write W1 is location-ordered before a write W2, and W2 is
+  location-ordered before a read R, then R may not see W1.
+- If a read R happens-before a write W3, then R may not see W3.
+
+The value returned by R is then defined as follows:
+
+- If no write is location-ordered before a read R, then R returns ``undef``.
+- Otherwise if the set consisting of R and all writes that R may see contains
+  only atomic operations with inclusive scopes, then R returns the value written
+  by one of those writes.
+- Otherwise, if R may see some write that is not location-ordered before R,
+  then R returns ``undef``.
+- Otherwise, if R may see exactly one write W, then R returns the value written
+  by W.
+- Otherwise, R returns ``undef``.
+
+Properties
+----------
+
+[This section is informational.]
+
+The following properties follow from the definitions above:
+
+1. **Happens-before is necessary for location-order.** A write W is
+   location-ordered before a read R only if W happens-before R. This follows
+   from the definition of availability and visibility operations, which always
+   require a happens-before link with the preceding operation in the chain.
+
+2. **A write cannot be made available in a scope that does not contain it.** The
+   definition of an availability operation X requires that X's scope instance
+   includes W as a precondition. Since every scope instance that includes X also
+   includes W, availability cannot reach a scope instance that excludes W. In
+   other words, availability can only "expand outwards" into progressively
+   larger scopes.
+
+3. **Visibility is bounded by availability.** When a write is available in a
+   scope instance, it can be made visible in that scope instance by a visibility
+   operation with the corresponding scope. Subsequent MakeVisible operations
+   make that write visible into narrower scope instances towards the observer.
+
+4. **A write can be made visible in a scope instance that does not contain it.**
+   The definition of a *visibility operation* anchors scope instances to the
+   observer (Y), not to the original write. The only precondition is that the
+   write must already be visible or available in the scope instance of the
+   visibility operation.
+
+5. **Availability and visibility chains.** For a write W to be visible to a read
+   R anywhere in the system, the sufficient condition is a chain of
+   happens-before edges that include availability and visibility operations with
+   inclusive scopes. It is not necessary that W and R themselves have inclusive
+   scopes. Each link in the availability and visibility definitions only checks
+   the immediate predecessor, so intermediate operations can bridge scope gaps
+   that the endpoints cannot satisfy directly. Such a chain passes through at
+   least one availability operation and at least one visibility operation with
+   inclusive scopes, such that their common scope includes both W and R.
+
+.. _amdgcn-av-vulkan:
+
+The Vulkan Memory Model
+=======================
+
+The AMDGPU memory model draws heavily on the Vulkan memory model. In
+particular, the following instructions are equivalent.
+
+.. csv-table::
+   :header: "LLVM", "SPIRV", "Available/Visible Semantics"
+   :widths: 25, 25, 50
+
+   "``load``", "OpLoad NonPrivatePointer", "\-"
+   "``load-visible``", "OpLoad NonPrivatePointer", "MakePointerVisible"
+   "``store``", "OpStore NonPrivatePointer", "\-"
+   "``store-available``", "OpStore NonPrivatePointer", "MakePointerAvailable"
+   "``load atomic``", "OpAtomicLoad", "MakePointerVisible. Also MakeVisible when order is at least ``acquire``."
+   "``load atomic !amdgcn-av-none``", "OpAtomicLoad", "MakePointerVisible"
+   "``store atomic``", "OpAtomicStore", "MakePointerAvailable. Also MakeAvailable when order is at least ``release``."
+   "``store atomic !amdgcn-av-none``", "OpAtomicStore", "MakePointerAvailable"
+   "``fence``", "OpMemoryBarrier", "MakeAvailable when order is at least ``release``, and MakeVisible when order is at least ``acquire``."
+   "``fence !amdgcn-av-none``", "OpMemoryBarrier", "\-"
+
+.. note::
+
+   The above table is representative only, and does not aim to be exhaustive. In
+   particular, it does not list composite atomic operations like ``rmw`` and
+   ``cmpxchg``. The ordering and semantics of these operations can be determined
+   by combining suitable rules such as:
+
+   - "MakeAvailable if the order is at least ``release``, and the operation
+     results in a store",
+   - "Only if ``!amdgcn-av-none`` is absent", etc.
+
+The AMDGPU memory model is a special case of the Vulkan memory model:
+
+a. LLVM fence/atomic ordering operations have ``MakeAvailable`` /
+   ``MakeVisible`` semantics by default, thus satisfying these chains. Hence the
+   LLVM memory model is a "strong" subset of the Vulkan memory model.
+b. The AMDGPU memory model described here makes it possible to opt-out of the
+   default availability and visibility semantics, and instead specify it on
+   select places including the new load-visible and store-available operations.
+   This expands the subset of the Vulkan memory model that can now be expressed
+   in LLVM IR.
diff --git a/llvm/docs/UserGuides.rst b/llvm/docs/UserGuides.rst
index f33ddcabc8d89..d8cfe5ea1c0ae 100644
--- a/llvm/docs/UserGuides.rst
+++ b/llvm/docs/UserGuides.rst
@@ -19,6 +19,7 @@ intermediate LLVM representation.
    AliasAnalysis
    AMDGPUUsage
    AMDGPUAsyncOperations
+   AMDGPUMemoryModel
    Benchmarking
    BigEndianNEON
    BuildingADistribution
@@ -292,6 +293,10 @@ Additional Topics
 :doc:`AMDGPUAsyncOperations`
    Builtins for invoking asynchronous data transfer operations in AMD GPUs.
 
+:doc:`AMDGPUMemoryModel`
+   This document describes *AMDGPU Memory Model* which overrides the :ref:`LLVM
+   memory model<memmodel>` when a program is compiled for the AMDGPU target.
+
 :doc:`AMDGPUDwarfExtensionsForHeterogeneousDebugging`
    This document describes DWARF extensions to support heterogeneous debugging
    for targets such as the AMDGPU backend.

>From 71b49183ea9c48e7493c3e7645eb4f8f0f6f96cf Mon Sep 17 00:00:00 2001
From: Sameer Sahasrabuddhe <sameer.sahasrabuddhe at amd.com>
Date: Fri, 15 May 2026 10:30:23 +0530
Subject: [PATCH 2/3] style, references, subscope, "system", visibility bugfix

---
 llvm/docs/AMDGPUMemoryModel.rst | 212 +++++++++++++++++---------------
 llvm/docs/AMDGPUUsage.rst       |  18 +++
 2 files changed, 134 insertions(+), 96 deletions(-)

diff --git a/llvm/docs/AMDGPUMemoryModel.rst b/llvm/docs/AMDGPUMemoryModel.rst
index 2d1eb03fbd98a..c8564bcc0a772 100644
--- a/llvm/docs/AMDGPUMemoryModel.rst
+++ b/llvm/docs/AMDGPUMemoryModel.rst
@@ -59,10 +59,10 @@ The AMDGPU memory model *does not change the structure of happens-before*, but
 changes the rules that determine how operations may observe the side-effects of
 other operations that happen before them.
 
-Consider a write W that ``happens-before`` a read R to the same address:
+Consider a write *W* that ``happens-before`` a read *R* to the same address:
 
-- R can potentially observe the side-effects of W **only if W is visible** to R.
-- W can potentially be visible to R **only if W is first made available** to R.
+- *R* can potentially observe the side-effects of *W* **only if W is visible** to *R*.
+- *W* can potentially be visible to *R* **only if W is first made available** to *R*.
 
 The instructions used in the default LLVM memory model automatically satisfy
 these necessary conditions, and hence they can be explained using the rules from
@@ -80,10 +80,10 @@ store-available
    cmpxchg      [syncscope("<target-scope>")]
 
 The ``@llvm.amdgcn.av.global.store.b128`` intrinsic performs a non-atomic
-store-available operation on ``ptr`` whose side-effects are made available to
+*store-available* operation on ``ptr`` whose side-effects are made available to
 :ref:`its instance<amdgpu-scopes>` of ``scope``.
 
-An atomic operation that results in a store operation is a store-available
+An atomic operation that results in a store operation is a *store-available*
 operation with scope ``syncscope``.
 
 load-visible
@@ -97,10 +97,10 @@ load-visible
    cmpxchg      [syncscope("<target-scope>")]
 
 The ``@llvm.amdgcn.av.global.load.b128`` intrinsic performs a non-atomic
-load-visible operation which ensures the visibility of all side-effects on
+*load-visible* operation which ensures the visibility of all side-effects on
 ``ptr`` available at :ref:`its instance<amdgpu-scopes>` of ``scope``.
 
-An atomic operation that results in a read operation is a load-visible operation
+An atomic operation that results in a read operation is a *load-visible* operation
 with scope ``syncscope``.
 
 .. note::
@@ -116,7 +116,7 @@ with scope ``syncscope``.
    If the metadata is dropped or ignored, there is no guarantee that the store
    will become available at the intended scope. In implementation terms, the
    store may be completed at a nearer cache than the one required for that
-   scope. A corresponding load-visible that does not access the same near cache
+   scope. A corresponding *load-visible* that does not access the same near cache
    will fail to observe this store.
 
 MakeAvailable and MakeVisible
@@ -138,9 +138,10 @@ A synchronization operation with at least ``acquire`` ordering is a
 ``MakeVisible`` operation with scope ``syncscope``, if it does not have the
 ``!amdgcn-av-none`` metadata.
 
-These operations include MakeVisible and MakeAvailable operations by default.
+These operations include ``MakeVisible`` and ``MakeAvailable`` operations by default.
 The presence of this metadata removes this ability and essentially creates
-"non-av" ordering operations.
+*non-av* ordering operations, i.e., ordering operations that do not establish
+availability or visibility.
 
 For an atomic operation which itself accesses memory (e.g., ``store atomic``
 or ``load atomic``), the metadata does not affect the availability or the
@@ -166,27 +167,29 @@ that scope, or a *scope instance* for short. Each memory access or
 synchronization operation belongs to at most one instance of every scope defined
 by the target.
 
-A "subscope" is a scope that is a subset of another scope. A "subscope instance"
-is a scope instance that is a subset of another scope instance.
+- A scope instance *S1* is a *subscope instance* of a scope instance *S2* if
+  *S1* is a subset of *S2*.
+- A scope *S1* is a *subscope* of a scope *S2* if every instance of *S1* is a
+  subset of some instance of *S2*.
 
-When an operation X specifies a scope S, it indicates the instance of S that
-contains X. This scope instance is also termed as *X's instance of scope S* or
-just "X's scope instance", when S is implied by the context.
+When an operation *X* specifies a scope *S*, it indicates the instance of *S* that
+contains *X*. This scope instance is also termed as *X's instance of scope S* or
+just "*X*'s scope instance", when *S* is implied by the context.
 
 When an operation does not specify a scope, it indicates the singleton instance
 of the LLVM system-wide scope defined below.
 
-**Inclusive Scopes**: Two operations X and Y are said to have *inclusive scopes*
+**Inclusive Scopes**: Two operations *X* and *Y* are said to have *inclusive scopes*
 if the scope instance of each operation contains the other operation. In that
-case, the *common scope instance* S' of X and Y is the intersection of their
-scope instances. The scope corresponding to S' is also termed as the *common
-scope* of X and Y. [Informational note -- Since scopes are arranged in a linear
-hierarchy, clearly S' is the smaller of the two scope instances.]
+case, the *common scope instance* *S'* of *X* and *Y* is the intersection of their
+scope instances. The scope corresponding to *S'* is also termed as the *common
+scope* of *X* and *Y*. [Informational note -- Since scopes are arranged in a linear
+hierarchy, clearly *S'* is the smaller of the two scope instances.]
 
 LLVM scopes
 -----------
 
-LLVM defines the following scopes:
+The LLVM Language Reference defines the following :ref:`scopes<syncscope>`:
 
 - A "singlethread" scope. Each thread has a corresponding "singlethread" scope
   instance that contains the memory accesses and synchronization operations
@@ -199,17 +202,17 @@ LLVM defines the following scopes:
 AMDGPU scopes
 -------------
 
-The AMDGPU backend defines the following scopes:
+The AMDGPU backend defines the following target-defined scopes:
 
-- "system" (the anonymous system-wide scope in LLVM)
+- The anonymous system-wide scope (same as LLVM)
 - "agent" scope
 - "cluster" scope
 - "workgroup" scope
 - "wavefront" scope
 - "singlethread" scope (same as LLVM)
 
-These are arranged from largest scope ("system") to smallest scope
-("singlethread"). Every scope instance (except the singleton "system" scope
+These are arranged from largest scope ("") to smallest scope
+("singlethread"). Every scope instance (except the singleton default scope
 instance) is included in a scope instance at the level above it.
 
 Ordering
@@ -221,115 +224,132 @@ Ordering
    eventually make that a parameter similar to the storage class parameter on
    operations and orders in Vulkan.
 
-An operation X is an *availability operation* on a write W if:
+Availability Operation
+----------------------
 
-- X is W itself, and W is a store-available operation, or,
-- X is a MakeAvailable operation that follows W in program order, or,
-- X is a MakeAvailable operation whose scope instance includes W, and there is
-  an availability operation Z on W such that:
+An operation *X* is an *availability operation* on a write *W* if one of the
+following holds:
 
-  - Z happens-before X, and,
-  - Z's scope instance includes X.
+- *X* is *W* itself, and *W* is a *store-available* operation, or,
+- *X* is a ``MakeAvailable`` operation that follows *W* in program order, or,
+- *X* is a ``MakeAvailable`` operation whose scope instance includes *W*, and there is
+  an availability operation *Z* on *W* such that:
 
-Then X makes W available in its own scope instance S and every subscope instance
-of S that also includes W.
+  - *Z* happens-before *X*, and,
+  - *Z*'s scope instance includes *X*.
 
-An operation Y is a *visibility operation* on a write W if:
+Then *X* makes *W* available in its own scope instance *S* and every subscope instance
+of *S* that also includes *W*.
 
-- Y is a load-visible operation to the same address, or a MakeVisible operation,
-  and,
-- There exists an *availability* or *visibility* operation X on write W such
-  that:
+Visibility Operation
+--------------------
 
-  - X happens-before Y, and,
-  - X and Y specify inclusive scopes.
+An operation *Y* is a *visibility operation* on a write *W* if *Y* is a
+*load-visible* operation to the same address, or a ``MakeVisible`` operation,
+and one of the following holds:
 
-Then Y makes W visible in the common scope instance S of X and Y, and every
-subscope instance of S that includes Y.
+- There exists an *availability* operation *X* on write *W* such that:
+
+  - *X* happens-before *Y*, and,
+  - *X* and *Y* specify inclusive scopes.
+
+  Then *Y* makes *W* visible in the common scope instance *S* of *X* and *Y*,
+  and every subscope instance of *S* that includes *Y*.
+
+- There exists a *visibility* operation *X* on write *W* such that:
+
+  - *X* happens-before *Y*, and,
+  - *X* makes *W* visible in a scope instance *S1* that includes *Y*, and,
+  - *X* is included in the scope instance *S2* of *Y*.
+
+  Then *Y* makes *W* visible in the intersection of *S1* and *S2*, and every
+  subscope instance of *S2* that includes *Y*.
 
 Location Order
 --------------
 
-A write W is location-ordered before an access Y to the same address if W is
-program-ordered before Y.
+A write *W* is *location-ordered* before an access *Y* to the same address if *W* is
+program-ordered before *Y*.
 
-A write W is location-ordered before a write W1 to the same address if there
-exists an availability operation Z on W such that:
+A write *W* is *location-ordered* before a write *W1* to the same address if there
+exists an availability operation *Z* on *W* such that:
 
-- Z happens-before W1, and,
-- W1 is included in Z's scope instance.
+- *Z* happens-before *W1*, and,
+- *W1* is included in *Z*'s scope instance.
 
-A write W is location-ordered before a read R to the same address if there
-exists a visibility operation Z on write W such that:
+A write *W* is *location-ordered* before a read *R* to the same address if there
+exists a visibility operation *Z* on write *W* such that:
 
-- Z is R itself, or,
-- Z precedes R in program order.
+- *Z* is *R* itself, or,
+- *Z* precedes *R* in program order.
 
-The AMDGPU memory model overrides the :ref:`definition of each
-byte<memmodel>` in the LLVM memory model as follows.
+The AMDGPU memory model overrides the definition of each byte in the
+:ref:`LLVM memory model<memmodel>` as follows.
 
-Every (defined) read operation R reads a series of bytes written by (defined)
-write operations. Each initialized global is assumed to have an initial write
-operation which is atomic and defined to be location-ordered before any other
+Every (defined) read operation *R* reads a series of bytes written by (defined)
+write operations. Each initialized global is assumed to have an initial
+system-scoped atomic write operation that is *location-ordered* before any other
 read or write to that same location.
 
-For each byte of a read R, R may see any write to the same byte, except:
+For each byte of a read *R*, *R* may see any write to the same byte, except:
 
-- If a write W1 is location-ordered before a write W2, and W2 is
-  location-ordered before a read R, then R may not see W1.
-- If a read R happens-before a write W3, then R may not see W3.
+- If a write *W1* is *location-ordered* before a write *W2*, and *W2* is
+  *location-ordered* before a read *R*, then *R* may not see *W1*.
+- If a read *R* happens-before a write *W3*, then *R* may not see *W3*.
 
-The value returned by R is then defined as follows:
+The value returned by *R* is then defined as follows:
 
-- If no write is location-ordered before a read R, then R returns ``undef``.
-- Otherwise if the set consisting of R and all writes that R may see contains
-  only atomic operations with inclusive scopes, then R returns the value written
+- If no write is *location-ordered* before a read *R*, then *R* returns ``undef``.
+- Otherwise if the set consisting of *R* and all writes that *R* may see contains
+  only atomic operations with inclusive scopes, then *R* returns the value written
   by one of those writes.
-- Otherwise, if R may see some write that is not location-ordered before R,
-  then R returns ``undef``.
-- Otherwise, if R may see exactly one write W, then R returns the value written
-  by W.
-- Otherwise, R returns ``undef``.
+- Otherwise, if *R* may see some write that is not *location-ordered* before *R*,
+  then *R* returns ``undef``.
+- Otherwise, if *R* may see exactly one write *W*, then *R* returns the value written
+  by *W*.
+- Otherwise, *R* returns ``undef``.
 
 Properties
-----------
+==========
+
+.. tip::
 
-[This section is informational.]
+   This section is informational.
 
 The following properties follow from the definitions above:
 
-1. **Happens-before is necessary for location-order.** A write W is
-   location-ordered before a read R only if W happens-before R. This follows
+1. **Happens-before is necessary for location-order.** A write *W* is
+   *location-ordered* before a read *R* only if *W* happens-before *R*. This follows
    from the definition of availability and visibility operations, which always
    require a happens-before link with the preceding operation in the chain.
 
 2. **A write cannot be made available in a scope that does not contain it.** The
-   definition of an availability operation X requires that X's scope instance
-   includes W as a precondition. Since every scope instance that includes X also
-   includes W, availability cannot reach a scope instance that excludes W. In
+   definition of an availability operation *X* requires that *X*'s scope instance
+   includes *W* as a precondition. Since every scope instance that includes *X* also
+   includes *W*, availability cannot reach a scope instance that excludes *W*. In
    other words, availability can only "expand outwards" into progressively
    larger scopes.
 
 3. **Visibility is bounded by availability.** When a write is available in a
    scope instance, it can be made visible in that scope instance by a visibility
-   operation with the corresponding scope. Subsequent MakeVisible operations
+   operation with the corresponding scope. Subsequent ``MakeVisible`` operations
    make that write visible into narrower scope instances towards the observer.
 
 4. **A write can be made visible in a scope instance that does not contain it.**
    The definition of a *visibility operation* anchors scope instances to the
-   observer (Y), not to the original write. The only precondition is that the
+   observer (*Y*), not to the original write. The only precondition is that the
    write must already be visible or available in the scope instance of the
    visibility operation.
 
-5. **Availability and visibility chains.** For a write W to be visible to a read
-   R anywhere in the system, the sufficient condition is a chain of
+5. **Availability and visibility chains.** For a write *W* to be visible to a read
+   *R* anywhere in the system, the sufficient condition is a chain of
    happens-before edges that include availability and visibility operations with
-   inclusive scopes. It is not necessary that W and R themselves have inclusive
+   inclusive scopes. It is not necessary that *W* and *R* themselves have inclusive
    scopes. Each link in the availability and visibility definitions only checks
    the immediate predecessor, so intermediate operations can bridge scope gaps
    that the endpoints cannot satisfy directly. Such a chain passes through at
    least one availability operation and at least one visibility operation with
-   inclusive scopes, such that their common scope includes both W and R.
+   inclusive scopes, such that their common scope includes both *W* and *R*.
 
 .. _amdgcn-av-vulkan:
 
@@ -343,16 +363,16 @@ particular, the following instructions are equivalent.
    :header: "LLVM", "SPIRV", "Available/Visible Semantics"
    :widths: 25, 25, 50
 
-   "``load``", "OpLoad NonPrivatePointer", "\-"
-   "``load-visible``", "OpLoad NonPrivatePointer", "MakePointerVisible"
-   "``store``", "OpStore NonPrivatePointer", "\-"
-   "``store-available``", "OpStore NonPrivatePointer", "MakePointerAvailable"
-   "``load atomic``", "OpAtomicLoad", "MakePointerVisible. Also MakeVisible when order is at least ``acquire``."
-   "``load atomic !amdgcn-av-none``", "OpAtomicLoad", "MakePointerVisible"
-   "``store atomic``", "OpAtomicStore", "MakePointerAvailable. Also MakeAvailable when order is at least ``release``."
-   "``store atomic !amdgcn-av-none``", "OpAtomicStore", "MakePointerAvailable"
-   "``fence``", "OpMemoryBarrier", "MakeAvailable when order is at least ``release``, and MakeVisible when order is at least ``acquire``."
-   "``fence !amdgcn-av-none``", "OpMemoryBarrier", "\-"
+   "``load``", "``OpLoad NonPrivatePointer``", "\-"
+   "``load-visible``", "``OpLoad NonPrivatePointer``", "``MakePointerVisible``"
+   "``store``", "``OpStore NonPrivatePointer``", "\-"
+   "``store-available``", "``OpStore NonPrivatePointer``", "``MakePointerAvailable``"
+   "``load atomic``", "``OpAtomicLoad``", "``MakePointerVisible``. Also ``MakeVisible`` when order is at least ``acquire``."
+   "``load atomic !amdgcn-av-none``", "``OpAtomicLoad``", "``MakePointerVisible``"
+   "``store atomic``", "``OpAtomicStore``", "``MakePointerAvailable``. Also ``MakeAvailable`` when order is at least ``release``."
+   "``store atomic !amdgcn-av-none``", "``OpAtomicStore``", "``MakePointerAvailable``"
+   "``fence``", "``OpMemoryBarrier``", "``MakeAvailable`` when order is at least ``release``, and ``MakeVisible`` when order is at least ``acquire``."
+   "``fence !amdgcn-av-none``", "``OpMemoryBarrier``", "\-"
 
 .. note::
 
@@ -361,7 +381,7 @@ particular, the following instructions are equivalent.
    ``cmpxchg``. The ordering and semantics of these operations can be determined
    by combining suitable rules such as:
 
-   - "MakeAvailable if the order is at least ``release``, and the operation
+   - "``MakeAvailable`` if the order is at least ``release``, and the operation
      results in a store",
    - "Only if ``!amdgcn-av-none`` is absent", etc.
 
@@ -372,6 +392,6 @@ a. LLVM fence/atomic ordering operations have ``MakeAvailable`` /
    LLVM memory model is a "strong" subset of the Vulkan memory model.
 b. The AMDGPU memory model described here makes it possible to opt-out of the
    default availability and visibility semantics, and instead specify it on
-   select places including the new load-visible and store-available operations.
+   select places including the new *load-visible* and *store-available* operations.
    This expands the subset of the Vulkan memory model that can now be expressed
    in LLVM IR.
diff --git a/llvm/docs/AMDGPUUsage.rst b/llvm/docs/AMDGPUUsage.rst
index 8d2bcfeca2a41..66c7a40298006 100644
--- a/llvm/docs/AMDGPUUsage.rst
+++ b/llvm/docs/AMDGPUUsage.rst
@@ -1150,6 +1150,15 @@ supported for the ``amdgcn`` target.
 Memory Scopes
 -------------
 
+.. note::
+
+   `:ref:amdgpu-memmodel` is a work in progress to provide a complete memory
+   consistency model for AMDGPU, including newer features like *availability*,
+   *visibility* and asynchronous operations. The new model will replace the
+   model described in this section. Until then, this section should only be read
+   along with the new model; any ambiguity is likely to be settled in favour of
+   the new model.
+
 This section provides LLVM memory synchronization scopes supported by the AMDGPU
 backend memory model when the target triple OS is ``amdhsa`` (see
 :ref:`amdgpu-amdhsa-memory-model` and :ref:`amdgpu-target-triples`).
@@ -7096,6 +7105,15 @@ code (see :ref:`memmodel`).
 The AMDGPU backend supports the memory synchronization scopes specified in
 :ref:`amdgpu-memory-scopes`.
 
+.. note::
+
+   `:ref:amdgpu-memmodel` is a work in progress to provide a complete memory
+   consistency model for AMDGPU, including newer features like *availability*,
+   *visibility* and asynchronous operations. The new model will replace the
+   model described in this section. Until then, this section should only be read
+   along with the new model; any ambiguity is likely to be settled in favour of
+   the new model.
+
 The code sequences used to implement the memory model specify the order of
 instructions that a single thread must execute. The ``s_waitcnt`` and cache
 management instructions such as ``buffer_wbinvl1_vol`` are defined with respect

>From 583de223ecbf9908a29dafe9881196fbb8374e90 Mon Sep 17 00:00:00 2001
From: Sameer Sahasrabuddhe <sameer.sahasrabuddhe at amd.com>
Date: Thu, 21 May 2026 16:30:24 +0530
Subject: [PATCH 3/3] cleanup scopes, fix visibility, update av operations, fix
 examples of !amdgcn-av-none

---
 llvm/docs/AMDGPUMemoryModel.rst | 151 +++++++++++++++++---------------
 llvm/docs/AMDGPUUsage.rst       |  12 ++-
 2 files changed, 85 insertions(+), 78 deletions(-)

diff --git a/llvm/docs/AMDGPUMemoryModel.rst b/llvm/docs/AMDGPUMemoryModel.rst
index c8564bcc0a772..91e8f642ddf93 100644
--- a/llvm/docs/AMDGPUMemoryModel.rst
+++ b/llvm/docs/AMDGPUMemoryModel.rst
@@ -1,8 +1,8 @@
 .. _amdgpu-memmodel:
 
-=================================
+=====================
  AMDGPU Memory Model
-=================================
+=====================
 
 .. contents::
    :local:
@@ -40,6 +40,66 @@ Synchronization Operations
   propagated in the system. Typical examples are atomic operations (including
   fences) with at least ``release`` or ``acquire`` ordering.
 
+.. _amdgpu-scopes:
+
+Scopes
+======
+
+A *scope* describes sets of memory accesses and synchronization operations in a
+multi-threaded execution environment. Each such set is called an *instance* of
+that scope, or a *scope instance* for short.
+
+- Each memory access or synchronization operation belongs to at most one
+  instance of every scope defined by the target.
+- When an operation *X* specifies a scope *S*, it indicates the instance of *S*
+  that contains *X*. This scope instance is also termed as *X's instance of
+  scope S* or just "*X*'s scope instance", when *S* is implied by the context.
+- When an operation does not specify a scope, it indicates the anonymous
+  system-wide scope.
+
+LLVM scopes
+-----------
+
+The LLVM Language Reference defines the following :ref:`scopes<syncscope>`:
+
+Anonymous system-wide scope ("")
+  There exists a single instance of this scope that contains the memory accesses
+  and synchronization operations performed by all threads. This is typically
+  called "system" scope in languages and targets supported by Clang/LLVM.
+
+"singlethread" scope
+  Each thread corresponds to a "singlethread" scope instance that contains the
+  memory accesses and synchronization operations performed by that thread.
+
+AMDGPU scopes
+-------------
+
+The AMDGPU backend further refines the LLVM scopes with the following
+target-defined scopes and constraints:
+
+- The anonymous system-wide scope (same as LLVM)
+- "agent" scope
+- "cluster" scope
+- "workgroup" scope
+- "wavefront" scope
+- "singlethread" scope (same as LLVM)
+
+These are arranged from largest scope ("") to smallest scope ("singlethread").
+
+- Every instance *X* of some scope *S1* other than "singlethread" scope is
+  partitioned by the scope *S2* one level below it. Each subset defined by this
+  partition is an instance of *S2* and is called a *subscope instance* of *X*.
+- It follows that if two scope instances *X* and *Y* intersect, then their
+  intersection is the smaller of *X* and *Y*.
+- A scope *S1* is a *subscope* of a scope *S2* if every instance of *S1* is a
+  subscope instance of some instance of *S2*.
+
+**Inclusive Scopes**: Two operations *X* and *Y* are said to have *inclusive scopes*
+if the scope instance of each operation contains the other operation. In that
+case, the *common scope instance* *S'* of *X* and *Y* is the intersection of their
+scope instances. The scope corresponding to *S'* is also termed as the *common
+scope* of *X* and *Y*.
+
 Availability and Visibility
 ===========================
 
@@ -69,6 +129,8 @@ these necessary conditions, and hence they can be explained using the rules from
 either memory model. But the new intrinsics and metadata *opt out* of the LLVM
 memory model, and can only be explained using the AMDGPU memory model.
 
+.. _amdgpu-store-available:
+
 store-available
 ---------------
 
@@ -80,12 +142,13 @@ store-available
    cmpxchg      [syncscope("<target-scope>")]
 
 The ``@llvm.amdgcn.av.global.store.b128`` intrinsic performs a non-atomic
-*store-available* operation on ``ptr`` whose side-effects are made available to
-:ref:`its instance<amdgpu-scopes>` of ``scope``.
+*store-available* operation on ``ptr`` with scope ``scope``.
 
 An atomic operation that results in a store operation is a *store-available*
 operation with scope ``syncscope``.
 
+.. _amdgpu-load-visible:
+
 load-visible
 ------------
 
@@ -97,8 +160,7 @@ load-visible
    cmpxchg      [syncscope("<target-scope>")]
 
 The ``@llvm.amdgcn.av.global.load.b128`` intrinsic performs a non-atomic
-*load-visible* operation which ensures the visibility of all side-effects on
-``ptr`` available at :ref:`its instance<amdgpu-scopes>` of ``scope``.
+*load-visible* operation on ``ptr`` with scope ``scope``.
 
 An atomic operation that results in a read operation is a *load-visible* operation
 with scope ``syncscope``.
@@ -150,70 +212,17 @@ the ordering of other memory accesses.
 
 .. code-block:: llvm
 
-   ; This includes a MakeAvailable operation.
-   store atomic [syncscope("<target-scope>")] <ordering>
-
-   ; This **does not** include a MakeAvailable operation.
-   store atomic [syncscope("<target-scope>")] <ordering> !amdgcn-av-none
-
-.. _amdgpu-scopes:
-
-Scopes
-======
-
-A *scope* describes sets of memory accesses and synchronization operations in a
-multi-threaded execution environment. Each such set is called an *instance* of
-that scope, or a *scope instance* for short. Each memory access or
-synchronization operation belongs to at most one instance of every scope defined
-by the target.
-
-- A scope instance *S1* is a *subscope instance* of a scope instance *S2* if
-  *S1* is a subset of *S2*.
-- A scope *S1* is a *subscope* of a scope *S2* if every instance of *S1* is a
-  subset of some instance of *S2*.
-
-When an operation *X* specifies a scope *S*, it indicates the instance of *S* that
-contains *X*. This scope instance is also termed as *X's instance of scope S* or
-just "*X*'s scope instance", when *S* is implied by the context.
-
-When an operation does not specify a scope, it indicates the singleton instance
-of the LLVM system-wide scope defined below.
-
-**Inclusive Scopes**: Two operations *X* and *Y* are said to have *inclusive scopes*
-if the scope instance of each operation contains the other operation. In that
-case, the *common scope instance* *S'* of *X* and *Y* is the intersection of their
-scope instances. The scope corresponding to *S'* is also termed as the *common
-scope* of *X* and *Y*. [Informational note -- Since scopes are arranged in a linear
-hierarchy, clearly *S'* is the smaller of the two scope instances.]
-
-LLVM scopes
------------
-
-The LLVM Language Reference defines the following :ref:`scopes<syncscope>`:
-
-- A "singlethread" scope. Each thread has a corresponding "singlethread" scope
-  instance that contains the memory accesses and synchronization operations
-  performed by that thread.
-- An anonymous system-wide scope with a single instance that contains the memory
-  accesses and synchronization operations performed by all threads. This is
-  typically called "system" scope in languages and targets supported by
-  Clang/LLVM.
-
-AMDGPU scopes
--------------
-
-The AMDGPU backend defines the following target-defined scopes:
-
-- The anonymous system-wide scope (same as LLVM)
-- "agent" scope
-- "cluster" scope
-- "workgroup" scope
-- "wavefront" scope
-- "singlethread" scope (same as LLVM)
+   ; This includes the following operations:
+   ; - The atomic store at "agent" scope,
+   ; - A store-available operation at "agent" scope on `ptr`,
+   ; - A `MakeAvailable` operation at "agent" scope that affects previous memory accesses.
+   store atomic syncscope("agent") release ptr
 
-These are arranged from largest scope ("") to smallest scope
-("singlethread"). Every scope instance (except the singleton default scope
-instance) is included in a scope instance at the level above it.
+   ; This includes the following operations:
+   ; - The atomic store at "agent" scope,
+   ; - A store-available operation at "agent" scope on `ptr`.
+   ; Noteably, it does not include a `MakeAvailable` operation on other memory accesses.
+   store atomic syncscope("agent") release ptr !amdgcn-av-none
 
 Ordering
 ========
@@ -262,8 +271,8 @@ and one of the following holds:
   - *X* makes *W* visible in a scope instance *S1* that includes *Y*, and,
   - *X* is included in the scope instance *S2* of *Y*.
 
-  Then *Y* makes *W* visible in the intersection of *S1* and *S2*, and every
-  subscope instance of *S2* that includes *Y*.
+  Then *Y* makes *W* visible in the intersection *S* of *S1* and *S2*, and every
+  subscope instance of *S* that includes *Y*.
 
 Location Order
 --------------
diff --git a/llvm/docs/AMDGPUUsage.rst b/llvm/docs/AMDGPUUsage.rst
index 36f3970970931..b3925d8c7ad00 100644
--- a/llvm/docs/AMDGPUUsage.rst
+++ b/llvm/docs/AMDGPUUsage.rst
@@ -1838,11 +1838,10 @@ The AMDGPU backend implements the following LLVM IR intrinsics.
 '``llvm.amdgcn.av``' Intrinsics
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
-The '``llvm.amdgcn.av``' intrinsics perform load and store operations on flat or
-global memory, with explicit control on how their side-effects propagate through
-the system. They take a *scope* argument as a string metadata, which indicates
-the scope within which these side-effects are guaranteed to be observable.
-[TODO: The exact semantics as a memory consistency model is a work in progress.]
+The '``llvm.amdgcn.av``' intrinsics perform
+:ref:`store-available<amdgpu-store-available>` and
+:ref:`load-visible<amdgpu-load-visible>` operations on flat or global memory
+with *scope* supplied as a metadata argument.
 
 The pointer argument can be a global pointer (``addrspace(1)``) or a flat
 pointer (``addrspace(0)``). Global pointers select ``global_load``/
@@ -1874,8 +1873,7 @@ Implementation Details
 
 This section is informational and for **internal reference only**. Users should
 not rely on the expansions described below. The only reliable user-level
-guarantees are those provided by the memory consistency model, which is
-currently a work in progress.
+guarantees are those provided by the :ref:`AMDGPU memory model<amdgpu-memmodel>`.
 
 The tables below show the cache policy bits for global pointer variants.
 Flat pointer variants use the corresponding ``flat_load``/``flat_store``



More information about the llvm-commits mailing list