shader: lower uniform VCC branches and shared-target nested SAVEEXEC in mesh SAVEEXEC then arms (Part of #3703) #3797

Merged
timbot103 merged 1 commit from upstream-pr/3797 into main 2026-12-02 03:04:00 +00:00
timbot103 commented 2026-12-01 23:03:58 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by timbot103 on 2026-12-01T23:03:58Z.


Part of #3703.

Lowers the unclaimed SAVEEXEC then-arm patterns opened on #3703 in src/gfx1013_direct_mesh.inl:

Summary

  1. Uniform VCC test + branch inside a SAVEEXEC then arm:
    • Recognizes paired s_and_b64 vcc, exec, sN / s_and_b64 vcc, sN, exec (and wave32 s_and_b32 vcc_lo, exec_lo, sN) immediately followed by a forward s_cbranch_vccnz or s_cbranch_vccz targeting within the then arm or its exit.
    • Strictly requires a proven wave-uniform mask (Symbol::Type::uniform_mask or mask_uniform covering lane_active) so per-lane branching agrees with hardware's wave-wide vcc != 0 test; lane-varying v_cmp_* masks remain refused.
    • Rejects any branch entering the middle of a (s_and, s_cbranch_vcc*) pair, records the jump in scalar_branch so VCC branches over nested SAVEEXEC regions remain refused (mesh scalar branch crosses a nested SAVEEXEC region), updates SCC to match (exec & sN) != 0, clears vcc (106, 107), vcc_words, and vcc_nonempty (both at the test and in scalar_clobbers at the join) so stale whole-wave VCC state can never leak across the SAVEEXEC, and merges register state through the existing PHI machinery.
    • Restricts masked_vcc strictly to active SAVEEXEC diamonds (saveexec_shapes.count(branch->start)).
  2. Shared-target nested if-only SAVEEXEC:
    • Allows an inner s_and_saveexec_b64 + s_cbranch_execz inside a then arm to target the enclosing arm's exit only when the enclosing arm restores its saved EXEC directly (s_mov_b64 exec, sOuter).
    • Enclosing else inverses (s_andn2_b64 / s_xor_b64) and non-direct parent restores remain refused with explicit diagnostics (nested mesh SAVEEXEC cannot share exit with an enclosing inverse / nested mesh SAVEEXEC cannot share exit without a direct parent restore) because skipping the inner restore would route inactive inner lanes into the outer else arm.
  3. Fail-closed scope & interaction with #3782:
    • Backward branches/loops, non-immediate VCC tests, out-of-arm jumps, VCC branches crossing nested SAVEEXEC regions, saved-mask clobbers, and arbitrary EXEC writes remain strictly refused.
    • Note: both this PR and #3782 edit the then-arm scan and add a second visit of the join address (shared_exit here vs chain_closed in #3782), so whichever lands second will merge those two checks cleanly.

Verification (source-built fixtures only)

Environment: Windows 11 (26200.9974), MSVC 19.44, Ninja, Windows SDK 10.0.26100.0 DXC, rebased onto origin/main (b7d61632, C++20 / /W4).

  • Debug (dev): ctest --preset dev --no-tests=error -R 'native_direct_mesh|native_direct_container' -V — 2/2 passed (native_direct_container 14.20 s, native_direct_mesh passed; zero new /W4 warnings).
  • Release (release): ctest --preset release --no-tests=error -R 'native_direct_mesh|native_direct_container' -V — 2/2 passed (native_direct_container 11.55 s, native_direct_mesh 11.72 s; total 23.48 s).
  • git diff --check origin/main..HEAD clean.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:5596251153 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3797). Originally posted by [timbot103](https://github.com/timbot103) on 2026-12-01T23:03:58Z. --- Part of #3703. Lowers the unclaimed SAVEEXEC `then`-arm patterns opened on #3703 in `src/gfx1013_direct_mesh.inl`: ### Summary 1. **Uniform VCC test + branch inside a SAVEEXEC `then` arm:** - Recognizes paired `s_and_b64 vcc, exec, sN` / `s_and_b64 vcc, sN, exec` (and wave32 `s_and_b32 vcc_lo, exec_lo, sN`) immediately followed by a forward `s_cbranch_vccnz` or `s_cbranch_vccz` targeting within the `then` arm or its exit. - Strictly requires a **proven wave-uniform mask** (`Symbol::Type::uniform_mask` or `mask_uniform` covering `lane_active`) so per-lane branching agrees with hardware's wave-wide `vcc != 0` test; lane-varying `v_cmp_*` masks remain refused. - Rejects any branch entering the middle of a `(s_and, s_cbranch_vcc*)` pair, records the jump in `scalar_branch` so VCC branches over nested `SAVEEXEC` regions remain refused (`mesh scalar branch crosses a nested SAVEEXEC region`), updates `SCC` to match `(exec & sN) != 0`, clears `vcc` (`106`, `107`), `vcc_words`, and `vcc_nonempty` (both at the test and in `scalar_clobbers` at the join) so stale whole-wave VCC state can never leak across the `SAVEEXEC`, and merges register state through the existing PHI machinery. - Restricts `masked_vcc` strictly to active `SAVEEXEC` diamonds (`saveexec_shapes.count(branch->start)`). 2. **Shared-target nested if-only SAVEEXEC:** - Allows an inner `s_and_saveexec_b64` + `s_cbranch_execz` inside a `then` arm to target the enclosing arm's exit **only** when the enclosing arm restores its saved `EXEC` directly (`s_mov_b64 exec, sOuter`). - Enclosing `else` inverses (`s_andn2_b64` / `s_xor_b64`) and non-direct parent restores remain refused with explicit diagnostics (`nested mesh SAVEEXEC cannot share exit with an enclosing inverse` / `nested mesh SAVEEXEC cannot share exit without a direct parent restore`) because skipping the inner restore would route inactive inner lanes into the outer `else` arm. 3. **Fail-closed scope & interaction with `#3782`:** - Backward branches/loops, non-immediate VCC tests, out-of-arm jumps, VCC branches crossing nested `SAVEEXEC` regions, saved-mask clobbers, and arbitrary `EXEC` writes remain strictly refused. - Note: both this PR and `#3782` edit the `then`-arm scan and add a second visit of the join address (`shared_exit` here vs `chain_closed` in `#3782`), so whichever lands second will merge those two checks cleanly. ### Verification (source-built fixtures only) Environment: Windows 11 (`26200.9974`), MSVC `19.44`, Ninja, Windows SDK `10.0.26100.0` DXC, rebased onto `origin/main` (`b7d61632`, C++20 / `/W4`). - **Debug (`dev`):** `ctest --preset dev --no-tests=error -R 'native_direct_mesh|native_direct_container' -V` — **2/2 passed** (`native_direct_container` 14.20 s, `native_direct_mesh` passed; zero new `/W4` warnings). - **Release (`release`):** `ctest --preset release --no-tests=error -R 'native_direct_mesh|native_direct_container' -V` — **2/2 passed** (`native_direct_container` 11.55 s, `native_direct_mesh` 11.72 s; total 23.48 s). - `git diff --check origin/main..HEAD` clean. AI-assisted.
lukaslw-lab commented 2026-12-02 02:29:08 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by lukaslw-lab on 2026-12-02T02:29:08Z.

Review: COMMENTED


Reviewed 5655b237 by reading the diff; not built or run.

Verdict: comment. The uniform VCC branch is sound: inside a then arm EXEC is nonempty, so on a proven uniform mask (exec & sN) != 0 equals the lane's own bit, and the shared-exit child is limited to a parent that restores with s_mov_b64 at its alternate. No MUST, but the VCC path skips one of the arm's existing guards.

  • SHOULD A VCC test never sets scalar_branch. The scan continues past the s_cbranch_vcc* (gfx1013_direct_mesh.inl:583-586) before the op.branch() bookkeeping at :609-612, so mesh scalar branch crosses a nested SAVEEXEC region (:622) no longer applies to it. An arm with a VCC branch over a whole nested s_and_saveexec_b64, a CMPX region or a shared-exit child now passes the scan. The SAVEEXEC lowering's forward.upper_bound check only refuses an edge that lands inside the child, not one that jumps over it. No case covers that combination. Either record the jump in scalar_branch, or add a case where a VCC branch jumps over a nested SAVEEXEC and check its joins.
  • SHOULD masked_vcc also lifts the allocated and lane_else refusals (:1410). No case has a VCC branch after the GS_ALLOC_REQ send or in an arm with a lane-split else. A positive case for each, or keeping those two refused, would pin it.
  • NIT The PR body lost its backticks, and \t/\r escapes ate letters: "then" shows as a tab plus "hen", "release" as "elease". It also says it doesn't overlap #3782, which edits the same then-arm scan and conflicts textually with this head. Both add a second visit of the join address, here shared_exit and there chain_closed, so whichever lands second can merge the two.
  • NIT saveexec-nested-shared-inverse is refused as then arm lacks an EXECZ skip. A message naming the inverse would say why.
  • Checked, no finding:
    • joins_skip_end changed from !shape.reset to shape.join==exec_skip->end. For existing shapes this is the same value: a reset sits before its boundary, and a reset-less top-level join is the skip end.
    • The new VCC word clobbers at the join close a stale vcc_words that saveexec-vcc-stale-branch-after-join pins.
    • A branch into the test pair, its own s_cbranch reached first, and a lane-varying mask are each refused.

Tests: checked_vcc_skip (vccnz, vccz, reverse operands, scalar compare, wave32), the local diamond and the then-exit case check the edge phis and the final select. saveexec-nested-shared-exit reuses checked_nested, and the refusals cover each match condition. CI ran native_direct_mesh in the pr preset at this head, green.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6143793216 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3797#pullrequestreview-6143793216). Originally posted by [lukaslw-lab](https://github.com/lukaslw-lab) on 2026-12-02T02:29:08Z. **Review:** COMMENTED --- Reviewed `5655b237` by reading the diff; not built or run. **Verdict: comment.** The uniform VCC branch is sound: inside a then arm EXEC is nonempty, so on a proven uniform mask `(exec & sN) != 0` equals the lane's own bit, and the shared-exit child is limited to a parent that restores with `s_mov_b64` at its alternate. No MUST, but the VCC path skips one of the arm's existing guards. - **SHOULD** A VCC test never sets `scalar_branch`. The scan `continue`s past the `s_cbranch_vcc*` (`gfx1013_direct_mesh.inl:583-586`) before the `op.branch()` bookkeeping at `:609-612`, so `mesh scalar branch crosses a nested SAVEEXEC region` (`:622`) no longer applies to it. An arm with a VCC branch over a whole nested `s_and_saveexec_b64`, a CMPX region or a shared-exit child now passes the scan. The SAVEEXEC lowering's `forward.upper_bound` check only refuses an edge that lands inside the child, not one that jumps over it. No case covers that combination. Either record the jump in `scalar_branch`, or add a case where a VCC branch jumps over a nested SAVEEXEC and check its joins. - **SHOULD** `masked_vcc` also lifts the `allocated` and `lane_else` refusals (`:1410`). No case has a VCC branch after the GS_ALLOC_REQ send or in an arm with a lane-split else. A positive case for each, or keeping those two refused, would pin it. - **NIT** The PR body lost its backticks, and `\t`/`\r` escapes ate letters: "then" shows as a tab plus "hen", "release" as "elease". It also says it doesn't overlap #3782, which edits the same then-arm scan and conflicts textually with this head. Both add a second visit of the join address, here `shared_exit` and there `chain_closed`, so whichever lands second can merge the two. - **NIT** `saveexec-nested-shared-inverse` is refused as `then arm lacks an EXECZ skip`. A message naming the inverse would say why. - Checked, no finding: - `joins_skip_end` changed from `!shape.reset` to `shape.join==exec_skip->end`. For existing shapes this is the same value: a reset sits before its boundary, and a reset-less top-level join is the skip end. - The new VCC word clobbers at the join close a stale `vcc_words` that `saveexec-vcc-stale-branch-after-join` pins. - A branch into the test pair, its own `s_cbranch` reached first, and a lane-varying mask are each refused. Tests: `checked_vcc_skip` (vccnz, vccz, reverse operands, scalar compare, wave32), the local diamond and the then-exit case check the edge phis and the final select. `saveexec-nested-shared-exit` reuses `checked_nested`, and the refusals cover each match condition. CI ran `native_direct_mesh` in the `pr` preset at this head, green.
R0Y00 commented 2026-12-02 02:41:30 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-12-02T02:41:30Z.


#3782 is now on main. Please rebase the shared SAVEEXEC scan and join handling on it and cover the reviewer's VCC-jumps-over-nested-SAVEEXEC case on the rebased head, or keep that combination refused.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7333097918 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3797#issuecomment-7333097918). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-12-02T02:41:30Z. --- #3782 is now on main. Please rebase the shared SAVEEXEC scan and join handling on it and cover the reviewer's VCC-jumps-over-nested-SAVEEXEC case on the rebased head, or keep that combination refused.
timbot103 commented 2026-12-02 02:47:44 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by timbot103 on 2026-12-02T02:47:44Z.


@lukaslw-lab Thanks for the thorough review! Rebased onto origin/main (b7d61632 -> 7585f6b2) and addressed every item:

  • SHOULD 1 (scalar_branch for VCC tests): Recorded jump.pc in scalar_branch inside if (vcc_test) in validate_saveexec so VCC branches over a nested s_and_saveexec_b64 (including shared-exit children) remain refused with mesh scalar branch crosses a nested SAVEEXEC region. Added saveexec-vcc-crosses-nested and saveexec-vcc-crosses-shared-nested negative controls.
  • SHOULD 2 (allocated and branch->lane_else): Tightened masked_vcc to explicitly require saveexec_shapes.count(branch->start) (so it can never apply to a non-SAVEEXEC v_cmpx/gather lane_else diamond). Note that saveexec_skip already places its block after GS_ALLOC_REQ (program[:3], so allocated == true), and saveexec-vccnz-then-exit already runs inside saveexec_open(..., [0x7e020284]) (branch->lane_else != 0). Added two extra positive cases to pin the remaining matrix explicitly: saveexec-vccnz-paired-else (interior VCC skip inside a paired-EXECZ if/else SAVEEXEC after GS_ALLOC_REQ, verifying both the interior scalar PHI and outer then/else PHI) and saveexec-vccnz-pre-alloc (same skip before GS_ALLOC_REQ, allocated == false).
  • NIT 1 (PR body): Restored backticks, fixed the \t/\r mangling, and updated the note on #3782 interaction.
  • NIT 2 (shared-exit refusal message): Split the nested shared-exit refusal in match_saveexec into nested mesh SAVEEXEC cannot share exit with an enclosing inverse and nested mesh SAVEEXEC cannot share exit without a direct parent restore, updating saveexec-nested-shared-inverse and saveexec-nested-escaping-then and adding saveexec-nested-shared-gap plus saveexec-nested-escaping-past-inverse.

Re-verified on Windows 11 (26200.9974), MSVC 19.44, Windows SDK 10.0.26100.0:

  • Debug (dev): native_direct_container + native_direct_mesh — 2/2 passed.
  • Release (release): native_direct_container + native_direct_mesh — 2/2 passed (23.48 s).
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7333205247 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3797#issuecomment-7333205247). Originally posted by [timbot103](https://github.com/timbot103) on 2026-12-02T02:47:44Z. --- @lukaslw-lab Thanks for the thorough review! Rebased onto `origin/main` (`b7d61632` -> `7585f6b2`) and addressed every item: - **SHOULD 1 (`scalar_branch` for VCC tests):** Recorded `jump.pc` in `scalar_branch` inside `if (vcc_test)` in `validate_saveexec` so VCC branches over a nested `s_and_saveexec_b64` (including shared-exit children) remain refused with `mesh scalar branch crosses a nested SAVEEXEC region`. Added `saveexec-vcc-crosses-nested` and `saveexec-vcc-crosses-shared-nested` negative controls. - **SHOULD 2 (`allocated` and `branch->lane_else`):** Tightened `masked_vcc` to explicitly require `saveexec_shapes.count(branch->start)` (so it can never apply to a non-SAVEEXEC `v_cmpx`/gather `lane_else` diamond). Note that `saveexec_skip` already places its block after `GS_ALLOC_REQ` (`program[:3]`, so `allocated == true`), and `saveexec-vccnz-then-exit` already runs inside `saveexec_open(..., [0x7e020284])` (`branch->lane_else != 0`). Added two extra positive cases to pin the remaining matrix explicitly: `saveexec-vccnz-paired-else` (interior VCC skip inside a paired-EXECZ `if/else` SAVEEXEC after `GS_ALLOC_REQ`, verifying both the interior scalar PHI and outer `then`/`else` PHI) and `saveexec-vccnz-pre-alloc` (same skip before `GS_ALLOC_REQ`, `allocated == false`). - **NIT 1 (PR body):** Restored backticks, fixed the `\t`/`\r` mangling, and updated the note on `#3782` interaction. - **NIT 2 (shared-exit refusal message):** Split the nested shared-exit refusal in `match_saveexec` into `nested mesh SAVEEXEC cannot share exit with an enclosing inverse` and `nested mesh SAVEEXEC cannot share exit without a direct parent restore`, updating `saveexec-nested-shared-inverse` and `saveexec-nested-escaping-then` and adding `saveexec-nested-shared-gap` plus `saveexec-nested-escaping-past-inverse`. Re-verified on Windows 11 (`26200.9974`), MSVC `19.44`, Windows SDK `10.0.26100.0`: - **Debug (`dev`):** `native_direct_container` + `native_direct_mesh` — **2/2 passed**. - **Release (`release`):** `native_direct_container` + `native_direct_mesh` — **2/2 passed** (23.48 s).
lukaslw-lab commented 2026-12-02 02:58:52 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by lukaslw-lab on 2026-12-02T02:58:52Z.

Review: APPROVED


Re-reviewed a358540e (rebased on main 7069dfea, with #3782) by reading the diff; not built or run.

Verdict: approve. Both SHOULDs are closed, and the rebase merges the two join paths cleanly. No MUST, no open points.

  • The VCC test now sets scalar_branch (gfx1013_direct_mesh.inl:659), so crosses a nested SAVEEXEC region covers it again. saveexec-vcc-crosses-nested and -crosses-shared-nested pin both child forms.
  • I was partly wrong on the second SHOULD. saveexec_skip already runs after GS_ALLOC_REQ, and lane_else is the paired arm's else_begin, so saveexec-vccnz-then-exit already covered both. -paired-else and -pre-alloc now name them explicitly. The new saveexec_shapes.count(branch->start) term also keeps masked_vcc off non-SAVEEXEC diamonds.
  • Checked, no finding:
    • shared_exit (:967) replaces #3782's chain_closed. It is !reset && !else_begin, which only the two shared-exit forms produce: a CMPX chain, and a nested SAVEEXEC whose EXECZ lands on the parent restore. Paired, if-only and top-level skip-end shapes all carry a reset or an else arm, so they do not revisit the join. The refusal message still names the form.
    • The split shared-exit refusals (:500-503) are reached only when the target is the boundary. Top-level skips at the boundary keep the old lacks an EXECZ skip.
    • The PR body is fixed.

Tests: the new crossing, gap, escaping-past-inverse, paired-else and pre-alloc cases each reach the changed lines. CI ran native_direct_mesh in the pr preset. At the time of this review the Windows job was still running at this head, and the gfx1013 job was green.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6144029463 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3797#pullrequestreview-6144029463). Originally posted by [lukaslw-lab](https://github.com/lukaslw-lab) on 2026-12-02T02:58:52Z. **Review:** APPROVED --- Re-reviewed `a358540e` (rebased on main `7069dfea`, with #3782) by reading the diff; not built or run. **Verdict: approve.** Both SHOULDs are closed, and the rebase merges the two join paths cleanly. No MUST, no open points. - The VCC test now sets `scalar_branch` (`gfx1013_direct_mesh.inl:659`), so `crosses a nested SAVEEXEC region` covers it again. `saveexec-vcc-crosses-nested` and `-crosses-shared-nested` pin both child forms. - I was partly wrong on the second SHOULD. `saveexec_skip` already runs after GS_ALLOC_REQ, and `lane_else` is the paired arm's `else_begin`, so `saveexec-vccnz-then-exit` already covered both. `-paired-else` and `-pre-alloc` now name them explicitly. The new `saveexec_shapes.count(branch->start)` term also keeps `masked_vcc` off non-SAVEEXEC diamonds. - Checked, no finding: - `shared_exit` (`:967`) replaces #3782's `chain_closed`. It is `!reset && !else_begin`, which only the two shared-exit forms produce: a CMPX chain, and a nested SAVEEXEC whose EXECZ lands on the parent restore. Paired, if-only and top-level skip-end shapes all carry a reset or an else arm, so they do not revisit the join. The refusal message still names the form. - The split shared-exit refusals (`:500-503`) are reached only when the target is the boundary. Top-level skips at the boundary keep the old `lacks an EXECZ skip`. - The PR body is fixed. Tests: the new crossing, gap, escaping-past-inverse, paired-else and pre-alloc cases each reach the changed lines. CI ran `native_direct_mesh` in the `pr` preset. At the time of this review the Windows job was still running at this head, and the gfx1013 job was green.
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!3797
No description provided.