Skip to content

shader: lower s_and_saveexec_b64 if/else diamonds inside a mesh lane skip (Part of #2413) - #3645

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

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

Conversation

@mumba17

@mumba17 mumba17 commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

Part of #2413 (claim: #2413 (comment)).

The s_and_saveexec_b64 handler from #3638 covers the instruction outside a lane skip. Inside one, the C++ mesh frontend still refused it as unsupported mesh instruction or EXEC state: s_and_saveexec_b64. On main that is the first refusal for 47 of the 454 cached slot 57 requests in my local census.

All 47 are a compiler if/else: s_and_saveexec_b64 sD, sS, s_cbranch_execz over the then arm, then s_andn2_b64 exec, sD, exec for the else arm. The else arm takes one of two shapes:

  • Paired EXECZ (5 requests): the else arm has its own s_cbranch_execz, and its target restores s_mov_b64 exec, sD. This is the same join the existing s_and_b64 gather diamond uses.
  • Skip-end join (42 requests): the else arm only writes VGPRs and has no EXECZ. It runs to the end of the enclosing lane skip, where s_mov_b64 exec, -1 restores full EXEC.

Change (src/gfx1013_direct_mesh.inl)

  • A new s_and_saveexec_b64 case for an open lane skip. It lowers the if/else as a per-lane branch on sS with the existing Diamond / lane_else machinery.

    • sD gets the parent EXEC, recorded as a saved mask, so the inverse and the paired restore resolve to it.
    • SCC becomes unknown.
    • Scalars written by the then arm are clobbered at the join.
  • For the skip-end shape, the diamond is merged before the skip closes. The skip-close block is now a lambda, and a Diamond::joins_skip_end flag reorders the two. That flag is only set here, so the order for every existing diamond is unchanged. Such a join also does not open the shader: accept scalar work between a mesh diamond join and its EXEC restore (Part of #3416) #3633 restore gap.

  • Fails closed with a named refusal:

    Refusal Cause
    mesh SAVEEXEC then arm depends on EXEC reads or writes EXEC (including CMPX and EXECZ), or uses DPP
    mesh SAVEEXEC then arm has a wave or memory side effect export, LDS, barrier, s_sendmsg, s_endpgm, PC ops, buffer/image store or atomic
    mesh SAVEEXEC then arm branches out of the arm branch target outside the then arm
    mesh SAVEEXEC then arm overwrites the saved EXEC then arm writes sD
    mesh SAVEEXEC else arm is not vector-only else arm writes a scalar, reads VCC implicitly, or moves data across lanes
    mesh SAVEEXEC else arm reads a scalar the then arm writes the wave runs both arms in order
    missing inverse, wrong restore, or a skip-end arm that is not followed by full EXEC shape check before lowering

Tests (tests/native_direct_mesh_check.py)

  • saveexec-skip-end and saveexec-paired: v1 is 2 in the then arm and 3 in the else arm. The test asserts the join phi [2, then] [3, else] and that the mask branch goes to the then arm.
  • saveexec-then-scalar: an SGPR written and used only in the then arm is accepted.
  • 10 negative controls, each checked against its exact offset and reason:
    • then arm: EXEC read, CMPX, LDS write, saved-mask overwrite;
    • else arm: reads a then-arm SGPR, scalar write, v_cndmask;
    • shape: missing inverse, skip end without a full restore, paired restore from the wrong mask.

Mutation checks, each caught by native_direct_mesh:

  • closing the skip before the join;
  • swapping the then/else branch targets;
  • dropping the then-arm EXEC-read check;
  • dropping the else-arm scalar-read check;
  • not checking for the full restore at the skip end.

I ran these on the branch before I merged main with #3633; the only change since then is where the skip close sits after the join.

Verification

🤖 Generated with Claude Code

…skip

Inside a lane-skip block, SAVEEXEC with an EXECZ-skipped then arm and an
s_andn2_b64 inverse becomes a per-lane if/else on the source mask. The else
arm either restores the saved mask after its own EXECZ, or writes only VGPRs
and joins at the skip end, where full EXEC is restored. That diamond joins
before the skip closes.

The then arm may not depend on EXEC, write the saved mask, export, use LDS,
barriers or s_sendmsg, store, or branch out of the arm. The else arm must be
vector-only and may not read a scalar the then arm writes. Source-built
fixtures cover both shapes and each refusal.

Part of R0Y00#2413.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
mumba17 added a commit to mumba17/Xbox2Windows that referenced this pull request Nov 30, 2026
Keeps the diamond join's diamond_joined line untouched, so this merges cleanly with R0Y00#3645.

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

Verdict: approve. The shape match and the per-lane if/else lowering are right for the census shapes, and the change fails closed as described. No MUST. Two SHOULDs extend the then-arm filter the same way the else arm is already filtered. Both are cheap refusals that can follow.

  • SHOULD Then-arm VCC writes are not clobbered at the join (src/gfx1013_direct_mesh.inl, for (auto r : then_writes) if (r<106)). A VOPC e32, or a VOP3 compare into vcc, puts 106/107 into then_writes, but only r < 106 reaches lane_scalar_clobbers. On hardware the compare runs with the then-arm EXEC and writes 0 into the else lanes' bits whenever any lane took the then arm. If the per-lane model carries VCC across the join as a per-lane value, else lanes keep their pre-diamond bit. A per-lane VCC read after the join (v_cndmask, _co_ci) would then differ for else lanes. Either clobber 106/107 too, or refuse VCC destinations in the then arm.
  • SHOULD The then arm does not refuse cross-lane reads; the else arm does (name.find("lane")). v_readlane_b32 or v_permlane16/x16 in the then arm read lanes that are inactive there. On hardware that is defined: the register holds whatever the else lanes held. In a divergent per-lane branch a lane read of an inactive lane is undefined. v_readfirstlane happens to agree, because both sides take the first active then-lane. Refusing *lane* except readfirstlane in the then arm would keep the fail-closed promise.
  • Checked, no finding:
    • Shape: 0xbefe04c1 is s_mov_b64 exec, -1 at the skip end. The inverse must be s_andn2_b64 exec, sD, exec, the paired restore must come from sD, and both EXECZ targets must stay inside the skip.
    • Then arm: EXEC reads/writes, CMPX, nested EXECZ, DPP, export/LDS/barrier/sendmsg/stores and out-of-arm branches are refused. SGPR and SMEM writes are clobbered at the join, and sD overwrites are refused.
    • Else arm: VGPR-only, with no implicit VCC readers and no then-arm scalars read.
    • joins_skip_end is set only here. Merging the diamond before close_skip() leaves every other diamond's order unchanged, and diamond_joined = !join_at_skip_end avoids opening the #3633 gap.

Tests: two positive shapes check the join phi [2, then] [3, else] and the branch direction, plus 10 refusal controls and five mutations. Neither SHOULD case has a control. native_direct_mesh is outside the Windows pr preset filter, so it has only your run on d5970858.

…n arm

Review follow-up. A then-arm VCC write runs under the then-arm EXEC and
zeroes the else lanes' bits, which a per-lane VCC cannot show after the
join. Refuse VOPC, VOP3 compares into VCC, VCC carry-outs and
v_div_scale; record SGPR carry-outs as then-arm writes. Refuse cross-lane
reads other than v_readfirstlane_b32, as the else arm already does.

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

mumba17 commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

Thanks. Both SHOULDs are in b0ee8b5b:

  • VCC. The then arm now refuses VOPC, VOP3 compares into vcc, vcc carry-outs (through carry_destination) and v_div_scale. A VOP3B carry into an ordinary SGPR goes into then_writes, so it is clobbered at the join and the else arm may not read it.
  • Cross-lane reads. The then arm refuses any *lane* name except v_readfirstlane_b32.

New controls, each at +0x34: saveexec-then-vopc, saveexec-then-vop3-vcc, saveexec-then-carry, saveexec-then-readlane. I removed each refusal in turn (VCC, carry/div_scale only, lane) and every mutation fails native_direct_mesh. Mesh and compile_dxil tests: 204/204.

The slot 57 census (454 requests) gives the same result line for every request before and after, so no real then arm hit either refusal.

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

Verdict: approve. b0ee8b5b fixes both points from our review at 29404d76, and the PR still merges cleanly with main a3bfd019. No MUST.

  • NIT No control for a VOP3B carry into an ordinary SGPR pair. if (carry) { then_writes.insert(...) } (the then-arm loop in src/gfx1013_direct_mesh.inl) is the only place that keeps the else arm from reading it, and no test fails if that line goes. A saveexec-else-reads-then variant with v_add_co_u32_e64 v1, s[30:31], ... in the then arm and v_mov_b32 v2, s30 in the else arm would cover it.
  • Checked, no finding:
    • VCC: the then arm refuses VOPC, scalar destinations 106/107, carry_destination ≥ 106 and v_div_scale*. carry_destination does not list v_mad_i64_i32, but the mesh frontend does not lower it, so it is refused anyway.
    • Cross-lane: *lane* except v_readfirstlane_b32. readfirstlane writes an SGPR, so it lands in then_writes and is clobbered at the join.
    • Merged with #3646: the scalar gap check runs only when diamond_joined is set. A join at the skip end sets it false and closes the skip right after, so the #3633 gap is not opened. A join at a forward target still resets it.
    • Merged with #3635 / #3655: s_movk_i32 and s_xor_b32 in the then arm go through the SOPK/SOP2 then_writes path. v_bfm_b32 is VOP3, which the else arm allows.

Tests: four new refusal controls at +0x34 reach the two new refusals (VOPC, VOP3 compare, VOP2 carry, readlane). Your mutation run shows each clause is needed. CI at this head ran before #3644 put native_direct_mesh into the pr filter, so these controls have not run on CI yet. A rerun or a merge of main would cover them.

@R0Y00 R0Y00 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Reviewed the paired-EXECZ and enclosing-skip-end shapes, including the saved-mask, cross-lane and side-effect guards. ctest --test-dir <build> --no-tests=error -j1 -R '^native_direct_mesh$' --output-on-failure passed. Two extra source-built v_mov_b32 cases reading exec_lo or exec_hi in the else arm were refused as unproven entry registers 126 and 127.

@R0Y00
R0Y00 merged commit e8179e2 into R0Y00:main Dec 1, 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