fix(shader): apply the DX9 zero rule to v_fma_legacy_f32 #595

Merged
NahuelGenchi merged 2 commits from upstream-pr/595 into main 2026-10-05 01:32:00 +00:00
NahuelGenchi commented 2026-10-05 00:54:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by NahuelGenchi on 2026-10-05T00:54:57Z.


What

v_fma_legacy_f32 (VOP3 0x140) was translated as a plain fma. The ISA says it follows the DX9 rule where 0.0 times anything is 0.0, so a zero source with an infinite or NaN other source gave NaN instead of the addend. With a zero source it now gives +0 + src2, which is how v_mul_legacy_f32 and v_mac_legacy_f32 already handle it. The decoder also accepts neg and abs on its sources now. Clamp and omod still throw.

I didn't touch the rounding of VOP2 0x06. Its RDNA 2 name is v_fmac_legacy_f32, but the measured rows in Float16Misc (-1.7 * FLT_MAX + inf gives NaN) show the product is rounded before the add, so I left it as it is.

tools/progress.py now counts the RDNA 2 names of both opcodes too. They were only matched as V_MAC_LEGACY_F32 and V_MAD_LEGACY_F32 before. Shaders go from 1013/1166 to 1015/1166 and VOP2 is complete.

Tested

Ubuntu 26.04, GCC 15.2, AMD R9 380 with RADV and with lavapipe, -DANYPS5_ENABLE_SPIRV_TOOLS=ON.

New agc_driver_fma_legacy test. It runs 20 rows through five forms of the instruction (plain, -src0, |src0| with -src2, an inline 0 as src0, and -src0 with -|src1|) and compares them with a CPU model. The rows cover zero times inf and NaN, inf and NaN addends, overflow and exact cancellation. Every non zero product is exact, so the result doesn't depend on whether the host fuses. I kept signed zero addends out of the zero product rows because I couldn't check that case on RDNA 2 hardware. Without the translation change the first row returns 0xffc00000 instead of 1.0.

Full ctest on lavapipe with ANYPS5_REQUIRE_VULKAN=1: 223/223. On RADV the same three tests fail with and without this change (agc_driver_global_load_addtid, agc_driver_pixel_helper_lanes, agc_driver_vop3_carry_out). They're the wave32 on wave64 failures from #487.

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:4738899722 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/595). Originally posted by [NahuelGenchi](https://github.com/NahuelGenchi) on 2026-10-05T00:54:57Z. --- ### What `v_fma_legacy_f32` (VOP3 0x140) was translated as a plain fma. The ISA says it follows the DX9 rule where 0.0 times anything is 0.0, so a zero source with an infinite or NaN other source gave NaN instead of the addend. With a zero source it now gives +0 + src2, which is how `v_mul_legacy_f32` and `v_mac_legacy_f32` already handle it. The decoder also accepts neg and abs on its sources now. Clamp and omod still throw. I didn't touch the rounding of VOP2 0x06. Its RDNA 2 name is `v_fmac_legacy_f32`, but the measured rows in Float16Misc (-1.7 * FLT_MAX + inf gives NaN) show the product is rounded before the add, so I left it as it is. `tools/progress.py` now counts the RDNA 2 names of both opcodes too. They were only matched as `V_MAC_LEGACY_F32` and `V_MAD_LEGACY_F32` before. Shaders go from 1013/1166 to 1015/1166 and VOP2 is complete. ### Tested Ubuntu 26.04, GCC 15.2, AMD R9 380 with RADV and with lavapipe, `-DANYPS5_ENABLE_SPIRV_TOOLS=ON`. New `agc_driver_fma_legacy` test. It runs 20 rows through five forms of the instruction (plain, -src0, |src0| with -src2, an inline 0 as src0, and -src0 with -|src1|) and compares them with a CPU model. The rows cover zero times inf and NaN, inf and NaN addends, overflow and exact cancellation. Every non zero product is exact, so the result doesn't depend on whether the host fuses. I kept signed zero addends out of the zero product rows because I couldn't check that case on RDNA 2 hardware. Without the translation change the first row returns 0xffc00000 instead of 1.0. Full ctest on lavapipe with `ANYPS5_REQUIRE_VULKAN=1`: 223/223. On RADV the same three tests fail with and without this change (`agc_driver_global_load_addtid`, `agc_driver_pixel_helper_lanes`, `agc_driver_vop3_carry_out`). They're the wave32 on wave64 failures from #487. ### 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-05 01:18:42 +00:00 (Migrated from github.com)

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


Thanks, the zero rule is right, and it matches what main's vMulLegacyF32 already does for VOP2 0x06: a zero source (after neg and abs) makes the product +0 whatever the other source is, so the result is +0 + src2. The new test covers the inf and NaN cases that a plain fma gets wrong, and letting the decoder take neg and abs is fine since readOperand applies them before the zero check.

agc_driver_fma_legacy passes here and fails on main's code, and main's vector_mad_legacy still passes. PPSA21564 has no VOP3 0x140 in its shaders, so this doesn't change my runs.

One question, not blocking: llvm-mc for gfx1013 decodes this encoding (0xd5400000) as v_mad_legacy_f32, and the VOP2 0x06 one as v_mac_legacy_f32. Only gfx1030 has the fma_legacy/fmac_legacy names. Your Float16Misc rows show 0x06 rounding the product before the add, which is the gfx1013 behaviour, so 0x140 may round its product too, the way vMulLegacyF32(inst, true) does. Main already uses a fused op there, so this PR doesn't change it, and your exact-product rows can't tell the two apart. If you have a way to measure a row where the product isn't exact, that would settle it; otherwise a TechnicalDebt line saying 0x140 is assumed to be fused might help the next person.

The workflow needs approval before CI runs. Ready to merge from my side once it's green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5986484154 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/595#issuecomment-5986484154). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-05T01:18:42Z. --- Thanks, the zero rule is right, and it matches what main's `vMulLegacyF32` already does for VOP2 0x06: a zero source (after neg and abs) makes the product +0 whatever the other source is, so the result is +0 + src2. The new test covers the inf and NaN cases that a plain fma gets wrong, and letting the decoder take neg and abs is fine since `readOperand` applies them before the zero check. `agc_driver_fma_legacy` passes here and fails on main's code, and main's `vector_mad_legacy` still passes. PPSA21564 has no VOP3 0x140 in its shaders, so this doesn't change my runs. One question, not blocking: llvm-mc for gfx1013 decodes this encoding (0xd5400000) as `v_mad_legacy_f32`, and the VOP2 0x06 one as `v_mac_legacy_f32`. Only gfx1030 has the `fma_legacy`/`fmac_legacy` names. Your Float16Misc rows show 0x06 rounding the product before the add, which is the gfx1013 behaviour, so 0x140 may round its product too, the way `vMulLegacyF32(inst, true)` does. Main already uses a fused op there, so this PR doesn't change it, and your exact-product rows can't tell the two apart. If you have a way to measure a row where the product isn't exact, that would settle it; otherwise a TechnicalDebt line saying 0x140 is assumed to be fused might help the next person. The workflow needs approval before CI runs. Ready to merge from my side once it's green.
github-actions[bot] commented 2026-10-05 01:30:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-05T01:30:34Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#596 core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp L163
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5986581486 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/595#issuecomment-5986581486). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-05T01:30:34Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #596 | `core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp` [L163](https://github.com/boykopovar/AnyPS5/blob/8a0efb6338ee12edf33a466de229f3b9fa2732c1/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp#L163) | | |
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!595
No description provided.