shader: prove spill-slot reads across branches in the startup translator #2516
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2524"
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 R0Y00/Xbox2Windows. Originally posted by NiiRoZz on 2026-11-25T19:47:35Z.
Refs #2125
What changes
tools/startup/gfx2hlsl.pylowers the constant-lane SGPR spill pairv_writelane_b32 vN, sM, k/v_readlane_b32 sD, vN, kto one scalar per (VGPR, lane) (item E). Until now a pixel stage with any branch was refused withv_writelane_b32 needs straight-line code to prove write-before-read. This PR replaces that requirement with a proof that works with control flow.spill_reachingruns a forward must-dataflow over the basic blocksjoined_registersalready builds. A slot counts as written at a join only if it is written on every incoming edge (intersection). The writes that can supply it are the union over those edges, iterated to a fixed point so that a later write in a loop body reaches the loop head through the back edge.joined_registers(with_cfg=True)also returns the block graph and, for eachs_cbranch_*, the blocks of the region it already computed (from the branch to its immediate post-dominator, or the loop range for a backward branch).scc_uniform;execz/vcczand thenzforms never are).Soundness argument
The slot variable is assigned on the pixel's own path. The hardware lane holds the last write on the wave's path: both instructions ignore EXEC, so a write runs whenever the wave runs it. The pixel's path and the wave's path agree except inside the region of a branch that is not proven uniform. There the wave also runs blocks this pixel skips, or, for
vccnz/execnz, skips blocks this pixel runs.m0and descriptor checks on the stored value all apply as before. The descriptor check on the stored value usessym, which handles joins. The lane-mask check (masks) still follows program order, not the joins (not changed here).3and0x3are one slot.Each shape that is not proven keeps its own refusal:
v_readlane_b32 reads vN lane k with no v_writelane_b32 of it on some path. This covers another lane written on another path.... written later in a loop (loop-carried).... outside the region of the branch at <off> that writes it, which is not proven uniform.v_readlane_b32 joins <n> writes of vN lane k, one inside the region of the branch at <off>, which is not proven uniform.... from a v_writelane_b32 that was not translated.translate()) keeps the original straight-line refusal.Measured
On a local set of 79 vertex/pixel requests, the first refusal was this one under a development translator. That translator has changes that are not merged.
v_fma_mix_f32source sN holds a lane mask or descriptor(all 30: a descriptor is spilled)Shape of the 79 pixel programs (a decode-only count, separate from the translator):
All but one are acyclic. They contain dozens to over a hundred
s_cbranch_execz, a few SCC branches and in some cases onevccz.One program has lane operations inside a loop.
Of 5994 spill reads:
execzregion, after the join.This count assumed every SCC branch is uniform; it is not what the translator refused. In census mode the translator reports the joined-write refusal in 4 programs (4 reads). Most other reads in these programs stop at a refused write first.
None of the 79 vertex programs contains
v_writelane_b32orv_readlane_b32.In census mode (every instruction, approximate after the first refusal), these programs also show:
vcc_lo/vcc_hias the stored value;All are existing refusals, unchanged here.
Tests
Head:
5c84039f. Windows 11, Python 3 fromout/pe-env.PASS. The check covers:
3and0x3as one slot.Pipeline.hlsl_ps(): the slot is declared once and assigned on both arms of a uniform branch before the read after the join.All passed: 70 passed, 0 failed (translator check, 67 gfx2hlsl checks, docs links, bidi controls). This includes the unchanged
startup_gfx2hlsl_sgpr_spill_check.py.git diff --checkagainst the base is clean; all files are LF.Not done / hypothesis
execz/vcczis taken from the existing code. The soundness argument assumes that model: a pixel skips a block only through a branch, and SALU and lane writes on its path run regardless of its EXEC bit.translate()does not updatemasks.uniformflag of a single write is carried to reads past joins on the pixel-path/wave-path argument alone.scc_uniform.Merge of current main at
dc94afac: one conflict intools/startup/gfx2hlsl.py(both sides' lines kept). Re-run at that head: the new check,startup_translator_check.pyand every no-argumentstartup_gfx2hlsl_*_check.py, all exit 0.🤖 Generated with Claude Code
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T20:01:26Z.
Review: COMMENTED
Review of head
dc94afac(comment only, not merging).What is good:
.github/workflows/windows.ymlPython-check list, AGENTS.md and docs/experiments.md are all updated. No other open or merged PR covers this slice (searched writelane / spill / branches).What is missing (why I do not merge):
execz/vcczand non-uniform regions that the soundness argument relies on. Please add a dispatch/draw test, with warp and hardware variants next tostartup_gfx2hlsl_sgpr_spill_*, that runs at least: a uniform branch with writes on both arms, a non-uniform branch where one pixel skips the write, and a loop with a write in the body. Compare every pixel with an integer reference..github/workflows/windows.ymlis touched (one line in the Python-check list). The PR's own CI run should show the new check executing; please confirm it in the job log.execz/vcczper-pixel model,uniformcarried past joins). Those are exactly what a readback would verify.Commands run (Linux, exact head):
python3 -B tests/startup_gfx2hlsl_spill_branches_check.py: PASSpython3 -B tests/startup_gfx2hlsl_sgpr_spill_check.py: PASSpython3 -B tests/startup_translator_check.py: 0 failedpython3 tests/docs_links_check.pyandpython3 tests/bidi_controls_check.py: okgit diff --check origin/main...HEAD: cleanspill_reaching(gfx2hlsl.py:4515): the new check goes red (KeyError), restored afterwards.No Win2025 run done, since the readback test does not exist yet. The merge stays with MasterDomino or @R0Y00 once a readback is added.
AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-25T20:10:11Z.
Please keep the dataflow controls and add a source-built pixel readback before merge. Use two uniform branch choices that write distinguishable values to the same slot, then reload and export the selected bits after the join. Both choices should render on WARP and on a physical GPU. A deliberately wrong opposite-arm reference must differ. Keep the existing missing-write and divergent/unproved region refusals, so a green draw cannot mask a provenance error.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T22:56:59Z.
Review: COMMENTED
Review of head
e9e2b543(merge of main on top of the reviewed change; the PR's own diff is unchanged sincedc94afac).Not ready to merge: the pixel readback that @R0Y00 asked for on 2026-11-25 is still missing.
What is missing
tests/startup_gfx2hlsl_spill_branches_check.py(text and refusal controls throughStage.translate()). The CMake entrystartup_gfx2hlsl_spill_branchesruns that Python file only; there is nooffscreen_checkdraw next to it, unlike the siblingstartup_gfx2hlsl_sgpr_spill_${adapter}tests inCMakeLists.txt.tests/startup_gfx2hlsl_sgpr_spill_check.py(same draw harness, same packed-position OR pattern) or addstartup_gfx2hlsl_spill_branches_${adapter}draws withSKIP_RETURN_CODE 77, and wire them inwindows.yml/AGENTS.mdlike the existing entries.What I checked (all Linux, in a clean worktree of the exact head)
python3 -B tests/startup_gfx2hlsl_spill_branches_check.py: PASS; the six translator mutations (union at joins, one pass instead of fixed point, lane keyed by spelling, each region rule removed, all branches uniform) each turn at least one control red.python3 tests/docs_links_check.py: 19 files, 499 links OK.python3 tests/bidi_controls_check.py: OK.git diff --check origin/main...HEAD: clean..github/workflows/windows.ymlis one added test path in an existing list; the PR's own CI run needs to exercise it.Once the draw test exists I can run it on Win2025 dev+release and re-check for merge.
AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T02:13:39Z.
Added the pixel readback asked for above, on top of a merge of main (no rebase).
What changed (
tests/startup_gfx2hlsl_spill_branches_check.py,CMakeLists.txt,docs/experiments.md):drawmode: two uniform SCC choices write different binary32 words to the same slot (v10lane 3), the join reloads it and exports it OR-ed with the packed screen position. All 16x16 pixels must equal the selected arm, with FXC 5_1 and DXC 6_0.binarymode checks the fixture words against llvm-mc. The existing dataflow controls, refusals and the 6 translator mutations are unchanged.startup_gfx2hlsl_spill_branches_warpand_hardware.Verification (Win2025, dev and release, patch applied on the previous head):
x2w_pipe.py win2025 2524 --patch <diff> --regex 'startup_gfx2hlsl_spill_branches|startup_gfx2hlsl_sgpr_spill' --wait 7200startup_gfx2hlsl_spill_branches,_warp,_hardware, andstartup_gfx2hlsl_sgpr_spill_controls/_warp/_hardware.binarywith llvm-mc (PASS),tests/docs_links_check.py,tests/bidi_controls_check.py,git diff --check.AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T02:26:47Z.
Merged
origin/maininto this branch (no rebase, no force-push). The only conflict was two neighbouring attribute lines intools/startup/gfx2hlsl.py; both sides kept. CMake and doc lists merged cleanly. The change itself is untouched, so the earlier Win2025 PASS still applies.Commands executed locally:
git merge origin/main(twice, main moved once during the work)python3 -m py_compile tools/startup/gfx2hlsl.pypython3 -B tests/startup_gfx2hlsl_spill_branches_check.py-> PASSAuto-merge (squash) is re-enabled.
AI-assisted; commands listed were executed locally.