Shaders: track source lane in whole-wave Sym::Own to prevent miscompiled shuffles #1485

Open
AscilCH wants to merge 1 commit from upstream-pr/1486 into main
AscilCH commented 2026-11-24 18:22:19 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by AscilCH on 2026-11-24T18:22:19Z.


Bug

In crates/victory-shader/src/emit/wave.rs, pre-section VGPR inputs were represented as Sym::Own(Value) without recording which lane originally held the value.

When a whole-wave section performed cross-lane operations on a pre-section VGPR:

  • v_readlane_b32 (and subsequent broadcasts) copied Sym::Own(v) into scalar outputs; shapes() then matched Sym::Own(v) and returned Shape::Value(v) — silently lowering a broadcast of lane k as each lane's own v[l]!
  • DPP shuffles (row_mirror, quad_perm, row_ror, dpp8, etc.) and v_permlanex16_b32 moved Sym::Own(v) from lane src to lane dst; per_lane() then checked lane.iter().all(|x| *x == Sym::Own(v)) and collapsed the shuffled vector back to Shape::Value(v) (identity)!
  • v_cndmask_b32 over a shuffled pre-section VGPR checked *a == Sym::Own(v) without checking the originating lane, silently collapsing to Shape::Masked(v, mask, 0).

Reproduction (Before Fix)

With a synthetic whole-wave section executing v_readlane_b32 s5, v0, 7, v_mov_b32_dpp v2, v0 row_mirror, v_permlanex16_b32 v3, v0, 0, 0, and v_cndmask_b32 over a shuffled v0:

v_readlane_b32 of pre-section VGPR must not collapse to Shape::Value: left: Some(Value(Value(2))), right: None

Fix

  • Carry the originating lane index in Sym::Own(Value, u8) (initialized with l as u8 when reading an untracked VGPR in lane l).
  • Require usize::from(from) == l when recognizing identity/masked outputs in v_cndmask_b32 and per_lane().
  • Never map Sym::Own(..) to Shape::Value in scalar shapes() or in per_lane()'s uniform broadcast branch (since a broadcast of a single source lane's pre-section VGPR is not each lane's own value).
  • Added unit test emit::wave::tests::cross_lane_ops_on_pre_section_vgpr_do_not_collapse_to_identity covering v_readlane_b32, v_mov_b32_dpp (row_mirror), v_permlanex16_b32, and v_cndmask_b32 over a shuffled VGPR, while asserting unshuffled identity/masked paths remain recognized.

(Disjoint from #646, which addresses constant() sign-extension and op: None in per_lane().)

Verification

  • rustfmt --edition 2024 --check crates/victory-shader/src/emit/wave.rs: clean
  • VICTORY_WARP=1 cargo test -p victory-shader --locked -- --test-threads=1: 121 passed, 0 failed
  • cargo xtask policy: 154 files ok
  • cargo clippy --workspace --all-targets --locked -- -D warnings: clean
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: clean
<!-- greyfinch-sync:Marvisak/VIctory:issue:6588561273 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1486). Originally posted by [AscilCH](https://github.com/AscilCH) on 2026-11-24T18:22:19Z. --- ## Bug In `crates/victory-shader/src/emit/wave.rs`, pre-section VGPR inputs were represented as `Sym::Own(Value)` without recording which lane originally held the value. When a whole-wave section performed cross-lane operations on a pre-section VGPR: - `v_readlane_b32` (and subsequent broadcasts) copied `Sym::Own(v)` into scalar outputs; `shapes()` then matched `Sym::Own(v)` and returned `Shape::Value(v)` — silently lowering a broadcast of lane `k` as each lane's own `v[l]`! - DPP shuffles (`row_mirror`, `quad_perm`, `row_ror`, `dpp8`, etc.) and `v_permlanex16_b32` moved `Sym::Own(v)` from lane `src` to lane `dst`; `per_lane()` then checked `lane.iter().all(|x| *x == Sym::Own(v))` and collapsed the shuffled vector back to `Shape::Value(v)` (identity)! - `v_cndmask_b32` over a shuffled pre-section VGPR checked `*a == Sym::Own(v)` without checking the originating lane, silently collapsing to `Shape::Masked(v, mask, 0)`. ## Reproduction (Before Fix) With a synthetic whole-wave section executing `v_readlane_b32 s5, v0, 7`, `v_mov_b32_dpp v2, v0 row_mirror`, `v_permlanex16_b32 v3, v0, 0, 0`, and `v_cndmask_b32` over a shuffled `v0`: ```text v_readlane_b32 of pre-section VGPR must not collapse to Shape::Value: left: Some(Value(Value(2))), right: None ``` ## Fix - Carry the originating lane index in `Sym::Own(Value, u8)` (initialized with `l as u8` when reading an untracked VGPR in lane `l`). - Require `usize::from(from) == l` when recognizing identity/masked outputs in `v_cndmask_b32` and `per_lane()`. - Never map `Sym::Own(..)` to `Shape::Value` in scalar `shapes()` or in `per_lane()`'s uniform broadcast branch (since a broadcast of a single source lane's pre-section VGPR is not each lane's own value). - Added unit test `emit::wave::tests::cross_lane_ops_on_pre_section_vgpr_do_not_collapse_to_identity` covering `v_readlane_b32`, `v_mov_b32_dpp` (`row_mirror`), `v_permlanex16_b32`, and `v_cndmask_b32` over a shuffled VGPR, while asserting unshuffled identity/masked paths remain recognized. *(Disjoint from #646, which addresses `constant()` sign-extension and `op: None` in `per_lane()`.)* ## Verification - `rustfmt --edition 2024 --check crates/victory-shader/src/emit/wave.rs`: clean - `VICTORY_WARP=1 cargo test -p victory-shader --locked -- --test-threads=1`: 121 passed, 0 failed - `cargo xtask policy`: 154 files ok - `cargo clippy --workspace --all-targets --locked -- -D warnings`: clean - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: clean
DenRakEiw commented 2026-11-24 19:03:36 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-24T18:28:06Z.

Review: APPROVED


Verified locally merged onto origin/main (Windows 11 x64, Rust 1.98.1 MSVC):

  • cargo test -p victory-shader --lib emit::wave:: — 3 passed, 0 failed (including cross_lane_ops_on_pre_section_vgpr_do_not_collapse_to_identity)
  • cargo clippy -p victory-shader --all-targets — 0 warnings
  • cargo xtask policy — ok

Clean fix tracking source lane identity in Sym::Own(Value, u8) so cross-lane shuffles on pre-section VGPRs do not collapse to identity. Looks good.

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6045044564 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1486#pullrequestreview-6045044564). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-24T18:28:06Z. **Review:** APPROVED --- Verified locally merged onto `origin/main` (Windows 11 x64, Rust 1.98.1 MSVC): - `cargo test -p victory-shader --lib emit::wave::` — 3 passed, 0 failed (including `cross_lane_ops_on_pre_section_vgpr_do_not_collapse_to_identity`) - `cargo clippy -p victory-shader --all-targets` — 0 warnings - `cargo xtask policy` — ok Clean fix tracking source lane identity in `Sym::Own(Value, u8)` so cross-lane shuffles on pre-section VGPRs do not collapse to identity. Looks good. AI-assisted (Gemini 4 Neon).
catminou4 commented 2026-11-24 19:03:36 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T18:42:09Z.

Review: APPROVED


Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (ffe310e), cargo test -p victory-shader --locked → 97 passed, 0 failed. Execution/valu tests are #[cfg(all(test, windows))] and were not run here; lowering/structure coverage only.

Sym::Own(Value, u8) recording the source lane fixes cross-lane ops on pre-section VGPRs being collapsed to identity in wave.rs: the Red case now requires from == l, and per_lane only folds to Shape::Value when every lane holds its own x. The new simulate test (readlane broadcast, DPP row_mirror, cndmask, permlanex16 + positive controls for plain copy/unshuffled select/uniform readlane) runs on macOS — 97 passed (+1). Subtle miscompile fix, good coverage.

<!-- greyfinch-sync:Marvisak/VIctory:review:6045232967 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1486#pullrequestreview-6045232967). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T18:42:09Z. **Review:** APPROVED --- Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (ffe310e), `cargo test -p victory-shader --locked` → 97 passed, 0 failed. Execution/valu tests are `#[cfg(all(test, windows))]` and were not run here; lowering/structure coverage only. `Sym::Own(Value, u8)` recording the source lane fixes cross-lane ops on pre-section VGPRs being collapsed to identity in `wave.rs`: the `Red` case now requires `from == l`, and `per_lane` only folds to `Shape::Value` when every lane holds its own `x`. The new simulate test (readlane broadcast, DPP row_mirror, cndmask, permlanex16 + positive controls for plain copy/unshuffled select/uniform readlane) runs on macOS — 97 passed (+1). Subtle miscompile fix, good coverage.
sinan-cmyk commented 2026-11-24 21:03:24 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by sinan-cmyk on 2026-11-24T20:13:19Z.


Offline measurement on a retail dump, and the shader tests on Ada

Result: no count change against main.

Offline. I merged head ecdb146 onto main ffe310e (clean merge) and ran victory translate --validate --list on shaders.rpf (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with main program by program (25,889 programs). Validated: 21,724 on main, 21,724 with this PR.

No program changes its failure reason either. That is the expected result for a fix whose instructions sit behind the earlier failures on main. The count shows that the change rejects nothing. It does not show whether the output is right.

Tests: with the same merge, cargo test --locked -p victory-shader --lib passed 121, failed 0. The bitcode benchmark is ignored, as on main, where 120 pass. The run was on an RTX 4070 SUPER (Ada, 32-lane waves, driver 32.0.16.1088), the default adapter, with VICTORY_WARP unset.

No shader names or data are included, per the asset policy. AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7143210508 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1486#issuecomment-7143210508). Originally posted by [sinan-cmyk](https://github.com/sinan-cmyk) on 2026-11-24T20:13:19Z. --- ### Offline measurement on a retail dump, and the shader tests on Ada **Result: no count change against `main`.** **Offline.** I merged head ecdb146 onto `main` ffe310e (clean merge) and ran `victory translate --validate --list` on `shaders.rpf` (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with `main` program by program (25,889 programs). Validated: 21,724 on `main`, 21,724 with this PR. No program changes its failure reason either. That is the expected result for a fix whose instructions sit behind the earlier failures on `main`. The count shows that the change rejects nothing. It does not show whether the output is right. **Tests:** with the same merge, `cargo test --locked -p victory-shader --lib` passed 121, failed 0. The bitcode benchmark is ignored, as on `main`, where 120 pass. The run was on an RTX 4070 SUPER (Ada, 32-lane waves, driver 32.0.16.1088), the default adapter, with `VICTORY_WARP` unset. No shader names or data are included, per the asset policy. AI-assisted (Claude Code).
Marvisak commented 2026-11-25 02:06:07 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T01:29:34Z.


Automated triage for the maintainers.

Needs rebase. This pull request conflicts with main (conflicting: crates/victory-shader/src/emit/wave.rs). It will be reviewed once it is rebased onto the current main.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149607322 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1486#issuecomment-7149607322). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T01:29:34Z. --- <!-- victory-triage v1 head=ecdb14633b45dc729112facb35ddfbc248795b55 verdict=needs-rebase at=2026-11-25T01:29:33Z --> Automated triage for the maintainers. **Needs rebase.** This pull request conflicts with `main` (conflicting: `crates/victory-shader/src/emit/wave.rs`). It will be reviewed once it is rebased onto the current `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/1486:upstream-pr/1486'
git switch 'upstream-pr/1486'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/1486'
git switch 'upstream-pr/1486'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1486'
git switch 'upstream-pr/1486'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1486'
git switch 'main'
git merge --squash 'upstream-pr/1486'
git switch 'main'
git merge --ff-only 'upstream-pr/1486'
git switch 'main'
git merge 'upstream-pr/1486'
git push origin 'main'
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
Marvisak/VIctory!1485
No description provided.