Shaders: guarded scalar writes kill stale SGPRs in dead_on_edge #2466

Merged
VastyViz merged 1 commit from upstream-pr/2468 into main 2026-11-25 21:57:27 +00:00
VastyViz commented 2026-11-25 14:12:02 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by VastyViz on 2026-11-25T14:12:02Z.


Problem

In Lower::dead_on_edge (crates/victory-shader/src/emit/lower.rs), an instruction that does not read any tainted register only removed its written registers from regs when guard.is_none().

On RDNA2 (and in Lower::write_i / Lower::write_placed), EXEC guards only preserve the previous value of VGPR writes (Reg::V(_)). Scalar and SCC writes from guarded VALU instructions — such as v_readfirstlane_b32, v_readlane_b32, v_cmp_*, and carry-out SGPRs — always overwrite their destination register unconditionally. Leaving such an SGPR in regs caused dead_on_edge to reject a subsequent read of the freshly written SGPR as a live use of the earlier stale value.

Fix

  • Update Lower::dead_on_edge so writes to non-VGPR registers (!matches!(r, Reg::V(_))) always remove the register from regs regardless of guard, while guarded VGPR writes continue to keep the register tainted.
  • Add synthetic unit test emit::lower::tests::a_guarded_scalar_write_kills_a_stale_sgpr covering:
    • A stale SGPR overwritten by a guarded v_readfirstlane_b32 before being read (lowers succeeds).
    • Negative controls: removing the guarded overwrite or setting the guard flag on both paths still refuses the shader.

Verification

  • Full title shader archive sweep (26,503 containers, dxil.dll validation):
    • emit: a lane mask read as data: 8 -> 4 (all 4 affected pixel shaders now emit and validate).
    • Vertex & pixel shaders: 14,205 -> 14,209 emitted (+4), 13,338 -> 13,342 validated (+4, 100% validated), 0 regressions across all 26,084 programs.
    • Confirmed D3D12 pipeline creation on an RTX 3080 for an affected pixel shader.
  • cargo clippy --workspace --all-targets --locked -- -D warnings — passed.
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings — passed.
  • cargo test -p victory-shader --locked — a_guarded_scalar_write_kills_a_stale_sgpr passed, along with 245 passed, 0 failed, 3 ignored (excluding the 3 known NVIDIA main failures from #2317).
  • cargo xtask policy and git diff --check — passed.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6606441528 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2468). Originally posted by [VastyViz](https://github.com/VastyViz) on 2026-11-25T14:12:02Z. --- ### Problem In `Lower::dead_on_edge` (`crates/victory-shader/src/emit/lower.rs`), an instruction that does not read any tainted register only removed its written registers from `regs` when `guard.is_none()`. On RDNA2 (and in `Lower::write_i` / `Lower::write_placed`), `EXEC` guards only preserve the previous value of **VGPR** writes (`Reg::V(_)`). Scalar and SCC writes from guarded VALU instructions — such as `v_readfirstlane_b32`, `v_readlane_b32`, `v_cmp_*`, and carry-out SGPRs — always overwrite their destination register unconditionally. Leaving such an SGPR in `regs` caused `dead_on_edge` to reject a subsequent read of the freshly written SGPR as a live use of the earlier stale value. ### Fix - Update `Lower::dead_on_edge` so writes to non-VGPR registers (`!matches!(r, Reg::V(_))`) always remove the register from `regs` regardless of `guard`, while guarded VGPR writes continue to keep the register tainted. - Add synthetic unit test `emit::lower::tests::a_guarded_scalar_write_kills_a_stale_sgpr` covering: - A stale SGPR overwritten by a guarded `v_readfirstlane_b32` before being read (`lowers` succeeds). - Negative controls: removing the guarded overwrite or setting the guard flag on both paths still refuses the shader. ### Verification - Full title shader archive sweep (`26,503` containers, `dxil.dll` validation): - `emit: a lane mask read as data`: `8 -> 4` (all 4 affected pixel shaders now emit and validate). - Vertex & pixel shaders: `14,205 -> 14,209` emitted (`+4`), `13,338 -> 13,342` validated (`+4`, 100% validated), `0` regressions across all `26,084` programs. - Confirmed D3D12 pipeline creation on an RTX 3080 for an affected pixel shader. - `cargo clippy --workspace --all-targets --locked -- -D warnings` — passed. - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings` — passed. - `cargo test -p victory-shader --locked` — `a_guarded_scalar_write_kills_a_stale_sgpr` passed, along with `245 passed, 0 failed, 3 ignored` (excluding the 3 known NVIDIA `main` failures from #2317). - `cargo xtask policy` and `git diff --check` — passed.
DenRakEiw commented 2026-11-25 15:03:54 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-25T14:17:35Z.

Review: APPROVED


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

  • Treating guarded scalar writes as killing stale SGPRs in dead_on_edge is clean and passes all lower tests.
  • cargo test -p victory-shader emit::lower::tests: 22/22 passed on RTX 5090, 22/22 passed on WARP (including a_guarded_scalar_write_kills_a_stale_sgpr).
  • 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:6059295124 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2468#pullrequestreview-6059295124). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-25T14:17:35Z. **Review:** APPROVED --- Verified against `origin/main` (`940d32f`) on Windows 11 + NVIDIA GeForce RTX 5090 and WARP: - Treating guarded scalar writes as killing stale SGPRs in `dead_on_edge` is clean and passes all lower tests. - `cargo test -p victory-shader emit::lower::tests`: 22/22 passed on RTX 5090, 22/22 passed on WARP (including `a_guarded_scalar_write_kills_a_stale_sgpr`). - `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:54 +00:00 (Migrated from github.com)

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

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 scalar writes kill stale SGPRs in dead lanes.

<!-- greyfinch-sync:Marvisak/VIctory:review:6059995339 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2468#pullrequestreview-6059995339). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T14:57:32Z. **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 scalar writes kill stale SGPRs in dead lanes.
cloph-dsp commented 2026-11-25 15:04:26 +00:00 (Migrated from github.com)

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


Independent local validation (Windows, Rust 1.100.0/MSVC, head ca16e45, no game data): cargo test --locked -p victory-shader --lib -> 248 tests passed, 0 failed, 3 ignored, exit 0; git diff --check HEAD~1 HEAD clean. No new tests appeared in the lib run (same 248 as main), so this is a compile/regression check rather than validation of a new test. The synthetic dead-edge SGPR scenario in the PR was not independently replayed. AI-assisted (Hy4 flash). GPU/driver: N/A for this CPU test run (hardware/WARP shader tests skipped here). No game data, assets or extracted content used.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7165869092 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2468#issuecomment-7165869092). Originally posted by [cloph-dsp](https://github.com/cloph-dsp) on 2026-11-25T14:49:22Z. --- Independent local validation (Windows, Rust 1.100.0/MSVC, head ca16e45, no game data): `cargo test --locked -p victory-shader --lib` -> 248 tests passed, 0 failed, 3 ignored, exit 0; `git diff --check HEAD~1 HEAD` clean. No new tests appeared in the lib run (same 248 as main), so this is a compile/regression check rather than validation of a new test. The synthetic dead-edge SGPR scenario in the PR was not independently replayed. AI-assisted (Hy4 flash). GPU/driver: N/A for this CPU test run (hardware/WARP shader tests skipped here). No game data, assets or extracted content used.
Marvisak commented 2026-11-25 16:09:37 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T15:59:47Z.


Automated triage for the maintainers.

In Lower::dead_on_edge (crates/victory-shader/src/emit/lower.rs), a guarded instruction now ends the tracked stale value for the scalar registers it writes; guarded VGPR writes still keep it. That matches write_placed, which applies the guard only to Reg::V writes, and the ISA rule that VALU writes to SGPRs (v_readfirstlane_b32, v_cmp_*, carry-outs) do not depend on EXEC. I checked the key encodings in the new test (v_readfirstlane_b32 s6, v0, v_cmpx_eq_u32), and the test includes negative controls that should still be refused. CI passes.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7167499119 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2468#issuecomment-7167499119). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T15:59:47Z. --- <!-- victory-triage v1 head=ca16e454c99383417eb1e8a9770c1a1bf8b6495f verdict=merge-candidate at=2026-11-25T15:59:09Z --> Automated triage for the maintainers. In `Lower::dead_on_edge` (`crates/victory-shader/src/emit/lower.rs`), a guarded instruction now ends the tracked stale value for the scalar registers it writes; guarded VGPR writes still keep it. That matches `write_placed`, which applies the guard only to `Reg::V` writes, and the ISA rule that VALU writes to SGPRs (`v_readfirstlane_b32`, `v_cmp_*`, carry-outs) do not depend on EXEC. I checked the key encodings in the new test (`v_readfirstlane_b32 s6, v0`, `v_cmpx_eq_u32`), and the test includes negative controls that should still be refused. CI passes. --- _Generated by [Claude Code](https://claude.ai/code)_
upstream-sync closed this pull request 2026-11-25 22:03:44 +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!2466
No description provided.