fix(shader): apply legacy MAD/MAC output modifiers across denormal modes #28479

Open
DannyOppenheimer wants to merge 1 commit from upstream-pr/28479 into main
DannyOppenheimer commented 2026-11-30 22:58:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DannyOppenheimer on 2026-11-30T22:58:16Z.


What

Measure VOP3 result modifiers (mul:2, mul:4, div:2, clamp) on v_mad_legacy_f32 (0x140) and v_mac_legacy_f32_e64 (0x106) natively on RDNA1 (gfx1010) and fix their FLOAT_MODE / s_denorm_mode gating:

  • Hardware behavior (gfx1010 via tools/hw-oracle): because legacy MAD/MAC flush f32 denormal inputs, tiny products, and results in every denorm32 mode (0..3), only IEEE_MODE gates omod. Both FLOAT_MODE denorm32 = 0..3 and dynamic s_denorm_mode 0..3 apply mul:2/mul:4/div:2 whenever IEEE_MODE == 0. div:2 on [2^-126, 2^-125) returns signed zero (±0.0) in all four round32 modes (0..3), mul:2/mul:4 overflow follows round32, and clamp executes after omod (-0.0 to +0.0, NaN to 0 if DX10_CLAMP == 1).
  • core/shader/recompiler/Translation/src/FloatInstructions.cpp (TranslationContext::vFmaLegacyF32): apply scaleF32Result(destination.omod, IrF32(*value)) directly when destination.omod != 0u && !ieeeMode and zero destination.omod before writeOperand(destination, value) (matching floatCube), instead of delegating omod to outputModifierApplies(4u) which disabled scaling on denorm32 >= 2 and could not model s_denorm_mode.
  • core/libs/prx/libSceAgcDriver/tests/execution/F32OutputModifier.cpp: fix CheckLegacyModifiers to expect scaling whenever !mode.ieeeMode, add LegacyModeAndSourceCode covering s_denorm_mode 3/2/1/0 and VOP3 neg/abs source modifiers with omod/clamp, and extend the legacy rows with -2^-126 div:2, flushed -0.0 (-2^-126 * 1.5 + 2^-126), ±FLT_MAX overflow across round32 0..3, and sNaN (ieee=0/1, dx10Clamp=0/1).
  • docs/dev/debt/changes/fix-gfx1013-legacy-mad-mac-modifiers.md: update the unfolded shader debt entry to record the native gfx1010 measurements and gating behavior while leaving Closes when: compared with native gfx1013..

Overlap note: tools/pr_overlap.py --branch fix/gfx1010-legacy-mad-mac-modifiers flagged PR #26100 (FloatInstructions.cpp around vFmaLegacyF32 lines 889–891) and uncompared PR #19505. Manual comparison shows #19505 covers unrelated scalar f16 directed rounding and #26100 covers directed rounding of the legacy product/addend without modifying VOP3 result modifiers (and builds on #19710), so there is no functional overlap.

Tested

  • Hardware oracle (tools/hw-oracle, HIP_VISIBLE_DEVICES=1, AMD Radeon RX 5700 XT, gfx1010, AMD Windows driver 32.0.21045.29003, Windows 11 Home 10.0.26200):
    • Native sweep across 819 input triples × 64 (round32 0..3, ieee 0/1, dx10_clamp 0/1, denorm32 0..3) configs × 16 encodings (v_mad_legacy_f32 and v_mac_legacy_f32_e64 × none/mul:2/mul:4/div:2 × unclamped/clamp, mixing wave32/wave64): 0 mismatches across 838,656 results.
    • HW_ORACLE_RUNTIME=vulkan (agc_oracle_runner) vs native HIP gfx1010: 2,652 mismatches on baseline 76d0a4a7dcdf (main_16cfg: 1,584, src_mods: 540, s_denorm_mode: 528) reduced to 0 mismatches on acac33539731 across 262,144 results (1,024 rows × 16 configs × 16 forms at round32=0), exact round32 1..3 overflow/underflow cases, source modifiers, and s_denorm_mode 0..3.
  • Vulkan validation gate (tools/vulkan_validation_gate.py, Khronos validation layer 1.4.369, ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, VK_LOADER_DEBUG=layer):
    • AMD Radeon RX 5700 XT: 8/8 tests passed across 2 iterations on both baseline (76d0a4a7dcdf) and candidate (acac33539731) (agc_driver_dx10_clamp, agc_driver_f32_denormal_flush, agc_driver_f32_output_modifier, agc_driver_f32_result_clamp, agc_driver_fma_legacy, agc_driver_legacy_product_tininess, agc_driver_mac_legacy, agc_driver_transcendental_output_modifier), Insert instance layer "VK_LAYER_KHRONOS_validation" logged 8 times per iteration (0 missing-layer lines, 0 skipped, 0 VUID- diagnostics).
    • AMD Radeon(TM) Graphics: same 8 focused ctest -V runs under identical validation environment passed on both baseline (8/8 in 9.37 s) and candidate (8/8 in 11.25 s), Insert instance layer "VK_LAYER_KHRONOS_validation" logged 8 times each, 0 missing-layer lines, 0 VUID- diagnostics.
  • Non-Vulkan recompiler tests & repo checks (acac33539731):
    • ctest -V: 5/5 passed (agc_shader_mode_register, vector_constant_factor_tininess, vector_fused_multiply_add, vector_half_double_output_modifiers, vector_mad_legacy).
    • python tools/debt_fold.py --check, python tools/check_conventions.py, and python tools/pr_overlap.py --branch fix/gfx1010-legacy-mad-mac-modifiers passed.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: AMD Radeon RX 5700 XT (gfx1010, RDNA1, driver 32.0.21045.29003, Vulkan 2.0.353 / 26.11.2) and AMD Radeon(TM) Graphics (gfx1036, RDNA2, driver 32.0.21045.29003, Vulkan 2.0.353 / 26.11.2), Microsoft Windows 11 Home 10.0.26200
  • AI-assisted: yes

Summary by CodeRabbit

  • Bug Fixes
    • Corrected legacy shader output modifier handling in non-IEEE mode, improving the accuracy of scaled and clamped results.
    • Expanded validation across rounding modes and floating-point edge cases, including signed zero, subnormal values, infinities, and NaNs.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5575312826 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28479). Originally posted by [DannyOppenheimer](https://github.com/DannyOppenheimer) on 2026-11-30T22:58:16Z. --- ### What Measure VOP3 result modifiers (`mul:2`, `mul:4`, `div:2`, `clamp`) on `v_mad_legacy_f32` (`0x140`) and `v_mac_legacy_f32_e64` (`0x106`) natively on RDNA1 (`gfx1010`) and fix their `FLOAT_MODE` / `s_denorm_mode` gating: - Hardware behavior (`gfx1010` via `tools/hw-oracle`): because legacy MAD/MAC flush f32 denormal inputs, tiny products, and results in every `denorm32` mode (`0..3`), **only `IEEE_MODE` gates `omod`**. Both `FLOAT_MODE` `denorm32 = 0..3` and dynamic `s_denorm_mode 0..3` apply `mul:2`/`mul:4`/`div:2` whenever `IEEE_MODE == 0`. `div:2` on `[2^-126, 2^-125)` returns signed zero (`±0.0`) in all four `round32` modes (`0..3`), `mul:2`/`mul:4` overflow follows `round32`, and `clamp` executes after `omod` (`-0.0` to `+0.0`, NaN to `0` if `DX10_CLAMP == 1`). - `core/shader/recompiler/Translation/src/FloatInstructions.cpp` (`TranslationContext::vFmaLegacyF32`): apply `scaleF32Result(destination.omod, IrF32(*value))` directly when `destination.omod != 0u && !ieeeMode` and zero `destination.omod` before `writeOperand(destination, value)` (matching `floatCube`), instead of delegating `omod` to `outputModifierApplies(4u)` which disabled scaling on `denorm32 >= 2` and could not model `s_denorm_mode`. - `core/libs/prx/libSceAgcDriver/tests/execution/F32OutputModifier.cpp`: fix `CheckLegacyModifiers` to expect scaling whenever `!mode.ieeeMode`, add `LegacyModeAndSourceCode` covering `s_denorm_mode 3/2/1/0` and VOP3 `neg`/`abs` source modifiers with `omod`/`clamp`, and extend the legacy rows with `-2^-126` `div:2`, flushed `-0.0` (`-2^-126 * 1.5 + 2^-126`), `±FLT_MAX` overflow across `round32` `0..3`, and `sNaN` (`ieee=0/1`, `dx10Clamp=0/1`). - `docs/dev/debt/changes/fix-gfx1013-legacy-mad-mac-modifiers.md`: update the unfolded shader debt entry to record the native `gfx1010` measurements and gating behavior while leaving `Closes when: compared with native gfx1013.`. Overlap note: `tools/pr_overlap.py --branch fix/gfx1010-legacy-mad-mac-modifiers` flagged [PR #26100](https://github.com/boykopovar/AnyPS5/pull/26100) (`FloatInstructions.cpp` around `vFmaLegacyF32` lines 889–891) and uncompared [PR #19505](https://github.com/boykopovar/AnyPS5/pull/19505). Manual comparison shows #19505 covers unrelated scalar `f16` directed rounding and #26100 covers directed rounding of the legacy product/addend without modifying VOP3 result modifiers (and builds on `#19710`), so there is no functional overlap. ### Tested - Hardware oracle (`tools/hw-oracle`, `HIP_VISIBLE_DEVICES=1`, `AMD Radeon RX 5700 XT`, `gfx1010`, AMD Windows driver `32.0.21045.29003`, Windows 11 Home `10.0.26200`): - Native sweep across `819` input triples × `64` (`round32 0..3`, `ieee 0/1`, `dx10_clamp 0/1`, `denorm32 0..3`) configs × `16` encodings (`v_mad_legacy_f32` and `v_mac_legacy_f32_e64` × `none`/`mul:2`/`mul:4`/`div:2` × unclamped/`clamp`, mixing wave32/wave64): `0` mismatches across `838,656` results. - `HW_ORACLE_RUNTIME=vulkan` (`agc_oracle_runner`) vs native HIP `gfx1010`: `2,652` mismatches on baseline `76d0a4a7dcdf` (`main_16cfg`: `1,584`, `src_mods`: `540`, `s_denorm_mode`: `528`) reduced to `0` mismatches on `acac33539731` across `262,144` results (`1,024` rows × `16` configs × `16` forms at `round32=0`), exact `round32` `1..3` overflow/underflow cases, source modifiers, and `s_denorm_mode` `0..3`. - Vulkan validation gate (`tools/vulkan_validation_gate.py`, Khronos validation layer `1.4.369`, `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1`, `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, `VK_LOADER_DEBUG=layer`): - `AMD Radeon RX 5700 XT`: `8/8` tests passed across `2` iterations on both baseline (`76d0a4a7dcdf`) and candidate (`acac33539731`) (`agc_driver_dx10_clamp`, `agc_driver_f32_denormal_flush`, `agc_driver_f32_output_modifier`, `agc_driver_f32_result_clamp`, `agc_driver_fma_legacy`, `agc_driver_legacy_product_tininess`, `agc_driver_mac_legacy`, `agc_driver_transcendental_output_modifier`), `Insert instance layer "VK_LAYER_KHRONOS_validation"` logged `8` times per iteration (`0` missing-layer lines, `0` skipped, `0` `VUID-` diagnostics). - `AMD Radeon(TM) Graphics`: same `8` focused `ctest -V` runs under identical validation environment passed on both baseline (`8/8` in `9.37 s`) and candidate (`8/8` in `11.25 s`), `Insert instance layer "VK_LAYER_KHRONOS_validation"` logged `8` times each, `0` missing-layer lines, `0` `VUID-` diagnostics. - Non-Vulkan recompiler tests & repo checks (`acac33539731`): - `ctest -V`: `5/5` passed (`agc_shader_mode_register`, `vector_constant_factor_tininess`, `vector_fused_multiply_add`, `vector_half_double_output_modifiers`, `vector_mad_legacy`). - `python tools/debt_fold.py --check`, `python tools/check_conventions.py`, and `python tools/pr_overlap.py --branch fix/gfx1010-legacy-mad-mac-modifiers` passed. ### 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) and the licence notice of code adapted from upstream (see AGENTS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: AMD Radeon RX 5700 XT (gfx1010, RDNA1, driver 32.0.21045.29003, Vulkan 2.0.353 / 26.11.2) and AMD Radeon(TM) Graphics (gfx1036, RDNA2, driver 32.0.21045.29003, Vulkan 2.0.353 / 26.11.2), Microsoft Windows 11 Home 10.0.26200 - [x] AI-assisted: yes <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Bug Fixes** * Corrected legacy shader output modifier handling in non-IEEE mode, improving the accuracy of scaled and clamped results. * Expanded validation across rounding modes and floating-point edge cases, including signed zero, subnormal values, infinities, and NaNs. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
MarcZX10 commented 2026-11-30 23:01:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-30T23:01:11Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this; the points below are optional.

What I ran

Reviewed head acac3353973176359dad4e885f64eafcc89883ca by inspecting the prepared diff and changed files, plus pinned source context from repo_read.py. No tests or builds were run.

repo_read.py search scaleF32Result
repo_read.py cat core/shader/recompiler/Translation/src/FloatInstructions.cpp 700 742
repo_read.py cat core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cpp 465 515

The reader printed the requested source, but could not append its audit log because the run directory is read-only.

Results

Check Result Note
Source review passed All three reviewed files inspected
Tests and builds not run Hardware and CI results are author-reported

Negative control

Not run; no tests were run during this review.

Findings

Blocking

None.

Non-blocking

None.

Sources

The changed translation applies scaleF32Result when IEEE_MODE is off, clears omod, and leaves clamp processing to writeOperand, matching the existing result-modifier pattern in floatCube (changed implementation). The associated test additions exercise denormal modes, source modifiers, clamp, NaNs, and directed-rounding overflow (test changes).

Not verified

Hardware oracle results, Vulkan validation, CTest, and repository checks were not independently run; those results are reported in the PR description. Files read: core/libs/prx/libSceAgcDriver/tests/execution/F32OutputModifier.cpp, core/shader/recompiler/Translation/src/FloatInstructions.cpp, and docs/dev/debt/changes/fix-gfx1013-legacy-mad-mac-modifiers.md. No files listed under reviewed were excluded.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6122112400 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28479#pullrequestreview-6122112400). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-30T23:01:11Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this; the points below are optional. ## What I ran Reviewed head `acac3353973176359dad4e885f64eafcc89883ca` by inspecting the prepared diff and changed files, plus pinned source context from `repo_read.py`. No tests or builds were run. ```text repo_read.py search scaleF32Result repo_read.py cat core/shader/recompiler/Translation/src/FloatInstructions.cpp 700 742 repo_read.py cat core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cpp 465 515 ``` The reader printed the requested source, but could not append its audit log because the run directory is read-only. ## Results | Check | Result | Note | |---|---|---| | Source review | passed | All three reviewed files inspected | | Tests and builds | not run | Hardware and CI results are author-reported | ## Negative control Not run; no tests were run during this review. ## Findings ### Blocking None. ### Non-blocking None. ## Sources The changed translation applies `scaleF32Result` when `IEEE_MODE` is off, clears `omod`, and leaves clamp processing to `writeOperand`, matching the existing result-modifier pattern in `floatCube` ([changed implementation](https://github.com/boykopovar/AnyPS5/blob/acac3353973176359dad4e885f64eafcc89883ca/core/shader/recompiler/Translation/src/FloatInstructions.cpp#L891)). The associated test additions exercise denormal modes, source modifiers, clamp, NaNs, and directed-rounding overflow ([test changes](https://github.com/boykopovar/AnyPS5/blob/acac3353973176359dad4e885f64eafcc89883ca/core/libs/prx/libSceAgcDriver/tests/execution/F32OutputModifier.cpp#L265)). ## Not verified Hardware oracle results, Vulkan validation, CTest, and repository checks were not independently run; those results are reported in the PR description. Files read: `core/libs/prx/libSceAgcDriver/tests/execution/F32OutputModifier.cpp`, `core/shader/recompiler/Translation/src/FloatInstructions.cpp`, and `docs/dev/debt/changes/fix-gfx1013-legacy-mad-mac-modifiers.md`. No files listed under `reviewed` were excluded. ## To get this to a good state Nothing is required.
ligmaSec commented 2026-12-03 08:26:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T08:26:05Z.

Review: COMMENTED


Both agc_driver_f32_output_modifier and agc_driver_gcn_scalar_buffer_base passed on the main baseline (96401c8077) and the patch. The changed fixture exercises legacy MAD and MAC results with the modifier, clamp and denormal combinations. Both runs used Linux lavapipe with Vulkan required and the shader cache disabled. The loader inserted Khronos validation 1.4.369, and neither log contained a VUID.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6166072761 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28479#pullrequestreview-6166072761). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T08:26:05Z. **Review:** COMMENTED --- Both agc_driver_f32_output_modifier and agc_driver_gcn_scalar_buffer_base passed on the main baseline (96401c8077) and the patch. The changed fixture exercises legacy MAD and MAC results with the modifier, clamp and denormal combinations. Both runs used Linux lavapipe with Vulkan required and the shader cache disabled. The loader inserted Khronos validation 1.4.369, and neither log contained a VUID. AI-assisted: yes.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/28479:upstream-pr/28479'
git switch 'upstream-pr/28479'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/28479'
git switch 'upstream-pr/28479'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/28479'
git switch 'upstream-pr/28479'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/28479'
git switch 'main'
git merge --squash 'upstream-pr/28479'
git switch 'main'
git merge --ff-only 'upstream-pr/28479'
git switch 'main'
git merge 'upstream-pr/28479'
git push origin 'main'
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!28479
No description provided.