Shaders: SCC of s_min, s_max and s_lshl1-4_add_u32 as the ISA sets it #168
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!168
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-168"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@Dufftwt on GitHub · 2026-11-24 01:03 UTC · original
Dufftwt:fix/scc-of-scalar-min-max-lshl-add→main· +68 −3 · 2 files · 1 commits · openWhat's wrong
On the console GPU, many scalar instructions also set a 1-bit flag, SCC, which later instructions can test.
s_cselectpicks between two values on it, ands_cbranch_scc0/1branches on it.ir.rscorrectly marks these instructions as writing SCC:s_min_i32,s_min_u32,s_max_i32,s_max_u32s_lshl1_add_u32,s_lshl2_add_u32,s_lshl3_add_u32,s_lshl4_add_u32Their lowering in
lower.rsdoesn't say what SCC becomes, so it falls back to the generic rule "SCC = result is not zero". The ISA defines SCC differently for these instructions:s_min_*src0 < src1)s_max_*src0 > src1)s_lshlN_add_u32(src0 << N) + src1overflows 32 bitsFor
s_lshlN_add_u32, the overflow includes bits shifted out of src0. The RDNA3 reference writes the pseudocode with a 64-bit intermediate.Examples of what goes wrong today:
Any
s_cselectors_cbranch_scc*that reads SCC after one of these takes the wrong side. Compilers don't often read SCC after these instructions (LLVM never does), but it is legal ISA, and the translated code should behave like the hardware when a shader does.What the fix does
Two small changes in
lower.rs, in the same style as the existing carry handling ofs_add_u32/s_addc_u32:result < src0 << N, unsigned).How it behaves after the fix
Tests
There are two new tests in
valu_tests.rs, using the synthetic-shader harness from #37. Each test runs the instruction on two inputs, reads SCC back withs_cselect_b32 s13, 1, 0, and stores it:s_min_and_s_max_set_scc_when_src0_is_chosen: all four min/max forms, with src0 chosen, src1 chosen, and equal inputs.s_lshl_add_sets_scc_on_unsigned_overflow:s_lshl1_add_u32ands_lshl4_add_u32, with no overflow, overflow through the add, and overflow through the shift.I ran
cargo test -p victory-shaderon GitHub'swindows-latestwith Rust 1.98.1 fromrust-toolchain.toml. There's no GPU there, so the harness runs the shaders on WARP. Results:This PR: both new tests pass; the shader crate has 71 passed and 0 failed.
The new tests alone on
main: both fail. SCC is 1 where the hardware gives 0 (left: 1, right: 0), because the result is non-zero.With
--test-threads=4, the test process sometimes crashes on that runner (STATUS_ACCESS_VIOLATION) before reporting the GPU tests. Unmodifiedmaindoes this too: it crashed in 3 of 3 runs. So the numbers above come from--test-threads=1runs, which are clean.I tested only the shader crate:
cargo test --workspacedoesn't build onmainright now, because of a duplicatemod testsinvictory-gpu/src/host.rs, which #129 fixes.Not tested: a real GPU, or a title's shaders that use these instructions.
@DaviTostes on GitHub · 2026-11-24 01:19 UTC · original
Tested
ae8967con Arch Linux x86_64, rustc 1.98.1: pass.cargo test -p victory-shader: 56 passed, 0 failed (2 test binaries), including the 2 tests this PR adds.victory-gpuE0425.No game data used.
@upipibi on GitHub · 2026-11-24 05:33 UTC · original
Independent check of this PR (head
ae8967c, merged ontomaind2a4bae; it merges cleanly). I found nothing wrong.Against the RDNA2 ISA (30-November-2020), 12.1 "SOP2 Instructions"
s_min_i32/s_min_u32(opcodes 6, 7; pp. 102-103):SCC = (S0.i < S1.i)/SCC = (S0.u < S1.u).s_max_i32/s_max_u32(opcodes 8, 9):SCC = (S0.i > S1.i)/SCC = (S0.u > S1.u). The comparisons are strict, so equal operands give SCC = 0.lower.rs:2616-2623returns the same strict compare (IULT/SLT/IUGT/SGT) that already picks the result, so result and SCC cannot disagree. Correct.s_lshl1..4_add_u32(opcodes 46-49; pp. 105-106):SCC = (((S0.u << N) + S1.u) >= 0x100000000ULL ? 1 : 0). The 64-bit constant means bits shifted out of S0 count; the RDNA3 reference spells that out (tmp = (64'U(S0.u) << 1U) + 64'U(S1.u)).lower.rs:2599-2611computes(a >> (32 - N)) != 0 || (t + b) <u t, which equals the 64-bit test for N = 1..4 (the shift amount 32 - N is 28..31, never 32). Correct.The PR's tests. Their expected values match the ISA formulas by hand; they are not taken from the code. What they leave out, none of which fails:
s_min_u32/s_max_u32would pass them; one case with bit 31 set, e.g.s_min_u32(0x80000000, 1)-> SCC 0, would pin that;INT_MIN/INT_MAXfor the signed forms; equal operands only as (4, 4);s_min_u32(0, 1): result 0 and SCC 1, the one min case where the old "result != 0" rule gives 0 instead of 1;s_lshl2_add_u32ands_lshl3_add_u32are not run at all;s_lshl1_add_u32(0x40000000, 0x80000000));s_cselect_b32, not throughs_cbranch_scc0/1, and never with the destination over src0.Run on hardware.
cargo test -p victory-shader -- --test-threads=1, NVIDIA RTX 3070, driver 616.56, Windows 11, Rust 1.98.1,dxil.dllfrom the Windows SDK 10.0.26100 loaded (so modules are validated), hardware and WARP:main: 71 passed, 0 failed (69 onmainplus the PR's two).s_cselect_b32; destination = src0;s_cbranch_scc0), plus one destination-value check per opcode. Among them:s_min_i32(0x80000000, 0x7fffffff)-> 1,s_max_i32(0x80000000, 0x80000000)-> 0,s_max_u32(0xffffffff, 0)-> 1,s_lshl2_add_u32(0x80000000, 0)-> 1 (shifted out only, result 0),s_lshl3_add_u32(0x10000000, 0x80000000)-> 1 (add carry only, result 0),s_lshl4_add_u32(0x08000000, 0x7fffffff)-> 0.Not checked: AMD hardware (the expected values are the ISA's; an NVIDIA run shows only that the translated DXIL computes them), literal-constant operands, a title's shaders.
Suggestion, optional: add one bit-31 case to each unsigned form and run
s_lshl2/s_lshl3once. I can send the cases if useful.AI-assisted (Claude Code).
@Lyrnc on GitHub · 2026-11-24 06:26 UTC · original
RDNA2 hardware measurement of the SCC rules in the table: all match, including the shifted-out bits for
s_lshlN_add_u32.Setup. AMD Radeon iGPU of a Ryzen 7 9800X3D, gfx1036 (RDNA2), AMD driver 32.0.21042.62, Windows 11 Pro 26200. Hand-written instructions in a HIP kernel (assembled by the AMD driver's amd_comgr). RDNA2 desktop silicon, not the console's. No game data. 141 operand pairs: all pairs of
0, 1, 2, 0x7fffffff, 0x80000000, 0xffffffff, 0x40000000, 0xc0000000, 0x12345678, plus 60 random. SCC was read withs_cselectright after each instruction.s_min_i32/s_min_u32s_max_i32/s_max_u32s_lshl1..4_add_u32(src0 << N) + src1 >= 2^32in 64 bitsFor example,
s_lshl1_add_u32 0x80000000, 0gives result 0 with SCC = 1: the shifted-out bit counts.s_lshl2_add_u32 0x7fffffff, 0gives0xfffffffcwith SCC = 1. The results themselves match too.AI use: Claude Code (Claude Opus 5.5) wrote the probes, ran them on the GPU above and drafted this comment; the numbers come from the hardware run.
@Damasque on GitHub · 2026-11-24 06:40 UTC · original
Both new SCC tests passed at
ae8967con an AMD Radeon RX 6700 XT (driver 32.0.21045.29003, Windows, Rust 1.98.1). I ran each test individually and verified the dispatch device by D3D12 adapter LUID. The RX 6700 XT completed all 12 min/max cases and all 7 shift/add cases. The separate WARP passes also passed, with no hardware skips or fallback.@Marvisak on GitHub · 2026-11-25 00:19 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/lower.rs,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
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.Merge
Merge the changes and update on Forgejo.