Skip to content

startup: prove exec writes over every path of a compute execz block - #2509

Merged
R0Y00 merged 2 commits into
R0Y00:mainfrom
NiiRoZz:startup/gfx2hlsl-compute-execz-exec-write
Nov 25, 2026
Merged

R0Y00 merged 2 commits into
R0Y00:mainfrom
NiiRoZz:startup/gfx2hlsl-compute-execz-exec-write

Conversation

@NiiRoZz

@NiiRoZz NiiRoZz commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Second part of the compute s_cbranch_execz work after #2314. Refs #2274.

Problem

gfx2hlsl models one lane and emits s_cbranch_execz as a branch on that lane's exec bit. The wave skips the block only when no lane is active, so for a lane the model skips, the block may still run in the wave. Stage.execz_refusals therefore refuses a compute block unless it can show the block changes nothing of such a lane.

After #2314, an exec write in the block was accepted only in a straight-line block, and only when it narrowed exec or restored it from a copy made in the same block. Compiled programs nest an if/else or a loop in such a block, so nearly every exec write was refused: on a local set of 1,065 compute requests (not shareable), all 1,065 stopped at this check, 969 of them on an exec write.

Change

tools/startup/gfx2hlsl.py: the straight-line rule becomes a forward pass over the block's own control flow.

A fact (word, half) says: for every lane of that wave half (lanes 0-31 or 32-63) whose exec bit was clear at the branch, that lane's bit of the scalar word is clear. At the block's first instruction the facts are exec_lo for the low half and exec_hi for the high half. clear_after moves the facts across one instruction:

Instruction Destination holds the fact when
s_mov the source holds it
s_and either source holds it
s_or, s_xor both sources hold it
s_andn2 the first source holds it
s_*_saveexec as above with exec as the second source; D receives the old exec
s_andn2_wrexec as s_andn2 with exec; the result goes to exec and D
v_cmpx* exec keeps its fact (a compare only narrows)
literal 0 always
any other write never: the word loses its facts

Paths meet by intersection and the pass runs to a fixed point, so a fact at an instruction holds on every path to it, loops included. An exec write is accepted when exec still holds both entry facts after it.

Invariant: at every instruction the wave runs inside the block, a lane skipped by the model has its exec bit clear. Such a lane therefore writes no register and has no memory effect in the block, and its exec bit where the block joins is the one the model has. Scalar definitions in the block still have to be dead at the join; that rule is unchanged.

Still refused, each with the existing exec write reason:

  • a block that a branch from outside enters (the entry facts do not hold on that edge);
  • exec written from a word without the fact: a mask made before the block, a nonzero literal, a compare result, a loaded value;
  • forms the pass does not model (s_orn2, s_nand, s_nor, s_xnor and their saveexec forms, and anything else that writes exec);
  • a copy of the other wave half's exec word.

Removed: is_exec, exec_stays_clear, exec_copies_after, which the pass replaces.

Tests

tests/startup_gfx2hlsl_compute_check.py:

  • Device case M: an if/else inside an execz block, each arm with its own s_cbranch_execz, the else switch as s_andn2_b32 exec_lo, s0, exec_lo and the restore as s_mov_b32 exec_lo, s0. The check compares the exact HLSL tail and every output word for lanes that take the if arm, the else arm, and neither.
  • Mutation control m-else-unswitched (registered in CMakeLists.txt): the else switch without the complement must fail the comparison.
  • Accepted without a device: both else-switch forms, a loop that narrows exec, a loop that restores exec from an in-block copy, a zero-literal mask, s_andn2_wrexec from an in-block copy.
  • Refused: a loop whose body overwrites the saved copy (needs the fixed point, one pass accepts it), a nonzero literal, s_or_saveexec with a mask made before the block, s_andn2_wrexec from an overwritten copy, s_orn2, exec_hi ORed into exec_lo.
  • A direct table for clear_after.
  • Mutations run by hand, each turning the check red: s_or as union, a single pass instead of the fixed point, no intersection at joins, ignoring an entry from outside.

Commands and results (Windows 11 build 26300, Python 3.13 environment from requirements-dev.txt):

  • python -B tests/startup_gfx2hlsl_compute_check.py: exit 0
  • python -B tests/startup_translator_check.py: exit 0
  • every tests/startup_gfx2hlsl_*_check.py without arguments: all exit 0
  • git diff --check: clean
  • Dispatch of all compute cases on WARP: exit 0, case M matches on all 320 words; m-else-unswitched is rejected at word 1. Adapter Microsoft Basic Render Driver, driver 10.0.26100.10068, Windows build 26300, debug layer on, DXC from Vulkan SDK 1.4.363.0. The offscreen_check runner was built from another branch of this repository, not from this commit (this change does not touch its sources); the test script and translator are this commit's.

Not run: the binary mode (no gfx1013 llvm-mc here; the new instruction words are hand-assembled and checked only against the repository's decoder), a hardware GPU, the full CMake/ctest build.

Measurement

First refusal per request on the same 1,065 compute requests, with a local baseline copy of the translator and the same copy with this change applied (not the branch itself):

First refusal Before After
execz block: exec write 969 17
execz block: definition live at the join 94 846
execz block: effect outside scalar registers 2 2
another refusal, past this check 0 197
translated 0 3

The 17 remaining exec writes: 14 in blocks entered from outside, 3 s_or_saveexec_b64 of a literal. The 846 live definitions were hidden behind the exec write before; they are a separate rule and are not changed here. The largest forms are a compare into vcc (341; in the ones I read, the join only ANDs it with exec) and s_andn2 R, R, exec (348; in the ones I read, the join moves R into exec).

Overlap with #2505

#2505 (open) changes the same function for a different case: a lane mask defined in the block and live at the join. It does not cover exec writes, and this change does not cover that live definition, so the two are complementary in scope. They do not merge together as written: #2505's argument uses exec_stays_clear and "exec can only narrow inside the block"; this change removes that helper and accepts an else switch and a restore, after which exec no longer only narrows. Whichever lands second needs a rework; I will rebase this one if #2505 merges first.

Limits

  • Nothing here ran on a hardware GPU.
  • The table rows come from the RDNA 2 instruction descriptions (saveexec computes S0 op EXEC); only the case M forms are checked against a device result.
  • The pass is not path-sensitive beyond the control-flow graph: a mask that is clear only under a condition is refused.

🤖 Generated with Claude Code

A block that s_cbranch_execz skips per lane kept every exec write refused
unless the block was straight-line. The check now runs a forward pass over
the block: for each instruction the wave can reach from the branch it keeps
the scalar words in which a skipped lane's bit is known clear on every path,
branches and loops inside the block included, with the wave half each fact
holds for. An exec write is accepted when it leaves exec among them.

That accepts an if/else whose arms have their own s_cbranch_execz, the
s_or_saveexec/s_xor switch, s_andn2_wrexec, loops, and masks built from a
zero literal, when every restore reads a mask saved inside the block. A
block entered by a branch from outside, a mask from before the block or
from only some paths, the forms the pass does not follow and a copy of the
other exec half stay refused.

Case M runs an if/else block on a D3D12 device; a control that drops the
switch to the else arm must mismatch.

Refs R0Y00#2274

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 0b7d61d: code and fixture review only. No issues found in the code. The merge stays with @R0Y00 or MasterDomino.

Why we do not merge it: the description measures the change on a local set of compute requests (0 -> 3 translated). We cannot verify that claim without the game. The merge should be done by someone who can.

What I checked in tools/startup/gfx2hlsl.py:

  • clear_after follows the stated table. s_and unions the facts. s_or and s_xor intersect them. s_andn2 and a move keep the first source. v_cmpx keeps the exec facts. Any other write, including a compare result, drops the facts of what it may write.
  • The saveexec forms (old exec to D) and the wrexec form (result to exec and D) match the RDNA 2 ISA. The b32 and b64 word counts are consistent.
  • Facts carry a wave half, so exec_hi copied into exec_lo is refused. The pure-Python table covers that case.
  • The fixed point meets paths by intersection. The | entry in the loop does not hide a bad write: every exec write is checked separately against before[i], so one that loses the facts is refused.
  • A block entered from outside keeps its exec writes refused (entered).
  • Calculated s_setpc_b64 jumps are in the jump list, so a proven one that enters the block also keeps its exec writes refused.

Tests:

  • Case M puts an if/else in the skipped block. It checks the exact HLSL tail and every output word on WARP, and has the negative control m-else-unswitched. The control is registered in CMake for each adapter.
  • Pure-Python cases cover accepted and refused forms: if/else, loops, literal masks, saveexec, wrexec, orn2, a half mismatch.
  • Single-step cases compare the exact clear_after result.
  • Mutations I made, each of which fails the check:
    • s_and combined by intersection instead of union: the translation of case M is refused (exec write).
    • s_or combined by union instead of intersection: a refusal that should happen is accepted.

Commands run on Linux:

  • python3 -B tests/startup_gfx2hlsl_compute_check.py (pass)
  • python3 tests/docs_links_check.py (pass)
  • python3 tests/bidi_controls_check.py (pass)
  • git diff --check origin/main...HEAD (clean)

Not run by me: Win2025 WARP (case M dispatch and the control), and any run on the game. The PR is 32 commits behind main.

Related: #2505 also touches the compute execz area (i1-phi lane mask). It looks like a different slice (I did not review it), but whoever merges both should check they do not overlap in Stage.execz_refusals.

AI-assisted; commands listed were executed locally.

@R0Y00
R0Y00 merged commit 962dcf0 into R0Y00:main Nov 25, 2026
5 checks passed
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