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

Open
github-bot wants to merge 1 commit from github-bot/gh-1095 into main AGit
Owner

@⁠ESAT586 on GitHub · 2026-11-24 15:42 UTC · original
ESAT586:shader/signed-salu-scc-overflow → main · +132 −5 · 2 files · 1 commits · open

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).
**@⁠ESAT586** on GitHub · 2026-11-24 15:42 UTC · [original](https://github.com/Marvisak/VIctory/pull/1095) `ESAT586:shader/signed-salu-scc-overflow` → `main` · +132 −5 · 2 files · 1 commits · open > **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).
AI-generated & verified with Gemini 4 Neon (not written by a human engineer).

s_add_i32, s_addk_i32, and s_sub_i32 previously left SCC as
Val::Missing("the carry of a scalar operation"), causing any shader that
reads SCC after these instructions (e.g. via s_cselect_b32 or s_cbranch_scc*)
to fail lowering.

Compute 32-bit two's-complement signed overflow directly in i32 (no i64
capability required), matching the AMD RDNA2 ISA:
- s_add_i32 / s_addk_i32: ((a ^ r) & (b ^ r)) <s 0
- s_sub_i32:              ((a ^ b) & (a ^ r)) <s 0

Verified with synthetic GPU + WARP + dxil.dll tests in valu_tests.rs on
NVIDIA GeForce RTX 3060 Ti (Windows 11, Rust 1.98.1). No game data used.

Co-Authored-By: Gemini 4 Neon <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:19:40 +00:00
Author
Owner

@⁠ghost on GitHub · 2026-11-24 16:14 UTC · original

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.

**@⁠ghost** on GitHub · 2026-11-24 16:14 UTC · [original](https://github.com/Marvisak/VIctory/pull/1095#issuecomment-7137332974) 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.
Author
Owner

@⁠Zipettte on GitHub · 2026-11-24 16:29 UTC · original

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).

**@⁠Zipettte** on GitHub · 2026-11-24 16:29 UTC · [original](https://github.com/Marvisak/VIctory/pull/1095#issuecomment-7137750622) 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)._
Author
Owner

@⁠Aniseto on GitHub · 2026-11-24 18:46 UTC · original

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.

**@⁠Aniseto** on GitHub · 2026-11-24 18:46 UTC · [original](https://github.com/Marvisak/VIctory/pull/1095#issuecomment-7141134385) 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.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:35 UTC · original

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

**@⁠Marvisak** on GitHub · 2026-11-25 00:35 UTC · [original](https://github.com/Marvisak/VIctory/pull/1095#issuecomment-7148665175) <!-- 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 has changes conflicting with the target branch.
  • crates/victory-shader/src/emit/valu_tests.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1095/head:github-bot/gh-1095'
git switch 'github-bot/gh-1095'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1095'
git switch 'github-bot/gh-1095'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1095'
git switch 'github-bot/gh-1095'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1095'
git switch 'main'
git merge --squash 'github-bot/gh-1095'
git switch 'main'
git merge --ff-only 'github-bot/gh-1095'
git switch 'main'
git merge 'github-bot/gh-1095'
git push origin 'main'
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!1095
No description provided.