Shaders: fix RPO resource tracking, SMEM alignment, raw VCC branch, and readlane/writelane semantics (#1249) #1336

Open
wbkepler wants to merge 2 commits from upstream-pr/1336 into main
wbkepler commented 2026-11-24 17:32:59 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by wbkepler on 2026-11-24T17:32:59Z.


Summary

Fixes five RDNA2 shader ISA and CFG tracking bugs identified in #1249:

  1. VIC-BT-024 (crates/victory-shader/src/resource.rs): Iterate resource::track() in reverse_postorder(&p.blocks) rather than raw vector index order so descriptor/waterfall s_loads in higher-indexed blocks (such as copies appended by structure::split_irreducible()) populate out.scalar before lower-indexed consumers inspect them.
  2. VIC-BT-025 (crates/victory-shader/src/emit/lower.rs): Mask each s_buffer_load_dword* address term (base, offset, soffset) independently with & !3u32 before summing (matching RDNA2 silicon where soffset = 3, offset = 3 resolves to byte +0, not +4).
  3. VIC-BT-026 (crates/victory-shader/src/structure.rs): Evaluate s_cbranch_vccz / s_cbranch_vccnz on raw vcc instead of vcc & exec (matching RDNA2 scalar VCC branch semantics; v_cmp already writes vcc under exec).
  4. VIC-BT-030 (crates/victory-shader/src/emit/lower.rs, wave.rs): Mask the S1 lane-select operand of v_readlane_b32 and v_writelane_b32 to S1 & 31 (Wave32) / S1 & 63 (Wave64) before waveReadLaneAt / lane_id == lane.
  5. VIC-BT-031 (crates/victory-shader/src/emit/lower.rs): Lower v_writelane_b32 unconditionally without gating the destination write on EXEC so SGPR spills to inactive lanes in divergent control flow are preserved.

Testing

  • Added 6 synthetic unit/DXIL tests across resource.rs, structure.rs, emit/wave.rs, and emit/lower.rs.
  • cargo clippy -p victory-shader --all-targets — clean
  • cargo test -p victory-shader — 126 passed, 0 failed (1 ignored benchmark)
  • cargo xtask policy — 154 files ok

(AI-assisted implementation and verification.)

<!-- greyfinch-sync:Marvisak/VIctory:issue:6587694804 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1336). Originally posted by [wbkepler](https://github.com/wbkepler) on 2026-11-24T17:32:59Z. --- ## Summary Fixes five RDNA2 shader ISA and CFG tracking bugs identified in #1249: 1. **`VIC-BT-024` (`crates/victory-shader/src/resource.rs`)**: Iterate `resource::track()` in `reverse_postorder(&p.blocks)` rather than raw vector index order so descriptor/waterfall `s_load`s in higher-indexed blocks (such as copies appended by `structure::split_irreducible()`) populate `out.scalar` before lower-indexed consumers inspect them. 2. **`VIC-BT-025` (`crates/victory-shader/src/emit/lower.rs`)**: Mask each `s_buffer_load_dword*` address term (`base`, `offset`, `soffset`) independently with `& !3u32` before summing (matching RDNA2 silicon where `soffset = 3, offset = 3` resolves to byte `+0`, not `+4`). 3. **`VIC-BT-026` (`crates/victory-shader/src/structure.rs`)**: Evaluate `s_cbranch_vccz` / `s_cbranch_vccnz` on raw `vcc` instead of `vcc & exec` (matching RDNA2 scalar VCC branch semantics; `v_cmp` already writes `vcc` under `exec`). 4. **`VIC-BT-030` (`crates/victory-shader/src/emit/lower.rs`, `wave.rs`)**: Mask the `S1` lane-select operand of `v_readlane_b32` and `v_writelane_b32` to `S1 & 31` (Wave32) / `S1 & 63` (Wave64) before `waveReadLaneAt` / `lane_id == lane`. 5. **`VIC-BT-031` (`crates/victory-shader/src/emit/lower.rs`)**: Lower `v_writelane_b32` unconditionally without gating the destination write on `EXEC` so SGPR spills to inactive lanes in divergent control flow are preserved. ## Testing - Added 6 synthetic unit/DXIL tests across `resource.rs`, `structure.rs`, `emit/wave.rs`, and `emit/lower.rs`. - `cargo clippy -p victory-shader --all-targets` — clean - `cargo test -p victory-shader` — 126 passed, 0 failed (1 ignored benchmark) - `cargo xtask policy` — 154 files ok *(AI-assisted implementation and verification.)*
dpalacios69 commented 2026-11-24 19:04:14 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by dpalacios69 on 2026-11-24T18:26:37Z.


Independent check on Linux of head da23e89, merged onto current main (ffe310e; a fast-forward): cargo test -p victory-shader --locked passes (102 passed, 0 failed, 1 ignored). The 6 test(s) it adds run on Linux and pass. cargo clippy -p victory-shader --all-targets -- -D warnings shows nothing of its own, only the two unused-import errors in emit/mod.rs that main already has on Linux. No GPU run. AI-assisted; no game data.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7140652301 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1336#issuecomment-7140652301). Originally posted by [dpalacios69](https://github.com/dpalacios69) on 2026-11-24T18:26:37Z. --- Independent check on Linux of head `da23e89`, merged onto current `main` (`ffe310e`; a fast-forward): `cargo test -p victory-shader --locked` passes (102 passed, 0 failed, 1 ignored). The 6 test(s) it adds run on Linux and pass. `cargo clippy -p victory-shader --all-targets -- -D warnings` shows nothing of its own, only the two unused-import errors in `emit/mod.rs` that `main` already has on Linux. No GPU run. AI-assisted; no game data.
sinan-cmyk commented 2026-11-24 20:03:19 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by sinan-cmyk on 2026-11-24T19:50:15Z.


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

Result: no count change against main.

Offline. I merged head da23e89 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 126, 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:7142658617 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1336#issuecomment-7142658617). Originally posted by [sinan-cmyk](https://github.com/sinan-cmyk) on 2026-11-24T19:50:15Z. --- ### Offline measurement on a retail dump, and the shader tests on Ada **Result: no count change against `main`.** **Offline.** I merged head da23e89 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 126, 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).
catminou4 commented 2026-11-24 21:02:08 +00:00 (Migrated from github.com)

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

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 → 102 passed, 0 failed. Execution/valu tests are #[cfg(all(test, windows))] and only run on Windows; macOS covers lowering/structure/IR unit tests.

Fixes RPO resource tracking, SMEM alignment and raw VCC branch handling, plus readlane/writelane lane masking; adds DXIL-level test helpers (eval_i32/calls/selects/cmp_eq) and structure tests that run on macOS (102 passed).

<!-- greyfinch-sync:Marvisak/VIctory:review:6046557757 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1336#pullrequestreview-6046557757). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T20:09:50Z. **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 → 102 passed, 0 failed. Execution/valu tests are #[cfg(all(test, windows))] and only run on Windows; macOS covers lowering/structure/IR unit tests. Fixes RPO resource tracking, SMEM alignment and raw VCC branch handling, plus readlane/writelane lane masking; adds DXIL-level test helpers (eval_i32/calls/selects/cmp_eq) and structure tests that run on macOS (102 passed).
wbkepler commented 2026-11-24 21:28:26 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by wbkepler on 2026-11-24T21:05:06Z.


Follow-up pushed in 4eead6e, prompted by a comparison with #1312 by @ashrafsdrop, who gets the credit for this exception:

  • v_readlane_b32 host lanes: this PR masked every lane index by the console wave. In a pixel or vertex program, a computed lane (MaskData::Host) is a host lane, so on a 64-lane AMD host wave the mask cut off lanes 32–63. Those lanes are now left unmasked, the same exception #1312 makes. Constant (console) lanes and v_writelane_b32 are still masked. This had no effect on 32-lane NVIDIA hosts.
  • Spill tracking (resource.rs): the v_writelane/v_readlane SGPR spill matcher now compares constant lanes modulo the wave too. Without that, a spill to lane 35 would not match its restore from lane 3 once the lowering wraps them.
  • New unit test a_computed_host_lane_in_a_pixel_program_is_not_masked: in a pixel program, a computed lane 35 reaches waveReadLaneAt as 35, not 3.

Verified with clippy -D warnings (victory-shader + victory diagnostics), cargo test -p victory-shader (127 passed) and cargo xtask policy.

Note for the maintainer: this PR and #1312 overlap on the readlane/writelane lane masking and the write_i change. Either one can land first, and the other will need a rebase.

AI-assisted (Claude Code); checked on Windows 11 / RTX 5090.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7144453943 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1336#issuecomment-7144453943). Originally posted by [wbkepler](https://github.com/wbkepler) on 2026-11-24T21:05:06Z. --- Follow-up pushed in `4eead6e`, prompted by a comparison with #1312 by @ashrafsdrop, who gets the credit for this exception: - **`v_readlane_b32` host lanes:** this PR masked every lane index by the console wave. In a pixel or vertex program, a computed lane (`MaskData::Host`) is a *host* lane, so on a 64-lane AMD host wave the mask cut off lanes 32–63. Those lanes are now left unmasked, the same exception #1312 makes. Constant (console) lanes and `v_writelane_b32` are still masked. This had no effect on 32-lane NVIDIA hosts. - **Spill tracking (`resource.rs`):** the `v_writelane`/`v_readlane` SGPR spill matcher now compares constant lanes modulo the wave too. Without that, a spill to lane 35 would not match its restore from lane 3 once the lowering wraps them. - New unit test `a_computed_host_lane_in_a_pixel_program_is_not_masked`: in a pixel program, a computed lane 35 reaches `waveReadLaneAt` as 35, not 3. Verified with clippy `-D warnings` (victory-shader + victory diagnostics), `cargo test -p victory-shader` (127 passed) and `cargo xtask policy`. Note for the maintainer: this PR and #1312 overlap on the readlane/writelane lane masking and the `write_i` change. Either one can land first, and the other will need a rebase. _AI-assisted (Claude Code); checked on Windows 11 / RTX 5090._
Marvisak commented 2026-11-25 02:05:57 +00:00 (Migrated from github.com)

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


Automated triage for the maintainers.

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


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149486746 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1336#issuecomment-7149486746). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T01:22:55Z. --- <!-- victory-triage v1 head=4eead6e1163c003d45305987c9b060d39e37c1de verdict=needs-rebase at=2026-11-25T01:22:53Z --> Automated triage for the maintainers. **Needs rebase.** This pull request conflicts with `main` (conflicting: `crates/victory-shader/src/emit/lower.rs`, `crates/victory-shader/src/emit/wave.rs`, `crates/victory-shader/src/structure.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/1336:upstream-pr/1336'
git switch 'upstream-pr/1336'

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/1336'
git switch 'upstream-pr/1336'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1336'
git switch 'upstream-pr/1336'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1336'
git switch 'main'
git merge --squash 'upstream-pr/1336'
git switch 'main'
git merge --ff-only 'upstream-pr/1336'
git switch 'main'
git merge 'upstream-pr/1336'
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!1336
No description provided.