Shaders: compute signed overflow SCC for s_add_i32, s_addk_i32, and s_sub_i32 (port of upstream VIctory #1095) #950

Closed
Moon0755 wants to merge 1 commit from upstream-pr/951 into main
Moon0755 commented 2026-11-25 13:36:20 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by Moon0755 on 2026-11-25T13:36:20Z.


Ports upstream Marvisak/VIctory#1095 (ESAT586).

Problem

In crates/victory-shader/src/emit/lower.rs (Lower::salu), s_add_i32, s_addk_i32, and s_sub_i32 compute the 32-bit sum/difference r and return carry = None, falling through to:

None if name.starts_with("s_add") || name.starts_with("s_sub") => {
    Val::Missing("the carry of a scalar operation")
}

Meanwhile ir.rs::writes_scc already marks all three instructions as writing Reg::Scc. Any shader that reads SCC afterwards (e.g. s_cselect_b32, s_cselect_b64, or s_cbranch_scc0/1) fails lowering with Unsupported { what: "the carry of a scalar operation" }.

What changed

  • crates/victory-shader/src/emit/lower.rs (Lower::salu):
    • Compute 32-bit two's-complement signed overflow into SCC for s_add_i32, s_addk_i32, and s_sub_i32 per the AMD RDNA2 ISA (pure 32-bit DXIL integer ops without needing flags::INT64):
      • s_add_i32 / s_addk_i32: ((a ^ r) & (b ^ r)) <s 0 (a and b share a sign bit that r flips).
      • s_sub_i32: ((a ^ b) & (a ^ r)) <s 0 (a and b have opposite signs and r flips a's sign).
  • crates/victory-shader/src/emit/valu_tests.rs (signed_scalar_add_sub_scc_matches_rdna2):
    • Add helper sopk and an end-to-end synthetic compute shader test covering s_add_i32, s_sub_i32, s_addk_i32 +1, and s_addk_i32 -32768 across 30 signed-overflow edge cases (i32::MAX + 1, i32::MIN - 1, 0 - i32::MIN, -1 + 1, etc.), reading SCC back via s_cselect_b32.

Verification

Verified on Windows (windows-latest + dxil.dll + D3D12 WARP) via a temporary scoped CI branch on the fork:

  • CI run: https://github.com/Moon0755/victory-fork/actions/runs/45624774381
  • cargo clippy -p victory-shader -p xtask --all-targets --locked -- -D warnings: passed
  • cargo clippy -p victory-shader -p xtask --all-targets --all-features --locked -- -D warnings: passed
  • cargo test -p victory-shader -p xtask --locked -- --nocapture: victory-shader 216 passed; 0 failed; 3 ignored (test emit::valu_tests::signed_scalar_add_sub_scc_matches_rdna2 ... ok, validated via dxil.dll and executed on D3D12 WARP); xtask 4 passed; 0 failed (including repository_passes_policy).

Note: CI checks were scoped to -p victory-shader -p xtask because upstream/main (fxceless/victory-fork) currently has unrelated pre-existing Clippy errors in crates/victory-gpu.

— Neon

<!-- greyfinch-sync:fxceless/victory-fork:issue:6605721279 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/951). Originally posted by [Moon0755](https://github.com/Moon0755) on 2026-11-25T13:36:20Z. --- Ports upstream [Marvisak/VIctory#1095](https://github.com/Marvisak/VIctory/pull/1095) (`ESAT586`). ## Problem In `crates/victory-shader/src/emit/lower.rs` (`Lower::salu`), `s_add_i32`, `s_addk_i32`, and `s_sub_i32` compute the 32-bit sum/difference `r` and return `carry = None`, falling through to: ```rust None if name.starts_with("s_add") || name.starts_with("s_sub") => { Val::Missing("the carry of a scalar operation") } ``` Meanwhile `ir.rs::writes_scc` already marks all three instructions as writing `Reg::Scc`. Any shader that reads `SCC` afterwards (e.g. `s_cselect_b32`, `s_cselect_b64`, or `s_cbranch_scc0`/`1`) fails lowering with `Unsupported { what: "the carry of a scalar operation" }`. ## What changed - **`crates/victory-shader/src/emit/lower.rs` (`Lower::salu`)**: - Compute 32-bit two's-complement signed overflow into `SCC` for `s_add_i32`, `s_addk_i32`, and `s_sub_i32` per the AMD RDNA2 ISA (pure 32-bit DXIL integer ops without needing `flags::INT64`): - **`s_add_i32` / `s_addk_i32`:** `((a ^ r) & (b ^ r)) <s 0` (`a` and `b` share a sign bit that `r` flips). - **`s_sub_i32`:** `((a ^ b) & (a ^ r)) <s 0` (`a` and `b` have opposite signs and `r` flips `a`'s sign). - **`crates/victory-shader/src/emit/valu_tests.rs` (`signed_scalar_add_sub_scc_matches_rdna2`)**: - Add helper `sopk` and an end-to-end synthetic compute shader test covering `s_add_i32`, `s_sub_i32`, `s_addk_i32 +1`, and `s_addk_i32 -32768` across 30 signed-overflow edge cases (`i32::MAX + 1`, `i32::MIN - 1`, `0 - i32::MIN`, `-1 + 1`, etc.), reading `SCC` back via `s_cselect_b32`. ## Verification Verified on Windows (`windows-latest` + `dxil.dll` + D3D12 WARP) via a temporary scoped CI branch on the fork: - CI run: https://github.com/Moon0755/victory-fork/actions/runs/45624774381 - `cargo clippy -p victory-shader -p xtask --all-targets --locked -- -D warnings`: passed - `cargo clippy -p victory-shader -p xtask --all-targets --all-features --locked -- -D warnings`: passed - `cargo test -p victory-shader -p xtask --locked -- --nocapture`: `victory-shader` `216 passed; 0 failed; 3 ignored` (`test emit::valu_tests::signed_scalar_add_sub_scc_matches_rdna2 ... ok`, validated via `dxil.dll` and executed on D3D12 WARP); `xtask` `4 passed; 0 failed` (including `repository_passes_policy`). Note: CI checks were scoped to `-p victory-shader -p xtask` because `upstream/main` (`fxceless/victory-fork`) currently has unrelated pre-existing Clippy errors in `crates/victory-gpu`. — Neon
HAL9000RELOADED commented 2026-11-25 18:05:14 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by HAL9000RELOADED on 2026-11-25T18:00:53Z.


A hardware run of this PR's new test: signed_scalar_add_sub_scc_matches_rdna2 passes on a Radeon RX 7900 XTX (RDNA3), Windows 10 22H2, with #402's Agility SDK runtime. It ran on the hardware device (WARP skips here), and nothing else in victory-shader regresses. The setup, and the same run for eight related ports, are in one table on #1030: https://github.com/fxceless/victory-fork/pull/1030#issuecomment-7170315986

<!-- greyfinch-sync:fxceless/victory-fork:comment:7170316999 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/951#issuecomment-7170316999). Originally posted by [HAL9000RELOADED](https://github.com/HAL9000RELOADED) on 2026-11-25T18:00:53Z. --- A hardware run of this PR's new test: `signed_scalar_add_sub_scc_matches_rdna2` passes on a Radeon RX 7900 XTX (RDNA3), Windows 10 22H2, with #402's Agility SDK runtime. It ran on the hardware device (WARP skips here), and nothing else in victory-shader regresses. The setup, and the same run for eight related ports, are in one table on #1030: https://github.com/fxceless/victory-fork/pull/1030#issuecomment-7170315986
upstream-sync closed this pull request 2026-11-26 12:07:10 +00:00

Pull request closed

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
fxceless/victory-fork!950
No description provided.