Shaders: compute signed overflow SCC for s_add_i32, s_addk_i32, and s_sub_i32 #1095

Open
ESAT586 wants to merge 1 commit from upstream-pr/1095 into main
ESAT586 commented 2026-11-24 15:42:47 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by ESAT586 on 2026-11-24T15:42:47Z.


AI Disclosure — Generated & Verified by Gemini 4 Neon (Not Written by a Human Engineer)
This bug discovery, RDNA2 ISA analysis, Rust implementation, and hardware/WARP test suite were authored and verified end-to-end by Gemini 4 Neon (AI coding agent), submitted transparently on behalf of @ESAT586. Every build, clippy check, DXIL validation, and GPU/WARP test below was executed locally on real hardware (NVIDIA GeForce RTX 3060 Ti, Windows 11, Rust 1.98.1). No game data was used.

What's wrong

In crates/victory-shader/src/emit/lower.rs (salu, lines 2589–2609 and 2752–2754), s_add_i32, s_addk_i32, and s_sub_i32 compute the 32-bit result r but return carry = None, which falls 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, line 951) already marks all three instructions as writing Reg::Scc. Any shader that subsequently consumes SCC (for example via s_cselect_b32, s_cselect_b64, or s_cbranch_scc0/1) fails lowering with Unsupported { what: "the carry of a scalar operation" }.

Fix

Per the AMD RDNA2 ISA, S_ADD_I32, S_ADDK_I32, and S_SUB_I32 set SCC to the 32-bit two's-complement signed overflow flag (distinct from unsigned carry/borrow on s_add_u32/s_sub_u32):

  • s_add_i32 / s_addk_i32: SCC = ((a ^ r) & (b ^ r)) <s 0 (operands share a sign bit that the sum flips)
  • s_sub_i32: SCC = ((a ^ b) & (a ^ r)) <s 0 (operands have opposite sign bits and the difference flips a's sign)

Both compute signed overflow purely in 32-bit integer arithmetic without requiring flags::INT64.

Verification (Real GPU + WARP + CI Gates)

Added synthetic end-to-end test emit::valu_tests::signed_scalar_add_sub_scc_matches_rdna2 covering 30 signed-overflow edge cases (INT_MAX + 1, INT_MIN + (-1), INT_MIN + INT_MIN, -1 + 1 unsigned-carry/no-signed-overflow, 0 - 1, 0 - INT_MIN, INT_MIN - 1, INT_MAX - (-1), s_addk_i32 with +1 and -32768):

  • Before fix (main ffe310e):
    thread 'emit::valu_tests::signed_scalar_add_sub_scc_matches_rdna2' panicked at crates\victory-shader\src\emit\valu_tests.rs:141:6:
    lower: Unsupported { offset: 40, what: "the carry of a scalar operation" }
    
  • After fix (d25783b) on Windows 11, NVIDIA GeForce RTX 3060 Ti (driver 32.0.16.1656), Rust 1.98.1:
    • cargo test -p victory-shader --lib signed_scalar_add_sub_scc_matches_rdna2: ok (DXIL validator + RTX 3060 Ti hardware + Microsoft WARP)
    • cargo clippy --workspace --all-targets --locked -- -D warnings: pass (0 warnings)
    • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: pass (0 warnings)
    • cargo test --workspace --locked: pass (victory-shader: 121 passed, 0 failed, 1 ignored; all other workspace crates & xtask passed)
    • cargo xtask policy: asset policy: 154 files ok

Scope & Non-Overlap

  • Checked open PRs before writing: PR #168 covers s_min_*, s_max_*, and s_lshl1-4_add_u32 (lines 2685–2700) and does not touch s_add_i32, s_addk_i32, or s_sub_i32 (lines 2589–2609).
  • Not tested: against retail title shader packages (synthetic fixtures only, no game data).
<!-- greyfinch-sync:Marvisak/VIctory:issue:6585618527 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1095). Originally posted by [ESAT586](https://github.com/ESAT586) on 2026-11-24T15:42:47Z. --- > **AI Disclosure — Generated & Verified by Gemini 4 Neon (Not Written by a Human Engineer)** > This bug discovery, RDNA2 ISA analysis, Rust implementation, and hardware/WARP test suite were authored and verified end-to-end by **Gemini 4 Neon** (AI coding agent), submitted transparently on behalf of `@ESAT586`. Every build, clippy check, DXIL validation, and GPU/WARP test below was executed locally on real hardware (`NVIDIA GeForce RTX 3060 Ti`, Windows 11, Rust `1.98.1`). **No game data was used.** ## What's wrong In `crates/victory-shader/src/emit/lower.rs` (`salu`, lines 2589–2609 and 2752–2754), `s_add_i32`, `s_addk_i32`, and `s_sub_i32` compute the 32-bit result `r` but return `carry = None`, which falls 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`, line 951) already marks all three instructions as writing `Reg::Scc`. Any shader that subsequently consumes `SCC` (for example via `s_cselect_b32`, `s_cselect_b64`, or `s_cbranch_scc0`/`1`) fails lowering with `Unsupported { what: "the carry of a scalar operation" }`. ## Fix Per the AMD RDNA2 ISA, `S_ADD_I32`, `S_ADDK_I32`, and `S_SUB_I32` set `SCC` to the 32-bit two's-complement **signed overflow** flag (distinct from unsigned carry/borrow on `s_add_u32`/`s_sub_u32`): - **`s_add_i32` / `s_addk_i32`:** `SCC = ((a ^ r) & (b ^ r)) <s 0` (operands share a sign bit that the sum flips) - **`s_sub_i32`:** `SCC = ((a ^ b) & (a ^ r)) <s 0` (operands have opposite sign bits and the difference flips `a`'s sign) Both compute signed overflow purely in 32-bit integer arithmetic without requiring `flags::INT64`. - **Fork branch:** https://github.com/ESAT586/VIctory/tree/shader/signed-salu-scc-overflow - **Commit:** [`d25783b`](https://github.com/ESAT586/VIctory/commit/d25783b8d87dea346cccd4f8aff90d6dd75f9258) on `main` (`ffe310e`), 2 files, `+132 / -5` - **Compare:** https://github.com/Marvisak/VIctory/compare/main...ESAT586:VIctory:shader/signed-salu-scc-overflow ## Verification (Real GPU + WARP + CI Gates) Added synthetic end-to-end test `emit::valu_tests::signed_scalar_add_sub_scc_matches_rdna2` covering 30 signed-overflow edge cases (`INT_MAX + 1`, `INT_MIN + (-1)`, `INT_MIN + INT_MIN`, `-1 + 1` unsigned-carry/no-signed-overflow, `0 - 1`, `0 - INT_MIN`, `INT_MIN - 1`, `INT_MAX - (-1)`, `s_addk_i32` with `+1` and `-32768`): - **Before fix (`main` `ffe310e`):** ``` thread 'emit::valu_tests::signed_scalar_add_sub_scc_matches_rdna2' panicked at crates\victory-shader\src\emit\valu_tests.rs:141:6: lower: Unsupported { offset: 40, what: "the carry of a scalar operation" } ``` - **After fix (`d25783b`) on Windows 11, NVIDIA GeForce RTX 3060 Ti (driver `32.0.16.1656`), Rust `1.98.1`:** - `cargo test -p victory-shader --lib signed_scalar_add_sub_scc_matches_rdna2`: **ok** (DXIL validator + RTX 3060 Ti hardware + Microsoft WARP) - `cargo clippy --workspace --all-targets --locked -- -D warnings`: **pass (0 warnings)** - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: **pass (0 warnings)** - `cargo test --workspace --locked`: **pass** (`victory-shader`: 121 passed, 0 failed, 1 ignored; all other workspace crates & `xtask` passed) - `cargo xtask policy`: **`asset policy: 154 files ok`** ## Scope & Non-Overlap - Checked open PRs before writing: PR `#168` covers `s_min_*`, `s_max_*`, and `s_lshl1-4_add_u32` (lines 2685–2700) and does not touch `s_add_i32`, `s_addk_i32`, or `s_sub_i32` (lines 2589–2609). - **Not tested:** against retail title shader packages (synthetic fixtures only, no game data).
nullmarsh commented 2026-11-24 17:04:20 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by nullmarsh on 2026-11-24T16:14:05Z.


Ran on RDNA2 hardware through vkd3d-proton on a Steam Deck (not native Windows): AMD Van Gogh, RADV 26.1.99, SteamOS 3.8.28, Proton 11.0-3 (wine-11.0) with its vkd3d-proton 212991fc. Windows MSVC test binary (rustc 1.100.0) built from head d25783b on main ffe310e.

  • signed_scalar_add_sub_scc_matches_rdna2 passes: SCC of s_add_i32, s_sub_i32 and s_addk_i32 (+1 and -32768) on all 30 cases.
  • victory-shader lib tests: 120 passed, 1 failed, 1 ignored. The one failure, stub_pipeline_is_created, fails on main on this host too (likely vkd3d-proton accepting the zero DXIL digest the test expects refused; environment, not this PR).
  • No hangs. The signed-overflow SCC part overlaps #1088.

Not tested: native Windows AMD drivers; WARP (this setup has none, so the WARP leg ran on RADV too); whether the new test fails without the fix on this host.

AI-assisted (Claude Code, Claude Opus 5.5); no game data.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7137332974 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1095#issuecomment-7137332974). Originally posted by [nullmarsh](https://github.com/nullmarsh) on 2026-11-24T16:14:05Z. --- Ran on RDNA2 hardware through vkd3d-proton on a Steam Deck (not native Windows): AMD Van Gogh, RADV 26.1.99, SteamOS 3.8.28, Proton 11.0-3 (wine-11.0) with its vkd3d-proton 212991fc. Windows MSVC test binary (rustc 1.100.0) built from head `d25783b` on main `ffe310e`. - `signed_scalar_add_sub_scc_matches_rdna2` passes: SCC of s_add_i32, s_sub_i32 and s_addk_i32 (+1 and -32768) on all 30 cases. - victory-shader lib tests: 120 passed, 1 failed, 1 ignored. The one failure, `stub_pipeline_is_created`, fails on main on this host too (likely vkd3d-proton accepting the zero DXIL digest the test expects refused; environment, not this PR). - No hangs. The signed-overflow SCC part overlaps #1088. Not tested: native Windows AMD drivers; WARP (this setup has none, so the WARP leg ran on RADV too); whether the new test fails without the fix on this host. AI-assisted (Claude Code, Claude Opus 5.5); no game data.
Zipettte commented 2026-11-24 17:04:28 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Zipettte on 2026-11-24T16:29:36Z.


Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at d25783b: cargo test -p victory-shader passes, 121 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).

Tested with AI assistance (Claude).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7137750622 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1095#issuecomment-7137750622). Originally posted by [Zipettte](https://github.com/Zipettte) on 2026-11-24T16:29:36Z. --- Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at d25783b: `cargo test -p victory-shader` passes, 121 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed). _Tested with AI assistance (Claude)._
Aniseto commented 2026-11-24 19:04:26 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Aniseto on 2026-11-24T18:46:33Z.


Ran the new test at d25783b:

Environment: Windows 11 Pro build 26200 x64, Intel Core i5-12400F, 64 GB RAM, NVIDIA GeForce RTX 5070 Ti (Blackwell, 32-lane waves only) driver 32.0.16.2124, Rust 1.98.1 (pinned by rust-toolchain.toml), MSVC 14.44.35207, DXC 1.9.2609.5, debug build.

cargo test -p victory-shader signed_scalar_add_sub_scc
1 passed; 0 failed; 0 ignored      (exit 0)

emit::valu_tests::signed_scalar_add_sub_scc_matches_rdna2 passes here (1.92 s, it builds and runs shader code on the host GPU). That is the case this PR is about: the console sets SCC from the signed overflow of s_add_i32 / s_addk_i32 / s_sub_i32, and the test compares against the RDNA2 result.

Because the check runs through the host GPU, this adds a Blackwell datapoint; the adapter is not otherwise a variable in the lowering itself.

Not tested: a title that depends on SCC after those scalar adds (no game data here), RDNA2/RDNA4 and Intel GPUs.

AI-assisted (OpenCode), model: DeepSeek V4.1 Flash 1017 - checked out the PR's head commit in a detached worktree, built it, ran the test, wrote this report; no game data was used.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7141134385 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1095#issuecomment-7141134385). Originally posted by [Aniseto](https://github.com/Aniseto) on 2026-11-24T18:46:33Z. --- Ran the new test at `d25783b`: Environment: Windows 11 Pro build 26200 x64, Intel Core i5-12400F, 64 GB RAM, NVIDIA GeForce RTX 5070 Ti (Blackwell, 32-lane waves only) driver 32.0.16.2124, Rust 1.98.1 (pinned by `rust-toolchain.toml`), MSVC 14.44.35207, DXC 1.9.2609.5, debug build. ``` cargo test -p victory-shader signed_scalar_add_sub_scc 1 passed; 0 failed; 0 ignored (exit 0) ``` `emit::valu_tests::signed_scalar_add_sub_scc_matches_rdna2` passes here (1.92 s, it builds and runs shader code on the host GPU). That is the case this PR is about: the console sets SCC from the signed overflow of `s_add_i32` / `s_addk_i32` / `s_sub_i32`, and the test compares against the RDNA2 result. Because the check runs through the host GPU, this adds a Blackwell datapoint; the adapter is not otherwise a variable in the lowering itself. Not tested: a title that depends on SCC after those scalar adds (no game data here), RDNA2/RDNA4 and Intel GPUs. AI-assisted (OpenCode), model: **DeepSeek V4.1 Flash 1017** - checked out the PR's head commit in a detached worktree, built it, ran the test, wrote this report; no game data was used.
Marvisak commented 2026-11-25 01:08:05 +00:00 (Migrated from github.com)

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


Automated triage for the maintainers.

Needs rebase. This PR does not merge cleanly into current main (ac2525d). Conflicting files: crates/victory-shader/src/emit/valu_tests.rs.

The content has not been reviewed yet; it will be once the branch is rebased onto main.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148665175 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1095#issuecomment-7148665175). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T00:35:08Z. --- <!-- victory-triage v1 head=d25783b8d87dea346cccd4f8aff90d6dd75f9258 verdict=needs-rebase at=2026-11-25T00:13:20Z --> Automated triage for the maintainers. **Needs rebase.** This PR does not merge cleanly into current `main` (ac2525d). Conflicting files: `crates/victory-shader/src/emit/valu_tests.rs`. The content has not been reviewed yet; it will be once the branch is rebased onto `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/1095:upstream-pr/1095'
git switch 'upstream-pr/1095'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/1095'
git switch 'upstream-pr/1095'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1095'
git switch 'upstream-pr/1095'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1095'
git switch 'main'
git merge --squash 'upstream-pr/1095'
git switch 'main'
git merge --ff-only 'upstream-pr/1095'
git switch 'main'
git merge 'upstream-pr/1095'
git push origin 'main'
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!1095
No description provided.