xshader: emit structured control flow for reducible shader CFGs #489
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/489"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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/elsediamonds and[loop] for (...)loops intuxbox-xshaderinstead of flatwhile (tb_pc != N)switch/block dispatchers or deeptb_skipflag chains, cutting DXC anddxil-spirvcompile work on branching and looping kernels:hlsl::structured_cfg(mod.rsandemit.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.structured_cfg::try_emitat both the interleaved-loop and general control-flow sites inhlsl/emitter/control_flow.rs, guarded bystructured_cfg_eligible(deferring shaders usingsingle_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_orderwaits,device_sync_before, barriers, or custom inline intrinsics, plus trivial branch-less/loop-less CFGs).// 0xXXXX s_endpgm/s_endpgm_ordered_ps_doneISA comments whenemit_isa_commentsis enabled, simplifies!(!cond)negation inarm_cond, updates unit tests andhost_profiles.txtfor the structured output, and updatestools/check_lockstep_gpu.pyandtools/check_wave_guard.pyinstrumentation to recognize structured CFG output alongsidetb_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.
GPU, driver and backend:
10de:1e84), driver620.73.04, GE-Proton11-14 (vkd3d-proton)1002:13c0), RADV / Mesa26.3.1-arch3.5, GE-Proton11-14 (vkd3d-proton)Commands run and their results:
Checks
CI runs the Python unit tests, the shader compiler, DXBC, and XG layout tests, and a Windows typecheck of
tuxbox-d3d12.Imported from brcly/TuxBox. Originally posted by NickStallman on 2026-11-29T19:20:21Z.
Testing this after the merge (
fd636d09againstad551d44), 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.rsgives 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:
1002:13c0, RADV) loggedring gfx_0.0.0 timeoutand a ring reset for the harness process, at the second the dispatch's output was read back. The next dispatch failed withVK_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.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.