fix(shader): match v_dot2_f32_f16 and v_dot2c_f32_f16 to hardware #669

Merged
adriadam10 merged 1 commit from upstream-pr/669 into main 2026-10-05 23:25:50 +00:00
adriadam10 commented 2026-10-05 13:20:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-05T13:20:53Z.


What

v_dot2_f32_f16 and v_dot2c_f32_f16 now match the hardware bit for bit. Main computed them as two chained Fmas, which differs from the hardware on every variant the decoder accepts.

Behaviour measured on HW (RDNA2 gfx1035 oracle, 2048 random + special rows per variant, IEEE=0 DENORM=0 and IEEE=1 DENORM=3 give identical results). v_dot2c is bit for bit v_dot2 with the accumulator in vdst:

  • One rounding (RNE) of a.lo*b.lo + a.hi*b.hi + c. Chained FMAs round twice and differ on ~2% of random rows.
  • f16 denormal inputs are kept, and an f32 denormal accumulator is flushed to a signed zero in both denorm modes.
  • NaN: the first NaN in the order a.lo, b.lo, a.hi, b.hi, c, quieted. An f16 NaN becomes sign | 0x7fc00000 | payload << 13. Invalid results (inf*0, inf-inf) give 0xffc00000.
  • An exact zero result is -0 only when all three terms are -0.
  • neg_lo/neg_hi flip the sign bit of the half, NaNs included.
  • clamp is ignored on v_dot2_f32_f16: 0 differences in 2048 rows with and without it, many of the results outside [0, 1]. Main applied it, and the old test asserted it.
  • DPP v_dot2c: the accumulator comes from the lane itself, not from the DPP source lane (main read it through inst.destination, which carries the DPP).
  • An inline constant 1.0 as a packed source is 0x00003c00.

The emitter computes the sum exactly in f64 with the existing TwoSum and AddRoundToOdd helpers (the Boldo-Melquiond correctly rounded 3-term sum: TwoSum twice, then round-to-odd), then rounds to f32 on the bits. NaN, infinity, invalid and zero results are chosen on the bits, so the result doesn't depend on the driver's Fma fusing, NaN payloads or denormal handling. The two f16 products are exact in f64, and no f64 denormal can appear. Before writing the emitter I checked this algorithm in Python against an exact rational model: 0 mismatches in 300k inputs, cancellation cases included.

Float16Dot.cpp is regenerated from the hardware results: 32 rows × 10 variants (plain, both neg patterns, op_sel on src0 and on src1, clamp, inline constant, literal, DPP v_dot2c). The rows are picked to cover rounding differences, NaN from each source, sNaN, signs, infinities, invalid results, signed zeros, f16 denormals and denormal accumulators. The existing refusal checks for the accumulator op_sel/neg_hi are kept. The old test computed its expected values with the same chained fma, so it agreed with main. Under the hardware rule, 22 of its own rows were wrong.

Tested

Linux, RADV (Mesa 26.2) + local lavapipe + CI's lavapipe (Mesa 25.2.8), agc_driver_float16_dot_tests passes. Cold on CI's lavapipe: 0 s. Full Release ctest: the only failures are agc_driver_pixel_helper_lanes, agc_driver_pixel_lds and agc_driver_pixel_lds_lane_slots, which also fail on main with local RADV (wave32 lane 32 on RADV's 64-wide subgroup). They are unrelated to this PR.
Without the fix, the new test fails on all 10 variants (main: 11-27 lanes per variant, 25 on DPP). Negative checks, each one failing the test: double rounding (truncation, ties away), accumulator denormal kept, accumulator sNaN not quieted, f16 NaN sign dropped, NaN source order, invalid sign, infinity sign, zero sign, f16 denormal scale, clamp applied, DPP accumulator read from the DPP lane, neg not applied on the bits.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4745541728 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/669). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-05T13:20:53Z. --- ### What `v_dot2_f32_f16` and `v_dot2c_f32_f16` now match the hardware bit for bit. Main computed them as two chained `Fma`s, which differs from the hardware on every variant the decoder accepts. Behaviour measured on HW (RDNA2 gfx1035 oracle, 2048 random + special rows per variant, `IEEE=0 DENORM=0` and `IEEE=1 DENORM=3` give identical results). `v_dot2c` is bit for bit `v_dot2` with the accumulator in vdst: - **One rounding** (RNE) of `a.lo*b.lo + a.hi*b.hi + c`. Chained FMAs round twice and differ on ~2% of random rows. - **f16 denormal inputs are kept**, and **an f32 denormal accumulator is flushed to a signed zero** in both denorm modes. - **NaN**: the first NaN in the order a.lo, b.lo, a.hi, b.hi, c, quieted. An f16 NaN becomes `sign | 0x7fc00000 | payload << 13`. Invalid results (inf*0, inf-inf) give `0xffc00000`. - An exact zero result is -0 only when all three terms are -0. - `neg_lo`/`neg_hi` flip the sign bit of the half, NaNs included. - **`clamp` is ignored** on `v_dot2_f32_f16`: 0 differences in 2048 rows with and without it, many of the results outside [0, 1]. Main applied it, and the old test asserted it. - **DPP `v_dot2c`**: the accumulator comes from the lane itself, not from the DPP source lane (main read it through `inst.destination`, which carries the DPP). - An inline constant `1.0` as a packed source is `0x00003c00`. The emitter computes the sum exactly in f64 with the existing `TwoSum` and `AddRoundToOdd` helpers (the Boldo-Melquiond correctly rounded 3-term sum: TwoSum twice, then round-to-odd), then rounds to f32 on the bits. NaN, infinity, invalid and zero results are chosen on the bits, so the result doesn't depend on the driver's `Fma` fusing, NaN payloads or denormal handling. The two f16 products are exact in f64, and no f64 denormal can appear. Before writing the emitter I checked this algorithm in Python against an exact rational model: 0 mismatches in 300k inputs, cancellation cases included. `Float16Dot.cpp` is regenerated from the hardware results: 32 rows × 10 variants (plain, both `neg` patterns, `op_sel` on src0 and on src1, `clamp`, inline constant, literal, DPP `v_dot2c`). The rows are picked to cover rounding differences, NaN from each source, sNaN, signs, infinities, invalid results, signed zeros, f16 denormals and denormal accumulators. The existing refusal checks for the accumulator `op_sel`/`neg_hi` are kept. The old test computed its expected values with the same chained `fma`, so it agreed with main. Under the hardware rule, 22 of its own rows were wrong. ### Tested Linux, RADV (Mesa 26.2) + local lavapipe + CI's lavapipe (Mesa 25.2.8), `agc_driver_float16_dot_tests` passes. Cold on CI's lavapipe: 0 s. Full Release ctest: the only failures are `agc_driver_pixel_helper_lanes`, `agc_driver_pixel_lds` and `agc_driver_pixel_lds_lane_slots`, which also fail on `main` with local RADV (wave32 lane 32 on RADV's 64-wide subgroup). They are unrelated to this PR. Without the fix, the new test fails on all 10 variants (main: 11-27 lanes per variant, 25 on DPP). Negative checks, each one failing the test: double rounding (truncation, ties away), accumulator denormal kept, accumulator sNaN not quieted, f16 NaN sign dropped, NaN source order, invalid sign, infinity sign, zero sign, f16 denormal scale, clamp applied, DPP accumulator read from the DPP lane, neg not applied on the bits. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
oneandonlydean commented 2026-10-05 19:45:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T19:45:43Z.


Thanks, this is careful work, and the measurements are very useful. The algorithm looks right to me: the two f16 products are exact in f64, the two TwoSums are exact, and round-to-odd into f64 followed by one rounding to f32 gives a single correct rounding (53 bits is more than 24 + 2). I also checked the edge cases on the rounding to f32. Any nonzero sum is at least about 2^-71, so it never lands in the f32 denormal range, and it can't reach 2^128, so the carry from rounding up only makes an infinity when it should. The accumulator for DPP v_dot2c now comes from accumulatorOperand, which fixes the read from the DPP source lane, and the inline constants keep main's low-half-only bits.

The test kernel disassembles as described with llvm-mc. I had to use gfx1011, since LLVM's gfx1013 has no dot2 encodings.

One thing I'd like to ask about is speed. PPSA21564 uses v_dot2c_f32_f16 in 5,482 of the 5,688 shaders I have dumped (about 291,000 instructions, often with an SGPR or 2.0 / -0.5 as src0). Today each one is two f32 FMAs. With this change each one becomes about two dozen f64 operations plus the bit handling, and f64 is slow on most consumer GPUs. Did you see any frame time change on a title? Would a fast path be worth it, for example the exact path only when the chained FMAs could round differently? I'm happy with the exact version as the default if the cost is acceptable. Either way I'll run it through my visual regression check and compare frame rates on PPSA21564 before it lands, since it changes almost every shader there.

Locally (Linux, GCC, NVIDIA): agc_driver_float16_dot fails on main's code, passes with the change, and passes 20 times in a row.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6001772734 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/669#issuecomment-6001772734). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-05T19:45:43Z. --- Thanks, this is careful work, and the measurements are very useful. The algorithm looks right to me: the two f16 products are exact in f64, the two TwoSums are exact, and round-to-odd into f64 followed by one rounding to f32 gives a single correct rounding (53 bits is more than 24 + 2). I also checked the edge cases on the rounding to f32. Any nonzero sum is at least about 2^-71, so it never lands in the f32 denormal range, and it can't reach 2^128, so the carry from rounding up only makes an infinity when it should. The accumulator for DPP `v_dot2c` now comes from `accumulatorOperand`, which fixes the read from the DPP source lane, and the inline constants keep main's low-half-only bits. The test kernel disassembles as described with llvm-mc. I had to use gfx1011, since LLVM's gfx1013 has no dot2 encodings. One thing I'd like to ask about is speed. PPSA21564 uses `v_dot2c_f32_f16` in 5,482 of the 5,688 shaders I have dumped (about 291,000 instructions, often with an SGPR or `2.0` / `-0.5` as src0). Today each one is two f32 FMAs. With this change each one becomes about two dozen f64 operations plus the bit handling, and f64 is slow on most consumer GPUs. Did you see any frame time change on a title? Would a fast path be worth it, for example the exact path only when the chained FMAs could round differently? I'm happy with the exact version as the default if the cost is acceptable. Either way I'll run it through my visual regression check and compare frame rates on PPSA21564 before it lands, since it changes almost every shader there. Locally (Linux, GCC, NVIDIA): `agc_driver_float16_dot` fails on main's code, passes with the change, and passes 20 times in a row.
adriadam10 commented 2026-10-05 22:30:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-05T22:30:57Z.


Thanks for checking it. I have no frame-time numbers: I can't run titles here, only the HW oracle and the execution tests, so the cost on a real title is unmeasured. A fast path that picks the exact path only when the two chained FMAs could round differently needs its own error bound check per lane, which isn't free either, so I'd rather not guess at it without numbers. If your PPSA21564 comparison shows a visible cost, I'll open a follow-up with a fast path and before/after numbers from your run; if it doesn't, the exact version can stay as it is.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6004493431 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/669#issuecomment-6004493431). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-05T22:30:57Z. --- Thanks for checking it. I have no frame-time numbers: I can't run titles here, only the HW oracle and the execution tests, so the cost on a real title is unmeasured. A fast path that picks the exact path only when the two chained FMAs could round differently needs its own error bound check per lane, which isn't free either, so I'd rather not guess at it without numbers. If your PPSA21564 comparison shows a visible cost, I'll open a follow-up with a fast path and before/after numbers from your run; if it doesn't, the exact version can stay as it is.
oneandonlydean commented 2026-10-06 00:16:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-06T00:16:11Z.


Here are the PPSA21564 numbers I promised. It ran through my visual regression gate as part of a batch of nine PRs on top of my local branch, on Linux with an RTX 4090. That card runs f64 at 1/64 of the f32 rate, so it's close to the worst case for this change.

  • Visual and video checks: all three batch runs passed, with no flicker, shimmer, torn frames or GPU errors.
  • Frame rate, batch against the same branch without the PRs (mean of the runs):
intro title menu level
with the batch 47.6 60.3 60.9 31.2
without 47.9 60.3 60.7 31.7

Title and menu sit at the 60 fps cap, so the level is the useful number. It's GPU-bound there, and the 0.5 fps difference is within the run-to-run spread I see on a single build (about 30.5 to 31.8). The batch also had other shader changes in it, so this is an upper bound on #669's cost rather than an isolated measurement. Either way, I don't see a cost that would justify a fast path, so the exact version is fine as it is from my side.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6006253195 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/669#issuecomment-6006253195). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-06T00:16:11Z. --- Here are the PPSA21564 numbers I promised. It ran through my visual regression gate as part of a batch of nine PRs on top of my local branch, on Linux with an RTX 4090. That card runs f64 at 1/64 of the f32 rate, so it's close to the worst case for this change. - Visual and video checks: all three batch runs passed, with no flicker, shimmer, torn frames or GPU errors. - Frame rate, batch against the same branch without the PRs (mean of the runs): | | intro | title | menu | level | |---|---|---|---|---| | with the batch | 47.6 | 60.3 | 60.9 | 31.2 | | without | 47.9 | 60.3 | 60.7 | 31.7 | Title and menu sit at the 60 fps cap, so the level is the useful number. It's GPU-bound there, and the 0.5 fps difference is within the run-to-run spread I see on a single build (about 30.5 to 31.8). The batch also had other shader changes in it, so this is an upper bound on #669's cost rather than an isolated measurement. Either way, I don't see a cost that would justify a fast path, so the exact version is fine as it is from my side.
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
boykopovar/AnyPS5!669
No description provided.