Skip to content

wave64_emulate_opt: a guarded s_barrier keeps only the tail in the round scheduler (SHADER-W64-STRUCTURED-SBARRIER-TAIL) - #2102

Merged
aslan2142 merged 1 commit into
ProjectLeonida:mainfrom
DadaCap:mesh-epilogue-tail
Nov 26, 2026
Merged

aslan2142 merged 1 commit into
ProjectLeonida:mainfrom
DadaCap:mesh-epilogue-tail

Conversation

@DadaCap

@DadaCap DadaCap commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: SHADER-W64-STRUCTURED-SBARRIER-TAIL (#1608 (comment))

Depends on: nothing

What and why

Under --wave64-emulate, plan_structured_emulation gives no plan for a multi-wave program (more than 64 threads) as soon as one s_barrier sits under a guard or in a loop. The NGG mesh epilogue has one (s_cbranch_vccnz over an s_barrier), so every mesh program of 1.0.2.1 runs whole in the round scheduler.

The planner already has a step for a tail that must stay in rounds (tail_scheduler, used from an early s_endpgm on). This change uses it from the first guarded s_barrier on as well, and emits what comes before in the structured form.

Why it is sound:

  • The tail starts at the first top-level item that holds a guarded or looped s_barrier and runs to the end of the program, so every such s_barrier, and every one after it, stays in the rounds. Waves on different paths still meet at different sites there (cs_barrier_divergent).
  • What runs before the tail can only hold s_barriers in top-level blocks, which the whole workgroup reaches, and no s_endpgm.
  • The tail step itself, and the barrier in front of it when memory is dirty, are unchanged.

Counts over 1.0.2.1 (translator only)

Programs Emulated rounds before Structured after spirv-val failures
Mesh 9,282 9,282 0
Compute, executable archives, still in rounds on main 222 40 0
Compute, pipeline library, still in rounds on main 262 34 0

All of them translate before and after. The compute programs were run through main's translator again for the "before" column (none is structured). For mesh, main's translator gives the same bytes as the old folder for three programs I checked. Other stages cannot reach this code.

Measured in the game

Two runs of the same executable (a local tree of db1a22d29 with other open work, GPU timer on), the only difference being the mesh modules. Median GPU time of the sampled indirect draws, for the 17 mesh programs sampled at the same group count in both; the sample counts are small for some:

Mesh program Before, ms (n) After, ms (n) Ratio
MS_VS_TransformD_c5fd36bb 2.94 (96) 0.40 (19) 0.14
MS_VS_OctaSLOD_5fd19871 3.60 (24) 0.76 (42) 0.21
MSNC_VS_TransformSkinD_9e3a1555 3.73 (81) 1.27 (75) 0.34
MS_VS_TransformD_c5fd36bb~153 18.61 (27) 11.12 (14) 0.60
MSNC_VS_PropFoliageSkinDeferred_4352f5e1~8 28.14 (22) 17.60 (4) 0.63
MSNC_VS_TransformSkinD_9e3a1555~50 25.20 (17) 19.68 (21) 0.78

The other 11 are between 0.13 and 0.64.

Not shown by these runs:

  • Pictures. The two runs' exposure differs a lot, as it does between runs of one build here, so I could not compare them by pixels. Correctness rests on the CPU workgroup interpreter.
  • Stability. The run with the old modules lost the device at 700 s, in a different scene, and the run with the new ones did not in 15 minutes. One event says nothing about this change.
  • The new compute modules were not run in the game.

Tested

  • tests/semantic/test_wave64_emulate_opt.py: test_multiwave_divergent_sbarrier_falls_back_to_rounds asserted the old fallback; it now asserts that both s_barrier arms are in the tail, and the same memory result in both invocation orders. New: a mesh-epilogue shape (guarded s_barrier, skipped and taken, 128 threads, both orders).
  • tests/semantic/test_loop_ballot_barrier.py: two tests asserted the rounds form for an s_barrier in a loop; they now expect the structured form, and their memory results (including the wave that ends early) are unchanged.
  • Whole translator suite: 491 files, 6,313 tests, 0 files failed (24 jobs)
  • tests/test_translate.py: 706 pass, 2 fail (cs_dpp_cmp.asm: my older llvm-mc refuses the DPP form of v_cmp); both fail the same way on plain main here.
  • python -I -m compileall -q tools tests, tests/test_docs_*.py, scripts/check_no_game_data.py on the changed files.

GPUs tested: NVIDIA GeForce RTX 5080, driver 621.24, Windows 11 (the game runs above; the hand-written GPU tests were not run)

AI-assisted: yes (Claude Code, Claude Opus 5.5 wrote the change, the tests and this text)

Checklist

  • Based on current main and merges cleanly.
  • One topic; follow-up work goes in a new pull request.
  • 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.

🤖 Generated with Claude Code

…und scheduler (SHADER-W64-STRUCTURED-SBARRIER-TAIL)

plan_structured_emulation gave no plan for any multi-wave program with an s_barrier under a guard or in a loop, which every mesh program has (NGG epilogue). The tail scheduler used from an early s_endpgm on now takes over from that s_barrier on, and what comes before is emitted structured.

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

DadaCap commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

@OreoFR asked on Discord which library programs move out of the rounds form. These 34 (lib_cs_ numbers of the 1.0.2.1 listing here):

00848, 00959, 00960, 00962, 00963, 01009, 01010, 01054, 01055, 01058, 01059, 01060, 01061, 01180, 01182, 01454, 01456, 01459, 01461, 01532, 01610, 02003, 02060, 02079, 02923, 02931, 02935, 02936, 03108, 03114, 03118, 03134, 03135, 03138

Of your five: 00959 is in the list. 01851 stays in rounds with this change. 01468 and 01855 were already structured before it, and 00906 is not emulated wave64, so this change does not touch those three. The new compute modules have not been run in the game here.

AI-assisted (Claude Code, Claude Opus 5.5).

@xel1uz xel1uz 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.

Approving 84816055. I verified it independently on main 747809e (merged cleanly).

  • Reading: the removed guard returned None for the whole program at any guarded or looped s_barrier. The new condition hands off at the first such top-level item, so every guarded s_barrier and everything after it stays in the rounds. Before that point only whole-workgroup top-level blocks can hold an s_barrier, and the tail step and its dirty-memory barrier are unchanged.
  • Own tests: tests/semantic/test_wave64_emulate_opt.py and test_loop_ballot_barrier.py are OK.
  • Bite check: with main's tools/wave64_emulate_opt.py they fail (2 + 2).
  • Whole translator suite: 491 files, 6,313 tests, 0 files failed.
  • Emulation fuzzing with this branch's translator on llvmpipe: tools/lds_fuzz.py --wave 64 --emulate (seed 55501) 300/300 match; tools/crosslane_fuzz.py --wave 64 --modes emulate --no-maskdata (seed 55502) 300/300 match.
  • run_gpu_tests.py with LEONIDA_W64_EMULATE=1 --filter cs_: 99/130 passed, the same as main. cs_barrier_divergent and cs_barrier_early_end pass with the expected 127 126 ... 120. cs_barrier_loop skips on llvmpipe, which can't pin 32 lanes. The one failure, cs_imgatomic_ext, also fails on main and is #1518.

All runs: Ubuntu 26.04, Python 3.14, LLVM 21.1.8 / 19.1.7, SPIRV-Tools 2026.1, Mesa 26.0.8 llvmpipe. I did not measure the in-game speedup.

AI-assisted review, CPU only (llvmpipe).

@OreoFR

OreoFR commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Thanks for the list. I ran it: only lib_cs_00959 swapped for your translation (main's translator reproduces my installed module byte for byte), main + mesh-indirect-on-main, RTX 4070 Laptop 621.40. The device was still lost at ~197 s. Aftermath shows lib_cs_00959 (new hash) at the loop head of the round-scheduler tail that stays after the guarded s_barrier. So for this hang the remaining tail is enough. Next run: all 16,762 mesh programs (9,282 + 7,480 fspx_gfx) translated with this PR.

@MoonEyes

Copy link
Copy Markdown
Collaborator

Verdict: ready to merge. This agrees with xel1uz's approval and adds a second mutation, a coverage note and a mesh sample. Two suggestions below, neither blocking.

Setup: PR head 84816055d on its base db1a22d29. Main is now d3b8251bc, 176 commits later; none of them touches the 4 changed files or tools/rdna2spirv.py, and the two touched test files give the same result on the PR merged with it (34/34 and 15/15). Windows 11, Python 3.14. The tests below run on the CPU SPIR-V interpreter, so no GPU was involved. I ran nothing in the game.

Tests

  • The two touched files, on main: test_wave64_emulate_opt 33/33, test_loop_ballot_barrier 15/15.
  • Same files on the PR: 34/34 and 15/15.
  • Mutation 1, the PR's tests against main's tools/wave64_emulate_opt.py: 4 fail, all on the control_flow assertion (rounds != structured). That is 2 in test_wave64_emulate_opt and 2 in test_loop_ballot_barrier.
  • Mutation 2, the PR with only the new or (...) clause removed at tools/wave64_emulate_opt.py:1792: only test_multiwave_guarded_sbarrier_epilogue_keeps_body_structured fails. See the first suggestion.
  • Full translator suite on the PR at its base: 491 files, 6,313 tests, 1 file failed (test_lds_src2, 63 failures). It fails the same way on main: my llvm-mc runs through WSL and cannot see the Windows temp path. This is unrelated to the PR, and test_lds_src2: feed llvm-mc through stdin, so a WSL LLVM_MC can read it (TEST-LDS-SRC2-MC-STDIN) #1733 fixes it. xel1uz's Linux run has 0 files failed.
  • scripts/check_no_game_data.py on the 4 changed files: clean.
  • Local translation of a 1-in-9 sample of the mesh programs (1,032 programs, --wave64-emulate):
    • translated and spirv-val-valid: 1,032/1,032 with main and 1,032/1,032 with the PR;
    • changed: 1,032, all moving from emulated wave64 rounds to emulated wave64 structured;
    • the output binary differs for all 1,032.

Safety

  • The new check at tools/wave64_emulate_opt.py:1792 runs before the sync-free and mini-scheduler paths (:1801-1822). It uses _blocks_in_tree (:1058-1068), which descends into both guards and loops. So any top-level guard or loop that contains an s_barrier, however deeply nested, starts the tail, and the tail runs to the end (:1795-1796).
  • The structured part keeps s_barrier only in top-level blocks (:1782-1788). check_sync_free also rejects any s_barrier (:1750). Folds cannot hide one: the fold rules exclude s_barrier (:14), and sb_has_sbarrier scans every block of a chain (:1594-1615).
  • An early s_endpgm still starts the tail through has_end (:1781, :1792). The flattening stops after it (:1719-1742), so a guarded s_barrier behind an early s_endpgm also ends up in the tail.
  • The tail is emitted with the same round loop head and tail as the global scheduler (tools/rdna2spirv.py:16598-16609).
  • One real change: tails can now contain divergent s_barrier sites. Main never produced that, because the old early return covered all of flat_top. test_multiwave_divergent_sbarrier_stays_in_tail_rounds (tests/semantic/test_wave64_emulate_opt.py:1433) checks it in both invocation orders.
  • I also ran an extra local probe, not part of the PR: a sync-free guard, then a divergent guarded s_barrier. Plan inline_block, inline_sync_free, inline_block, tail_scheduler, and the memory result was correct in both orders.

Suggestions (non-blocking)

  1. In the divergent test (:1433), each arm ends in s_endpgm, so has_end already sends it to the tail. In the epilogue test (:1461), vcc is the same for both waves. As a result, no PR test pins a wave-divergent guarded s_barrier that rejoins with no s_endpgm (for example an if/else with an s_barrier in each arm). I tried that shape locally: on the PR the plan is inline_block, tail_scheduler and the memory is correct in both orders. With mutation 2 the two arms become two separate mini-schedulers, and the CPU interpreter still returns the correct memory. So the test needs a tail_blocks assertion; the memory check alone does not catch it. Mutation 2 also passes test_loop_ballot_barrier unchanged, because those loops go to a mini-scheduler. tail_blocks assertions at tests/semantic/test_loop_ballot_barrier.py:1395 and :1468 would pin the new clause there too.
  2. The docstring at tools/wave64_emulate_opt.py:1517 still says a divergent s_barrier returns None. The comment at :1793 only mentions s_endpgm. Both could say "or a guarded/looped s_barrier".

Not covered by this review

  • The in-game timing ratios (0.13-0.78) and the RTX 5080 runs are the author's numbers. I did not reproduce them, and I did not compare pictures.
  • OreoFR's later run (one compute program retranslated with this PR, device still lost, stopping in the remaining tail) fits the reading above: the PR shortens the round scheduler and keeps the tail. I did not reproduce that run, and I see nothing in it that argues against merging.
  • I did not check the compute counts in the PR body (222/262 programs, 40/34 structured).
  • I did not run tests/test_translate.py or the hand-written GPU tests.
  • My mesh check was a 1-in-9 sample, translator only, with nothing run on a GPU.
  • wave64_emulate_opt: model s_bitset0/1_b32/b64 as read-modify-write and SCC-less (SHADER-W64-EMU-BITSET-RMW) #1906 edits the same docs/design.md row, so whichever PR lands second will need a trivial merge.
  • The PR body names individual programs. A counts-only table may be safer under the no-game-data rule.

AI-assisted: yes (Claude Code, Claude Fable 5.2, ran the tests and wrote this; a second agent re-ran the mutations and checked this text)

@ZroIsHere

Copy link
Copy Markdown
Contributor

I integrated this change into ZroIsHere/leonida on integration/world-frame-blockers as ad86d5ebd. It applies cleanly over the current integration branch. The existing PR remains the review path for this topic.

@ammonium231

Copy link
Copy Markdown

RTX 3090 / 621.40, LEONIDA_W64_EMULATE=1: handwritten wave64 GPU tests (run_gpu_tests.py --filter w64 and --filter cs_barrier) are 28/29 pass + 1 skip and 3/3 pass on current main a73a57e, and identical with this PR merged onto that main (including cs_w64x_sbarrier, cs_w64x_chain_barrier, and the three cs_barrier_* fixtures).

@aslan2142

Copy link
Copy Markdown
Collaborator

Merging.

  • Why: a guarded s_barrier now keeps only the code from it onward in the rounds scheduler, instead of sending the whole program there. Nothing changes without --wave64-emulate.
  • Tested on main fac8b4bb9 + this PR (84816055d):
    • The PR's four changed tests fail with main's planner (4 of 49) and pass here.
    • Translator suite: 500 files, 6,374 tests, 0 failed.
    • test_ci_registration.py, the docs tests, compileall, make_game.py --help and check_no_game_data.py pass.
    • 450 programs (150 mesh, 150 compute, 150 library compute) translated without emulation are byte-identical to main's output.
    • With --wave64-emulate, 154 of those 450 move from rounds to structured (150 mesh, 2 compute, 2 library), 27 stay in rounds, and all 450 pass spirv-val with both translators.
  • CI is red for reasons outside this PR: cs_dpp_cmp under CI's llvm-mc, an uncalled test in test_feature_scan.py, and a missing define in vk_null_descriptor_test.cpp. None is in the four files changed here.
  • Not tested here: a GPU run of an emulated pack; our machine runs the native wave64 pack. @ammonium231's RTX 3090 results are unchanged with the PR, and the device loss @OreoFR reported predates it.

Optional follow-ups from @MoonEyes's review: a tail_blocks assertion in the loop tests, and the docstring at wave64_emulate_opt.py:1516.

Thanks @DadaCap, and @xel1uz, @MoonEyes, @ammonium231 and @OreoFR for the reviews and runs.

AI-assisted (Claude Code, Claude Opus 5.5 and Claude Fable 5.2)

@aslan2142
aslan2142 merged commit 8bc59a7 into ProjectLeonida:main Nov 26, 2026
5 of 12 checks passed
DadaCap added a commit to DadaCap/leonida that referenced this pull request Nov 26, 2026
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.

7 participants