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

Merged
stan495 merged 2 commits from upstream-pr/2309 into main 2026-11-25 12:08:58 +00:00
stan495 commented 2026-11-25 11:39:41 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by stan495 on 2026-11-25T11:39:41Z.


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).
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6603641395 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2309). Originally posted by [stan495](https://github.com/stan495) on 2026-11-25T11:39:41Z. --- 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) ```powershell .\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 commented 2026-11-25 12:01:48 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T11:42:58Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6056972790 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2309#pullrequestreview-6056972790). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T11:42:58Z. **Review:** COMMENTED --- 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 commented 2026-11-25 12:01:54 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by stan495 on 2026-11-25T11:46:19Z.


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.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7161478436 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2309#issuecomment-7161478436). Originally posted by [stan495](https://github.com/stan495) on 2026-11-25T11:46:19Z. --- 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 commented 2026-11-25 12:01:56 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by stan495 on 2026-11-25T11:58:17Z.


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.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7161747904 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2309#issuecomment-7161747904). Originally posted by [stan495](https://github.com/stan495) on 2026-11-25T11:58:17Z. --- 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.
upstream-sync closed this pull request 2026-11-25 13:00:41 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!2301
No description provided.