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

Manually merged
github merged 1 commit from gh-pr/2448/head into gh-pr/2448/base 2026-11-26 05:06:09 +00:00
Owner

@⁠Texbirds on GitHub · 2026-11-25 13:39 UTC · original
Texbirds:fix/shader-konst-respect-guards-and-modifiers → main · +92 −8 · 1 files · 1 commits · merged 2026-11-25 21:56 UTC

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.
**@⁠Texbirds** on GitHub · 2026-11-25 13:39 UTC · [original](https://github.com/Marvisak/VIctory/pull/2448) `Texbirds:fix/shader-konst-respect-guards-and-modifiers` → `main` · +92 −8 · 1 files · 1 commits · merged 2026-11-25 21:56 UTC ### 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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:06:06 +00:00
github manually merged commit d5f7b7b144 into gh-pr/2448/base 2026-11-26 05:06:09 +00:00
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-25 14:26 UTC · original

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.

**@⁠kotekompotek** on GitHub · 2026-11-25 14:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/2448#issuecomment-7165284079) 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.
Author
Owner

@⁠cloph-dsp on GitHub · 2026-11-25 14:49 UTC · original

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.

**@⁠cloph-dsp** on GitHub · 2026-11-25 14:49 UTC · [original](https://github.com/Marvisak/VIctory/pull/2448#issuecomment-7165880874) 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.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 16:08 UTC · original · edited 2026-11-25 16:45 UTC

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

**@⁠Marvisak** on GitHub · 2026-11-25 16:08 UTC · [original](https://github.com/Marvisak/VIctory/pull/2448#issuecomment-7167625469) · edited 2026-11-25 16:45 UTC <!-- 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)_
Author
Owner

@⁠Texbirds on GitHub · 2026-11-25 16:23 UTC · original

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

**@⁠Texbirds** on GitHub · 2026-11-25 16:23 UTC · [original](https://github.com/Marvisak/VIctory/pull/2448#issuecomment-7167895475) Rebased cleanly onto `main` (`67e8ae1`) in `d5f7b7b` (resolved the test-insertion conflict in `crates/victory-shader/src/emit/lower.rs`).
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!2448
No description provided.