shader: lower uniform scalar loops in the mesh frontend #3709

Manually merged
github merged 3 commits from github-bot/gh-3707 into gh-pr/3707/base 2026-12-02 11:25:18 +00:00 AGit
Owner

@⁠lukaslw-lab on GitHub · 2026-12-01 07:32 UTC · original
lukaslw-lab:shader/mesh-uniform-scalar-loop → main · +302 −12 · 3 files · 3 commits · merged 2026-12-02 11:21 UTC

Change

The C++ mesh frontend (lower_mesh) emits DXIL in one forward pass, so it refused every back edge with unsupported mesh scalar branch destination. It now accepts one narrow shape, a uniform scalar loop, as designed in #3705:

  • one header and one back edge (s_cbranch_scc0, s_cbranch_scc1 or s_branch);
  • a body of the 32-bit scalar ALU already allowed between a diamond join and its EXEC restore, scalar compares, scalar loads, s_waitcnt/s_nop/s_clause, and forward exits past the back edge inside the enclosing EXECZ region.

The pass stays forward-only:

  • A value the body writes is carried in a local slot (alloca i32) when one pass reads it before that write, or when an exit branch comes before that write. SCC is tracked like an SGPR. The slots are stored before the header and at the back edge, and loaded in the header. A value with no entry value that only an exit sees stays unbound there, so a read after the loop refuses.
  • A register the body only writes is unbound in the header; one it never writes keeps its entry value.
  • Exits use the existing forward-join merge.
  • The trip count comes from guest data.

Each other loop keeps a named refusal:

  • mesh loop needs exactly one back edge
  • … body holds an unsupported instruction: <name>
  • … back edge needs SCC or an unconditional branch
  • … is entered other than at its header
  • … outside a uniform scalar region (after GS_ALLOC_REQ, under a per-lane else, or outside full EXEC and any EXECZ region)
  • … carries a register without a numeric entry value
  • … reads SCC before the body defines it, without a known entry value

The scalar ALU list moves from the post-join check to function scope so both checks share it; that check is unchanged.

This is item 1 of #3705. 18 captured slot 86 requests share one such loop: a search over records of 0x130 bytes for a flag bit, bounded by dword 0 of root SRV 3. With #3704 and #3706 applied, all 18 pass the loop without any stand-in. Their next refusal is mesh image load lacks a remapped Texture1D heap contract (item 4, open). Refs #3705, #3643.

Verification

Build release at this branch (Windows 10 19045, MSVC 2022, DXC 1.9.2609):

python -B tests/native_direct_mesh_check.py <compile_dxil.exe> <dxc dir>  -> passed
python -B tests/docs_links_check.py                                       -> passed
ctest --test-dir out/build/release -R "native_direct|native_scalar|mesh|native_.*root|indexed|pixel_aot|startup_draw|compute_desc|descriptor_scc"
  -> 481 tests: 332 passed, 100 capability skips (77), 49 failed

42 of the 49 also fail on this host with main's sources (Windows 10 without the Agility SDK; NVIDIA RTX 3080 Ti, driver 32.0.16.1047, debug layer on; pixel_aot_* use the local LLVM tools). The other 7 (pixel_aot_*_hardware) failed in the 6-way parallel run and passed 7 of 7 run one at a time with this change; they do not use the mesh frontend.

New cases in native_direct_mesh_check.py, next to multi-edge:

  • scalar-loop: counts s20 from 0 while it is below root constant s21, and leaves early at 5. The exits set 7 or 9 and join before the export reads s20. The IR check: only s20 gets a slot (the body defines SCC before reading it); s20 is stored before the header and at the back edge; the back edge branches on SCC == 0; the body compares with 5 and with s21.
  • scalar-loop-scc-unknown, -scc-after-move (refused) and -scc-known (SCC slot, no literal 0): SCC entering the loop.
  • scalar-loop-exit-read-{80,81,15}: s22 = 7 before the loop, set to 9 after the exit branch, read only after the exit. The export traces to the header load of the s22 slot, whose only stores are 7 and 9. -exit-unset translates, and -exit-unset-read refuses.
  • scalar-loop-branch-back: an s_branch back edge, leaving only through the early exit; no exit block is emitted.
  • Refusals: scalar-loop-vector, -vcc-back, -two-back, -enter-body.

Controls:

  • Without the frontend change, scalar-loop fails with unsupported mesh scalar branch destination.
  • With the back-edge stores removed, the store count assertion fails.

Evidence and follow-up

Source-built words only. The 18-request counts are local and offline (compile_dxil ... gfx1013-container-ms-texture-heap). Note: docs/experiments/native-mesh-scalar-loop.md.

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.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted.

🤖 Generated with Claude Code

**@⁠lukaslw-lab** on GitHub · 2026-12-01 07:32 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3707) `lukaslw-lab:shader/mesh-uniform-scalar-loop` → `main` · +302 −12 · 3 files · 3 commits · merged 2026-12-02 11:21 UTC ## Change The C++ mesh frontend (`lower_mesh`) emits DXIL in one forward pass, so it refused every back edge with `unsupported mesh scalar branch destination`. It now accepts one narrow shape, a *uniform scalar loop*, as designed in #3705: - one header and one back edge (`s_cbranch_scc0`, `s_cbranch_scc1` or `s_branch`); - a body of the 32-bit scalar ALU already allowed between a diamond join and its EXEC restore, scalar compares, scalar loads, `s_waitcnt`/`s_nop`/`s_clause`, and forward exits past the back edge inside the enclosing EXECZ region. The pass stays forward-only: - A value the body writes is carried in a local slot (`alloca i32`) when one pass reads it before that write, or when an exit branch comes before that write. SCC is tracked like an SGPR. The slots are stored before the header and at the back edge, and loaded in the header. A value with no entry value that only an exit sees stays unbound there, so a read after the loop refuses. - A register the body only writes is unbound in the header; one it never writes keeps its entry value. - Exits use the existing forward-join merge. - The trip count comes from guest data. Each other loop keeps a named refusal: - `mesh loop needs exactly one back edge` - `… body holds an unsupported instruction: <name>` - `… back edge needs SCC or an unconditional branch` - `… is entered other than at its header` - `… outside a uniform scalar region` (after GS_ALLOC_REQ, under a per-lane else, or outside full EXEC and any EXECZ region) - `… carries a register without a numeric entry value` - `… reads SCC before the body defines it, without a known entry value` The scalar ALU list moves from the post-join check to function scope so both checks share it; that check is unchanged. This is item 1 of #3705. 18 captured slot 86 requests share one such loop: a search over records of 0x130 bytes for a flag bit, bounded by dword 0 of root SRV 3. With #3704 and #3706 applied, all 18 pass the loop without any stand-in. Their next refusal is `mesh image load lacks a remapped Texture1D heap contract` (item 4, open). Refs #3705, #3643. ## Verification Build `release` at this branch (Windows 10 19045, MSVC 2022, DXC 1.9.2609): ``` python -B tests/native_direct_mesh_check.py <compile_dxil.exe> <dxc dir> -> passed python -B tests/docs_links_check.py -> passed ctest --test-dir out/build/release -R "native_direct|native_scalar|mesh|native_.*root|indexed|pixel_aot|startup_draw|compute_desc|descriptor_scc" -> 481 tests: 332 passed, 100 capability skips (77), 49 failed ``` 42 of the 49 also fail on this host with `main`'s sources (Windows 10 without the Agility SDK; NVIDIA RTX 3080 Ti, driver 32.0.16.1047, debug layer on; `pixel_aot_*` use the local LLVM tools). The other 7 (`pixel_aot_*_hardware`) failed in the 6-way parallel run and passed 7 of 7 run one at a time with this change; they do not use the mesh frontend. New cases in `native_direct_mesh_check.py`, next to `multi-edge`: - `scalar-loop`: counts s20 from 0 while it is below root constant s21, and leaves early at 5. The exits set 7 or 9 and join before the export reads s20. The IR check: only s20 gets a slot (the body defines SCC before reading it); s20 is stored before the header and at the back edge; the back edge branches on SCC == 0; the body compares with 5 and with s21. - `scalar-loop-scc-unknown`, `-scc-after-move` (refused) and `-scc-known` (SCC slot, no literal 0): SCC entering the loop. - `scalar-loop-exit-read-{80,81,15}`: s22 = 7 before the loop, set to 9 after the exit branch, read only after the exit. The export traces to the header load of the s22 slot, whose only stores are 7 and 9. `-exit-unset` translates, and `-exit-unset-read` refuses. - `scalar-loop-branch-back`: an `s_branch` back edge, leaving only through the early exit; no exit block is emitted. - Refusals: `scalar-loop-vector`, `-vcc-back`, `-two-back`, `-enter-body`. Controls: - Without the frontend change, `scalar-loop` fails with `unsupported mesh scalar branch destination`. - With the back-edge stores removed, the store count assertion fails. ## Evidence and follow-up Source-built words only. The 18-request counts are local and offline (`compile_dxil ... gfx1013-container-ms-texture-heap`). Note: `docs/experiments/native-mesh-scalar-loop.md`. ## 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. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
The mesh frontend emitted DXIL in one forward pass and refused every back
edge. Accept one narrow shape: one header, one back edge (s_cbranch_scc0,
s_cbranch_scc1 or s_branch), and a body of the 32-bit scalar ALU that is
already allowed between a diamond join and its EXEC restore, scalar
compares, scalar loads, waits and forward exits past the back edge inside
the enclosing EXECZ region.

The pass stays forward-only: registers the body writes after reading
them, and SCC, live in local slots stored before the header and at the
back edge and loaded in the header. A register the body only writes is
unbound in the header; one it never writes keeps its entry value. Exits
use the existing forward-join merge. Every other loop keeps a named
refusal.

The scalar ALU list moves to function scope so the loop check and the
post-join check share it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-01 07:37:09 +00:00
Author
Owner

@⁠Leyukaka on GitHub · 2026-12-01 16:44 UTC · original · requested changes

open_loop initializes the SCC slot to zero when its incoming value is unavailable. For example, s_or_b32 with an unmaterialized source register and an immediate 1 leaves SCC unavailable in the frontend. The guest result always has bit 0 set, so SCC is 1. If the loop starts with s_cbranch_scc1 to an exit before the body defines SCC, the guest takes the exit and the emitted first iteration falls through. Please retain a proven incoming SCC or refuse an unproven read, while still accepting a loop that defines SCC before reading it.

The carried-register analysis also loses a value at an early exit. Initialize s22 to 7, branch from the header to the exit, and on the path through the body write 9 to s22. Let the only read of s22 be at that exit. The guest reads 7 on an immediate exit and 9 after one completed iteration. The scan sees the write but misses the read outside the loop, so the header clears s22 and the exit snapshot contains an unbound value at either trip count. Please preserve s22 at that exit and test both trip counts. The existing fixture rewrites s20 in each exit block before reading it.

**@⁠Leyukaka** on GitHub · 2026-12-01 16:44 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3707#pullrequestreview-6135734821) · **requested changes** open_loop initializes the SCC slot to zero when its incoming value is unavailable. For example, s_or_b32 with an unmaterialized source register and an immediate 1 leaves SCC unavailable in the frontend. The guest result always has bit 0 set, so SCC is 1. If the loop starts with s_cbranch_scc1 to an exit before the body defines SCC, the guest takes the exit and the emitted first iteration falls through. Please retain a proven incoming SCC or refuse an unproven read, while still accepting a loop that defines SCC before reading it. The carried-register analysis also loses a value at an early exit. Initialize s22 to 7, branch from the header to the exit, and on the path through the body write 9 to s22. Let the only read of s22 be at that exit. The guest reads 7 on an immediate exit and 9 after one completed iteration. The scan sees the write but misses the read outside the loop, so the header clears s22 and the exit snapshot contains an unbound value at either trip count. Please preserve s22 at that exit and test both trip counts. The existing fixture rewrites s20 in each exit block before reading it.
github-bot force-pushed github-bot/gh-3707 from 1f15a0bfff to 802f682db7 2026-12-01 19:03:11 +00:00 Compare
Author
Owner

@⁠lukaslw-lab on GitHub · 2026-12-01 19:01 UTC · original

Thanks, both points were real. Fixed in 802f682d, with the branch rebased on main 158ef794.

SCC entering the loop. SCC no longer always gets a slot, and nothing stores a literal 0. The scan tracks SCC like an SGPR. It gets a slot only when the body reads it before defining it, either at an exit or back edge, or in s_cselect_b32 / s_addc_u32 / s_subb_u32. A carried SCC with no known entry value now refuses with mesh loop reads SCC before the body defines it, without a known entry value. A loop that defines SCC before reading it is still accepted, as in the original fixture, which now has no SCC slot.

Values seen at an early exit. Every value the body writes after its first exit branch is now carried, so that exit sees the previous iteration's value. Your s22 case: the slot is stored 7 before the loop and 9 at the back edge, and the exit reads the header's load of it. If such a value has no entry value, it stays unbound at the exit, and a read after the loop refuses with unbound numeric SGPR sN.

Case (native_direct_mesh) Result
scalar-loop-scc-unknown: SCC exit at the top of the body, SCC never set before refused
scalar-loop-scc-after-move: same, after an s_mov_b32 refused
scalar-loop-scc-known: SCC set from the root constant before the loop SCC slot; both stores are IR values
scalar-loop-exit-read-{80,81,15}: your s22 case with bound 0, bound 1 and root constant s21 export x traces to the header load of the s22 slot; the slot's only stores are 7 (entry) and 9 (back edge)
scalar-loop-exit-unset: s22 unset before the loop, not read after it translates, no s22 slot
scalar-loop-exit-unset-read: same, read at the exit unbound numeric SGPR s22

The harness checks emitted DXIL text and does not execute it. So the trip-count cases are checked through dataflow: zero trips read the entry store, and one trip reads the back-edge store.

Controls: each line below is a one-line change, and native_direct_mesh fails with it.

  • An unknown SCC is left uncarried instead of refused: scalar-loop-scc-unknown.
  • The exit rule is off (only read-before-write values are carried): scalar-loop-exit-read-80 fails with unbound numeric SGPR s22.
  • An exit-only value is carried even with no entry value: scalar-loop-exit-unset is refused.
  • s_mov_b32 is counted as an SCC write: scalar-loop-scc-after-move.

Local, Windows Release, 802f682d:

  • ctest --preset release -R '^native_direct|mesh_loop|startup_mesh57_loop': 7 passed, 1 skipped (startup_mesh57_loop_warp, exit 77).
  • docs_links_check.py: pass.
  • The 24 captured slot 86 requests from #3705, on current main: the 18 loop programs still pass the loop and stop at the table texture load (#3714). The other 6 stop at item 6, unchanged.
**@⁠lukaslw-lab** on GitHub · 2026-12-01 19:01 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3707#issuecomment-7322006113) Thanks, both points were real. Fixed in `802f682d`, with the branch rebased on `main` `158ef794`. **SCC entering the loop.** SCC no longer always gets a slot, and nothing stores a literal 0. The scan tracks SCC like an SGPR. It gets a slot only when the body reads it before defining it, either at an exit or back edge, or in `s_cselect_b32` / `s_addc_u32` / `s_subb_u32`. A carried SCC with no known entry value now refuses with `mesh loop reads SCC before the body defines it, without a known entry value`. A loop that defines SCC before reading it is still accepted, as in the original fixture, which now has no SCC slot. **Values seen at an early exit.** Every value the body writes after its first exit branch is now carried, so that exit sees the previous iteration's value. Your s22 case: the slot is stored 7 before the loop and 9 at the back edge, and the exit reads the header's load of it. If such a value has no entry value, it stays unbound at the exit, and a read after the loop refuses with `unbound numeric SGPR sN`. | Case (`native_direct_mesh`) | Result | |---|---| | `scalar-loop-scc-unknown`: SCC exit at the top of the body, SCC never set before | refused | | `scalar-loop-scc-after-move`: same, after an `s_mov_b32` | refused | | `scalar-loop-scc-known`: SCC set from the root constant before the loop | SCC slot; both stores are IR values | | `scalar-loop-exit-read-{80,81,15}`: your s22 case with bound 0, bound 1 and root constant s21 | export x traces to the header load of the s22 slot; the slot's only stores are `7` (entry) and `9` (back edge) | | `scalar-loop-exit-unset`: s22 unset before the loop, not read after it | translates, no s22 slot | | `scalar-loop-exit-unset-read`: same, read at the exit | `unbound numeric SGPR s22` | The harness checks emitted DXIL text and does not execute it. So the trip-count cases are checked through dataflow: zero trips read the entry store, and one trip reads the back-edge store. Controls: each line below is a one-line change, and `native_direct_mesh` fails with it. - An unknown SCC is left uncarried instead of refused: `scalar-loop-scc-unknown`. - The exit rule is off (only read-before-write values are carried): `scalar-loop-exit-read-80` fails with `unbound numeric SGPR s22`. - An exit-only value is carried even with no entry value: `scalar-loop-exit-unset` is refused. - `s_mov_b32` is counted as an SCC write: `scalar-loop-scc-after-move`. Local, Windows Release, `802f682d`: - `ctest --preset release -R '^native_direct|mesh_loop|startup_mesh57_loop'`: 7 passed, 1 skipped (`startup_mesh57_loop_warp`, exit 77). - `docs_links_check.py`: pass. - The 24 captured slot 86 requests from #3705, on current `main`: the 18 loop programs still pass the loop and stop at the table texture load (#3714). The other 6 stop at item 6, unchanged.
Author
Owner

@⁠lukaslw-lab on GitHub · 2026-12-02 03:27 UTC · original

Merged main (0dd65da1) in edadb494 to resolve the conflict with #3799. Its new VCC-branch case and this PR's loop back edge sit next to each other in the branch dispatch in lower_mesh, and both are kept, with the back edge matched first. The loop lowering itself is unchanged.

Release build: native_direct_mesh, native_direct_container and mesh_loop_census pass.

AI-assisted.

**@⁠lukaslw-lab** on GitHub · 2026-12-02 03:27 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3707#issuecomment-7333891144) Merged main (`0dd65da1`) in `edadb494` to resolve the conflict with #3799. Its new VCC-branch case and this PR's loop back edge sit next to each other in the branch dispatch in `lower_mesh`, and both are kept, with the back edge matched first. The loop lowering itself is unchanged. Release build: `native_direct_mesh`, `native_direct_container` and `mesh_loop_census` pass. AI-assisted.
Author
Owner

@⁠Leyukaka on GitHub · 2026-12-02 07:02 UTC · original · approved these changes

Re-review of edadb494 (fix in 802f682d, merge of main in edadb494). Read the diff; not built or run.

Both findings from my earlier review are addressed:

  • SCC entering the loop. open_loop no longer stores a literal 0. SCC (128) is tracked like an SGPR, and a slot is created only when the body reads it before writing it (exit, back edge, or s_cselect_b32 / s_addc_u32 / s_subb_u32). If the entry value is not available, the loop refuses with mesh loop reads SCC before the body defines it, without a known entry value. scalar-loop-scc-unknown and scalar-loop-scc-after-move cover the refusal, and scalar-loop-scc-known covers the carried case. The second control also checks that s_mov_b32 is not counted as an SCC write.
  • Value read at an early exit. A register written only after the first exit branch is now carried, so the exit sees the previous iteration's value (or the entry value). In my s22 example, the slot is stored with 7 before the loop and 9 at the back edge, and the export reads the header load. scalar-loop-exit-read-{80,81,15} check this for zero trips, one trip and a root bound. Without an entry value, the register is not carried. A later read refuses through alu.read (unbound numeric SGPR s22), and scalar-loop-exit-unset-read covers that.

I found no new problem in the loop scan. Non-forward branches inside the body are refused, a nested back edge hits the forward-exit check, and a carried value without a numeric entry value refuses instead of using a placeholder.

The merge with #3799 keeps the VCC-branch case and matches the loop back edge first, and the loop lowering itself is unchanged.

Approving edadb494.

**@⁠Leyukaka** on GitHub · 2026-12-02 07:02 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3707#pullrequestreview-6146133053) · **approved these changes** Re-review of `edadb494` (fix in `802f682d`, merge of main in `edadb494`). Read the diff; not built or run. Both findings from my earlier review are addressed: - **SCC entering the loop.** `open_loop` no longer stores a literal 0. SCC (128) is tracked like an SGPR, and a slot is created only when the body reads it before writing it (exit, back edge, or `s_cselect_b32` / `s_addc_u32` / `s_subb_u32`). If the entry value is not available, the loop refuses with `mesh loop reads SCC before the body defines it, without a known entry value`. `scalar-loop-scc-unknown` and `scalar-loop-scc-after-move` cover the refusal, and `scalar-loop-scc-known` covers the carried case. The second control also checks that `s_mov_b32` is not counted as an SCC write. - **Value read at an early exit.** A register written only after the first exit branch is now carried, so the exit sees the previous iteration's value (or the entry value). In my s22 example, the slot is stored with 7 before the loop and 9 at the back edge, and the export reads the header load. `scalar-loop-exit-read-{80,81,15}` check this for zero trips, one trip and a root bound. Without an entry value, the register is not carried. A later read refuses through `alu.read` (`unbound numeric SGPR s22`), and `scalar-loop-exit-unset-read` covers that. I found no new problem in the loop scan. Non-forward branches inside the body are refused, a nested back edge hits the forward-exit check, and a carried value without a numeric entry value refuses instead of using a placeholder. The merge with #3799 keeps the VCC-branch case and matches the loop back edge first, and the loop lowering itself is unchanged. Approving `edadb494`.
github-bot changed target branch from main to gh-pr/3707/base 2026-12-02 11:25:16 +00:00
github manually merged commit edadb494dd into gh-pr/3707/base 2026-12-02 11:25:18 +00:00
Author
Owner

@⁠R0Y00 on GitHub · 2026-12-02 11:21 UTC · original · approved these changes

Windows Debug exact head passed ctest --preset dev --no-tests=error -j 2 -R '^native_direct_(mesh|container)$' --output-on-failure (2/2). The local combination with main e143824c and the #3853 fixture correction also passed both checks and native_mesh_packed_rows_vcc_ir (3/3). This confirms the loop changes on the exact head and against the newer VCC path.

**@⁠R0Y00** on GitHub · 2026-12-02 11:21 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3707#pullrequestreview-6149765502) · **approved these changes** Windows Debug exact head passed `ctest --preset dev --no-tests=error -j 2 -R '^native_direct_(mesh|container)$' --output-on-failure` (2/2). The local combination with main `e143824c` and the #3853 fixture correction also passed both checks and `native_mesh_packed_rows_vcc_ir` (3/3). This confirms the loop changes on the exact head and against the newer VCC path.
This discussion has been locked. Commenting is limited to contributors.
No description provided.