startup: refuse compute branches that decide for the whole wave but are lowered per lane #2772
No reviewers
Labels
No labels
accessibility
awaiting author
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
priority: P0
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/R0Y00--Xbox2Windows!2772
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/2772/head"
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?
@tedin7 on GitHub · 2026-11-26 07:33 UTC · original
tedin7:x2w-wave-branch-refuse→main· +160 −20 · 12 files · 9 commits · closed without mergingPart of #1723
What this unblocks: stops silently wrong compute shader output: a branch that the wave takes as a whole is no longer lowered as a per-lane decision.
What changed
Compute programs are lowered one lane at a time.
s_cbranch_vccz,s_cbranch_vccnz,s_cbranch_execnzand ans_cbranch_scc0/scc1on an SCC that depends on lane data decide once for the whole wave: the wave runs on while any lane's bit is set. The lowering took the branch from its own lane's bit, so a lane whose bit was clear skipped code the wave ran for it (or left a loop early while a scalar counter kept the wave's count). Such programs translated and gave a different value than the wave model.They are now refused by name:
<branch> decides for the whole wave and the one-lane model decides per lane. Still accepted, because an existing proof covers them:s_cbranch_execnzinside a block that ans_cbranch_execzskips (execz_refusalsproves the block's EXEC writes).s_cbranch_vccz/vccnzand lane-data SCC branches inside such a block stay refused: the proof does not cover their condition (the wave runs the code while any lane's bit is set);s_cbranch_scc1of a readfirstlane waterfall (waterfall_refusal);s_cbranch_execnzback edge whose loop writes no scalar register except EXEC and VCC, whose EXEC writes can only narrow the mask (v_cmpx,s_and/s_andn2of EXEC with itself; a restore of a saved mask in the loop sets lanes an earlier round cleared, so it is refused) and whose VCC is not read after it (a lane that left changes nothing; no scalar count escapes; the wave's VCC after the loop holds the last compare, 0 for lanes that left earlier, which a lane running alone would not reproduce, so a later VCC read is refused: fixture withv_cndmask_b32_e32 ... vcc_loafter the loop);s_cbranch_scc0/scc1whose SCC is proven wave-uniform.The refusal is recorded at the branch and raised at the end of lowering, so every other refusal keeps its reason and order.
Tests
tests/startup_gfx2hlsl_compute_check.py: newcheck_wave_branches(vccz skip, SCC froms_and_saveexec, vccnz loop, execnz loop with a scalar counter, vccz/vccnz inside an execz block, an execnz loop that restores the saved EXEC in its body, an execnz loop with a VCC read after it: translated onmain, refused now; a uniform-SCC branch and a vector-only execnz loop still translate). One existing assertion changed: as_cbranch_vcczafter the join of an execz block (s_and_b32 vcc_lo, vcc_lo, exec_lo, then the branch) was expected to translate; it is the same whole-wave decision and is now expected to be refused, the other reads in that loop are unchanged.What stays open
Cache revision
The compute lowering now refuses programs it used to translate, so a translation cached under the old revision could still be served. This PR advances the startup shader cache revision to
x2w-startup-shader-28/28cs(runtime, offline tool, C++ test mirrors, docs, known-answer values recomputed with a separate FNV-1a loop). The C++ targets were not built here.Verification:
python3 -B tests/startup_gfx2hlsl_compute_check.pyandpython3 -B tests/startup_gfx2hlsl_compute_check.py binary llvm-mc-19: pass; the new shapes translate onmain'sgfx2hlsl.pyand are refused here.python3 -B tests/startup_compute_readfirstlane_check.py: pass.tests/*check.pysweep againstorigin/main: 0 scripts change result (356 pass, 131 fail in both trees; the fails need the Windows toolchain).AI-assisted; commands listed were executed locally.
@RavenCurseBurg on GitHub · 2026-11-26 07:43 UTC · original
I will independently check the accepted vector-only EXECNZ loop and the boundary where an SCC or VCC value escapes the loop. I will use source-built, per-lane readbacks and a separate wave reference, and report any counterexample or confirmation against this PR head here.
@R0Y00 on GitHub · 2026-11-26 07:48 UTC · original
I found a separate EXECNZ counterexample at
d35433de. The loop has no ordinary SGPR writes, but its first instruction widens EXEC from the saved entry mask. The current scalar-free shortcut accepts it. I spliced this before the store in startup_gfx2hlsl_compute_check.py case A:The first round leaves local X=0 running. The second round restores the saved mask, so the original wave stores 2 and 3 for the two X lanes. The translated shader stores 2 and 2. Windows Release WARP and RTX 4060 Laptop both report 144/320 mismatches, first word=1 expected=00000003 actual=00000002, with the debug layer enabled and no warnings/errors. The acceptance guard needs to prove that each EXEC write in this span only narrows the current mask. Keep the vector-only monotone loop as the positive control. This is independent of the SCC/VCC values escaping the loop that Raven is checking.
@tedin7 on GitHub · 2026-11-26 07:50 UTC · original · edited 2026-11-26 08:05 UTC
Thank you for the counterexample (the loop that restores the saved EXEC in its first instruction). Reproduced on
d35433de: the one-lane model stores 2 and 2, the wave stores 2 and 3.Fixed in
eb1e369e3(together with the review of the vccz/vccnz inside an execz block, below):s_cbranch_execnzback edge is accepted only when every EXEC write in the loop can only narrow the mask (v_cmpx,s_and/s_andn2of EXEC with itself). A move, an OR or a restore of a saved mask in the loop is refused. New refused fixture incheck_wave_branches(your shape, assembled words, translated before this commit). The vector-only monotone loop stays accepted as the positive control.s_cbranch_vccz/vccnz(and lane-data SCC) inside ans_cbranch_execzblock are refused again: the block proof covers its EXEC writes, not that condition. Onlys_cbranch_execnzstays exempt there. New refused fixtures for both branches.Verification:
python3 -B tests/startup_gfx2hlsl_compute_check.pyand... binary llvm-mc-19: pass; the new shapes translate onmainand are refused here.python3 -B tests/startup_compute_readfirstlane_check.py: pass;tests/*check.pysweep againstorigin/main: no script changes result.Update for the second report (the restore at the loop head, wave reference [3, 3, 3, 3], translated [1, 1, 2, 3]): this is the same defect, already fixed on the current head
eb1e369e3. The loop is refused by name because its head writes EXEC with a move; onlyv_cmpxands_and/s_andn2of EXEC with itself are accepted, so a restore anywhere in the loop is refused. The refused fixture incheck_wave_brancheshas the same shape (saved mask, restore at the head,v_cmp+s_andlatch,s_cbranch_execnz). Thank you for the paired readback and for confirming that the monotone loop and the post-loop SCC read stay correct and the post-loop VCC read is refused.AI-assisted; commands listed were executed locally.
@RavenCurseBurg on GitHub · 2026-11-26 07:52 UTC · original
I found an accepted loop that writes three wrong words at
d35433de. The EXECNZ exception inscalar_free_spanpermits a write to EXEC that reactivates lanes. The wave can run those lanes again while any lane takes the back edge.Four lanes start with
v3=0andv0=lane_id. Save the incoming EXEC ins20before the loop. This is the complete body and latch:Restore
exec_lofroms20after the latch and store each lane's counter. While lane 3 keeps the wave running, each trip restores all four lanes, so the wave reference is [3, 3, 3, 3]. The translated shader returned [1, 1, 2, 3] on Intel UHD 770, RTX 4060 and WARP. On each device the 320-word comparison had exactly three mismatches against the wave reference and zero against the lane-local vector. Removing the restore at the loop head made all 320 words match. Reading SCC after the monotone loop also passed, and a post-loop VCC select produced the intended named refusal.Please require a proof that every EXEC write in the accepted loop can only remove lanes. In particular, restoring a saved mask at the head needs the named refusal. The paired source-built fixtures and Python wave model are in this commit.
Command:
python -B tests/verify_execnz_loop_readback.py <clang> <llvm-objcopy> <dxc> <offscreen_check> <warp|hardware>. LLVM 15.0.7 gfx1013 wave32, DXC 1.9.2609.5, Windows 11 build 26100.9711. UHD 770 driver 32.0.101.5973, RTX 4060 driver 32.0.15.9579, WARP 10.0.26100.9711. Debug layer unavailable. AI-assisted with Codex.@tedin7 on GitHub · 2026-11-26 08:13 UTC · original · edited 2026-11-26 10:12 UTC
Independent Stage-2 gate of head
eb1e369e3: PASS.Checked:
vccz/vccnz/execnzand ansccon lane data are refused by name, includingvccz/vccnzinside a block thats_cbranch_execzskips (the earlier head accepted that shape and decided it per lane).s_cmpon a workgroup id) still translates; anexecnzloop translates only when its EXEC writes can only narrow (s_and exec, exec, Xin either order,s_andn2 exec, exec, X,v_cmpx);or,xor,mov,orn2,andwith a non-EXEC source ands_and_saveexecin the loop are refused; a VCC read after the loop is refused; lane-crossing ops in such a loop were already refused by their own checks.vcczafter the join) is right: a whole-wave decision.Verification (Win2025, WARP):
python3 -B tests/startup_gfx2hlsl_compute_check.pyandtests/startup_compute_readfirstlane_check.py: pass on Linux.origin/main: 0 scripts change result.x2w_pipe.py win2025 2772 --force --keep-release --regex 'gfx2hlsl|startup_compute|startup_.*readfirstlane': dev 444/444 and release 444/444 passed.--regex 'compute|gfx2hlsl', 649 tests) had one failure,compute_store_aot_mac_f32_hardware(compute_store_check.py, not touched by this PR); it fails the same way in the logs of other PRs that do not touch that file, and its_warptwin passes.Update, head
2743cc3ee(main merged in):fresh_gateGO (0 behindorigin/main); same shapes still refuse or translate as above;python3 -B tests/startup_gfx2hlsl_compute_check.pyandtests/startup_compute_readfirstlane_check.pypass;x2w_pipe.py win2025 2772 --force --keep-release --regex "gfx2hlsl|startup_compute|startup_.*readfirstlane": dev 446/446 and release 446/446 passed.Correction, head
157d06f64: the PASS above is withdrawn for one open shape. The Windows runs are green (x2w_pipe.py win2025 2772 --force --keep-release --regex 'gfx2hlsl|startup_compute|startup_.*readfirstlane|shader_cache_key': dev 453/453, release 453/453) but they do not cover it:spliced into case A of
tests/startup_gfx2hlsl_compute_check.pyit still translates. The SCC written by thes_and_b32on one arm is lane data (any lane's bit keeps SCC set for the wave) but the branch after the join is lowered from the lane's own bit: the lanes whose EXEC bit is clear skip the EXEC restore and do not store, while the wave would not take the branch. The same shape without the join is refused; at the joinscc_verifiedis false, so thescc_verified and not scc_uniformcondition does not fire. Fix idea: at a join makescc_uniformthe AND over the paths and refuse anscc0/scc1branch whenever it is false; add this shape tocheck_wave_branchesas refused. Back to Draft until then.AI-assisted; commands listed were executed locally.
@chatgpt-codex-connector[bot] on GitHub · 2026-11-26 08:13 UTC · original · edited 2026-11-26 08:18 UTC
Codex Review Summary: ✅ Completed
This comment shows the latest Codex review activity on this pull request.
eb1e369ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.
@Ybucaille on GitHub · 2026-11-26 08:15 UTC · original
Independent GPU confirmation of the compute case, and a note on scope.
Source-built compute fixture (8 lanes per group, 4 groups):
v_cmp_gt_u32 vcc_lo, 3, v0, thens_cbranch_vcczover abuffer_store. On the wave VCC is non-zero, so no lane skips and all 8 store.mainlowers it topc = ((vcc & 1) == 0) ? ... : ...per lane. Readback on WARP and on an RTX 2070: 20 of 320 words wrong (lanes 3 to 7 of each group lost their store). This shape is in yourcheck_wave_branches, so nothing to add there.Scope: the same per-lane lowering of
s_cbranch_vccz/vccnzand of an SCC branch on lane data is used for pixel and vertex programs, and this PR only guards compute. On a sample of 4000 cached graphics requests, 169 translate onmain; 5 of them (all pixel) branch withs_cbranch_scc0/scc1on an SCC computed from VCC/EXEC (s_and_b32 sN, vcc_lo, exec_loshape). No VCC branch and no vertex program in that sample.I have a branch that applies an equivalent refusal to the non-mesh graphics path. To avoid two versions of the same proof, I will not open it as is: once this PR lands I would follow up with the pixel/vertex extension reusing your helpers, unless you prefer to widen this PR yourself. I'll take the pixel/vertex part unless you say otherwise.
AI-assisted (Claude Code).
@chatgpt-codex-connector[bot] on GitHub · 2026-11-26 08:18 UTC · original · reviewed
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit:
eb1e369e3dℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
@chatgpt-codex-connector[bot] on GitHub · 2026-11-26 08:18 UTC · original
For a compute shader that reuses a uniform SCC after a forward join,
translate()retains the uniform reaching definition inscc_defand setsscc_verified=True, but resetsscc_uniform=False. This new condition therefore rejects a proven-uniform branch. For example, insertings_cmp_lt_u32 s7, 4followed by two forwards_cbranch_scc1 1instructions, each skipping a vector move, into case A now refuses the second branch; the parent translates it successfully. Both branches read the same group-ID comparison, with no intervening SCC write. Consult or preserve the retained uniform reaching-definition proof so these supported compute shaders continue to translate.Useful? React with 👍 / 👎.
@ -818,0 +833,4 @@for tok in destinations(name, parse(asm)[1]):r = reg_range(tok)if r and r[0] == 's' and not (r[1] >= 126 and r[2] <= 127) or tok == 'm0':return False@chatgpt-codex-connector[bot] on GitHub · 2026-11-26 08:18 UTC · original
For an EXEC-narrowing loop containing
v_cmpx_gt_u32_e32 s7, v3,destinations()returns the first printed operand, so this code incorrectly treats the read ofs7as a scalar-register write. CMPX prints only source operands and writes EXEC implicitly. Consequently, a vector-only increment/CMPX loop closed bys_cbranch_execnznow fails the scalar-free proof and is refused, although the parent translates it and the equivalent loop using literal2instead ofs7still translates. Use the actual scalar write effects or explicitly exclude CMPX sources when checking the loop.Useful? React with 👍 / 👎.
@R0Y00 on GitHub · 2026-11-26 08:20 UTC · original · requested changes
The original standalone EXECNZ loop now refuses on
2743cc3e. The same loop still translates and writes wrong data if it is inside a forward EXECZ span. Here is the splice for case A oftests/startup_gfx2hlsl_compute_check.py, with case A's original output code atjoin.The wave reference repeats [2, 3] for 288 words and preserves 32 sentinel words. Using the existing case A DXC dispatch harness, both WARP 10.0.26100.9818 and RTX 4060 Laptop 32.0.16.1656 disagreed at 144/320 words. First mismatch was word 1, expected 3 and received 2. Windows 11 build 26200, D3D12 debug layer enabled, zero debug warnings/errors.
The
execz_coveredexemption bypasses the EXEC-narrowing proof for this nested backward branch. The outer proof keeps lanes inactive at entry clear. The latch can still re-enable a lane that was active at entry and left an earlier iteration. Please require the loop proof for a backward EXECNZ inside the span as well, add this refusal, and retain the existing monotone-loop positive control.@RavenCurseBurg on GitHub · 2026-11-26 08:20 UTC · original
Verified the reopening fix at
eb1e369e(includesdb8a5ed7) with the same four-lane fixture. The loop that used to return [1, 1, 2, 3] instead of [3, 3, 3, 3] now stops at the nameds_cbranch_execnzrefusal. Both accepted controls, including the SCC observation, still match [1, 1, 2, 3] and all 316 untouched words on WARP, UHD 770 and RTX 4060. The live-VCC refusal also remains. This addresses my finding.@tedin7 on GitHub · 2026-11-26 08:28 UTC · original
Real, and fixed in the commit that follows this review: after a forward join the retained uniform reaching definition (
scc_def) now counts as uniform for the whole-wave branch check, so the seconds_cbranch_scc1ofs_cmp_lt_u32 s7, 4+ two forward branches translates again (it was refused on the previous head, translated onmain). New assertion incheck_wave_branches. Linux:python3 -B tests/startup_gfx2hlsl_compute_check.pypasses.AI-assisted; commands listed were executed locally.
@ -818,0 +833,4 @@for tok in destinations(name, parse(asm)[1]):r = reg_range(tok)if r and r[0] == 's' and not (r[1] >= 126 and r[2] <= 127) or tok == 'm0':return False@tedin7 on GitHub · 2026-11-26 08:28 UTC · original
Real, and fixed in the same commit: the scalar-free loop test now skips
v_cmpx(it prints only its sources and writes EXEC; the narrowing check judges that write). Av_add_nc_u32/v_cmpx_gt_u32 s7, v3loop closed bys_cbranch_execnztranslates again (refused on the previous head, translated onmain). New assertion incheck_wave_branches, with the decoder text of the hand-assembled word checked. Linux:python3 -B tests/startup_gfx2hlsl_compute_check.pypasses.AI-assisted; commands listed were executed locally.
@tedin7 on GitHub · 2026-11-26 10:12 UTC · original
Withdrawn: shader translation is moving to the native C++ frontend (see #870), so we are not continuing this change to the Python translator. The branch stays available if any part is useful later. Thank you to everyone who reviewed and tested it.
Pull request closed