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

Merged
mumba17 merged 2 commits from upstream-pr/3645 into main 2026-12-01 01:17:52 +00:00
mumba17 commented 2026-11-30 20:37:33 +00:00 (Migrated from github.com)

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


Part of #2413 (claim: https://github.com/R0Y00/Xbox2Windows/issues/2413#issuecomment-7289341190).

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

  • ctest --preset release -R "mesh|compile_dxil" -j 4 on top of main d5970858: 204 passed (the two mesh_two_exits cases skip, as on main).
  • Census of the 47 requests with this branch:
    • 41 pass the SAVEEXEC and stop next at s_xor_b32 (#3635).
    • 6 are refused as then arm depends on EXEC (a nested CMPX/EXECZ in the then arm).
  • With #3635 stacked locally, the 41 then stop at graphics register undefined on an incoming path (v10–v13). Those are VGPRs written in both arms and read by the position export after the vertex-count exchange, under a different guard. That is the existing refusal class, already the most common one on main; it is not part of this PR.

🤖 Generated with Claude Code

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:5573326446 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3645). Originally posted by [mumba17](https://github.com/mumba17) on 2026-11-30T20:37:33Z. --- Part of #2413 (claim: https://github.com/R0Y00/Xbox2Windows/issues/2413#issuecomment-7289341190). 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 #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 - `ctest --preset release -R "mesh|compile_dxil" -j 4` on top of main `d5970858`: 204 passed (the two `mesh_two_exits` cases skip, as on main). - Census of the 47 requests with this branch: - 41 pass the SAVEEXEC and stop next at `s_xor_b32` (#3635). - 6 are refused as `then arm depends on EXEC` (a nested CMPX/EXECZ in the then arm). - With #3635 stacked locally, the 41 then stop at `graphics register undefined on an incoming path` (v10–v13). Those are VGPRs written in both arms and read by the position export after the vertex-count exchange, under a different guard. That is the existing refusal class, already the most common one on main; it is not part of this PR. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
lukaslw-lab commented 2026-11-30 20:57:34 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by lukaslw-lab on 2026-11-30T20:57:34Z.

Review: APPROVED


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6120422898 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3645#pullrequestreview-6120422898). Originally posted by [lukaslw-lab](https://github.com/lukaslw-lab) on 2026-11-30T20:57:34Z. **Review:** APPROVED --- 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`.
mumba17 commented 2026-11-30 21:29:05 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by mumba17 on 2026-11-30T21:29:05Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7292471677 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3645#issuecomment-7292471677). Originally posted by [mumba17](https://github.com/mumba17) on 2026-11-30T21:29:05Z. --- 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 commented 2026-12-01 01:10:18 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by lukaslw-lab on 2026-12-01T01:10:18Z.

Review: APPROVED


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6123391481 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3645#pullrequestreview-6123391481). Originally posted by [lukaslw-lab](https://github.com/lukaslw-lab) on 2026-12-01T01:10:18Z. **Review:** APPROVED --- 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 commented 2026-12-01 01:16:18 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-12-01T01:16:18Z.

Review: APPROVED


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6123448622 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3645#pullrequestreview-6123448622). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-12-01T01:16:18Z. **Review:** APPROVED --- 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.
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!3645
No description provided.