Skip to content

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

Open
Melow-code wants to merge 1 commit into
trustno1-coder:mainfrom
Melow-code:fix/lshl-add-carry
Open

Melow-code wants to merge 1 commit into
trustno1-coder:mainfrom
Melow-code:fix/lshl-add-carry

Conversation

@Melow-code

Copy link
Copy Markdown
Contributor

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 Shaders: compute exact SCC for s_lshl{1..4}_add_u32 and s_min/s_max_{u32,i32} 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 e1f2eb6: 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

🤖 Generated with 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 <noreply@anthropic.com>
@github-actions github-actions Bot added pr-fix Pull request: fixes a bug shaders Shader translation, preload or pipeline building labels Nov 29, 2026
@github-actions github-actions Bot changed the title [pr-fix][shaders] s_lshl<n>_add_u32 sets SCC to its carry s_lshl<n>_add_u32 sets SCC to its carry Nov 29, 2026
@dyshay

dyshay commented Nov 29, 2026

Copy link
Copy Markdown

Merged into our fork's integration branch for testing (dyshay/VIctory-NVIDIA, branch janus-prs, on top of 829188e 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-fix Pull request: fixes a bug shaders Shader translation, preload or pipeline building

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants