fix(shader): match v_dot2_f32_f16 and v_dot2c_f32_f16 to hardware #669
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/669"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-05T13:20:53Z.
What
v_dot2_f32_f16andv_dot2c_f32_f16now match the hardware bit for bit. Main computed them as two chainedFmas, 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=0andIEEE=1 DENORM=3give identical results).v_dot2cis bit for bitv_dot2with the accumulator in vdst:a.lo*b.lo + a.hi*b.hi + c. Chained FMAs round twice and differ on ~2% of random rows.sign | 0x7fc00000 | payload << 13. Invalid results (inf*0, inf-inf) give0xffc00000.neg_lo/neg_hiflip the sign bit of the half, NaNs included.clampis ignored onv_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.v_dot2c: the accumulator comes from the lane itself, not from the DPP source lane (main read it throughinst.destination, which carries the DPP).1.0as a packed source is0x00003c00.The emitter computes the sum exactly in f64 with the existing
TwoSumandAddRoundToOddhelpers (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'sFmafusing, 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.cppis regenerated from the hardware results: 32 rows × 10 variants (plain, bothnegpatterns,op_selon src0 and on src1,clamp, inline constant, literal, DPPv_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 accumulatorop_sel/neg_hiare kept. The old test computed its expected values with the same chainedfma, 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_testspasses. Cold on CI's lavapipe: 0 s. Full Release ctest: the only failures areagc_driver_pixel_helper_lanes,agc_driver_pixel_ldsandagc_driver_pixel_lds_lane_slots, which also fail onmainwith 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
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)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_dot2cnow comes fromaccumulatorOperand, 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_f16in 5,482 of the 5,688 shaders I have dumped (about 291,000 instructions, often with an SGPR or2.0/-0.5as 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_dotfails on main's code, passes with the change, and passes 20 times in a row.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.
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.
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.