Skip to content

startup: prove v_readfirstlane_b32 sources in the entry block of branched programs - #2183

Merged
R0Y00 merged 2 commits into
R0Y00:mainfrom
NiiRoZz:startup/gfx2hlsl-readfirstlane-entry-block
Nov 25, 2026
Merged

R0Y00 merged 2 commits into
R0Y00:mainfrom
NiiRoZz:startup/gfx2hlsl-readfirstlane-entry-block

Conversation

@NiiRoZz

@NiiRoZz NiiRoZz commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Change

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

v_readfirstlane_b32 is lowered to a copy (sD = vS) only when vS is proven uniform. Until now, a branch anywhere in the program refused every read. The uniformity tracking (known exec, scalar uniformity, VGPR proofs) is kept in program order, and program order is not the execution order after a join.

Before the first branch target and before the first terminator, every path has executed exactly the instructions above. Program-order tracking is therefore exact there, as in straight-line code. This change accepts proofs and reads in that entry block:

  • Where the entry block ends: past_entry becomes true at the first block boundary that translate() already makes, which is a branch target or the instruction after a terminator.
  • Loops: a branch back to offset 0 (a loop that re-enters the entry block) puts the whole program past the entry.
  • Dead code: code after s_endpgm in a branch-free program also counts as past the entry block. Otherwise branch-free programs are unchanged.
  • Unchanged: the exec, source and overwrite rules. has_branches keeps its other use, the depth-export check.

Relation to other work:

Verification

Run on e3e39c43 plus 7a3037fb and 0068cde4. origin/main (a2010c91) merges without conflict.

  • python -B tests/startup_gfx2hlsl_readfirstlane_uniform_check.py: Readfirstlane uniformity checks: 0 failed.
  • python -B tests/startup_uniform_lane_check.py: fixed scalar buffer value translated; five unproven reads rejected.
  • 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.

Mutants of the rule, each applied alone to a copy of gfx2hlsl.py and run against the check:

Mutant Result Killing cases
drop the block-boundary test killed read at the first branch target, read after the first terminator, read in a loop whose header is in the entry block, definition in another block
targets do not split blocks killed read in a loop whose header is in the entry block, read at a backward branch target
conditional-branch targets ignored killed read in a loop whose header is in the entry block, entry block re-entered by a loop
a read at a branch target counts as inside killed read at the first branch target, read in a loop whose header is in the entry block, read at a backward branch target
drop the loop-to-entry test (0 in targets) killed entry block re-entered by a loop
off by one block (len(blocks) > 1) killed read after the first terminator, read at a backward branch target
code after s_endpgm counts as inside a branch-free program killed read after s_endpgm in a branch-free program
never past the entry killed the boundary refusals
any branch refuses (old gate) killed branch after read, read before the first branch target, chain in the entry block
proof gate or read gate left on has_branches killed the same three accepted cases

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 in all), translated offline with translate_request against e3e39c43.

  • 191 requests move past v_readfirstlane_b32 requires a proven uniform source.
    • 188 of the 266 stop next at s_mul_hi_u32.
    • The other 3 are cached requests: 2 stop next at s_mul_hi_u32 and 1 at s_pack_ll_b32_b16.
  • The 75 requests that translated before give byte-identical HLSL.
  • No request translates fully because of this change alone.

Evidence and follow-up

The fixture is hand-written disassembly text in the existing check's style. It is not taken from any title.

Two existing refusals become accepted cases, which is the intended change: "branch after read" (#1931) and #2153's "chain in a program with branches". In both, the read sits in the entry block.

Accepted cases:

Refused cases:

  • a read at the first branch target
  • a read after the first terminator
  • a read in a loop whose header is in the entry block
  • a read at a backward branch target
  • an entry block re-entered by a loop
  • an exec write in the entry block
  • a lane mask in the entry block
  • a definition in another block
  • a read after s_endpgm in a branch-free program

Follow-up, outside this change:

  • Reads past the entry block are still refused.
  • s_mov_b32 exec_hi, s2 raises a TypeError in scalar() instead of a refusal. That is older than this change.

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

ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_readfirstlane_uniform$' -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 03:49
…ched programs

A program with any branch refused every v_readfirstlane_b32. Before the
first branch target and the first terminator, every path has executed
exactly the instructions above, so the program-order tracking of exec,
scalar uniformity and VGPR proofs is as exact as in straight-line code.
Proofs and reads are now accepted there. A branch back to the first
instruction keeps the whole program refused, because a loop re-enters
the entry block. Branch-free programs are unchanged.

The check moves "branch after read" to the accepted cases, adds the
read just before the first branch target, and refuses a read at the
first branch target, after the first terminator, in an entry block
re-entered by a loop, after an exec write and from a lane mask.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
… s_endpgm

Review follow-up. A forward branch reaches its terminator before its
target, so no case pinned the rule that a branch target alone ends the
entry block. Two refused cases now do: a read in a loop whose header is
in the entry block, and a read at a backward branch target. Code after
s_endpgm in a branch-free program now also counts as past the entry
block (dead code), and a refused case covers it. The test docstring and
the CMake comment no longer say straight-line only.

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 0068cde4fbabd601f33a73fc041835110be7155c (comment only, no approval).

Result: no P1, no P2 findings.

What I checked:

  • past_entry is set in translate() (tools/startup/gfx2hlsl.py:708) after the block-boundary split, so it is true exactly at a branch target (off in targets and off != start), after any terminator (self.blocks non-empty), or when a branch targets offset 0 (loop re-entering the entry block). That matches the claim: before those points every path has run the same instructions, so program-order tracking is exact.
  • Both gates (UNIFORM_VALU proof at line 759, op_v_readfirstlane_b32 at line 1737) switched from has_branches to past_entry. Missing-state probes still default to refuse (getattr(..., True)). has_branches is kept for the depth-export checks (lines 880, 2065).
  • Backward branch to a target inside the entry block: the target is in the precomputed targets, so the read at or after it is past the entry. Covered by tests read in a loop whose header is in the entry block and read at a backward branch target.
  • Test changes only move the old branch refusals into precise accepted/refused cases. No test was weakened: the entry-block cases are accepted, and the first target, first terminator, loop-to-0, exec write, mask and dead-code-after-s_endpgm cases are refused.
  • Docs sentence and CMake comment match the behavior. Mergeable, CI green (Linux Python, Windows build and test, gfx1013 fixtures), title neutral, no model or title names.

Commands executed locally in a worktree at that head:

  • python3 -B tests/startup_gfx2hlsl_readfirstlane_uniform_check.py -> Readfirstlane uniformity checks: 0 failed
  • python3 -B tests/startup_uniform_lane_check.py -> fixed scalar buffer value translated; five unproven reads rejected
  • python3 -B tests/startup_translator_check.py -> startup translator checks: 0 failed
  • python3 -B tests/startup_gfx2hlsl_scalar_m0_check.py -> startup scalar m0 controls passed
  • python3 -B tests/startup_gfx2hlsl_refusals_check.py -> ran to the end, last line normalization of unexpected value N in source: ok
  • python3 tests/docs_links_check.py -> Checked 18 Markdown files and 401 links.
  • python3 tests/bidi_controls_check.py -> no bidirectional controls.
  • git diff --check origin/main...HEAD -> clean

Mutations (reverted with git checkout -- .):

  • past_entry = bool(self.blocks) (drop the loop-to-0 case): 1 failed, killed.
  • past_entry = 0 in targets (drop the terminator/target boundary): 6 failed, killed.

Notes (not blocking):

  • Nit: in refusals-census mode (refusals list set), an instruction whose handler raises is skipped before the terminator bookkeeping. If a refused terminator ever occurred, the block would not close at that point. It does not affect the translated output on the normal path and is not reachable for the terminators now modelled.
  • The author already notes the integration with #2060 (tainted_s should be limited to code past the entry block once both land). Agreed, whichever lands second needs it.
  • No shader output changes for branch-free programs, so I did not queue a Windows GPU run. The CI Windows job passed.

AI-assisted; commands listed were executed locally.

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