Skip to content

Shaders: preserve SCC on signed scalar add and subtract - #1421

Merged
trustno1-coder merged 1 commit into
trustno1-coder:mainfrom
ghptc:fix/shader-scalar-condition-code
Nov 29, 2026
Merged

trustno1-coder merged 1 commit into
trustno1-coder:mainfrom
ghptc:fix/shader-scalar-condition-code

Conversation

@ghptc

@ghptc ghptc commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

AMD defines SCC for S_ADD_I32, S_ADDK_I32, and S_SUB_I32 as signed 32-bit overflow. The translator emits the arithmetic result but leaves SCC missing, so a later S_CSELECT_B32 can cause lowering to return Unsupported.

This ports the 32-bit sign-bit overflow calculation from ESAT586’s Marvisak/VIctory#1095. It uses the existing sign-extended SOPK immediate and retains the unsigned carry and borrow rules. AMD RDNA 2 ISA reference, sections 12.1 and 12.3

The regression uses LLVM MC gfx1030 assembly. It dispatches an unsigned carry control, then compares four signed arithmetic results and their packed SCC flags with Rust’s overflowing_add and overflowing_sub. An independent Python checker derives the same result from unbounded arithmetic and signed range checks.

  • Baseline 32ecacfae73f plus the identical test fixture returned [1, 0] for UINT_MAX + 1, then rejected the signed shader with Unsupported { offset: 132, what: "the carry of a scalar operation" }.
  • Candidate 6b4bb259aec8 returned the same unsigned control and matched all 55 signed UAV words across 11 input pairs on an RTX 5070, driver 620.73.04, through Proton Experimental and VKD3D-Proton 30b0eb9bc12dc9f. For INT_MAX, 1 the readback was [00000005, 80000000, 7ffffffe, 80000000, 7ffffffe]. For INT_MIN, 1 it was [0000000a, 80000001, 7fffffff, 80000001, 7fffffff].
  • Both distinct dumped SPIR-V modules passed spirv-val --target-env vulkan1.3.

Run the exact regression on an SM 6.6 D3D12 hardware adapter:

cargo test -p victory-shader --lib --locked -- emit::valu_tests::signed_scalar_arithmetic_preserves_scc --exact --nocapture --test-threads=1

The fixture and resources are synthetic. Validation covers the NVIDIA hardware path through Proton.

Validation on current main

  • The same patch was applied to 923e38cc8c92cd1c42090076382138538f2bb487 as 9790ba0c8e425b3a7d3ec64e9202f0a3244cc4e5. The measured original head 6b4bb259aec85cdedf13554746d98764993785b0 and its evidence were saved.
  • The fixture-only baseline 99f0b329ddd019761a7a7f7f48ac4eda09468bfa reproduced Unsupported at byte offset 132 after the physical unsigned [1, 0] control. The new head again returned [1, 0] and all 55 signed words on the RTX 5070. The independent checker matched all 11 rows.
  • The forward DXIL and SPIR-V dumps are byte-identical to the respective original dumps. The forward SPIR-V modules also passed validation.

Port the 32-bit overflow calculation from ESAT586’s Marvisak/VIctory#1095 (d25783b8d87dea346cccd4f8aff90d6dd75f9258). A separately assembled compute fixture compares both the result and SCC against a scalar reference, with an unsigned carry control.

Co-authored-by: ESAT <esatgani_41@hotmail.com>
(cherry picked from commit 6b4bb25)
@ghptc
ghptc force-pushed the fix/shader-scalar-condition-code branch from 6b4bb25 to 9790ba0 Compare November 28, 2026 19:53
@upipibi

upipibi commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

Tested on Ampere: RTX 3070 (10de:2484), driver 616.56, native Windows 11 Pro 10.0.26100, rustc 1.98.1. Debug test profile. The system WARP is the one that ships with Windows. llvm-mc 21.1.8 (AMDGPU), used only to check the test's instruction words.

  • Head 9790ba0c8, merged onto main 77df871e5 (--no-ff, clean, no conflicts). The PR's base is 923e38cc8. Main's only commit after it is a README change, so the merge changes the same two files by the same amounts as the PR: lower.rs +22/-8, valu_tests.rs +84/-2. The head was still 9790ba0c8 when I finished.

Ampere reproduces the refusal on main, and the PR fixes it. With the PR, the 3070 matches all 55 signed words (11 input pairs x 5 words) and the unsigned control. On main's code, the same test is refused with the description's error, at the same offset. No case gives a wrong value instead of a refusal.

The test

cargo test -p victory-shader --lib --locked -- emit::valu_tests::signed_scalar_arithmetic_preserves_scc --exact --nocapture --test-threads=1
VICTORY_WARP=1 cargo test ...   # same command
  • Default, 3 runs: 1 passed, 0 failed, 0 ignored each (0.77 to 0.83 s).
  • VICTORY_WARP=1, 3 runs: 1 passed, 0 failed, 0 ignored each (0.05 s).

The test needs no llvm-mc at run time. The gfx1030 words are hardcoded in valu_tests.rs, and nothing in the crate reads LLVM_MC or calls llvm-mc. So the test does not skip without it. I disassembled each word with llvm-mc -arch=amdgcn -mcpu=gfx1030 --disassemble, and all 18 match their comments. For example, 0xB786FFFF is s_addk_i32 s6, 0xffff, the -1 case.

Unlike most valu_tests, this test asks for one adapter only: devices = [("hardware", false)]. A default run therefore does not test WARP. With VICTORY_WARP=1, Host::new(false) opens WARP, but the readback lines still say hardware.

There is a silent pass: if no SM 6.6 hardware device opens, run_buffer_cases_on prints no SM 6.6 hardware device; a hardware-only case not run and returns. The test then passes without dispatching. That line did not appear in any of my runs.

Adapters seen

I added a temporary eprintln! at the end of Host::new. It resolves device.GetAdapterLuid() through EnumAdapterByLuid and prints GetDesc1 and WaveLaneCountMin/Max. I removed it before the lib suites. The test opens two devices, one for the unsigned shader and one for the signed shader.

run adapter (both devices) vendor:device flags wave lanes
default NVIDIA GeForce RTX 3070 10de:2484 0x0 32-32
VICTORY_WARP=1 Microsoft Basic Render Driver 1414:008c 0x2 (software) 4-4

Values (PR merged)

All 6 runs gave the same values, and every row matches the test's expected. Unsigned control (s_add_u32, UINT_MAX + 1): [00000001, 00000000].

Word 0 is the packed SCC (bit 0 s_add_i32, bit 1 s_sub_i32, bit 2 s_addk_i32 1, bit 3 s_addk_i32 -1). Words 1 to 4 are the results.

a b readback (3070 and WARP)
INT_MAX 1 [00000005, 80000000, 7ffffffe, 80000000, 7ffffffe]
INT_MAX -1 [00000006, 7ffffffe, 80000000, 80000000, 7ffffffe]
INT_MIN 1 [0000000a, 80000001, 7fffffff, 80000001, 7fffffff]
INT_MIN -1 [00000009, 7fffffff, 80000001, 80000001, 7fffffff]
INT_MAX INT_MAX [00000005, fffffffe, 00000000, 80000000, 7ffffffe]
INT_MIN INT_MIN [00000009, 00000000, 00000000, 80000001, 7fffffff]
0 INT_MIN [00000002, 80000000, 80000000, 00000001, ffffffff]
-1 1 [00000000, 00000000, fffffffe, 00000000, fffffffe]
0 1 [00000000, 00000001, ffffffff, 00000001, ffffffff]
1 -1 [00000000, 00000000, 00000002, 00000002, 00000000]
0 0 [00000000, 00000000, 00000000, 00000001, ffffffff]

The INT_MAX, 1 and INT_MIN, 1 rows are the same as the description's RTX 5070 readback.

Negative control: main's lower.rs, PR's tests

git checkout 77df871e5 -- crates/victory-shader/src/emit/lower.rs. git diff --stat 77df871e5 -- crates/: valu_tests.rs +86/-2 (the PR's test and helper) and host.rs +13 (my temporary print). It builds.

3 runs by default and 3 with VICTORY_WARP=1: 0 passed, 1 failed, 0 ignored each. All 6 runs gave the same result:

  • The unsigned control dispatches and passes: [00000001, 00000000] on the 3070 and on WARP.
  • The signed shader never reaches a device. It panics at valu_tests.rs:172 with
    lower: Unsupported { offset: 132, what: "the carry of a scalar operation" }.

This is a refusal at lowering, the same in every run and on both adapters. No case returned a wrong value. The offset and text match the description's fixture-only baseline.

The offset 132 is not an SCC reader. On main, s_add_i32/s_sub_i32/s_addk_i32 store SCC as Val::Missing("the carry of a scalar operation") (lower.rs:7440), and the error appears where that value is used. By my count of the program build_valu_cs makes (a 7-word prologue, then the body), byte 132 is the final buffer_store_dword of v0, which holds the s_cselect/s_or result. This comes from reading the code, not from a measurement.

cargo test -p victory-shader --lib --locked (default, once each)

  • PR merged onto 77df871e5: 349 passed, 0 failed, 8 ignored (317 s).
  • Plain main 77df871e5: 348 passed, 0 failed, 8 ignored (319 s).
  • The extra pass is the new test. There were no failures.

The PR adds no compiler warnings. The one warning in both builds, unused colour, is main's (lower.rs:13896 on main, :13910 on the merge).

CI

gh pr checks 1421: check fails at Clippy, with unused variable: colour (lower.rs:13910) and manual checked division (lower.rs:2213). Both lines are unchanged from main (lower.rs:13896 and :2213), so this PR did not cause either failure. I did not run clippy.

Overlap

Not tested

  • WARP in a default run: the test asks for the hardware adapter only, so WARP data comes from the VICTORY_WARP=1 runs alone.
  • s_addk_i32 with immediates other than 1 and -1, and signed SCC read by s_cbranch_scc* rather than s_cselect_b32.
  • The unsigned s_sub_u32 borrow (only the s_add_u32 carry control is dispatched), and s_addc_u32/s_subb_u32.
  • Native wave64 hosts, AMD or Intel cards, and the Vulkan path (the description's Proton/VKD3D run).
  • A real title shader that uses these instructions, and the game.
  • VICTORY_SHADER_OPT, clippy, the release build, and the rest of the workspace.

AI use: Claude Code (Claude Opus 5.5) ran the tests and wrote this report.

@nick0451

Copy link
Copy Markdown

A smoke test in play: RTX 3090 24 GB (driver 610.88), i9-14900K, Windows 11. A release build of this PR merged onto main 923e38c, run in turn with main and three other PR builds. Series S, DLSS Performance, frame generation off. The same frozen save, outdoors on a balcony, standing still. Two rounds, each run 40 s to settle and 60 s measured, then a screenshot.

  • No crash, device loss or ERROR line. The picture matches main. The shaders that do not translate are the same 10 as main (all no wave32 lowering), and none fails with Unsupported in this scene on main either. So nothing here reaches the S_CSELECT_B32 case; this is a no-harm check only.
  • fps in round 2: 22.8 against main 23.0-23.5 (round 1 was slow for every new build in the session).

Tested with an AI assistant (Claude Code); results are from this PC.

@trustno1-coder
trustno1-coder merged commit ec611f8 into trustno1-coder:main Nov 29, 2026
1 check failed
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.

4 participants