Skip to content

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

Closed
evilclanker9000 wants to merge 2 commits into
ProjectLeonida:mainfrom
evilclanker9000:claude/ds-append
Closed

evilclanker9000 wants to merge 2 commits into
ProjectLeonida:mainfrom
evilclanker9000:claude/ds-append

Conversation

@evilclanker9000

@evilclanker9000 evilclanker9000 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).

@evilclanker9000

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).

@kapitche

Copy link
Copy Markdown
Collaborator

Tested 84eec83 merged with main d974c88 on Linux (WSL2), Python 3.12.16: pass

Semantic tests only, GPU fixtures and check.py/run_gpu_tests.py not run (no GPU). tests/semantic/test_ds_append.py 4 ok; test_fixtures.py and test_rule_gpu_fixtures.py ok; regen_fixtures.py --check exits 0; all tests/test_docs_*.py pass with the docs/design.md row. I read the ds_append translator, the expected-word tables in check.py and run_gpu_tests.py (identical for both) and the two new test fixtures; nothing wrong found. The expected GPU words themselves are not verified here.

Negative control: tools/rdna2spirv.py checked out from main, test_ds_append fails (1 failure, 9 errors), restored afterwards.

@Hiiva

Hiiva commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

checked: #3976 at 84eec83, composed onto main 427a7ec | on: Windows x64, Python 3.14.6, SPIRV-Tools v2026.4.rc2, CPU only | result: a guarded multi-wave emulation case hits AssertionError: wave64 emulation: unexpected OpControlBarrier in sync-free multi-wave block. The relevant translator/planner files are unchanged through main 867d922.

ds_append and ds_consume are already in _FORBIDDEN_FOLD_OPS, but neither is in _ALWAYS_BARRIER_OPS. The structured-emulation planner therefore treats a guarded block containing either operation as sync-free, although its emission performs exchange rounds.

This complete synthetic reproduction uses the existing test helper, from the repo root with the PR applied:

import sys
sys.path.insert(0, "tests/semantic")
from test_wave64_emulate_opt import _translate_encoded

insns = [
    ("v_cmp_lt_u32_e64 s[6:7], v0, 32", 8),
    ("s_and_saveexec_b64 s[10:11], s[6:7]", 4),
    ("s_cbranch_execz 2", 4),
    ("ds_append v8", 8),
    ("s_mov_b64 exec, -1", 4),
    ("s_endpgm", 4),
]
_translate_encoded("cs_guarded_ds", insns, (128, 1, 1), emulate=True)

Changing the operation to ds_consume reproduces the same assertion. A fuller fixture with shared-LDS initialization and output stores fails for both operations at 128 and 256 threads, while both 64-thread controls translate.

A local proof adding both names to _ALWAYS_BARRIER_OPS fixes all four failing cases. All six modules pass spirv-val --target-env vulkan1.2. All 12 forward/reverse strict-workgroup executions match the independent expected return values and shared LDS: the fixture starts its counter at 512 and activates only the first 32 invocations; final counters are 544 for append and 480 for consume. For these CPU checks, the strict interpreter was extended in memory with the standard 32-bit OpAtomicISub rule, which it currently lacks. Repository interpreter files were unchanged.

The integration also contains #6446 and #7489; these synthetic inputs use neither TG_SIZE_EN nor scalar VCC addressing. No game inputs or GPU execution were used for this report.

@MarcPique

Copy link
Copy Markdown

I integrated this translator change into a recent boot stack (ladder-frame@a1751a9f0). It translated the last of the 36 compute programs missing in our Windows RTX 4090 boot, after #6446, #8842 and #8448 covered the other 35. The module passed spirv-val.

One rebase detail for the CPU fixture. On this base, the SPIR-V interpreter shares Workgroup memory. The original test assumes a private LDS copy per invocation, so seven subtests fail on the final LDS word even though the returned per-lane value matches. I added s_barrier after initializing the LDS word and after restoring full EXEC, before the read. The expected final LDS result is then 100 + active_count for append or 100 - active_count for consume in every lane. The expected instruction return remains 100 for active lanes and the sentinel for inactive lanes.

With those fixture-only changes, all four tests in test_ds_append.py passed on CPython 3.12. This was a local integration and interpreter test. No game data in this report. AI-assisted.

@Rohan03x

Copy link
Copy Markdown

Validated against the real retail compute corpus

Applied this PR's translator change (tools/rdna2spirv.py, your 0db7fed5..84eec830) onto current main
(867d922f0) as a clean 3-way merge, then translated the 2684 distinct compute programs from the retail Xbox
1.0.2.1 pipeline library (shaders.rpf, compute stage), sharded 32× with spirv-val.

translated pass spirv-val invalid regressions¹
main baseline 1778 (66.2%) 1778 0 —
+ this PR 1804 (67.2%) 1804 0 0

+26 programs, every one also passing spirv-val; 0 translated-but-invalid; all 1778 programs valid on
main stay valid
(exact index.json key-set diff).

ds_append is the sole first-blocker for 28 programs here; all 28 clear it, and 2 then hit a secondary gap
(s_bitreplicate_b64_b32 on a lane mask, an s_load_dwordx4 … slist vcc form), netting +26.

Your tests/semantic/test_ds_append.py passes (4 tests).

Heads-up: GitHub marks this PR CONFLICTING, but that's in non-translator files — the tools/rdna2spirv.py change
3-way-merges cleanly onto today's main and runs with no translator errors (the branch is ~2305 commits behind).
A rebase should make it mergeable; the translator change itself is sound on the real corpus.

Env: Windows 11 x64, Python 3.12.10, SPIRV-Tools v2026.4. GPU: none available — translate + spirv-val only, not
vk_pipeline_check. AI-assisted.

@autogenes-sudo

Copy link
Copy Markdown

Tested 84eec8302 on Windows 11, CPython 3.13 (translator only, no GPU needed): --wave64-emulate aborts the whole translation run on a ds_append/ds_consume inside an EXEC-guarded block of a multi-wave compute group.

AssertionError: wave64 emulation: unexpected OpControlBarrier in sync-free multi-wave block

It's an assert, not Unsupported, so rdna2spirv.py stops at that shader and make_game.py's shaders stage fails. --wave64-emulate is the default on NVIDIA and Intel.

Cause. Under emulation, ds_append gets the old value to the other lanes through x_round (an exchange round, so an OpControlBarrier). But tools/wave64_emulate_opt.py doesn't list ds_append/ds_consume among the ops that always need a barrier, so the planner can put that block in sync_free_blocks, and translate() then asserts. They're already in the forbidden-fold list next to it, but not in the always-barrier list.

Fix, one line. On this branch's wave64_emulate_opt.py it goes in the always-barrier check (if op in ("s_barrier", "v_readlane_b32", ... "s_wqm_b32", "s_wqm_b64"): return True). On current main that's the _ALWAYS_BARRIER_OPS frozenset (#6895):

     "ds_bpermute_b32", "ds_permute_b32", "ds_swizzle_b32",
+    "ds_append", "ds_consume",
     "s_wqm_b32", "s_wqm_b64",

Reproducer (hand-written; wave64 header with threads=(N,1,1), built with test_active_cache.py's _make_fssm_cs / _encode_listing, rdna2spirv.WAVE64_EMULATE = True):

v_mov_b32 v8, 0
v_cmp_gt_u32 vcc, 16, v0
s_and_saveexec_b64 s[6:7], vcc
s_cbranch_execz BB_skip
ds_append v8
BB_skip:
s_or_b64 exec, exec, s[6:7]
v_lshlrev_b32 v21, 2, v0
buffer_store_dword v8, v21, s[0:3], 0 offen
s_endpgm
group size straight-line ds_append guarded ds_append (above) guarded + VALU around it two guards (ds_append then ds_consume)
64 (one wave) ok ok ok ok
128 ok assert assert assert
256 ok assert assert assert

With the line added, all 12 translate. tests/semantic/test_ds_append.py and tests/semantic/test_wave64_emulate_opt.py still pass. A test with the 128-thread guarded case would pin it.

On the game. Over the 2,684 compute programs of the pipeline library (#7529's step; the ones device slot 67 creates at boot), with #6446 also applied, exactly 1 program hits this. On the run without #6446 it's rejected earlier by the thread-group SGPR check, which is why it doesn't show up yet. With the fix, it translates and passes spirv-val. Counts only; the program isn't quoted.

AI-assisted (Claude Code).

@gautch29

gautch29 commented Nov 23, 2026 •

Copy link
Copy Markdown

I independently checked @autogenes-sudo's guarded multi-wave regression and suggested fix on forge main@de3b04df0 (contains #3976 as 8b24c7f8b and #6446). The affected files match forge 1812e0bd1 too.

  • A synthetic 128-thread compute workgroup has only lanes 0–15 of wave 0 take the guarded ds_append or ds_consume. On the forge base, both cases hit unexpected OpControlBarrier in sync-free multi-wave block during --wave64-emulate translation.
  • With both instructions classified as exchange operations, the semantic test reads the correct old counter, untouched inactive lanes, and the correct final shared counter (116 for append, 84 for consume). 60 focused tests and 179 subtests pass.
  • I assembled both programs with LLVM 23.1.6 and ran them through the actual CLI with --wave64-emulate. Both modules passed spirv-val --target-env vulkan1.2. On Apple M3 Pro, macOS 27.0, MoltenVK 1.4.2, both dispatches returned all 256 expected words, 512 total.

A follow-up GPU check used 256 threads and 48 active lanes across both 32-lane halves of the first wave. Each op matched all 512 output words. The full semantic suite ran 454 files and 6,000 tests with the patch. The unpatched and patched runs had the same failures in test_image_atomics_guard and test_image_store_d16.

The scheduler change and regression test are below. All programs were synthetic. AI-assisted with Codex.

Patch for forge main
diff --git a/tests/semantic/test_wave64_emulate_opt.py b/tests/semantic/test_wave64_emulate_opt.py
index 3ebbe02b7..3c15faaa8 100644
--- a/tests/semantic/test_wave64_emulate_opt.py
+++ b/tests/semantic/test_wave64_emulate_opt.py
@@ -411,7 +411,7 @@ def run_emulated_workgroup(spvasm, local_size, push_constants=None, memory=None,
                     memory[p[1]] = v & MASK32
                 else:
                     p[0][p[1]] = v
-            elif op in ("OpAtomicOr", "OpAtomicIAdd", "OpAtomicAnd", "OpAtomicXor", "OpAtomicExchange"):
+            elif op in ("OpAtomicOr", "OpAtomicIAdd", "OpAtomicISub", "OpAtomicAnd", "OpAtomicXor", "OpAtomicExchange"):
                 p = ptrs[a[1]]
                 old = memory.get(p[1], 0) if p[0] == "psb" else p[0][p[1]]
                 v = vals[a[4]] & MASK32
@@ -419,6 +419,8 @@ def run_emulated_workgroup(spvasm, local_size, push_constants=None, memory=None,
                     new = old | v
                 elif op == "OpAtomicIAdd":
                     new = (old + v) & MASK32
+                elif op == "OpAtomicISub":
+                    new = (old - v) & MASK32
                 elif op == "OpAtomicAnd":
                     new = old & v
                 elif op == "OpAtomicXor":
@@ -1283,6 +1285,36 @@ class StructuredEmulationTests(unittest.TestCase):
                     for l in range(threads):
                         self.assertEqual(mem[512 + l * 4], v4[l] & 0xFFFFFFFF, f"lane {l} rev={rev}")
 
+    def test_guarded_lds_counter_exchange_in_a_multiwave_workgroup(self):
+        # Lane 0 initializes LDS. Lanes 0..15 enter the guard and the second wave skips it.
+        for op, final in (("ds_append", 116), ("ds_consume", 84)):
+            with self.subTest(op=op):
+                insns = [
+                    ("v_mov_b32 v8, 0xdead", 8),
+                    ("v_mov_b32 v1, 0", 4),
+                    ("v_mov_b32 v2, 100", 8),
+                    ("v_cmp_eq_u32 vcc, 0, v0", 4),
+                    ("s_and_saveexec_b64 s[4:5], vcc", 4),
+                    ("ds_write_b32 v1, v2", 8),
+                    ("s_or_b64 exec, exec, s[4:5]", 4),
+                    ("s_barrier", 4),
+                    ("v_cmp_gt_u32 vcc, 16, v0", 4),
+                    ("s_and_saveexec_b64 s[6:7], vcc", 4),
+                    ("s_cbranch_execz 2", 4),
+                    (f"{op} v8", 8),
+                    ("s_or_b64 exec, exec, s[6:7]", 4),
+                    ("s_barrier", 4),
+                    ("ds_read_b32 v9, v1", 8),
+                    ("v_lshlrev_b32 v21, 3, v0", 4),
+                    ("buffer_store_dwordx2 v[8:9], v21, s[0:3], 0 offen", 8),
+                    ("s_endpgm", 4),
+                ]
+                size = (128, 1, 1)
+                spv, _ = _translate_encoded("cs_guarded_lds_exchange", insns, size, rsrc2=0x40088)
+                mem, _ = run_emulated_workgroup(spv, size, push_constants=VSHARP + [0] * 28)
+                self.assertEqual([(mem[8 * lane], mem[8 * lane + 4]) for lane in range(size[0])],
+                                 [(100 if lane < 16 else 0xDEAD, final) for lane in range(size[0])])
+
     def test_mask_flbit_is_synchronized_in_a_branch_taken_by_only_one_wave(self):
         # One wave takes the SCC branch body. Its bit scan still needs a workgroup exchange.
         for op, ext_inst in (("s_flbit_i32_b32", "FindUMsb"), ("s_flbit_i32", "FindSMsb")):
diff --git a/tools/wave64_emulate_opt.py b/tools/wave64_emulate_opt.py
index f917c9c4d..e7959f8f1 100644
--- a/tools/wave64_emulate_opt.py
+++ b/tools/wave64_emulate_opt.py
@@ -138,6 +138,7 @@ _ALWAYS_BARRIER_OPS = frozenset({
     "s_barrier", "v_readlane_b32", "v_readfirstlane_b32",
     "v_permlane16_b32", "v_permlanex16_b32",
     "ds_bpermute_b32", "ds_permute_b32", "ds_swizzle_b32",
+    "ds_append", "ds_consume",  # The old LDS counter is broadcast across the wave.
     "s_wqm_b32", "s_wqm_b64",
 })
 

@Impre-visible

Copy link
Copy Markdown

Tested 84eec83 on the pipeline library's compute programs (own 1.0.2.1 copy; tools/shader_library.py --stages cs --disasm --carve, 2,684 programs) with --wave64-emulate, Arch Linux, Python 3.14.10, llvm-mc 23.1.5: the translator stops on an assertion instead of refusing the program.

So on this corpus the once-per-wave ds_append / ds_consume path meets wave64 emulation's region folding in at least two places that only assert. Without --wave64-emulate I have not run it. I can't post the programs; the 29 ds_append refusals above are the ones to look at, and they are compute programs that get wave64 emulation on a 32-wide device.

Not run: the PR's own tests, and a reduced synthetic reproducer (I have not built one yet).

AI-assisted run (Claude). Counts and the tool's own messages only; nothing from the game leaves this machine.

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.