Skip to content

startup: keep src_scc proven at a branch target one uniform SCC write reaches - #2577

Merged
tedin7 merged 8 commits into
R0Y00:mainfrom
NiiRoZz:startup/gfx2hlsl-src-scc-proof
Nov 26, 2026
Merged

tedin7 merged 8 commits into
R0Y00:mainfrom
NiiRoZz:startup/gfx2hlsl-src-scc-proof

Conversation

@NiiRoZz

@NiiRoZz NiiRoZz commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Depends on #1821. This branch merges it; the review diff starts after that merge.

What changed

#1821 reads src_scc as (scc ? 1u : 0u) only while a modelled writer has set SCC in the current block. Every branch target drops that proof. In the local request cache, src_scc with unverified SCC is the largest single refusal. In the samples inspected, the first refused read is the compiler copying SCC into an SGPR on both arms of an if/else:

s_bitcmp1_b32 sN, imm        ; or s_cmp_*
s_cbranch_scc0 ELSE
  s_mov_b32 sX, src_scc      ; then arm: accepted
  ...                        ; may write SCC (often an elided s_bfe_u32 of a descriptor word)
  s_branch JOIN
ELSE:
  s_mov_b32 sX, src_scc      ; refused: branch target
JOIN:

Stage.translate() now tracks scc_def, the offset of the emitted SCC write that reaches each instruction, and only when that write was uniform (scc_uniform true at the write). Each forward branch records its scc_def for its target. A target keeps SCC proven when:

  • the fall-through, if any, and every edge into it carry the same write, and
  • no back edge enters it.

Every path into the target then ran that one write last, both for the lane being modelled and for the wave, whichever path each took. That includes a block that s_cbranch_execz skips for one lane while the wave runs it. So the model's SCC is the hardware value.

These stay refused:

  • lane-mask SCC (set_exec_scc, a mask op on vcc/exec, which is never uniform)
  • unmodelled writers (SCC_UNMODELLED) and descriptor-elided writers on any path
  • different SCC writes on different paths
  • loop heads

The existing whole-program descriptor_scc_reads check is unchanged.

Census (local cache, 239 requests whose recorded refusal is src_scc)

All counts are from the first refused src_scc read per request. The reaching writer comes from a backward walk over the control-flow graph, not from program order.

  • Reads: s_mov_b32 sN, src_scc 238, s_mul_i32 sN, sN, src_scc 1.
  • Reaching SCC writes, by mnemonic set. One write reaches 230 reads; two or three different writes reach 9.
    • s_bitcmp1_b32 sN, imm 169
    • s_bitcmp0_b32 sN, imm 17
    • s_cmp_eq_i32 sN, imm 17
    • s_cmp_lg_i32 sN, imm 16
    • s_cmp_lt_i32 7
    • s_cmp_lt_u32 sN, sN 6
    • s_cmp_eq_u64 sN, sN 3
    • s_and_b64 sN, vcc, exec 2 (lane mask: stays refused)
    • s_cmp_eq_i32 | s_cmp_lg_i32 2
  • Program-order tracking had blamed the other arm's s_branch (120) or an elided s_bfe_u32 of a descriptor word (103). Neither reaches the read.

Measured

The translator was origin/main plus #1821, 4 workers, fxc compile on. The cached requests were keyed under translation revision 14, so the measuring harness, not this branch, recomputed the compute and RTV keys with that revision.

  • Before: 85 requests refuse first on src_scc. The other 154 stop earlier on refusals in main: 137 on NEG on an integer or literal source, 7 on s_bfm_b64, 4 on s_load with a register offset through ('resheap',), 4 on v_readfirstlane_b32 requires a proven uniform source, and 2 on SDWA form of v_cndmask_b32.
  • After: 75 of the 85 change their first refusal; the other 154 are unchanged. 1 request (compute) translates fully and compiles. The 75 now stop at:
Requests Next refusal
46 formatted buffer load requires descriptor words proven in the entry block or a local table load
10 image_sample_lz modifier d16
4 export target null
2 operand '|v43|' in v_cndmask_b32_e64
2 operand '|v41|' in v_cndmask_b32_e64
2 buffer store through a modified descriptor
1 each s_bcnt1_i32_b64; 64-bit source 0x3f800000; and 6 different ... inside a block s_cbranch_execz skips per lane: definition of X, live where the block joins refusals (s_buffer_load_dwordx2, s_mov_b64, s_and_saveexec_b64, s_cselect_b64, s_load_dwordx4, v_cmp_lt_f32_e64)
1 translates fully
  • 10 still refuse on src_scc, all compute requests. Each has at least one reaching s_cmp_* whose SGPR source the model does not prove uniform. In 7, a different write reaches on another path; in 3, the path crosses a loop head.
  • An audit hook in the harness checked every executed instruction that writes hardware SCC, from a hand-written list of mnemonics. Each one either emitted SCC or cleared the proof: 0 violations over the 239 requests and the 304 secondary ones.

Requests that still have a src_scc refusal site somewhere, collecting every refusal (keep-going mode; these counts are noisier than first refusals):

  • Primary set: 239 before, 214 after. At the first remaining site:
    • 205 have one reaching s_cmp_* whose SGPR source the model does not prove uniform
    • 5 have that plus different writes on different paths
    • 4 have both and also cross a loop head
  • Secondary set (304 more requests): 304 before, 300 after. All 300 remaining have one reaching write that is not proven uniform. Before the change, the first-site writes were s_cmp_eq_i32 sN, imm (300), s_bitcmp1_b32 sN, imm (3) and s_and_b32 sN, sN, src_scc (1).
    • In the one traced request, the compared SGPR is uniform at its last write (a buffer load). It loses its uniformity at an intervening branch target, through the join's uniform-SGPR bookkeeping, not at a refused instruction.
    • So the likely next blocker for these reads is keeping SGPR uniformity across joins. It is not attempted here.
    • First refusals do not move on this set: 284 stop at SDWA form of v_cndmask_b32, both before and after, even with startup: support |x| abs modifier on v_cndmask_b32_e64 operands in gfx2hlsl #2449's head merged locally.

Hypothesis, not measured

  • The 10 remaining reads would need three things: a uniformity proof for those SGPR sources, an "every edge verified and every branch decision uniform" rule (divergence-aware), and loop-carried SCC. None is attempted here.
  • No GPU or title run; the result is translation and compilation only.

Tests

Run with out/pe-env/Scripts/python.exe -B. Each line gives the last line the check printed:

tests/startup_gfx2hlsl_src_scc_join_check.py         PASS (new)
tests/startup_gfx2hlsl_src_scc_check.py              PASS
tests/startup_translator_check.py                    startup translator checks: 0 failed
tests/startup_gfx2hlsl_calculated_target_check.py    Calculated-target CFG, selected arithmetic result and refusals pass
tests/startup_gfx2hlsl_compute_check.py              compute entry, ... refusals pass
tests/startup_gfx2hlsl_cselect_mask_check.py         cselect mask: ... null refused
tests/startup_gfx2hlsl_cvt_off_f32_i4_check.py       all controls passed
tests/startup_gfx2hlsl_handler_arity_check.py        33 accepted instructions, 99 scalar refusals and 16 load refusals
tests/startup_gfx2hlsl_image_get_lod_check.py        ... refused without the handler
tests/startup_gfx2hlsl_join_symbols_check.py         ... 17 refusal controls and the old behaviour hold
tests/startup_gfx2hlsl_loop_constants_check.py       PASS
tests/startup_gfx2hlsl_pack_absdiff_check.py         ok
tests/startup_gfx2hlsl_readfirstlane_recompute_check.py  0 failed
tests/startup_gfx2hlsl_readfirstlane_uniform_check.py    0 failed
tests/startup_gfx2hlsl_s_bitcmp_check.py             ok (216 scalar cases, 10 lane-mask refusals)
tests/startup_gfx2hlsl_s_lshl_add_check.py           all ok
tests/startup_gfx2hlsl_s_sext_cmov_check.py          all ok
tests/startup_gfx2hlsl_saveexec_same_reg_check.py    0 failed
tests/startup_gfx2hlsl_scalar_m0_check.py            controls passed
tests/startup_gfx2hlsl_scalar_shift64_bfe_check.py   controls passed (3498 helper cases)
tests/startup_gfx2hlsl_sgpr_spill_check.py           PASS
tests/startup_gfx2hlsl_sop2_complement_check.py      PASS
tests/startup_gfx2hlsl_sopk_accum_check.py           PASS
tests/startup_gfx2hlsl_sopk_signext_check.py         ok (1152 compare cases, ...)
tests/docs_links_check.py                            Checked 19 Markdown files and 488 links.

ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_src_scc(_join)?$' -V was not run: no CMake configure here. The new test is registered next to #1821's.

The new check builds its words from the decoder's field layout.

  • Exact HLSL for 3 positives:
    • the if/else shape, with an SCC write in the then arm after its read
    • a join after a skipped block that writes no SCC
    • the same over s_cbranch_execz
  • 5 negatives that keep src_scc with unverified SCC:
    • an SCC write in one arm
    • an SCC write in an execz-skipped block
    • a back-edge target
    • s_and_b32 with vcc_lo before the branch (the same program with s5 is accepted)
    • s_add_u32 on one path
  • On the translator without this change, all 3 positives refuse as src_scc with unverified SCC.

#1821's join case used a skipped s_nop, which is now accepted. It now skips an SCC write, so it still pins the join refusal. Its old input is positive case 2 in the new check.

🤖 Generated with Claude Code

triggered02 and others added 4 commits November 26, 2026 01:27
Fixes R0Y00#1820. Addresses review feedback on R0Y00#1821: Stage.u() now refuses src_scc unless a modeled SCC writer set scc_verified on the reaching path in the current block. scc_verified starts False at entry, clears at branch targets/joins, non-fallthroughs, SCC_UNMODELLED (such as s_add_u32, cleared after reading its sources), descriptor-elided SCC writes, calculated-PC elision, and s_wqm. Test runs full Stage.translate() over synthetic bytes covering entry refusal, modeled compare/bitwise writers, low16_only=True, the s_add_u32 carry case, input-before-write on s_add_u32, and CFG joins; all 62 pure-Python startup_gfx2hlsl checks and CTest src_scc/sopk_accum/startup_translator pass.
… reaches

A compiler copies SCC into an SGPR on both arms of an if/else (s_cmp_* or
s_bitcmp*, s_cbranch_scc0/1, then s_mov_b32 sN, src_scc in each arm). The
arm at the branch target was refused as unverified, because every target
dropped SCC. In 230 of the 239 locally refused requests, the first refused
read has a single reaching SCC write.

Each forward branch now carries the offset of the emitted SCC write that
reaches it, but only when that write was uniform. A target keeps SCC proven
when the fall-through and every edge into it carry the same write and no
back edge enters it. Lane-mask SCC, unmodelled and elided writers,
different writes on different paths, and loop heads stay refused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…scc-proof

# Conflicts:
#	tools/startup/gfx2hlsl.py

@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 5814ea5 (comment only, no merge from me).

Not merge-ready yet: the PR says it depends on #1821, and #1821 is still open. It should land first.

What I checked:

  • tools/startup/gfx2hlsl.py: SCC proof at a branch target is kept only when the fall-through and every forward edge carry the same uniform emitted write, and no back edge enters. That matches the PR text. Lane-mask, unmodelled and descriptor-elided writers still refuse.
  • Tests tests/startup_gfx2hlsl_src_scc_check.py and tests/startup_gfx2hlsl_src_scc_join_check.py pass and are wired in CMakeLists.txt.
  • Mutation 1: len(set(defs)) == 1 changed to len(defs) >= 1 makes both tests fail (join refusal, execz-skipped block).
  • Mutation 2: dropping the off not in back_targets guard makes the join test fail (back-edge target).
  • Docs and diff hygiene are clean.

Scope note: these are fixture and code checks only. The census and request counts in the body come from the author's local cache and I did not reproduce them. Any claim about startup progress has to be verified on the real title by whoever merges.

Commands run:

  • python3 tests/startup_gfx2hlsl_src_scc_check.py
  • python3 tests/startup_gfx2hlsl_src_scc_join_check.py
  • python3 tests/docs_links_check.py
  • python3 tests/bidi_controls_check.py
  • git diff --check origin/main...HEAD
  • the two mutations above, each reverted with git checkout -- .

AI-assisted; commands listed were executed locally.

…scc-proof

# Conflicts:
#	docs/experiments.md
@Leyukaka

Copy link
Copy Markdown
Contributor

Review of head 58ad96d2, by reading the diff against main fc97ade (not run).

A lane-mask SCC reaches src_scc in the same block (compute or pixel stage):

v_cmp_eq_u32_e32 vcc_lo, 1, v0      ; e.g. set in lane 1 only
s_and_b32 s6, vcc_lo, -1             ; guest SCC = (whole result != 0) = 1
s_mov_b32 s10, src_scc               ; guest s10 = 1 in every lane

s_and_b32 takes the is_mask path (main gfx2hlsl.py:3338 and :3375), so s6 is the lane's own bit and set_s(..., scc='$ != 0') calls set_scc('s6 != 0'). With this PR set_scc sets scc_verified = True unconditionally, while scc_uniform becomes False because of the vcc token. u('src_scc') checks only scc_verified, so the read is accepted and emits s6 = (vcc & ...) & 1; scc = s6 != 0; s10 = (scc ? 1u : 0u);. Lane 0 gets s10 = 0, while the guest writes 1.

The docs paragraph says lane-mask writers stay refused. The join test covers that across a branch, but nothing covers a lane-mask writer followed by src_scc in the same block.

Suggested fix: clear scc_verified only where a lane-mask writer sets SCC, not in set_scc as a whole. That means the is_mask path after set_s, the path with an exec/vcc destination, the m0 destination path when a source is a lane mask (s_and_b32 m0, vcc_lo, -1), and set_exec_scc. set_s discards the destination from masks before it calls set_scc, so the test cannot live inside set_scc. Making set_scc set scc_verified = scc_uniform instead would also refuse join_rewritten in startup_gfx2hlsl_src_scc_check.py: after the join since_join is empty, so s4 is not proven uniform there.

Plus a refusal control with the sequence above.

The same per-lane reading of a lane-mask SCC is already accepted on main through s_cselect_b32 s10, 1, 0 (scalar_select, gfx2hlsl.py:3557). That is outside this PR.

AI-assisted.

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

tedin7 commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

Win2025 WARP receipt for head d0682d1 (merge of main after #1821, plus one test).

What I added, and why:

  • Merged origin/main (startup: read src_scc as the SCC bit #1821 is in main now). Conflicts in CMakeLists.txt, tools/startup/gfx2hlsl.py and tests/startup_gfx2hlsl_src_scc_check.py resolved keeping both sides: the scc_def tracking on top of main's address-carry s_add_u32 branch, and the join case in src_scc_check that now skips an SCC write.
  • The new check compared emitted HLSL text only. I added a dispatch mode to tests/startup_gfx2hlsl_src_scc_join_check.py (the same pattern as startup_gfx2hlsl_pack_absdiff_dispatch_*): the translated if/else and skipped-block programs are compiled with DXC and run on D3D12 (WARP and hardware tests: startup_gfx2hlsl_src_scc_join_dispatch_warp / _hardware). The UAV readback is compared with a CPU reference (bit 3 of s4 on both arms; s4 == s5 at the join) over 6 value pairs.

Review of the remaining diff (translator change only):

  • Two mutations make the new checks fail: len(set(defs)) == 1 -> >= 1, and dropping the off not in back_targets guard.
  • Refusals kept: lane-mask SCC, unmodelled writers, differing writes on different paths, loop heads.
  • No sibling table is left out. git diff --check is clean.
  • The PR text measures refusals on a local cache; I did not check those numbers and they are not a result of this review.

Commands executed locally:

  • python3 -B tests/startup_gfx2hlsl_src_scc_check.py, ..._src_scc_join_check.py, tests/startup_translator_check.py: pass.
  • x2w_pipe.py win2025 2577 --patch <diff to d0682d17> --regex 'startup_gfx2hlsl_src_scc|startup_translator|startup_gfx2hlsl_compute|startup_gfx2hlsl_s_bitcmp|startup_gfx2hlsl_calculated|startup_gfx2hlsl_join' --keep-release:
    • ctest --preset dev: 43/43 passed.
    • ctest --preset release: 43/43 passed.
    • Both include startup_gfx2hlsl_src_scc_join_dispatch_warp and _hardware.

AI-assisted; commands listed were executed locally.

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

Reviewed head d0682d1 (translator diff after #1821, plus the WARP readback test). No blocking findings; details and commands are in the receipt comment above.

AI-assisted; commands listed were executed locally.

@tedin7
tedin7 enabled auto-merge (squash) November 26, 2026 03:50
@tedin7
tedin7 merged commit 0827f43 into R0Y00:main Nov 26, 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.

4 participants