[llvm] [AMDGPU] A Vulkan-style memory model weaker than the LLVM model (PR #191246)
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 29 11:45:56 PDT 2026
================
@@ -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
----------------
arsenm wrote:
These should use `:ref:` blocks to the relevant LangRef sections
https://github.com/llvm/llvm-project/pull/191246
More information about the llvm-commits
mailing list