startup: lower 32-bit scalar writes to m0 and track symbolic m0 state #1888

Merged
consolelogkyo merged 3 commits from upstream-pr/1895 into main 2026-11-24 21:12:16 +00:00
consolelogkyo commented 2026-11-24 20:27:44 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by consolelogkyo on 2026-11-24T20:27:44Z.


Problem

In tools/startup/gfx2hlsl.py, Stage.declarations() declares uint m0 = 0;, and both Stage.u('m0') and Stage.op_s_buffer_load_dword() read m0 as a runtime uint register (for example s_buffer_load_dword s0, s[4:7], m0). However, scalar operations writing m0 (sdst == 124) were either silently dropped or crashed:

  1. Silent drop of all Stage.scalar() writes to m0 (and their SCC updates). Stage.scalar() returned None whenever dname == 'm0', discarding s_mov_b32 m0, ..., s_movk_i32 m0, ..., s_lshl_b32 m0, ..., s_add_u32 m0, ..., etc., so m0 stayed 0 at run time and any SCC-producing write to m0 left scc stale.
  2. TypeError on s_cselect_b32, s_bfm_b32, saveexec and wrexec targeting m0. Those handlers subscripted reg_range(ops[0])[1] directly, which is None for 'm0'.
  3. Unchecked 64-bit scalar operations on m0. For 64-bit scalar opcodes (*_b64), decode_s_reg64(124) formats operand 124 as 'm0'. Because m0 is a 32-bit register on RDNA 2, 64-bit scalar instructions naming m0 as destination or source either vanished (dname == 'm0') or raised TypeError.

What Changed

  • tools/startup/gfx2hlsl.py:
    • Updated Stage.scalar() to emit m0 = <expr>; (and scc = ...; when defined by SCA) for 32-bit scalar ALU writes to m0.
    • Preserved pixel interpolation prologues (s_mov_b32 m0, sN before v_interp_*) when sN happens to hold a symbolic pointer/descriptor from setup_entry(): non-SCC pointer/descriptor moves into m0 record the symbolic tag in self.sym['m0'] without failing unless m0 is later read numerically in Stage.u('m0') (or used in an SCC-producing operation on descriptor data), and numeric writes to m0 clear self.sym['m0'].
    • Updated Stage.scalar_select() and Stage.op_s_bfm_b32() to emit 32-bit m0 writes and clear self.sym['m0'], allowed m0 as a 32-bit source in op_s_andn2_wrexec_b32, and refused m0 with Unsupported on 64-bit scalar operations (*_b64) and saveexec/wrexec destinations.
  • tests/startup_gfx2hlsl_scalar_m0_check.py, CMakeLists.txt, .github/workflows/windows.yml, AGENTS.md, docs/experiments.md:
    • Added synthetic RDNA 2 SOP1/SOP2/SOPK/SMEM encodings with binary llvm-mc-15 parity verification, pure-Python controls (including symbolic pointer propagation/clearing, 64-bit and saveexec/wrexec refusals, and an in-process monkeypatch control), and two 16-vertex D3D12 buffer-load readback fixtures:
      • s_lshl_b32 m0, s8, 4 (writing both m0 and scc) -> s_cselect_b32 m0, m0, 48 -> s_add_u32 m0, m0, 8 -> s_buffer_load_dwordx4 at s8 = 0 and s8 = 2;
      • s_movk_i32 m0, 0x20 -> s_buffer_load_dwordx2 and runtime s_bfm_b32 m0, s8, 2 -> s_buffer_load_dwordx2 at s8 = 3 (plus FXC vs_5_1/ps_5_1 compilation).

Verification

Tested on Windows 11 26H2 (build 26300.9974), MSVC 19.44.35230.0, Windows SDK 10.0.26100.0 (dxc.exe + fxc.exe), D3D12 debug layer enabled (debug_errors=0 debug_warnings=0):

  • WARP: Microsoft Basic Render Driver 10.0.26100.9818 — 0 mismatches across all 320-dword buffer readbacks; all wrong-reference controls and translation mutations (m0 writes dropped, scc update dropped) failed as expected.
  • Physical GPU: AMD Radeon RX 9070 XT (32.0.32029.2010) — 0 mismatches across all 320-dword buffer readbacks; all wrong-reference controls and translation mutations failed as expected.
cmake --preset dev
ctest --preset dev --no-tests=error -R '^(startup_gfx2hlsl_scalar_m0_(controls|warp|hardware)|startup_flat_param_(controls|warp|hardware)|startup_gfx2hlsl_smem_offset|startup_tools_lint)$' -V
python -B tests/docs_links_check.py
python -B tests/bidi_controls_check.py
git diff --check

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6590763356 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1895). Originally posted by [consolelogkyo](https://github.com/consolelogkyo) on 2026-11-24T20:27:44Z. --- ## Problem In `tools/startup/gfx2hlsl.py`, `Stage.declarations()` declares `uint m0 = 0;`, and both `Stage.u('m0')` and `Stage.op_s_buffer_load_dword()` read `m0` as a runtime `uint` register (for example `s_buffer_load_dword s0, s[4:7], m0`). However, scalar operations writing `m0` (`sdst == 124`) were either silently dropped or crashed: 1. **Silent drop of all `Stage.scalar()` writes to `m0` (and their `SCC` updates).** `Stage.scalar()` returned `None` whenever `dname == 'm0'`, discarding `s_mov_b32 m0, ...`, `s_movk_i32 m0, ...`, `s_lshl_b32 m0, ...`, `s_add_u32 m0, ...`, etc., so `m0` stayed `0` at run time and any `SCC`-producing write to `m0` left `scc` stale. 2. **`TypeError` on `s_cselect_b32`, `s_bfm_b32`, `saveexec` and `wrexec` targeting `m0`.** Those handlers subscripted `reg_range(ops[0])[1]` directly, which is `None` for `'m0'`. 3. **Unchecked 64-bit scalar operations on `m0`.** For 64-bit scalar opcodes (`*_b64`), `decode_s_reg64(124)` formats operand `124` as `'m0'`. Because `m0` is a 32-bit register on RDNA 2, 64-bit scalar instructions naming `m0` as destination or source either vanished (`dname == 'm0'`) or raised `TypeError`. ## What Changed - **`tools/startup/gfx2hlsl.py`**: - Updated `Stage.scalar()` to emit `m0 = <expr>;` (and `scc = ...;` when defined by `SCA`) for 32-bit scalar ALU writes to `m0`. - Preserved pixel interpolation prologues (`s_mov_b32 m0, sN` before `v_interp_*`) when `sN` happens to hold a symbolic pointer/descriptor from `setup_entry()`: non-`SCC` pointer/descriptor moves into `m0` record the symbolic tag in `self.sym['m0']` without failing unless `m0` is later read numerically in `Stage.u('m0')` (or used in an `SCC`-producing operation on descriptor data), and numeric writes to `m0` clear `self.sym['m0']`. - Updated `Stage.scalar_select()` and `Stage.op_s_bfm_b32()` to emit 32-bit `m0` writes and clear `self.sym['m0']`, allowed `m0` as a 32-bit source in `op_s_andn2_wrexec_b32`, and refused `m0` with `Unsupported` on 64-bit scalar operations (`*_b64`) and `saveexec`/`wrexec` destinations. - **`tests/startup_gfx2hlsl_scalar_m0_check.py`**, **`CMakeLists.txt`**, **`.github/workflows/windows.yml`**, **`AGENTS.md`**, **`docs/experiments.md`**: - Added synthetic RDNA 2 SOP1/SOP2/SOPK/SMEM encodings with `binary llvm-mc-15` parity verification, pure-Python controls (including symbolic pointer propagation/clearing, 64-bit and `saveexec`/`wrexec` refusals, and an in-process monkeypatch control), and two 16-vertex D3D12 buffer-load readback fixtures: - `s_lshl_b32 m0, s8, 4` (writing both `m0` and `scc`) -> `s_cselect_b32 m0, m0, 48` -> `s_add_u32 m0, m0, 8` -> `s_buffer_load_dwordx4` at `s8 = 0` and `s8 = 2`; - `s_movk_i32 m0, 0x20` -> `s_buffer_load_dwordx2` and runtime `s_bfm_b32 m0, s8, 2` -> `s_buffer_load_dwordx2` at `s8 = 3` (plus FXC `vs_5_1`/`ps_5_1` compilation). ## Verification Tested on Windows 11 26H2 (build `26300.9974`), MSVC `19.44.35230.0`, Windows SDK `10.0.26100.0` (`dxc.exe` + `fxc.exe`), D3D12 debug layer **enabled** (`debug_errors=0 debug_warnings=0`): - **WARP**: Microsoft Basic Render Driver `10.0.26100.9818` — 0 mismatches across all 320-dword buffer readbacks; all wrong-reference controls and translation mutations (`m0` writes dropped, `scc` update dropped) failed as expected. - **Physical GPU**: AMD Radeon RX 9070 XT (`32.0.32029.2010`) — 0 mismatches across all 320-dword buffer readbacks; all wrong-reference controls and translation mutations failed as expected. ```powershell cmake --preset dev ctest --preset dev --no-tests=error -R '^(startup_gfx2hlsl_scalar_m0_(controls|warp|hardware)|startup_flat_param_(controls|warp|hardware)|startup_gfx2hlsl_smem_offset|startup_tools_lint)$' -V python -B tests/docs_links_check.py python -B tests/bidi_controls_check.py git diff --check ``` AI-assisted.
R0Y00 commented 2026-11-24 21:01:25 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T20:55:24Z.


Local review found that s_bfm_b32 targeting m0 cleared the symbolic tag before reading either source. A self-read could therefore use an old numeric value even when m0 held a descriptor pointer. The fast-forward checks both operands while the tag is still live and adds a source-built refusal control for either position. The updated scalar controls passed. Please keep the shared SMEM m0 read through Stage.u once #1881 lands; that ensures it observes this same symbolic guard.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7144236247 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1895#issuecomment-7144236247). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T20:55:24Z. --- Local review found that s_bfm_b32 targeting m0 cleared the symbolic tag before reading either source. A self-read could therefore use an old numeric value even when m0 held a descriptor pointer. The fast-forward checks both operands while the tag is still live and adds a source-built refusal control for either position. The updated scalar controls passed. Please keep the shared SMEM m0 read through Stage.u once #1881 lands; that ensures it observes this same symbolic guard.
upstream-sync closed this pull request 2026-11-24 21:27:27 +00:00
tedin7 commented 2026-11-24 21:27:27 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T21:13:01Z.

Review: COMMENTED


Review of #1895 at exact head 498dcf585285d0a8f689a3d895fb5207ffa6bbbe (fetched from pull/1895/head, HEAD equals current headRefOid; git merge-tree against current main shows no conflicts).

Verdict: no P1/P2 found from what is runnable on Linux. Comment only, not an approval. The GPU readback part is not verified by me (see limits).

What I checked

  • Scope: tools/startup/gfx2hlsl.py (+48/-8), new tests/startup_gfx2hlsl_scalar_m0_check.py (+467), ctest entries (controls, warp, hardware, SKIP_RETURN_CODE 77), windows.yml (+2), AGENTS.md (+1), docs/experiments.md section.
  • Clean-room: instruction words are built in the test from the public RDNA 2 bit layouts and the repo's own tools/disasm_rdna2.py; data is synthetic. Title is neutral.
  • Logic, read line by line: Stage.scalar() m0 destination now emits m0 = expr; (+ scc with $ -> m0, same template as set_s), pointer/descriptor sources are recorded in self.sym['m0'] and any later numeric read refuses (u('m0'), smem_soffset). A numeric write pops the stale symbol. 64-bit ops naming m0 and saveexec/wrexec with a non-s destination now raise Unsupported instead of TypeError. The pixel-shader entry register (('const', 0), setup_entry) still moves into m0 without failing, so the usual s_mov_b32 m0, sN interpolation prologue is not turned into a refusal.

Commands run locally

  • git diff --check origin/main...HEAD -> exit 0
  • python -B tests/startup_gfx2hlsl_scalar_m0_check.py controls -> startup scalar m0 controls passed
  • python -B tests/startup_gfx2hlsl_scalar_m0_check.py binary llvm-mc-19 -> scalar m0 encodings match llvm-mc (llvm-mc-19, the only one on this host; CI uses llvm-mc-15)
  • Regression, same venv, all exit 0: startup_gfx2hlsl_{b64_shift,bit_alu,capture,cndmask_neg,cvt_alu,mad,op_refusals,operand_count,sdwa,sdwa_cvt,shift64,smem_offset,tbuffer_16f,vcvt_omod}_check.py, startup_translator_check.py, startup_flat_param_check.py, startup_buffer_address_check.py
  • bidi_controls_check.py -> no bidirectional controls; docs_links_check.py -> Checked 18 Markdown files and 384 links

Mutations (applied to tools/startup/gfx2hlsl.py, controls re-run, then git checkout -- .)

Mutation Result
u('m0') stops refusing a symbolic m0 killed
scalar() m0 destination branch disabled (writes dropped again) killed (crash in fall-through)
scc update after an m0 write removed killed (scc = m0 != 0; missing)
m0 symbol always copied from syms[0] killed (reason text differs)
64-bit m0 refusal in scalar() removed killed
smem_soffset back to the literal 'm0' killed
scalar_select keeps a stale m0 symbol killed (crash on later use)
s_bfm_b32 constant mask shift off by one killed (0x3c vs 0x78)
saveexec destination check removed killed (TypeError)
wrexec source fallback reverted killed (TypeError)

10/10 killed. A few kills are crashes rather than precise assertion failures (the two noted above); they still fail the suite.

Not run (honest limits)

  • The DXC/FXC compile and the D3D12 draw/readback cases (startup_gfx2hlsl_scalar_m0_warp|hardware) need DXC, offscreen_check and a D3D12 adapter, none on this Linux host. The "zero mismatches on WARP and RX 9070 XT" claim in the docs is therefore not independently reproduced by me.
  • binary mode was run with llvm-mc-19, not the pinned llvm-mc-15.
  • I have no real titles' shaders here, so I cannot say whether previously translating shaders now refuse because of the new Unsupported paths (e.g. a numeric read of an m0 that holds a pointer). The tests argue the intent is right; a Windows run on the local corpus would confirm.

Non-blocking notes (P3)

  • docs/experiments.md (new section): the paragraph is long and mixes the old defect description, the new behavior and the measured results in four dense sentences; splitting "what changed" from "what was measured" would match the neighbouring sections.
  • Two mutation kills surface as TypeError/unhandled Unsupported instead of an assertion, so a future regression there would fail the suite with a less readable message.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6047499217 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1895#pullrequestreview-6047499217). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T21:13:01Z. **Review:** COMMENTED --- Review of #1895 at exact head `498dcf585285d0a8f689a3d895fb5207ffa6bbbe` (fetched from `pull/1895/head`, HEAD equals current `headRefOid`; `git merge-tree` against current `main` shows no conflicts). **Verdict: no P1/P2 found from what is runnable on Linux. Comment only, not an approval.** The GPU readback part is not verified by me (see limits). ### What I checked - Scope: `tools/startup/gfx2hlsl.py` (+48/-8), new `tests/startup_gfx2hlsl_scalar_m0_check.py` (+467), ctest entries (`controls`, `warp`, `hardware`, `SKIP_RETURN_CODE 77`), `windows.yml` (+2), `AGENTS.md` (+1), `docs/experiments.md` section. - Clean-room: instruction words are built in the test from the public RDNA 2 bit layouts and the repo's own `tools/disasm_rdna2.py`; data is synthetic. Title is neutral. - Logic, read line by line: `Stage.scalar()` m0 destination now emits `m0 = expr;` (+ `scc` with `$` -> `m0`, same template as `set_s`), pointer/descriptor sources are recorded in `self.sym['m0']` and any later numeric read refuses (`u('m0')`, `smem_soffset`). A numeric write pops the stale symbol. 64-bit ops naming `m0` and `saveexec`/`wrexec` with a non-`s` destination now raise `Unsupported` instead of `TypeError`. The pixel-shader entry register (`('const', 0)`, `setup_entry`) still moves into `m0` without failing, so the usual `s_mov_b32 m0, sN` interpolation prologue is not turned into a refusal. ### Commands run locally - `git diff --check origin/main...HEAD` -> exit 0 - `python -B tests/startup_gfx2hlsl_scalar_m0_check.py controls` -> `startup scalar m0 controls passed` - `python -B tests/startup_gfx2hlsl_scalar_m0_check.py binary llvm-mc-19` -> `scalar m0 encodings match llvm-mc` (**llvm-mc-19**, the only one on this host; CI uses `llvm-mc-15`) - Regression, same venv, all exit 0: `startup_gfx2hlsl_{b64_shift,bit_alu,capture,cndmask_neg,cvt_alu,mad,op_refusals,operand_count,sdwa,sdwa_cvt,shift64,smem_offset,tbuffer_16f,vcvt_omod}_check.py`, `startup_translator_check.py`, `startup_flat_param_check.py`, `startup_buffer_address_check.py` - `bidi_controls_check.py` -> `no bidirectional controls`; `docs_links_check.py` -> `Checked 18 Markdown files and 384 links` ### Mutations (applied to `tools/startup/gfx2hlsl.py`, controls re-run, then `git checkout -- .`) | Mutation | Result | |---|---| | `u('m0')` stops refusing a symbolic m0 | killed | | `scalar()` m0 destination branch disabled (writes dropped again) | killed (crash in fall-through) | | `scc` update after an m0 write removed | killed (`scc = m0 != 0;` missing) | | m0 symbol always copied from `syms[0]` | killed (reason text differs) | | 64-bit m0 refusal in `scalar()` removed | killed | | `smem_soffset` back to the literal `'m0'` | killed | | `scalar_select` keeps a stale m0 symbol | killed (crash on later use) | | `s_bfm_b32` constant mask shift off by one | killed (`0x3c` vs `0x78`) | | `saveexec` destination check removed | killed (`TypeError`) | | `wrexec` source fallback reverted | killed (`TypeError`) | 10/10 killed. A few kills are crashes rather than precise assertion failures (the two noted above); they still fail the suite. ### Not run (honest limits) - The DXC/FXC compile and the D3D12 draw/readback cases (`startup_gfx2hlsl_scalar_m0_warp|hardware`) need DXC, `offscreen_check` and a D3D12 adapter, none on this Linux host. The "zero mismatches on WARP and RX 9070 XT" claim in the docs is therefore **not independently reproduced** by me. - `binary` mode was run with `llvm-mc-19`, not the pinned `llvm-mc-15`. - I have no real titles' shaders here, so I cannot say whether previously translating shaders now refuse because of the new `Unsupported` paths (e.g. a numeric read of an m0 that holds a pointer). The tests argue the intent is right; a Windows run on the local corpus would confirm. ### Non-blocking notes (P3) - `docs/experiments.md` (new section): the paragraph is long and mixes the old defect description, the new behavior and the measured results in four dense sentences; splitting "what changed" from "what was measured" would match the neighbouring sections. - Two mutation kills surface as `TypeError`/unhandled `Unsupported` instead of an assertion, so a future regression there would fail the suite with a less readable message. AI-assisted; commands listed were executed locally.
R0Y00 commented 2026-11-24 21:27:39 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T21:08:08Z.


Integrated current main and routed the new SMEM m0 offset path through the same guarded read as other scalar uses. The scalar m0 fixture now passes the SMEM pointer rejection and both BFM operand-position controls together, and the startup translator suite reports zero failures.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7144522617 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1895#issuecomment-7144522617). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T21:08:08Z. --- Integrated current `main` and routed the new SMEM `m0` offset path through the same guarded read as other scalar uses. The scalar `m0` fixture now passes the SMEM pointer rejection and both BFM operand-position controls together, and the startup translator suite reports zero failures.
consolelogkyo commented 2026-11-24 21:27:40 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by consolelogkyo on 2026-11-24T21:10:20Z.


Thank you for catching the s_bfm_b32 self-read ordering (6a41a3e9) and merging smem_soffset through self.u(tok) (498dcf58)!

Pulled 498dcf58 locally and verified startup_gfx2hlsl_scalar_m0_(controls|warp|hardware) and startup_smem_address_(controls|warp|hardware) — 6/6 passed with 0 mismatches on WARP 10.0.26100.9818 and AMD Radeon RX 9070 XT 32.0.32029.2010 (debug_errors=0 debug_warnings=0).

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7144572525 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1895#issuecomment-7144572525). Originally posted by [consolelogkyo](https://github.com/consolelogkyo) on 2026-11-24T21:10:20Z. --- Thank you for catching the `s_bfm_b32` self-read ordering (`6a41a3e9`) and merging `smem_soffset` through `self.u(tok)` (`498dcf58`)! Pulled `498dcf58` locally and verified `startup_gfx2hlsl_scalar_m0_(controls|warp|hardware)` and `startup_smem_address_(controls|warp|hardware)` — 6/6 passed with 0 mismatches on WARP `10.0.26100.9818` and AMD Radeon RX 9070 XT `32.0.32029.2010` (`debug_errors=0 debug_warnings=0`). AI-assisted.
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!1888
No description provided.