Shader: ignore guarded or modified v_mov_b32 in Lower::konst #2446

Merged
Texbirds merged 1 commit from upstream-pr/2448 into main 2026-11-25 21:56:58 +00:00
Texbirds commented 2026-11-25 13:39:27 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Texbirds on 2026-11-25T13:39:27Z.


Problem

In crates/victory-shader/src/emit/lower.rs, Lower::konst() (used by dead_on_edge() via compared() to prove branch conditions constant and discard dead incoming phi edges) treats any v_mov_b32 with sdwa.is_none() && dpp.is_none() as unconditionally producing its raw w.src[0] constant bits.

That misses two cases already handled elsewhere in lower.rs:

  1. Per-lane EXEC guard (self.x.st.guards[b][i].is_some(), cf. whole() at line 8574): a guarded v_mov_b32 v1, 0 only writes 0 in active lanes; inactive lanes preserve their previous register value, so v1 is not unconditionally 0.
  2. VOP3 float modifiers (w.abs != 0 || w.neg != 0 || w.clamp || w.omod != 0, cf. v_mov_b32 lowering at line 5636): v_mov_b32_e64 v1, -(1.0) writes -1.0f (0xBF80_0000), not the unmodified source 1.0f (0x3F80_0000).

In either case, dead_on_edge() can falsely prove a branch condition constant and drop a live incoming phi edge.

Change

  • Add Lower::def_site(v) returning (block, inst_index, &Inst) (with def_inst delegating to it).
  • In Lower::konst(), return None when self.x.st.guards[b][i].is_some(), and require w.abs == 0 && w.neg == 0 && !w.clamp && w.omod == 0 in addition to w.sdwa.is_none() && w.dpp.is_none() for v_mov_b32.
  • Unguarded, unmodified v_mov_b32, s_mov_b32, s_movk_i32, and compared() are unchanged.

Test

guarded_or_modified_v_mov_b32_is_not_folded_as_constant (synthetic GFX10 compute shader words following a_value_no_lane_from_its_edge_reads_needs_no_form, cross-platform, no game data):

  • Guarded v_mov_b32: v1 is initialized to -1, then v_mov_b32 v1, 0 runs under v_cmpx_eq_u32 exec, 0, v0. Inactive lanes keep -1, pass the join compare v_cmp_eq_u32 vcc_lo, -1, v1, and read s6 (s_memtime, unsupported in DXIL). Asserts lowering refuses it identically to a direct read (on main, dead_on_edge falsely folds v1 to 0 and accepts it). Replacing the guard with s_nop verifies unguarded v_mov_b32 still folds and lowers.
  • Negated v_mov_b32_e64: v_mov_b32_e64 v1, -(1.0) ([0xD581_0001, 0x2000_00F2]) writes -1.0f, matching v_cmp_eq_u32 vcc_lo, -1.0, v1 so s6 is live. Asserts lowering refuses it identically to a direct read (on main, konst ignores neg and folds 1.0f != -1.0f). Clearing the neg bit (0x0000_00F2) verifies unmodified v_mov_b32_e64 still folds and lowers.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6605783952 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2448). Originally posted by [Texbirds](https://github.com/Texbirds) on 2026-11-25T13:39:27Z. --- ### Problem In `crates/victory-shader/src/emit/lower.rs`, `Lower::konst()` (used by `dead_on_edge()` via `compared()` to prove branch conditions constant and discard dead incoming phi edges) treats any `v_mov_b32` with `sdwa.is_none() && dpp.is_none()` as unconditionally producing its raw `w.src[0]` constant bits. That misses two cases already handled elsewhere in `lower.rs`: 1. **Per-lane EXEC guard (`self.x.st.guards[b][i].is_some()`, cf. `whole()` at line 8574):** a guarded `v_mov_b32 v1, 0` only writes `0` in active lanes; inactive lanes preserve their previous register value, so `v1` is not unconditionally `0`. 2. **VOP3 float modifiers (`w.abs != 0 || w.neg != 0 || w.clamp || w.omod != 0`, cf. `v_mov_b32` lowering at line 5636):** `v_mov_b32_e64 v1, -(1.0)` writes `-1.0f` (`0xBF80_0000`), not the unmodified source `1.0f` (`0x3F80_0000`). In either case, `dead_on_edge()` can falsely prove a branch condition constant and drop a live incoming phi edge. ### Change - Add `Lower::def_site(v)` returning `(block, inst_index, &Inst)` (with `def_inst` delegating to it). - In `Lower::konst()`, return `None` when `self.x.st.guards[b][i].is_some()`, and require `w.abs == 0 && w.neg == 0 && !w.clamp && w.omod == 0` in addition to `w.sdwa.is_none() && w.dpp.is_none()` for `v_mov_b32`. - Unguarded, unmodified `v_mov_b32`, `s_mov_b32`, `s_movk_i32`, and `compared()` are unchanged. ### Test `guarded_or_modified_v_mov_b32_is_not_folded_as_constant` (synthetic GFX10 compute shader words following `a_value_no_lane_from_its_edge_reads_needs_no_form`, cross-platform, no game data): - **Guarded `v_mov_b32`:** `v1` is initialized to `-1`, then `v_mov_b32 v1, 0` runs under `v_cmpx_eq_u32 exec, 0, v0`. Inactive lanes keep `-1`, pass the join compare `v_cmp_eq_u32 vcc_lo, -1, v1`, and read `s6` (`s_memtime`, unsupported in DXIL). Asserts lowering refuses it identically to a direct read (on `main`, `dead_on_edge` falsely folds `v1` to `0` and accepts it). Replacing the guard with `s_nop` verifies unguarded `v_mov_b32` still folds and lowers. - **Negated `v_mov_b32_e64`:** `v_mov_b32_e64 v1, -(1.0)` (`[0xD581_0001, 0x2000_00F2]`) writes `-1.0f`, matching `v_cmp_eq_u32 vcc_lo, -1.0, v1` so `s6` is live. Asserts lowering refuses it identically to a direct read (on `main`, `konst` ignores `neg` and folds `1.0f != -1.0f`). Clearing the `neg` bit (`0x0000_00F2`) verifies unmodified `v_mov_b32_e64` still folds and lowers.
DenRakEiw commented 2026-11-25 14:07:04 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-25T13:57:18Z.

Review: APPROVED


Verified against origin/main (940d32f) on Windows 11 + NVIDIA GeForce RTX 5090 and WARP:

  • Avoiding constant-folding guarded or modified v_mov_b32 in Lower::konst prevents unsound constant propagation.
  • cargo test -p victory-shader emit::lower::tests: 22/22 passed on RTX 5090, 22/22 passed on WARP (including guarded_or_modified_v_mov_b32_is_not_folded_as_constant).
  • cargo clippy -p victory-shader --all-targets -- -D warnings: 0 warnings.
  • cargo xtask policy: 183 files ok.

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6058933468 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2448#pullrequestreview-6058933468). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-25T13:57:18Z. **Review:** APPROVED --- Verified against `origin/main` (`940d32f`) on Windows 11 + NVIDIA GeForce RTX 5090 and WARP: - Avoiding constant-folding guarded or modified `v_mov_b32` in `Lower::konst` prevents unsound constant propagation. - `cargo test -p victory-shader emit::lower::tests`: 22/22 passed on RTX 5090, 22/22 passed on WARP (including `guarded_or_modified_v_mov_b32_is_not_folded_as_constant`). - `cargo clippy -p victory-shader --all-targets -- -D warnings`: 0 warnings. - `cargo xtask policy`: 183 files ok. AI-assisted (Gemini 4 Neon).
catminou4 commented 2026-11-25 15:03:45 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T14:11:04Z.

Review: APPROVED


Tested locally: merged into main 940d32f69b, cargo test -p victory-shader --locked on macOS arm64 — 145 passed, 0 failed, 2 ignored (Windows-gated hardware execution tests not run here). Approving — guarded or modified v_mov_b32 ignored in Lower::konst.

<!-- greyfinch-sync:Marvisak/VIctory:review:6059172917 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2448#pullrequestreview-6059172917). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T14:11:04Z. **Review:** APPROVED --- Tested locally: merged into main 940d32f69b, `cargo test -p victory-shader --locked` on macOS arm64 — 145 passed, 0 failed, 2 ignored (Windows-gated hardware execution tests not run here). Approving — guarded or modified v_mov_b32 ignored in Lower::konst.
kotekompotek commented 2026-11-25 15:04:19 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T14:26:33Z.


Both holes are real miscompiles: a guarded v_mov_b32 only writes active lanes (inactive lanes keep the old value — the test's v_cmpx case shows the folded constant leaking into lanes that kept -1), and VOP3 neg/abs/clamp/omod transform the source (-(1.0) folds to the wrong sign). Checking guards at the def site plus the modifier fields is the right granularity — unguarded plain moves still fold. The test's refusal-equality trick (guarded program must fail the same way as the direct s_memtime read) is a clean way to pin liveness through the fold.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7165284079 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2448#issuecomment-7165284079). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-25T14:26:33Z. --- Both holes are real miscompiles: a guarded `v_mov_b32` only writes active lanes (inactive lanes keep the old value — the test's `v_cmpx` case shows the folded constant leaking into lanes that kept `-1`), and VOP3 `neg`/`abs`/`clamp`/`omod` transform the source (`-(1.0)` folds to the wrong sign). Checking `guards` at the def site plus the modifier fields is the right granularity — unguarded plain moves still fold. The test's refusal-equality trick (guarded program must fail the same way as the direct `s_memtime` read) is a clean way to pin liveness through the fold.
cloph-dsp commented 2026-11-25 15:04:27 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by cloph-dsp on 2026-11-25T14:49:49Z.


Independent local validation (Windows, Rust 1.100.0/MSVC, head 8f5cf05, no game data): cargo test --locked -p victory-shader --lib -> 248 passed, 0 failed, 3 ignored, exit 0; git diff --check HEAD~1 HEAD clean. The lib test count matches upstream main, so this is a compile/regression check only; I did not independently replay the synthetic guarded/modified v_mov_b32 scenario in Lower::konst. AI-assisted (Hy4 flash). GPU/driver: N/A for the CPU test run. No game data, assets or extracted content used.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7165880874 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2448#issuecomment-7165880874). Originally posted by [cloph-dsp](https://github.com/cloph-dsp) on 2026-11-25T14:49:49Z. --- Independent local validation (Windows, Rust 1.100.0/MSVC, head 8f5cf05, no game data): `cargo test --locked -p victory-shader --lib` -> 248 passed, 0 failed, 3 ignored, exit 0; `git diff --check HEAD~1 HEAD` clean. The lib test count matches upstream main, so this is a compile/regression check only; I did not independently replay the synthetic guarded/modified v_mov_b32 scenario in Lower::konst. AI-assisted (Hy4 flash). GPU/driver: N/A for the CPU test run. No game data, assets or extracted content used.
Marvisak commented 2026-11-25 16:09:55 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T16:08:16Z.


Automated triage for the maintainers.

Merge candidate.

Lower::konst (emit/lower.rs) no longer folds a v_mov_b32 whose definition is EXEC-guarded (st.guards[b][i]), or one with VOP3 abs/neg/clamp/omod. Both folds were wrong: a guarded move keeps the old value in inactive lanes, and the modifiers change the result. This matches how main already handles both: whole() checks the same guard, and the v_mov_b32 lowering goes the float path under the same modifiers. Without the fix, dead_on_edge could drop a live phi edge. I checked the test's encodings by hand (v_cmpx_eq_u32, v_cmp_eq_u32, v_mov_b32_e64 with neg at bit 29 of word 1). Each case has a control showing that the unguarded or unmodified move still folds. The new def_site helper is shared with def_inst.

CI: check passing.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7167625469 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2448#issuecomment-7167625469). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T16:08:16Z. --- <!-- victory-triage v1 head=d5f7b7b144574362a88eb2597f04e56df894588a verdict=merge-candidate at=2026-11-25T16:43:20Z --> Automated triage for the maintainers. **Merge candidate.** `Lower::konst` (`emit/lower.rs`) no longer folds a `v_mov_b32` whose definition is EXEC-guarded (`st.guards[b][i]`), or one with VOP3 `abs`/`neg`/`clamp`/`omod`. Both folds were wrong: a guarded move keeps the old value in inactive lanes, and the modifiers change the result. This matches how `main` already handles both: `whole()` checks the same guard, and the `v_mov_b32` lowering goes the float path under the same modifiers. Without the fix, `dead_on_edge` could drop a live phi edge. I checked the test's encodings by hand (`v_cmpx_eq_u32`, `v_cmp_eq_u32`, `v_mov_b32_e64` with `neg` at bit 29 of word 1). Each case has a control showing that the unguarded or unmodified move still folds. The new `def_site` helper is shared with `def_inst`. CI: `check` passing. --- _Generated by [Claude Code](https://claude.ai/code)_
Texbirds commented 2026-11-25 17:05:04 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Texbirds on 2026-11-25T16:23:12Z.


Rebased cleanly onto main (67e8ae1) in d5f7b7b (resolved the test-insertion conflict in crates/victory-shader/src/emit/lower.rs).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7167895475 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2448#issuecomment-7167895475). Originally posted by [Texbirds](https://github.com/Texbirds) on 2026-11-25T16:23:12Z. --- Rebased cleanly onto `main` (`67e8ae1`) in `d5f7b7b` (resolved the test-insertion conflict in `crates/victory-shader/src/emit/lower.rs`).
upstream-sync closed this pull request 2026-11-25 22:03:41 +00:00
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!2446
No description provided.