Skip to content

rdna2spirv: bound structured nesting depth with the dispatch-loop form (SHADER-299) - #1993

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
Ayinaki:shader-299-nesting-bound
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
Ayinaki:shader-299-nesting-bound

Conversation

@Ayinaki

@Ayinaki Ayinaki commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Item: SHADER-299 (tracker #1745).

spirv-val time grows roughly with the cube of the structured nesting depth. Measured over the 1.0.2.1 corpus (26,503 modules, spirv-val 2026.4):

  • the deepest block of a module is nested 118 constructs deep; 12,963 modules are structured (median depth 15), and 6,000 of those are deeper than 16;
  • an estimated 85% of the corpus's spirv-val time is in modules nested 20 or deeper;
  • median spirv-val time for one module in the settled vs the flat form: depth 20 181 -> 129 ms (1.4x), 30 507 -> 371 (1.4x), 50 1,712 -> 738 (2.3x), 64 2,692 -> 1,163 (2.3x), 77 5,122 -> 1,130 (4.5x), 118 13,683 -> 1,352 (10.1x);
  • depth 5-12 shows no gain (at depth 6 the flat form is slightly slower), which is why the bound sits at 16.

Change: cfg_structure.depth() measures a tree from structure(); rdna2spirv falls back to the existing flat dispatch-loop form when the tree is deeper than MAX_STRUCTURED_DEPTH (16) and records gen.nesting. Both forms emit the same blocks, in the same order; only the routing differs. The rule is in the "Control flow" row of docs/design.md with how it was confirmed.

Tests:

  • tests/test_translate.py::test_deep_structured_nesting_uses_the_dispatch_loop: a tree above the bound comes out as the dispatch loop and passes spirv-val; raising the bound restores the structured form.
  • tests/test_cfg_structure.py::test_depth: depth counts blocks at 0 and each guard/loop as one.
  • py -3 -m pytest tests/test_translate.py tests/test_cfg_structure.py -q -> 473 passed, 2 skipped.
  • Corpus slice, --limit 200: 200 translated and 200 pass spirv-val with the default form, and 200/200 with --dispatch-loop.
  • The deepest corpus shader (118) re-translates valid: 1.35 s instead of 13.68 s.

Counts and timings only; no shader text.

spirv-val's structured-control-flow checking cost grows roughly with the cube
of the nesting depth: over the 1.0.2.1 corpus the deepest block sits 118
constructs deep and about 85% of validation time is in modules nested 20 or
deeper. cfg_structure.depth() measures the tree and a module deeper than
MAX_STRUCTURED_DEPTH (16) is emitted in the existing flat dispatch-loop form
instead, which emits the same blocks and only changes the routing. Depth
5-12 shows no gain, hence the bound at 16.
@Thatters

Copy link
Copy Markdown
Contributor

Game-copy check of the driver side, which the spirv-val numbers don't cover: on NVIDIA the dispatch-loop form compiles 1.6-2.6× slower and needs up to 3.4× the memory, much more than it saves in spirv-val.

Setup. My own 1.0.2.1 copy; a random sample of 400 modules (fixed seed; the two #884 giants excluded). Translated with --wave64-emulate by this PR (3e38c983) and by its base 89b047cd.

  • 400/400 translate and pass spirv-val both ways.
  • 76 of 400 (19%) change form, all structured → dispatch loop: 56 ps, 18 cs, 2 vs. Their size is unchanged (−1.4%).
  • Compiled on an RTX 5070 Ti / 621.24 with vk_pipeline_check --jobs 4, partner stubs from tools/spirv_stubs.py, and no cache file. Each run's modules got a unique SPIR-V generator word, so the driver's disk cache couldn't serve an earlier compile. Two rounds in alternating order.
the 76 changed modules base form this PR ratio
56 ps, summed compile time (round 1 / 2) 290.7 / 286.4 s 552.6 / 520.9 s 1.8-1.9×
2 vs 19.5 / 19.0 s 31.1 / 30.5 s 1.6×
18 cs (round 1 / round 2 under heavy CPU load) 291.0 / 535.8 s 744.4 / 844.9 s 2.6× / 1.6×
slowest cs 186-332 s 494-501 s
peak private memory, cs set 2.34 / 2.35 GB 8.02 / 8.02 GB 3.4×
spirv-val, all 76 51.9 s 28.2 s 0.54×

All pipelines compile in both forms, with no failures.

The trade on this sample: about 24 s of spirv-val saved against about 700 s of extra driver compile time (ps +250 s, cs +450 s, vs +12 s), roughly 30:1. Scaled to the corpus (19% of 26,503, about 5,000 modules), that's on the order of an hour more for the pipelines stage at --jobs 12, against a few minutes less spirv-val in the shaders stage. The compute modules' memory goes up too, at a time when NVIDIA's compiler is already the pipelines stage's memory problem (#884).

So the bound might be better applied only where spirv-val time dominates, or as an option rather than the default. Or, if the goal is faster validation, the structured form could stay and spirv-val could be given the deep modules separately. Happy to re-run with another bound (e.g. 32 or 64) if that helps choose.

AI-assisted; counts and timings only, no game data.

@Faterek

Faterek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 3e38c98 on AMD Radeon RX 9070 XT (RADV GFX1201), Mesa 26.2.6-arch3.1, Linux 7.3.4-1-cachyos: pass.

  • pytest -q tests/test_cfg_structure.py tests/test_translate.py: 473 passed, 2 skipped.
  • scripts/run_gpu_tests.py: 158/158 passed (8 D3D12-only skipped).
  • 1.0.2.1 corpus slice (--limit 500 --no-asm, counts only): 500/500 translated, 500/500 pass spirv-val (411 structured, 60 fell back to dispatch loop vs 471/0 on main, 29 straight line); vk_pipeline_check: 500/500 pipelines ok (420 ms + 80 ps, 0 failed, 0 skipped, 5.3s wall on 8 threads).

Code read of cfg_structure.depth() and rdna2spirv.py looks clean and minimal (iterative depth traversal, no recursion risk). AI-assisted.

@ItsNotRexed
ItsNotRexed merged commit 92800c6 into ItsNotRexed:main Nov 22, 2026
4 checks passed
ItsNotRexed added a commit that referenced this pull request Nov 22, 2026
- test_translate: #1904 caps structured depth at 16, so the #1993 dispatch-loop test lowers MAX_STRUCTURED_DEPTH.
- test_ctest_registration: skip-code scan ignores conditional returns.
- test_d3d12_stubs_generation: INCLUDE uses os.pathsep.
- docs/README.md: one Other languages line.
- first_light.py, make_game.py: LF text writes; stdout guard on three tools.

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

Copy link
Copy Markdown
Contributor

Independent package validation of SHADER-299 on main at 4947579, including subsequent control-flow changes. On Windows 11 with an RTX 5070 (subgroup size 32, driver 32.0.16.1656), the first 200 sorted inputs all translated with wave64 emulation and passed spirv-val. Vulkan pipeline creation succeeded for 171 mesh and 29 pixel shaders, with zero failures or skips.

The slice did not exercise the depth limit. A separate package pixel shader measured at nesting depth 18 selected the dispatch-loop fallback and successfully created a Vulkan graphics pipeline. This exercised compilation and pipeline creation only. The four CFG structure tests and the synthetic deep-nesting regression also passed. AI-assisted validation.

@sovljanskii

Copy link
Copy Markdown
Contributor

Post-merge check of 3e38c983 on Apple M4 (MoltenVK 1.4.2, macOS 27.2.1, LLVM 23.1.6). Own 1.0.2.1 copy; base 89b047cd; both sides with --wave64-emulate --mesh-compute --f16-interp-fallback --no-asm. It matches @Thatters' NVIDIA result above: the translation is correct and spirv-val gets faster, but the driver compiles the switched modules much slower.

Translation, shards 0–3/16 (6,628 of 26,503 listings)

  • Translated and spirv-val-valid: 6,527 on both sides. The 101 refusals are the same, with the same reasons.
  • Switched: 1,414 of 3,200 structured modules (44%, depth median 14, max 117) switch to the dispatch loop: 1,022 ps, 345 cs, 47 vs.
  • Unchanged: the other 5,113 are byte-identical to base.
  • Same as --dispatch-loop: 41 switched modules re-translated with --dispatch-loop are byte-identical to the default output.
  • spirv-val time on the 1,414 switched modules: 646.8 → 200.8 s (3.2×). That runs from 1.24× at depth 17–20 to 7.0× at depth 81–117, and 58 modules, all at depth ≤ 50, got slower.

MoltenVK pipeline creation

  • Method: vk_pipeline_check --jobs 4, 79 sampled switched modules, base and PR alternating in chunks of 15. Metal caches compiled shaders, so each module is timed once per side.
base 3e38c983
ok / failed 61 / 17 61 / 17 (the same VS, the known #1784 subgroup error)
still compiling at 30 min 0 1 (a cs with 3,748 blocks: 547 s on base)
the 61 compiled on both, summed 995 s 3,761 s (3.8×)
ps (43), median 4.3 s 9.0 s
cs (18), median 51.7 s 217.0 s
four whole chunks, wall time 484 s 1,778 s (3.7×)
  • Without queueing: compiled one at a time (--jobs 1), five fresh modules were 1.4–2.5× slower on the PR.
  • Where the time goes: a warm-cache re-run takes under a second on both sides, so the extra time is in Metal's compiler, not SPIRV-Cross.

So on the two drivers measured so far, NVIDIA and MoltenVK, the bound trades about 0.3 s of build-time validation per switched module for seconds to minutes of first-run pipeline compile per module, on the player's machine. I agree with @Thatters that it fits better as an option, or with a much higher bound. I'll post this as a finding in the coordination issue so someone can pick it up.

From the diff, latent (no game shader hits it)

  • Barrier rounds: needs_barrier_rounds still decides from the unbounded tree (cfg_structure.structure(succ) if STRUCTURED). A wave32 cs with a top-level s_barrier and depth > 16 now gets the dispatch loop with its barrier inside a switch case and no barrier rounds. The "Workgroup barriers" row in docs/design.md says the dispatch loop runs in rounds, and --dispatch-loop does use rounds for the same shader.
    • Reproduced: with a hand-written shader (a top-level barrier, then 20 nested guards), all three variants pass spirv-val.
    • Corpus: none of the 1,414 switched modules has a barrier, and the 5 structured wave32 cs with s_barrier all have depth 1.
  • Nesting not recorded: gen.nesting is not written to index.json, so only control_flow: "dispatch loop" shows the fallback.

No game data. AI-assisted.

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

Post-merge full-corpus check of 3e38c983 against its parent 89b047cd (git diff --stat between them: only this PR's 5 files), on my own 1.0.2.1 copy. make_game.py --stages shaders --jobs 11 (--wave64-emulate picked automatically), the same disassembly reused for every run. Ryzen 5 5500X3D (12 threads), Windows 11 Pro (10.0.26200), Python 3.14.7, llvm-mc 21.1.8.

                         89b047cd (parent)   3e38c983 (this PR)
translated / spirv-val   26,503 / 26,503     26,503 / 26,503 (both runs)
structured               13,066              7,199
dispatch loop            0                   5,867
emulated wave64 rounds   10,149              10,149
straight line            3,246               3,246
barrier rounds           42                  42
make_game.py wall time   2,727 s             2,415 s and 2,280 s
(check + shaders)
  • 5,867 of the 13,066 structured modules (44.9%) switch to the dispatch loop, 22.1% of the corpus. Every other control_flow count is unchanged.
  • make_game.py (check + shaders) was 312 s and 447 s faster on the two PR runs (11-16%). Run order was PR, parent, PR, with one parent run.
  • Driver-side pipeline compile time isn't measured here.

AI-assisted (Claude).

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