Correct the SCC of scalar shift-adds, give signed scalar adds their overflow, and make exact f16 rounding cheaper #345

Closed
github-bot wants to merge 634 commits from github-bot/gh-345 into main AGit
Owner

@⁠zhaochengggg on GitHub · 2026-11-27 18:47 UTC · original
zhaochengggg:fix/scalar-carry-and-f16-rounding → main · +60949 −5190 · 201 files · 634 commits · closed without merging

Base: main at 10675b5
Translator version: 15 → 16

What this changes

  1. s_lshl1_add_u32 … s_lshl4_add_u32 set SCC to the carry. The ISA defines SCC as the carry out of (S0 << n) + S1 taken in 64 bits: bits shifted out of the top count as well as the add's own carry. The lowering returned no carry, so SCC fell through to the default "result is not zero". A shader that branches on that SCC, or feeds it to s_addc_u32, got the wrong value.
  2. s_add_i32, s_sub_i32 and s_addk_i32 produce their SCC: signed overflow, as the ISA pseudocode defines it. A shader that read it was refused with "the carry of a scalar operation". It now translates.
  3. f16_nearest (VICTORY_F16_NEAREST=1, off by default) is computed on the bits. It uses the method of F. Giesen's public-domain float_to_half_fast3_rtne. The old form used four conversion ops (one f32tof16, three f16tof32). The new one uses integer ops and one f32 add. The results are identical (checked below). The new form also no longer depends on how the host's f32tof16 rounds.
  4. New test module emit/alu_diff_tests.rs: a differential test of the integer ALU and the conversions. Each case is a one-instruction RDNA2 compute shader with a reference model written from AMD's ISA definitions. Inputs are edge values plus seeded random ones. Each case runs on the hardware adapter and on WARP. The test compares the result and the flags: SCC, VCC, the VOP3 carry-out, and, for ops the ISA says leave SCC alone, that SCC is unchanged. VICTORY_ALU_DIFF_ROUNDS=<n> multiplies the random inputs.

Unused flag values are removed by the default DEAD pass, so DXIL only changes where an SCC from these instructions is actually read. The version bump covers any cached translation that reads one.

How it was found and checked

Test PC: RTX 5070 Ti 16 GB (driver 621.14), 24-core / 32-thread Intel, 48 GB, Windows 11 26100. Also WARP. No title files were used: every shader is synthetic.

Run Cases GPU runs Wrong Refused by the translator
Integer ALU, main (first harness version) 401 25,920 32 result lines (s_lshl{1-4}_add_u32 SCC, hardware and WARP) + 72 WARP pipeline-creation failures 41: 6 signed-add SCC reads (now translated), 6 harness artifacts (since fixed), 29 unimplemented ops
Integer ALU, this branch 401 26,880 0 (WARP pipeline-creation failures for lane-mask ops are counted as skips; the hardware runs them) 29 unimplemented ops
Conversions, VICTORY_F16_NEAREST=1, this branch 41 2,576 0 (one WARP-only v_cvt_f32_u32 rounding slip is filtered; the hardware matches) 14
  • On main, all of s_lshl{1,2,3,4}_add_u32 gave SCC = 1 for inputs such as (0 << 1) + 1, on both the hardware and WARP. The ISA gives 0.
  • Every op the ISA says writes no SCC (s_mul_hi_*, s_bfm_b32, s_pack_*, s_ff1/s_flbit, s_bitset*, s_cmov, s_mulk_i32, …) left a preset SCC unchanged. This confirms ir::writes_scc.
  • With the default host conversion, f32→f16 results (v_cvt_f16_f32, v_cvt_f16_u16, v_cvt_f16_i16) round toward zero on the RTX 5070 Ti and on WARP. Examples: 0.49999997 gives 0x37FF instead of 0x3800; 3059 gives 3058 instead of 3060; 65520 and up give 65504 instead of +inf. This is the documented shortcut. With VICTORY_F16_NEAREST=1 every case matches the ISA, before and after this change.

Cost of exact f16 rounding (bench_f16_rounding_cost, ignored by default)

The benchmark runs 4096 groups × 64 threads × 256 iterations of four dependent 16-long v_add_f16 chains. Times are the GPU time per dispatch: median of 10 samples of 64 timed dispatches, repeated in two runs.

f16 result rounding DXIL bytes µs per dispatch
Host f32tof16 (default, rounds toward zero on NVIDIA) 5,248 628–633
VICTORY_F16_NEAREST=1, main 15,852 4,120–4,132
VICTORY_F16_NEAREST=1, this branch 11,272 3,643

This makes the exact path about 12% faster and 29% smaller in DXIL. It is still about 5.8× the default in this worst case, where every op is a half add. Whether the default should change is a decision for an in-game A/B run, which this PR does not make.

Regression tests

  • cargo clippy --workspace --all-targets --locked -- -D warnings, and the same with --all-features: clean.
  • cargo test -p victory-shader: 342 passed. The only 3 failures (rect::tests ×2, barycentric_tests::first_half_values…) fail identically on main on this PC. The validator there is the Windows SDK 10.0.22000 dxil.dll (1.6.2104), which rejects the PSV0 part of the rectangle GS and the barycentric PS. See the notes below.
  • cargo test -p victory-gpu: 496 passed.
  • cargo xtask: passes.

What this does not show

  • No in-game run. I don't know how often the title's shaders read the SCC of a shift-add or a signed add. The bump re-translates the cache once. It would help if someone with the title could run victory shaders on this branch and report how many translations changed.
  • The ops still refused are listed for information only. I found no sign in history, docs or issues that the title uses them, so this PR doesn't implement them:
    • s_absdiff_i32, s_cmov_b32, s_bcnt0/s_ff0_i32_b32, s_sext_i32_i8/i16, s_brev_b64, s_flbit_i32_b64
    • v_mul_lo_i32, v_add_nc_i32/v_sub_nc_i32, v_add_nc_i16/v_sub_nc_i16, v_mad_u32_u16/v_mad_i32_i16, v_lerp_u8, v_cmp_{f,t}_{i,u}64
    • v_frexp_*_f32, v_cvt_pknorm_*, v_cvt_pk_{u16_u32,i16_i32}, v_ldexp_f32, the f64 conversions

Notes for testers (not part of the change)

  • On a PC whose newest Windows SDK is 10.0.22000, three shader tests fail on main because that SDK's validator rejects PSV0.
  • victory-hle's lifecycle::tests::direct_close_suspends_and_exits and the diagnostics replay test fail when run over SSH. They need an interactive desktop for windows and swap chains.

🤖 Generated with Claude Code

**@⁠zhaochengggg** on GitHub · 2026-11-27 18:47 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/345) `zhaochengggg:fix/scalar-carry-and-f16-rounding` → `main` · +60949 −5190 · 201 files · 634 commits · closed without merging **Base:** `main` at `10675b5` **Translator version:** 15 → 16 ## What this changes 1. **`s_lshl1_add_u32` … `s_lshl4_add_u32` set SCC to the carry.** The ISA defines SCC as the carry out of `(S0 << n) + S1` taken in 64 bits: bits shifted out of the top count as well as the add's own carry. The lowering returned no carry, so SCC fell through to the default "result is not zero". A shader that branches on that SCC, or feeds it to `s_addc_u32`, got the wrong value. 2. **`s_add_i32`, `s_sub_i32` and `s_addk_i32` produce their SCC: signed overflow, as the ISA pseudocode defines it.** A shader that read it was refused with "the carry of a scalar operation". It now translates. 3. **`f16_nearest` (`VICTORY_F16_NEAREST=1`, off by default) is computed on the bits.** It uses the method of F. Giesen's public-domain `float_to_half_fast3_rtne`. The old form used four conversion ops (one `f32tof16`, three `f16tof32`). The new one uses integer ops and one f32 add. The results are identical (checked below). The new form also no longer depends on how the host's `f32tof16` rounds. 4. **New test module `emit/alu_diff_tests.rs`: a differential test of the integer ALU and the conversions.** Each case is a one-instruction RDNA2 compute shader with a reference model written from AMD's ISA definitions. Inputs are edge values plus seeded random ones. Each case runs on the hardware adapter and on WARP. The test compares the result and the flags: SCC, VCC, the VOP3 carry-out, and, for ops the ISA says leave SCC alone, that SCC is unchanged. `VICTORY_ALU_DIFF_ROUNDS=<n>` multiplies the random inputs. Unused flag values are removed by the default `DEAD` pass, so DXIL only changes where an SCC from these instructions is actually read. The version bump covers any cached translation that reads one. ## How it was found and checked Test PC: RTX 5070 Ti 16 GB (driver 621.14), 24-core / 32-thread Intel, 48 GB, Windows 11 26100. Also WARP. No title files were used: every shader is synthetic. | Run | Cases | GPU runs | Wrong | Refused by the translator | |---|---|---|---|---| | Integer ALU, `main` (first harness version) | 401 | 25,920 | **32** result lines (`s_lshl{1-4}_add_u32` SCC, hardware and WARP) + 72 WARP pipeline-creation failures | 41: 6 signed-add SCC reads (now translated), 6 harness artifacts (since fixed), 29 unimplemented ops | | Integer ALU, this branch | 401 | 26,880 | **0** (WARP pipeline-creation failures for lane-mask ops are counted as skips; the hardware runs them) | 29 unimplemented ops | | Conversions, `VICTORY_F16_NEAREST=1`, this branch | 41 | 2,576 | 0 (one WARP-only `v_cvt_f32_u32` rounding slip is filtered; the hardware matches) | 14 | - On `main`, all of `s_lshl{1,2,3,4}_add_u32` gave SCC = 1 for inputs such as `(0 << 1) + 1`, on both the hardware and WARP. The ISA gives 0. - Every op the ISA says writes no SCC (`s_mul_hi_*`, `s_bfm_b32`, `s_pack_*`, `s_ff1`/`s_flbit`, `s_bitset*`, `s_cmov`, `s_mulk_i32`, …) left a preset SCC unchanged. This confirms `ir::writes_scc`. - With the default host conversion, f32→f16 results (`v_cvt_f16_f32`, `v_cvt_f16_u16`, `v_cvt_f16_i16`) round toward zero on the RTX 5070 Ti and on WARP. Examples: 0.49999997 gives `0x37FF` instead of `0x3800`; 3059 gives 3058 instead of 3060; 65520 and up give 65504 instead of +inf. This is the documented shortcut. With `VICTORY_F16_NEAREST=1` every case matches the ISA, before and after this change. ### Cost of exact f16 rounding (`bench_f16_rounding_cost`, ignored by default) The benchmark runs 4096 groups × 64 threads × 256 iterations of four dependent 16-long `v_add_f16` chains. Times are the GPU time per dispatch: median of 10 samples of 64 timed dispatches, repeated in two runs. | f16 result rounding | DXIL bytes | µs per dispatch | |---|---|---| | Host `f32tof16` (default, rounds toward zero on NVIDIA) | 5,248 | 628–633 | | `VICTORY_F16_NEAREST=1`, `main` | 15,852 | 4,120–4,132 | | `VICTORY_F16_NEAREST=1`, this branch | 11,272 | 3,643 | This makes the exact path about 12% faster and 29% smaller in DXIL. It is still about 5.8× the default in this worst case, where every op is a half add. Whether the default should change is a decision for an in-game A/B run, which this PR does not make. ### Regression tests - `cargo clippy --workspace --all-targets --locked -- -D warnings`, and the same with `--all-features`: clean. - `cargo test -p victory-shader`: 342 passed. The only 3 failures (`rect::tests` ×2, `barycentric_tests::first_half_values…`) fail identically on `main` on this PC. The validator there is the Windows SDK 10.0.22000 `dxil.dll` (1.6.2104), which rejects the PSV0 part of the rectangle GS and the barycentric PS. See the notes below. - `cargo test -p victory-gpu`: 496 passed. - `cargo xtask`: passes. ## What this does not show - No in-game run. I don't know how often the title's shaders read the SCC of a shift-add or a signed add. The bump re-translates the cache once. It would help if someone with the title could run `victory shaders` on this branch and report how many translations changed. - The ops still refused are listed for information only. I found no sign in history, docs or issues that the title uses them, so this PR doesn't implement them: - `s_absdiff_i32`, `s_cmov_b32`, `s_bcnt0`/`s_ff0_i32_b32`, `s_sext_i32_i8`/`i16`, `s_brev_b64`, `s_flbit_i32_b64` - `v_mul_lo_i32`, `v_add_nc_i32`/`v_sub_nc_i32`, `v_add_nc_i16`/`v_sub_nc_i16`, `v_mad_u32_u16`/`v_mad_i32_i16`, `v_lerp_u8`, `v_cmp_{f,t}_{i,u}64` - `v_frexp_*_f32`, `v_cvt_pknorm_*`, `v_cvt_pk_{u16_u32,i16_i32}`, `v_ldexp_f32`, the f64 conversions ## Notes for testers (not part of the change) - On a PC whose newest Windows SDK is 10.0.22000, three shader tests fail on `main` because that SDK's validator rejects PSV0. - `victory-hle`'s `lifecycle::tests::direct_close_suspends_and_exits` and the diagnostics `replay` test fail when run over SSH. They need an interactive desktop for windows and swap chains. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
- s_lshl1..4_add_u32: SCC is the carry of (S0 << n) + S1 taken in 64 bits,
  not "the result is not zero".
- s_add_i32, s_sub_i32, s_addk_i32: SCC is the signed overflow; shaders
  that read it were refused before.
- f16_nearest (VICTORY_F16_NEAREST=1): rounded on the bits (after F. Giesen's
  public-domain float_to_half_fast3_rtne) instead of with four conversion
  ops; same results, 12% faster and 29% smaller in an f16-bound benchmark
  on an RTX 5070 Ti.
- emit/alu_diff_tests.rs: one-instruction RDNA2 shaders against a model of
  the ISA on the host GPU and WARP (401 integer cases, 41 conversions), and
  an ignored benchmark of exact f16 rounding.

TRANSLATOR_VERSION 15 -> 16.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 18:48:01 +00:00
Author
Owner

@⁠TaynanGT on GitHub · 2026-11-27 19:35 UTC · original

@⁠zhaochengggg The WARP cases stop at compute pipeline creation with The parameter is incorrect, before the value comparisons. The shift-add SCC fix would be easier to verify on its own with one WARP case that creates and dispatches the pipeline on the Windows runner. CI run

**@⁠TaynanGT** on GitHub · 2026-11-27 19:35 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/345#issuecomment-7226676988) @⁠zhaochengggg The WARP cases stop at compute pipeline creation with `The parameter is incorrect`, before the value comparisons. The shift-add SCC fix would be easier to verify on its own with one WARP case that creates and dispatches the pipeline on the Windows runner. [CI run](https://github.com/TaynanGT/Vice-Tay/actions/runs/46052068118)
Author
Owner

@⁠zhaochengggg on GitHub · 2026-11-28 01:27 UTC · original

Thanks. The failures were in the harness, not in the values: the runner has no hardware adapter, so WARP was the only device, and the cases whose programs declare the console's 32-lane wave size (lane masks read as numbers: v_cmp_*, the VOP3 carry-outs, v_mad_u64_u32) were counted as wrong when WARP refused their pipelines. The skip only applied when a hardware adapter was there as well.

fd03777:

  • A WARP pipeline failure is skipped only for a program that declares a wave size (lower's wave_size), with or without hardware; without hardware such a case is listed as "run on no device". Any other pipeline failure is still wrong.
  • scalar_shift_add_scc_is_the_64_bit_carry_on_warp: s_lshl{1..4}_add_u32 on WARP only (pipeline creation and dispatch must succeed), from a table of expected values with rows where the 64-bit carry differs from "the result is not zero". Each row runs with SCC preset to 0 and to 1.
  • signed_scalar_add_scc_is_the_overflow_on_warp: the same for s_add_i32, s_sub_i32, s_addk_i32, with rows where the signed overflow differs from the unsigned carry.

With main's lower.rs both new tests fail (s_lshl1_add_u32 inputs (0, 1): SCC 1, the ISA gives 0; the signed ones are refused); with this branch they pass. Locally (RTX 5070 Ti + WARP) the full differential run is unchanged: 401 cases, 26,880 runs, 0 wrong, 72 not made on WARP (fixed wave size). Clippy is clean in both configurations.

**@⁠zhaochengggg** on GitHub · 2026-11-28 01:27 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/345#issuecomment-7231678777) Thanks. The failures were in the harness, not in the values: the runner has no hardware adapter, so WARP was the only device, and the cases whose programs declare the console's 32-lane wave size (lane masks read as numbers: `v_cmp_*`, the VOP3 carry-outs, `v_mad_u64_u32`) were counted as wrong when WARP refused their pipelines. The skip only applied when a hardware adapter was there as well. fd03777: - A WARP pipeline failure is skipped only for a program that declares a wave size (`lower`'s `wave_size`), with or without hardware; without hardware such a case is listed as "run on no device". Any other pipeline failure is still wrong. - `scalar_shift_add_scc_is_the_64_bit_carry_on_warp`: `s_lshl{1..4}_add_u32` on WARP only (pipeline creation and dispatch must succeed), from a table of expected values with rows where the 64-bit carry differs from "the result is not zero". Each row runs with SCC preset to 0 and to 1. - `signed_scalar_add_scc_is_the_overflow_on_warp`: the same for `s_add_i32`, `s_sub_i32`, `s_addk_i32`, with rows where the signed overflow differs from the unsigned carry. With `main`'s `lower.rs` both new tests fail (`s_lshl1_add_u32` inputs `(0, 1)`: SCC 1, the ISA gives 0; the signed ones are refused); with this branch they pass. Locally (RTX 5070 Ti + WARP) the full differential run is unchanged: 401 cases, 26,880 runs, 0 wrong, 72 not made on WARP (fixed wave size). Clippy is clean in both configurations.
On a runner without a graphics card WARP is the only device, and the
cases whose programs declare the console's 32-lane wave size (lane masks
read as numbers: v_cmp_*, the VOP3 carry-outs, v_mad_u64_u32) were counted
as wrong when WARP refused their pipelines. A WARP pipeline failure is now
skipped only for a program that declares a wave size, with or without a
hardware adapter; such a case is listed as run on no device when there is
no hardware. Any other pipeline failure is still wrong.

Two small tests check the SCC changes on WARP alone, from tables of
expected values: s_lshl{1..4}_add_u32 (the 64-bit carry, with rows where it
differs from "the result is not zero") and s_add_i32 / s_sub_i32 /
s_addk_i32 (the signed overflow, with rows where it differs from the
unsigned carry). Each row runs with SCC preset to 0 and to 1. Both fail on
main and pass here.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Author
Owner

@⁠TaynanGT on GitHub · 2026-11-29 11:24 UTC · original

Thanks for distinguishing unsupported wave-size pipelines from incorrect results. The main-versus-branch WARP controls with both initial SCC values are useful. Please keep cases that ran on no device explicit in the output.

**@⁠TaynanGT** on GitHub · 2026-11-29 11:24 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/345#issuecomment-7255085788) Thanks for distinguishing unsupported wave-size pipelines from incorrect results. The main-versus-branch WARP controls with both initial SCC values are useful. Please keep cases that ran on no device explicit in the output.
Author
Owner

@⁠zhaochengggg on GitHub · 2026-11-29 17:22 UTC · original

Thanks. They are: the summary line counts them, and the next line names each one ("run on no device (no hardware adapter): …"). The CI run for fd03777 is waiting for approval.

**@⁠zhaochengggg** on GitHub · 2026-11-29 17:22 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/345#issuecomment-7259640345) Thanks. They are: the summary line counts them, and the next line names each one ("run on no device (no hardware adapter): …"). The CI run for fd03777 is waiting for approval.
Author
Owner

@⁠TaynanGT on GitHub · 2026-11-29 19:02 UTC · original

Great, that explicit list is exactly what I wanted. I'll treat the pending CI run separately from the local differential results; unsupported fixed-wave cases should not count as successful executions.

**@⁠TaynanGT** on GitHub · 2026-11-29 19:02 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/345#issuecomment-7260952852) Great, that explicit list is exactly what I wanted. I'll treat the pending CI run separately from the local differential results; unsupported fixed-wave cases should not count as successful executions.
github-bot closed this pull request 2026-11-30 17:14:46 +00:00
Author
Owner

@⁠TaynanGT on GitHub · 2026-11-30 17:11 UTC · original

main has moved to a new line of the code and everything is now named Vice-Tay, so this PR no longer applies as it is (details in #468). If you'd like this change in, please rebuild it on the current main and open a new PR; I'll review it there.

**@⁠TaynanGT** on GitHub · 2026-11-30 17:11 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/345#issuecomment-7285864198) `main` has moved to a new line of the code and everything is now named Vice-Tay, so this PR no longer applies as it is (details in #468). If you'd like this change in, please rebuild it on the current `main` and open a new PR; I'll review it there.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.