fix(shader): apply the f32 output modifier on the bits like the hardware #567

Merged
adriadam10 merged 5 commits from upstream-pr/567 into main 2026-10-05 05:07:22 +00:00
adriadam10 commented 2026-10-04 21:55:53 +00:00 (Migrated from github.com)

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


What

The VOP3 output modifier (mul:2, mul:4, div:2) on f32 results was a plain FMul by 2, 4 or 0.5. That differs from the hardware in three places, and the NaN result was left to the driver:

  • a zero result becomes +0 (main kept -0, e.g. v_mul_f32_e64 v, -0, 1.0 mul:2);
  • with div:2, a result that drops below the smallest normal becomes a zero with the sign of the result (0x80800000 → 0x80000000, and 0x00ffffff → +0 where round to nearest gives 0x00800000);
  • a NaN result passes through unchanged, signaling NaNs included. FMul on a NaN lets the driver choose the payload (NVIDIA returns 0x7fffffff).

applyF32ResultModifiers now picks these cases on the bits of the unmodified result. Other results still go through FMul, which is exact for these factors, overflow to infinity included. Clamp is still applied after the output modifier.

f16 results follow #323: applyF16ResultModifiers ignores omod, as the hardware does while f16 denormals are enabled (the LLVM default), so this PR only changes the f32 path. I added a line to TechnicalDebt for the mode assumption of the f32 rule and for the f16 case with denormals flushed, which isn't modelled. packedFloat16 (VOP3P) has no output modifier field, so its call sites don't change.

Behaviour measured on HW

The measurements were taken on a gfx1035 GPU through the HSA runtime, with MODE set to IEEE=0 and f32 denormals flushed. That is the mode the recompiler already assumes for f32 (the LLVM graphics default). With IEEE=1, or with f32 output denormals on, the hardware ignores the output modifier.

  • 3072 rows, each run through v_mul/add/fma/min/max/fract/cvt_f32_i32 and each of the three modifiers. Every cell matches the rule above when it is applied to the hardware's own unmodified result:
    • NaN: unchanged;
    • magnitude below 0x00800000: +0;
    • div:2 with magnitude below 0x01000000: signed zero;
    • otherwise the f32 product.
  • The modifier is applied after the operation has rounded, so double rounding is possible: v_mul_f32 2.0, 0xff61b1e6 div:2 overflows to -inf and stays -inf.

Tested

  • New agc_driver_f32_output_modifier test (tests/execution/F32OutputModifier.cpp): 16 forms (mul, add, sub, fma, min, max, floor, cvt_f32_i32 with each modifier, and clamp after the modifier), with 32 rows of expected values from the hardware. A cell is checked only when AnyPS5's result without the modifier is defined on every driver: no subnormal inputs or results, and fma only where the product is exact. For max with two identical NaN sources the NaN bits are chosen by our code, so those cells are checked bit for bit (sNaN included). For add/mul/fma NaN cells the driver chooses the NaN, so they only require a NaN.
  • The test fails without the change ("lane 0 v_mul_f32_e64 v10, v4, v5 mul:2 is 0x80000000, expected 0x00000000") on RADV and on lavapipe. Each select was mutated separately and each mutation fails the test. The NaN select fails on lavapipe. The div:2 underflow select passes both local drivers when it is removed, because they flush the same way, so I checked that the rows reach that branch by changing its value.
  • Passes on RADV (Mesa 26.2), local lavapipe and CI's lavapipe (Mesa 25.2.8, cold run under 1 s). Full Release ctest on Linux: everything passes except agc_driver_pixel_helper_lanes, agc_driver_global_load_addtid and agc_driver_vop3_carry_out, which also fail on main on this RADV (wave32 lane 32 on RADV's 64-wide subgroup).

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: #537 (carries #323); the first four commits are theirs, unchanged
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4737914714 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/567). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-04T21:55:53Z. --- ### What The VOP3 output modifier (`mul:2`, `mul:4`, `div:2`) on f32 results was a plain `FMul` by 2, 4 or 0.5. That differs from the hardware in three places, and the NaN result was left to the driver: - a zero result becomes +0 (main kept -0, e.g. `v_mul_f32_e64 v, -0, 1.0 mul:2`); - with `div:2`, a result that drops below the smallest normal becomes a zero with the sign of the result (`0x80800000` → `0x80000000`, and `0x00ffffff` → `+0` where round to nearest gives `0x00800000`); - a NaN result passes through unchanged, signaling NaNs included. `FMul` on a NaN lets the driver choose the payload (NVIDIA returns `0x7fffffff`). `applyF32ResultModifiers` now picks these cases on the bits of the unmodified result. Other results still go through `FMul`, which is exact for these factors, overflow to infinity included. Clamp is still applied after the output modifier. f16 results follow #323: `applyF16ResultModifiers` ignores omod, as the hardware does while f16 denormals are enabled (the LLVM default), so this PR only changes the f32 path. I added a line to TechnicalDebt for the mode assumption of the f32 rule and for the f16 case with denormals flushed, which isn't modelled. `packedFloat16` (VOP3P) has no output modifier field, so its call sites don't change. #### Behaviour measured on HW The measurements were taken on a gfx1035 GPU through the HSA runtime, with MODE set to IEEE=0 and f32 denormals flushed. That is the mode the recompiler already assumes for f32 (the LLVM graphics default). With IEEE=1, or with f32 output denormals on, the hardware ignores the output modifier. - 3072 rows, each run through `v_mul/add/fma/min/max/fract/cvt_f32_i32` and each of the three modifiers. Every cell matches the rule above when it is applied to the hardware's own unmodified result: - NaN: unchanged; - magnitude below `0x00800000`: +0; - `div:2` with magnitude below `0x01000000`: signed zero; - otherwise the f32 product. - The modifier is applied after the operation has rounded, so double rounding is possible: `v_mul_f32 2.0, 0xff61b1e6 div:2` overflows to -inf and stays -inf. ### Tested - New `agc_driver_f32_output_modifier` test (`tests/execution/F32OutputModifier.cpp`): 16 forms (mul, add, sub, fma, min, max, floor, cvt_f32_i32 with each modifier, and clamp after the modifier), with 32 rows of expected values from the hardware. A cell is checked only when AnyPS5's result without the modifier is defined on every driver: no subnormal inputs or results, and fma only where the product is exact. For max with two identical NaN sources the NaN bits are chosen by our code, so those cells are checked bit for bit (sNaN included). For add/mul/fma NaN cells the driver chooses the NaN, so they only require a NaN. - The test fails without the change ("lane 0 v_mul_f32_e64 v10, v4, v5 mul:2 is 0x80000000, expected 0x00000000") on RADV and on lavapipe. Each select was mutated separately and each mutation fails the test. The NaN select fails on lavapipe. The div:2 underflow select passes both local drivers when it is removed, because they flush the same way, so I checked that the rows reach that branch by changing its value. - Passes on RADV (Mesa 26.2), local lavapipe and CI's lavapipe (Mesa 25.2.8, cold run under 1 s). Full Release ctest on Linux: everything passes except `agc_driver_pixel_helper_lanes`, `agc_driver_global_load_addtid` and `agc_driver_vop3_carry_out`, which also fail on `main` on this RADV (wave32 lane 32 on RADV's 64-wide subgroup). ### 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: #537 (carries #323); the first four commits are theirs, unchanged - [x] AI-assisted: yes
oneandonlydean commented 2026-10-04 22:38:22 +00:00 (Migrated from github.com)

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


Thanks, this is the follow-up from #508. The rule reads right to me: the picks are made on the bits of the unmodified result, the div:2 underflow select runs before the general "below 0x00800000 gives +0" select (so a zero input still gives +0), NaN is kept as is, consistent with IEEE=0 as in #508, and everything else goes through an FMul that is exact for these factors. Clamp after the modifier matches the ISA's order, and the TechnicalDebt line states the mode assumption.

The one thing to sort out is the order with #323 and #537, which change the same three files:

  • #323 (carried by #537) adds applyF16ResultModifiers, which applies clamp and ignores omod on f16 results, and drops the result modifiers from v_cvt_pkrtz_f16_f32. That is the hardware rule in the LLVM default mode (f16 denormals on), which your TechnicalDebt line describes too, so the two PRs agree on the hardware but the code collides: after #323 the f16 call sites no longer reach applyF32ResultModifiers, so the float16Result parameter has no callers, and the TechnicalDebt sentence "On f16 results it is still a plain multiply" would no longer be true.
  • Since #323 and #537 are further along, could this be rebased on #537 after they land, dropping float16Result and rewording the f16 sentence to match #323 (omod ignored on f16 results; the flushed-denormal +0 case is the gap)?

agc_driver_f32_output_modifier passes on my NVIDIA machine at this head, and on main's code it fails at lane 0 (v_mul_f32 ... mul:2 gives 0x80000000 where 0x00000000 is expected).

Output modifiers are on PPSA21564's shader path, so once it's rebased I'll run it through my PPSA21564 check before merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5985221825 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/567#issuecomment-5985221825). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T22:38:22Z. --- Thanks, this is the follow-up from #508. The rule reads right to me: the picks are made on the bits of the unmodified result, the `div:2` underflow select runs before the general "below 0x00800000 gives +0" select (so a zero input still gives +0), NaN is kept as is, consistent with IEEE=0 as in #508, and everything else goes through an FMul that is exact for these factors. Clamp after the modifier matches the ISA's order, and the TechnicalDebt line states the mode assumption. The one thing to sort out is the order with #323 and #537, which change the same three files: - #323 (carried by #537) adds `applyF16ResultModifiers`, which applies clamp and ignores omod on f16 results, and drops the result modifiers from `v_cvt_pkrtz_f16_f32`. That is the hardware rule in the LLVM default mode (f16 denormals on), which your TechnicalDebt line describes too, so the two PRs agree on the hardware but the code collides: after #323 the f16 call sites no longer reach `applyF32ResultModifiers`, so the `float16Result` parameter has no callers, and the TechnicalDebt sentence "On f16 results it is still a plain multiply" would no longer be true. - Since #323 and #537 are further along, could this be rebased on #537 after they land, dropping `float16Result` and rewording the f16 sentence to match #323 (omod ignored on f16 results; the flushed-denormal +0 case is the gap)? `agc_driver_f32_output_modifier` passes on my NVIDIA machine at this head, and on main's code it fails at lane 0 (`v_mul_f32 ... mul:2` gives 0x80000000 where 0x00000000 is expected). Output modifiers are on PPSA21564's shader path, so once it's rebased I'll run it through my PPSA21564 check before merging.
adriadam10 commented 2026-10-05 01:34:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-05T01:34:26Z.


Rebased on current main: the only conflict was the neighbouring TechnicalDebt line (now sceAjmDecMp3ParseFrame); the omod line is unchanged. agc_driver_f32_output_modifier_tests passes on RADV and lavapipe, full ctest green on RADV apart from the known agc_driver_pixel_helper_lanes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5986612439 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/567#issuecomment-5986612439). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-05T01:34:26Z. --- Rebased on current main: the only conflict was the neighbouring TechnicalDebt line (now `sceAjmDecMp3ParseFrame`); the omod line is unchanged. `agc_driver_f32_output_modifier_tests` passes on RADV and lavapipe, full ctest green on RADV apart from the known `agc_driver_pixel_helper_lanes`.
oneandonlydean commented 2026-10-05 02:47:18 +00:00 (Migrated from github.com)

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


Thanks for the rebase. The TechnicalDebt conflict is resolved the right way, and the commit is otherwise the same as before.

One thing I noticed while merging the open shader PRs together: this now conflicts with #323 (and so with #537, which carries #323's commits) in TranslationContext.hpp, FloatInstructions.cpp and ConvertInstructions.cpp, and it isn't only textual. #323 adds applyF16ResultModifiers, which applies clamp but not omod to f16 results, while this PR adds a float16Result flag to applyF32ResultModifiers and your TechnicalDebt line says omod on f16 results "is still a plain multiply". Whichever lands second has to pick one rule for f16 (and the TechnicalDebt line should match it). Since #323 and #537 are ahead of this one, could you rebase it onto #537 and keep #323's f16 handling?

The f32 part is unchanged and still looks right to me. PPSA21564 uses mul:2, mul:4 and div:2 in about 2,500 of its shaders, so this one goes through my PPSA21564 check before merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5987229334 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/567#issuecomment-5987229334). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-05T02:47:18Z. --- Thanks for the rebase. The TechnicalDebt conflict is resolved the right way, and the commit is otherwise the same as before. One thing I noticed while merging the open shader PRs together: this now conflicts with #323 (and so with #537, which carries #323's commits) in `TranslationContext.hpp`, `FloatInstructions.cpp` and `ConvertInstructions.cpp`, and it isn't only textual. #323 adds `applyF16ResultModifiers`, which applies clamp but not omod to f16 results, while this PR adds a `float16Result` flag to `applyF32ResultModifiers` and your TechnicalDebt line says omod on f16 results "is still a plain multiply". Whichever lands second has to pick one rule for f16 (and the TechnicalDebt line should match it). Since #323 and #537 are ahead of this one, could you rebase it onto #537 and keep #323's f16 handling? The f32 part is unchanged and still looks right to me. PPSA21564 uses `mul:2`, `mul:4` and `div:2` in about 2,500 of its shaders, so this one goes through my PPSA21564 check before merging.
adriadam10 commented 2026-10-05 03:01:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-05T03:01:21Z.


Rebased onto #537 (carries #323's commits unchanged) and kept #323's f16 rule:

  • dropped the float16Result flag; f16 paths use #323's applyF16ResultModifiers (clamp, no omod) and v_cvt_pkrtz_f16_f32 keeps #323's unmodified inputs.
  • TechnicalDebt line now says omod is ignored on f16 results (hardware with f16 denormals on); the flushed-denormal case is the unmodelled gap.
  • description and "Depends on" updated. agc_driver_f32_output_modifier, agc_driver_float16_result_modifiers and agc_driver_vop1_float_unary pass on RADV and lavapipe; full ctest green except agc_driver_pixel_helper_lanes (fails on main on RADV).
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5987347207 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/567#issuecomment-5987347207). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-05T03:01:21Z. --- Rebased onto #537 (carries #323's commits unchanged) and kept #323's f16 rule: - dropped the `float16Result` flag; f16 paths use #323's `applyF16ResultModifiers` (clamp, no omod) and `v_cvt_pkrtz_f16_f32` keeps #323's unmodified inputs. - TechnicalDebt line now says omod is ignored on f16 results (hardware with f16 denormals on); the flushed-denormal case is the unmodelled gap. - description and "Depends on" updated. `agc_driver_f32_output_modifier`, `agc_driver_float16_result_modifiers` and `agc_driver_vop1_float_unary` pass on RADV and lavapipe; full ctest green except `agc_driver_pixel_helper_lanes` (fails on main on RADV).
oneandonlydean commented 2026-10-05 05:03:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T05:03:51Z.


Thanks, this is what I was after. The branch is now #537's head (f3b74e2, with #323's three commits unchanged) plus only this commit, and the range-diff against the previous version shows just the f16 parts gone: no float16Result flag, the f16 unary and trig paths and v_cvt_pkrtz_f16_f32 keep #323's handling (applyF16ResultModifiers, unmodified inputs for pkrtz), and the f32 bit rule is the same as before. The TechnicalDebt line now matches #323's rule.

#323, then #537, then this one merges cleanly at every step here, and the full ctest passes on main with the stack merged (agc_driver_f32_output_modifier, agc_driver_float16_result_modifiers and agc_driver_vop1_float_unary included).

As before, PPSA21564 uses mul:2, div:2 and mul:4 in about 2,500 of the 5,688 shaders I have dumped, so the stack goes through my PPSA21564 check before merging. Ready to merge from my side after #537 once that's done.

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

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5988440689 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/567#issuecomment-5988440689). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-05T05:03:51Z. --- Thanks, this is what I was after. The branch is now #537's head (f3b74e2, with #323's three commits unchanged) plus only this commit, and the range-diff against the previous version shows just the f16 parts gone: no `float16Result` flag, the f16 unary and trig paths and `v_cvt_pkrtz_f16_f32` keep #323's handling (`applyF16ResultModifiers`, unmodified inputs for pkrtz), and the f32 bit rule is the same as before. The TechnicalDebt line now matches #323's rule. #323, then #537, then this one merges cleanly at every step here, and the full ctest passes on main with the stack merged (`agc_driver_f32_output_modifier`, `agc_driver_float16_result_modifiers` and `agc_driver_vop1_float_unary` included). As before, PPSA21564 uses `mul:2`, `div:2` and `mul:4` in about 2,500 of the 5,688 shaders I have dumped, so the stack goes through my PPSA21564 check before merging. Ready to merge from my side after #537 once that's done. Gated on PPSA21564 on top of #537: no visual or FPS change.
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!567
No description provided.