Skip to content

[pr-fix] Shaders: s_lshl<n>_add_u32 sets SCC to its carry - #12

Merged
KyleS98 merged 1 commit into
KyleS98:mainfrom
Melow-code:fix/lshl-add-carry
Nov 29, 2026
Merged

KyleS98 merged 1 commit into
KyleS98: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. No issue or pull request here mentions it. The same fix is open in trustno1-coder/VIctory-NVIDIA#1972 (also mine). Credit: the defect and the formula were reported before in Marvisak/VIctory#168 and fxceless/victory-fork#350 (both open).

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 fix is in sop (emit/lower.rs). The test evaluator in bitcode.rs (test code) learns or, zero extension and three integer compares.

How it was tested

  • Profile: no game run. No visible change is claimed, and I do not know which of the title's programs use the carry.
  • 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 pull request: 2,000 random scalar programs (the four opcodes among adds, subtracts, selects, logic and shifts; literals, inline constants, the destination over a source), 24 inputs each, against 64-bit arithmetic: 432,000 stored values, no mismatch. It fails with the change reverted.
  • GPU: 40 such programs, 12 inputs each, on an RTX 3070 and on WARP, through the DXIL validator: 4,320 values on each, no mismatch; it fails without the change. That run was on an earlier Neo tree with other changes of mine and the same lowering of these opcodes, not on this branch.
  • Wave sizes: scalar code; lowered as wave32.
  • cargo test passed for the crates it touches: yes. Linux, Rust 1.98.1, on 1675e22: cargo test -p victory-formats -p victory-shader --lib: 57 and 246 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 (main's own are still there; run on e4c123c, two README commits earlier).
  • cargo xtask policy: ok (331 files; also on e4c123c).
  • Not run: the Windows tests on this branch, the rest of the workspace. cargo fmt --check already lists 614 places in this crate on main; this adds one, in the test.

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>
@KyleS98

KyleS98 commented Nov 29, 2026

Copy link
Copy Markdown
Owner

Read against the instruction set: (a << n) + b reaches 2^32 exactly when bits are shifted out of a or the 32-bit add wraps, and the new lowering computes that OR for all four opcodes; the test's encodings decode as stated. Upstream's main (829188e) still leaves this flag unset, so it is new there too. It merges cleanly onto 64208f1.

It changes the shader cache key, so I will take it together with #14 under one full preload and a play of the opening, the cell and the first house before merging, and report here. The red check is main's own Clippy finding in victory-shader ("manual checked division"), not this change.

AI-assisted (Claude Code, Claude Fable 5.2).

@KyleS98
KyleS98 merged commit b68189d into KyleS98:main Nov 29, 2026
1 of 2 checks passed
@KyleS98

KyleS98 commented Nov 29, 2026

Copy link
Copy Markdown
Owner

Merged (b68189d, in main at 147473d), thank you. Tested together with #14 under one preload on an RTX 3090 Ti; the numbers are in #14's thread. For this change alone: your test passes on the card with the rest of victory-shader's (380 passed), every program of the title still translates and validates as before, and the opening, the cell, the first house and the deck showed no error and no difference I could put on it, which is what you expected of a flag few programs read.

AI-assisted (Claude Code, Claude Fable 5.2).

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants