Skip to content

startup: recompute uniform v_readfirstlane_b32 sources past the entry block - #2203

Merged
R0Y00 merged 3 commits into
R0Y00:mainfrom
NiiRoZz:startup/gfx2hlsl-readfirstlane-recompute
Nov 25, 2026
Merged

R0Y00 merged 3 commits into
R0Y00:mainfrom
NiiRoZz:startup/gfx2hlsl-readfirstlane-recompute

Conversation

@NiiRoZz

@NiiRoZz NiiRoZz commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Change

Part of #1723. Follows #1931, #2153 and #2183.

Since #2183, v_readfirstlane_b32 in a program with branches is proven only in the entry block. Past it, every read is still refused. There exec is 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.0 the read becomes s9 = U(F(s3) * 2.0);.

Why the value is right. The hardware returns vS from the first active lane L at the read. If L ran the definition vS = E(x), then vS[L] = E(x), where x is a set of SGPRs and constants. If x is unchanged since the definition, every lane can compute E(x) from its own scalar copies. L ran the definition when, between the definition and the read:

  1. No branch target. A branch target is a join, loop head or back edge, and it clears every VGPR proof. The path from definition to read is therefore a straight run. Conditional branches between them are fall-throughs and are allowed.
  2. No exec write. That covers explicit writes, saveexec, wrexec, v_cmpx, WQM, s_movreld and any write to s[126:127]. So exec at the read equals exec at the definition, and L was active at the definition. Narrowing is refused as well as widening, which is stricter than needed.
  3. No other write to vS. This is the existing overwrite rule.
  4. No write to an SGPR that E reads. VOP3b carry-out and division-scale destinations count as writes, and s_movreld counts as a write to every SGPR.
  5. Every SGPR source was proven uniform since the last branch target. Program-order tracking is exact only on the straight run, so a value or lane mask from another path cannot reach 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:

  • The entry block keeps copying the VGPR.
  • Branch-free programs keep copying the VGPR.
  • Dead code after s_endpgm in 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_f32 result also need the separate change that adds v_med3_f32 to 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.

#2060 This change
Proof scope one basic block the straight run since the last branch target (fall-throughs allowed)
exec writes none allowed anywhere in the program; no loop back to offset 0 allowed elsewhere; none between definition and read
Lowering copies the VGPR recomputes the expression
Scalars from before a join trusted unless a whole-program scan (tainted_s) marks them refused

Verification

Run on fd457299 (origin/main) plus this branch (051b0919, then 14411478 after review). origin/main at 19e04be9 merges 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.
  • The new check is registered in three places: CMakeLists.txt (startup_gfx2hlsl_readfirstlane_recompute), the platform-independent Python job in .github/workflows/windows.yml, and AGENTS.md.

All 18 mutants of the rule fail the two checks:

  • no VGPR clear at a join
  • no since-join reset at a join
  • since-join reset only at the first join
  • no SCC reset at a join
  • no since-join test on sources
  • an exec write keeps expressions
  • a scalar write keeps expressions
  • a scalar write keeps its since-join mark
  • carry-out SGPR not counted as a write
  • s_movreld writes nothing
  • compare counted as a write
  • v_cmpx not counted as an exec write
  • the exec test sees only the first destination
  • exec range off by one
  • the read copies the VGPR past the entry
  • the past-entry read needs no expression
  • dead code after s_endpgm recomputed
  • an unproven VGPR source keeps an expression

Local 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.

  • First refusal: unchanged for every request (translate_request). The 75 requests that translated before give byte-identical HLSL.
  • All refusals (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.
  • Still refused: no request has all of its refused reads cleared. The ones that remain mostly read a scalar written before the last join, which needs a whole-program argument such as shader: extend v_readfirstlane_b32 lowering to branched control flow in gfx2hlsl (Part of #1723) #2060's.

Evidence and follow-up

The fixture is hand-written disassembly text. It is not taken from any title.

tests/startup_gfx2hlsl_readfirstlane_recompute_check.py accepts:

  • a definition and read after a join
  • an exec narrowing or restore before the definition
  • a fall-through between definition and read
  • a chain through a second VGPR
  • an unrelated scalar write in between

It refuses:

  • an exec write between definition and read: widened (a definition under a narrower exec than the read), narrowed, v_cmpx, WQM
  • a scalar input rewritten, reloaded, or rewritten as a pair
  • a loop back edge between definition and read
  • a join where two definitions reach
  • a scalar written before the join
  • a scalar written before the second join (a loop back to the definition turns it into a lane mask)
  • an SCC select from before the join
  • a lane-varying source or a lane-mask source
  • an overwritten VGPR
  • a chain across an exec write

It also covers the SGPR writes that do not go through the first operand, and the exec write detector on every destination operand (a VOP3b carry-out into exec_lo included).

Follow-up, not in this change:

  • Reads of a formatted buffer load with no address (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.
  • Scalars from before a join need a whole-program argument.
  • Two points the independent review did not check. Both predate this change:
    • How the compiler treats the recomputed expression compared with the original definition. By construction it uses the same helpers in the same order.
    • The per-lane lowering of wave-level conditional branches. A lane that skips a region keeps its old scalars, which is why scalars written before the last join are not trusted here.

docs/experiments.md (uniform sources of v_readfirstlane_b32) states the rule. Reproduce with:

ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_readfirstlane_(uniform|recompute)$' -V

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted (Claude Code). All commands above were run locally.

🤖 Generated with Claude Code

NiiRoZz and others added 2 commits November 25, 2026 05:17
… 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 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 failed
  • python3 tests/startup_gfx2hlsl_readfirstlane_uniform_check.py: 0 failed
  • All 44 tests/startup_gfx2hlsl*.py scripts: exit 0
  • python3 tests/docs_links_check.py: passed (18 files, 405 links)
  • python3 tests/bidi_controls_check.py: passed
  • git diff --check origin/main...HEAD: clean
  • CI (gh pr checks): Linux checks, gfx1013 fixtures and Windows build/test are green. /O2 and 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_join guard in uniform_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 and set_v/swap.
  • v_swap_b32 moves the expression together with the uniform mark. Good.
  • Nit (no change needed): forget_writes treats operand 0 of s_store_*-style instructions as a destination. This is conservative (it only drops proofs), so it is safe.
  • Nit: since_join adds an SGPR only when set_s proves it uniform. An SGPR written by an unmodelled path is dropped by forget_writes. This is correct, but a one-line comment on that invariant near set_s would 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
@NiiRoZz

NiiRoZz commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Merged current main into the branch (a30e744c): the one conflict was a single line in set_v, where this change's capture of the last definition met the low-16 bookkeeping added by #2139; both are kept. The D16 image_load path writes through set_v, so proofs are dropped there as for any other VGPR write. Also added the requested one-line comment near set_s on the since_join invariant.

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, git diff --check is clean, and translator output is unchanged on the local sample.

AI-assisted (Claude Code).

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 failed
  • python3 tests/startup_gfx2hlsl_readfirstlane_uniform_check.py: 0 failed
  • All tests/startup_gfx2hlsl*.py scripts: exit 0
  • python3 tests/docs_links_check.py: 18 files, 406 links OK
  • python3 tests/bidi_controls_check.py: OK
  • git diff --check origin/main...HEAD: clean
  • gh pr checks: Linux checks, gfx1013 fixtures and the Windows build/test are green. The /O2 and chart jobs are skipped. The PR is mergeable.

Mutation (reverted with git checkout -- .):

  • Replace the uniform_v.clear() at a join with pass: the recompute test reports 2 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.

@R0Y00
R0Y00 merged commit 342a193 into R0Y00:main Nov 25, 2026
5 checks passed
denn0x added a commit to denn0x/Xbox2Windows that referenced this pull request Nov 25, 2026
…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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants