Repository navigation
gfx2hlsl: keep a scalar constant across a branch target when its write dominates - #2460
Conversation
s_nand_b32, s_nor_b32 and s_xnor_b32 were refused whenever an operand was a lane mask. Bit i of ~(S0 op S1) depends only on bit i of each source, so with every source a lane mask (vcc_lo, exec_lo or an SGPR holding a compare mask) and an SGPR destination the lane's bit of the result is exact. SCC is (the whole complemented word != 0), which the bits of the other lanes decide. It is not emitted: the write is recorded as an SCC effect that is not modelled, and the existing stale-SCC scan refuses a program that can read it before the next SCC write, through joins and back edges. The mask mark follows program order. At a branch target a marked SGPR stays proven only if its last write dominates the target and no block on a path from that write to the target writes it again; a complement on any other marked SGPR is refused until the register is written again. A mask mixed with a plain word or a constant, and a vcc_lo, exec_lo or m0 destination, stay refused. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
… dominates At a branch target the translator drops every scalar constant whose register is in the target's joined set. That set also holds the writes of every earlier block that is off the dominator chain, so a register used inside an earlier conditional block and then set to a constant on every path lost the constant at each later join. A constant sampler built from s_movk_i32, s_bfm_b32 and s_mov_b64 then reached the image instruction as four plain words and was refused as a descriptor operand without a symbol. The constant is now dropped only when it is not proven by its own write: the block of the register's last write must dominate the target and no block on a path from that write to the target may write the register again (the rule the lane-mask complement uses for mask marks, now shared as Stage.unjoined). A later arm or a loop body that writes the register still drops it. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
tedin7
left a comment
There was a problem hiding this comment.
Review of head 661c9a1 (Linux checks only; no Windows run by me). This branch is stacked on #2459: its first commit (d94dae5) is that PR's head, so I reviewed only the second commit (git diff HEAD~1 HEAD: gfx2hlsl.py, loop_constants check, a 6-line tweak to the complement check).
What I checked
Stage.unjoined(target, last_write, regs)generalizesunjoined_masks. For constants it keeps a scalar constant at a join only when the block of its last write dominates the target and no block on a path from that write to the target writes the register again. A loop counter set before a loop and bumped in the body is still dropped at the head (the body block is inafter(w) & into). This matches the PR description.last_writenow records every SGPR for ananydestination (s_movreld), so those writes also invalidate the proof. Good catch, consistent with the mask path.- Scope: small and additive, no CI/launcher/hook files. Because of the stack, the merge order must be #2459 first; after it merges the diff here shrinks to the second commit.
- Duplicates: no other open or merged PR covers this (searched the repo for the constant-at-join change).
Commands run (all passed)
python3 -B tests/startup_gfx2hlsl_loop_constants_check.py-> PASSpython3 -B tests/startup_gfx2hlsl_sop2_complement_check.py-> PASSpython3 tests/docs_links_check.py-> OK;python3 tests/bidi_controls_check.py-> OKgit diff --check origin/main...HEAD-> clean- Mutation 1: drop all
constsunconditionally (old behavior) -> loop_constants check FAILS ("constant set after an earlier arm ..."). - Mutation 2: never drop a constant at a join -> loop_constants check FAILS (loop counter case reads the stale constant
0x0 << 3). - Worktree restored with
git checkout -- .after each.
Notes
- Evidence is CPU translation/evaluation only; the PR posts no Windows results, so I make no claim about Windows.
- No P1/P2 correctness issue found.
AI-assisted; commands listed were executed locally.
Five mutations on the join proof: four caught, and the survivor is a branch that no check in the family reachesBaseline:
The first two are the ones worth having together: The survivor:
|
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Both callers pass the offset of an instruction in self.ins, and the flow index is built from the same list, so the target is always in index. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
Thanks for the mutation run. The Both callers (the join of Current main is merged into the branch (the base PR #2459 landed); head
No CMake build or ctest run; these are the pure-Python checks only. 🤖 Generated with Claude Code |
tedin7
left a comment
There was a problem hiding this comment.
Review of head dfff36e (Linux checks only; Windows run follows separately). #2459 is merged, so the dependency is satisfied. The change since the earlier review is main merges plus the removal of the unreachable target not in index guard in Stage.unjoined.
What I checked
Stage.unjoined(target, last_write, regs)now serves both the lane-mask marks and the constant drop (tools/startup/gfx2hlsl.py, join code near theconstsset). The dominance plus no-rewrite-on-path rule is the same one the mask marks already used.s_movreldnow counts as the last write of every SGPR, which is the safe direction.- Sibling callsites: the only callers are the mask join and the constant join, both inside the instruction loop and passing a real block offset, so the removed guard cannot be hit.
- No other open or merged PR covers this slice (searched gfx2hlsl / constant / branch target).
- Test guards the behavior:
tests/startup_gfx2hlsl_loop_constants_check.pyhas two folded cases and three negative controls.
Commands run (worktree on the exact head)
python3 -B tests/startup_gfx2hlsl_loop_constants_check.py-> PASSpython3 -B tests/startup_gfx2hlsl_sop2_complement_check.py-> PASSpython3 -B tests/startup_gfx2hlsl_join_symbols_check.py-> PASSpython3 -B tests/startup_translator_check.py-> 0 failedpython3 tests/docs_links_check.py,python3 tests/bidi_controls_check.py,git diff --check origin/main...HEAD-> clean- Mutation 1: never keep a constant at a join (
if False:in the drop condition) -> FAIL, 5 cases MISMATCH - Mutation 2: dominator check only, later-write check removed -> FAIL (the written-in-loop negative controls)
Findings: none. This is a translator-only change and makes no claim about running the game.
AI-assisted; commands listed were executed locally.
|
Win2025 WARP receipt for head dfff36e. Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit
AI-assisted; commands listed were executed locally. |
tedin7
left a comment
There was a problem hiding this comment.
Review of head 9038e45 (merge of main into the PR branch; the PR's own diff is unchanged: gfx2hlsl.py plus two check files; #2459 is merged so the dependency has landed).
Checked:
Stage.unjoined(target, last_write, regs)now serves both lane-mask marks and scalar constants. A constant is dropped at a join only if the block of its last write does not dominate the target, or a block on a path from that write to the target writes it again. Logic reads correct, including entry constants (no write counts as entry block) ands_movreld(anydestination now records every SGPR inlast_write).- Tests guard the behavior:
tests/startup_gfx2hlsl_loop_constants_check.pycovers constant set after an earlier arm (kept), the same constant written in a loop (dropped), loop counter, and loop offset from counter. The sop2 check was adapted to the renamed helper. - Mutation 1: drop every constant in
constsinstead of only the unjoined ones. The loop-constants check FAILS (constant set after an earlier arm). - Mutation 2: never drop. The check FAILS (loop counter, loop offset, constant written in the loop: MISMATCH).
- Both mutations restored afterwards.
Commands run:
python3 tests/startup_gfx2hlsl_loop_constants_check.pyPASSpython3 tests/startup_gfx2hlsl_sop2_complement_check.pyPASSpython3 tests/docs_links_check.pyOKpython3 tests/bidi_controls_check.pyOKgit diff --check origin/main...HEADclean
Not a lowering of a math op, so no new WARP readback is needed; Win2025 WARP dev+release receipt follows on this head.
AI-assisted; commands listed were executed locally.
|
Win2025 WARP receipt for head 9038e45 after updating with main. Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit
AI-assisted; commands listed were executed locally. |
Main now carries R0Y00#2459, R0Y00#2461, R0Y00#2466, R0Y00#2483, R0Y00#2428 and others in their final form; the conflicts take main's versions. Kept from this branch: R0Y00#2460's generalized unjoined(), R0Y00#2432's scalar(mods), the s_setreg MODE check, the cube image kind, the v_cvt_f64_f32 inline check. Upstream chose R0Y00#2428 for v_{add,sub,subrev}_co_ci_u32, so the local co_ci handlers are renamed unbound_* and their two checks unregistered. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Depends on #2459 (this branch is stacked on it and reuses its dominance helper; the diff here includes that commit until it merges).
What / Why
At a branch target
Stage.translate()drops every scalar constant whose register is in the target's joined set.That set is built to be safe for symbols: besides the writes on paths into the target, it holds the writes of every
earlier block that is off the dominator chain. So a register that an earlier conditional block used for anything,
and that every path then sets to a constant, loses the constant at each later join, although no path into that join
writes it.
Measured effect: a constant sampler built with
s_movk_i32,s_bfm_b32ands_mov_b64a few dozen instructionsbefore the image instruction reached it as four plain words. The image instruction was refused as a descriptor
operand without a symbol, or as
image_gather4_lz requires a constant sampler. In every measured case theconstant's write dominated the join where it was dropped and no block on a path from the write to the join wrote
the register.
Change
last write (program order) must dominate the target, and no block on a path from that write to the target may
write the register again. A register without a write in the program (entry constant) counts as written in the
entry block.
forward-join controls are unchanged and pass.
Stage.unjoined_masks(target, last_write)from the parent branch becomesStage.unjoined(target, last_write, regs)and serves both the mask marks and the constants.
s_movreld(any destination) now counts as the last write ofevery SGPR.
Tests
tests/startup_gfx2hlsl_loop_constants_check.py(extended, pure Python), 5 new programs:The one file that did not pass,
tests/startup_image_get_lod_view_check.py, needs a compiler, renderer and adapterargument and stops at its argument list. Not run:
binary/decodemodes (llvm-mc, libLLVM) and device checks.Measured
First refusal per request over the same 124 locally captured vertex/pixel requests, on top of the parent branch and
a local merge of open translator PRs:
image_gather4_lzrequires a constant samplerv_cndmask_b32(open PR #2391)s_loadthrough a pointer joined at a branch targets_getpc_b64v_readfirstlane_b32requires a proven uniform sourceLimits
set_semits theassignment), so the change affects folding and constant-descriptor recognition, not which values exist.
covered by the measurement only, not by a fixture.
docs/statement described the old drop rule, so no doc text changes. The test docstring's claim about aloop-body mutation was already not red on main and is corrected.
joined_registers(calculateds_setpc_b64targets included).🤖 Generated with Claude Code