shader: accept scalar work between a mesh diamond join and its EXEC restore (Part of #3416) #3633

Merged
mumba17 merged 2 commits from upstream-pr/3633 into main 2026-11-30 19:35:08 +00:00
mumba17 commented 2026-11-30 18:21:20 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by mumba17 on 2026-11-30T18:21:20Z.


Part of #3416 (claim in #2413). Target main.

Problem

mesh EXEC write inside a lane-skip block is the second-largest C++ refusal among the top slot 57 variants in the #3416 call-weighted table (0.78 M refused DispatchMesh calls in a 360 s run). In those programs a signed-CMPX if/else closes with s_mov_b64 exec, <saved>, but the compiler places EXEC-independent scalar work between the diamond's join and that restore:

  • SMEM loads;
  • SALU;
  • s_waitcnt and s_clause.

The frontend accepted the restore only exactly at the join. With anything in between, the restore counted as an EXEC write inside the enclosing lane skip.

Change

src/gfx1013_direct_mesh.inl, lower_mesh:

  • After a diamond join, the frontend keeps a diamond_joined flag for as long as each following instruction is EXEC-independent scalar work:
    • SMEM loads (s_load_dword*, s_buffer_load_dword*) that write below VCC;
    • s_waitcnt, s_clause and s_nop;
    • SOP1, SOP2, SOPK and SOPC instructions that neither read nor write EXEC or VCC, and are not PC or movrel forms.
  • Any other instruction clears the flag, and so does another path arriving through a forward branch.
  • An s_mov_b64 exec, <saved> reached while the flag holds is accepted on the same terms as the existing closed-nested-skip restore: the saved mask must still be valid and must name the open skip's guard. An instruction in the gap that overwrites the saved mask therefore still leaves the restore refused.

These instructions run once per wave whatever EXEC holds, so their effect is the same as if they followed the restore. #2900 made the same argument for the compute path.

Tests

tests/native_direct_mesh_check.py, next to the existing signed-cmpx-exec-restore case. Programs are source-built gfx1013 words:

  • signed-cmpx-scalar-gap: the same diamond with a table s_load_dwordx4, its s_waitcnt and an s_add_u32 between the join and the restore. It translates, and the join produces a phi.
  • Negative controls, each refused at the restore with mesh EXEC write inside a lane-skip block:
    • gap-vcc-write: s_mov_b64 vcc, -1 in the gap;
    • gap-branch: an s_branch in the gap;
    • gap-saved-mask: the saved mask register overwritten in the gap;
    • gap-exec-read: s_mov_b64 s[28:29], exec in the gap.
  • Mutation checks:
    • without the restore change, signed-cmpx-scalar-gap fails at +0x5c;
    • accepting every instruction in the gap lets gap-vcc-write compile.

Windows 11 build 26300, MSVC 19.44, Release, RTX 3090. ctest --preset release -R "mesh|compile_dxil" -j4: 192 of 192 passed (two mesh_two_exits capability skips).

ctest --preset release --no-tests=error -R "^native_direct_mesh$" --output-on-failure

Census

compile_dxil --refusals over 454 slot 57 requests in a local start-up cache, with the #3416 layout (packed_parameters, validate_uint_heap_reads, guest_root):

Refusal Before After
mesh EXEC write inside a lane-skip block 63 8

The 55 requests that move on stop at later refusals, mostly instructions the mesh frontend does not cover yet:

Next refusal Requests
v_fma_mixlo_f16 +15
v_sqrt_f32 +8
v_fmamk_f32 +5
v_cvt_f16_f32 +5
s_movk_i32 +5
v_cmpx_eq_i32 +4
v_fma_mixhi_f16 +4

No request translates fully yet, so this does not change a title draw on its own.

Not covered: the 8 left with this refusal include an else arm (s_andn2_b64 exec, <older saved mask>, exec) that merges lanes after a large then-arm with scalar branches. That needs lane-merge support, which is not part of this change.

🤖 Generated with Claude Code

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:5571173724 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3633). Originally posted by [mumba17](https://github.com/mumba17) on 2026-11-30T18:21:20Z. --- Part of #3416 (claim in #2413). Target `main`. ## Problem `mesh EXEC write inside a lane-skip block` is the second-largest C++ refusal among the top slot 57 variants in the #3416 call-weighted table (0.78 M refused `DispatchMesh` calls in a 360 s run). In those programs a signed-CMPX if/else closes with `s_mov_b64 exec, <saved>`, but the compiler places EXEC-independent scalar work between the diamond's join and that restore: - SMEM loads; - SALU; - `s_waitcnt` and `s_clause`. The frontend accepted the restore only exactly at the join. With anything in between, the restore counted as an EXEC write inside the enclosing lane skip. ## Change `src/gfx1013_direct_mesh.inl`, `lower_mesh`: - After a diamond join, the frontend keeps a `diamond_joined` flag for as long as each following instruction is EXEC-independent scalar work: - SMEM loads (`s_load_dword*`, `s_buffer_load_dword*`) that write below VCC; - `s_waitcnt`, `s_clause` and `s_nop`; - SOP1, SOP2, SOPK and SOPC instructions that neither read nor write EXEC or VCC, and are not PC or `movrel` forms. - Any other instruction clears the flag, and so does another path arriving through a forward branch. - An `s_mov_b64 exec, <saved>` reached while the flag holds is accepted on the same terms as the existing closed-nested-skip restore: the saved mask must still be valid and must name the open skip's guard. An instruction in the gap that overwrites the saved mask therefore still leaves the restore refused. These instructions run once per wave whatever EXEC holds, so their effect is the same as if they followed the restore. #2900 made the same argument for the compute path. ## Tests `tests/native_direct_mesh_check.py`, next to the existing `signed-cmpx-exec-restore` case. Programs are source-built gfx1013 words: - `signed-cmpx-scalar-gap`: the same diamond with a table `s_load_dwordx4`, its `s_waitcnt` and an `s_add_u32` between the join and the restore. It translates, and the join produces a `phi`. - Negative controls, each refused at the restore with `mesh EXEC write inside a lane-skip block`: - `gap-vcc-write`: `s_mov_b64 vcc, -1` in the gap; - `gap-branch`: an `s_branch` in the gap; - `gap-saved-mask`: the saved mask register overwritten in the gap; - `gap-exec-read`: `s_mov_b64 s[28:29], exec` in the gap. - Mutation checks: - without the restore change, `signed-cmpx-scalar-gap` fails at `+0x5c`; - accepting every instruction in the gap lets `gap-vcc-write` compile. Windows 11 build 26300, MSVC 19.44, Release, RTX 3090. `ctest --preset release -R "mesh|compile_dxil" -j4`: 192 of 192 passed (two `mesh_two_exits` capability skips). ```powershell ctest --preset release --no-tests=error -R "^native_direct_mesh$" --output-on-failure ``` ## Census `compile_dxil --refusals` over 454 slot 57 requests in a local start-up cache, with the #3416 layout (`packed_parameters`, `validate_uint_heap_reads`, `guest_root`): | Refusal | Before | After | |---|---:|---:| | `mesh EXEC write inside a lane-skip block` | 63 | 8 | The 55 requests that move on stop at later refusals, mostly instructions the mesh frontend does not cover yet: | Next refusal | Requests | |---|---:| | `v_fma_mixlo_f16` | +15 | | `v_sqrt_f32` | +8 | | `v_fmamk_f32` | +5 | | `v_cvt_f16_f32` | +5 | | `s_movk_i32` | +5 | | `v_cmpx_eq_i32` | +4 | | `v_fma_mixhi_f16` | +4 | No request translates fully yet, so this does not change a title draw on its own. Not covered: the 8 left with this refusal include an else arm (`s_andn2_b64 exec, <older saved mask>, exec`) that merges lanes after a large then-arm with scalar branches. That needs lane-merge support, which is not part of this change. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
lukaslw-lab commented 2026-11-30 18:54:33 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by lukaslw-lab on 2026-11-30T18:54:33Z.

Review: APPROVED


Reviewed ebaf1817 by reading the diff; not built or run. I checked the two target sites offline on our side (see the table).

Verdict: approve. The acceptance stays on the existing closed-nested-skip terms: the restored mask must be the open skip's own guard (gfx1013_direct_mesh.inl:1074). So whatever the gap holds, a restore cannot enable lanes outside the skip. The gap allowlist is extra caution on top of that. No MUST.

Target sites, using compile_dxil built from main 35a3f6d6 + #3619 (inert without heap_tokens; the rest of the mesh frontend is unchanged up to bee9c64d), with the #3416 layout, offline on Windows 10 19045:

Request Main Join → restore
f2b3d808 +0x70c EXEC write inside a lane-skip block join +0x6fc: s_load_dwordx4, s_buffer_load_dword, then s_mov_b64 exec, s[20:21]
2319d05e same same shape; s[20:21] saved at +0x4e0, inside the skip opened at +0x1ac, so the mask is the guard
  • SHOULD The fixture's gap (native_direct_mesh_check.py, scalar_gap) has s_load_dwordx4, s_waitcnt and s_add_u32, but no s_buffer_load_dword. That is the second instruction at both real sites, and the only path through the SMEM op 8–12 branch (:424). Adding one to scalar_gap covers that branch.
  • NIT diamond_joined is set at the join (:409) before it is read (:416). So a restore placed at the join of a diamond whose restore names another mask (restores_exec == false) is now accepted too, when that mask is the guard. This is sound on the same terms, but neither the comment nor a fixture describes it.
  • NIT The SALU test is a name denylist (:429). s_call_b64, s_setreg_b32, s_subvector_loop_* and s_rfe_b64 pass it, and they stay safe only because the frontend refuses them later (:1274). An allowlist of the SALU forms the frontend lowers would keep the gap rule tight as SOPK support grows (the s_movk_i32 claim on #2413).

Tests: the four negative controls prove that a VCC write, a branch, a saved-mask overwrite or an EXEC read in the gap keeps the refusal. CI did not run them: native_direct_mesh is not in the pr preset filter. The author ran it locally (192/192).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6118464235 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3633#pullrequestreview-6118464235). Originally posted by [lukaslw-lab](https://github.com/lukaslw-lab) on 2026-11-30T18:54:33Z. **Review:** APPROVED --- Reviewed `ebaf1817` by reading the diff; not built or run. I checked the two target sites offline on our side (see the table). **Verdict: approve.** The acceptance stays on the existing closed-nested-skip terms: the restored mask must be the open skip's own guard (`gfx1013_direct_mesh.inl:1074`). So whatever the gap holds, a restore cannot enable lanes outside the skip. The gap allowlist is extra caution on top of that. No MUST. Target sites, using `compile_dxil` built from main 35a3f6d6 + #3619 (inert without `heap_tokens`; the rest of the mesh frontend is unchanged up to bee9c64d), with the #3416 layout, offline on Windows 10 19045: | Request | Main | Join → restore | |---|---|---| | f2b3d808 | `+0x70c` EXEC write inside a lane-skip block | join `+0x6fc`: `s_load_dwordx4`, `s_buffer_load_dword`, then `s_mov_b64 exec, s[20:21]` | | 2319d05e | same | same shape; `s[20:21]` saved at `+0x4e0`, inside the skip opened at `+0x1ac`, so the mask is the guard | - **SHOULD** The fixture's gap (`native_direct_mesh_check.py`, `scalar_gap`) has `s_load_dwordx4`, `s_waitcnt` and `s_add_u32`, but no `s_buffer_load_dword`. That is the second instruction at both real sites, and the only path through the SMEM op 8–12 branch (`:424`). Adding one to `scalar_gap` covers that branch. - **NIT** `diamond_joined` is set at the join (`:409`) before it is read (`:416`). So a restore placed *at* the join of a diamond whose restore names another mask (`restores_exec == false`) is now accepted too, when that mask is the guard. This is sound on the same terms, but neither the comment nor a fixture describes it. - **NIT** The SALU test is a name denylist (`:429`). `s_call_b64`, `s_setreg_b32`, `s_subvector_loop_*` and `s_rfe_b64` pass it, and they stay safe only because the frontend refuses them later (`:1274`). An allowlist of the SALU forms the frontend lowers would keep the gap rule tight as SOPK support grows (the `s_movk_i32` claim on #2413). Tests: the four negative controls prove that a VCC write, a branch, a saved-mask overwrite or an EXEC read in the gap keeps the refusal. CI did not run them: `native_direct_mesh` is not in the `pr` preset filter. The author ran it locally (192/192).
mumba17 commented 2026-11-30 19:50:18 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by mumba17 on 2026-11-30T19:50:18Z.


Thanks for the review. All three points are in dbf725ef, on top of a merge of main a0e126c2:

  • SHOULD: scalar_gap now includes s_buffer_load_dword s30, s[24:27], 0 and its wait, so the positive case goes through the SMEM op 8–12 path.
  • NIT (allowlist): the SALU test is now an allowlist. It accepts the 32-bit SOP1/SOP2/SOPK forms the frontend lowers, plus s_cmp* / s_bitcmp* for SOPC. s_call_b64, s_setreg_b32, s_subvector_loop_*, s_rfe_b64 and every 64-bit form now end the gap. New control gap-salu-b64 (s_mov_b64 s[30:31], 0 in the gap) keeps the restore refused.
  • NIT (restore at the join): added a comment at diamond_joined=true. A restore at the join of a diamond whose own restore names another mask is accepted only when the mask it names is the open skip's guard.

Mutation checks, each caught by native_direct_mesh:

  • allowing s_mov_b64;
  • limiting SMEM to ops 0–4;
  • clearing diamond_joined unconditionally.

ctest -R "mesh|compile_dxil" -j 4: 202 passed (the two mesh_two_exits skips, as on main). Census on the 454 cached requests is unchanged: 8 mesh EXEC write inside a lane-skip block left.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7290011055 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3633#issuecomment-7290011055). Originally posted by [mumba17](https://github.com/mumba17) on 2026-11-30T19:50:18Z. --- Thanks for the review. All three points are in `dbf725ef`, on top of a merge of main `a0e126c2`: - **SHOULD**: `scalar_gap` now includes `s_buffer_load_dword s30, s[24:27], 0` and its wait, so the positive case goes through the SMEM op 8–12 path. - **NIT (allowlist)**: the SALU test is now an allowlist. It accepts the 32-bit SOP1/SOP2/SOPK forms the frontend lowers, plus `s_cmp*` / `s_bitcmp*` for SOPC. `s_call_b64`, `s_setreg_b32`, `s_subvector_loop_*`, `s_rfe_b64` and every 64-bit form now end the gap. New control `gap-salu-b64` (`s_mov_b64 s[30:31], 0` in the gap) keeps the restore refused. - **NIT (restore at the join)**: added a comment at `diamond_joined=true`. A restore at the join of a diamond whose own restore names another mask is accepted only when the mask it names is the open skip's guard. Mutation checks, each caught by `native_direct_mesh`: - allowing `s_mov_b64`; - limiting SMEM to ops 0–4; - clearing `diamond_joined` unconditionally. `ctest -R "mesh|compile_dxil" -j 4`: 202 passed (the two `mesh_two_exits` skips, as on main). Census on the 454 cached requests is unchanged: 8 `mesh EXEC write inside a lane-skip block` left.
mumba17 commented 2026-11-30 20:43:59 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by mumba17 on 2026-11-30T20:43:59Z.


This merged before my review follow-up landed. The follow-up is now #3646, on top of main.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7291389639 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3633#issuecomment-7291389639). Originally posted by [mumba17](https://github.com/mumba17) on 2026-11-30T20:43:59Z. --- This merged before my review follow-up landed. The follow-up is now #3646, on top of main.
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!3633
No description provided.