xshader: emit structured control flow for reducible shader CFGs #489

Merged
glovepost merged 1 commit from upstream-pr/489 into main 2026-11-29 18:04:33 +00:00
glovepost commented 2026-11-29 15:59:01 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by glovepost on 2026-11-29T15:59:01Z.


Summary

Lowers eligible reducible RDNA2 shaders (including interleaved reducible loops and guard-only loop exits) to structured if/else diamonds and [loop] for (...) loops in tuxbox-xshader instead of flat while (tb_pc != N) switch/block dispatchers or deep tb_skip flag chains, cutting DXC and dxil-spirv compile work on branching and looping kernels:

  • Adds hlsl::structured_cfg (mod.rs and emit.rs): Cooper-Harvey-Kennedy iterative dominator tree, reducible natural loop forest (routing explicit-exit-less loops to the program exit so watchdog guards terminate cleanly), post-dominator join recovery, short-tail cloning, and bounded per-scope topological fallback (MAX_DEPTH / growth budget) when a single scope would expand excessively.
  • Wires structured_cfg::try_emit at both the interleaved-loop and general control-flow sites in hlsl/emitter/control_flow.rs, guarded by structured_cfg_eligible (deferring shaders using single_wave32, paired, portable_wave64, lockstep, wave_cull, light_grid, page_waves, hoist_wave_exchanges, compute_swizzle, indexed_lane, view_cull, micropoly_vote, reduction_execz_guards, wave_bounded_latches, exec_snapshots, lds_order waits, device_sync_before, barriers, or custom inline intrinsics, plus trivial branch-less/loop-less CFGs).
  • Emits standard // 0xXXXX s_endpgm / s_endpgm_ordered_ps_done ISA comments when emit_isa_comments is enabled, simplifies !(!cond) negation in arm_cond, updates unit tests and host_profiles.txt for the structured output, and updates tools/check_lockstep_gpu.py and tools/check_wave_guard.py instrumentation to recognize structured CFG output alongside tb_skip / tb_pc.

Prepared with AI assistance; all changes were reviewed and verified on host unit tests and both NVIDIA and AMD GPUs.

Tested

Run the checks relevant to this change.

  • The checks relevant to this change passed
  • Changed or new behaviour is covered by a Lanes scenario, or the reason it cannot be is stated below
  • Where the table calls for AMD and NVIDIA, they passed on both. Otherwise, say which vendor still needs to run them:

GPU, driver and backend:

  • NVIDIA GeForce RTX 2070 SUPER (10de:1e84), driver 620.73.04, GE-Proton11-14 (vkd3d-proton)
  • AMD Ryzen 7 9800X3D iGPU (1002:13c0), RADV / Mesa 26.3.1-arch3.5, GE-Proton11-14 (vkd3d-proton)

Commands run and their results:

python3 tuxbox.py check --no-gpu
  -> 7/7 suites PASS (Python tests; xshader 614/614 PASS across lib/bin/integration including 6 structured_cfg tests; DXBC 96/96 PASS; XG layout 17/17 PASS, 1 ignored; storage-codec 18/18 PASS; D3D12 Windows typecheck PASS; repository hygiene PASS)

python3 tools/check_translator_gpu.py --provider <d3d12_xs.dll> --proton <proton> --prefix <prefix> --compiler-dir <dxc-win> --dxc <dxc> --work <work> --adapter 10de:1e84
  -> 65/65 PASS (0 failures)

python3 tuxbox.py verify --backend proton --adapter 10de:1e84 --output <out>
  -> 123/123 PASS (0 failures)

TUXBOX_CHECK_JOBS=4 python3 tools/check_translator_gpu.py --provider <d3d12_xs.dll> --proton <proton> --prefix <prefix> --compiler-dir <dxc-win> --dxc <dxc> --work <work> --adapter 1002:13c0
  -> 64/65 PASS on full sweep + 1/1 PASS on --only lockstep after harness update (= 65/65 PASS, 0 failures)

TUXBOX_CHECK_JOBS=4 python3 tuxbox.py verify --backend proton --adapter 1002:13c0 --output <out>
  -> 123/123 PASS (0 failures)

Checks

CI runs the Python unit tests, the shader compiler, DXBC, and XG layout tests, and a Windows typecheck of tuxbox-d3d12.

<!-- greyfinch-sync:brcly/TuxBox:issue:5552454029 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/489). Originally posted by [glovepost](https://github.com/glovepost) on 2026-11-29T15:59:01Z. --- ## Summary Lowers eligible reducible RDNA2 shaders (including interleaved reducible loops and guard-only loop exits) to structured `if`/`else` diamonds and `[loop] for (...)` loops in `tuxbox-xshader` instead of flat `while (tb_pc != N)` switch/block dispatchers or deep `tb_skip` flag chains, cutting DXC and `dxil-spirv` compile work on branching and looping kernels: - Adds `hlsl::structured_cfg` (`mod.rs` and `emit.rs`): Cooper-Harvey-Kennedy iterative dominator tree, reducible natural loop forest (routing explicit-exit-less loops to the program exit so watchdog guards terminate cleanly), post-dominator join recovery, short-tail cloning, and bounded per-scope topological fallback (`MAX_DEPTH` / growth budget) when a single scope would expand excessively. - Wires `structured_cfg::try_emit` at both the interleaved-loop and general control-flow sites in `hlsl/emitter/control_flow.rs`, guarded by `structured_cfg_eligible` (deferring shaders using `single_wave32`, `paired`, `portable_wave64`, `lockstep`, `wave_cull`, `light_grid`, `page_waves`, `hoist_wave_exchanges`, `compute_swizzle`, `indexed_lane`, `view_cull`, `micropoly_vote`, `reduction_execz_guards`, `wave_bounded_latches`, `exec_snapshots`, `lds_order` waits, `device_sync_before`, barriers, or custom inline intrinsics, plus trivial branch-less/loop-less CFGs). - Emits standard `// 0xXXXX s_endpgm` / `s_endpgm_ordered_ps_done` ISA comments when `emit_isa_comments` is enabled, simplifies `!(!cond)` negation in `arm_cond`, updates unit tests and `host_profiles.txt` for the structured output, and updates `tools/check_lockstep_gpu.py` and `tools/check_wave_guard.py` instrumentation to recognize structured CFG output alongside `tb_skip` / `tb_pc`. Prepared with AI assistance; all changes were reviewed and verified on host unit tests and both NVIDIA and AMD GPUs. ## Tested Run the [checks relevant to this change](../CONTRIBUTING.md#test-what-your-change-touches). - [x] The checks relevant to this change passed - [x] Changed or new behaviour is covered by a Lanes scenario, or the reason it cannot be is stated below - [x] Where the table calls for AMD and NVIDIA, they passed on both. Otherwise, say which vendor still needs to run them: GPU, driver and backend: - NVIDIA GeForce RTX 2070 SUPER (`10de:1e84`), driver `620.73.04`, GE-Proton11-14 (`vkd3d-proton`) - AMD Ryzen 7 9800X3D iGPU (`1002:13c0`), RADV / Mesa `26.3.1-arch3.5`, GE-Proton11-14 (`vkd3d-proton`) Commands run and their results: ```text python3 tuxbox.py check --no-gpu -> 7/7 suites PASS (Python tests; xshader 614/614 PASS across lib/bin/integration including 6 structured_cfg tests; DXBC 96/96 PASS; XG layout 17/17 PASS, 1 ignored; storage-codec 18/18 PASS; D3D12 Windows typecheck PASS; repository hygiene PASS) python3 tools/check_translator_gpu.py --provider <d3d12_xs.dll> --proton <proton> --prefix <prefix> --compiler-dir <dxc-win> --dxc <dxc> --work <work> --adapter 10de:1e84 -> 65/65 PASS (0 failures) python3 tuxbox.py verify --backend proton --adapter 10de:1e84 --output <out> -> 123/123 PASS (0 failures) TUXBOX_CHECK_JOBS=4 python3 tools/check_translator_gpu.py --provider <d3d12_xs.dll> --proton <proton> --prefix <prefix> --compiler-dir <dxc-win> --dxc <dxc> --work <work> --adapter 1002:13c0 -> 64/65 PASS on full sweep + 1/1 PASS on --only lockstep after harness update (= 65/65 PASS, 0 failures) TUXBOX_CHECK_JOBS=4 python3 tuxbox.py verify --backend proton --adapter 1002:13c0 --output <out> -> 123/123 PASS (0 failures) ``` ## Checks CI runs the Python unit tests, the shader compiler, DXBC, and XG layout tests, and a Windows typecheck of `tuxbox-d3d12`.
NickStallman commented 2026-11-29 19:20:21 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by NickStallman on 2026-11-29T19:20:21Z.


Testing this after the merge (fd636d09 against ad551d44), mesh output is fine, but nested loops lost their shared iteration limit.

The limit. The earlier structured form shares one counter, ++tb_guard < 65536u, across the loops of an invocation. structured_cfg/emit.rs gives each loop its own (tb_g{id} < LOOP_CAP), so two nested loops can run 65,536 × 65,536 times before they stop.

What it did here. A recorded compute program with two nested counted loops, whose bound comes from a constant buffer. Our test harness fills buffers with generated words, which made the bound about 132 million:

  • before: stops after 65,536 steps, 32 texels written, same result twice;
  • after: the AMD iGPU (1002:13c0, RADV) logged ring gfx_0.0.0 timeout and a ring reset for the harness process, at the second the dispatch's output was read back. The next dispatch failed with VK_ERROR_DEVICE_LOST. The kernel doesn't name the dispatch, so timing and order are the evidence. I haven't repeated it on a GPU, nor run this program on NVIDIA.
  • With a bound of 4, both forms write the same bytes (three runs each, lavapipe).

A console bound that large is bad data, so I don't claim a title reaches this with its own data. It matters when a buffer isn't filled or resident yet: that used to end the loop, and can now lose the device. By a rough count of braces, loops nest two or more deep in 327 of 800 random changed compute programs and 140 of 800 pixel programs. One counter shared by the loops of an invocation, as before, should restore the old bound; not built or tested.

Mesh. Voting programs: the HLSL of all 13,858 recorded ones is unchanged. Non-voting: 100 random changed ones, run as mesh against their compute form: 100 right before and after on both the iGPU and an RTX 5090 (620.70.10).

Compute. Of 300 random changed programs, 74 could run on AMD: 73 equivalent, and the one above. The NVIDIA half is unfinished. Pixel and vertex programs aren't covered.

<!-- greyfinch-sync:brcly/TuxBox:comment:7261200235 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/489#issuecomment-7261200235). Originally posted by [NickStallman](https://github.com/NickStallman) on 2026-11-29T19:20:21Z. --- Testing this after the merge (fd636d09 against ad551d44), mesh output is fine, but nested loops lost their shared iteration limit. **The limit.** The earlier structured form shares one counter, `++tb_guard < 65536u`, across the loops of an invocation. `structured_cfg/emit.rs` gives each loop its own (`tb_g{id} < LOOP_CAP`), so two nested loops can run 65,536 × 65,536 times before they stop. **What it did here.** A recorded compute program with two nested counted loops, whose bound comes from a constant buffer. Our test harness fills buffers with generated words, which made the bound about 132 million: - before: stops after 65,536 steps, 32 texels written, same result twice; - after: the AMD iGPU (`1002:13c0`, RADV) logged `ring gfx_0.0.0 timeout` and a ring reset for the harness process, at the second the dispatch's output was read back. The next dispatch failed with `VK_ERROR_DEVICE_LOST`. The kernel doesn't name the dispatch, so timing and order are the evidence. I haven't repeated it on a GPU, nor run this program on NVIDIA. - With a bound of 4, both forms write the same bytes (three runs each, lavapipe). A console bound that large is bad data, so I don't claim a title reaches this with its own data. It matters when a buffer isn't filled or resident yet: that used to end the loop, and can now lose the device. By a rough count of braces, loops nest two or more deep in 327 of 800 random changed compute programs and 140 of 800 pixel programs. One counter shared by the loops of an invocation, as before, should restore the old bound; not built or tested. **Mesh.** Voting programs: the HLSL of all 13,858 recorded ones is unchanged. Non-voting: 100 random changed ones, run as mesh against their compute form: 100 right before and after on both the iGPU and an RTX 5090 (620.70.10). **Compute.** Of 300 random changed programs, 74 could run on AMD: 73 equivalent, and the one above. The NVIDIA half is unfinished. Pixel and vertex programs aren't covered.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
brcly/TuxBox!489
No description provided.