Skip to content

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

Merged
R0Y00 merged 1 commit into
R0Y00:mainfrom
timbot103:shader/mesh-saveexec-uniform-vcc
Dec 2, 2026
Merged

R0Y00 merged 1 commit into
R0Y00:mainfrom
timbot103:shader/mesh-saveexec-uniform-vcc

Conversation

@timbot103

@timbot103 timbot103 commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

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

R0Y00 commented Dec 2, 2026

Copy link
Copy Markdown
Owner

#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
timbot103 force-pushed the shader/mesh-saveexec-uniform-vcc branch from 5655b23 to 7585f6b Compare December 2, 2026 02:47
@timbot103

Copy link
Copy Markdown
Contributor Author

@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).

@timbot103
timbot103 force-pushed the shader/mesh-saveexec-uniform-vcc branch from 7585f6b to a358540 Compare December 2, 2026 02:54

@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.

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.

@R0Y00
R0Y00 merged commit 572f0d6 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