fix(shader): v_mullit_f32 legacy rules, v_cvt_pk_u8_f32 rounding and VOP3 neg/abs #501

Merged
adriadam10 merged 2 commits from upstream-pr/501 into main 2026-10-05 13:32:07 +00:00
adriadam10 commented 2026-10-04 10:27:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-04T10:27:31Z.


What

VOP3 neg/abs on v_mullit_f32 (all three sources) and v_cvt_pk_u8_f32 (src0) used to throw. Enabling them showed that both ops differed from hardware even without modifiers, so this PR fixes that as well:

  • v_mullit_f32 was a plain v_mul_f32 that ignored src2. It now follows the measured rules.
  • v_cvt_pk_u8_f32 truncated. It now rounds to nearest even, like the hardware (1.5 → 2, 2.5 → 2, 3.5 → 4).

Anything still unmodelled keeps throwing: clamp/omod on both ops, and neg/abs on src1/src2 of v_cvt_pk_u8_f32 (llvm-mc rejects those too).

Behaviour measured on HW

The ISA guide doesn't spell these out, so I measured them on an RDNA2 GPU (gfx1035; llvm-mc gives the same VOP3 opcode 0x150/0x15e on gfx1013). Modifiers apply before every check below.

  • v_mullit_f32 d, s0, s1, s2:
    1. If s1 is −MAX_FLOAT, −inf or NaN, or s2 is NaN, ±0 or negative, the result is 0xff7fffff (−MAX_FLOAT). The −MAX test is exact: 0xff7ffffe multiplies normally.
    2. Otherwise, if s0 or s1 is ±0, the result is +0, even for 0 × inf, 0 × NaN and −0 × x.
    3. Otherwise, if s0 is NaN, the result is s0 quieted (| 0x400000), sign kept.
    4. Otherwise, the result is the IEEE product (overflow → ±inf).
  • v_cvt_pk_u8_f32: NaN and values ≤ 0 → 0, values ≥ 255 → 255, otherwise round to nearest even.

These results don't depend on the IEEE or DENORM mode, with two exceptions: IEEE=0 keeps sNaN s0 unquieted, and flushed f32 denormals make a positive-denormal s2 count as 0. The implementation follows the default compute mode (IEEE=1, denormals kept), and the test has no sNaN or denormal rows.

To stay exact on any driver, everything is done on the bits:

  • The FMul only sees non-NaN, non-zero operands. The NaN result is chosen on the bits.
  • Rounding uses a truncating convert, an exact FSub for the fraction, and compares. RoundEven isn't in Vulkan's precision table.

v_mad_legacy_f32 is not included: on gfx1030+ its opcode 0x140 is v_fma_legacy_f32, so I can't measure it on my GPU, and its translation stays as it is on main.

Tested

Linux, RADV + lavapipe (local Mesa and CI's Ubuntu Mesa 25.2.8):

  • New execution test agc_driver_vop3_legacy_source_modifiers_tests: 32 lanes × 10 forms, expected values taken from the hardware. The rows cover the specials (±0, ±inf, qNaN with payloads, ±MAX, −MAX−1ulp), rounding ties, and both clamp edges.
  • The model matched the hardware on a 4096-row sweep (36952 checks, 0 mismatches).
  • Each piece of the fix fails the test when reverted (12 negative checks: −MAX/NaN/sign rules, legacy zero, NaN bits, src2 modifiers, decoder accept, truncation, ties, 255 guard).
  • Full Release ctest passes, apart from agc_driver_pixel_helper_lanes, which already fails on main with local RADV (not touched here).

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:4733644081 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/501). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-04T10:27:31Z. --- ### What VOP3 `neg`/`abs` on `v_mullit_f32` (all three sources) and `v_cvt_pk_u8_f32` (src0) used to throw. Enabling them showed that both ops differed from hardware even without modifiers, so this PR fixes that as well: - `v_mullit_f32` was a plain `v_mul_f32` that ignored src2. It now follows the measured rules. - `v_cvt_pk_u8_f32` truncated. It now rounds to nearest even, like the hardware (1.5 → 2, 2.5 → 2, 3.5 → 4). Anything still unmodelled keeps throwing: clamp/omod on both ops, and neg/abs on src1/src2 of `v_cvt_pk_u8_f32` (llvm-mc rejects those too). #### Behaviour measured on HW The ISA guide doesn't spell these out, so I measured them on an RDNA2 GPU (gfx1035; llvm-mc gives the same VOP3 opcode 0x150/0x15e on gfx1013). Modifiers apply before every check below. - `v_mullit_f32 d, s0, s1, s2`: 1. If s1 is −MAX_FLOAT, −inf or NaN, or s2 is NaN, ±0 or negative, the result is `0xff7fffff` (−MAX_FLOAT). The −MAX test is exact: `0xff7ffffe` multiplies normally. 2. Otherwise, if s0 or s1 is ±0, the result is +0, even for 0 × inf, 0 × NaN and −0 × x. 3. Otherwise, if s0 is NaN, the result is s0 quieted (`| 0x400000`), sign kept. 4. Otherwise, the result is the IEEE product (overflow → ±inf). - `v_cvt_pk_u8_f32`: NaN and values ≤ 0 → 0, values ≥ 255 → 255, otherwise round to nearest even. These results don't depend on the IEEE or DENORM mode, with two exceptions: IEEE=0 keeps sNaN s0 unquieted, and flushed f32 denormals make a positive-denormal s2 count as 0. The implementation follows the default compute mode (IEEE=1, denormals kept), and the test has no sNaN or denormal rows. To stay exact on any driver, everything is done on the bits: - The FMul only sees non-NaN, non-zero operands. The NaN result is chosen on the bits. - Rounding uses a truncating convert, an exact FSub for the fraction, and compares. `RoundEven` isn't in Vulkan's precision table. `v_mad_legacy_f32` is not included: on gfx1030+ its opcode 0x140 is `v_fma_legacy_f32`, so I can't measure it on my GPU, and its translation stays as it is on main. ### Tested Linux, RADV + lavapipe (local Mesa and CI's Ubuntu Mesa 25.2.8): - New execution test `agc_driver_vop3_legacy_source_modifiers_tests`: 32 lanes × 10 forms, expected values taken from the hardware. The rows cover the specials (±0, ±inf, qNaN with payloads, ±MAX, −MAX−1ulp), rounding ties, and both clamp edges. - The model matched the hardware on a 4096-row sweep (36952 checks, 0 mismatches). - Each piece of the fix fails the test when reverted (12 negative checks: −MAX/NaN/sign rules, legacy zero, NaN bits, src2 modifiers, decoder accept, truncation, ties, 255 guard). - Full Release ctest passes, apart from `agc_driver_pixel_helper_lanes`, which already fails on main with local RADV (not touched here). ### 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-04 21:07:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T21:07:56Z.


Thanks, checked against the RDNA2 ISA guide (V_MULLIT_F32 only says "specific INF, NaN, overflow rules", and the older GCN description has the same -MAX_FLOAT conditions you measured) and the code follows your rules exactly: the -MAX test catches -MAX, -inf and negative NaNs, the FMul never sees a NaN or zero operand, and the u8 rounding is exact (truncate, exact fraction, ties to even, 255 guard). The new modifier block for v_cvt_pk_u8_f32 sits before the generic path, so src1/src2 modifiers, clamp and omod still throw. A small note, not blocking: the sNaN s0 quieting and the positive-denormal s2 case follow IEEE=1 with denormals kept, while the recompiler doesn't read the mode; a TechnicalDebt line would make that visible. This changes results of unmodified v_cvt_pk_u8_f32 and v_mullit_f32, so I'll run it through my PPSA21564 check before merging; ready to merge from my side.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5984395592 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/501#issuecomment-5984395592). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T21:07:56Z. --- Thanks, checked against the RDNA2 ISA guide (V_MULLIT_F32 only says "specific INF, NaN, overflow rules", and the older GCN description has the same -MAX_FLOAT conditions you measured) and the code follows your rules exactly: the -MAX test catches -MAX, -inf and negative NaNs, the FMul never sees a NaN or zero operand, and the u8 rounding is exact (truncate, exact fraction, ties to even, 255 guard). The new modifier block for `v_cvt_pk_u8_f32` sits before the generic path, so src1/src2 modifiers, clamp and omod still throw. A small note, not blocking: the sNaN s0 quieting and the positive-denormal s2 case follow IEEE=1 with denormals kept, while the recompiler doesn't read the mode; a TechnicalDebt line would make that visible. This changes results of unmodified `v_cvt_pk_u8_f32` and `v_mullit_f32`, so I'll run it through my PPSA21564 check before merging; ready to merge from my side.
adriadam10 commented 2026-10-04 21:09:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-04T21:09:33Z.


Added the TechnicalDebt line under "Functional": v_mullit_f32 follows the hardware measured at IEEE=1 with denormals kept (sNaN src0 quieted, positive denormal src2 counts as positive), and the recompiler does not read MODE. Also rebased on current main; agc_driver_vop3_legacy_source_modifiers_tests still passes on RADV and lavapipe.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5984408790 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/501#issuecomment-5984408790). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-04T21:09:33Z. --- Added the TechnicalDebt line under "Functional": `v_mullit_f32` follows the hardware measured at IEEE=1 with denormals kept (sNaN src0 quieted, positive denormal src2 counts as positive), and the recompiler does not read MODE. Also rebased on current main; `agc_driver_vop3_legacy_source_modifiers_tests` still passes on RADV and lavapipe.
oneandonlydean commented 2026-10-04 22:38:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T22:38:10Z.


Thanks, the TechnicalDebt line says exactly what the code assumes (IEEE=1 with denormals kept, sNaN src0 quieted, positive denormal src2 positive) and the rebase left the fix commit unchanged.

agc_driver_vop3_legacy_source_modifiers still passes on my NVIDIA machine with the new head on current main, and fails on main's code.

I'll run it through my PPSA21564 check next and merge it after that if it's clean.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5985220258 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/501#issuecomment-5985220258). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T22:38:10Z. --- Thanks, the TechnicalDebt line says exactly what the code assumes (IEEE=1 with denormals kept, sNaN src0 quieted, positive denormal src2 positive) and the rebase left the fix commit unchanged. `agc_driver_vop3_legacy_source_modifiers` still passes on my NVIDIA machine with the new head on current main, and fails on main's code. I'll run it through my PPSA21564 check next and merge it after that if it's clean.
oneandonlydean commented 2026-10-05 07:26:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T07:26:30Z.


#498 landed today, and it now conflicts with this one in checkNativeVop3Modifiers: #498 added an if (opcode == RdnaOpcode::VLdexpF16) block in the same place as your VCvtPkU8F32 block, and the two bodies are identical. Keeping both fixes it, either as two blocks or as one condition:

if (opcode == RdnaOpcode::VLdexpF16 || opcode == RdnaOpcode::VCvtPkU8F32) {

I tried the combined form here on current main and the full ctest passes, agc_driver_vop3_legacy_source_modifiers included. Could you rebase? I also checked the 5,688 PPSA21564 shaders I have dumped: none use v_mullit_f32 or v_cvt_pk_u8_f32, so this doesn't need my PPSA21564 check after all, and I'll merge it once it's rebased.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5990011556 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/501#issuecomment-5990011556). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-05T07:26:30Z. --- #498 landed today, and it now conflicts with this one in `checkNativeVop3Modifiers`: #498 added an `if (opcode == RdnaOpcode::VLdexpF16)` block in the same place as your `VCvtPkU8F32` block, and the two bodies are identical. Keeping both fixes it, either as two blocks or as one condition: ```cpp if (opcode == RdnaOpcode::VLdexpF16 || opcode == RdnaOpcode::VCvtPkU8F32) { ``` I tried the combined form here on current main and the full ctest passes, `agc_driver_vop3_legacy_source_modifiers` included. Could you rebase? I also checked the 5,688 PPSA21564 shaders I have dumped: none use `v_mullit_f32` or `v_cvt_pk_u8_f32`, so this doesn't need my PPSA21564 check after all, and I'll merge it once it's rebased.
adriadam10 commented 2026-10-05 08:57:16 +00:00 (Migrated from github.com)

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


Rebased on current main after #498:

  • checkNativeVop3Modifiers: kept both opcodes in one condition, VLdexpF16 || VCvtPkU8F32, as suggested.
  • agc_driver_vop3_legacy_source_modifiers and the other agc_driver_vop3*/agc_shader* tests pass on RADV.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5991285150 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/501#issuecomment-5991285150). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-05T08:57:16Z. --- Rebased on current `main` after #498: - `checkNativeVop3Modifiers`: kept both opcodes in one condition, `VLdexpF16 || VCvtPkU8F32`, as suggested. - `agc_driver_vop3_legacy_source_modifiers` and the other `agc_driver_vop3*`/`agc_shader*` tests pass on RADV.
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!501
No description provided.