Shaders: lower mul_hi without INT64 #2148

Closed
github-bot wants to merge 1 commit from github-bot/gh-2148 into main AGit
Owner

@⁠scoresby13 on GitHub · 2026-11-25 06:09 UTC · original
scoresby13:shader/mul-hi-no-int64 → main · +163 −10 · 2 files · 1 commits · closed without merging

Summary

Lowers Lower::mul_hi (s_mul_hi_u32, s_mul_hi_i32, v_mul_hi_u32, v_mul_hi_i32, v_mul_hi_u32_u24, v_mul_hi_i32_i24) in victory-shader using pure 32-bit DXIL operations instead of 64-bit integer multiplication, removing flags::INT64 (SFI0 bit 0x8000 / Int64ShaderOps) from shaders that use 32-bit/24-bit high-multiply instructions.

Claimed on #1633.

Details

  • Splits each 32-bit factor into 16-bit halves and computes the exact unsigned 32-bit high dword via 16x16 partial products without intermediate unsigned overflow (Hacker's Delight 8-2).
  • When signed is true, applies sign-bit masks (a >>s 31) & b and (b >>s 31) & a subtracted from the unsigned high word to produce the exact signed high dword.
  • Leaves all six s_mul_hi_*, v_mul_hi_*, and v_mul_hi_*_24 callers on mul_hi without requiring Int64ShaderOps.
  • Adds CPU-only SFI0 validation (emit::lower::tests::mul_hi_needs_no_int64) and GPU/WARP tests (emit::valu_tests::mul_hi_matches_rdna2_without_int64) covering all 144 ordered pairs of 12 edge-case 32-bit values across all 6 instructions.

Testing

  • cargo clippy -p victory-shader --all-targets --locked -- -D warnings (passed)
  • cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings (passed)
  • cargo xtask policy (asset policy: 182 files ok)
  • cargo test -p victory-shader --locked mul_hi -- --nocapture (2 passed, 0 failed)
  • cargo test -p victory-shader --locked the_24_bit_multiplies_lower_both_halves -- --nocapture (1 passed, 0 failed)

No game data was used. AI-assisted.

**@⁠scoresby13** on GitHub · 2026-11-25 06:09 UTC · [original](https://github.com/Marvisak/VIctory/pull/2148) `scoresby13:shader/mul-hi-no-int64` → `main` · +163 −10 · 2 files · 1 commits · closed without merging ## Summary Lowers `Lower::mul_hi` (`s_mul_hi_u32`, `s_mul_hi_i32`, `v_mul_hi_u32`, `v_mul_hi_i32`, `v_mul_hi_u32_u24`, `v_mul_hi_i32_i24`) in `victory-shader` using pure 32-bit DXIL operations instead of 64-bit integer multiplication, removing `flags::INT64` (`SFI0` bit `0x8000` / `Int64ShaderOps`) from shaders that use 32-bit/24-bit high-multiply instructions. Claimed on #1633. ## Details - Splits each 32-bit factor into 16-bit halves and computes the exact unsigned 32-bit high dword via `16x16` partial products without intermediate unsigned overflow (Hacker's Delight 8-2). - When `signed` is true, applies sign-bit masks `(a >>s 31) & b` and `(b >>s 31) & a` subtracted from the unsigned high word to produce the exact signed high dword. - Leaves all six `s_mul_hi_*`, `v_mul_hi_*`, and `v_mul_hi_*_24` callers on `mul_hi` without requiring `Int64ShaderOps`. - Adds CPU-only `SFI0` validation (`emit::lower::tests::mul_hi_needs_no_int64`) and GPU/WARP tests (`emit::valu_tests::mul_hi_matches_rdna2_without_int64`) covering all 144 ordered pairs of 12 edge-case 32-bit values across all 6 instructions. ## Testing - `cargo clippy -p victory-shader --all-targets --locked -- -D warnings` (passed) - `cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings` (passed) - `cargo xtask policy` (`asset policy: 182 files ok`) - `cargo test -p victory-shader --locked mul_hi -- --nocapture` (2 passed, 0 failed) - `cargo test -p victory-shader --locked the_24_bit_multiplies_lower_both_halves -- --nocapture` (1 passed, 0 failed) No game data was used. AI-assisted.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:56:11 +00:00
github-bot closed this pull request 2026-11-26 07:27:19 +00:00
Author
Owner

@⁠sinan-cmyk on GitHub · 2026-11-25 08:20 UTC · original

Offline measurement on a retail dump

Result: no count change against main.

I merged head 8c9e7eba onto main 940d32f (clean merge) and ran victory translate --validate --list on shaders.rpf (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with main program by program (25,889 programs). Validated: 23,918 on main, 23,918 with this PR.

No program changes its failure reason. The count shows that the change rejects nothing on this data. It does not show whether the output is right.

No shader names or data are included, per the asset policy. AI-assisted (Claude Code).

**@⁠sinan-cmyk** on GitHub · 2026-11-25 08:20 UTC · [original](https://github.com/Marvisak/VIctory/pull/2148#issuecomment-7156903317) ### Offline measurement on a retail dump **Result: no count change against `main`.** I merged head 8c9e7eba onto `main` 940d32f (clean merge) and ran `victory translate --validate --list` on `shaders.rpf` (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with `main` program by program (25,889 programs). Validated: 23,918 on `main`, 23,918 with this PR. No program changes its failure reason. The count shows that the change rejects nothing on this data. It does not show whether the output is right. No shader names or data are included, per the asset policy. AI-assisted (Claude Code).
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 11:24 UTC · original

Automated triage for the maintainers.

Needs work. The arithmetic is right. What the PR is missing is a reason to make the change.

What I checked: Lower::mul_hi (crates/victory-shader/src/emit/lower.rs) now builds the high dword from four 16×16 partial products. The steps match Hacker's Delight mulhu, and no intermediate overflows 32 bits. The signed result subtracts (a >> 31) & b and (b >> 31) & a, which is the standard mulhs correction. The 24-bit callers still sign- or zero-extend before the call. mul_hi_matches_rdna2_without_int64 compares all 144 pairs of edge values on the GPU against a 64-bit reference, which is a real check.

Why it isn't mergeable as is:

  • No host benefits that we know of. Main never checks Int64ShaderOps on the host (crates/victory-gpu/src/host.rs checks only the 64-bit atomic caps), and every SM 6.6 GPU VIctory targets exposes it. The offline measurement on the thread found no change in validated programs (23,918 before and after).
  • It costs instructions. One native 64-bit multiply becomes about 20 32-bit operations, in instructions that show up in address and hash math.
  • Programs keep INT64 anyway. It is still set by src64, the s_lshl_b64/s_lshr_b64/s_ashr_i64 path and the 64-bit atomics, so many programs will declare it regardless.

What would change the verdict: a host or driver that refuses Int64ShaderOps and so fails on these programs today, or a measured gain (for example programs that no longer declare INT64 at all, with a host that cares). CI: passing.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 11:24 UTC · [original](https://github.com/Marvisak/VIctory/pull/2148#issuecomment-7160999795) <!-- victory-triage v1 head=8c9e7eba491d049f9ab6c83a92a2e42edd7b5f0d verdict=needs-work at=2026-11-25T11:24:12Z --> Automated triage for the maintainers. **Needs work.** The arithmetic is right. What the PR is missing is a reason to make the change. What I checked: `Lower::mul_hi` (`crates/victory-shader/src/emit/lower.rs`) now builds the high dword from four 16×16 partial products. The steps match Hacker's Delight `mulhu`, and no intermediate overflows 32 bits. The signed result subtracts `(a >> 31) & b` and `(b >> 31) & a`, which is the standard `mulhs` correction. The 24-bit callers still sign- or zero-extend before the call. `mul_hi_matches_rdna2_without_int64` compares all 144 pairs of edge values on the GPU against a 64-bit reference, which is a real check. Why it isn't mergeable as is: - **No host benefits that we know of.** Main never checks `Int64ShaderOps` on the host (`crates/victory-gpu/src/host.rs` checks only the 64-bit atomic caps), and every SM 6.6 GPU VIctory targets exposes it. The offline measurement on the thread found no change in validated programs (23,918 before and after). - **It costs instructions.** One native 64-bit multiply becomes about 20 32-bit operations, in instructions that show up in address and hash math. - **Programs keep `INT64` anyway.** It is still set by `src64`, the `s_lshl_b64`/`s_lshr_b64`/`s_ashr_i64` path and the 64-bit atomics, so many programs will declare it regardless. What would change the verdict: a host or driver that refuses `Int64ShaderOps` and so fails on these programs today, or a measured gain (for example programs that no longer declare `INT64` at all, with a host that cares). CI: passing. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠steven-seagull27 on GitHub · 2026-11-26 03:27 UTC · original

@⁠scoresby13 I measured the partial-product cost raised in triage using your 8c9e7eba merged onto main 63bcca43. On the RTX 5080 and built-in WARP, all six forms on your branch matched the CPU reference for 32,768 input pairs each (196,608/196,608 high dwords per device).

On the RTX, a chain of 64 dependent v_mul_hi_u32 operations per lane took 6.18–6.21 µs with your lowering, 4.13 µs on main, and 4.16 µs using #3007's DXIL intrinsic. A signed 24-bit chain gave 7.62–7.68, 5.66–5.70, and 4.90–4.93 µs respectively. These are medians from two independent runs with the three variants interleaved. The intrinsic keeps your INT64-removal goal and was faster than the partial products on both measured shaders. Fixtures, method and results on #3007.

**@⁠steven-seagull27** on GitHub · 2026-11-26 03:27 UTC · [original](https://github.com/Marvisak/VIctory/pull/2148#issuecomment-7181733066) @⁠scoresby13 I measured the partial-product cost raised in triage using your `8c9e7eba` merged onto main `63bcca43`. On the RTX 5080 and built-in WARP, all six forms on your branch matched the CPU reference for 32,768 input pairs each (196,608/196,608 high dwords per device). On the RTX, a chain of 64 dependent `v_mul_hi_u32` operations per lane took 6.18–6.21 µs with your lowering, 4.13 µs on main, and 4.16 µs using #3007's DXIL intrinsic. A signed 24-bit chain gave 7.62–7.68, 5.66–5.70, and 4.90–4.93 µs respectively. These are medians from two independent runs with the three variants interleaved. The intrinsic keeps your INT64-removal goal and was faster than the partial products on both measured shaders. [Fixtures, method and results on #3007](https://git.gtahub.st/github/Marvisak--VIctory/pulls/3007).
Author
Owner

@⁠scoresby13 on GitHub · 2026-11-26 07:08 UTC · original

Thank you @⁠steven-seagull27 for the RTX 5080 benchmark and @⁠Ingenioeren for #3007! Since #3007 (Shaders: lower the 32-bit mul_hi forms to one UMul/IMul) drops SFI0 INT64 using DXIL's native UMul/IMul intrinsic directly and runs faster without the 16x16 partial-product overhead, I'm closing this PR in favor of #3007.

AI-assisted.

**@⁠scoresby13** on GitHub · 2026-11-26 07:08 UTC · [original](https://github.com/Marvisak/VIctory/pull/2148#issuecomment-7185142560) Thank you @⁠steven-seagull27 for the RTX 5080 benchmark and @⁠Ingenioeren for #3007! Since #3007 (`Shaders: lower the 32-bit mul_hi forms to one UMul/IMul`) drops `SFI0` `INT64` using DXIL's native `UMul`/`IMul` intrinsic directly and runs faster without the 16x16 partial-product overhead, I'm closing this PR in favor of #3007. AI-assisted.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!2148
No description provided.