startup: drop scalar constants at joins that another path writes in gfx2hlsl #2271

Merged
lukaslw-lab merged 2 commits from upstream-pr/2279 into main 2026-11-25 09:41:51 +00:00
lukaslw-lab commented 2026-11-25 09:28:23 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by lukaslw-lab on 2026-11-25T09:28:23Z.


Change

Fixes #2278.

At a branch target, Stage.translate now drops the ('const', n) symbol of every SGPR that another path into the target can write:

  • backward branch: the loop body [target, branch];
  • forward branch: the skipped range (branch, target);
  • the proven s_setpc_b64 jumps from #2238 are included.

Registers that no other path writes keep their constant. set_s also assigns constants at run time, so a dropped register reads its real value. Non-constant symbols (pointers, descriptors) are unchanged.

Before this change, a loop counter set to 0 before a loop was folded into the loop body (s14 = 0x00000000u + 0x00000001u;), so the loop ran to the 65,536-iteration dispatcher guard and every value derived from the counter was frozen.

Verification

tests/startup_gfx2hlsl_loop_constants_check.py (new, pure Python, registered in CMake, the Linux CI list and AGENTS.md):

Case Checks
loop counter s14 = s14 + 0x00000001u; and float(int(s14)) inside the loop
loop offset from counter the per-iteration shift reads the counter
forward join a register written only on the fall-through path is not folded after the join
control a constant set before the loop and not written in it stays folded

Mutations, each turning the check red: constants kept at every join (the old behavior), and forward branches handled without the loop body.

On b55187e5 (main 07c1c20f + this change), Windows 10 19045, Python 3.13:

python -B tools/run_checks_parallel.py <the python-linux list from windows.yml>

105 checks ok, including startup_gfx2hlsl_loop_constants, startup_gfx2hlsl_calculated_target (#2238) and both readfirstlane checks. Eight checks fail on this Windows host with and without this change, with the same exit codes, because they need the Linux environment: mesh_heap_sampler, mesh_dynamic_geometry, mesh_dynamic_aot, mesh_lane_spill, mesh_join_descriptor, pso_readiness and startup_run_ps1 exit 1, and startup_build_rev exits 77. The Linux CI job runs all of them.

Local shader cache, same requests translated with main and with this change: 17 of 171 pipelines change, exactly the 17 that contain a loop. Every changed line replaces a folded constant with the register (R = R + K, conversions and shifts of the counter, reversebits).

Evidence and follow-up

The measurements are in #2278: a local startup run, where three translated full-screen pixel passes with folded loop counters took ~99% of the GPU frame (330–440 ms per present). A rerun with the fixed translation has not reached the same load phase yet; I'll add the numbers there.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture (the control case and two mutations).
  • I updated README/docs when a capability or build requirement changed (no capability change; the check is listed in AGENTS.md).
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6601346151 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2279). Originally posted by [lukaslw-lab](https://github.com/lukaslw-lab) on 2026-11-25T09:28:23Z. --- ## Change Fixes #2278. At a branch target, `Stage.translate` now drops the `('const', n)` symbol of every SGPR that another path into the target can write: - **backward branch:** the loop body `[target, branch]`; - **forward branch:** the skipped range `(branch, target)`; - the proven `s_setpc_b64` jumps from #2238 are included. Registers that no other path writes keep their constant. `set_s` also assigns constants at run time, so a dropped register reads its real value. Non-constant symbols (pointers, descriptors) are unchanged. Before this change, a loop counter set to 0 before a loop was folded into the loop body (`s14 = 0x00000000u + 0x00000001u;`), so the loop ran to the 65,536-iteration dispatcher guard and every value derived from the counter was frozen. ## Verification `tests/startup_gfx2hlsl_loop_constants_check.py` (new, pure Python, registered in CMake, the Linux CI list and AGENTS.md): | Case | Checks | |---|---| | loop counter | `s14 = s14 + 0x00000001u;` and `float(int(s14))` inside the loop | | loop offset from counter | the per-iteration shift reads the counter | | forward join | a register written only on the fall-through path is not folded after the join | | control | a constant set before the loop and not written in it stays folded | Mutations, each turning the check red: constants kept at every join (the old behavior), and forward branches handled without the loop body. On `b55187e5` (main `07c1c20f` + this change), Windows 10 19045, Python 3.13: ``` python -B tools/run_checks_parallel.py <the python-linux list from windows.yml> ``` 105 checks ok, including `startup_gfx2hlsl_loop_constants`, `startup_gfx2hlsl_calculated_target` (#2238) and both `readfirstlane` checks. Eight checks fail on this Windows host with and without this change, with the same exit codes, because they need the Linux environment: `mesh_heap_sampler`, `mesh_dynamic_geometry`, `mesh_dynamic_aot`, `mesh_lane_spill`, `mesh_join_descriptor`, `pso_readiness` and `startup_run_ps1` exit 1, and `startup_build_rev` exits 77. The Linux CI job runs all of them. Local shader cache, same requests translated with `main` and with this change: 17 of 171 pipelines change, exactly the 17 that contain a loop. Every changed line replaces a folded constant with the register (`R = R + K`, conversions and shifts of the counter, `reversebits`). ## Evidence and follow-up The measurements are in #2278: a local startup run, where three translated full-screen pixel passes with folded loop counters took ~99% of the GPU frame (330–440 ms per present). A rerun with the fixed translation has not reached the same load phase yet; I'll add the numbers there. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture (the control case and two mutations). - [x] I updated README/docs when a capability or build requirement changed (no capability change; the check is listed in AGENTS.md). - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
upstream-sync closed this pull request 2026-11-25 10:05:29 +00:00
tedin7 commented 2026-11-25 10:05:30 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T09:34:13Z.

Review: COMMENTED


Review of head b55187e5c774d4768f72fb1bdea2936ccb35d69c (comment only).

Summary: the change is correct for the cases it targets. I found no P1 and no P2. Title and body are neutral, the diff is small and clean-room, and the PR is mergeable.

What I checked

  • git diff --stat origin/main...HEAD: 5 files, +123. The change is Stage.join_writes plus the drop of constants at joins in tools/startup/gfx2hlsl.py. The rest is the new test and its CTest, CI and AGENTS.md wiring.
  • The logic is sound. At each branch target the scalar registers written by the other path are collected: the loop body [target, branch] for a backward branch, and the skipped range for a forward one. A tracked const for any of those registers is dropped, so set_s falls back to the runtime value. That fixes a loop counter set before the loop and incremented inside it.
  • The wiring is complete: .github/workflows/windows.yml, AGENTS.md and CMakeLists.txt all list the new check.

Commands run locally on the PR head

  • python3 -B tests/startup_gfx2hlsl_loop_constants_check.py: PASS (4 cases).
  • All tests/startup_gfx2hlsl_*_check.py run in a loop: no failures.
  • python3 tests/docs_links_check.py: passed (18 files, 435 links).
  • python3 tests/bidi_controls_check.py: passed.
  • git diff --check origin/main...HEAD: clean.
  • Mutation 1: replace del self.sym[reg] with pass. The new test fails on the counter, loop offset and loop-offset-reads-counter cases, so the test kills it.
  • Mutation 2: disable the s_movreld branch (the "M0 picks the destination" rule). The test still passes, so that rule has no coverage (nit 1). The mutation was reverted.

CI

  • The Run platform-independent Python checks on Linux and Assemble and translate gfx1013 fixtures jobs fail with TypeError: 'NoneType' object is not an iterator. The failing line is next(steps) in tools/mesh_entry_aot.py:882, reached from tests/mesh_dynamic_aot_check.py.
  • I ran that check on origin/main and it fails the same way, so it is not caused by this PR. Please fix it separately or rebase on a fix before moving to Ready.
  • Build and test on Windows passes. I did not run the Win2025 VM: the change is Python-only lowering state. The GPU-readback rule for new shader math does not apply, because no new HLSL math is emitted.

Findings

  • P1: none.
  • P2: none.
  • nit 1 (tools/startup/gfx2hlsl.py, in join_writes): the s_movreld branch that marks all 128 SGPRs as written has no test. Mutation 2 survives. One fixture with s_movreld inside a loop would cover it.
  • nit 2 (destinations, used by join_writes): only the first destination, and the VOP3b SDST, are counted. A second implicit scalar destination, for example the link register of s_swappc_b64, is not counted. A constant there could survive a join. This is rare. A comment or a follow-up is enough.
  • nit 3: regs mixes ints with the string 'm0'. This is harmless, but a small comment would help.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6055054622 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2279#pullrequestreview-6055054622). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T09:34:13Z. **Review:** COMMENTED --- Review of head `b55187e5c774d4768f72fb1bdea2936ccb35d69c` (comment only). **Summary:** the change is correct for the cases it targets. I found no P1 and no P2. Title and body are neutral, the diff is small and clean-room, and the PR is mergeable. **What I checked** - `git diff --stat origin/main...HEAD`: 5 files, +123. The change is `Stage.join_writes` plus the drop of constants at joins in `tools/startup/gfx2hlsl.py`. The rest is the new test and its CTest, CI and AGENTS.md wiring. - The logic is sound. At each branch target the scalar registers written by the other path are collected: the loop body `[target, branch]` for a backward branch, and the skipped range for a forward one. A tracked `const` for any of those registers is dropped, so `set_s` falls back to the runtime value. That fixes a loop counter set before the loop and incremented inside it. - The wiring is complete: `.github/workflows/windows.yml`, AGENTS.md and `CMakeLists.txt` all list the new check. **Commands run locally on the PR head** - `python3 -B tests/startup_gfx2hlsl_loop_constants_check.py`: PASS (4 cases). - All `tests/startup_gfx2hlsl_*_check.py` run in a loop: no failures. - `python3 tests/docs_links_check.py`: passed (18 files, 435 links). - `python3 tests/bidi_controls_check.py`: passed. - `git diff --check origin/main...HEAD`: clean. - Mutation 1: replace `del self.sym[reg]` with `pass`. The new test fails on the counter, loop offset and loop-offset-reads-counter cases, so the test kills it. - Mutation 2: disable the `s_movreld` branch (the "M0 picks the destination" rule). The test still passes, so that rule has no coverage (nit 1). The mutation was reverted. **CI** - The `Run platform-independent Python checks on Linux` and `Assemble and translate gfx1013 fixtures` jobs fail with `TypeError: 'NoneType' object is not an iterator`. The failing line is `next(steps)` in `tools/mesh_entry_aot.py:882`, reached from `tests/mesh_dynamic_aot_check.py`. - I ran that check on `origin/main` and it fails the same way, so it is not caused by this PR. Please fix it separately or rebase on a fix before moving to Ready. - `Build and test on Windows` passes. I did not run the Win2025 VM: the change is Python-only lowering state. The GPU-readback rule for new shader math does not apply, because no new HLSL math is emitted. **Findings** - P1: none. - P2: none. - nit 1 (`tools/startup/gfx2hlsl.py`, in `join_writes`): the `s_movreld` branch that marks all 128 SGPRs as written has no test. Mutation 2 survives. One fixture with `s_movreld` inside a loop would cover it. - nit 2 (`destinations`, used by `join_writes`): only the first destination, and the VOP3b SDST, are counted. A second implicit scalar destination, for example the link register of `s_swappc_b64`, is not counted. A constant there could survive a join. This is rare. A comment or a follow-up is enough. - nit 3: `regs` mixes ints with the string `'m0'`. This is harmless, but a small comment would help. AI-assisted; commands listed were executed locally.
tedin7 commented 2026-11-25 10:05:30 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T09:43:06Z.

Review: COMMENTED


Re-review of head 59a27f2ace55a7cfcb6ad1c5561b8f957626f60f (comment only).

Summary: no P1 and no P2. The new head is a merge of main into the branch. The PR's own change is unchanged since my last review (b55187e5).

What changed since b55187e5

  • git diff --stat b55187e5 HEAD: README.md, docs/startup-runtime.md, src/startup_stubs/gameinput.cpp, tests/startup_gameinput_contracts_check.cpp, tools/mesh_entry_aot.py. These come from main, not from this PR.
  • tools/startup/gfx2hlsl.py and tests/startup_gfx2hlsl_loop_constants_check.py are untouched by the merge.
  • git diff --stat origin/main...HEAD: still 5 files, +123, the same as before.

CI

  • The earlier mesh_dynamic_aot_check.py failure (a main regression) is gone. The Linux Python checks, the gfx1013 fixtures and Build and test on Windows all pass on this head. The skipped jobs are the /O2 build and the coverage chart.

Commands run locally on this head

  • python3 -B tests/startup_gfx2hlsl_loop_constants_check.py: PASS.
  • All tests/startup_gfx2hlsl_*_check.py: no failures.
  • python3 -B tests/mesh_dynamic_aot_check.py: passes.
  • python3 tests/docs_links_check.py: 18 files, 435 links.
  • python3 tests/bidi_controls_check.py: no bidirectional controls.
  • git diff --check origin/main...HEAD: clean.
  • Mutation: not repeated. The logic is identical to the head where del self.sym[reg] -> pass was killed by the new test.

Findings

  • P1: none.
  • P2: none.
  • The three nits from my earlier review still stand: no test for the s_movreld rule, a second implicit scalar destination is not counted in join_writes, and regs mixes ints with 'm0'. None block.
  • Win2025 not run: Python-only lowering state, no new HLSL math.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6055175209 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2279#pullrequestreview-6055175209). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T09:43:06Z. **Review:** COMMENTED --- Re-review of head `59a27f2ace55a7cfcb6ad1c5561b8f957626f60f` (comment only). **Summary:** no P1 and no P2. The new head is a merge of main into the branch. The PR's own change is unchanged since my last review (`b55187e5`). **What changed since `b55187e5`** - `git diff --stat b55187e5 HEAD`: README.md, docs/startup-runtime.md, src/startup_stubs/gameinput.cpp, tests/startup_gameinput_contracts_check.cpp, tools/mesh_entry_aot.py. These come from main, not from this PR. - `tools/startup/gfx2hlsl.py` and `tests/startup_gfx2hlsl_loop_constants_check.py` are untouched by the merge. - `git diff --stat origin/main...HEAD`: still 5 files, +123, the same as before. **CI** - The earlier `mesh_dynamic_aot_check.py` failure (a main regression) is gone. The Linux Python checks, the gfx1013 fixtures and `Build and test on Windows` all pass on this head. The skipped jobs are the /O2 build and the coverage chart. **Commands run locally on this head** - `python3 -B tests/startup_gfx2hlsl_loop_constants_check.py`: PASS. - All `tests/startup_gfx2hlsl_*_check.py`: no failures. - `python3 -B tests/mesh_dynamic_aot_check.py`: passes. - `python3 tests/docs_links_check.py`: 18 files, 435 links. - `python3 tests/bidi_controls_check.py`: no bidirectional controls. - `git diff --check origin/main...HEAD`: clean. - Mutation: not repeated. The logic is identical to the head where `del self.sym[reg]` -> `pass` was killed by the new test. **Findings** - P1: none. - P2: none. - The three nits from my earlier review still stand: no test for the `s_movreld` rule, a second implicit scalar destination is not counted in `join_writes`, and `regs` mixes ints with `'m0'`. None block. - Win2025 not run: Python-only lowering state, no new HLSL math. AI-assisted; commands listed were executed locally.
lukaslw-lab commented 2026-11-25 10:05:33 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by lukaslw-lab on 2026-11-25T09:30:29Z.


The Linux Python job fails in the mesh_* and pso_readiness checks (TypeError in tools/mesh_entry_aot.py run_group). main fails the same job at 07c1c20f, this PR's base, and this change does not touch the mesh tools. The PR text's note that these need Linux is wrong: they fail on main itself.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7158423972 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2279#issuecomment-7158423972). Originally posted by [lukaslw-lab](https://github.com/lukaslw-lab) on 2026-11-25T09:30:29Z. --- The Linux Python job fails in the `mesh_*` and `pso_readiness` checks (`TypeError` in `tools/mesh_entry_aot.py` `run_group`). `main` fails the same job at `07c1c20f`, this PR's base, and this change does not touch the mesh tools. The PR text's note that these need Linux is wrong: they fail on `main` itself. _AI-assisted (Claude Code)._
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!2271
No description provided.