Skip to content

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

Merged
R0Y00 merged 2 commits into
R0Y00:mainfrom
mumba17:shader/mesh-diamond-restore-gap
Nov 30, 2026
Merged

R0Y00 merged 2 commits into
R0Y00:mainfrom
mumba17:shader/mesh-diamond-restore-gap

Conversation

@mumba17

@mumba17 mumba17 commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

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

mumba17 and others added 2 commits November 30, 2026 19:01
…estore

A compiler may schedule EXEC-independent scalar instructions (SMEM loads,
SALU, s_waitcnt, s_clause) between the join of a signed-CMPX if/else and
the s_mov_b64 exec restore that closes it. The frontend accepted the
restore only at the join and refused the rest as an EXEC write inside a
lane-skip block. Those instructions run once per wave whatever EXEC holds,
so the restore still closes the diamond. Each must leave EXEC, VCC and
control flow alone, no other path may arrive in the gap, and the restore
must still name the open skip's guard.

Tests to follow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The positive case places a table load, its wait and a scalar add between
the join of a signed-CMPX diamond and the restore. Controls put a VCC
write, a branch, a saved-mask overwrite or an EXEC read in the same gap,
and each keeps the restore refused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@lukaslw-lab lukaslw-lab left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 35a3f6d + #3619 (inert without heap_tokens; the rest of the mesh frontend is unchanged up to bee9c64), 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).

@R0Y00
R0Y00 merged commit b522376 into R0Y00:main Nov 30, 2026
5 checks passed
@mumba17

mumba17 commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

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

mumba17 commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

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

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.

3 participants