refactor(recompiler): emit the v_fma_mixlo/hi and v_interp_*_f16 emulation once per module #4932

Merged
fdfawef324 merged 1 commit from upstream-pr/4932 into main 2026-10-14 17:37:58 +00:00
fdfawef324 commented 2026-10-13 05:09:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fdfawef324 on 2026-10-13T05:09:27Z.


What

FPInterpolateF16 and FPInterpolateF32 (from v_fma_mixlo_f16, v_fma_mixhi_f16, v_interp_p1ll_f16, v_interp_p1lv_f16 and v_interp_p2_f16) were lowered by expanding Interpolate in SpirvAluEmitterFloat64.cpp inline at every use. One expansion is 1203 SPIR-V words (a 1158-word body and 15 NoContraction decorations), and wave64 shaders emit it twice per instruction, once per half. Interpolate is now emitted once per module as a DontInline helper function per result width and float mode, created on first use like the formatted buffer helpers, and every use is an OpFunctionCall.

Semantics are unchanged and come from the existing implementation on main: the function body is Interpolate itself, called with the same halfResult and mode, so rounding, denormal flushes, NaN propagation and the f16 overflow handling are the same instructions. Operand selection and the op_sel/neg/abs modifiers are applied in Translation before the IR op and are not touched. v_fma_mix_f32 is not part of this change: it already lowers to a native Fma (11 words per use).

Measured with agc_shader_replay on the Demon's Souls requests in the local run directory, main at e51428bf2 (word counts, not a speed claim):

Program main this PR v_fma_mixlo/hi expansions on main
0x2489ed100 (120 instructions, 16 mixlo + 16 mixhi, wave64) 145276 69968 64 x 1203 = 76992 words, about 53%
0x2489eab00 (14 mixlo + 14 mixhi, wave64) 287114 221374 56 x 1203 = 67368 words, about 23%
0x2488d5500 688301 688301 none, byte-identical
0x2488dc500 689811 689811 none, byte-identical
0x2488e3800 674320 674320 none, byte-identical

0x248910200 has no saved request, so it was not measured. Over the 66 requests in that directory that recompile (17 others fail resource analysis on main and with this PR alike), the total goes from 12735297 to 12594249 words; only the two programs above contain these instructions, and the other 64 modules are byte-identical.

In 0x2489ed100 the four largest basic blocks of main (three of 28012 words and one of 28041) each hold 16 inline expansions (8 instructions, two wave halves) with their f16 conversions. Denormal flushing costs nothing in these shaders (mode 0; the mix path requires f16 denormals to be kept).

For both changed modules, each OpFunctionCall was replaced by the helper's body with parameters substituted, and the result was compared with main after renaming ids, using constant values, type shapes, decorations and operand dataflow: the instruction streams are identical.

#4171 (karanvora2599, open) changes the same Interpolate code: it renames it to InterpolateExact, moves the denormal flushes out and adds an f32 fast path, and it edits SpirvEmitterState.hpp as well. The two conflict. If #4171 merges first I will rebase this onto it and emit its exact and fast paths once per module the same way; if this one merges first, its helper key (result width and float mode) is where the exact/fast choice would go.

Tested

Current head 8847086a0 is rebased onto 84b01f9cd, retaining the new vertex-input test, grouped BDA loads and rounding frontend. Fresh -O3 CPU builds of 48 actual production units plus a direct backend fixture and the actual main inline control pass their expected verdicts. Two repeated calls reuse each of 16 result-width/mode helper keys: 17 functions including the entry point and 32 calls. The same helper-count fixture fails with the actual current-main inline emitter. The matching whole FmaMixOnce translation unit, conventions and diff checks pass. These checks establish helper structure and reuse; no fresh current-head numerical, native GPU or title execution is claimed. The GPU and replay results below are historical at the revisions explicitly recorded.

Windows Server 2025, RTX 3060, GCC 15.2.

  • New agc_fma_mix_once: compiles a wave32 compute shader with 0, 1 and 3 chained v_fma_mixlo_f16/v_fma_mixhi_f16 and requires that the two extra uses add fewer words than the first use with its shared function, that the first use adds one function and that the extra uses add none. With the fix reverted it fails (2 more v_fma_mixlo/mixhi_f16 added 3657 SPIR-V words, as many as the first one with its shared emulation (2379)). With the fix it passes (697 words without the instruction, 3116 for one, 4381 for three).
  • Tests that cover these paths, run on the RTX 3060 against hardware-measured expectations, passed on main and with this PR: agc_driver_mix_precision, agc_driver_interpolation_modes (all 8 float modes, calling EmitFPInterpolateF16/F32 directly), agc_driver_interpolation_f16, agc_driver_fp16_overflow, agc_driver_f32_arithmetic_nan, agc_driver_f32_denormal_flush, agc_driver_fma_rounding, agc_driver_float16_fma_rounding, agc_driver_float16_result_modifiers, agc_driver_float16_misc, agc_driver_packed_float16_native, agc_driver_pixel_barycentric_emulation, agc_driver_graphics, agc_driver_prepared_graphics, agc_driver_prepared_unregistered_graphics, agc_shader_memory and its variants, agc_vop3_interp_decode, agc_interpolant_mapping, agc_float16_conversion, agc_formatted_buffer_once.
  • All 456 agc_* and shader_* ctest tests built and run with the fix on e51428bf2 (ctest -R "^(agc_|shader_)" -j 4): all pass. agc_storage_upload_lifetime hit its 30 s timeout once under the parallel run and passed when rerun alone.
  • agc_shader_replay before and after on every request above. The tool only recompiles; it cannot execute the shaders, so the replay compares word counts and the SPIR-V itself, not output values.
  • agc_vector_fused_multiply_add (core/libs/tests/VectorFusedMultiplyAdd.cpp, which also covers the mix instructions) passes on main and with this PR.
  • Rebased onto main 3cfb915cf (one conflict in the driver test list, both entries kept); agc_fma_mix_once, agc_driver_mix_precision and agc_driver_interpolation_f16 were rebuilt and pass on it. The other results above are from main e51428bf2.
  • python -I tools/check_test_registration.py build-dev: 746 test executables, none built and not run.
  • Not run: any title, SPIR-V validation with SPIRV-Tools (ANYPS5_ENABLE_SPIRV_TOOLS is off in this build), Linux or lavapipe.

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 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
  • Unblocks: none
  • GPUs tested: NVIDIA GeForce RTX 3060 12 GB, driver 591.86 (32.0.15.9186), Vulkan 1.4.325, Windows Server 2025 10.0.26100
  • AI-assisted: yes

🤖 Generated with Claude Code

Co-Authored-By: Codex [email protected]

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4838663222 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4932). Originally posted by [fdfawef324](https://github.com/fdfawef324) on 2026-10-13T05:09:27Z. --- ### What `FPInterpolateF16` and `FPInterpolateF32` (from `v_fma_mixlo_f16`, `v_fma_mixhi_f16`, `v_interp_p1ll_f16`, `v_interp_p1lv_f16` and `v_interp_p2_f16`) were lowered by expanding `Interpolate` in `SpirvAluEmitterFloat64.cpp` inline at every use. One expansion is 1203 SPIR-V words (a 1158-word body and 15 `NoContraction` decorations), and wave64 shaders emit it twice per instruction, once per half. `Interpolate` is now emitted once per module as a `DontInline` helper function per result width and float mode, created on first use like the formatted buffer helpers, and every use is an `OpFunctionCall`. Semantics are unchanged and come from the existing implementation on `main`: the function body is `Interpolate` itself, called with the same `halfResult` and mode, so rounding, denormal flushes, NaN propagation and the f16 overflow handling are the same instructions. Operand selection and the `op_sel`/`neg`/`abs` modifiers are applied in Translation before the IR op and are not touched. `v_fma_mix_f32` is not part of this change: it already lowers to a native `Fma` (11 words per use). Measured with `agc_shader_replay` on the Demon's Souls requests in the local run directory, `main` at e51428bf2 (word counts, not a speed claim): | Program | main | this PR | v_fma_mixlo/hi expansions on main | |---|---|---|---| | 0x2489ed100 (120 instructions, 16 mixlo + 16 mixhi, wave64) | 145276 | 69968 | 64 x 1203 = 76992 words, about 53% | | 0x2489eab00 (14 mixlo + 14 mixhi, wave64) | 287114 | 221374 | 56 x 1203 = 67368 words, about 23% | | 0x2488d5500 | 688301 | 688301 | none, byte-identical | | 0x2488dc500 | 689811 | 689811 | none, byte-identical | | 0x2488e3800 | 674320 | 674320 | none, byte-identical | 0x248910200 has no saved request, so it was not measured. Over the 66 requests in that directory that recompile (17 others fail resource analysis on `main` and with this PR alike), the total goes from 12735297 to 12594249 words; only the two programs above contain these instructions, and the other 64 modules are byte-identical. In 0x2489ed100 the four largest basic blocks of `main` (three of 28012 words and one of 28041) each hold 16 inline expansions (8 instructions, two wave halves) with their f16 conversions. Denormal flushing costs nothing in these shaders (mode 0; the mix path requires f16 denormals to be kept). For both changed modules, each `OpFunctionCall` was replaced by the helper's body with parameters substituted, and the result was compared with `main` after renaming ids, using constant values, type shapes, decorations and operand dataflow: the instruction streams are identical. ### Related #4171 (karanvora2599, open) changes the same `Interpolate` code: it renames it to `InterpolateExact`, moves the denormal flushes out and adds an f32 fast path, and it edits `SpirvEmitterState.hpp` as well. The two conflict. If #4171 merges first I will rebase this onto it and emit its exact and fast paths once per module the same way; if this one merges first, its helper key (result width and float mode) is where the exact/fast choice would go. ### Tested Current head `8847086a0` is rebased onto `84b01f9cd`, retaining the new vertex-input test, grouped BDA loads and rounding frontend. Fresh `-O3` CPU builds of 48 actual production units plus a direct backend fixture and the actual main inline control pass their expected verdicts. Two repeated calls reuse each of 16 result-width/mode helper keys: 17 functions including the entry point and 32 calls. The same helper-count fixture fails with the actual current-main inline emitter. The matching whole FmaMixOnce translation unit, conventions and diff checks pass. These checks establish helper structure and reuse; no fresh current-head numerical, native GPU or title execution is claimed. The GPU and replay results below are historical at the revisions explicitly recorded. Windows Server 2025, RTX 3060, GCC 15.2. - New `agc_fma_mix_once`: compiles a wave32 compute shader with 0, 1 and 3 chained `v_fma_mixlo_f16`/`v_fma_mixhi_f16` and requires that the two extra uses add fewer words than the first use with its shared function, that the first use adds one function and that the extra uses add none. With the fix reverted it fails (`2 more v_fma_mixlo/mixhi_f16 added 3657 SPIR-V words, as many as the first one with its shared emulation (2379)`). With the fix it passes (697 words without the instruction, 3116 for one, 4381 for three). - Tests that cover these paths, run on the RTX 3060 against hardware-measured expectations, passed on `main` and with this PR: `agc_driver_mix_precision`, `agc_driver_interpolation_modes` (all 8 float modes, calling `EmitFPInterpolateF16/F32` directly), `agc_driver_interpolation_f16`, `agc_driver_fp16_overflow`, `agc_driver_f32_arithmetic_nan`, `agc_driver_f32_denormal_flush`, `agc_driver_fma_rounding`, `agc_driver_float16_fma_rounding`, `agc_driver_float16_result_modifiers`, `agc_driver_float16_misc`, `agc_driver_packed_float16_native`, `agc_driver_pixel_barycentric_emulation`, `agc_driver_graphics`, `agc_driver_prepared_graphics`, `agc_driver_prepared_unregistered_graphics`, `agc_shader_memory` and its variants, `agc_vop3_interp_decode`, `agc_interpolant_mapping`, `agc_float16_conversion`, `agc_formatted_buffer_once`. - All 456 `agc_*` and `shader_*` ctest tests built and run with the fix on e51428bf2 (`ctest -R "^(agc_|shader_)" -j 4`): all pass. `agc_storage_upload_lifetime` hit its 30 s timeout once under the parallel run and passed when rerun alone. - `agc_shader_replay` before and after on every request above. The tool only recompiles; it cannot execute the shaders, so the replay compares word counts and the SPIR-V itself, not output values. - `agc_vector_fused_multiply_add` (core/libs/tests/VectorFusedMultiplyAdd.cpp, which also covers the mix instructions) passes on `main` and with this PR. - Rebased onto `main` 3cfb915cf (one conflict in the driver test list, both entries kept); `agc_fma_mix_once`, `agc_driver_mix_precision` and `agc_driver_interpolation_f16` were rebuilt and pass on it. The other results above are from `main` e51428bf2. - `python -I tools/check_test_registration.py build-dev`: 746 test executables, none built and not run. - Not run: any title, SPIR-V validation with SPIRV-Tools (`ANYPS5_ENABLE_SPIRV_TOOLS` is off in this build), Linux or lavapipe. ### Checklist - [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 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] Unblocks: none - [x] GPUs tested: NVIDIA GeForce RTX 3060 12 GB, driver 591.86 (32.0.15.9186), Vulkan 1.4.325, Windows Server 2025 10.0.26100 - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Codex <[email protected]>
xXPBSXx commented 2026-10-13 07:12:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-13T07:12:18Z.


Windows test (MinGW GCC 15.2, Release, Windows 10, RTX 4080 SUPER). Built on main 88107b7de together with thirteen other open GPU PRs (their own commits cherry-picked, no merge commits), so only the tests that belong to this PR say something about it:

  • tests/FmaMixOnce.cpp (agc_fma_mix_once): 3 of 3 runs passed.

Not tested: no game run. The batch was built once, not per PR; nothing failed, so no bisect was needed.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6140321867 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4932#issuecomment-6140321867). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-13T07:12:18Z. --- Windows test (MinGW GCC 15.2, Release, Windows 10, RTX 4080 SUPER). Built on main `88107b7de` together with thirteen other open GPU PRs (their own commits cherry-picked, no merge commits), so only the tests that belong to this PR say something about it: - `tests/FmaMixOnce.cpp` (`agc_fma_mix_once`): 3 of 3 runs passed. Not tested: no game run. The batch was built once, not per PR; nothing failed, so no bisect was needed. AI-assisted (Claude Code).
adriadam10 commented 2026-10-14 17:26:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-14T17:26:47Z.


Checked the diff, built and ran Agc/AgcDriver ctest: agc_fma_mix_once passes and fails with the SpirvAlu change reverted; agc_driver_interpolation_modes, agc_driver_mix_precision pass (lavapipe). The helper body is the existing Interpolate, so semantics are unchanged. Merging once the full Build is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6174261760 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4932#issuecomment-6174261760). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-14T17:26:47Z. --- Checked the diff, built and ran Agc/AgcDriver ctest: `agc_fma_mix_once` passes and fails with the SpirvAlu change reverted; `agc_driver_interpolation_modes`, `agc_driver_mix_precision` pass (lavapipe). The helper body is the existing `Interpolate`, so semantics are unchanged. Merging once the full Build is green.
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!4932
No description provided.