Skip to content

shader: lower uniform scalar loops in the mesh frontend - #3707

Open
lukaslw-lab wants to merge 3 commits into
R0Y00:mainfrom
lukaslw-lab:shader/mesh-uniform-scalar-loop
Open

lukaslw-lab wants to merge 3 commits into
R0Y00:mainfrom
lukaslw-lab:shader/mesh-uniform-scalar-loop

Conversation

@lukaslw-lab

@lukaslw-lab lukaslw-lab commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

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 #3703:

  • 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 #3703. 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 #3702 and #3704 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 #3703, #3641.

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

@Leyukaka Leyukaka left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

lukaslw-lab and others added 2 commits December 1, 2026 18:48
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 <noreply@anthropic.com>
Track SCC like an SGPR in the loop scan: give it a slot only when the body
reads it before defining it, and refuse when its entry value is unknown
instead of storing 0. Also carry any value the body writes after its first
exit branch, so an exit sees the previous iteration's value; with no entry
value it stays unbound and a later read refuses.

Inline constant 0 shares operand code 128 with the SCC index, so operand
reads stay limited to SGPRs and SCC reads are explicit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@lukaslw-lab
lukaslw-lab force-pushed the shader/mesh-uniform-scalar-loop branch from 1f15a0b to 802f682 Compare December 1, 2026 19:01
@lukaslw-lab

Copy link
Copy Markdown
Contributor Author

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:

…-scalar-loop

# Conflicts:
#	src/gfx1013_direct_mesh.inl
@lukaslw-lab

Copy link
Copy Markdown
Contributor Author

Merged main (0dd65da1) in edadb494 to resolve the conflict with #3797. 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.

@Leyukaka Leyukaka left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 #3797 keeps the VCC-branch case and matches the loop back edge first, and the loop lowering itself is unchanged.

Approving edadb494.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants