Skip to content

Shaders: lower v_dot2_f32_f16 and v_dot2c_f32_f16 - #465

Open
Baconzido wants to merge 3 commits into
Marvisak:mainfrom
Baconzido:shaders/v-dot2-f32-f16
Open

Baconzido wants to merge 3 commits into
Marvisak:mainfrom
Baconzido:shaders/v-dot2-f32-f16

Conversation

@Baconzido

@Baconzido Baconzido commented Nov 24, 2026 •

Copy link
Copy Markdown

Two VALU instructions the translator refused (Unsupported { what: "v_dot2_f32_f16" }), so any shader using one dropped its draws or dispatches:

  • v_dot2_f32_f16 (VOP3P): S0.f16[0] * S1.f16[0] + S0.f16[1] * S1.f16[1] + S2.f32, with op_sel/op_sel_hi choosing each source's half per product, neg_lo/neg_hi negating them (neg_lo bit 2 negates the f32 addend) and clamp saturating the result;
  • v_dot2c_f32_f16 (VOP2): the same sum with the destination as the addend, read like v_fmac_f32 reads its accumulator.

Each f16 goes through legacyF16ToF32 as packed16 does, so the halves and their products are exact in f32 (65504 * 65504 does not overflow as it would in f16). The two products and the addend are summed with one rounding, as RDNA2 does (measured on gfx1036 in this PR's comments): Boldo and Melquiond's correctly rounded sum of three, two TwoSums whose errors are added with rounding to odd, then one rounded add. Shortcut, noted in the code: f32 denormals along the way follow the shader's denormal mode. Modifiers the lowering does not model (neg_hi bit 2, SDWA, VOP3-encoded v_dot2c with abs/neg/op_sel, omod) are refused.

This complements #157, which lowers the integer dot products (v_dot2_i32_i16 … v_dot8_u32_u4); there is no overlap.

Tests (new, in emit/valu_tests.rs, all failing before this change with the Unsupported above):

  • v_dot2_f32_f16_sums_f16_products_in_f32: exact cases, mixed signs, 65504², infinity;
  • v_dot2_f32_f16_applies_op_sel_and_negation_per_half: swapped halves of src0 and negation on both halves;
  • v_dot2_f32_f16_clamps_to_zero_one;
  • v_dot2c_f32_f16_accumulates_into_its_destination;
  • v_dot2_f32_f16_rounds_the_exact_sum_once and v_dot2c_f32_f16_rounds_the_exact_sum_once: sums where rounding the products' sum first gives another f32 (these fail with a two-rounding sum).

Expected values come from an independent model of the ISA definition, not from the lowering.

Run on Windows 11 (26300), both backends of run_cases: AMD Radeon RX 5700 (RDNA1, driver 26.11.2) and WARP, with dxil.dll from SDK 10.0.26100 validating every shader: 6/6 pass, on main ffe310e. cargo test --workspace: 348 passed, 1 failed (victory-hle display::sdr_and_display_deferrals, which also fails on main here because the run is over SSH with no desktop). cargo clippy -p victory-shader --all-targets is clean; cargo fmt adds nothing in the changed lines; cargo xtask policy --staged: ok.

AI-assisted (Claude Code wrote the lowering and tests and ran them over SSH); every run is on my machine. No game files.

@towiice towiice left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested:

  • Hardware: NVIDIA GeForce GTX 1650 SUPER (Turing TU116, 4 GB VRAM, 32-wide subgroups / wave32, driver 610.88), Windows 11 x64 (26H2 / 10.0.26300), toolchain stable-x86_64-pc-windows-gnullvm (llvm-mingw 23.1.6).
  • Test command: cargo +stable-x86_64-pc-windows-gnullvm test -p victory-shader
  • Result: 73 passed, 0 failed (6.95 s):
    • v_dot2_f32_f16_sums_f16_products_in_f32 (exact f32 summation without f16 overflow)
    • v_dot2_f32_f16_applies_op_sel_and_negation_per_half (half-swapping & per-half negation)
    • v_dot2_f32_f16_clamps_to_zero_one
    • v_dot2c_f32_f16_accumulates_into_its_destination
      All new VALU test cases pass cleanly without regressions.

@DaviTostes

Copy link
Copy Markdown

Verified aa55c55 on Arch Linux x86_64 (rustc 1.98.1, cargo test -p victory-shader): 56 passed, 0 failed, 0 ignored.

Not covered: the dot products executed on a real GPU (no SM 6.6 device here) and title shaders (no game data).

@marquezinii

Copy link
Copy Markdown
Contributor

Ran 1d20123 merged onto main d2a4bae (clean) on NVIDIA. valu_tests runs each case on the hardware device and on WARP.

  • NVIDIA GeForce RTX 5070 12 GB (32-lane waves), driver 621.24; Windows 11 Pro 26300; Rust 1.98.1 (pinned) x86_64-pc-windows-msvc, debug
  • the four new tests (v_dot2_f32_f16_sums_f16_products_in_f32, …_applies_op_sel_and_negation_per_half, …_clamps_to_zero_one, v_dot2c_f32_f16_accumulates_into_its_destination): ok on both
  • cargo test -p victory-shader --lib -- --test-threads=1: 73 passed, 0 failed
  • cargo test --workspace -- --test-threads=1: 268 passed, 0 failed, 4 ignored

@Lyrnc

Lyrnc commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

RDNA2 hardware measurement of the shortcut:: the hardware rounds once. v_dot2_f32_f16 and v_dot2c_f32_f16 are fused, so the two-rounding sum is not what RDNA2 does.

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. 404 operand sets: 400 random halves in ±4 with an f32 addend chosen close to minus the dot product (so the final rounding is sensitive), plus four built cases. v_dot2c_f32_f16 takes the addend from the destination.

Model v_dot2_f32_f16 v_dot2c_f32_f16
one rounding of a0*b0 + a1*b1 + c (exact) 404/404 404/404
(a0*b0 + a1*b1) rounded, then + c (this PR's order) 353/404 353/404
(a0*b0 + c) rounded, then + a1*b1 336/404 336/404
a0*b0 + (a1*b1 + c) rounded 328/404 328/404

For example, halves 1.0 · 1.0 + 2^-12 · 2^-12 with c = -1.0 gives 0x33800000 (2^-24) on the hardware. Summing the products first rounds 1 + 2^-24 to 1.0 and gives 0.

The products of two halves are exact in f32, as you note, so the hardware result is those three exact terms rounded once. Matching it needs the sum formed without an intermediate rounding before the final one, for example an error-free TwoSum on the f32 terms. A plain f64 sum is not always enough when the terms' exponents are far apart.

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.

@mercuryVM mercuryVM left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested 1d20123 on Windows 11, RTX 5070, MSVC: cargo test --workspace --no-fail-fast passes (268 passed, 0 failed), including the new valu_tests on hardware and WARP. victory translate <title exe> --validate --pipelines gives the same counts before and after: 12 of 12 programs emitted, 12 validated, 12 pipelines. None of these programs use the ops.

  • legacyF16ToF32 (op 131) widens each half exactly, and f16 × f16 is exact in f32, so only the two adds round. The shortcut comment says so.
  • Per-half negation is -0.0 - x, which negates zeros correctly too. The f32 addend goes through f(self, 2), which applies neg_lo bit 2.
  • The modifiers that aren't modelled are refused rather than ignored: neg_hi bit 2, SDWA, omod, and abs/neg/op_sel on the VOP3 form of v_dot2c.
  • v_dot2c reads its accumulator from the destination's current value, the same way the v_fmac_f32 path does.

No issues found.

AI-assisted (Claude Code).

@slint6614-dev

Copy link
Copy Markdown

AMD hardware check, Windows 11, Adrenalin driver: RX 9070 (RDNA4) and Ryzen 9600X iGPU (RDNA2, gfx1036), each picked as the hardware device of run_cases (local adapter-selection patch, not part of this PR); cargo test -p victory-shader --release valu_tests at the PR head. Negative control: the PR's non-test files reverted to its merge base, its new tests kept.

  • With this PR, all four v_dot2* tests pass on both AMD GPUs.
  • Negative control: all four fail on both GPUs without the fix, so the tests do catch the change on AMD hardware.

@slint6614-dev

Copy link
Copy Markdown

Corpus run note: merged locally with current main (c7a5f49), this PR no longer compiles:

error[E0061]: this method takes 4 arguments but 3 arguments were supplied
    --> crates\victory-shader\src\emit\lower.rs:3817:34
3817 |   self.packed_src(inst, k, high)?
     |        ^^^^^^^^^^---------------- argument #4 of type `bool` is missing

packed_src gained a fourth argument on main, so this PR needs a rebase. At its own head it builds, and its GPU tests pass on AMD (see my earlier comment).

@MichaelSmithAI

Copy link
Copy Markdown

Following up on the gfx1036 single-rounding measurement: on an RTX 4090 under Wine/vkd3d-proton, DXIL Dot2AddHalf returned 0x00000000 for the cancellation example above. It also returned 0x4b800000 for packed operands a=b=0x00013c00, addend 0x4b800000; rounding the exact sum once gives 0x4b800001. Both results repeated on vkd3d-proton 3.1 and 3.2.

We compared 1,026 deliberately rounding-sensitive finite inputs against an exact rational reference. With DXC denormal preservation enabled, the direct intrinsic matched 142. A separate compensated FP32 test shader matched all 1,026 on both backends. Without explicit denormal preservation, that same compensated shader matched 770; all 256 misses were subnormal results after cancellation.

These counts describe this constructed test set. The compensated shader is an experimental reference. Guest denormal mode, NaNs, infinities, and rendering cost remain open before adopting a lowering. No title shaders were used.

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Textually clean merge with current main (ffe310e), but it fails to compile after merge — a semantic conflict: main's Lower::packed_src now takes a 4th bool argument, and this PR's call in crates/victory-shader/src/emit/lower.rs:3973 still passes 3:

error[E0061]: this method takes 4 arguments but 3 arguments were supplied
    --> crates/victory-shader/src/emit/lower.rs:3973:34
     = note: argument #4 of type `bool` is missing

Needs a rebase onto current main plus updating that call site with the new flag. I'll re-review once it's green.

@Baconzido
Baconzido force-pushed the shaders/v-dot2-f32-f16 branch from 1d20123 to 2b0d00b Compare November 24, 2026 12:40
Baconzido added a commit to Baconzido/VIctory that referenced this pull request Nov 24, 2026
RDNA2 rounds the exact sum of the two f16 products and the f32 addend
once (measured on gfx1036 in Marvisak#465), where summing the products first
rounds twice: (1, 2^-12) . (1, 2^-12) - 1 gives 2^-24 on the hardware
and 0 that way.

The sum is now Boldo and Melquiond's correctly rounded sum of three:
two TwoSums, their errors added with rounding to odd, then one final
rounded add. A zero tail keeps the sign of an all-negative-zero sum,
and an infinite or NaN term gives the plain sum.
@Baconzido

Copy link
Copy Markdown
Author

Thanks for the measurements. Both points are addressed, and the branch is rebased on main ffe310e.

Rounding (@Lyrnc, @MichaelSmithAI). 2b0d00b sums the two products and the addend with a single rounding, using Boldo and Melquiond's correctly rounded sum of three: two TwoSums, their errors added with rounding to odd, then one rounded add. A zero tail keeps the sign of an all-negative-zero sum, and an infinite or NaN term falls back to the plain sum. The shortcut: comment is gone. One limit is written down instead: f32 denormals along the way follow the shader's denormal mode, which the translator doesn't set (the subnormal misses @MichaelSmithAI saw).

New tests, v_dot2_f32_f16_rounds_the_exact_sum_once and v_dot2c_f32_f16_rounds_the_exact_sum_once:

  • (1, 2^-12) . (1, 2^-12) - 1 → 0x33800000, the gfx1036 result above;
  • (1, 2^-14) . (1, 2^-14) + 2^24 → 0x4b800001, the tie case (a normal-half variant of 0x00013c00), which needs the round-to-odd step;
  • (-0, -0) . (1, 1) - 0 → -0.

Rebase (@slint6614-dev). packed_src now takes float = true, so inline constants read as halves, as in packed16.

Tested on Windows 11, AMD Radeon RX 5700 (RDNA1, driver 26.11.2) and WARP, dxil.dll from SDK 10.0.26100, Rust 1.98.1:

Result
new tests with the old lowering (e1da143's lower.rs) fail on the hardware device: 0x00000000, want 0x33800000
cargo test -p victory-shader --lib dot2 6 passed, hardware and WARP
cargo test -p victory-shader 126 passed, 0 failed, 1 ignored
cargo test --workspace --no-fail-fast 348 passed; 1 failed: victory-hle display::sdr_and_display_deferrals, which opens a window; it failed the same way over SSH on main d2a4bae
cargo clippy -p victory-shader --all-targets clean

I also checked the algorithm against an exact rational reference with subnormals kept: 40,000 random cases, 40% of them cancellations, all match. No game data used.

AI use: Claude Code (Claude Opus 5.5) wrote the change, the tests and this comment; I ran the tests on the hardware above.

nazmi pushed a commit to nazmi/VIctory that referenced this pull request Nov 24, 2026
Applied as the net diff of upstream PR Marvisak#465 (head 2b0d00b), 3 commits: 2b0d00b,e1da1435,4c879baf.
@Lyrnc

Lyrnc commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Re-checked 2b0d00b against the hardware, using the same encodings on both sides:

  • Hardware: Radeon iGPU (Ryzen 7 9800X3D), gfx1036 / RDNA2, driver 32.0.21042.62. I ran v_dot2_f32_f16 (VOP3P, default op_sel_hi) and v_dot2c_f32_f16 (VOP2) on 554 inputs:
    • your three cases: 0x33800000, the 0x4b800001 tie, and -0, which the hardware gives exactly;
    • 400 random cancellations where c ≈ -(a·b);
    • 150 combinations of half edges (±0, ±inf, NaN, denormals, max) with f32 addends (±0, ±inf, NaN, a denormal, 2^24).
  • Translator: the same words through build_valu_cs, on the RTX 5080 (610.88) and on the same Radeon iGPU (GPU: prefer high-performance D3D12 adapter and support VICTORY_ADAPTER #94's VICTORY_ADAPTER).
Host v_dot2_f32_f16 v_dot2c_f32_f16
Radeon gfx1036 506 exact, 48 NaN payload, 0 other 506 exact, 48 NaN payload, 0 other
RTX 5080 446 exact, 108 NaN payload, 0 other 446 exact, 108 NaN payload, 0 other

The single rounding matches the hardware on every non-NaN result. That includes the cancellations, which an earlier head got wrong. The only differences are which NaN comes back. This run used the shader's default denormal mode, so it doesn't speak to the denormal limit you wrote down. No game data.

AI use: Claude Code (Claude Opus 5.5) wrote the probe and the comparison and ran them; the numbers are from those runs.

The two products of f16 halves, exact in f32, summed with the f32 addend (v_dot2c: the destination). VOP3P op_sel/op_sel_hi pick each half, neg_lo/neg_hi negate them and clamp saturates; modifiers not modelled are refused. Tests run each case on the host GPU and WARP.
RDNA2 rounds the exact sum of the two f16 products and the f32 addend
once (measured on gfx1036 in Marvisak#465), where summing the products first
rounds twice: (1, 2^-12) . (1, 2^-12) - 1 gives 2^-24 on the hardware
and 0 that way.

The sum is now Boldo and Melquiond's correctly rounded sum of three:
two TwoSums, their errors added with rounding to odd, then one final
rounded add. A zero tail keeps the sign of an all-negative-zero sum,
and an infinite or NaN term gives the plain sum.
@Baconzido
Baconzido force-pushed the shaders/v-dot2-f32-f16 branch from 2b0d00b to bdced0f Compare November 24, 2026 22:16
@Baconzido

Baconzido commented Nov 24, 2026 •

Copy link
Copy Markdown
Author

Rebased on main 70564ae. The conflicts were only in placement: main's new narrow16 and the upscaler dot-product tests landed where this PR adds its helpers and tests, so both sides are kept. Against main the diff is additions only. The one other change is the wording of the denormal limit, which now follows main's convention: f32 denormals inside the sum follow the host's mode, not the console's, which reads them as zero (ftz). No behavior change.

Re-run on Windows 11, AMD Radeon RX 5700 (RDNA1, driver 26.11.2) and WARP, dxil.dll from SDK 10.0.26100, Rust 1.98.1, head bdced0f:

Result
cargo test -p victory-shader --lib dot2 6 passed, hardware and WARP
cargo test -p victory-shader 214 passed, 1 failed, 2 ignored
cargo test --workspace --no-fail-fast 480 passed, 2 failed, 7 ignored
cargo clippy -p victory-shader --all-targets clean

Neither failure comes from this branch:

  • victory-hle display::sdr_and_display_deferrals opens a window and fails over SSH, as before.
  • fspx_programs_run_with_the_hardwares_dot_products fails the same way on plain main 70564ae, on the RX 5700 device only: the first case ([cc164000, 1c0e0501]) gives 0x80007FF5, want 0x7FFFFBF5. The RX 5700 is RDNA1, so this is likely how its driver handles dot4AddPacked, not the lowering.

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate. Lowers v_dot2_f32_f16 (VOP3P) and v_dot2c_f32_f16 (VOP2) in crates/victory-shader/src/emit/lower.rs: the two exact f16 products and the addend are summed with a single rounding (sum3_rounded_once: two TwoSums, then round-to-odd), and unsupported modifiers are refused. It uses main's current 4-argument packed_src and reads the accumulator the same way v_fmac_f32 does; main has no other lowering of these. Hardware evidence: 554 inputs on gfx1036 matched except the NaN payload. The six new tests in valu_tests.rs fail on the old lowering; the known gap (f32 denormals follow the host mode) is documented in the code.

CI: no check runs on the head commit.


Generated by Claude Code

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants