fix(shader): choose packed f16 NaN results on the bits, as RDNA does #533
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/533"
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-04T15:46:30Z.
What
The packed f16 ops (
v_pk_add_f16,v_pk_mul_f16,v_pk_fma_f16,v_pk_fmac_f16,v_pk_min_f16,v_pk_max_f16) left NaN results to the driver, or got them wrong:min(0xffff, 0)returned the NaN instead of0.neg_lo/neg_hi, so a negated NaN kept its old sign.NaN results are now chosen on the bits of each half, after
op_sel/op_sel_hi/neg_lo/neg_hi. Numeric results are unchanged. Withclampthe result keeps the current path: on hardware it is+0for any NaN, which belongs to the f16 clamp work (#323, #500).Behaviour measured on HW (RDNA2, gfx1035)
0xfe00, even when src2 is a NaN; if no source is a NaN and the result is invalid (inf−inf), it is0xfe00too.v_pk_fmac_f16gives the same result asv_pk_fma_f16with src2 = the destination.GuestContext::floatMode(#1816); a shader with no known mode is translated as IEEE=0. A mode that flushes f16 denormals or rounds f16 other than to nearest even throws (FLOAT_MODE step of #981).Also added to TechnicalDebt (not changed here):
v_fma_f16,v_pk_fma_f16andv_pk_fmac_f16are an f32 FMA rounded to f16, so the result can be rounded twice (0x62e9 * 0xd159 + 0x23ecgives0xf89e; the hardware gives0xf89f).Tested
New execution test
agc_driver_packed_alu_testsruns 44 instructions covering all 19 VOP3P packed ops plusv_pk_fmac_f16: each plain op,op_sel/op_sel_hivariants, andneg_lo/neg_hivariants on the f16 ops. Rows were picked by class (integer overflow, sign and shift counts; f16 inf/zero/NaN, quiet and signaling NaN per source), with fixed rows for inf·0, inf−inf and inf·0 with a NaN src2. The expected values are what the hardware returns at IEEE=1 (Expected) and IEEE=0 (ExpectedIeeeOff), and the Python model matches them in every cell. The test runs with no mode (checked against IEEE=0), with IEEE=1, and with a flush-f16 mode that must throw. Denormal inputs and results are excluded.It passes on RADV, local lavapipe and CI's lavapipe (Mesa 25.2.8). The integer ops already passed on
main. The test fails with each of these changes: no NaN override (=main), quiet NaNs treated as signaling,negignored on NaN sources, a positive invalid NaN, the last NaN source picked first, NaN not quieted, src1 picked when both min/max sources are NaN, the wrong signaling-NaN order, and the fmac accumulator ignored.The full Release ctest passes (489/489).
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 adriadam10 on 2026-10-04T20:36:22Z.
Rebased on current main: the only Windows failure was
guest_audio_in_no_device, fixed by #532. No other changes.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T21:19:11Z.
Thanks. I checked the min/max part against the RDNA2 ISA pseudocode for V_MIN_F16/V_MAX_F16: a signaling NaN wins and is quieted (src0 before src1), and a single NaN returns the other source, which is what the selects do. For two quiet NaNs the pseudocode would give src1, while your capture and the code give src0; I'm fine going with the hardware there, and the mutation row covers it. The old sNaN test (
payload & 0x1ff) was indeed wrong for 0xffff, and testing the quiet bit fixes it. The accumulator is read throughaccumulatorOperand, so this composes with #514's change to the numeric path, and the neg_lo/neg_hi and op_sel/op_sel_hi handling on the bits matches whatreadF16LaneAsF32does for the value.One note, not blocking: quieting a signaling NaN on add/mul/fma is the IEEE=1 result, and the recompiler doesn't read the mode from PGM_RSRC1. Main already made the same assumption for v_pk_min/max, so this keeps it consistent.
agc_driver_packed_alufails on main and passes with this branch on my NVIDIA machine. Since packed f16 ops are on PPSA21564's path, I'll run it through my PPSA21564 check before merging. Otherwise it's ready to merge from my side.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-05T17:05:21Z.
Rebased on main (TechnicalDebt conflict with the v_mullit_f32 line: both kept). PackedAlu passes on RADV and lavapipe.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-05T19:01:15Z.
Rebased on main (TechnicalDebt conflict with the v_fma_legacy_f32 line, both kept). packed_alu passes on RADV and on CI's lavapipe.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T21:35:49Z.
Taken off hold for #981. The ops now follow the shader's float mode, which #1816 carries into the request. Rebased on
mainafter #1826.0xfe00even when src2 is a NaN. Rows 30 and 31 now cover this.Expectedwas recaptured on gfx1035 at IEEE=1. A second table,ExpectedIeeeOff, was captured at IEEE=0. The test runs both, plus the throwing mode.0x31016fac), as #639 requires. TechnicalDebt has a line for the measured rules.Tested: packed_alu on RADV and CI's lavapipe, full ctest 489/489. Negative checks each fail: quieting at IEEE=0, src2 NaN beating an invalid product, sNaN min/max rule applied at IEEE=0, the flush mode not refused.
Imported from boykopovar/AnyPS5. Originally posted by SleglJan on 2026-10-08T21:40:52Z.
Scalar cross-check of the NaN rules, measured on RDNA2 (gfx1036, Ryzen 7950X3D iGPU) with the hardware oracle:
v_min_f64andv_max_f64over every pair of 14 operands (±0, ±1, ±inf, ±denormal, three quiet and three signalling NaNs; 196 rows), andv_min_f16,v_max_f16,v_min3_f16,v_max3_f16,v_med3_f16over every triple (2,744 rows), each inIEEE_MODE0 and 1 with f16/f64 denormals kept and flushed. 0 mismatches against this PR's rules: withIEEE_MODE=1the first signalling NaN source (src0 before src1) is returned quieted; withIEEE_MODE=0a NaN source loses to a non-NaN one; and in both modes two quiet NaN sources give src0, not src1 as the ISA pseudocode has it (withIEEE_MODE=0, any two NaN sources give src0). Rows, results and the fit script: https://gist.github.com/SleglJan/30456e5687240f0b639a78e5477f2f91 (minmax-matrix.json,minmax_fit.py).AI-assisted: yes (Claude Code).