Repository navigation
startup: recompute uniform v_readfirstlane_b32 sources past the entry block - #2203
Conversation
… block Past the entry block of a program with branches, every v_readfirstlane_b32 was refused. The read now recomputes the uniform expression that defined its VGPR, from scalars and constants, instead of copying this lane's VGPR. That is the first active lane's value when, between the definition and the read, there is no branch target, no EXEC write, no write of the VGPR and no write of a scalar the expression reads. Every scalar source must also have been proven uniform since the last branch target. A lane inactive at the definition keeps an older VGPR value, which the recomputed expression does not read, so EXEC need not be known. The entry block and branch-free programs keep copying the VGPR. tests/startup_gfx2hlsl_readfirstlane_recompute_check.py accepts reads after a join, after an EXEC change before the definition, and across a fall-through. It refuses an EXEC write between definition and read (wider, narrower, v_cmpx, WQM), a rewritten or reloaded scalar input, a loop back edge, a join of two definitions, a scalar or SCC from before the join, a lane-varying or lane-mask source, an overwritten VGPR and a chain across an EXEC write. It also covers the SGPR writes that do not go through the first operand. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…XEC destination Review follow-up. No case had a second join, so resetting the since-join marks only at the first branch target went unnoticed; a refused case now reads a scalar that a loop back to the definition turns into a lane mask. writes_exec now checks every destination operand (the VOP3b SGPR one too), through a destinations() helper shared with forget_writes. The uniformity docs no longer say every read needs a known exec, and list the read after the first terminator as recomputed, not refused. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
tedin7
left a comment
There was a problem hiding this comment.
Review of head 144114786037f15f95ca40d1090e6c85fba64e4c (comment only, no approval).
Summary: the approach is sound. Past the entry block, v_readfirstlane_b32 is lowered to the uniform expression that defined the VGPR, rebuilt from scalars and constants. This avoids copying a stale lane VGPR. No P1 or P2 findings.
Checks run locally (Linux):
git diff --stat origin/main...HEAD: 7 files, +231/-9. Title is neutral, and the diff has no model names or probe mentions.python3 tests/startup_gfx2hlsl_readfirstlane_recompute_check.py:0 failedpython3 tests/startup_gfx2hlsl_readfirstlane_uniform_check.py:0 failed- All 44
tests/startup_gfx2hlsl*.pyscripts: exit 0 python3 tests/docs_links_check.py: passed (18 files, 405 links)python3 tests/bidi_controls_check.py: passedgit diff --check origin/main...HEAD: clean- CI (
gh pr checks): Linux checks, gfx1013 fixtures and Windows build/test are green./O2and chart jobs were skipped. The PR is mergeable.
Mutations (each reverted with git checkout -- .), all killed by the new test:
- Skip the
uniform_v.clear()at a join: 2 failed - Skip the
uniform_expr.clear()on an EXEC write: 4 failed - Skip the dependent-expression drop on an SGPR write: 5 failed
- Drop the
since_joinguard inuniform_source: 5 failed - Ignore the VOP3b SGPR destination (
destinations): 3 failed
Logic read:
- Soundness argument holds: the first active lane ran the definition, because no join, EXEC write, VGPR overwrite or write of a read scalar lies between definition and read. Each of these is covered by
forget_writes, the join reset andset_v/swap. v_swap_b32moves the expression together with the uniform mark. Good.- Nit (no change needed):
forget_writestreats operand 0 ofs_store_*-style instructions as a destination. This is conservative (it only drops proofs), so it is safe. - Nit:
since_joinadds an SGPR only whenset_sproves it uniform. An SGPR written by an unmodelled path is dropped byforget_writes. This is correct, but a one-line comment on that invariant nearset_swould help future readers.
Not run: no Windows WARP run. The change is Python-only lowering inside gfx2hlsl.py, and it only adds refusals or exact scalar expressions. The PR body and CI show the Windows suite passing. A real D3D12 readback of the recomputed expression is still the gate before Ready, as per the project process.
AI-assisted; commands listed were executed locally.
…firstlane-recompute # Conflicts: # tools/startup/gfx2hlsl.py
|
Merged current Re-run on the merged head: the registered recompute check and the readfirstlane, uniform-lane and op-refusals checks pass, all 18 mutants are still killed, AI-assisted (Claude Code). |
tedin7
left a comment
There was a problem hiding this comment.
Re-review of head a30e744c65388d19e525463b02899f989f7c5770 (comment only, no approval).
What changed since the previous review (14411478): only a merge of origin/main. The PR's own files are unchanged: tools/startup/gfx2hlsl.py and both tests/startup_gfx2hlsl_readfirstlane_*_check.py are identical, and the earlier nit (comment on the since_join invariant near set_s) is now in the code. My earlier findings still stand: no P1, no P2.
Checks run locally (Linux):
git diff --stat origin/main...HEAD: 7 files, +233/-9. Title is neutral. The diff has no model names.python3 tests/startup_gfx2hlsl_readfirstlane_recompute_check.py:0 failedpython3 tests/startup_gfx2hlsl_readfirstlane_uniform_check.py:0 failed- All
tests/startup_gfx2hlsl*.pyscripts: exit 0 python3 tests/docs_links_check.py: 18 files, 406 links OKpython3 tests/bidi_controls_check.py: OKgit diff --check origin/main...HEAD: cleangh pr checks: Linux checks, gfx1013 fixtures and the Windows build/test are green. The/O2and chart jobs are skipped. The PR is mergeable.
Mutation (reverted with git checkout -- .):
- Replace the
uniform_v.clear()at a join withpass: the recompute test reports2 failed, so it is killed. The other mutations from the previous review were not re-run, because the logic is byte-identical.
Not run: no Windows WARP run in this review. The Python-only lowering is unchanged since the last review and CI Windows is green on this head. A real D3D12 WARP readback of the recomputed expression remains the gate for the project's process.
Nit: the PR is currently not Draft. If the Win2025 WARP gate has not passed on this head, consider converting it back to Draft first.
AI-assisted; commands listed were executed locally.
…ites A VGPR written under a partial exec from a lane-invariant value already got a shadow xvN for v_readfirstlane_b32. The unsigned-division reciprocal that compilers emit inside a branch writes the source four times, each write reading the previous one: v_cvt_f32_u32 v1, sN; v_rcp_f32 v1, v1; v_mul_f32 v1, 0x4f7ffffe, v1; v_cvt_u32_f32 v1, v1; v_readfirstlane_b32 sM, v1 Only the first write was lane-invariant, so the read was refused. Now a partial write whose VGPR inputs are uniform or same-epoch partial writes gets a shadow expression over their shadows. At the read the chain is materialized in program order: each write becomes `xvN = <shadow>; if (exec & 1) vN = xvN;`. Exec written anywhere in the chain, or a branch target inside it, still refuses. This is the idea of upstream PR R0Y00#2203 (recompute the scalar expression that defined the source), written for this translator's one-lane model. Our own code; no upstream code copied. Fixture tests/startup_readfirstlane_chain_check.py covers the 4-write chain and a 2-register chain. Its 4 controls are refused: a per-lane link, exec written inside the chain, exec written after it, and a branch target inside it. Negative control: the single-write rule refuses the chain. Census (tools census, all refusals per key, 1405 non-ok keys of the request set), distinct (vs, ps) pairs: - ps readfirstlane 451 -> 239; - vs readfirstlane 167 -> 40; - ps "numeric use of sN holding desc" 409 -> 220. 6 more keys translate fully. The HLSL of every key that translated before is byte-identical (1135 keys).
Change
Part of #1723. Follows #1931, #2153 and #2183.
Since #2183,
v_readfirstlane_b32in a program with branches is proven only in the entry block. Past it, every read is still refused. Thereexecis usually unknown (after an else arm, a WQM toggle or a restore). Copying this lane's VGPR is then wrong for a lane that was inactive at the definition, because its VGPR still holds an older value.This change lowers such a read to the uniform expression that defined the VGPR, rebuilt from scalars and constants. For example, after
v_mul_f32 v7, s3, 2.0the read becomess9 = U(F(s3) * 2.0);.Why the value is right. The hardware returns
vSfrom the first active laneLat the read. IfLran the definitionvS = E(x), thenvS[L] = E(x), wherexis a set of SGPRs and constants. Ifxis unchanged since the definition, every lane can computeE(x)from its own scalar copies.Lran the definition when, between the definition and the read:execwrite. That covers explicit writes,saveexec,wrexec,v_cmpx, WQM,s_movreldand any write tos[126:127]. Soexecat the read equalsexecat the definition, andLwas active at the definition. Narrowing is refused as well as widening, which is stricter than needed.vS. This is the existing overwrite rule.Ereads. VOP3b carry-out and division-scale destinations count as writes, ands_movreldcounts as a write to every SGPR.E. An SCC from before the last branch target is not trusted.The definition may run under any
exec, because the read never looks at an inactive lane's VGPR copy. The one case where hardware can differ is a read with no active lane. The ISA then returns lane 0's VGPR, which compiled code cannot rely on.Unchanged:
s_endpgmin a branch-free program stays refused.What it enables: reads in later blocks whose defining expression is uniform and unchanged since the last join. Reads whose scalars come from before the last join stay refused.
Sites that read a
v_med3_f32result also need the separate change that addsv_med3_f32to the uniform set. This branch does not depend on it.Relation to #2060 (open, tedin7, head
c346072c, reviewed for this draft): #2060 also proves reads past the entry block, on different conditions.execwritestainted_s) marks themexecwrite. There each accepts reads the other refuses.uniform_sourceunconditionally, so it would also refuse pre-join scalars that shader: extend v_readfirstlane_b32 lowering to branched control flow in gfx2hlsl (Part of #1723) #2060 trusts. If both land, the since-join test should apply only where shader: extend v_readfirstlane_b32 lowering to branched control flow in gfx2hlsl (Part of #1723) #2060's proof does not.docs/experiments.mdparagraph.execsomewhere.Verification
Run on
fd457299(origin/main) plus this branch (051b0919, then14411478after review).origin/mainat19e04be9merges without conflict.python -B tests/startup_gfx2hlsl_readfirstlane_recompute_check.py:Readfirstlane recompute checks: 0 failed.python -B tests/startup_gfx2hlsl_readfirstlane_uniform_check.py:Readfirstlane uniformity checks: 0 failed. The case "read after the first terminator" moves from refused to recomputed, which is the intended change.python -B tests/startup_uniform_lane_check.py: passes.python -B tests/startup_gfx2hlsl_op_refusals_check.py:gfx2hlsl op refusals: 3 cases passed.python -B tests/docs_links_check.py: passes.git diff --check origin/main...HEAD: clean. The changed files are LF in the index.CMakeLists.txt(startup_gfx2hlsl_readfirstlane_recompute), the platform-independent Python job in.github/workflows/windows.yml, andAGENTS.md.All 18 mutants of the rule fail the two checks:
execwrite keeps expressionss_movreldwrites nothingv_cmpxnot counted as anexecwriteexectest sees only the first destinationexecrange off by ones_endpgmrecomputedLocal measurement, not reproducible from the repository: 266 graphics pipelines used by indirect draws in one captured frame, plus 117 cached requests from other local runs, 383 requests in all.
translate_request). The 75 requests that translated before give byte-identical HLSL.request_refusals): 160 requests lose at least one refused read, and no request gains one. Refused read sites drop from 616 to 438. In this mode an unsupported instruction is skipped, so its VGPR write is not seen. Some reads counted as cleared may therefore read an overwritten source. The first-refusal results and the byte-identical translating requests are the firm numbers.Evidence and follow-up
The fixture is hand-written disassembly text. It is not taken from any title.
tests/startup_gfx2hlsl_readfirstlane_recompute_check.pyaccepts:execnarrowing or restore before the definitionIt refuses:
execwrite between definition and read: widened (a definition under a narrowerexecthan the read), narrowed,v_cmpx, WQMexecwriteIt also covers the SGPR writes that do not go through the first operand, and the
execwrite detector on every destination operand (a VOP3b carry-out intoexec_loincluded).Follow-up, not in this change:
buffer_load_format_x v0, off, ...) need a proof for that load. The proof depends on how the formatted load is lowered; startup: lower buffer_load_format_x/xy/xyz with the format from the buffer descriptor #2138 is open.docs/experiments.md(uniform sources ofv_readfirstlane_b32) states the rule. Reproduce with:Checklist
AI-assisted (Claude Code). All commands above were run locally.
🤖 Generated with Claude Code