Skip to content

shader: preserve one-path SGPRs and uniform loop counters for v_readfirstlane_b32 - #2309

Merged
R0Y00 merged 2 commits into
R0Y00:mainfrom
stan495:startup/gfx2hlsl-readfirstlane-loop-proof
Nov 25, 2026
Merged

R0Y00 merged 2 commits into
R0Y00:mainfrom
stan495:startup/gfx2hlsl-readfirstlane-loop-proof

Conversation

@stan495

@stan495 stan495 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Refs #2274 (Part A: v_readfirstlane proof across joins and loops).

Summary

Preserve proven uniform SGPRs across branch joins for v_readfirstlane_b32 in two CFG-verified cases:

  1. One-path registers: SGPRs proven uniform on the dominating path into a branch target t that are not written on any non-dominating path reaching t or any path reaching t at/after t.
  2. Uniform loop counters: SGPRs defined uniformly before a loop (or join) and updated only by uniform scalar operations under uniform SCC branch conditions (cond_uniform = cond in ('scc0', 'scc1') and self.scc_uniform), validated via a fixed-point pass in Stage.translate() against non-uniform scalar writes (bad_sgprs) and divergent s_cbranch_* regions (div_targets over dominators and post-dominators). Constants (const) are still cleared at clobbered joins so loop counters emit dynamic HLSL scalar reads.

What remains refused (v_readfirstlane_b32 requires a proven uniform source):

  • Non-uniform s_cbranch_* back-edges (e.g. s_cbranch_vccnz), divergent forward branch arms writing the candidate scalar before reconvergence (e.g. else-arm write under s_cbranch_vccz), lane-mask writes (s_mov_b32 sN, vcc_lo), unmodelled SGPR destinations (v_add_co_u32_e64, s_movreld, s_cselect on stale scc), and joins overwriting the scalar inside tentative_kept when later invalidated.

Also fixes Stage.one() / Stage.forget_writes() ordering so in-place uniform scalar updates (e.g. s_add_u32 s3, s3, 1) inspect s3's prior uniformity mark before unmodelled-write invalidation.

Sources and semantics

  • AMD RDNA 2 ISA (v_readfirstlane_b32, SOPP s_cbranch_scc0/s_cbranch_scc1/s_cbranch_vccz/s_cbranch_vccnz, SOPC, SOP1, SOP2, VOP1, VOP2) and LLVM 15 AMDGPU gfx1013 wave64 semantics (all lanes execute Uniform scalar/branch decisions identically, while divergent vcc/exec branches split active lane masks until immediate post-dominator reconvergence).

Verification

Branch commit: b0c83b06 (rebased onto d8b345cb).
Environment: Windows 11 25H2 (build 26200.9974), MSVC 19.44.35228.0, D3D12 debug layer enabled (debug_errors=0 debug_warnings=0).

CTest (dev preset)

.\tools\dev.ps1
cmake --preset dev -DINTAKE_PYTHON=E:\02_Workspace\Xbox2Windows\out\pe-env\Scripts\python.exe
cmake --build --preset dev --target offscreen_check
ctest --preset dev --no-tests=error -R '^(startup_gfx2hlsl_(readfirstlane_(uniform|recompute)|loop_constants)|startup_compute_readfirstlane(_(warp|hardware))?)$' -V

Result: 6 / 6 passed (100% tests passed, 0 tests failed out of 6):

  • startup_compute_readfirstlane (controls, machine-code refusals, lowering check): passed
  • startup_gfx2hlsl_loop_constants: passed (PASS)
  • startup_gfx2hlsl_readfirstlane_uniform: passed (0 failed)
  • startup_gfx2hlsl_readfirstlane_recompute: passed (0 failed; covers one-path scalars, uniform loop counters inside/after loops, and divergent/mask refusals)
  • startup_compute_readfirstlane_hardware (adapter=AMD Radeon RX 7900 XTX, vendor=1002 device=744c, driver=32.0.32029.2010, debug_layer=enabled): passed (compared_words=320 mismatches=0 debug_errors=0 debug_warnings=0)
    • Pinned negative controls rejected as expected:
      • dropped-one-path: word=0 expected=0000008b actual=00000019
      • dropped-loop-read: word=0 expected=0000008b actual=0000007f
      • dropped-post-loop: word=0 expected=0000008b actual=00000087
  • startup_compute_readfirstlane_warp (adapter=Microsoft Basic Render Driver, vendor=1414 device=008c, driver=10.0.26100.9818, debug_layer=enabled): passed (compared_words=320 mismatches=0 debug_errors=0 debug_warnings=0, same 3 pinned negative controls rejected)

Pure-Python & repo checks

  • tests/startup_gfx2hlsl_compute_check.py, tests/startup_translator_check.py, tests/docs_links_check.py (18 files, 448 links), tests/bidi_controls_check.py (1600 files), tests/form_coverage_check.py, git diff --check: all passed.
  • Ubuntu CI will verify python3 -B tests/startup_compute_readfirstlane_check.py --llvm-mc llvm-mc-15 (and all other gfx1013 checks).

@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 b0c83b064dcf0c401abc065e1155e108388d98b6 (Linux-only; no Windows run, CI for Windows was still pending).

Commands run locally in a worktree of this head:

  • git diff --stat origin/main...HEAD: 6 files, +589/-69
  • python3 tests/startup_compute_readfirstlane_check.py: passed ("one-path, loop-counter and refusal checks passed")
  • python3 tests/startup_gfx2hlsl_readfirstlane_recompute_check.py: "0 failed"
  • python3 tests/docs_links_check.py: passed (448 links)
  • python3 tests/bidi_controls_check.py: passed
  • git diff --check origin/main...HEAD: clean
  • Mutation: in gfx2hlsl.py replaced force_unproven.update(new_bad) with pass (the feedback of loop-written SGPRs into the next pass). The new check did not report a failure: the fixpoint loop never converged and the run hung until I stopped it. So the mutation is detected, but only as a hang, not as an assertion. Reverted afterwards.

Findings

  • P1: none.
  • P2: none.
  • Nit 1 (tools/startup/gfx2hlsl.py:1120-1121): join_writes = self.join_writes(branches) is assigned twice in a row. Drop one.
  • Nit 2 (tools/startup/gfx2hlsl.py, the while True pass loop that ends near line 1257): it ends because force_unproven and force_at only grow, and the mutation above shows a hang if that growth is lost. A small pass cap that raises Unsupported would turn a future regression into a clear refusal rather than a stuck translation.
  • Nit 3: the new hasattr(self, 'defined_uniform_s') / '_uniform_written' / 'bad_sgprs' guards in set_s and the write path suggest these fields are only sometimes present. Initializing them in Stage.__init__ would remove the guards.

Clean-room, neutral title, MIT-only content, mergeable: all fine. The new test is wired into CMake and the Windows workflow. Windows WARP/D3D12 coverage comes from the added --dispatch CTest entries; I did not run those here.

AI-assisted; commands listed were executed locally.

@stan495

stan495 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Addressed all three nits in 0395dd92:

  1. Removed the duplicate join_writes = self.join_writes(branches) assignment in Stage.translate().
  2. Replaced while True: with a bounded for _ in range(max(4, len(self.ins) + 2)): loop that refuses via Unsupported('v_readfirstlane_b32 uniformity proof exceeded pass limit') (or records in refusals) on exhaustion instead of hanging. Re-tested the force_unproven.update(new_bad) -> pass mutation in a temporary copy; it now terminates immediately with Unsupported: v_readfirstlane_b32 uniformity proof exceeded pass limit.
  3. Added immutable None class defaults on Stage (defined_uniform_s = None, bad_sgprs = None, _uniform_written = None) and initialized fresh set()s in Stage.__init__, replacing hasattr(...) checks in set_s and one with is not None (so Stage.__new__(Stage) unit probes continue working cleanly without sharing mutable defaults).

Re-ran locally after 0395dd92:

  • ctest --preset dev --no-tests=error -R '^(startup_gfx2hlsl_(readfirstlane_(uniform|recompute)|loop_constants)|startup_compute_readfirstlane(_(warp|hardware))?)$' -V: 6 / 6 passed (startup_compute_readfirstlane_hardware on AMD Radeon RX 7900 XTX driver 32.0.32029.2010 and startup_compute_readfirstlane_warp on Microsoft Basic Render Driver 10.0.26100.9818, debug_layer=enabled, 0 mismatches/errors/warnings, all 3 GPU negative controls rejected at expected word 0).
  • tests/startup_translator_check.py, tests/docs_links_check.py (448 links), tests/bidi_controls_check.py (1600 files), git diff --check: all passed.

@stan495
stan495 force-pushed the startup/gfx2hlsl-readfirstlane-loop-proof branch from 0395dd9 to beba849 Compare November 25, 2026 11:57
@stan495

stan495 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto current main (2f5c54c0, including #2292 and #2310) -> head beba8498 (mergeable: MERGEABLE):

  • Consolidated CFG dominator/join computation into Stage.joined_registers(with_cfg=True) (dropping the duplicate cfg_join_writes() helper) and separated forward split clobbers (forward_splits) from loop back-edges so JOIN_RFL in tests/startup_gfx2hlsl_join_symbols_check.py stays refused while uniform loop counters (LOOP_RFL) lower dynamically (s23 = s23 + 0x00000001u; -> s24 = s23;) with no constant folding.
  • Kept Stage.translate()'s bounded loop strictly on (bad_sgprs, bad_at_target) with Stage.__init__'s force_joined handling remaining outside so descriptor_scc_reads runs before back_force overrides.

Re-verified locally at beba8498:

  • ctest --preset dev --no-tests=error -R '^(startup_gfx2hlsl_(readfirstlane_(uniform|recompute)|loop_constants|join_symbols(_(warp|hardware))?|compare_dst)|startup_compute_readfirstlane(_(warp|hardware))?)$' -V: 8 / 8 passed (including startup_gfx2hlsl_join_symbols_{warp,hardware} and startup_compute_readfirstlane_{warp,hardware} on AMD Radeon RX 7900 XTX 32.0.32029.2010 and WARP 10.0.26100.9818, debug_layer=enabled, 0 errors/warnings).
  • Pure-Python & repo checks (readfirstlane_uniform, readfirstlane_recompute, loop_constants, join_symbols, compare_dst, compute_readfirstlane, compute, translator, docs_links 448 links, bidi_controls 1607 files, form_coverage, git diff --check): all passed.

@stan495
stan495 force-pushed the startup/gfx2hlsl-readfirstlane-loop-proof branch from beba849 to e319842 Compare November 25, 2026 12:03
@R0Y00
R0Y00 merged commit 31f4685 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