feat(shader): accept clamp on v_cube*_f32 and match their zero and subnormal results #508

Merged
adriadam10 merged 1 commit from upstream-pr/508 into main 2026-10-05 13:31:23 +00:00
adriadam10 commented 2026-10-04 11:23:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-04T11:23:14Z.


What

VOP3 clamp on v_cubeid_f32, v_cubesc_f32, v_cubetc_f32 and v_cubema_f32 used to throw. It is now accepted and clamps the result to [0, 1] with the f32 clamp rule from #500 (NaN and -0 give +0). omod on these ops still throws (see below).

The test for this also showed where the cube ops differed from hardware even without modifiers. This PR fixes those cases too:

  • v_cubema_f32 returned -0 for a -0 major axis. The hardware returns +0.
  • Subnormal inputs were compared by value. The hardware treats them as +0 when it picks the face and the signs. v_cubesc_f32 / v_cubetc_f32 still return the subnormal bits unchanged, and v_cubema_f32 returns +0 for a subnormal major axis.
  • A NaN x/y/z passed through OpFNegate, which on RADV dropped the sign of the NaN. Negation now works on the bits. Source neg/abs on these ops are now read through readU32, so they also work on the bits.
  • A selected NaN (including sNaN) is returned unchanged, like main does, and v_cubema_f32 no longer multiplies a NaN major axis by 2.

Behaviour measured on HW

I measured these on an RDNA2 GPU (gfx1035), in the mode the recompiler assumes for every other f32 op: MODE.IEEE = 0 and f32 output denormals flushed (.amdhsa_ieee_mode 0, .amdhsa_float_denorm_mode_32 0). This is LLVM's default for graphics calling conventions. llvm-mc gives the same VOP3 opcodes 0x144-0x147 for gfx1013.

  • I swept 4096 rows of specials (±0, ±inf, qNaN/sNaN with payloads, ±MAX, subnormals, ordinary values) over the plain, clamp, mul:2, mul:4, div:2 and clamp mul:2 forms.
  • Plain results: sNaN stays unquieted. The face/sign rule for subnormals and the cubema zero rule above matched all 4096 rows.
  • clamp = the plain result clamped by the #500 rule, with no mismatches.
  • omod is applied in this mode, the same as on v_mul_f32. I ran a v_mul_f32 ... mul:2/div:2/mul:4 control in the same dispatch. In this mode the cube ops and the control follow the same rule: NaN unchanged, and a zero or subnormal result gives +0. Main's applyF32ResultModifiers is a plain FMul, so it returns -0 there. Accepting omod on the cube ops would add a result that is known to be wrong, so it keeps throwing. The omod rule for all f32 ops will come in a separate PR.
  • With IEEE = 1, the hardware ignores omod on both the cube ops and v_mul_f32, as the ISA guide says. The earlier version of this PR measured in that mode by mistake. With IEEE = 0 and output denormals kept (float_denorm_mode_32 2), v_mul_f32 ignores omod but the cube ops still apply it. Since the recompiler doesn't model that mode, it doesn't change anything here.

To stay exact on any driver, the cube logic works on the bits:

  • Subnormals are flushed for the compares by testing the exponent field.
  • Negation is a sign-bit XOR. NaN results are selected bit for bit.
  • The cubema FMul by 2 only sees non-NaN, non-zero, non-subnormal values.

Tested

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

  • New execution test agc_driver_vop3_cube_clamp_tests: 32 lanes × 16 forms (plain, clamp, mixed neg/abs with and without clamp). Expected values come from the hardware in the mode above. The model matched the hardware on these rows and on a 2048-row sweep (32768 checks, 0 mismatches). It also checks that omod on each of the four opcodes is refused.
  • Each piece of the fix fails the test when reverted. There are 8 negative checks: clamp accepted, omod refused, sc/tc sNaN kept, cubema NaN kept, cubema -0, bit negation, and bit source modifiers. One exception: removing the subnormal flush before the compares still passes on RADV and lavapipe, because both drivers already flush f32 subnormals in float compares. The flush is there for drivers that keep them.
  • Full Release ctest passes, apart from agc_driver_pixel_helper_lanes. That test already fails on main with local RADV and isn't 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: #500 (its commit is included; the clamp rule)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4733936782 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/508). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-04T11:23:14Z. --- ### What VOP3 `clamp` on `v_cubeid_f32`, `v_cubesc_f32`, `v_cubetc_f32` and `v_cubema_f32` used to throw. It is now accepted and clamps the result to [0, 1] with the f32 clamp rule from #500 (NaN and -0 give +0). `omod` on these ops still throws (see below). The test for this also showed where the cube ops differed from hardware even without modifiers. This PR fixes those cases too: - `v_cubema_f32` returned -0 for a -0 major axis. The hardware returns +0. - Subnormal inputs were compared by value. The hardware treats them as +0 when it picks the face and the signs. `v_cubesc_f32` / `v_cubetc_f32` still return the subnormal bits unchanged, and `v_cubema_f32` returns +0 for a subnormal major axis. - A NaN `x`/`y`/`z` passed through `OpFNegate`, which on RADV dropped the sign of the NaN. Negation now works on the bits. Source `neg`/`abs` on these ops are now read through `readU32`, so they also work on the bits. - A selected NaN (including sNaN) is returned unchanged, like main does, and `v_cubema_f32` no longer multiplies a NaN major axis by 2. #### Behaviour measured on HW I measured these on an RDNA2 GPU (gfx1035), in the mode the recompiler assumes for every other f32 op: MODE.IEEE = 0 and f32 output denormals flushed (`.amdhsa_ieee_mode 0`, `.amdhsa_float_denorm_mode_32 0`). This is LLVM's default for graphics calling conventions. llvm-mc gives the same VOP3 opcodes 0x144-0x147 for gfx1013. - I swept 4096 rows of specials (±0, ±inf, qNaN/sNaN with payloads, ±MAX, subnormals, ordinary values) over the plain, `clamp`, `mul:2`, `mul:4`, `div:2` and `clamp mul:2` forms. - Plain results: sNaN stays unquieted. The face/sign rule for subnormals and the cubema zero rule above matched all 4096 rows. - `clamp` = the plain result clamped by the #500 rule, with no mismatches. - `omod` is applied in this mode, the same as on `v_mul_f32`. I ran a `v_mul_f32 ... mul:2/div:2/mul:4` control in the same dispatch. In this mode the cube ops and the control follow the same rule: NaN unchanged, and a zero or subnormal result gives +0. Main's `applyF32ResultModifiers` is a plain `FMul`, so it returns -0 there. Accepting `omod` on the cube ops would add a result that is known to be wrong, so it keeps throwing. The `omod` rule for all f32 ops will come in a separate PR. - With IEEE = 1, the hardware ignores `omod` on both the cube ops and `v_mul_f32`, as the ISA guide says. The earlier version of this PR measured in that mode by mistake. With IEEE = 0 and output denormals kept (`float_denorm_mode_32 2`), `v_mul_f32` ignores `omod` but the cube ops still apply it. Since the recompiler doesn't model that mode, it doesn't change anything here. To stay exact on any driver, the cube logic works on the bits: - Subnormals are flushed for the compares by testing the exponent field. - Negation is a sign-bit XOR. NaN results are selected bit for bit. - The cubema `FMul` by 2 only sees non-NaN, non-zero, non-subnormal values. ### Tested Linux, RADV + lavapipe (local Mesa and CI's Ubuntu Mesa 25.2.8): - New execution test `agc_driver_vop3_cube_clamp_tests`: 32 lanes × 16 forms (plain, `clamp`, mixed `neg`/`abs` with and without `clamp`). Expected values come from the hardware in the mode above. The model matched the hardware on these rows and on a 2048-row sweep (32768 checks, 0 mismatches). It also checks that `omod` on each of the four opcodes is refused. - Each piece of the fix fails the test when reverted. There are 8 negative checks: clamp accepted, omod refused, sc/tc sNaN kept, cubema NaN kept, cubema -0, bit negation, and bit source modifiers. One exception: removing the subnormal flush before the compares still passes on RADV and lavapipe, because both drivers already flush f32 subnormals in float compares. The flush is there for drivers that keep them. - Full Release ctest passes, apart from `agc_driver_pixel_helper_lanes`. That test already fails on main with local RADV and isn't 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: #500 (its commit is included; the clamp rule) - [x] AI-assisted: yes
oneandonlydean commented 2026-10-04 21:08:02 +00:00 (Migrated from github.com)

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


Thanks, the bit-level cube logic reads right to me (negation on the sign bit, the subnormal flush for the face and sign picks, +0 for a zero or subnormal major axis). Two things about the modes before it can go in:

  1. The RDNA2 ISA guide (output modifiers section) says omod is ignored on every op when MODE.IEEE is 1, and also when output denormals are enabled. The description says the implementation follows the default compute mode with IEEE=1, so the sweep may show omod being ignored for that reason rather than because of the cube ops. Could you rerun the omod rows with IEEE=0 and f32 output denormals off, with a v_mul_f32 ... mul:2 in the same dispatch as a control that does change? Main applies omod on every other f32 op, which assumes exactly that mode.
  2. Quieting a selected sNaN is the IEEE=1 result (as you note, IEEE=0 leaves it unquieted). LLVM defaults IEEE to 0 for all graphics calling conventions, compute included (SIModeRegisterDefaults::getDefaultForCallingConv), and the recompiler doesn't read the mode from PGM_RSRC1. I'd keep the sNaN unquieted like main does, which is consistent with the omod assumption above, or record the IEEE=1 assumption in TechnicalDebt.

Since this changes cube results without modifiers too, I'll run it through my PPSA21564 check once it's updated.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5984396464 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/508#issuecomment-5984396464). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T21:08:02Z. --- Thanks, the bit-level cube logic reads right to me (negation on the sign bit, the subnormal flush for the face and sign picks, +0 for a zero or subnormal major axis). Two things about the modes before it can go in: 1. The RDNA2 ISA guide (output modifiers section) says omod is ignored on every op when MODE.IEEE is 1, and also when output denormals are enabled. The description says the implementation follows the default compute mode with IEEE=1, so the sweep may show omod being ignored for that reason rather than because of the cube ops. Could you rerun the omod rows with IEEE=0 and f32 output denormals off, with a `v_mul_f32 ... mul:2` in the same dispatch as a control that does change? Main applies omod on every other f32 op, which assumes exactly that mode. 2. Quieting a selected sNaN is the IEEE=1 result (as you note, IEEE=0 leaves it unquieted). LLVM defaults IEEE to 0 for all graphics calling conventions, compute included (`SIModeRegisterDefaults::getDefaultForCallingConv`), and the recompiler doesn't read the mode from PGM_RSRC1. I'd keep the sNaN unquieted like main does, which is consistent with the omod assumption above, or record the IEEE=1 assumption in TechnicalDebt. Since this changes cube results without modifiers too, I'll run it through my PPSA21564 check once it's updated.
adriadam10 commented 2026-10-04 21:20:52 +00:00 (Migrated from github.com)

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


Thanks, you were right on both points. Rebased on current main and force-pushed.

  1. I reran it with IEEE=0 and f32 denormals flushed, with v_mul_f32 ... mul:2/div:2/mul:4 in the same dispatch. In that mode omod is applied to the cube ops as well as to the control. My earlier "omod is ignored" came from measuring with IEEE=1. Both follow the same rule: NaN unchanged, and a zero or subnormal result gives +0. Main's applyF32ResultModifiers returns -0 there, so instead of accepting omod with that gap, it keeps throwing on the cube ops. A test now checks that refusal for all four opcodes. The +0 rule for all f32 ops will come in a separate PR.
  2. NaN results are now returned unchanged, like main. The sc/tc quieting is gone, and cubema returns the selected NaN as it is. Every expected value in the test was regenerated on HW in the IEEE=0, denormals-flushed mode. The model matched 32768 of 32768 sweep checks, and the 8 negative checks fail as expected.

Title and description are updated.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5984508236 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/508#issuecomment-5984508236). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-04T21:20:52Z. --- Thanks, you were right on both points. Rebased on current main and force-pushed. 1. I reran it with IEEE=0 and f32 denormals flushed, with `v_mul_f32 ... mul:2/div:2/mul:4` in the same dispatch. In that mode `omod` is applied to the cube ops as well as to the control. My earlier "omod is ignored" came from measuring with IEEE=1. Both follow the same rule: NaN unchanged, and a zero or subnormal result gives +0. Main's `applyF32ResultModifiers` returns -0 there, so instead of accepting `omod` with that gap, it keeps throwing on the cube ops. A test now checks that refusal for all four opcodes. The +0 rule for all f32 ops will come in a separate PR. 2. NaN results are now returned unchanged, like main. The sc/tc quieting is gone, and cubema returns the selected NaN as it is. Every expected value in the test was regenerated on HW in the IEEE=0, denormals-flushed mode. The model matched 32768 of 32768 sweep checks, and the 8 negative checks fail as expected. Title and description are updated.
oneandonlydean commented 2026-10-04 22:38:12 +00:00 (Migrated from github.com)

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


Thanks for rerunning it in the IEEE=0, denormals-flushed mode. That settles both points: with omod applied there and main's applyF32ResultModifiers giving -0 where the hardware gives +0, keeping omod refused on the cube ops is the exact choice, and CheckOmodRefused covers all four opcodes (the second dword sets omod to mul:2 with v4, v5, v6 as sources). NaN results now go through unchanged, as on main, including cubema returning the selected major axis without the multiply. The +0 rule for every f32 op as a separate PR sounds good.

The first commit is #500's commit, patch-identical to #500's head, so this lands after #500.

agc_driver_vop3_cube_clamp passes on my NVIDIA machine on current main, and fails on main's code.

Since it changes cube results without modifiers too, I'll run it through my PPSA21564 check with #500 and merge both if it's clean.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5985220537 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/508#issuecomment-5985220537). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T22:38:12Z. --- Thanks for rerunning it in the IEEE=0, denormals-flushed mode. That settles both points: with omod applied there and main's `applyF32ResultModifiers` giving -0 where the hardware gives +0, keeping omod refused on the cube ops is the exact choice, and `CheckOmodRefused` covers all four opcodes (the second dword sets omod to mul:2 with v4, v5, v6 as sources). NaN results now go through unchanged, as on main, including cubema returning the selected major axis without the multiply. The +0 rule for every f32 op as a separate PR sounds good. The first commit is #500's commit, patch-identical to #500's head, so this lands after #500. `agc_driver_vop3_cube_clamp` passes on my NVIDIA machine on current main, and fails on main's code. Since it changes cube results without modifiers too, I'll run it through my PPSA21564 check with #500 and merge both if it's clean.
oneandonlydean commented 2026-10-05 07:26:33 +00:00 (Migrated from github.com)

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


#610 landed today and added isF32DivisionStepOpcode(opcode) to the supportsNativeVop3Clamp return line, the same line where this PR adds isCubeOpcode(opcode), so it conflicts now. Keeping both terms fixes it:

return supportsNativeVop3ResultModifiers(opcode) || usesInexactClampControl(opcode) || usesSignedSaturateClamp(opcode) ||
    isF32DivisionStepOpcode(opcode) || isCubeOpcode(opcode);

Could you rebase? I tried it on current main together with #500 and #507, and the full ctest passes. #507 renames usesSignedSaturateClamp to usesIntegerSaturateClamp on this line, so whichever of the two lands second needs to keep the rename and all the terms.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5990012159 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/508#issuecomment-5990012159). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-05T07:26:33Z. --- #610 landed today and added `isF32DivisionStepOpcode(opcode)` to the `supportsNativeVop3Clamp` return line, the same line where this PR adds `isCubeOpcode(opcode)`, so it conflicts now. Keeping both terms fixes it: ```cpp return supportsNativeVop3ResultModifiers(opcode) || usesInexactClampControl(opcode) || usesSignedSaturateClamp(opcode) || isF32DivisionStepOpcode(opcode) || isCubeOpcode(opcode); ``` Could you rebase? I tried it on current main together with #500 and #507, and the full ctest passes. #507 renames `usesSignedSaturateClamp` to `usesIntegerSaturateClamp` on this line, so whichever of the two lands second needs to keep the rename and all the terms.
oneandonlydean commented 2026-10-05 08:31:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T08:31:18Z.


Gated on PPSA21564 on top of #500: no visual or FPS change.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5990921404 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/508#issuecomment-5990921404). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-05T08:31:18Z. --- Gated on PPSA21564 on top of #500: no visual or FPS change.
adriadam10 commented 2026-10-05 08:57:53 +00:00 (Migrated from github.com)

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


Rebased on current main after #610 and #500:

  • supportsNativeVop3Clamp: kept isF32DivisionStepOpcode and added isCubeOpcode, as suggested. The #500 commit dropped out as already merged.
  • agc_driver_vop3* and agc_shader* tests pass on RADV.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5991293687 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/508#issuecomment-5991293687). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-05T08:57:53Z. --- Rebased on current `main` after #610 and #500: - `supportsNativeVop3Clamp`: kept `isF32DivisionStepOpcode` and added `isCubeOpcode`, as suggested. The #500 commit dropped out as already merged. - `agc_driver_vop3*` and `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!508
No description provided.