shader: lower 32-bit scalar ops on wave64 lane-mask halves (#423) #433

Open
Sheffy007 wants to merge 1 commit from upstream-pr/433 into main
Sheffy007 commented 2026-11-27 18:03:28 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Sheffy007 on 2026-11-27T18:03:28Z.


Summary

Lowers 32-bit scalar ALU writes to individual halves of wave64 lane masks when read as data (addressing the 9 world compute shaders hitting "a lane mask holding lanes elsewhere read as data: written by a scalar instruction of the other width"):

  • Lower::mask_dword & Lower::mask_half_number: When a wave64 mask half is written by a 1-dword scalar ALU instruction that does not touch EXEC, evaluate that half's 32-bit operation directly on its 32-bit scalar/constant operands (s_mov_b32, s_movk_i32, s_not_b32, s_wqm_b32, s_lshl_b32, s_lshr_b32, s_bitset0_b32, s_bitset1_b32, s_bfm_b32, s_brev_b32, s_quadmask_b32, s_cselect_b32, s_cmov_b32, s_cmovk_i32, and bitwise and/or/xor/andn2/orn2/andn1/orn1/nand/nor/xnor), enforcing the existing MaskData::Host wave-size safety guards (SHIFT on cross-lane/non-uniform constants and zeroing the high half on 32-lane host waves). Also models s_bitreplicate_b64_b32 in mask_number for MaskData::Console, and records mask_scc for 1-dword mask writes via is_mask.
  • Exact (lo, hi) pair tracking across partial 32-bit updates:
    • Keeps paired_other alongside paired_masks (Structured::mask_or_other) and looks up exact (lo, hi) pairs in mask_dword, v_cndmask_b32, v_*_co_ci_u32, and uniform_with_exec so updating only one 32-bit half ((lo, hi_old) -> (lo, hi_new)) does not collide on lo.
    • Carries both dwords in Atom::Opaque(lo, hi) instead of guessing pair_high(lo), and avoids marking k.0 in Structured::active when a 1-dword instruction writes only k.1.
    • Fixes an accidental s9 register collision with saved s[8:9] in a_wave64_prefix_sum_whose_rows_s_bitreplicate_and_s_bfm_select_runs.

Verification

  • Unit tests in crates/victory-shader/src/emit/mask_tests.rs (split_32bit_wave64_mask_halves_and_partial_updates_lower_as_data + existing negative tests).
  • Metal GPU compute test in crates/victory-gpu/src/native/split_wave_tests.rs (split_32bit_wave64_mask_halves_and_partial_updates_run_on_the_gpu, checking all 64 lanes across split 32-lane host waves).
  • cargo test -p victory-hle -p victory -p victory-gpu -p victory-metal -p victory-shader -p xtask
  • cargo clippy --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings
  • cargo clippy --workspace --all-targets --all-features --target x86_64-pc-windows-gnullvm -- -D warnings
  • cargo xtask policy

Closes #423
Refs #4, #258

cc @Olima34

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:5531082523 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/433). Originally posted by [Sheffy007](https://github.com/Sheffy007) on 2026-11-27T18:03:28Z. --- ## Summary Lowers 32-bit scalar ALU writes to individual halves of wave64 lane masks when read as data (addressing the 9 world compute shaders hitting `"a lane mask holding lanes elsewhere read as data: written by a scalar instruction of the other width"`): - **`Lower::mask_dword` & `Lower::mask_half_number`:** When a wave64 mask half is written by a 1-dword scalar ALU instruction that does not touch `EXEC`, evaluate that half's 32-bit operation directly on its 32-bit scalar/constant operands (`s_mov_b32`, `s_movk_i32`, `s_not_b32`, `s_wqm_b32`, `s_lshl_b32`, `s_lshr_b32`, `s_bitset0_b32`, `s_bitset1_b32`, `s_bfm_b32`, `s_brev_b32`, `s_quadmask_b32`, `s_cselect_b32`, `s_cmov_b32`, `s_cmovk_i32`, and bitwise `and`/`or`/`xor`/`andn2`/`orn2`/`andn1`/`orn1`/`nand`/`nor`/`xnor`), enforcing the existing `MaskData::Host` wave-size safety guards (`SHIFT` on cross-lane/non-uniform constants and zeroing the high half on 32-lane host waves). Also models `s_bitreplicate_b64_b32` in `mask_number` for `MaskData::Console`, and records `mask_scc` for 1-dword mask writes via `is_mask`. - **Exact `(lo, hi)` pair tracking across partial 32-bit updates:** - Keeps `paired_other` alongside `paired_masks` (`Structured::mask_or_other`) and looks up exact `(lo, hi)` pairs in `mask_dword`, `v_cndmask_b32`, `v_*_co_ci_u32`, and `uniform_with_exec` so updating only one 32-bit half (`(lo, hi_old)` -> `(lo, hi_new)`) does not collide on `lo`. - Carries both dwords in `Atom::Opaque(lo, hi)` instead of guessing `pair_high(lo)`, and avoids marking `k.0` in `Structured::active` when a 1-dword instruction writes only `k.1`. - Fixes an accidental `s9` register collision with saved `s[8:9]` in `a_wave64_prefix_sum_whose_rows_s_bitreplicate_and_s_bfm_select_runs`. ## Verification - Unit tests in `crates/victory-shader/src/emit/mask_tests.rs` (`split_32bit_wave64_mask_halves_and_partial_updates_lower_as_data` + existing negative tests). - Metal GPU compute test in `crates/victory-gpu/src/native/split_wave_tests.rs` (`split_32bit_wave64_mask_halves_and_partial_updates_run_on_the_gpu`, checking all 64 lanes across split 32-lane host waves). - `cargo test -p victory-hle -p victory -p victory-gpu -p victory-metal -p victory-shader -p xtask` - `cargo clippy --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings` - `cargo clippy --workspace --all-targets --all-features --target x86_64-pc-windows-gnullvm -- -D warnings` - `cargo xtask policy` Closes #423 Refs #4, #258 cc @Olima34
Olima34 commented 2026-11-28 00:37:13 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-28T00:37:13Z.

Review: CHANGES_REQUESTED


Thanks for this PR! We're helping the maintainer clear the queue tonight, and main has moved a lot (the preview branch landed as 86cefd22, then a batch of PRs; main is now a6a58361). This branch now conflicts with main in crates/victory-gpu/src/native/split_wave_tests.rs.

Could you merge current main into the branch (a merge commit is fine, no need to rebase) and resolve the conflict? It looks like tests appended at the same spot as tests that landed on main; keep both sides. Once GitHub shows it as mergeable and CI is green on the new head, we'll read it through and merge it if it holds up.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:review:6094669269 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/433#pullrequestreview-6094669269). Originally posted by [Olima34](https://github.com/Olima34) on 2026-11-28T00:37:13Z. **Review:** CHANGES_REQUESTED --- Thanks for this PR! We're helping the maintainer clear the queue tonight, and main has moved a lot (the preview branch landed as 86cefd22, then a batch of PRs; main is now a6a58361). This branch now conflicts with main in `crates/victory-gpu/src/native/split_wave_tests.rs`. Could you merge current `main` into the branch (a merge commit is fine, no need to rebase) and resolve the conflict? It looks like tests appended at the same spot as tests that landed on main; keep both sides. Once GitHub shows it as mergeable and CI is green on the new head, we'll read it through and merge it if it holds up.
Olima34 commented 2026-11-28 00:46:52 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-28T00:46:52Z.


The maintainer is handling this one himself; please follow his lead rather than my review.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7231186300 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/433#issuecomment-7231186300). Originally posted by [Olima34](https://github.com/Olima34) on 2026-11-28T00:46:52Z. --- The maintainer is handling this one himself; please follow his lead rather than my review.
Olima34 commented 2026-11-28 02:00:36 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-28T02:00:36Z.


Thanks for this PR! An offline check of 635c7776. The current head b607dfad has the same non-test code.

Measured offline on main 86cefd22 with examples/sweep + tools/corpus/compare.sh, on our 21,600-shader world corpus. The PR alone was swept over the 10,319 world shaders that the combination below changes. "Passes/frame" is an estimate from four traced world frames of an earlier preview run: the dropped passes whose only refused shader this PR translates.

#433 alone:

  • Newly translated: 9 compute, previously refused for "a lane mask holding lanes elsewhere read as data: written by a scalar instruction of the other width".
  • Outputs changed: 4.
  • Refused for another reason now: 10 (4 now stop at "a lane mask whose lanes depend on its own value").
  • Passes/frame: about 4.
  • Regression, 1: vertex_9aae2e2d80abafe1 translates on main and is refused with #433 for "a lane-mask join of an untracked mask: a value the translation could not make". This is the only regression in the whole combination, and it comes from #433 alone.

Merging: the only conflicts are in test files: mask_tests.rs and split_wave_tests.rs, which other PRs also append to.

In combination with #418 #466 #420 #409 #448 #433 #495 #497 on 86cefd22 (a local integration build, plus the pass-trace fix from #509):

  • World corpus: 302 shaders newly translated and 1 regression (see #433).
  • Windowed run (6 min): no GPU errors, and the GPU memory drained afterwards. About 1.4 fps in the world. 351 passes dropped per world frame, against 456 in an earlier census of the preview branch; these are different runs, so not a clean A/B.
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7232065644 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/433#issuecomment-7232065644). Originally posted by [Olima34](https://github.com/Olima34) on 2026-11-28T02:00:36Z. --- Thanks for this PR! An offline check of 635c7776. The current head b607dfad has the same non-test code. Measured offline on main 86cefd22 with `examples/sweep` + `tools/corpus/compare.sh`, on our 21,600-shader world corpus. The PR alone was swept over the 10,319 world shaders that the combination below changes. "Passes/frame" is an estimate from four traced world frames of an earlier preview run: the dropped passes whose only refused shader this PR translates. **#433 alone:** - **Newly translated:** 9 compute, previously refused for "a lane mask holding lanes elsewhere read as data: written by a scalar instruction of the other width". - **Outputs changed:** 4. - **Refused for another reason now:** 10 (4 now stop at "a lane mask whose lanes depend on its own value"). - **Passes/frame:** about 4. - **Regression, 1:** `vertex_9aae2e2d80abafe1` translates on main and is refused with #433 for "a lane-mask join of an untracked mask: a value the translation could not make". This is the only regression in the whole combination, and it comes from #433 alone. **Merging:** the only conflicts are in test files: `mask_tests.rs` and `split_wave_tests.rs`, which other PRs also append to. **In combination** with #418 #466 #420 #409 #448 #433 #495 #497 on 86cefd22 (a local integration build, plus the pass-trace fix from #509): - **World corpus:** 302 shaders newly translated and 1 regression (see #433). - **Windowed run (6 min):** no GPU errors, and the GPU memory drained afterwards. About 1.4 fps in the world. 351 passes dropped per world frame, against 456 in an earlier census of the preview branch; these are different runs, so not a clean A/B.
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/433:upstream-pr/433'
git switch 'upstream-pr/433'

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