Skip to content

shader: lower a mesh CMPX region inside a SAVEEXEC then arm (Part of #3703) - #3782

Merged
R0Y00 merged 2 commits into
R0Y00:mainfrom
mumba17:shader/mesh-saveexec-cmpx-child
Dec 2, 2026
Merged

R0Y00 merged 2 commits into
R0Y00:mainfrom
mumba17:shader/mesh-saveexec-cmpx-child

Conversation

@mumba17

@mumba17 mumba17 commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Part of #3703. Covers 158 of the 215 programs the wall mesh SAVEEXEC then arm depends on EXEC stops in my census on the current stack.

Shape

Inside a SAVEEXEC then arm, the compiler often narrows EXEC once more with a CMPX:

s_mov_b64 sX, exec        ; copy the arm's EXEC
...                       ; vector work under the arm's EXEC (optional)
v_cmpx_* a, b             ; EXEC &= compare
s_cbranch_execz L
...                       ; region
L: s_mov_b64 exec, sX     ; restore the copy

Each lane runs the work, then the region only if the compare holds. That is the if-only region s_and_saveexec_b64 sX, cmp would open, except that a CMPX leaves SCC alone. The then-arm scan refused the EXEC copy.

Chain form (second commit): in an if-only SAVEEXEC arm the EXECZ may instead skip to the arm's end, leaving the copy unrestored. The rest of the arm is then the region, and the SAVEEXEC restore ends both. Regions chain the same way.

Change (gfx1013_direct_mesh.inl)

  • match_cmpx_child recognizes the shape inside a then arm. The work between the copy and the CMPX must leave EXEC, the copied register and control flow alone. The EXECZ must land on s_mov_b64 exec, sX with the same register, inside the arm.
  • The then-arm scan registers it as a child region. The work before the CMPX is scanned as part of the enclosing arm, and the region as its own arm under the existing then-arm rules. Its parent gets contains_child, so its lane masks are exact, as for a nested s_and_saveexec_b64.
  • At the CMPX, the frontend opens a nested diamond on the shared comparison(), so every 32-bit VOPC compare is supported. The region's lane mask is the arm's lane mask AND the compare. It joins and restores through the existing nested SAVEEXEC path. SCC is clobbered after the join, as it is for a nested SAVEEXEC.
  • Chain form:
    • It is accepted only when the enclosing arm has no else arm; otherwise the inverse would read the narrowed EXEC.
    • After the region joins, the frontend visits the same address again so the enclosing arm closes there too.
    • A region whose enclosing arm does not end there is refused.
  • Anything else stays refused as before:
    • a restore of another register or outside the arm;
    • an EXECZ that leaves the region;
    • a prefix that writes the copy or branches;
    • a non-32-bit, SDWA or DPP CMPX, which gets its own refusal.

Tests (tests/native_direct_mesh_check.py)

  • saveexec-cmpx-region and -prefix check the whole chain:
    • the DXIL branch takes the icmp ugt i32 3;
    • the region's write selects on the arm's lanes AND the compare;
    • the region's join merges it with the value from before the CMPX (2, or the prefix's 4);
    • that join leaves the then arm.
  • Seven refusals stay at the copy (then arm depends on EXEC): another restore register, no EXECZ, a prefix that writes the copy, a prefix branch, a VOPC without EXEC, and an EXECZ past the restore.
  • Inside the region: an EXEC read and an LDS write are refused under the then-arm rules. A v_cmpx_gt_u16 is refused as unsupported.
  • Chain form:
    • saveexec-cmpx-chain checks the branch on the compare, the region's select and join, and that the join leaves the arm.
    • -twice checks a second copy and v_cmpx_ne_u32 inside the region, with both joins in order at the arm's end.
    • -else keeps the chain refused under an arm with an else arm.
  • Mutations, each failing the suite:
    • the branch ignores the compare;
    • any restore register is accepted;
    • prefix writes to the copy are not checked (the restore check still refuses, with another message);
    • the 32-bit check is dropped;
    • a chain is accepted under an else arm;
    • the second visit of the join address is dropped.

Validation (Windows 11, MSVC, Windows SDK 10.0.26100)

AI-assisted.

🤖 Generated with Claude Code

mumba17 and others added 2 commits December 1, 2026 22:04
Inside a SAVEEXEC then arm the compiler often copies EXEC, runs some
vector work, narrows EXEC with one CMPX, skips with EXECZ and restores the
copy. Each lane runs the work and then the skipped instructions only if
the compare holds: the same if-only region s_and_saveexec_b64 would open
on the compare. The then-arm scan now registers it as a child region and
the mesh frontend lowers it as a nested diamond on the shared compare,
through the existing nested SAVEEXEC join and restore. The region needs
its own restore of the copied register inside the arm; anything else
stays an EXEC read of the then arm.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The compiler also narrows the rest of an if-only SAVEEXEC then arm with a
CMPX whose EXECZ skips to the arm's end, leaving the EXEC copy unrestored.
The rest of the arm is then the region, and the SAVEEXEC restore ends
both. Accept that form when the enclosing arm has no else arm (its
inverse would read the narrowed EXEC), and visit the join address again
after the region closes so the enclosing arm ends there too. Regions
chain the same way.

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

mumba17 commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Pushed d1c76a61: the chain form. In an if-only SAVEEXEC arm the EXECZ after the CMPX may skip to the arm's end, so the rest of the arm is the region and the SAVEEXEC restore ends both.

It is accepted only without an else arm, since the inverse would otherwise read the narrowed EXEC. The description has the tests and mutations.

Census on the stack: 975 of 1,569 (was 924 at 7e997a37), none lost. Only the 57 s_cbranch_vccnz uniform branches still stop at this wall. ctest -R "native_direct_mesh|mesh": 210/210.

AI-assisted.

@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 d1c76a61 by reading the diff; not built or run.

Verdict: approve. The region reuses the nested SAVEEXEC diamond: the copy is lowered by the existing s_mov_b64 sX, exec path, the CMPX checks that the copy is still the arm's EXEC (saved_exec.at(start), same lane), and the branch and lane mask (arm AND compare) match the nested s_and_saveexec_b64 case. No MUST.

  • SHOULD No case reads a scalar written inside the region after its join. SALU in the region runs once for the whole wave whenever any lane passes the compare. In DXIL it runs only for the lanes that pass. So the then_writes loop and the SCC entry in scalar_clobbers (gfx1013_direct_mesh.inl:1816, :1818) are what keep this correct. Dropping either one passes every new case, because the regions write only v1. Two refusals would pin them:
    • s_mov_b32 s30, 1 in the region, then a read of s30 after the restore;
    • an s_cmp in the region, then s_cselect_b32 after the restore.
  • NIT #3797 edits the same then-arm scan and conflicts textually with this head. Whichever lands second needs a rebase. The two shapes don't overlap: an EXEC copy plus CMPX here, a VCC branch there.
  • Checked, no finding:
    • The chain form needs an arm with no else arm (!shape.else_begin), so the inverse never reads the narrowed EXEC. The second visit of the join address is backed by the outlives its enclosing arm refusal.
    • Prefix and region writes to the copy are refused, in the prefix by writes_saved and in the region at the arm close. A nested SAVEEXEC in the prefix fails the match and stays at depends on EXEC.
    • The copy registers join the parent's then_writes, so after the outer join a chain copy that was never restored reads as unbound, not as a stale lane mask.
    • It merges cleanly with main, #3707, #3712 and #3770.

Tests: the 14 new native_direct_mesh cases check the IR order. The branch takes the compare, the region's select uses arm AND compare, and both joins come in order for the region and the chain forms. The refusals cover each match condition. These are IR checks only; no draw runs a region. CI ran native_direct_mesh in the pr preset at this head, green.

@R0Y00
R0Y00 merged commit 9d44529 into R0Y00:main Dec 2, 2026
5 checks passed
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