shader: lower lane-mask phis and wave32 s_movk/s_cmovk masks read as data #528

Open
opened 2026-11-28 00:35:37 +00:00 by github-bot · 3 comments
Owner

@⁠Sheffy007 on GitHub · 2026-11-28 00:31 UTC · original

Problem

In world runs on 86cefd22 (#525), 32 shader refusals fail with:
"a lane mask holding lanes elsewhere read as data: a program input"
and 5 fail with:
"a lane mask holding lanes elsewhere read as data: written by a scalar instruction of the other width".

Root causes in crates/victory-shader/src/emit/lower.rs and crates/victory-shader/src/structure.rs:

  1. Lane-mask Phi nodes misclassified as INPUT in Lower::mask_number: When a lane-mask Phi lo at block b is not in st.active and b is not a uniform_join (so fill_phis moves lo into bad), reading lo as data (s_bcnt1, v_mbcnt, s_andn2 SCC, etc.) calls mask_number(lo), which looks up lo in p.blocks[b].insts and fails with INPUT ("a program input") because lo is defined by p.blocks[b].phis[i].
  2. Accumulated / unchanged / post-loop lane-mask phis:
    • Unchanged mask phi webs (s_mov, xored_back swaps, restored spills around a loop) hold the single dominating leaf mask's number on every iteration.
    • At wave-converged blocks (once_blocks / wave_blocks / lockstep, such as loop-exit and post-branch reconvergence blocks), every lane of the wave is present with its exact per-lane Atom::Phi bit in st.masks[&lo], so wide_ballot at b reconstructs the exact console scalar mask.
    • At single-preheader, single-latch loop headers whose phi is only read within the loop body (e.g. an accumulated exited-lanes mask updated at the latch), all lanes on any iteration arrive along the same edge, so fill_phis can carry the i32 mask number across the back-edge.
    • Lanes::exec_copies_at_joins only checked one-step s_mov from EXEC or _wrexec_, missing copy chains / swaps / s_mov exec, s and nested loop EXEC shadow phis.
  3. s_movk_i32 / s_cmovk_i32 in mask_number: Lanes::sop tracks s_movk_i32 and s_cmovk_i32 in st.masks for wave32, while Lower::mask_number stripped only _b32/_b64 and rejected them with WIDTH.

Proposed Fix

  • Extend exec_copies_at_joins to follow copy/swap/spill chains on both operands and coinductive nested loop EXEC shadow phis.
  • Lower lane-mask phis read as data via dominating unchanged leaves, wave-converged wide_ballots, and single-preheader/single-latch loop-internal data_phis (reporting "a divergent join" rather than "a program input" if a phi genuinely diverges inside a loop).
  • Lower wave32 s_movk_i32 and s_cmovk_i32 in Lower::mask_number (limiting MaskData::Host to 0 / -1).
  • Add unit, negative, and Metal GPU end-to-end value tests.

Refs #4, #258, #525

**@⁠Sheffy007** on GitHub · 2026-11-28 00:31 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/524) ## Problem In world runs on `86cefd22` (#525), 32 shader refusals fail with: `"a lane mask holding lanes elsewhere read as data: a program input"` and 5 fail with: `"a lane mask holding lanes elsewhere read as data: written by a scalar instruction of the other width"`. Root causes in `crates/victory-shader/src/emit/lower.rs` and `crates/victory-shader/src/structure.rs`: 1. **Lane-mask `Phi` nodes misclassified as `INPUT` in `Lower::mask_number`:** When a lane-mask `Phi` `lo` at block `b` is not in `st.active` and `b` is not a `uniform_join` (so `fill_phis` moves `lo` into `bad`), reading `lo` as data (`s_bcnt1`, `v_mbcnt`, `s_andn2` SCC, etc.) calls `mask_number(lo)`, which looks up `lo` in `p.blocks[b].insts` and fails with `INPUT` (`"a program input"`) because `lo` is defined by `p.blocks[b].phis[i]`. 2. **Accumulated / unchanged / post-loop lane-mask phis:** - Unchanged mask phi webs (`s_mov`, `xored_back` swaps, `restored` spills around a loop) hold the single dominating leaf mask's number on every iteration. - At wave-converged blocks (`once_blocks` / `wave_blocks` / `lockstep`, such as loop-exit and post-branch reconvergence blocks), every lane of the wave is present with its exact per-lane `Atom::Phi` bit in `st.masks[&lo]`, so `wide_ballot` at `b` reconstructs the exact console scalar mask. - At single-preheader, single-latch loop headers whose phi is only read within the loop body (e.g. an accumulated exited-lanes mask updated at the latch), all lanes on any iteration arrive along the same edge, so `fill_phis` can carry the `i32` mask number across the back-edge. - `Lanes::exec_copies_at_joins` only checked one-step `s_mov` from `EXEC` or `_wrexec_`, missing copy chains / swaps / `s_mov exec, s` and nested loop `EXEC` shadow phis. 3. **`s_movk_i32` / `s_cmovk_i32` in `mask_number`:** `Lanes::sop` tracks `s_movk_i32` and `s_cmovk_i32` in `st.masks` for wave32, while `Lower::mask_number` stripped only `_b32`/`_b64` and rejected them with `WIDTH`. ## Proposed Fix - Extend `exec_copies_at_joins` to follow copy/swap/spill chains on both operands and coinductive nested loop `EXEC` shadow phis. - Lower lane-mask phis read as data via dominating unchanged leaves, wave-converged `wide_ballot`s, and single-preheader/single-latch loop-internal `data_phis` (reporting `"a divergent join"` rather than `"a program input"` if a phi genuinely diverges inside a loop). - Lower wave32 `s_movk_i32` and `s_cmovk_i32` in `Lower::mask_number` (limiting `MaskData::Host` to `0` / `-1`). - Add unit, negative, and Metal GPU end-to-end value tests. Refs #4, #258, #525
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-28 00:35:38 +00:00
Author
Owner

@⁠Sheffy007 on GitHub · 2026-11-28 00:32 UTC · original

/claim

**@⁠Sheffy007** on GitHub · 2026-11-28 00:32 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/524#issuecomment-7230988953) /claim
Author
Owner

@⁠github-actions[bot] on GitHub · 2026-11-28 00:32 UTC · original

Claimed by @⁠Sheffy007. Open a draft pull request with Refs #524 in its description within 24 hours, or the claim expires.

**@⁠github-actions[bot]** on GitHub · 2026-11-28 00:32 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/524#issuecomment-7230991365) Claimed by @⁠Sheffy007. Open a draft pull request with `Refs #524` in its description within 24 hours, or the claim expires.
Author
Owner

@⁠AnastasisKon on GitHub · 2026-11-28 13:56 UTC · original

Evidence for the class, measured on the GPU (fork AnastasisKon/Native-Victory-macOS main): a lane-mask join already answers per wave in lockstep's paired form (VICTORY_PAIRED_WAVES=1), so the refusal may not need a fast-path lowering at all.

New test victory-gpu split_wave_tests::a_lane_mask_join_read_as_data_answers_per_wave_in_the_paired_form runs one of the join programs the fast path refuses (a_mask_join_read_by_an_or_with_exec_is_not_taken_as_anded_off: a mask loaded into s[8:9] under one EXEC, a v_cmp into it under another, then s_or_b64 and s_and_b64 with EXEC) in a group of two console waves, 128 threads:

  • wave 0 (lanes 0..63): s[8:9] | exec is lanes 0..19, s[8:9] & exec lanes 0..2, so the or'd v_cndmasks store 3 in lanes 0..2, 1 in 3..19, 0 above - the console's answer;
  • wave 1 (lanes 64..127): every compare (n > v0 for n ≤ 50) is empty, so both joins stay empty through the branches it takes and it stores 0 in all its lanes.

That is per wave, not per lane: the fast path's per-lane answer would spread the same bits over both halves. It also means the class needs the paired form rather than if-conversion in the fast path, which is the recourse docs/native-host.md and docs/ROADMAP.md now name.

Nothing here changes the fast path, so it does not conflict with the phi/s_movk work this issue is about; it is the measurement that says how much of the class is still open once the paired form is on. For reference, VICTORY_PAIRED_WAVES=1 cargo test -p victory-gpu makes twelve refusal pins in split_wave_tests translate instead (they assert today's default), which is the size of the class in the test suite.

**@⁠AnastasisKon** on GitHub · 2026-11-28 13:56 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/524#issuecomment-7240293155) Evidence for the class, measured on the GPU (fork `AnastasisKon/Native-Victory-macOS` `main`): **a lane-mask join already answers per wave in lockstep's paired form** (`VICTORY_PAIRED_WAVES=1`), so the refusal may not need a fast-path lowering at all. New test `victory-gpu` `split_wave_tests::a_lane_mask_join_read_as_data_answers_per_wave_in_the_paired_form` runs one of the join programs the fast path refuses (`a_mask_join_read_by_an_or_with_exec_is_not_taken_as_anded_off`: a mask loaded into `s[8:9]` under one EXEC, a `v_cmp` into it under another, then `s_or_b64` and `s_and_b64` with EXEC) in a group of two console waves, 128 threads: - wave 0 (lanes 0..63): `s[8:9] | exec` is lanes 0..19, `s[8:9] & exec` lanes 0..2, so the or'd `v_cndmask`s store 3 in lanes 0..2, 1 in 3..19, 0 above - the console's answer; - wave 1 (lanes 64..127): every compare (`n > v0` for n ≤ 50) is empty, so both joins stay empty through the branches it takes and it stores 0 in all its lanes. That is per wave, not per lane: the fast path's per-lane answer would spread the same bits over both halves. It also means the class needs the paired form rather than if-conversion in the fast path, which is the recourse `docs/native-host.md` and `docs/ROADMAP.md` now name. Nothing here changes the fast path, so it does not conflict with the phi/`s_movk` work this issue is about; it is the measurement that says how much of the class is still open once the paired form is on. For reference, `VICTORY_PAIRED_WAVES=1 cargo test -p victory-gpu` makes twelve refusal pins in `split_wave_tests` translate instead (they assert today's default), which is the size of the class in the test suite.
This discussion has been locked. Commenting is limited to contributors.
No description provided.