startup: clear the lane-mask mark when gfx2hlsl overwrites an SGPR #2122

Manually merged
github merged 1 commit from gh-pr/2122/head into gh-pr/2122/base 2026-11-26 23:58:16 +00:00
Owner

@⁠denn0x on GitHub · 2026-11-25 00:53 UTC · original
denn0x:startup/gfx2hlsl-mask-overwrite → main · +158 −4 · 6 files · 1 commits · merged 2026-11-25 01:13 UTC

Part of #1723. Related: #2023 (item L, a lane mask derived from descriptor data, relies on the same mask tracking; this does not lower it)

Stage.masks in tools/startup/gfx2hlsl.py records the SGPRs that hold a lane mask, and a scalar logic op with a mask source is lowered as a mask op, so its result is reduced to the lane's bit. Only the generic scalar path cleared the mark on an ordinary write. After v_cmp_eq_u32_e64 s6, v0, v1 and s_buffer_load_dword s6, s[16:19], 0x0, the instruction s_or_b32 s8, s6, s7 was emitted as s8 = (s6 | s7) & 1;, which cuts the loaded word to bit 0. The stale mark would also make the lane-mask refusals, such as the one for scalar bit tests, refuse a register that no longer holds a mask (not tested here).

Now:

  • set_s clears the mark on every write, after the uniformity of the new value is judged (#1931), so the readfirstlane proof still sees a source register as it was before the write.
  • The writers of a mask (v_cmp*_e64, s_and_saveexec, s_*_wrexec, a copy of exec_lo, s_cselect_b64, the mask logic ops) mark the register again after the write.
  • s_load clears the mark on its destination registers, including the forms that only record a descriptor symbol.

New test tests/startup_gfx2hlsl_mask_overwrite_check.py, registered as startup_gfx2hlsl_mask_overwrite_controls in CMakeLists.txt, AGENTS.md and the Python job, and described in docs/experiments.md. It overwrites a compare result with s_buffer_load, s_load (through a root pointer, and of a descriptor) or s_cselect_b32, then evaluates the following OR on a buffer word. Controls: without the overwrite the OR stays a mask OR, and a compare, saveexec, wrexec, exec copy, s_cselect_b64 or class compare after a load marks the register again.

Verification (branch head 7ebe57e7 on main 246a875d, Windows 11 Pro 10.0.26300, Python 3.13):

  • python -B tests/startup_gfx2hlsl_mask_overwrite_check.py -> exit 0 (mask overwrite: loads and selects clear the mask mark; mask writers mark again). On main the same test reports FAILED: 7 check(s).
  • every tests/startup_gfx2hlsl_*_check.py (including startup_gfx2hlsl_readfirstlane_uniform_check.py), tests/startup_translator_check.py, tests/docs_links_check.py and tests/startup_tools_lint_check.py tools/startup -> 33 scripts, all exit 0.
  • git diff --check main..HEAD -> clean.
  • Not run: a DXC compile or a draw; the check evaluates the emitted HLSL text only.

AI-assisted.

**@⁠denn0x** on GitHub · 2026-11-25 00:53 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2122) `denn0x:startup/gfx2hlsl-mask-overwrite` → `main` · +158 −4 · 6 files · 1 commits · merged 2026-11-25 01:13 UTC Part of #1723. Related: #2023 (item L, a lane mask derived from descriptor data, relies on the same mask tracking; this does not lower it) `Stage.masks` in `tools/startup/gfx2hlsl.py` records the SGPRs that hold a lane mask, and a scalar logic op with a mask source is lowered as a mask op, so its result is reduced to the lane's bit. Only the generic scalar path cleared the mark on an ordinary write. After `v_cmp_eq_u32_e64 s6, v0, v1` and `s_buffer_load_dword s6, s[16:19], 0x0`, the instruction `s_or_b32 s8, s6, s7` was emitted as `s8 = (s6 | s7) & 1;`, which cuts the loaded word to bit 0. The stale mark would also make the lane-mask refusals, such as the one for scalar bit tests, refuse a register that no longer holds a mask (not tested here). Now: - `set_s` clears the mark on every write, after the uniformity of the new value is judged (#1931), so the readfirstlane proof still sees a source register as it was before the write. - The writers of a mask (`v_cmp*_e64`, `s_and_saveexec`, `s_*_wrexec`, a copy of `exec_lo`, `s_cselect_b64`, the mask logic ops) mark the register again after the write. - `s_load` clears the mark on its destination registers, including the forms that only record a descriptor symbol. New test `tests/startup_gfx2hlsl_mask_overwrite_check.py`, registered as `startup_gfx2hlsl_mask_overwrite_controls` in `CMakeLists.txt`, `AGENTS.md` and the Python job, and described in `docs/experiments.md`. It overwrites a compare result with `s_buffer_load`, `s_load` (through a root pointer, and of a descriptor) or `s_cselect_b32`, then evaluates the following OR on a buffer word. Controls: without the overwrite the OR stays a mask OR, and a compare, saveexec, wrexec, exec copy, `s_cselect_b64` or class compare after a load marks the register again. Verification (branch head `7ebe57e7` on `main` `246a875d`, Windows 11 Pro 10.0.26300, Python 3.13): - `python -B tests/startup_gfx2hlsl_mask_overwrite_check.py` -> exit 0 (`mask overwrite: loads and selects clear the mask mark; mask writers mark again`). On `main` the same test reports `FAILED: 7 check(s)`. - every `tests/startup_gfx2hlsl_*_check.py` (including `startup_gfx2hlsl_readfirstlane_uniform_check.py`), `tests/startup_translator_check.py`, `tests/docs_links_check.py` and `tests/startup_tools_lint_check.py tools/startup` -> 33 scripts, all exit 0. - `git diff --check main..HEAD` -> clean. - Not run: a DXC compile or a draw; the check evaluates the emitted HLSL text only. AI-assisted.
The translator keeps the SGPRs that hold a lane mask in Stage.masks and
lowers a scalar logic op with a mask source as a mask op (the lane's bit).
Only the generic scalar path cleared the mark on an ordinary write, so
after v_cmp_eq_u32_e64 s6, v0, v1 and s_buffer_load_dword s6, s[16:19], 0x0
the OR s_or_b32 s8, s6, s7 was emitted as s8 = (s6 | s7) & 1. set_s now
clears the mark on every write, the mask writers mark the register again
after it, and s_load clears the mark on its destination registers, also in
the forms that only record a descriptor symbol.

tests/startup_gfx2hlsl_mask_overwrite_check.py overwrites a compare result
with s_buffer_load, s_load (root pointer and descriptor) and s_cselect_b32
and evaluates the following OR on a buffer word; controls keep the mask
OR without the overwrite and re-mark after a later compare or mask op. On
the translator before this change seven checks fail.

Test: ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_mask_overwrite_controls$' -V
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 23:58:15 +00:00
github manually merged commit 7ebe57e73f into gh-pr/2122/base 2026-11-26 23:58:16 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.