Shaders: honor v_permlanex16 EXEC/FI/BOUND_CTRL and 64-bit masks in wave sections #3393

Open
fuzeofficlal wants to merge 2 commits from upstream-pr/3393 into main
fuzeofficlal commented 2026-11-26 21:45:42 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T21:45:42Z.


Summary

Fixes two soundness bugs in whole-wave section simulation (crates/victory-shader/src/emit/wave.rs) that silently miscompiled valid RDNA2 shaders into wrong DXIL wave operations:

  1. v_permlanex16_b32 inactive source handling under narrowed EXEC:
    simulate() previously read a[other | pick] unconditionally, ignoring both the current simulated exec mask and the VOP3 op_sel flags (op_sel[0] = FI, op_sel[1] = BOUND_CTRL). Under a narrowed constant EXEC mask (for example s_mov_b32 exec_lo, 0xFFFF_0000 in prefix-sum sequences), reading an inactive source lane with FI=0 must preserve the destination lane (BOUND_CTRL=0) or produce 0 (BOUND_CTRL=1) rather than fetching the inactive lane and miscompiling into WavePrefixSum.

  2. Wave64 64-bit mask tracking and wave-width matching:
    sections() and simulate() previously accepted 32-bit saveexec / s_and_b32 / partial EXEC_LO restores in Wave64 programs and tracked only the low 32-bit SGPR SSA value of saved and derived masks. In Wave64, a 32-bit saveexec leaves EXEC_HI untouched, a partial s_mov_b32 exec_lo, s0 does not restore EXEC_HI, and overwriting the high dword of a 64-bit mask before v_cndmask_b32 or EXEC restore was ignored — silently lowering partial-wave reductions into full 64-lane WaveActive* operations.

Changes

  • crates/victory-shader/src/emit/wave.rs:
    • Require s_or_saveexec_* / s_nand_saveexec_* and s_and_b* to match the program's wave width (_b64 for Wave64, _b32 for Wave32).
    • Track full lane-width (lo, hi) SSA mask pairs (Mask = (Value, Value)) for section start, EXEC restore detection, s_and mask derivation, and v_cndmask_b32 mask checks.
    • Model v_permlanex16_b32 source-lane activity with exec, op_sel[0] (FI), and op_sel[1] (BOUND_CTRL) per the RDNA2 ISA (fi || exec >> src & 1 != 0 fetches a[src]; otherwise bound_ctrl yields 0 and !bound_ctrl clears the lane's write bit to preserve old[l]).
    • Add unit and end-to-end (including D3D12 compute dispatch) regression tests covering v_permlanex16_b32 under narrowed EXEC (FI=0/BC=0, FI=0/BC=1, FI=1, active source lane) and Wave64 mask high-half clobbering, 32-bit s_and_b32, wrong-width saveexec, and partial EXEC_LO / clobbered-high-half restores.

Verification

  • Confirmed all 3 new regression tests fail deterministically on 63bcca43 before the fix and pass 20/20 rounds after the fix (including D3D12 compute execution of the FI=1 prefix sum).
  • rustfmt --check --edition 2024 crates/victory-shader/src/emit/wave.rs passed.
  • cargo test -p victory-shader --lib --locked emit::wave::tests (default and --all-features), emit::lower::tests, emit::flow_tests, emit::diff_tests, and --doc passed.
  • cargo clippy --workspace --all-targets --locked -- -D warnings and --all-features passed with zero warnings.
  • cargo test -p xtask --locked policy::tests::repository_passes_policy passed.
<!-- greyfinch-sync:Marvisak/VIctory:issue:5517568940 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3393). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T21:45:42Z. --- ## Summary Fixes two soundness bugs in whole-wave section simulation (`crates/victory-shader/src/emit/wave.rs`) that silently miscompiled valid RDNA2 shaders into wrong DXIL wave operations: 1. **`v_permlanex16_b32` inactive source handling under narrowed `EXEC`**: `simulate()` previously read `a[other | pick]` unconditionally, ignoring both the current simulated `exec` mask and the VOP3 `op_sel` flags (`op_sel[0] = FI`, `op_sel[1] = BOUND_CTRL`). Under a narrowed constant `EXEC` mask (for example `s_mov_b32 exec_lo, 0xFFFF_0000` in prefix-sum sequences), reading an inactive source lane with `FI=0` must preserve the destination lane (`BOUND_CTRL=0`) or produce `0` (`BOUND_CTRL=1`) rather than fetching the inactive lane and miscompiling into `WavePrefixSum`. 2. **Wave64 64-bit mask tracking and wave-width matching**: `sections()` and `simulate()` previously accepted 32-bit `saveexec` / `s_and_b32` / partial `EXEC_LO` restores in Wave64 programs and tracked only the low 32-bit SGPR SSA value of saved and derived masks. In Wave64, a 32-bit `saveexec` leaves `EXEC_HI` untouched, a partial `s_mov_b32 exec_lo, s0` does not restore `EXEC_HI`, and overwriting the high dword of a 64-bit mask before `v_cndmask_b32` or `EXEC` restore was ignored — silently lowering partial-wave reductions into full 64-lane `WaveActive*` operations. ## Changes - `crates/victory-shader/src/emit/wave.rs`: - Require `s_or_saveexec_*` / `s_nand_saveexec_*` and `s_and_b*` to match the program's wave width (`_b64` for Wave64, `_b32` for Wave32). - Track full lane-width `(lo, hi)` SSA mask pairs (`Mask = (Value, Value)`) for section start, `EXEC` restore detection, `s_and` mask derivation, and `v_cndmask_b32` mask checks. - Model `v_permlanex16_b32` source-lane activity with `exec`, `op_sel[0]` (`FI`), and `op_sel[1]` (`BOUND_CTRL`) per the RDNA2 ISA (`fi || exec >> src & 1 != 0` fetches `a[src]`; otherwise `bound_ctrl` yields `0` and `!bound_ctrl` clears the lane's write bit to preserve `old[l]`). - Add unit and end-to-end (including D3D12 compute dispatch) regression tests covering `v_permlanex16_b32` under narrowed `EXEC` (`FI=0/BC=0`, `FI=0/BC=1`, `FI=1`, active source lane) and Wave64 mask high-half clobbering, 32-bit `s_and_b32`, wrong-width `saveexec`, and partial `EXEC_LO` / clobbered-high-half restores. ## Verification - Confirmed all 3 new regression tests fail deterministically on `63bcca43` before the fix and pass 20/20 rounds after the fix (including D3D12 compute execution of the `FI=1` prefix sum). - `rustfmt --check --edition 2024 crates/victory-shader/src/emit/wave.rs` passed. - `cargo test -p victory-shader --lib --locked emit::wave::tests` (default and `--all-features`), `emit::lower::tests`, `emit::flow_tests`, `emit::diff_tests`, and `--doc` passed. - `cargo clippy --workspace --all-targets --locked -- -D warnings` and `--all-features` passed with zero warnings. - `cargo test -p xtask --locked policy::tests::repository_passes_policy` passed.
Dukestar69 commented 2026-11-26 21:47:17 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T21:47:17Z.

Review: APPROVED


LGTM — verified on Windows against origin/main (63bcca4).

  • Honoring v_permlanex16 EXEC/FI/BOUND_CTRL semantics and tracking full 64-bit masks in wave64 sections closes real whole-wave simulation soundness gaps cleanly.
  • cargo test -p victory-shader --lib emit::wave:: -- --test-threads=1: 10 passed, 0 failed (including v_permlanex16_honors_exec_fi_and_bound_ctrl, wave64_whole_wave_section_tracks_full_64bit_masks_and_width, and end_to_end_permlanex16_and_wave64_high_mask_do_not_miscompile).
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6080831086 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3393#pullrequestreview-6080831086). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T21:47:17Z. **Review:** APPROVED --- LGTM — verified on Windows against `origin/main` (`63bcca4`). - Honoring `v_permlanex16` `EXEC`/`FI`/`BOUND_CTRL` semantics and tracking full 64-bit masks in wave64 sections closes real whole-wave simulation soundness gaps cleanly. - `cargo test -p victory-shader --lib emit::wave:: -- --test-threads=1`: 10 passed, 0 failed (including `v_permlanex16_honors_exec_fi_and_bound_ctrl`, `wave64_whole_wave_section_tracks_full_64bit_masks_and_width`, and `end_to_end_permlanex16_and_wave64_high_mask_do_not_miscompile`). - `cargo clippy -p victory-shader --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
andrewpozdnakov7-jpg commented 2026-11-26 22:11:39 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-26T22:11:39Z.


I exercised the end-to-end test on 09046e7 and made pipeline creation failures visible when the test has a capable D3D12 device. Test-only follow-up 42fc8db also adds readbacks for both BOUND_CTRL values with FI=1 and the intact Wave64 reduction. The DXIL validator checks each positive shader before dispatch.

Three app-local WARP runs read the exact prefix sums from 1 through 528 in all 32 lanes for each FI=1 variant. The intact Wave64 case read 2080 in all 64 lanes. Both FI=0 forms under narrowed EXEC and the clobbered Wave64 mask still fail translation in the same fixture. The affected wave, lowering, semantics and IR tests had 86 passed and one ignored. Strict Clippy in both configurations and the asset policy check passed.

@fuzeofficlal, the stronger fixture is ready to take if you want to keep all three positive readbacks in the PR. Please retain the Git author or use Co-authored-by: andrewpozdnakov7-jpg <[email protected]> if folding it into another commit.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7204450087 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3393#issuecomment-7204450087). Originally posted by [andrewpozdnakov7-jpg](https://github.com/andrewpozdnakov7-jpg) on 2026-11-26T22:11:39Z. --- I exercised the end-to-end test on `09046e7` and made pipeline creation failures visible when the test has a capable D3D12 device. [Test-only follow-up `42fc8db`](https://github.com/andrewpozdnakov7-jpg/VIctory/commit/42fc8db51e10391a74ac6def8b46c24faf9c308e) also adds readbacks for both `BOUND_CTRL` values with `FI=1` and the intact Wave64 reduction. The DXIL validator checks each positive shader before dispatch. Three app-local WARP runs read the exact prefix sums from 1 through 528 in all 32 lanes for each `FI=1` variant. The intact Wave64 case read 2080 in all 64 lanes. Both `FI=0` forms under narrowed EXEC and the clobbered Wave64 mask still fail translation in the same fixture. The affected wave, lowering, semantics and IR tests had 86 passed and one ignored. Strict Clippy in both configurations and the asset policy check passed. @fuzeofficlal, the stronger fixture is ready to take if you want to keep all three positive readbacks in the PR. Please retain the Git author or use `Co-authored-by: andrewpozdnakov7-jpg <[email protected]>` if folding it into another commit.
fuzeofficlal commented 2026-11-26 22:49:33 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T22:49:33Z.


Thanks @andrewpozdnakov7-jpg — cherry-picked 42fc8db5 onto this branch as 9be9a097 (preserving your commit authorship, with only the 1-line rustfmt --edition 2024 wrap on let host = ... at wave.rs:1402).

Verified locally:

  • rustfmt --edition 2024 --check crates/victory-shader/src/emit/wave.rs: clean
  • cargo clippy --locked -p victory-shader --all-targets -- -D warnings & xtask asset policy: clean
  • cargo test --locked -p victory-shader --lib emit::wave:: on RTX 5090 (VICTORY_WARP=0, WaveLaneCount=32..32: both FI=1 BOUND_CTRL={0,1} readbacks pass all 32 lanes; Wave64 validates DXIL and skips readback cleanly) and WARP (VICTORY_WARP=1, lanes=4..4: all 3 validate DXIL cleanly).
<!-- greyfinch-sync:Marvisak/VIctory:comment:7205017129 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3393#issuecomment-7205017129). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T22:49:33Z. --- Thanks @andrewpozdnakov7-jpg — cherry-picked `42fc8db5` onto this branch as `9be9a097` (preserving your commit authorship, with only the 1-line `rustfmt --edition 2024` wrap on `let host = ...` at `wave.rs:1402`). Verified locally: - `rustfmt --edition 2024 --check crates/victory-shader/src/emit/wave.rs`: clean - `cargo clippy --locked -p victory-shader --all-targets -- -D warnings` & `xtask` asset policy: clean - `cargo test --locked -p victory-shader --lib emit::wave::` on RTX 5090 (`VICTORY_WARP=0`, `WaveLaneCount=32..32`: both `FI=1` `BOUND_CTRL={0,1}` readbacks pass all 32 lanes; Wave64 validates DXIL and skips readback cleanly) and WARP (`VICTORY_WARP=1`, `lanes=4..4`: all 3 validate DXIL cleanly).
futurepro62 commented 2026-11-27 07:36:47 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by futurepro62 on 2026-11-27T07:36:47Z.


NVIDIA full-suite run, to go with the reports above by Dukestar69, andrewpozdnakov7-jpg, since none of them names a GPU run. Tested exact head 9be9a09 (on its base 63bcca4).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), dxil.dll from Windows SDK 10.0.26100 (the loader's default), debug.
  • cargo build --workspace --locked, then cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware: 869 passed, 0 failed, 10 ignored.
  • The new tests ran and passed: v_permlanex16_honors_exec_fi_and_bound_ctrl, wave64_whole_wave_section_tracks_full_64bit_masks_and_width, end_to_end_permlanex16_and_wave64_high_mask_do_not_miscompile.
  • --skip leaves out tests that fail on main 63bcca4 on this card too, not because of this PR: heap_direct_texture_reads_mark_their_heap_index ends the victory-shader test process with 0xc0000005 (#2571), so without the skip the rest of that crate never reports; dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views and the two stride_tests hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); gathers_at_a_level_match_the_hardware fails on lane 0 (#1744).

Not tested

  • AMD (the tests take the default adapter), a release build, the title (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7212643698 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3393#issuecomment-7212643698). Originally posted by [futurepro62](https://github.com/futurepro62) on 2026-11-27T07:36:47Z. --- NVIDIA full-suite run, to go with the reports above by Dukestar69, andrewpozdnakov7-jpg, since none of them names a GPU run. Tested exact head `9be9a09` (on its base `63bcca4`). **Tested** - NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), `dxil.dll` from Windows SDK 10.0.26100 (the loader's default), debug. - `cargo build --workspace --locked`, then `cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware`: **869 passed, 0 failed, 10 ignored**. - The new tests ran and passed: `v_permlanex16_honors_exec_fi_and_bound_ctrl`, `wave64_whole_wave_section_tracks_full_64bit_masks_and_width`, `end_to_end_permlanex16_and_wave64_high_mask_do_not_miscompile`. - `--skip` leaves out tests that fail on main `63bcca4` on this card too, not because of this PR: `heap_direct_texture_reads_mark_their_heap_index` ends the `victory-shader` test process with `0xc0000005` (#2571), so without the skip the rest of that crate never reports; `dynamic_slots_are_held_until_released`, `lists_share_a_table_and_copies_keep_their_views` and the two `stride_tests` hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); `gathers_at_a_level_match_the_hardware` fails on lane 0 (#1744). **Not tested** - AMD (the tests take the default adapter), a release build, the title (test run only). **AI use** - Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
ColorMeBoom commented 2026-11-27 13:52:08 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by ColorMeBoom on 2026-11-27T13:52:08Z.


AMD run: RX 5700 XT (RDNA1, 32–64 lanes, native Wave64 and FP64), driver 32.0.21045.29003, Windows 11 26200, Rust 1.98.1, debug. The head was merged onto main 63bcca4 and run on the native adapter (VICTORY_WARP unset), no game files. Head 9be9a09. The RTX 5070 Ti run above is 32-lane only, and the end-to-end test skips its Wave64 readback on a card that can't run Wave64.

  • end_to_end_permlanex16_and_wave64_high_mask_do_not_miscompile: Wave32 FI=1 BOUND_CTRL=0 and FI=1 BOUND_CTRL=1 read back all 32 lanes, and Wave64 intact reduction: read back all 64 lanes, so the 64-bit high-mask case ran on native Wave64 and passed.
  • v_permlanex16_honors_exec_fi_and_bound_ctrl and wave64_whole_wave_section_tracks_full_64bit_masks_and_width pass.
  • Full victory-shader suite (--features host): 305 passed, 0 failed, 3 ignored. cargo clippy -p victory-shader --all-targets --features host -- -D warnings: clean.

AI use: Claude Code (Claude Opus 5.5).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7220043881 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3393#issuecomment-7220043881). Originally posted by [ColorMeBoom](https://github.com/ColorMeBoom) on 2026-11-27T13:52:08Z. --- **AMD run: RX 5700 XT** (RDNA1, 32–64 lanes, native Wave64 and FP64), driver 32.0.21045.29003, Windows 11 26200, Rust 1.98.1, debug. The head was merged onto `main` `63bcca4` and run on the native adapter (`VICTORY_WARP` unset), no game files. Head `9be9a09`. The RTX 5070 Ti run above is 32-lane only, and the end-to-end test skips its Wave64 readback on a card that can't run Wave64. - `end_to_end_permlanex16_and_wave64_high_mask_do_not_miscompile`: `Wave32 FI=1 BOUND_CTRL=0` and `FI=1 BOUND_CTRL=1` read back all 32 lanes, and **`Wave64 intact reduction: read back all 64 lanes`**, so the 64-bit high-mask case ran on native Wave64 and passed. - `v_permlanex16_honors_exec_fi_and_bound_ctrl` and `wave64_whole_wave_section_tracks_full_64bit_masks_and_width` pass. - Full `victory-shader` suite (`--features host`): **305 passed, 0 failed, 3 ignored**. `cargo clippy -p victory-shader --all-targets --features host -- -D warnings`: clean. AI use: Claude Code (Claude Opus 5.5).
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/3393:upstream-pr/3393'
git switch 'upstream-pr/3393'

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