shader: refuse lane masks a divergent loop's passes change, keep OR-accumulated ones (#257, part one) #329

Open
gagarinyury wants to merge 1 commit from upstream-pr/329 into main
gagarinyury commented 2026-11-27 12:06:39 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by gagarinyury on 2026-11-27T12:06:39Z.


What this changes

Refs #257 (part one of splitting #271, as agreed there)

A lane mask read after a divergent loop is refused ("a loop-carried lane mask read after a divergent loop") unless no pass changes it, or each pass only ORs into it EXEC or a compare made in that pass (both 0 for lanes that have left), with EXEC never widened inside the loop. Otherwise a lane that left on an earlier pass reads the mask as it was on its exit pass, not the wave's final mask (#257: lane 0 reads 0 where the console gives 1). One-wave compute retries in lockstep.

The accept rule is the one main's two loop-mask tests need (a_mask_of_the_lanes_leaving_a_loop_by_a_uniform_exit_holds_them, the shadow pass, and a_mask_a_32_lane_wave_starts_as_a_pairs_constant_holds_the_lanes_ored_in, the G-buffer's 32-lane programs); the refusal without it breaks both. The other accept rules (s_cselect/s_cmov, AND/ANDN2/XOR, joins in the loop, xor copies, restored EXEC) are part two, #271. The hang fix of #271 (memoized walks) is in here.

How it was tested

  • split_wave_tests with Metal Shader Converter 4.0 beta 3 (GPU execution, no skips): 35 passed, including main's two loop-mask tests and a_loop_carried_mask_changing_exited_lanes_runs_in_lockstep (the #257 shape: lanes 0..47 read 1, lanes 48..63 read 0; on main all 64 read 0).
  • cargo test -p victory-shader (332 passed), cargo clippy --no-deps -p victory-shader --all-targets -- -D warnings, cargo xtask policy.
  • Not run here: the corpus (I have no dump) and the Windows-only tests. No game run.

Checklist

  • No game files, game code, disassembly listings or SDK files are included
  • Guesses about undocumented console behaviour are marked in the code and in docs/native-host.md (none new)
  • New diagnostics are behind a VICTORY_* variable and documented (none added)
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:5526336503 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/329). Originally posted by [gagarinyury](https://github.com/gagarinyury) on 2026-11-27T12:06:39Z. --- ## What this changes Refs #257 (part one of splitting #271, as agreed there) A lane mask read after a divergent loop is refused ("a loop-carried lane mask read after a divergent loop") unless no pass changes it, or each pass only ORs into it EXEC or a compare made in that pass (both 0 for lanes that have left), with EXEC never widened inside the loop. Otherwise a lane that left on an earlier pass reads the mask as it was on its exit pass, not the wave's final mask (#257: lane 0 reads 0 where the console gives 1). One-wave compute retries in lockstep. The accept rule is the one main's two loop-mask tests need (`a_mask_of_the_lanes_leaving_a_loop_by_a_uniform_exit_holds_them`, the shadow pass, and `a_mask_a_32_lane_wave_starts_as_a_pairs_constant_holds_the_lanes_ored_in`, the G-buffer's 32-lane programs); the refusal without it breaks both. The other accept rules (`s_cselect`/`s_cmov`, AND/ANDN2/XOR, joins in the loop, xor copies, restored EXEC) are part two, #271. The hang fix of #271 (memoized walks) is in here. ## How it was tested - `split_wave_tests` with Metal Shader Converter 4.0 beta 3 (GPU execution, no skips): 35 passed, including main's two loop-mask tests and `a_loop_carried_mask_changing_exited_lanes_runs_in_lockstep` (the #257 shape: lanes 0..47 read 1, lanes 48..63 read 0; on main all 64 read 0). - `cargo test -p victory-shader` (332 passed), `cargo clippy --no-deps -p victory-shader --all-targets -- -D warnings`, `cargo xtask policy`. - Not run here: the corpus (I have no dump) and the Windows-only tests. No game run. ## Checklist - [x] No game files, game code, disassembly listings or SDK files are included - [x] Guesses about undocumented console behaviour are marked in the code and in docs/native-host.md (none new) - [x] New diagnostics are behind a `VICTORY_*` variable and documented (none added)
Olima34 commented 2026-11-27 13:06:05 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-27T13:06:05Z.


Since #304 merged (main c5d7f4d0), this branch conflicts with main in crates/victory-gpu/src/native/split_wave_tests.rs, on the tests appended at the end of the file. @gagarinyury, could you rebase it on main? Then I'll sweep it by output (boot-time set of 4,921 containers and the 4,129 refused at boot, per stage), counting how many of the maintainer's 503 shaders part one alone keeps refused, and post the table here.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7219056946 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/329#issuecomment-7219056946). Originally posted by [Olima34](https://github.com/Olima34) on 2026-11-27T13:06:05Z. --- Since #304 merged (main `c5d7f4d0`), this branch conflicts with main in `crates/victory-gpu/src/native/split_wave_tests.rs`, on the tests appended at the end of the file. @gagarinyury, could you rebase it on main? Then I'll sweep it by output (boot-time set of 4,921 containers and the 4,129 refused at boot, per stage), counting how many of the maintainer's 503 shaders part one alone keeps refused, and post the table here.
AnastasisKon commented 2026-11-27 13:27:17 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by AnastasisKon on 2026-11-27T13:27:17Z.


Verified by running the branch: full cargo test -p victory-shader green (332 passed, 3 ignored, x86_64-unknown-linux-gnu, rustc 1.98.1). The read-site hooks are the part I checked hardest, since a refusal rule is only as good as its coverage: mask_dword_at, uniform_scc, uniform_bits, mask_scc, both opaque-atom paths, EXEC-written SOPs, v_cndmask_b32 selects, and the addcarry carry-in all call check_loop_mask — I could not find a lane-mask-as-number read path that escapes it. The proof structure (loop-invariant masks dominate-check; otherwise trace_preserved_phi per predecessor with the exec_unwidened and wave_wide guards) matches the stated rule, and the memoization (loop_mask_ok, bounded walks) is what #271's hang needed. The LOOP_MASK_EXIT refusal propagating even for dead results (effect || why == LOOP_MASK_EXIT) is the right conservatism.

One open item before merge, in my view: the author states the corpus was not run (no dump). Since this is part one of replacing #271's blanket refusal, a before/after sweep with the #286 tool would show both directions — how many shaders stop being wrongly accepted (the #257 bug, the win) and how many previously-translating shaders now refuse (the risk that black something out). #328's author just demonstrated exactly this workflow on their branch; the same table here would make the trade visible. The split-wave GPU suite (35 passed including the two main loop-mask tests and the #257 shape) covers correctness of what still translates — this ask is only about counting what doesn't.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7219496980 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/329#issuecomment-7219496980). Originally posted by [AnastasisKon](https://github.com/AnastasisKon) on 2026-11-27T13:27:17Z. --- Verified by running the branch: full cargo test -p victory-shader green (332 passed, 3 ignored, x86_64-unknown-linux-gnu, rustc 1.98.1). The read-site hooks are the part I checked hardest, since a refusal rule is only as good as its coverage: mask_dword_at, uniform_scc, uniform_bits, mask_scc, both opaque-atom paths, EXEC-written SOPs, v_cndmask_b32 selects, and the addcarry carry-in all call check_loop_mask — I could not find a lane-mask-as-number read path that escapes it. The proof structure (loop-invariant masks dominate-check; otherwise trace_preserved_phi per predecessor with the exec_unwidened and wave_wide guards) matches the stated rule, and the memoization (loop_mask_ok, bounded walks) is what #271's hang needed. The LOOP_MASK_EXIT refusal propagating even for dead results (effect || why == LOOP_MASK_EXIT) is the right conservatism. One open item before merge, in my view: the author states the corpus was not run (no dump). Since this is part one of replacing #271's blanket refusal, a before/after sweep with the #286 tool would show both directions — how many shaders stop being wrongly accepted (the #257 bug, the win) and how many previously-translating shaders now refuse (the risk that black something out). #328's author just demonstrated exactly this workflow on their branch; the same table here would make the trade visible. The split-wave GPU suite (35 passed including the two main loop-mask tests and the #257 shape) covers correctness of what still translates — this ask is only about counting what doesn't.
LakshanSivakumar commented 2026-11-27 16:01:31 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T16:01:31Z.


Leaving this open. Two problems on the current head:

  • loop_invariant_mask accepts any v_cmp made inside the divergent loop as safe to read after the exit. On the console, later passes write 0 into the bits of lanes that already left; on the host each lane keeps its own last compare. A small loop (v_cmp_le_u32 vcc, s_andn2 exec, exec, vcc, s_cbranch_execnz, then v_cndmask on vcc after the loop) translates with this PR. Taking that accept out fixes it.
  • Merged with main, it refuses main's test mask_tests::a_loop_register_that_is_exec_on_every_edge_is_exec_at_the_head (a world lighting shape from #258) with "a loop-carried lane mask read after a divergent loop". It needs to let that shape through or show main's output is wrong there.
    I have not run a corpus sweep or Metal test of it.
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7222764810 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/329#issuecomment-7222764810). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-27T16:01:31Z. --- Leaving this open. Two problems on the current head: - `loop_invariant_mask` accepts any v_cmp made inside the divergent loop as safe to read after the exit. On the console, later passes write 0 into the bits of lanes that already left; on the host each lane keeps its own last compare. A small loop (`v_cmp_le_u32 vcc`, `s_andn2 exec, exec, vcc`, `s_cbranch_execnz`, then `v_cndmask` on vcc after the loop) translates with this PR. Taking that accept out fixes it. - Merged with main, it refuses main's test `mask_tests::a_loop_register_that_is_exec_on_every_edge_is_exec_at_the_head` (a world lighting shape from #258) with "a loop-carried lane mask read after a divergent loop". It needs to let that shape through or show main's output is wrong there. I have not run a corpus sweep or Metal test of it.
gagarinyury commented 2026-11-27 16:17:59 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by gagarinyury on 2026-11-27T16:17:59Z.


Rebased onto origin/main (be7dc1b5) in 056ac63a (commit message and docs/native-host.md updated) and addressed the review points:

  1. Removed in-loop v_cmp from loop_invariant_mask (@LakshanSivakumar): removed loop_invariant_mask so v_cmp inside a divergent loop is no longer treated as loop-invariant after exit, and added negative case neg_in_loop_v_cmp inside a_loop_carried_mask_changing_exited_lanes_is_refused_after_divergent_loop (v_cmp_le_u32 vcc_lo, 32, v3, s_andn2_b32 exec_lo, exec_lo, vcc_lo, s_cbranch_execnz, v_cndmask_b32 after loop -> LOOP_MASK_EXIT).
  2. Fixed mask_tests::a_loop_register_that_is_exec_on_every_edge_is_exec_at_the_head and hardened saved_in_loop (@LakshanSivakumar): in exec_unwidened::saved_in_loop, accepted loop phis in st.active (exec_copies_at_joins) and monotone s_and/s_andn2 narrowings inside the loop while restricting single-operand scalar copies strictly to s_mov_b32/s_mov_b64 (so s_not/s_wqm/s_brev cannot pass, guarded by neg_exec_widens_s_not inside the same test).
  3. Resolved rebase conflicts on main (@Olima34): resolved split_wave_tests.rs and lower.rs on be7dc1b5.
  4. Tests & offline corpus sweep on 056ac63a (@AnastasisKon):
    • cargo clippy --no-deps -p victory-shader --all-targets --locked -- -D warnings (passed on macOS host and --target x86_64-pc-windows-msvc)
    • cargo test -p victory-shader --locked (383 passed; 0 failed; 3 ignored unit tests + 5 passed integration tests, including a_loop_register_that_is_exec_on_every_edge_is_exec_at_the_head, neg_in_loop_v_cmp, and neg_exec_widens_s_not)
    • cargo test -p victory-gpu --lib split_wave_tests (47 passed; 0 failed with Metal Shader Converter installed)
    • cargo xtask policy (asset policy: 390 files ok)
    • Offline dump sweep (25,985 unique containers from local dump, Options { wave32_host: true, zero_copy_memory: true, .. }):
      • be7dc1b5: 25,669 ok / 316 refused
      • 056ac63a: 25,596 ok / 389 refused
      • Newly refused: 73 (Cs: 73, all "a loop-carried lane mask read after a divergent loop is not supported", 0 Ps / 0 Vs / 0 Ms) — identical to the 73 count before removing the in-loop v_cmp accept (none of the 25,985 corpus shaders used that rule)
      • Newly accepted: 0
      • Changed DXIL output (ok -> ok): 1 Cs (fell back to lockstep)
        (Runtime world-run shaders aren't covered by this static dump sweep, so ready for @Olima34 / @LakshanSivakumar's boot/world corpus check.)
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7223094877 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/329#issuecomment-7223094877). Originally posted by [gagarinyury](https://github.com/gagarinyury) on 2026-11-27T16:17:59Z. --- Rebased onto `origin/main` (`be7dc1b5`) in `056ac63a` (commit message and `docs/native-host.md` updated) and addressed the review points: 1. **Removed in-loop `v_cmp` from `loop_invariant_mask`** (@LakshanSivakumar): removed `loop_invariant_mask` so `v_cmp` inside a divergent loop is no longer treated as loop-invariant after exit, and added negative case `neg_in_loop_v_cmp` inside `a_loop_carried_mask_changing_exited_lanes_is_refused_after_divergent_loop` (`v_cmp_le_u32 vcc_lo, 32, v3`, `s_andn2_b32 exec_lo, exec_lo, vcc_lo`, `s_cbranch_execnz`, `v_cndmask_b32` after loop -> `LOOP_MASK_EXIT`). 2. **Fixed `mask_tests::a_loop_register_that_is_exec_on_every_edge_is_exec_at_the_head` and hardened `saved_in_loop`** (@LakshanSivakumar): in `exec_unwidened::saved_in_loop`, accepted loop phis in `st.active` (`exec_copies_at_joins`) and monotone `s_and`/`s_andn2` narrowings inside the loop while restricting single-operand scalar copies strictly to `s_mov_b32`/`s_mov_b64` (so `s_not`/`s_wqm`/`s_brev` cannot pass, guarded by `neg_exec_widens_s_not` inside the same test). 3. **Resolved rebase conflicts on `main`** (@Olima34): resolved `split_wave_tests.rs` and `lower.rs` on `be7dc1b5`. 4. **Tests & offline corpus sweep on `056ac63a`** (@AnastasisKon): - `cargo clippy --no-deps -p victory-shader --all-targets --locked -- -D warnings` (passed on macOS host and `--target x86_64-pc-windows-msvc`) - `cargo test -p victory-shader --locked` (`383 passed; 0 failed; 3 ignored` unit tests + `5 passed` integration tests, including `a_loop_register_that_is_exec_on_every_edge_is_exec_at_the_head`, `neg_in_loop_v_cmp`, and `neg_exec_widens_s_not`) - `cargo test -p victory-gpu --lib split_wave_tests` (`47 passed; 0 failed` with Metal Shader Converter installed) - `cargo xtask policy` (`asset policy: 390 files ok`) - **Offline dump sweep (`25,985` unique containers from local dump, `Options { wave32_host: true, zero_copy_memory: true, .. }`)**: - `be7dc1b5`: `25,669 ok / 316 refused` - `056ac63a`: `25,596 ok / 389 refused` - Newly refused: **`73` (`Cs: 73`**, all `"a loop-carried lane mask read after a divergent loop is not supported"`, **`0 Ps / 0 Vs / 0 Ms`**) — identical to the `73` count before removing the in-loop `v_cmp` accept (none of the `25,985` corpus shaders used that rule) - Newly accepted: `0` - Changed DXIL output (`ok -> ok`): **`1 Cs`** (fell back to lockstep) *(Runtime world-run shaders aren't covered by this static dump sweep, so ready for @Olima34 / @LakshanSivakumar's boot/world corpus check.)*
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/329:upstream-pr/329'
git switch 'upstream-pr/329'

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