Skip to content

Translator: --lds-wave-order (opt-in) orders LDS exchange between lanes of one wave with subgroup barriers - #1608

Closed
AlxcBTW wants to merge 5 commits into
ItsNotRexed:mainfrom
AlxcBTW:lds-wave-order-v2
Closed

AlxcBTW wants to merge 5 commits into
ItsNotRexed:mainfrom
AlxcBTW:lds-wave-order-v2

Conversation

@AlxcBTW

@AlxcBTW AlxcBTW commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Rebased onto main after #1541 (SHADER-10) landed: one commit, replacing #1564, which conflicted in tools/rdna2spirv.py. I rebased it rather than merging main in, as asked for that file. The conflicts were all "keep both": options, globals and index fields. Re-run on the rebased commit: test_translate/test_shader_checks/test_wave64_halves_fixtures/test_tool_stdout_encoding 492 passed, 2 skipped; semantic test_lds_wave_order 31 OK and test_wave64_halves 57 OK.

Carried over from #1564: @Itschotsch tested 1f9b84a on an RTX 4090 (Linux, 595.99.02): pass, with the option both off and on, and 0 mismatches in cs_lds_wave_exchange. Thanks.

What and why

Adds an opt-in translator option, --lds-wave-order off|on. The default is off, and output is unchanged. LEONIDA_LDS_WAVE_ORDER=1 turns it on for runtime/test/shaders/build.py. Claimed in #1218.

The problem: RDNA2 runs a wave in lockstep with an in-order LDS queue. Shaders therefore write LDS in one lane and read it in another with only s_waitcnt lgkmcnt(0), or nothing, in between. Reductions, prefix sums and transposes all do this. SPIR-V and the Vulkan memory model order nothing between two invocations without a barrier or an atomic. A compiler may reorder the accesses, and a GPU with independent thread scheduling may have the lanes of a subgroup at different instructions. Prior art, facts only: shadPS4 #5285 saw RADV reorder such reads.

What on does: a native compute or mesh shader (not --wave64-emulate) gets an OpControlBarrier Subgroup Subgroup AcquireRelease|WorkgroupMemory before an LDS access that conflicts with an earlier, still unordered one. "Unordered" means no s_barrier and no earlier subgroup barrier sits between them. The conflicting pairs are RAW, WAR, WAW, and atomic vs plain; atomic after atomic needs no barrier. The unordered set is a forward data flow over the blocks, so a loop-carried pair and a join are handled.

Where it emits nothing:

  • shaders without LDS;
  • ds_bpermute, ds_permute and ds_swizzle;
  • a read after a read;
  • after an s_barrier;
  • the emulate path;
  • blocks behind an SCC branch that may split a wave64 subgroup (scc_logic takes a lane's own bit for a 32-bit op on one half of a lane mask).

With on, the index records lds_wave_order, the number of barriers emitted. docs/design.md has the row, with the spec text quoted.

Tested

  • GPU tests: cs_lds_wave_exchange (wave32) and cs_w64x_lds_wave_exchange (wave64, including the other half) run 512 groups × 256 rounds of four dependent exchanges; every thread must store 0. The check.py lab on an RTX 5070 Laptop and a Radeon 880M passes on both, with the default and with LEONIDA_LDS_WAVE_ORDER=1.
  • lds_fuzz (seed 1, 100 programs, wave both), native and --emulate, on both GPUs: no mismatch.
  • tests/semantic/test_lds_wave_order.py (31 tests): barrier placement, no barrier where none is needed, default off unchanged vs on, the metadata, a loop-carried pair, a join, and the wave64 SCC exception.
  • test_translate + test_shader_checks + tests/semantic: 1705 passed, 2 skipped (ms_f16_pack). The nested-barrier test ran and wasn't skipped. Tools were llvm-mc and spirv-val. This run was on the pre-rebase base. After the rebase onto current main, the LDS, barrier-round and shader-check tests pass (88).
  • Byte identity: with off, all 137 test shaders are byte-identical to main.

Honest limits

  • The race did not reproduce in 100 runs per variant on an RTX 5070 Laptop and a Radeon 880M (wave32 and native wave64). The SPIR-V is racy by the memory model, but these drivers kept program order. The tests pass with and without the option, so they show no benefit, only that it is harmless on these GPUs. There was no measurable cost either.

  • Intel is untested. Nested barriers lose the device on Xe-LPG; see the "Workgroup barriers" row.

  • The spec is ambiguous.

    • SPIR-V 1.6 r2 says a Subgroup-scope OpControlBarrier in non-uniform control flow is "defined by the client API". The current text dropped that sentence.
    • I found no explicit Vulkan rule for a plain Subgroup barrier in divergent flow.
    • GLSL_EXT_split_barrier says Subgroup-scope barriers need not be in uniform control flow under Vulkan.

    Hence the wave64 SCC exception and the opt-in default. Quotes and links are in docs/design.md.

  • Not done: skipping the barrier when a lane provably reads only its own slot.

Checklist

  • docs/design.md row (rule, why, source, how tested)
  • No game data, no keys
  • Default output byte-identical

AI-assisted (Claude Code).

🤖 Generated with Claude Code

… wave with subgroup barriers (opt-in)

RDNA2 runs a wave in lockstep, so shaders exchange LDS data between lanes with
only s_waitcnt in between; SPIR-V orders nothing between invocations without a
barrier. With --lds-wave-order on (default off, output unchanged), conflicting
LDS accesses that no s_barrier orders get an OpControlBarrier at Subgroup scope
(AcquireRelease|WorkgroupMemory), found by data flow over the blocks; skipped
behind SCC branches that may split a wave64 subgroup and in --wave64-emulate.
Adds GPU tests cs_lds_wave_exchange / cs_w64x_lds_wave_exchange, semantic tests
and the design.md row.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@dcastell1 dcastell1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hardware and CPU run on this branch (RTX 5090, 621.24, Windows 11, LEONIDA_W64_EMULATE=1, all six vk_*_test programs rebuilt from the PR tree):

PASS  cs_lds_wave_exchange      512 groups x 64: 0 mismatches
PASS  cs_w64x_lds_wave_exchange 512 groups x 64: 0 mismatches
147/147 passed

CPU side: tests/semantic → 1369 tests OK (2 skips) including the 31 in test_lds_wave_order.py; tests/test_shader_checks.py → 46 passed, 181 subtests; tests/test_translate.py skips without a build fixture here.

So the opt-in orders the LDS exchange on a 32-wide subgroup without touching the default path (main's 146 + this one). The --lds-wave-order flag being opt-in is the right call given the extra subgroup barriers.

@Impre-visible

Copy link
Copy Markdown

Tested b913e79 on NVIDIA GeForce GTX 1660 (Turing, TU116, subgroup 32..32), driver 620.73.04 (proprietary, Vulkan 1.4.357), Arch Linux (kernel 6.18): pass. This is a 32-wide check for the "AMD + 32-wide GPU" list.

Native build of runtime/ from this head (Ninja, RelWithDebInfo, clang 23.1.5), llvm-mc 23.1.5, SPIRV-Tools v2026.4.

  • tests/semantic: 1369 OK
  • build.py: 159 translated, 159 pass spirv-val in all four configurations below
check.py configuration result cs_lds_wave_exchange cs_w64x_lds_wave_exchange
default 105 of 139 pass, 34 skipped ok SKIP (no 64-wide subgroup)
LEONIDA_LDS_WAVE_ORDER=1 105 of 139 pass, 34 skipped ok SKIP (no 64-wide subgroup)
LEONIDA_W64_EMULATE=1 136 of 139 pass, 3 skipped ok ok
LEONIDA_LDS_WAVE_ORDER=1 LEONIDA_W64_EMULATE=1 136 of 139 pass, 3 skipped ok ok
  • run_gpu_tests.py: 152/152 translated and pass spirv-val; 116/147 passed, 31 skipped (wave64), 0 failed; cs_lds_wave_exchange 512 groups x 64: 0 mismatches
  • LEONIDA_W64_EMULATE=1 run_gpu_tests.py: 147/147 passed; both exchange tests 512 groups x 64: 0 mismatches

0 failures in every run.

AI-assisted run (Claude). Counts only, no game files involved.

@towade

towade commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested b913e79 on AMD Radeon RX 9070 XT (RDNA4, subgroup 32..64), AMD 32.0.32029.2010 (Vulkan driver 2.0.416), Windows 11 Pro 10.0.26300, MSVC 19.44.35228, llvm-mc 21.1.8 (WSL), SPIRV-Tools v2026.4: pass. This is the AMD half of the "AMD + 32-wide GPU" check, next to the GTX 1660 above.

main 41f4482 this PR
build.py (native and LEONIDA_W64_EMULATE=1) 157/157 translated, 157 pass spirv-val 159/159, 159 pass spirv-val
check.py (native and emulated) 134 of 137, 3 skipped 136 of 139, 3 skipped
run_gpu_tests.py (native and emulated) 145/145 147/147: cs_lds_wave_exchange and cs_w64x_lds_wave_exchange both 512 groups x 64: 0 mismatches
ctest 89/89 89/89
pytest on the PR's tests (test_lds_wave_order.py, test_shader_checks.py, test_translate.py) — 516 passed, 2 skipped

The same results came from its predecessor #1564 (1f9b84a) on main 99f9989. The skips are the three mesh fixtures build.py doesn't build.

AI-assisted (Claude Code); no game files.

@Sanonymous01

Copy link
Copy Markdown
Contributor

Carried over from #1564: I tested its head 1f9b84a, the commit this PR rebases. That's the same 12 files and line counts, and git range-diff shows only the conflict resolution in tools/rdna2spirv.py changed.

Tested 1f9b84a on Windows 11, on two GPUs: pass. Both are new architecture/OS points for this PR: native RDNA2 wave64 on the AMD Windows driver, and Blackwell on Windows.

AMD:     RDNA2 integrated GPU (gfx1036), AMD Windows driver 32.0.21044.64 (Vulkan, LLPC), subgroup 32..64
NVIDIA:  Blackwell (desktop), NVIDIA 621.24, subgroup 32
Tools:   runtime rebuilt at this head with MSVC, Vulkan SDK 1.4.363.0, llvm-mc from AMD LLVM 23.0.0git
Run AMD gfx1036 NVIDIA Blackwell
run_gpu_tests.py --filter lds_wave_exchange 2/2 pass, 512 groups × 64, 0 mismatches each cs_lds_wave_exchange pass; cs_w64x_… skipped (no 64-wide subgroups)
same with LEONIDA_LDS_WAVE_ORDER=1 2/2 pass same as above
run_gpu_tests.py (full) 143/143 112/143, 31 skipped (native wave64)
full, LEONIDA_LDS_WAVE_ORDER=1 143/143 112/143, 31 skipped
lds_fuzz --seed 1 --programs 100 --wave both 200/200 100/100, 100 skipped (wave64)
lds_fuzz … --emulate 142/142 that ran match, 58 harness errors 200/200
  • The 58 AMD harness errors aren't from this PR. main at the PR's base (99f9989), rebuilt, gives the same 142/142 + 58 on this GPU. It's the vk_compute_test crash in the AMD driver's pipeline compile under --emulate, reported for RDNA2 in lds_fuzz --emulate: vk_compute_test crashes inside amdvlk64.dll on 31/50 wave64 programs (RDNA2 iGPU, AMD 32.0.21042.62) #1280.
  • The race didn't reproduce here either. The native wave64 exchange passes with and without the barriers on a 64-wide RDNA2 subgroup, so this is harmlessness data, as on the 880M.
  • Unit tests:
    • python -m unittest discover -s tests/semantic -p test_lds_wave_order.py: 31 OK.
    • Full tests/semantic: 1278 OK, 2 skipped.
    • tests/test_shader_checks.py: 40 OK.

AI-assisted (Claude Code).

@josevictorp

Copy link
Copy Markdown
Contributor

Tested b913e79 on Linux 6.8 (Python 3.12.3, CPU only, semantic interpreter; no llvm-mc, spirv-val or GPU here, so the GPU tests and the byte-identity claim were not re-run).

  • tests/semantic/test_lds_wave_order.py: 31 tests OK. Whole tests/semantic: 1369 tests OK (2 skipped).
  • Negative control: with the base tools/rdna2spirv.py, all 31 fail.
  • The tools/rdna2spirv.py and tests/semantic part of the diff applies cleanly on current main (a0acd33) and the 31 tests pass there. The branch itself now conflicts in scripts/run_gpu_tests.py and tests/test_shader_checks.py.
  • Extra: 400 random straight-line programs of ds_read_b32, ds_write_b32, ds_add_u32, ds_wrxchg_rtn_b32, ds_cmpst_rtn_b32, s_barrier, ds_bpermute_b32 and a v_mov_b32 filler, 2 to 14 instructions each. For each I read the LDS accesses and barriers back from the output and checked two things by the rule in the description: no conflicting pair is left without a barrier between, and no subgroup barrier is emitted with nothing unordered before it. 400 of 400 hold (1231 barriers).

Two things I found, neither a problem with the default (off).

1. The two-address exchanges are not classified, so they get no barrier. lds_access_kind turns ds_wrxchg2_rtn_b32 into ds_wrxchg2_b32, which is not in _LDS_ATOMIC_NAMES, and returns None: the access is neither ordered nor remembered. Translated with on:

program subgroup barriers LDS events in the output
ds_write_b32 ; ds_wrxchg_rtn_b32 (control) 1 w, sub, a
ds_write_b32 ; ds_wrxchg2_rtn_b32 0 w, a, a
ds_write_b32 ; ds_wrxchg2st64_rtn_b32 0 w, a, a
ds_wrxchg2_rtn_b32 ; ds_read_b32 0 a, a, r

On current main the same holds for the forms #1605 added: ds_wrxchg_rtn_b64, ds_wrxchg2_rtn_b64 and ds_wrxchg2st64_rtn_b64 (ds_write_b32 ; ds_wrxchg_rtn_b64 gives w, a, a, no barrier). Listing over every "ds_*" name in the file, the ones that classify as None are ds_nop, the permute/swizzle group, and these five. A test that walks the names the DS handler accepts would catch the next one too.

2. "Atomic after atomic: the memory model orders those" holds per access, not for the program order. Two atomics on one dword cannot race, but nothing says lane 1's first instruction runs before lane 0's second. Two lanes, one dword starting at 0, ds_add_u32 with 5 then ds_max_u32 with 7:

  • in lockstep, as on the hardware: (0 + 5 + 5), then max with 7 twice = 10
  • lane 0 runs both before lane 1 starts: max(0 + 5, 7) = 7, then + 5 = 12, then max with 7 = 12

ds_add_u32 ; ds_max_u32 and ds_add_u32 ; ds_wrxchg_rtn_b32 both translate with 0 barriers. Two instructions of the same commuting kind (add after add, the case test_atomic_after_atomic_needs_none uses) are fine either way; a pair that does not commute is the same kind of order the rule exists for. Whether to bar every atomic after a different atomic, or to write the limit down in the design row, is for the author and coordinator.

AI-assisted review.

@bobcatchris

Copy link
Copy Markdown
Contributor

Tested b913e79ea8120e6eaf72c20cc3df81605edaa896 on AMD Radeon Pro V540 (RDNA1, Navi 12, PCI 1002:7360, RADV Mesa 25.2.8-0ubuntu0.24.04.4, Ubuntu 24.04.4, kernel 7.0.0-31-generic, Vulkan device 1.4.318, subgroup 32..64): pass. Independent RDNA1/RADV data point (the existing GPU result is NVIDIA-only).

  • Command: DRI_PRIME=pci-0000_1c_00_0! python -I scripts/run_gpu_tests.py --filter cs_lds_wave_exchange --build runtime/build/linux: 1/1 passed, 512 groups x 64: 0 mismatches.
  • Same command with LEONIDA_LDS_WAVE_ORDER=1 (the opt-in path): 1/1 passed, identical 0 mismatches.
  • Command: LEONIDA_LDS_WAVE_ORDER=1 python -I scripts/run_gpu_tests.py --build runtime/build/linux: 134/147 passed, 12 skipped, 1 failed (152 shaders translated, 152 pass spirv-val). Clean upstream/main 41f44826 control: 132/145, 12 skipped, 1 failed; the extra tests come from this PR/its stack, so no regression.
  • The one failure is the pre-existing cs_w32_salu64 RDNA1/RADV issue at Coordinating who works on what (7) (moved to #1745) #1548 (comment), not related to LDS wave ordering.

Tools: llvm-mc 21.1.8 with AMDGPU target, SPIRV-Tools 2026.1, GCC 13.3.0, Python 3.12.3. No code was edited or pushed; only synthetic fixtures were used.

AI-assisted.

# Conflicts:
#	scripts/run_gpu_tests.py
#	tests/test_shader_checks.py
# Conflicts:
#	tests/test_shader_checks.py
@crastinator

Copy link
Copy Markdown
Contributor

Tested b913e79 merged into main 41f44826 (clean merge) on AMD Radeon RX 6700 XT (Navi 22, gfx1031, subgroup 32..64), RADV Mesa 26.3.1, CachyOS, Linux 7.3.5, GCC 16.2.1, llvm-mc 23.1.5: pass.

  • New tests pass natively: cs_lds_wave_exchange (512 groups x 64: 0 mismatches); cs_w64x_lds_wave_exchange (512 groups x 64: 0 mismatches).
  • Whole run_gpu_tests.py: 147/147 native, 147/147 with LEONIDA_W64_EMULATE=1 (main alone: 145).
  • tests/semantic/test_lds_wave_order.py, tests/test_shader_checks.py, tests/test_translate.py: 516 passed, 2 skipped, 181 subtests passed.

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

# Conflicts:
#	tests/test_shader_checks.py
@zneel

zneel commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Tested 871117f7 (current head; the earlier runs here were at b913e79e) on AMD Radeon RX 9070 XT (RDNA4, subgroup 32..64), AMD 26.11.2, Windows 11 Pro 10.0.26300, AMD Ryzen 7 9800X3D CPU (64 GB RAM): pass.

Runtime and test programs built from this head (MSVC 14.51, Ninja, RelWithDebInfo), native Windows llvm-mc 21.1.8, Vulkan SDK 1.4.363.0, Python 3.12.10. No game files.

run result
run_gpu_tests.py (whole suite, native wave64) 169/182, 0 failed (main 4eb6155d: 167/180); both exchange tests 512 groups x 64, 0 mismatches
same with LEONIDA_W64_EMULATE=1 169/182, 0 failed; both exchange tests 0 mismatches
build.py + check.py, cs_lds_wave_exchange and cs_w64x_lds_wave_exchange, wave order off and LEONIDA_LDS_WAVE_ORDER=1, native and emulated all four combinations ok
ctest 116/117; only d3d12_draw fails, as on main
pytest tests/test_shader_checks.py tests/test_translate.py 564 passed, 2 skipped
unittest discover -s tests/semantic 1702 OK

With on, the native wave32 and wave64 modules grow by 144 bytes, and the emulated wave64 module stays byte-identical, as the description says for the emulate path.

One note: scripts/run_gpu_tests.py doesn't read LEONIDA_LDS_WAVE_ORDER. Only build.py passes --lds-wave-order on, so LEONIDA_LDS_WAVE_ORDER=1 run_gpu_tests.py quietly tests the default translation. The on rows above come from build.py/check.py. If the env var should work there too, it's a two-line addition next to w64_emulated() in the translator command.

AI-assisted (Claude Code); I checked the results.

@AlxcBTW

AlxcBTW commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #2345: the same change as one commit, rebased onto main after the latest merge round. It conflicted in tools/rdna2spirv.py, and I can't force-push, so it's on a new branch. All test results from here are carried over; thanks @Itschotsch, @dcastell1, @bobcatchris, @crastinator, @zneel and @josevictorp.

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.

9 participants