Skip to content

rdna2spirv: ds_append and ds_consume (LDS), once per wave - #3976

Open
Vonsek wants to merge 2 commits into
ItsNotRexed:mainfrom
Vonsek:claude/ds-append
Open

Vonsek wants to merge 2 commits into
ItsNotRexed:mainfrom
Vonsek:claude/ds-append

Conversation

@Vonsek

@Vonsek Vonsek commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Requested by Lucas · project thread

Claimed in the coordination issue as: EGEN-DS-APPEND, #3796 (comment)

What and why

ds_append and ds_consume raised Unsupported. Compute shaders use these GFX10 LDS ops for wave-level stream compaction. I found them by translating every gfx1013 opcode form on main. In the open PRs they appear only in #2323's decoder tables and in a CI script comment.

Change (tools/rdna2spirv.py, Gen.ds_append):

  • What it does. One LDS add (append) or subtract (consume) of the number of lanes on in EXEC, for the whole wave, at the byte address in the offset field. Every active lane gets the value from before the op, and inactive lanes keep vD.
  • Who does it. The first active lane performs the op, found from the wave's EXEC gathered as s_bcnt1 does.
  • How the old value reaches the other lanes. A subgroup shuffle from that lane, or an exchange round (x_round) under --wave64-emulate. Without emulation, a wave64 on 32-wide subgroups is only right in the half that holds the first active lane. That is the known cross-half approximation.
  • Ordering. The op is dispatched before lds_op's per-lane EXEC selection, so the subgroup operations stay in uniform control flow.
  • Not covered. The gds form stays Unsupported.

Measured natively on RDNA2 gfx1036 (Radeon iGPU of a Ryzen 9 9950X3D, driver 26.10.1, tools/hw_oracle raw kernels, wave32 and wave64):

  • EXEC 0xf0 appends 4 and returns the old 100 to lanes 4-7 only. The other lanes keep their value.
  • A wave64 with lanes 0, 32 and 63 on appends 3.
  • ds_consume offset:8 on 3 lanes subtracts 3.
  • M0 values 0x4, 0x100, 0x10000, 0x40000 and 0x80000 (with s_nops after the write) do not move the address. The offset field alone is the address. RDNA1 is not measured.

New contract row in docs/design.md.

Tested

Windows 11, LLVM llvm-mc (-mcpu=gfx1013), VK_LOADER_LAYERS_DISABLE=~implicit~, one GPU at a time, python -I scripts/run_gpu_tests.py --filter ds_append:

GPU Driver cs_w32_ds_append cs_w64_ds_append
AMD Radeon RDNA2 iGPU, Ryzen 9 9950X3D 26.10.1 PASS 100 57005 200 57005 108 197 108 140 PASS, native wave64: 100 100 57005 103 200 199 103 167
NVIDIA GeForce RTX 5080 616.92 PASS, same words PASS with LEONIDA_W64_EMULATE=1, same words (SKIP without: no 64-wide subgroup)
  • New fixtures: runtime/test/shaders/cs_w32_ds_append.asm and cs_w64_ds_append.asm. They cover partial-EXEC append and consume, the returned value read in active and inactive lanes (lanes 32, 33 and 63 for wave64), the LDS words, and a full-wave append. The words are derived in their headers. Both are registered in check.py, run_gpu_tests.py and regen_fixtures.py.

  • New test: tests/semantic/test_ds_append.py. spirv_mini_interp keeps one LDS copy per invocation, so the test checks the parts that don't need shared LDS against a plain-Python reference:

    • the count;
    • that only the first active lane's copy changes;
    • the returned value in every active lane, and the inactive lanes;
    • wave64 across both halves and the offset address;
    • that the fixtures translate and gds is refused.

    The shared-LDS words are checked by the GPU fixtures.

  • Red on main: the test errors with Unsupported: ds_append / ds_consume.

  • Negative control: I made every active lane perform the op. The test fails, and on the Radeon both fixtures fail: the first append leaves 164 instead of 108, and the wave64 one 109 instead of 103. Then I reverted it.

  • Passing: python -m unittest discover -s tests/semantic (no new failures: the 7 failures and 3 errors it reports fail the same way on main aa2988cc, in test_loop_ballot_barrier (emulate), test_vcmp16_opsel (spirv-val), test_cmp_class_f64 (TEST-853) and test_mutations (v_mul_legacy_f32 anchor)), python tests/semantic/regen_fixtures.py --check (clean), python -I tests/test_shader_checks.py, and pytest tests/test_docs_design_rows.py tests/test_run_gpu_tests.py.

AI-assisted.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

One add / subtract of the wave's active-lane count at the offset address,
done by the first active lane; the old value to every active lane by a
subgroup shuffle (an exchange round under --wave64-emulate). Measured
natively on gfx1036: the offset alone is the address, M0 does not move it.
The gds form stays Unsupported. GPU fixtures cs_w32/w64_ds_append, semantic
test test_ds_append.py, design.md row.

AI-assisted.
@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 0fc8a15 on NVIDIA GeForce RTX 4090 (driver 32.0.16.2101) and on AMD Radeon(TM) Graphics (driver 32.0.21043.5001), Windows 11, MSVC 14.51: pass.

Vulkan reports subgroup sizes 32..64 on the AMD GPU and 32..32 on the NVIDIA GPU. The runner confirmed the compute subgroup size of each dispatch.

I translated the checked-in synthetic disassembly with the translator of this head. For each of cs_w32_ds_append and cs_w64_ds_append, I used spirv_asm.assemble(t.support.translate_fixture(n, fssm=t.header(n))) with t = tests/semantic/test_ds_append.py. For the emulated form, I set t.rdna2spirv.WAVE64_EMULATE = True before I translated cs_w64_ds_append.
I ran the modules with vk_compute_test.exe <module> 1 dump (and --require-wave64 for native wave64), built from main aa2988cc. I selected the GPU with LEONIDA_VK_DEVICE and the subgroup size with VKC_SUBGROUP_SIZE.

  • Wave32, on the two GPUs: 100 57005 200 57005 108 197 108 140.
  • Native wave64, on the AMD GPU: 100 100 57005 103 200 199 103 167.
  • Emulated wave64 on 32-wide subgroups, on the two GPUs: the same eight words as native wave64.

python -m pytest -q tests/semantic/test_ds_append.py gives 4 tests passed and 8 subtests passed. An empty wave32 EXEC mask also gives the reference result.
With the merge-base translator (aa2988c) and the tests of this head, pytest reports 10 failures and 2 passes.

This pull request does not change runtime C++ code. I did not make the disassembly again with LLVM, and I did not run the full CTest suite on this head. AI-assisted.

@controldebils

Copy link
Copy Markdown
Contributor

Tested 0fc8a15 (contains main aa2988c) on AMD Radeon RX 6900 XT (gfx1030, discrete RDNA2), AMD driver 26.11.2, Windows 11: pass, and the rule holds natively on a second RDNA2 chip.

Native, tools/hw_oracle raw kernels on gfx1030 (AMD clang from rocm-sdk-core 10.0.0): 120 cases, 120/120 match the rule in the description (the first value goes to every active lane, inactive lanes keep vD, the LDS word moves by the EXEC popcount, the offset field alone is the address):

  • wave32 EXEC 0xf0, 0x1, 0x80000000, 0xffffffff, 0x80000001; wave64 lanes {0, 32, 63}, lanes 32-35 only (low half empty), all 64, 0xf0, lane 63 only;
  • ds_append offset:0, ds_consume offset:8, two ds_append offset:4 in a row (the second returns the first's result), and ds_append then ds_consume at the same address;
  • each with M0 = 0xffffffff, 0x4 and 0x10000 (with s_nop after the write): M0 never moved the address.

GPU fixtures, scripts/run_gpu_tests.py --filter ds_append, runtime built from main f847748 (this PR changes no runtime code):

  • native: cs_w32_ds_append PASS 100 57005 200 57005 108 197 108 140; cs_w64_ds_append PASS 100 100 57005 103 200 199 103 167, run with --require-wave64 and compute subgroup size 64.
  • LEONIDA_W64_EMULATE=1: both PASS, same words.

CPU: tests/semantic/test_ds_append.py OK, regen_fixtures.py --check OK.

AI-assisted (Claude Code).

@Vonsek

Vonsek commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Tested 0fc8a15 (up to date with main aa2988c) on three setups, scripts/run_gpu_tests.py --filter ds_append with a build of the runtime tools:

  • NVIDIA GeForce RTX 5080 (driver 616.92, 32-wide subgroups, Windows 11): cs_w32_ds_append PASS (100 57005 200 57005 108 197 108 140); cs_w64_ds_append SKIP (no 64-wide compute subgroup).
  • AMD Radeon RDNA2 iGPU (gfx1036, driver 26.10.1, subgroup 32..64, Windows 11): cs_w32_ds_append PASS (same words) and cs_w64_ds_append PASS on native wave64 (100 100 57005 103 200 199 103 167).
  • RTX 5080 with LEONIDA_W64_EMULATE=1: both PASS, same words as the native wave64 run.

Also OK: python -m unittest discover -s tests/semantic -p "*append*" (4 tests) and tests/semantic/regen_fixtures.py --check. No findings.

AI-assisted.

@thatirishguyyy

Copy link
Copy Markdown

Tested 0fc8a15 on Windows 11, RX 9070 XT, AMD Software 26.11.2. Both stock fixtures translated and passed spirv-val. Native wave32, native wave64, and wave64 emulated on 32-wide subgroups each passed 3/3 runs. All eight expected output words matched every run, with zero Vulkan synchronization validation warnings or errors. Baseline aa2988c rejected both fixtures at ds_append. AI-assisted (Codex). No game files used.

@Zombins2

Copy link
Copy Markdown

AI-assisted. No game files used.

Tested 0fc8a15 with four waves per workgroup sharing one LDS counter: Ryzen 5 7600X iGPU (RDNA2 gfx1036, AMD 26.10.1), Windows 11 build 26200, llvm-mc 23.1.6, vk_compute_test built from the parent aa2988cc (MSVC 19.50, Ninja Release). Pass. This adds shared-LDS, multi-wave coverage alongside the existing single-wave fixtures. The PR's provided fixtures use one wave per workgroup.

Four local fixtures (not part of the PR), each with 4 waves in one workgroup on the same LDS words:

  • mw: one append per wave on lanes >= 8w (wave32) or >= 16w (wave64), so the first active lane is 0, 8, 16, 24 or 0, 16, 32, 48. In wave64, waves 2 and 3 have only their upper half on. One consume (offset:8) per wave runs on the odd lanes.
  • mwloop: the same append masks, 64 appends per wave in a loop. Each active lane records all 64 returned values; inactive trace entries keep the harness fill value.

Each run's whole VKC_DUMP buffer is checked. It must be exactly the expected length, and the requested subgroup size must be confirmed. In every region, the active lanes of a wave must agree, and the inactive lanes must be untouched. An append reserves [old, old + active-lane count); a consume reserves [old - active-lane count, old). For mw, the append ranges must tile 1000–1080 (wave32) or 1000–1160 (wave64), and the consume ranges 4936–5000 or 4872–5000, without gaps or overlaps. For mwloop, all 256 append ranges must tile 1000–6120 or 1000–11240, and each wave's returns must rise in program order. The final LDS words read after an s_barrier must be exact in every thread.

200 runs per mode on the AMD iGPU, all passing:

mode mw (LDS[0] / LDS[2]) mwloop (LDS[0])
wave32 (VKC_SUBGROUP_SIZE=32) 200/200, 1080 / 4936 200/200, 6120
wave64 native (--require-wave64) 200/200, 1160 / 4872 200/200, 11240
wave64 LEONIDA_W64_EMULATE=1 on 32-wide subgroups 200/200, 1160 / 4872 200/200, 11240

The validated loop histories are highly interleaved: sorted by returned counter value, 254 or 255 of the 255 adjacent append pairs belong to different waves. This describes observed counter order, rather than measuring simultaneous hardware execution. On the parent, all four fixtures fail to translate (ds_append untranslated).

Negative control: in a local copy of the translator, I replaced the one OpAtomicIAdd/OpAtomicISub with a plain OpLoad / OpIAdd / OpStore:

  • Local fixtures: rejected in 20/20 runs per mode, all six modes. For example, a retained wave32 mw dump returns 1000 to each wave while LDS[0] ends at 1016 instead of 1080. In mwloop, the recorded ranges overlap and the wave32 final was 3056–5448 instead of 6120.
  • PR's own fixtures: cs_w32_ds_append / cs_w64_ds_append still passed 5/5 per configuration, native and emulated.

So in this sample the one-wave fixtures don't detect a non-atomic implementation, and the multi-wave ones do. One of them might be worth adding to the suite.

cs_w32_ds_append_mw.asm (wave32, one append + one consume per wave; the others are similar)
; group: 128,1,1
; rsrc2: 0x40088
; FOUR wave32s of one workgroup append/consume on the same LDS counters. Thread 0 sets LDS[0] = 1000 and
; LDS[2] (byte 8) = 5000, then s_barrier.
;   append: wave w runs it on lanes >= 8*w (32, 24, 16, 8 lanes)  -> LDS[0] = 1080
;   consume offset:8: every wave on its odd lanes (16 each)        -> LDS[2] = 4936
; Each active lane gets its wave's value before the op; inactive lanes keep 0xdead. Sorted by returned value,
; append ranges [old, old+count) tile 1000..1080; consume ranges [old-count, old) tile 4936..5000.
; out[t] = append value, out[128+t] = consume value, out[256+t] / out[384+t] = LDS[0] / LDS[2] after a second s_barrier.
; Run: VKC_SUBGROUP_SIZE=32 VKC_OUT_WORDS=512 VKC_DUMP=<file> vk_compute_test cs_w32_ds_append_mw.spv 1 dump
; mattr: +wavefrontsize32,-wavefrontsize64
v_mbcnt_lo_u32_b32 v1, -1, 0
v_lshrrev_b32_e32 v2, 5, v0
v_mov_b32_e32 v8, 0xdead
v_mov_b32_e32 v9, 0xdead
v_mov_b32_e32 v3, 0
v_mov_b32_e32 v4, 1000
v_mov_b32_e32 v5, 5000
v_cmpx_eq_u32_e32 0, v0
ds_write_b32 v3, v4
ds_write_b32 v3, v5 offset:8
s_waitcnt lgkmcnt(0)
s_mov_b32 exec_lo, -1
s_barrier
v_lshlrev_b32_e32 v6, 3, v2
v_cmp_ge_u32_e32 vcc_lo, v1, v6
s_and_saveexec_b32 s10, vcc_lo
ds_append v8
s_waitcnt lgkmcnt(0)
s_mov_b32 exec_lo, s10
v_and_b32_e32 v7, 1, v1
v_cmp_eq_u32_e32 vcc_lo, 1, v7
s_and_saveexec_b32 s11, vcc_lo
ds_consume v9 offset:8
s_waitcnt lgkmcnt(0)
s_mov_b32 exec_lo, s11
s_barrier
ds_read_b32 v10, v3
ds_read_b32 v11, v3 offset:8
s_waitcnt lgkmcnt(0)
v_lshlrev_b32_e32 v12, 2, v0
buffer_store_dword v8, v12, s[0:3], 0 offen
v_add_nc_u32_e32 v13, 0x200, v12
buffer_store_dword v9, v13, s[0:3], 0 offen
v_add_nc_u32_e32 v13, 0x400, v12
buffer_store_dword v10, v13, s[0:3], 0 offen
v_add_nc_u32_e32 v13, 0x600, v12
buffer_store_dword v11, v13, s[0:3], 0 offen
s_endpgm

Not run: NVIDIA, RDNA1, the gds form, more than one workgroup, and the full suite.

@lcfcntr009

Copy link
Copy Markdown

Tested 0fc8a15 (contains main aa2988c) on AMD Radeon RX 6650 XT (gfx1032, discrete RDNA2, subgroup 32..64), driver 32.0.21045.29003, Windows 11, MSVC 19.44, llvm-mc 21.1.8 (built with scripts/build_llvm_mc.ps1): pass.

  • python -I scripts/run_gpu_tests.py --filter ds_append: cs_w32_ds_append PASS (100 57005 200 57005 108 197 108 140), cs_w64_ds_append PASS, native wave64 (100 100 57005 103 200 199 103 167).
  • Same with LEONIDA_W64_EMULATE=1: both PASS, same words.
  • tests/semantic/test_ds_append.py: 4/4 OK.
  • Full run_gpu_tests.py on the PR branch: 197/212 passed, 15 skipped, 0 failed (main aa2988c on the same machine: 195/210, same 15 skips).

The PR changes no C++, so the test programs were built from aa2988c. No game files used. AI-assisted.

@slopradish slopradish left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CPU verification: checked out pull/3976/head in an isolated worktree and ran pytest -q tests/semantic/test_ds_append.py: 4 passed, 8 subtests passed.

The implementation matches the documented hardware behaviour: one LDS add/subtract per wave by the first active lane, with the old value broadcast to the other active lanes via subgroup shuffle (or an exchange round under wave64 emulation). The semantic test checks the count, the performing lane, and that inactive lanes keep vD — those are exactly the parts most likely to be wrong in a first implementation.

The change also updates the contract table, GPU fixtures, checker word lists and GPU runner registration together, so the CPU test won't silently drift from the hardware fixture. I could not run the GPU fixtures here, so the RDNA2/RTX measurements remain the author's; the CPU path is verified.

No game files or keys involved.

@jimokulou2-ops

Copy link
Copy Markdown

Tested 84eec83 merged onto main 98fa503 (clean merge) on AMD Radeon RX 9070 XT (RDNA4), AMD 26.11.2 (32.0.32029.2010, LLPC, Vulkan 1.4.359), Windows 11 Pro 26300: pass.

  • Test programs built from main with llvm-mingw (the PR doesn't touch runtime/), llvm-mc 23.1.6 (conda-forge), Vulkan SDK 1.4.363.0, LEONIDA_VK_DEVICE=9070.
  • scripts/run_gpu_tests.py, full suite: 204/219 passed, 15 skipped, 0 failed; 223 shaders translated, 223 pass spirv-val. main 98fa503 on the same machine: 202/217, the same 15 skipped. The only difference is the two new tests.
  • cs_w32_ds_append: PASS 100 57005 200 57005 108 197 108 140
  • cs_w64_ds_append (native wave64): PASS 100 100 57005 103 200 199 103 167. Both match the RDNA2 words in the description.
  • With LEONIDA_W64_EMULATE=1, --filter ds_append: 2/2 passed.
  • tests/semantic/test_ds_append.py: 4 OK.

No game files. AI-assisted (Claude Code).

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants