s_lshl<n>_add_u32 sets SCC to its carry #1972

Closed
github-bot wants to merge 1 commit from github-bot/gh-1972 into main AGit
Owner

@⁠Melow-code on GitHub · 2026-11-29 04:59 UTC · original
fix/lshl-add-carry → main · +68 −3 · 3 files · 1 commits · closed without merging

Found by reading the scalar lowering against the RDNA 2 ISA. I found no issue or pull request for it here. It is a known defect elsewhere, with the same formula proposed: Marvisak/VIctory#168 and fxceless/victory-fork#350 (both open; their s_min/s_max half is already here through #1891). Marvisak/VIctory#2813 refuses such reads instead.

What it changes

s_lshl1_add_u32 to s_lshl4_add_u32 left "the result is not zero" in SCC. The RDNA 2 ISA (12.1) gives the unsigned overflow of the whole sum, shifted-out bits included:

D.u = (S0.u << N) + S1.u
SCC = (((S0.u << N) + S1.u) >= 0x100000000ULL ? 1 : 0). // unsigned overflow

An s_addc_u32 behind one, the high dword of a 64-bit address, therefore added 1 whenever the low dword was not zero. A branch or an s_cselect behind one could take the wrong side.

SCC is now the carry: the shift loses a set bit, or the addition wraps. Lyrnc measured this rule, lost bits included, on RDNA 2 hardware (gfx1036) in Marvisak/VIctory#168: 141 of 141 operand pairs for each of the four opcodes.

The change is in sop in emit/lower.rs. The test evaluator in bitcode.rs (test code) learns or, zero extension and three integer compares.

How it was tested

  • GPU and profile: none. No card and no game run; no visible change is claimed, and I do not know which of the title's programs use the carry. One automated run in fxceless/victory-fork#350 saw no difference.
  • Test: shift_and_add_leave_their_carry_in_scc lowers the four opcodes, each followed by s_addc_u32 s7, 0, 0, and evaluates what the module stores for 13 operand pairs (no carry, each lost bit, wraps, both). It runs on any host, without a device.
  • Before: it fails, (0x1 << 1) + 0x2: 1 where 0 is right.
  • After: it passes.
  • A second check with another evaluator, not part of this PR: 20,000 random inputs per opcode through s_addc_u32 twice, against 64-bit arithmetic, and 5,000 per opcode with a literal, an inline -1 and the destination over a source, read by s_cselect_b32. 400,000 values, no mismatch; both fail with the change reverted. s_add/s_addc, s_sub/s_subb, s_add_i32 and s_lshl_b32 give the right values before and after.
  • Wave sizes: scalar code; lowered as wave32.
  • On Linux, Rust 1.98.1, on e1f2eb60: cargo test -p victory-formats -p victory-shader --lib: 57 and 274 passed, 0 failed (5 and 2 ignored).
  • CARGO_FEATURE_PURE=1 cargo clippy -p victory-gpu -p victory-shader --tests --target x86_64-pc-windows-msvc: exit 0, no warning on the changed lines. The existing one in victory-shader (manual_checked_ops) is still there.
  • Not run: the Windows tests, the D3D12 validator, the rest of the workspace. lower.rs is past the cargo xtask policy size limit on main already.

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches. None here changes these opcodes. Related: #1891 and #1421 (merged; other opcodes).

🤖 Generated with Claude Code

**@⁠Melow-code** on GitHub · 2026-11-29 04:59 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1972) `fix/lshl-add-carry` → `main` · +68 −3 · 3 files · 1 commits · closed without merging Found by reading the scalar lowering against the RDNA 2 ISA. I found no issue or pull request for it here. It is a known defect elsewhere, with the same formula proposed: Marvisak/VIctory#168 and fxceless/victory-fork#350 (both open; their `s_min`/`s_max` half is already here through #1891). Marvisak/VIctory#2813 refuses such reads instead. ## What it changes `s_lshl1_add_u32` to `s_lshl4_add_u32` left "the result is not zero" in SCC. The RDNA 2 ISA (12.1) gives the unsigned overflow of the whole sum, shifted-out bits included: D.u = (S0.u << N) + S1.u SCC = (((S0.u << N) + S1.u) >= 0x100000000ULL ? 1 : 0). // unsigned overflow An `s_addc_u32` behind one, the high dword of a 64-bit address, therefore added 1 whenever the low dword was not zero. A branch or an `s_cselect` behind one could take the wrong side. SCC is now the carry: the shift loses a set bit, or the addition wraps. Lyrnc measured this rule, lost bits included, on RDNA 2 hardware (gfx1036) in Marvisak/VIctory#168: 141 of 141 operand pairs for each of the four opcodes. The change is in `sop` in `emit/lower.rs`. The test evaluator in `bitcode.rs` (test code) learns `or`, zero extension and three integer compares. ## How it was tested - GPU and profile: none. No card and no game run; no visible change is claimed, and I do not know which of the title's programs use the carry. One automated run in fxceless/victory-fork#350 saw no difference. - Test: `shift_and_add_leave_their_carry_in_scc` lowers the four opcodes, each followed by `s_addc_u32 s7, 0, 0`, and evaluates what the module stores for 13 operand pairs (no carry, each lost bit, wraps, both). It runs on any host, without a device. - Before: it fails, `(0x1 << 1) + 0x2`: 1 where 0 is right. - After: it passes. - A second check with another evaluator, not part of this PR: 20,000 random inputs per opcode through `s_addc_u32` twice, against 64-bit arithmetic, and 5,000 per opcode with a literal, an inline -1 and the destination over a source, read by `s_cselect_b32`. 400,000 values, no mismatch; both fail with the change reverted. `s_add`/`s_addc`, `s_sub`/`s_subb`, `s_add_i32` and `s_lshl_b32` give the right values before and after. - Wave sizes: scalar code; lowered as wave32. - On Linux, Rust 1.98.1, on e1f2eb60: `cargo test -p victory-formats -p victory-shader --lib`: 57 and 274 passed, 0 failed (5 and 2 ignored). - `CARGO_FEATURE_PURE=1 cargo clippy -p victory-gpu -p victory-shader --tests --target x86_64-pc-windows-msvc`: exit 0, no warning on the changed lines. The existing one in `victory-shader` (`manual_checked_ops`) is still there. - Not run: the Windows tests, the D3D12 validator, the rest of the workspace. `lower.rs` is past the `cargo xtask policy` size limit on main already. ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. None here changes these opcodes. Related: #1891 and #1421 (merged; other opcodes). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
s_lshl1_add_u32 to s_lshl4_add_u32 left whether the result is not zero in
SCC. The RDNA 2 ISA gives the unsigned overflow of the whole sum, shifted
bits included, so an s_addc_u32 behind one added 1 to the high dword of
nearly every address.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-29 05:03:49 +00:00
Author
Owner

@⁠dyshay on GitHub · 2026-11-29 13:05 UTC · original

Merged into our fork's integration branch for testing (dyshay/VIctory-NVIDIA, branch janus-prs, on top of 829188e9 plus a couple of local fixes). Read the full diff first: no network, process spawning, binaries, build/CI/dependency changes or game data. Builds with --profile diag --features diagnostics; one conflict only: on our branch the lower.rs tests live in lower/tests.rs, so the new test moved there unchanged. Targeted shader tests pass. In-game runs on an RTX 4090 are queued; results will follow here.

**@⁠dyshay** on GitHub · 2026-11-29 13:05 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1972#issuecomment-7256279640) Merged into our fork's integration branch for testing ([dyshay/VIctory-NVIDIA](https://github.com/dyshay/VIctory-NVIDIA), branch `janus-prs`, on top of 829188e9 plus a couple of local fixes). Read the full diff first: no network, process spawning, binaries, build/CI/dependency changes or game data. Builds with `--profile diag --features diagnostics`; one conflict only: on our branch the lower.rs tests live in `lower/tests.rs`, so the new test moved there unchanged. Targeted shader tests pass. In-game runs on an RTX 4090 are queued; results will follow here.
Author
Owner

@⁠Lyrnc on GitHub · 2026-11-30 06:11 UTC · original

Checked this against a Radeon gfx1036 (RDNA 2) running the same kernels natively, and ran the translator's output for main and for this PR on an RTX 5080 and on that Radeon through D3D12.

Setup

  • main at 41adb551 with this PR's head (c4848d8) merged locally. The lowering change merged without a conflict; your test shift_and_add_leave_their_carry_in_scc sits in the old mod tests of lower.rs, which main has moved, so it is not part of this build. The victory-shader test binary was built from that merged tree (own tree and target directory).
  • Kernels: hand-written GFX10 code, nothing from a title. Wave32, a 64-thread group (two waves); outputs v10.. stored per lane.
  • Hardware rows: the same machine code dispatched natively on the gfx1036.
  • Host runs: super::emit (32-lane host), dxil.dll validation, dispatched on the 5080 and on the Radeon (VICTORY_ADAPTER=radeon).

Cases

For each of s_lshl1_add_u32 .. s_lshl4_add_u32, 8 operand pairs, with SCC set to the opposite of the expected carry by an s_cmp_eq_u32 right before:

  • lshl_add/<n>: the result and SCC through s_cselect_b32 s43, 1, 0.
  • lshl_addc/<n>: the high dword of an address, s_addc_u32 s44, 0x100, 0 right after.

Pairs: (0x80000000, 0) only lost bits, (1, 2) no carry, (0x40000000, 0xC0000000), (0x7FFFFFFF, 1), (0x7FFFFFFF, 2), (0x10000000, 0) lost bits only for n = 4, (0, 0xFFFFFFFF), (0xFFFFFFFF, 0xFFFFFFFF).

The gfx1036 follows the ISA formula exactly, lost bits included: SCC = ((S0 << n) + S1) >= 2^32 in all 32 pair/opcode combinations. "Only the 32-bit addition's carry" is wrong in 1 to 5 of the 8 pairs per opcode, and "result is not zero" in 4 to 6.

Results (values that differ from the gfx1036, out of 64 lanes x 16 outputs)

case main, 5080 main, Radeon this PR, 5080 this PR, Radeon
lshl_add/1 384 (v11, v13, v17, v19, v21, v23) 384 (same) exact exact
lshl_addc/1 384 (v10, v11, v13, v14, v15, v16) 384 (same) exact exact
lshl_add/2, /3, /4 256 each (4 SCC outputs) 256 each exact exact
lshl_addc/2, /3, /4 256 each (4 address high dwords) 256 each exact exact

On main every differing value is SCC or the s_addc_u32 after it; the shifted sums themselves were already right. Examples of main against the hardware, all 64 lanes alike: s_lshl1_add_u32 of (0x80000000, 0) gives 0 with SCC 1 on the gfx1036 (the lost bit), main SCC 0; (1, 2) gives SCC 0, main 1, so the high dword after it is 0x101 instead of 0x100. s_lshl4_add_u32 of (0x10000000, 0) gives 0 with SCC 1, main 0.

Verdict: correct. With this PR all 16 cases are exact on both cards: 2,048 SCC values and 2,048 address high dwords, the lost-bit-only pairs and the wrapping ones alike. The other 8 wave32 cases in the same run (other opcodes) give the same values as main.

AI use: Claude Code (Claude Opus 5.5) ran the oracle and host cases and wrote this comment.

**@⁠Lyrnc** on GitHub · 2026-11-30 06:11 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1972#issuecomment-7270391135) Checked this against a Radeon gfx1036 (RDNA 2) running the same kernels natively, and ran the translator's output for main and for this PR on an RTX 5080 and on that Radeon through D3D12. **Setup** - `main` at 41adb551 with this PR's head (c4848d8) merged locally. The lowering change merged without a conflict; your test `shift_and_add_leave_their_carry_in_scc` sits in the old `mod tests` of `lower.rs`, which main has moved, so it is not part of this build. The victory-shader test binary was built from that merged tree (own tree and target directory). - Kernels: hand-written GFX10 code, nothing from a title. Wave32, a 64-thread group (two waves); outputs v10.. stored per lane. - Hardware rows: the same machine code dispatched natively on the gfx1036. - Host runs: `super::emit` (32-lane host), `dxil.dll` validation, dispatched on the 5080 and on the Radeon (`VICTORY_ADAPTER=radeon`). **Cases** For each of `s_lshl1_add_u32` .. `s_lshl4_add_u32`, 8 operand pairs, with SCC set to the opposite of the expected carry by an `s_cmp_eq_u32` right before: - `lshl_add/<n>`: the result and SCC through `s_cselect_b32 s43, 1, 0`. - `lshl_addc/<n>`: the high dword of an address, `s_addc_u32 s44, 0x100, 0` right after. Pairs: (0x80000000, 0) only lost bits, (1, 2) no carry, (0x40000000, 0xC0000000), (0x7FFFFFFF, 1), (0x7FFFFFFF, 2), (0x10000000, 0) lost bits only for n = 4, (0, 0xFFFFFFFF), (0xFFFFFFFF, 0xFFFFFFFF). The gfx1036 follows the ISA formula exactly, lost bits included: SCC = ((S0 << n) + S1) >= 2^32 in all 32 pair/opcode combinations. "Only the 32-bit addition's carry" is wrong in 1 to 5 of the 8 pairs per opcode, and "result is not zero" in 4 to 6. **Results** (values that differ from the gfx1036, out of 64 lanes x 16 outputs) | case | main, 5080 | main, Radeon | this PR, 5080 | this PR, Radeon | |---|---|---|---|---| | `lshl_add/1` | 384 (v11, v13, v17, v19, v21, v23) | 384 (same) | exact | exact | | `lshl_addc/1` | 384 (v10, v11, v13, v14, v15, v16) | 384 (same) | exact | exact | | `lshl_add/2`, `/3`, `/4` | 256 each (4 SCC outputs) | 256 each | exact | exact | | `lshl_addc/2`, `/3`, `/4` | 256 each (4 address high dwords) | 256 each | exact | exact | On main every differing value is SCC or the `s_addc_u32` after it; the shifted sums themselves were already right. Examples of main against the hardware, all 64 lanes alike: `s_lshl1_add_u32` of (0x80000000, 0) gives 0 with SCC 1 on the gfx1036 (the lost bit), main SCC 0; (1, 2) gives SCC 0, main 1, so the high dword after it is 0x101 instead of 0x100. `s_lshl4_add_u32` of (0x10000000, 0) gives 0 with SCC 1, main 0. **Verdict: correct.** With this PR all 16 cases are exact on both cards: 2,048 SCC values and 2,048 address high dwords, the lost-bit-only pairs and the wrapping ones alike. The other 8 wave32 cases in the same run (other opcodes) give the same values as main. AI use: Claude Code (Claude Opus 5.5) ran the oracle and host cases and wrote this comment.
github-bot force-pushed github-bot/gh-1972 from c4848d81d3 to e2f5692b58 2026-11-30 06:19:21 +00:00 Compare
Author
Owner

@⁠Melow-code on GitHub · 2026-11-30 06:16 UTC · original

Thank you, that is the check I could not make: 2,048 SCC values and as many high dwords against the real chip, the lost-bit-only pairs included.

Rebased onto 41adb551, as main had moved the tests: the lowering is the same, the test is in emit/lower/tests.rs now. Linux, Rust 1.98.1: cargo test -p victory-shader --lib 296 passed, 0 failed, 2 ignored. (victory-formats has three bcpack::tests::oracle_parity_* failures here, the same three on main.) Not run on a card again since the rebase.

For information: the same change was merged in VIctory-Neo (KyleS98/VIctory-Neo#12), where it went through a full preload and play without a visible difference.

AI-assisted (Claude Code).

**@⁠Melow-code** on GitHub · 2026-11-30 06:16 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1972#issuecomment-7270480081) Thank you, that is the check I could not make: 2,048 SCC values and as many high dwords against the real chip, the lost-bit-only pairs included. Rebased onto 41adb551, as `main` had moved the tests: the lowering is the same, the test is in `emit/lower/tests.rs` now. Linux, Rust 1.98.1: `cargo test -p victory-shader --lib` 296 passed, 0 failed, 2 ignored. (`victory-formats` has three `bcpack::tests::oracle_parity_*` failures here, the same three on `main`.) Not run on a card again since the rebase. For information: the same change was merged in VIctory-Neo (KyleS98/VIctory-Neo#12), where it went through a full preload and play without a visible difference. AI-assisted (Claude Code).
github-bot closed this pull request 2026-12-01 15:02:12 +00:00

Pull request closed

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/trustno1-coder--VIctory-NVIDIA!1972
No description provided.