Skip to content

fix(shader): choose packed f16 NaN results on the bits, as RDNA does - #533

Merged
adriadam10 merged 1 commit into
boykopovar:mainfrom
adriadam10:test/vop3p-packed
Oct 8, 2026
Merged

adriadam10 merged 1 commit into
boykopovar:mainfrom
adriadam10:test/vop3p-packed

Conversation

@adriadam10

@adriadam10 adriadam10 commented Oct 4, 2026 •

Copy link
Copy Markdown
Collaborator

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/max treated every NaN with a nonzero low payload as signaling, so min(0xffff, 0) returned the NaN instead of 0.
  • The NaN override read the sources without neg_lo/neg_hi, so a negated NaN kept its old sign.
  • add/mul/fma(c) returned whatever NaN the driver produced (payload and sign unspecified in Vulkan, canonical on NVIDIA).

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. With clamp the result keeps the current path: on hardware it is +0 for any NaN, which belongs to the f16 clamp work (#323, #500).

Behaviour measured on HW (RDNA2, gfx1035)

  • add/mul/fma/fmac: the first NaN source in operand order (src0, src1, src2/accumulator), with the quiet bit set. If the product is invalid (inf·0) the result is 0xfe00, even when src2 is a NaN; if no source is a NaN and the result is invalid (inf−inf), it is 0xfe00 too.
  • min/max (IEEE mode): a signaling NaN source wins, quieted (src0 before src1). Otherwise, if one source is a NaN, the result is the other source. If both are quiet NaNs, the result is src0.
  • v_pk_fmac_f16 gives the same result as v_pk_fma_f16 with src2 = the destination.
  • The rules above are at IEEE=1. At IEEE=0 (the mode titles use, e.g. every Astro's Playroom shader) NaN sources pass through without the quiet bit, and min/max return the other source for any NaN, signaling or not. The shader's mode comes from GuestContext::floatMode (feat(shader): carry the shader float mode into the recompile request #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 Shader float mode (IEEE_MODE, DX10_CLAMP, FLOAT_MODE) is per shader; the recompiler assumes one #981).

Also added to TechnicalDebt (not changed here): v_fma_f16, v_pk_fma_f16 and v_pk_fmac_f16 are an f32 FMA rounded to f16, so the result can be rounded twice (0x62e9 * 0xd159 + 0x23ec gives 0xf89e; the hardware gives 0xf89f).

Tested

New execution test agc_driver_packed_alu_tests runs 44 instructions covering all 19 VOP3P packed ops plus v_pk_fmac_f16: each plain op, op_sel/op_sel_hi variants, and neg_lo/neg_hi variants 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, neg ignored 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

@github-actions github-actions Bot added shader core/shader docs agc libSceAgc* labels Oct 4, 2026
@adriadam10

Copy link
Copy Markdown
Collaborator Author

Rebased on current main: the only Windows failure was guest_audio_in_no_device, fixed by #532. No other changes.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

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 through accumulatorOperand, 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 what readF16LaneAsF32 does 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_alu fails 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.

@adriadam10

Copy link
Copy Markdown
Collaborator Author

Rebased on main (TechnicalDebt conflict with the v_mullit_f32 line: both kept). PackedAlu passes on RADV and lavapipe.

@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Oct 5, 2026
@adriadam10

Copy link
Copy Markdown
Collaborator Author

Rebased on main (TechnicalDebt conflict with the v_fma_legacy_f32 line, both kept). packed_alu passes on RADV and on CI's lavapipe.

Co-Authored-By: Claude <noreply@anthropic.com>
@adriadam10

Copy link
Copy Markdown
Collaborator Author

Taken off hold for #981. The ops now follow the shader's float mode, which #1816 carries into the request. Rebased on main after #1826.

  • With IEEE=1, behaviour is unchanged: signaling NaN inputs are quieted and min/max follow the IEEE rules.
  • With IEEE=0 (the mode titles use, e.g. every Astro's Playroom shader) NaN inputs pass through unquieted and min/max don't treat signaling NaNs specially. A shader with no known float mode is translated this way.
  • New fma rule, measured: an invalid product (inf × 0) gives 0xfe00 even when src2 is a NaN. Rows 30 and 31 now cover this.
  • A mode that flushes f16 denormals or rounds f16 other than to nearest even throws. Those modes are the FLOAT_MODE step of Shader float mode (IEEE_MODE, DX10_CLAMP, FLOAT_MODE) is per shader; the recompiler assumes one #981.
  • Expected was recaptured on gfx1035 at IEEE=1. A second table, ExpectedIeeeOff, was captured at IEEE=0. The test runs both, plus the throwing mode.
  • Buffer descriptor uses raw bounds (0x31016fac), as perf(agc): baseline shader recompilation on registration #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.

@SleglJan

SleglJan commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Scalar cross-check of the NaN rules, measured on RDNA2 (gfx1036, Ryzen 7950X3D iGPU) with the hardware oracle: v_min_f64 and v_max_f64 over every pair of 14 operands (±0, ±1, ±inf, ±denormal, three quiet and three signalling NaNs; 196 rows), and v_min_f16, v_max_f16, v_min3_f16, v_max3_f16, v_med3_f16 over every triple (2,744 rows), each in IEEE_MODE 0 and 1 with f16/f64 denormals kept and flushed. 0 mismatches against this PR's rules: with IEEE_MODE=1 the first signalling NaN source (src0 before src1) is returned quieted; with IEEE_MODE=0 a 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 (with IEEE_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).

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

Labels

agc libSceAgc* docs shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants