Skip to content

Shaders: s_cbranch_vccz/vccnz on a scalar VCC decide once for the wave (#2780, on #2836) - #3023

Open
wwwwwwww3333333 wants to merge 5 commits into
Marvisak:mainfrom
wwwwwwww3333333:fix/vcc-branch-whole
Open

wwwwwwww3333333 wants to merge 5 commits into
Marvisak:mainfrom
wwwwwwww3333333:fix/vcc-branch-whole

Conversation

@wwwwwwww3333333

Copy link
Copy Markdown
Contributor

Fixes the open point of #2780 that both #2783 and #2836 leave (noted in the triage there): s_cbranch_vccz/s_cbranch_vccnz test VCC as a whole (RDNA2 ISA, 12.5: VCCZ is VCC == 0), one decision for the wave, not each lane's bit.

Built on #2836: its two commits are kept as they are (Devin, @kotekompotek), rebased onto main 63bcca4. The third commit is this change.

What changes

  • A VCC that is a pair the structure pass cannot see into (Atom::Opaque, both dwords since Shaders: a lane mask's high dword is the pair's own, not a guess at the register next to the low one #2836) now gives a new Atom::Nonzero(lo, hi) at the branch, the same in every lane, lowered as (lo | hi) != 0. The branch is uniform (uniform_join and the translate counts treat it so).
  • A constant VCC other than all lanes or none is taken.
  • No wave operation and no forced wave size, so 32-lane hosts are not affected.
  • shortcut (marked in the code): a VCC from lane results (compares) still branches per lane. That is the same as the hardware while VCC is set only in lanes of EXEC and the skipped block does nothing in lanes whose bit is clear.

GPU test synthetic_tests::vcc_branches_test_the_whole_mask: a full wave64 group, VCC written by two s_mov_b32, v4 = 17 on the taken path and 43 otherwise. Lanes taking s_cbranch_vccz with only VCC_HI set (the hardware takes none):

tree lanes taking the branch
main 63bcca4 64
main + #2836 63 (all but lane 32)
this PR 0, and every lane right in all six cases (VCC_HI only, VCC zero, VCC_LO only; vccz and vccnz)

RX 6750 XT (driver 32.0.21045.29003) and WARP. This replaces a CPU replay with a GPU run; the numbers for main and #2836 match the ones reported on #2780.

Also: structure::tests::vcc_branches_on_scalar_masks_are_uniform checks the branch's condition is Atom::Nonzero alone, without a GPU.

Suites (Windows 11, Rust 1.98.1 MSVC): victory-shader 305 passed, twice. cargo clippy -p victory-shader --all-targets -- -D warnings: clean. cargo check -p victory: clean.

Not measured: any title. Unit tests only, no game files.

🤖 Generated with Claude Code

https://claude.ai/code/session_01LwHmoV6jP696CZH3DVW7V1

devin-ai-integration Bot and others added 3 commits November 26, 2026 06:34
…he register next to the low one

In wave64 a mask the structure pass cannot see into was kept as
Atom::Opaque of the pair's low value alone, and lowering guessed the high
dword from the register next to it (Marvisak#2780). A mask whose dwords are
written apart — LLVM splits a 64-bit constant that is not inline into two
s_mov_b32, and vcc_lo/vcc_hi are written apart the same way — got no
guess, so lanes 32-63 tested the low dword's bits again; a high dword
rewritten after a 64-bit write could be guessed stale.

The opaque atom now names both dwords of the pair as they were read, and
the emitter tests them directly (no guessing, so pair_high goes); two
masks sharing a low dword with different high dwords are different masks.
The atom's birth index is the later of the pair's, so forgetting masks
when a region's values are redefined drops a pair the region assembled.

A wave64 cndmask over s[12:13] written by halves now differs in DXIL for
a high dword of -1 versus 0 (identical before), matching the console.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
s_cbranch_vccz and s_cbranch_vccnz test VCC as a whole (RDNA2 ISA, 12.5:
VCCZ is VCC == 0), one decision for the wave. The structure pass gave
them each lane's own bit of VCC under EXEC. With VCC written by halves
from scalars, and only VCC_HI set, lanes 0-31 and 33-63 took the branch
and lane 32 did not, on Marvisak#2836 (all 64 took it on main).

A VCC that is a pair the pass cannot see into (Atom::Opaque, both dwords
since Marvisak#2836) now gives Atom::Nonzero, the same in every lane and lowered
as (lo | hi) != 0, so the branch is uniform; a constant VCC other than
all lanes or none is taken. No wave operation and no forced wave size.

shortcut: a VCC from lane results (compares) still branches per lane,
the same while VCC is set only in lanes of EXEC and the skipped block
does nothing in lanes whose bit is clear.

Tests: synthetic_tests::vcc_branches_test_the_whole_mask (wave64, VCC_HI
only, VCC zero and VCC_LO only, for both branches; every lane takes the
same path, on the hardware adapter and WARP) and
structure::tests::vcc_branches_on_scalar_masks_are_uniform.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LwHmoV6jP696CZH3DVW7V1

@fuzeofficlal fuzeofficlal 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 8fc606d0 against main (63bcca43; emit/lower.rs stays at 520,714 bytes, safely under the 512 KiB limit). Two concrete issues to fix before merge:

  1. Scalar VCC expressions beyond a single positive Atom::Opaque / Atom::Constant still fall back to a per-lane branch (crates/victory-shader/src/structure.rs:1371-1383)

    • whole only matches Some((Atom::Opaque(lo, hi), Bool::FALSE, Bool::TRUE)) and Some((Atom::Constant(_), Bool::FALSE, Bool::TRUE)).
    • Any scalar wave64 mask operation that produces a negated or compound BDD over scalar atoms — for example:
      • s_not_b64 vcc, s[2:3] (produces (Atom::Opaque(lo, hi), Bool::TRUE, Bool::FALSE)),
      • s_cselect_b64 vcc, 1, 2 (in structure.rs:1120, builds ite(Atom::Uniform(scc), Atom::Constant(1), Atom::Constant(2)), whose root is Atom::Uniform(scc)), or
      • scalar masks merged at a CFG join (Atom::Phi)
        misses whole and falls through to self.b.and(vcc, exec). For s_cselect_b64 vcc, 1, 2 + s_cbranch_vccnz, the whole wave must take the branch for either SCC value, but falling back to per-lane vcc & exec causes only lane 0 or lane 1 to take it (and a CFG join can produce a mask PHI whose scalar inputs the wave64 emitter rejects).
  2. Test coverage gaps (crates/victory-shader/src/emit/synthetic_tests.rs:681-684, crates/victory-shader/src/structure.rs:1767)

    • In vcc_branches_test_the_whole_mask, host.compute_pipeline(&shader) errors continue and assert!(ran > 0) is only checked once across all 6 cases. If the key vcc_hi = 1 case fails pipeline creation while vcc = 0 succeeds, the test still passes silently. Tracking a per-case run count (or failing on pipeline creation once a valid SM 6.6 device is created) avoids hiding regressions.
    • In structure.rs:1767, the second fixture sets vcc_lo = 0 and vcc_hi = -1 via two s_mov_b32 instructions, which creates Atom::Opaque(lo, hi) rather than Atom::Constant, so the Atom::Constant arm at structure.rs:1377 isn't actually exercised (and the assertion accepts either Bool::TRUE or Atom::Nonzero). Using a 64-bit constant move (e.g. s_mov_b64 vcc, 1) or testing s_cselect_b64 / s_not_b64 would directly cover the non-Opaque paths.

@zed6000

zed6000 commented Nov 26, 2026

Copy link
Copy Markdown

Measured offline on the title's shaders (the method in #1247): head 8fc606d merged onto main 63bcca4 (a clean merge, security-reviewed first), victory translate --validate --list over shaders.rpf 1.0.2.1, program by program: 0 gained, 0 lost. All 24,850 programs that validate on main still validate. Since this is the same VCC-branch area where #2405's head lost 1,829 programs (and the hardware-exact variant on #2406 lost 18,170), that's the number that matters here: the wave-uniform reading of scalar VCC doesn't cost this title anything. Offline counts only; no program names or data.

@steven-seagull27

Copy link
Copy Markdown

On 8fc606d, all six checked-in cases passed on the RTX 5080 (621.40), Windows’ built-in WARP and WARP 1.0.22: 0/384 wrong lane values per device. A separate Wave64 test found a remaining issue inside the positive Atom::Nonzero case. An SGPR copied into VCC can already have a different host value in each lane.

The fixture initializes s12 = 0, sets EXEC to just lane 0, and uses s_cbranch_execz to skip s_mov_b32 s12, 1 only if EXEC is empty. Lane 0 exists, so the scalar write executes once for the guest wave. It then restores EXEC, copies s12 to vcc_lo, clears vcc_hi, and branches. The branch should see VCC = 1 for every lane. These are counts read from the UAV on all three devices:

Selected lane Branch Expected taken Actual taken
0, 1, or 32 s_cbranch_vccnz 64 1
0, 1, or 32 s_cbranch_vccz 0 63
64 (absent) s_cbranch_vccnz 0 0
64 (absent) s_cbranch_vccz 64 64

That’s 378/512 wrong lane values per device across the eight cases. With lane 0 selected, WARP 1.0.22 also gives 63/64 wrong on parent fd2b765 for vccnz. RTX and built-in WARP skip that parent Wave64 pipeline. The PR’s original uniform scalar controls pass on all three.

I put a conservative guard and low/high-word regression on the fork. It follows scalar inputs through copies and operations before treating a VCC word as wave-wide. When a contributing scalar phi can select different incoming edges across lanes, it returns an explicit refusal. The regression fails on this PR head and passes with the guard. On all three devices the six original readbacks still pass, along with 1,728/1,728 additional results for constant VCC values under empty, single-lane and full EXEC. This guard covers the joined scalar input. The reviewer’s s_not_b64 and s_cselect_b64 expression cases are separate.

git fetch https://github.com/steven-seagull27/VIctory.git refs/heads/codex/pr3023-vcc-scalar-join
git cherry-pick 6e9e8463cad4b34841543c34968bef93a2dfc022
cargo test -p victory-shader --lib --locked vcc_branch

Shader Clippy with -D warnings and asset policy passed. AI-assisted; synthetic fixtures only. Title coverage is unmeasured.

@kotekompotek kotekompotek left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTM — making Atom::Opaque carry the pair (lo,hi) explicitly removes the fragile pair_high lookup; Nonzero ORs both dwords under wave64 with the hi!=lo dedup. cbranch_vccz/vccnz on a scalar VCC deciding on pair-nonzero matches hardware.

Follow scalar inputs and reachable phi edges before treating a VCC word as wave-wide. A lane-local DXIL phi can feed an ordinary SGPR copy even when the final VCC value is opaque. Check both dwords in wave64 and preserve the original comparison for proven inputs.

Cover a lane-selected write to s12 copied into either word. On a 64-lane synthetic dispatch the unguarded comparison wrongly branches in just one lane on RTX 5080, built-in WARP and WARP 1.0.22.
wwwwwwww3333333 added a commit to wwwwwwww3333333/VIctory that referenced this pull request Nov 26, 2026
s_cbranch_vccz/vccnz decided once for the wave only when VCC was one
positive opaque pair or one constant. s_not_b64, s_cselect_b64 (an SCC
select), s_and_b64 with a constant and the like over scalar masks fell
back to each lane's own bit: s_cselect_b64 vcc, 1, 2 took the branch in
lane 1 alone.

A VCC whose diagram tests only atoms held in scalars is now one condition
for the wave. With constants and SCC alone it is decided in the structure
pass, lane by lane over the constants. With opaque pairs it becomes
Atom::Any, lowered as the scalar mask's two words and tested for zero
(VCC_LO alone in wave32). A VCC from compares or a mask join keeps the
per-lane shortcut.

vcc_branches_test_the_whole_mask also fails when a device runs some
cases but makes no pipeline for others, and covers the new forms; the
structure test covers s_mov_b64, s_cselect_b64, s_not_b64 and s_and_b64.

Review by fuzeofficlal on Marvisak#3023.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LwHmoV6jP696CZH3DVW7V1
s_cbranch_vccz/vccnz decided once for the wave only when VCC was one
positive opaque pair or one constant. s_not_b64, s_cselect_b64 (an SCC
select), s_and_b64 with a constant and the like over scalar masks fell
back to each lane's own bit: s_cselect_b64 vcc, 1, 2 took the branch in
lane 1 alone.

A VCC whose diagram tests only atoms held in scalars is now one condition
for the wave. With constants and SCC alone it is decided in the structure
pass, lane by lane over the constants. With opaque pairs it becomes
Atom::Any, lowered as the scalar mask's two words and tested for zero
(VCC_LO alone in wave32). A VCC from compares or a mask join keeps the
per-lane shortcut.

vcc_branches_test_the_whole_mask also fails when a device runs some
cases but makes no pipeline for others, and covers the new forms; the
structure test covers s_mov_b64, s_cselect_b64, s_not_b64 and s_and_b64.

Review by fuzeofficlal on Marvisak#3023.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LwHmoV6jP696CZH3DVW7V1
@wwwwwwww3333333

Copy link
Copy Markdown
Contributor Author

Thanks @fuzeofficlal, both points were right, and thanks @steven-seagull27 for the guard.

  • Scalar expressions (3228393): a VCC whose diagram tests only atoms held in scalars (opaque pairs, constants, SCC selects) is now one condition for the wave, however it was combined. With constants and SCC alone it is decided in the structure pass, lane by lane over the constants, so s_cselect_b64 vcc, 1, 2 and s_mov_b64 vcc, 1 give TRUE. With an opaque pair (s_not_b64, s_and_b64 with a constant) it becomes Atom::Any, lowered as the mask's two words and tested for zero (VCC_LO alone in wave32). Steven's join guard also runs on the pairs inside it. A VCC from compares or from a mask join (Atom::Phi) still branches per lane, the shortcut already marked in the code.
  • Tests: vcc_branches_test_the_whole_mask now fails if a device runs some cases but makes no pipeline for others. It has 14 cases, including s_mov_b64, s_cselect_b64 (2 and 0), s_not_b64 (hi bit only, and zero) and s_and_b64. The structure test now covers s_mov_b64, s_cselect_b64, s_not_b64 and s_and_b64 directly.
  • steven-seagull27's guard (91f3915) is on the branch with his authorship.

On an RX 6750 XT and WARP, every lane took the expected path in all 14 cases, both ways. Before this, s_cselect_b64 2 and s_and_b64 2 took the branch in lane 1 alone, and s_not_b64 hi 1 in lane 32 alone. victory-shader passed 307 of 307, and Clippy with -D warnings, cargo check -p victory and the xtask tests are clean. lower.rs is 521,058 bytes. The title's shaders are not measured on this head: @zed6000's 0/0 count was on 8fc606d.

Marvisak commented Nov 26, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

The direction is right and the evidence is cited: RDNA2 ISA 12.5 (VCCZ tests the whole of VCC), with GPU readbacks on an RX 6750 XT and WARP. The change spans structure.rs, emit/lower.rs and the new emit/lower/scalar_vcc.rs:

  • Lanes::whole turns a VCC built only from scalar atoms into one condition for the wave: Atom::Nonzero for an opaque pair, a per-lane fold over constants (Bools::some_lane), or Atom::Any for scalar expressions.
  • Atom::Opaque now carries both dwords of the pair, replacing pair_high's guess at the next register (from Shaders: a lane mask's high dword is the pair's own, not a guess at the register next to the low one #2836).
  • check_scalar_vcc_joins refuses a VCC fed by a scalar phi that can diverge across lanes.
  • The per-lane shortcut for VCC built from compares is marked in the code.

Update after the author's reply: merge order is settled. The first two commits here (64c89b9, fd2b765) are #2836's two commits rebased onto main with the same messages, so reviewing this PR alone and closing #2836 in its favour is the maintainer's call.

What is still needed before it can be recommended:

  • A title measurement on this head. The only shader-pack count (0 gained / 0 lost) was on 8fc606d. Since then the head adds Atom::Any lowering through mask_words and a new refusal ("a scalar VCC branch depending on a divergent scalar join"), and either could cost programs. Shaders: test raw VCC on s_cbranch_vccz and s_cbranch_vccnz without EXEC masking #2405 lost 1,829 programs in this same area. A program-by-program count on 3228393 (gained/lost) from anyone with the title's shaders would settle it.

CI check: passing.


Generated by Claude Code

@wwwwwwww3333333

Copy link
Copy Markdown
Contributor Author

Thanks for the triage.

Merge order: #3023 carries #2836's two commits unchanged, so I'd suggest closing #2836 in favour of this PR and reviewing one stack. If you'd rather land #2836 first, say so and I'll rebase this PR onto it so only the guard and the whole-VCC commits remain.

Title count: I can't measure the title's shaders on 3228393 (I have no dump here). The new parts since 8fc606d are Atom::Any lowering and the divergent-scalar-join refusal. @zed6000, if you can rerun your offline count on 3228393, a gained/lost number would settle that point. Until then it is unmeasured, and I'm not claiming a safe count.

Unit tests only, no game files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants