Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ How the generated SPIR-V expects the runtime to work:
| `ds_cmpst_f32`/`_rtn_f32`, `global_atomic_fcmpswap`, `buffer_atomic_fcmpswap` | `tmp = M; M = (tmp == CMP as binary32) ? DATA : tmp`, and the returning forms give `tmp` (RDNA2 ISA). The compare is a float compare (`OpFOrdEqual`): −0 matches +0, and a NaN never matches, not even the same bits. `OpAtomicCompareExchange` compares bits, so this is a loop (`Gen.float_cmpswap_loop`): read M, pick DATA or M by the float compare, and swap that in if M still holds what was read. Exact under contention. Operands: `ds_cmpst_f32` takes CMP then DATA, as `ds_cmpst_b32`; the global and buffer forms take `vdata` as {DATA, CMP}, as `cmpswap`. Inactive lanes, bounds and `glc` are as for their integer siblings. **Not measured:** subnormal inputs; the interpreter compares them exactly. `_x2` forms stay `Unsupported`; the image form is #2653's. Until 2026-11-22 all four raised `Unsupported`. CPU-checked by `tests/semantic/test_float_cmpswap.py` (±0, NaN, ∞, a subnormal, no `glc`, buffer bounds, 32 lanes on one dword). **Not GPU-verified.** |
| LDS ADDTID (`ds_read_addtid_b32`, `ds_write_addtid_b32`) | The LDS forms address one dword at `M0[15:0] + offset + 4 * lane`, where `offset` is an unsigned 16-bit byte offset and `lane` is the invocation index within the RDNA wave. The Workgroup array supplies LDS_BASE. The compute/mesh path uses the ordinary LDS addressing and EXEC guard. The GDS form raises `Unsupported`. Source: RDNA2 ISA §10.4 and §12.13 (opcodes 176/177), LLVM GFX1030 assembly syntax. `tests/semantic/test_lds_addtid.py` compares the two forms with ordinary LDS reads/writes over two waves, including partial EXEC and emulated wave64. |
| `ds_add_f32`, `ds_add_rtn_f32` | Atomic binary32 add on LDS (`Workgroup` memory); `ds_add_rtn_f32` returns the exact pre-addition 32-bit value in `vdst`, while `ds_add_f32` discards it. Lowered to a core SPIR-V structured `OpAtomicCompareExchange` (`Workgroup` scope `2`, relaxed `None`/`None` equal and unequal semantics) CAS loop over `%v_lds` (`%uint` storage, `OpBitcast` <-> `%float` + `OpFAdd`, bitwise `OpIEqual` exit check so `NaN` terminates in 1 iteration without looping, returning loop-header `%cur` for `ds_add_rtn_f32` with no `VK_EXT_shader_atomic_float` dependency). Hardware-verified on native RDNA2 (`gfx1036`, Windows 11, driver `32.0.21045.29003`, wave32 & wave64, 0 mismatches): confirmed normal binary32 sums (`1.5 + 2.25 -> 3.75`), signed zero rules (`+0.0 + +0.0 -> +0.0`, `-0.0 + -0.0 -> -0.0`, `+0.0 + -0.0 -> +0.0`), infinities/NaNs (`+Inf + 1.0 -> +Inf`, `+Inf + -Inf -> 0xffc00000` qNaN, quiet NaN payload `0x7fc12345` preserved), exact old-value return on `_rtn_` vs unchanged `vdst` on non-return, `offset:` variants (4, 16, 64, 256, 1020), unaligned byte addressing (unaligned non-4-byte-multiple byte addresses dropped by hardware), `EXEC` inactive-lane masking (0x55555555 leaves LDS untouched by inactive lanes), and wave32/wave64 multi-lane same-address contention (32 and 64 lanes concurrently adding 1.0f yield exact sums 32.0 / 64.0 and return unique permutation chains with zero lost updates). Subnormal note: native `gfx1036` flushed FP32 subnormal inputs/outputs to zero (FTZ/DAZ) in the LDS adder, whereas core SPIR-V `OpFAdd` leaves FP32 denorm flushing to the host Vulkan driver / `DenormFlushToZero` mode. CPU-checked by `tests/semantic/test_lds_add_f32.py`. `M = M + DATA` as binary32 on the LDS dword (RDNA2 ISA, `DS_ADD_F32`); the `_rtn_` form returns the old M. No core SPIR-V atomic adds floats on Workgroup memory, so it is an `OpAtomicCompareExchange` loop with an `OpFAdd` (round to nearest even, `NoContraction`). That is exact under contention: every update is one successful exchange of the value it read. **Not measured:** how the LDS adder treats denormals and NaN payloads; the result is `OpFAdd`'s IEEE sum. The f32 min/max forms are #2495's. Until 2026-11-22 both raised `Unsupported`. CPU-checked by `tests/semantic/test_lds_add_f32.py` (a value grid, the non-returning form, `offset:`, ties to even, -0 + +0, overflow to ±inf, seeded values, against a once-rounded double sum). **Hardware-measured natively on `gfx1100`** (RX 7900 XTX, driver 26.10.1) with `tools/hw_oracle`, 49 cases: the LDS adder **flushes denormal inputs and denormal results to zero** (`0x00800001 + -0x00800000` gives `+0`, not `0x00000001`; `0x007fffff + 0x00800000` gives `0x00800000`, not the tie's `0x00ffffff`; `0x00000001 + 0x00000001` gives `+0`), while the `OpFAdd` the translator emits keeps them, so denormal operands and results are a known divergence. A NaN is quieted with its payload and sign kept (sNaN `0x7f800001` becomes `0x7fc00001`) and when both operands are NaN the DATA operand wins whatever the payloads (`0x7fc00007 + 0x7fc00000` gives `0x7fc00000`); `inf + -inf` gives `0xffc00000`. Normal sums match that model on the same run: ties to even (`1.0 + 2^-24` stays `1.0`, `1.0 + 3*2^-25` goes up), `-0 + -0` is `-0`, `+0 + -0` is `+0`, overflow to `±inf`, and the `_rtn_` form returns the raw pre-op dword, denormal or not. |
| `ds_mskor_b32`, `ds_mskor_rtn_b32` | `tmp = M[A]; M[A] = (tmp & ~DATA) \| DATA2`; the `_rtn_` form returns `tmp` (RDNA2 ISA, DS). Emitted as an atomic AND with `~DATA`, whose result is `tmp`, then an atomic OR with `DATA2`, so the returned value is exact. DATA2 bits outside DATA are ORed in too, as the formula says; it isn't a masked insert. **Approximation:** the memory ends as the single op leaves it unless another lane's op on the same dword lands between the two atomics. Until 2026-11-22 both raised `Unsupported`. CPU-checked by `tests/semantic/test_lds_mskor.py` (a value grid, the non-returning form, DATA2 outside DATA, `offset:`, seeded values). Measured on native gfx1100, including DATA2 bits outside DATA being ORed in, so it is not a masked insert. **GPU-verified** on native RDNA2 (gfx1036, the Radeon iGPU of a Ryzen 7 9800X3D, Windows driver 32.0.21042.62) through `tools/hw_oracle`: 20 of 20 one-thread cases (five M/DATA/DATA2 triples including DATA2 bits outside DATA, both forms, `offset:` 0 and 12) leave `(M & ~DATA) \| DATA2` and return M. **The approximation, measured:** 32 lanes on one dword (M = 0xABCD12FF, DATA = 0xFF, DATA2 = 1 << (lane & 7)) run one lane at a time in lane order on the hardware: each lane returns the word the lane before it left (lane 0 0xABCD12FF, lane 1 0xABCD1201, lane 2 0xABCD1202, …) and the dword ends 0xABCD1280. The two-atomic emission does every AND before every OR, so it would end 0xABCD12FF and return 0xABCD1200 to lanes 1..31 (expected, not run through the translator). **Not GPU-verified.** |
| `ds_mskor_b32`, `ds_mskor_rtn_b32` | `tmp = M[A]; M[A] = (tmp & ~DATA) \| DATA2`; the `_rtn_` form returns `tmp` (RDNA2 ISA, DS). Emitted as a Workgroup-scope atomic load and compare-exchange loop: a failed exchange retries from the word it observed, so each lane publishes the complete update and returns the old word of its successful exchange. DATA2 bits outside DATA are ORed in too; this is not a masked insert. EXEC predication, LDS bounds and `offset:` still apply. Until 2026-11-22 both forms raised `Unsupported`; the later split atomic AND/OR implementation allowed another lane to interleave between the two updates. CPU-checked by `tests/semantic/test_lds_mskor.py` (value grid, seeded values, both forms, offsets, destination aliasing, inactive lanes, and two contending lanes accepting either atomic serial order). Measured on native gfx1100, including DATA2 bits outside DATA. Native RDNA2 measurements through `tools/hw_oracle` (gfx1036, Radeon iGPU of Ryzen 7 9800X3D, Windows driver 32.0.21042.62): 20/20 one-thread cases cover five M/DATA/DATA2 triples, both forms and offsets 0/12. A 32-lane case (M = 0xABCD12FF, DATA = 0xFF, DATA2 = 1 << (lane & 7)) followed lane order and ended 0xABCD1280. The CAS lowering guarantees atomic updates, but does not promise that observed native lane order on another GPU. **Translator GPU-checked:** the hand-written two-lane contention case in `test_lds_mskor.py`, on NVIDIA RTX 3080 10 GB, driver 610.88, Windows 11 build 26300, wave32 with GPU-assisted validation. From zero, lane 0 clears bit 0/sets bit 1 and lane 1 clears bit 1/sets bit 0. Only final 1 or 2 is valid: the old lowering produced `[old0, final0, old1, final1] = [0,3,0,3]`; the CAS lowering produced `[0,1,2,1]`. Both modules passed SPIR-V validation. |
| `ds_{inc,dec,rsub}_u32`, `ds_{min,max}_f32` (and `_rtn_`) | 32-bit LDS atomics lowered inside `Gen.lds_op` via a Workgroup-scope `OpAtomicCompareExchange` CAS loop (no shader float atomic extension needed): `ds_inc_u32` gives `(M >= DATA) ? 0 : M + 1` (unsigned), `ds_dec_u32` gives `(M == 0 \|\| M > DATA) ? DATA : M - 1` (unsigned), `ds_rsub_u32` gives `DATA - M` (wrapping), and `ds_{min,max}_f32` use GLSL.std.450 `NMin`/`NMax` (NaN returns the non-NaN operand, `-0.0 < +0.0`, matching `global_atomic_fmin`/`fmax`). The `_rtn_` forms return the pre-op word in `vdst`; inactive lanes skip the op and preserve `vdst`. Source: AMD RDNA2 ISA DS encoding. CPU-checked by `tests/semantic/test_lds_atomics_cas.py` (edge value grids, NaN/±0/±inf, `offset:`, `vdst` aliasing, inactive lanes, sequential wraparound, Workgroup CAS loop structure, outside-compute and bad-operand rejections). Measured on native gfx1100 (single thread, 28 cases): `ds_inc_u32` gives `M + 1` until `M >= DATA` and 0 from there, `ds_dec_u32` gives `M - 1` or `DATA` when `M == 0` or `M > DATA`, `ds_rsub_u32` wraps, the `_rtn_` forms return the pre-op word, and `ds_{min,max}_f32` take the non-NaN operand and rank `-0.0` below `+0.0`. **Not GPU-verified.** |
| `ds_wrxchg2_rtn_b32`, `ds_wrxchg2st64_rtn_b32` | Two atomic exchanges, addressed as `ds_write2_b32` addresses its dwords: `vaddr + offset0 * 4` and `vaddr + offset1 * 4` (times 64 for st64). `M[A0] = data0` and `M[A1] = data1`, and `vdst` (a VGPR pair) gets the two old words. Both exchanges run before either result is written, so `vdst` may alias the address or data VGPRs. When both offsets name one dword, the second exchange returns `data0`. Each exchange is atomic on its own, not the pair together, as on hardware (two DS operations). Until 2026-11-22 both ops raised `Unsupported`. CPU-checked by `tests/semantic/test_lds_wrxchg2.py` (offsets, st64 = 256-byte steps, the same dword twice, aliasing, a single-VGPR `vdst` rejected). Measured on native gfx1100: with `vaddr` 16 the offsets 0/4 read `vaddr + 0` and `vaddr + 16`, the `_st64_` form's offsets 1/2 read `vaddr + 256` and `vaddr + 512` (64 dwords per step), `vdst` takes the two old words, and with both offsets on one dword the second exchange returns `data0`; `vdst` may alias the address or the data pair, which still lands both data words in LDS and leaves the old words in `vdst`. **Not GPU-verified.** |
| `ds_wrxchg_rtn_b64`, `ds_wrxchg2_rtn_b64`, `ds_wrxchg2st64_rtn_b64` | 64-bit LDS exchanges (two dwords, low first): `M[A] = DATA`, and `vdst` gets the old 64 bits. `wrxchg_rtn_b64` has one element at `vaddr + offset`. The `2` forms have two, at `vaddr + offset0/offset1 * 8` (times 64 for st64), as `ds_write2_b64` addresses them, and `vdst` is four VGPRs. Each dword is its own 32-bit `OpAtomicExchange`, without Int64 atomics on Workgroup memory. All exchanges run before `vdst` is written. **Approximation:** two lanes exchanging into one element at once can tear it, leaving one's low dword with the other's high dword (A.lo, B.lo, B.hi, A.hi), which a 64-bit exchange never does; and the returned pair comes from two reads, so another lane's 64-bit op on the same element between them can show up half done (as for `ds_and/or/xor_b64`). Until 2026-11-22 these three raised `Unsupported`. CPU-checked by `tests/semantic/test_lds_wrxchg_b64.py` (values, `offset:`, `vdst` aliasing the data, both 2 forms, st64 = 512-byte steps, bad operands rejected). Measured on native gfx1100: `M[vaddr]` takes the data pair and `vdst` gets the old pair, the `2` form's offsets 0/2 read `vaddr + 0` and `vaddr + 16` with four old dwords in `vdst`, and the `_st64_` form's offsets 1/2 read `vaddr + 512` and `vaddr + 1024` (64 pairs per step). **Not GPU-verified.** GPU-checked by `runtime/test/shaders/cs_lds_xchg64_ext.asm` (all three forms: both dwords and the returned pair of `ds_wrxchg_rtn_b64`, the four old and four new dwords of `ds_wrxchg2_rtn_b64` at offsets 0/2, the low dwords of `ds_wrxchg2st64_rtn_b64` at offsets 1/2; one lane, on an RX 7900 XT, RADV NAVI31, Mesa 26.3.1, Linux, native wave64 and `LEONIDA_W64_EMULATE=1`). |
Expand Down
64 changes: 61 additions & 3 deletions tests/semantic/test_lds_mskor.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Value tests for RDNA2 ds_mskor_b32 / ds_mskor_rtn_b32, run through spirv_mini_interp on the CPU.

RDNA2 ISA, "Data Share Instructions": tmp = M[A]; M[A] = (tmp & ~DATA) | DATA2; the _rtn_ form returns tmp in vdst.
The translator does an atomic AND with ~DATA (whose result is tmp) and then an atomic OR with DATA2. Each case runs
one invocation (lane 0): it seeds the dword, runs the op and reads the dword back. Standard library only.
Single-lane cases check values and register aliasing; a two-lane race checks the old values and final memory against
both possible serial orders. Standard library only.
"""
import os
import random
Expand All @@ -13,7 +13,9 @@
sys.dont_write_bytecode = True
sys.path.insert(0, HERE)

from test_lds_atomics_values import run_atomic # noqa: E402 (also puts tools/ on sys.path)
from test_lds_atomics_values import FSSM, VSHARP, run_atomic # noqa: E402 (also puts tools/ on sys.path)
import support # noqa: E402
import spirv_mini_interp as si # noqa: E402

M32 = 0xFFFFFFFF
VALUES = [0, M32, 0x12345678, 0xF0F0F0F0, 0x0000FFFF, 0x80000001]
Expand All @@ -23,6 +25,26 @@ def mskor(m, mask, data):
return (m & ~mask | data) & M32


def contention_listing(returning=True, offset=0):
"""Two lanes replace each other's bit. An atomic execution ends in 1 or 2, never their union 3."""
atomic = "ds_mskor_rtn_b32 v4, v1, v2, v3" if returning else "ds_mskor_b32 v1, v2, v3"
return ["v_mov_b32_e32 v1, 0", "v_mov_b32_e32 v4, 0xdead",
"s_mov_b32 exec_lo, 1", f"ds_write_b32 v1, v1 offset:{offset}", "s_waitcnt lgkmcnt(0)",
"s_mov_b32 exec_lo, 3", "s_barrier",
"v_lshlrev_b32_e32 v2, v0, 1", "v_xor_b32_e32 v3, 3, v2", f"{atomic} offset:{offset}",
"s_waitcnt lgkmcnt(0)", "s_barrier", f"ds_read_b32 v5, v1 offset:{offset}",
"s_waitcnt lgkmcnt(0)", "v_lshlrev_b32_e32 v6, 3, v0",
"buffer_store_dword v4, v6, s[0:3], 0 offen",
"buffer_store_dword v5, v6, s[0:3], 0 offen offset:4", "s_endpgm"]


def run_wave(body):
spv = support.translate_lines("cs_mskor", body, fssm=FSSM)
return si.run_workgroup(spv, 32, 32,
inputs_for=lambda i: {"%wg_id": (0, 0, 0), "%local_id": (i, 0, 0), "%sg_lane": i},
push_constants=VSHARP + [0] * 28, memory={})


class LdsMskor(unittest.TestCase):
def test_returning_form(self):
for m in VALUES:
Expand Down Expand Up @@ -53,6 +75,42 @@ def test_seeded(self):
self.assertEqual(run_atomic("ds_mskor_rtn_b32 v5, v1, v3, v4", m, data=(mask, data)),
(m, mskor(m, mask, data)))

def test_contending_updates_are_atomic(self):
# run_workgroup interleaves one SPIR-V instruction per lane: split AND/OR clears both bits before either
# lane sets one, incorrectly leaving 3. A CAS loser must retry from the winner's value. Accept either order.
for returning in (False, True):
for offset in (0, 12):
with self.subTest(returning=returning, offset=offset):
mem = run_wave(contention_listing(returning, offset))
got = tuple(mem[i] for i in (0, 4, 8, 12))
expected = ((0, 1, 2, 1), (1, 2, 0, 2)) if returning else (
(0xDEAD, 1, 0xDEAD, 1), (0xDEAD, 2, 0xDEAD, 2))
self.assertIn(got, expected)

def test_return_can_alias_address_mask_or_data(self):
init, mask, data = 0x12345678, 0xFF, 0x555
for reg in (1, 2, 3):
with self.subTest(destination=reg):
mem = run_wave(["s_mov_b32 exec_lo, 1", "v_mov_b32_e32 v1, 0x40",
f"v_mov_b32_e32 v2, {mask}", f"v_mov_b32_e32 v3, {data}",
f"v_mov_b32_e32 v4, {init}", "ds_write_b32 v1, v4", "s_waitcnt lgkmcnt(0)",
f"ds_mskor_rtn_b32 v{reg}, v1, v2, v3", "s_waitcnt lgkmcnt(0)",
"v_mov_b32_e32 v10, 0x40", "ds_read_b32 v5, v10", "s_waitcnt lgkmcnt(0)",
"v_mov_b32_e32 v6, 0", f"buffer_store_dword v{reg}, v6, s[0:3], 0 offen",
"buffer_store_dword v5, v6, s[0:3], 0 offen offset:4", "s_endpgm"])
self.assertEqual((mem[0], mem[4]), (init, mskor(init, mask, data)))

def test_inactive_lanes_keep_their_destination(self):
mem = run_wave(["v_mov_b32_e32 v1, 0x40", "v_mov_b32_e32 v2, 7", "v_mov_b32_e32 v3, 3",
"v_mov_b32_e32 v4, 8", "v_mov_b32_e32 v5, 0xdead", "s_mov_b32 exec_lo, 1",
"ds_write_b32 v1, v2", "s_waitcnt lgkmcnt(0)", "ds_mskor_rtn_b32 v5, v1, v3, v4",
"s_waitcnt lgkmcnt(0)", "s_mov_b32 exec_lo, -1", "s_barrier", "ds_read_b32 v6, v1",
"s_waitcnt lgkmcnt(0)", "v_lshlrev_b32_e32 v7, 3, v0",
"buffer_store_dword v5, v7, s[0:3], 0 offen",
"buffer_store_dword v6, v7, s[0:3], 0 offen offset:4", "s_endpgm"])
self.assertEqual([mem[8 * lane] for lane in range(32)], [7] + [0xDEAD] * 31)
self.assertEqual([mem[8 * lane + 4] for lane in range(32)], [12] * 32)


if __name__ == "__main__":
unittest.main()
Loading
Loading