perf(shader): round f16 interpolation and mix results in f32 when exact #4171

Closed
karanvora2599 wants to merge 1 commit from upstream-pr/4171 into main
karanvora2599 commented 2026-10-12 00:34:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-12T00:34:13Z.


What

v_fma_mixlo/mixhi_f16, v_mad_mixlo/mixhi_f16, v_interp_p2_f16 and v_interp_p1ll/p1lv_f16 compute their single rounding in f32 when the operands allow it, and fall back to the existing f64 code otherwise. Results are unchanged.

Background. Interpolate (IR FPInterpolateF16/FPInterpolateF32) computes the exact product in f64, adds the third operand with round-to-odd, and rounds once to f16 or f32. On consumer NVIDIA GPUs FP64 runs at 1/32 (Pascal, Turing) or 1/64 (Ampere, Ada, Blackwell) of the f32 rate, so these instructions are the slowest common f16 arithmetic. A v_fma_mixlo_f16 chain took 26× as long as a v_fma_f32 chain on an RTX 4060. #2428 solved the same problem for v_dot2_f32_f16 with an f32 fast path and an f64 fallback; this does the same here.

How.

  • Eligibility:
    • all three operands are finite;
    • one multiplicand has at most 11 significant bits (low 13 bits zero, as every f16 value converted to f32 has);
    • the magnitudes are in bands where every step below is exact: the multiplicands zero or in [2^-40, 2^60), the addend and the result zero or in [2^-100, 2^127);
    • the f16 forms round to nearest even (directed f16 rounding keeps the f64 code inline, as now).
  • Product: the other multiplicand is split into a 13-bit and an 11-bit part (Veltkamp, x·4098, that is 2·2049), so both partial products with the 11-bit multiplicand are exact in f32. No fused multiply-add is relied on.
  • Sum: the two products and the addend go through the three-term sum v_dot2_f32_f16 uses: two-sums, the error terms rounded to odd, one last addition. That is Boldo and Melquiond's correctly rounded sum of three floats.
    • f32 result: the last addition rounds to nearest.
    • f16 result: the last addition is rounded to odd at f32 precision, then converted to f16 with round to nearest even (EmitF32ToF16BitsRte). Rounding to odd keeps 13 more bits than f16, so the double rounding is exact.
    • An f16 overflow comes out as an infinity, where the f64 path returns ±65536. writeF16 treats both the same: without FP16_OVFL both become infinity, and clampF16Overflow clamps any magnitude from 65520 up to infinity when no source is infinite.
  • Fallback: everything else calls the unchanged f64 code, which is now one DontInline function per result type and NaN mode, as dot2_f32_f16_exact is.

Before / after on an RTX 4060 Laptop (Ada, driver 591.86).

Run time: dependent chains over 16,384 groups of 32 lanes, median of 15 interleaved dispatches in one process. For the measurement only, a temporary switch forced the f64 path; it isn't part of the PR.

Chain f64 path this PR
1,024 × v_fma_mixlo_f16 (f16 sources) 82.3 ms 12.9 ms
1,024 × v_fma_mix_f32 (f16 sources; doesn't use Interpolate, control) 5.3 ms 5.8 ms
2,048 × v_fma_f32 (reference) — 6.6 ms

Per instruction, v_fma_mixlo_f16 goes from about 25× a v_fma_f32 to about 4×.

Static size goes the other way. SASS per instruction from APS5_PIPELINE_STATS over chains of 16: v_fma_mixlo/mixhi_f16 and v_mad_mixlo/mixhi_f16 106 → 129, v_fma_mixlo_f16 with f32 sources 90 → 114. Registers drop from 40 to 36. Every instruction now carries the inline fast path plus a call to the shared exact function, and a static count includes both even though only one runs.

Tested

Windows 11, MinGW-w64 GCC 15.2.0, Release, main at bca7a29d. RTX 4060 Laptop (driver 591.86), Intel UHD Graphics (Raptor Lake, driver 101.6790), lavapipe from Mesa 25.2.8 (the version CI installs; Windows build, GALLIUM_OVERRIDE_CPU_CAPS=avx).

  • New agc_driver_fma_mix_f16_exact:
    • v_fma_mixlo_f16 with f16, f16, f16 sources, f32, f16, f32, f16, f32, f16 and f32, f32, f32 (the last mostly takes the fallback).
    • 16 batches of 4,096 lanes, weighted toward near-cancellation, results near the f16 overflow threshold and products that land on f16 subnormals.
    • Run without and with FP16_OVFL, and compared with an exact oracle: the product in 80-bit precision, the sum rounded to odd, then rounded to f16.
    • 524,288 results identical on the RTX 4060 and on lavapipe. The Intel UHD has no shaderFloat64, which the f64 fallback still needs, so it skips this test as it skips agc_driver_mix_precision.
  • agc_driver_interpolation_modes (all three ops, both result types, against the 32 RDNA2 hardware rows in 8 float modes) and agc_driver_mix_precision pass on the RTX 4060 and lavapipe, agc_driver_interpolation_f16 on the RTX 4060 (lavapipe and the Intel UHD skip it). interpolation_modes builds its module by hand, so there the fallback is inlined rather than called.
  • Negative check: rounding the f16 path's last addition to nearest instead of to odd makes the new test fail with a double-rounding error (v_fma_mixlo_f16 (f32, f16, f32): a=0xabf337d7 b=0x4282b6f6 c=0x40fdd000 is 0x000047ee, expected 0x000047ef).
  • Shader tests (agc_driver_*, agc_shader_*, agc_spirv_*, vector_*, agc_masked_select, agc_validate_program, agc_denormal*, agc_float16_*; 338 tests). The BDA tests that hang on driver 591.86 until #3291 lands are left out.
    • RTX 4060: everything passes except agc_driver_prepared_shaders_registered_workgroup_sync, which fails the same way (expected preparation failure) on main.
    • Intel UHD: the same, plus agc_driver_ds_swizzle and agc_driver_f32_dot2_rounding (no Float64; it throws instead of skipping), which also fail on main.
    • lavapipe 25.2.8: the same registered_workgroup_sync failure and the intermittent agc_driver_depth_fast_clear_submit segfault that also happens on main.

Why results don't change. For eligible operands every step before the final rounding is exact or rounds to odd, so the result equals the f64 path's single rounding of the exact value. I checked the algorithm on the CPU against an exact oracle: about 19 million eligible random operand triples, f16 and f32 results, zero mismatches. That oracle computes the exact product in 80-bit precision, rounds the sum to odd, then rounds to f16 or f32. Leaving out either round-to-odd step makes it report mismatches (293 and 90).

Limits.

  • I haven't run it on AMD hardware or on NVIDIA generations other than Ada.
  • The f32-result form (v_interp_p1ll/p1lv_f16) runs only in pixel shaders, so the random test covers it only through the shared code; agc_driver_interpolation_f16 and agc_driver_interpolation_modes check it against the hardware rows.
  • Directed f16 rounding modes, both multiplicands with more than 11 significant bits (v_fma_mix with two f32 multiplicands), and NaN, infinity and extreme magnitudes keep the f64 path.

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: #4932 (I'll rebase onto main once it merges)
  • Unblocks: none
  • GPUs tested: NVIDIA GeForce RTX 4060 Laptop GPU (Ada), driver 591.86, Windows 11; Intel UHD Graphics (Raptor Lake), driver 101.6790, Windows 11; lavapipe (Mesa 25.2.8), Windows 11
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4823545986 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4171). Originally posted by [karanvora2599](https://github.com/karanvora2599) on 2026-10-12T00:34:13Z. --- ### What `v_fma_mixlo/mixhi_f16`, `v_mad_mixlo/mixhi_f16`, `v_interp_p2_f16` and `v_interp_p1ll/p1lv_f16` compute their single rounding in f32 when the operands allow it, and fall back to the existing f64 code otherwise. Results are unchanged. **Background.** `Interpolate` (IR `FPInterpolateF16`/`FPInterpolateF32`) computes the exact product in f64, adds the third operand with round-to-odd, and rounds once to f16 or f32. On consumer NVIDIA GPUs FP64 runs at 1/32 (Pascal, Turing) or 1/64 (Ampere, Ada, Blackwell) of the f32 rate, so these instructions are the slowest common f16 arithmetic. A `v_fma_mixlo_f16` chain took 26× as long as a `v_fma_f32` chain on an RTX 4060. #2428 solved the same problem for `v_dot2_f32_f16` with an f32 fast path and an f64 fallback; this does the same here. **How.** - **Eligibility:** - all three operands are finite; - one multiplicand has at most 11 significant bits (low 13 bits zero, as every f16 value converted to f32 has); - the magnitudes are in bands where every step below is exact: the multiplicands zero or in [2^-40, 2^60), the addend and the result zero or in [2^-100, 2^127); - the f16 forms round to nearest even (directed f16 rounding keeps the f64 code inline, as now). - **Product:** the other multiplicand is split into a 13-bit and an 11-bit part (Veltkamp, `x·4098`, that is 2·2049), so both partial products with the 11-bit multiplicand are exact in f32. No fused multiply-add is relied on. - **Sum:** the two products and the addend go through the three-term sum `v_dot2_f32_f16` uses: two-sums, the error terms rounded to odd, one last addition. That is Boldo and Melquiond's correctly rounded sum of three floats. - f32 result: the last addition rounds to nearest. - f16 result: the last addition is rounded to odd at f32 precision, then converted to f16 with round to nearest even (`EmitF32ToF16BitsRte`). Rounding to odd keeps 13 more bits than f16, so the double rounding is exact. - An f16 overflow comes out as an infinity, where the f64 path returns ±65536. `writeF16` treats both the same: without `FP16_OVFL` both become infinity, and `clampF16Overflow` clamps any magnitude from 65520 up to infinity when no source is infinite. - **Fallback:** everything else calls the unchanged f64 code, which is now one `DontInline` function per result type and NaN mode, as `dot2_f32_f16_exact` is. **Before / after** on an RTX 4060 Laptop (Ada, driver 591.86). Run time: dependent chains over 16,384 groups of 32 lanes, median of 15 interleaved dispatches in one process. For the measurement only, a temporary switch forced the f64 path; it isn't part of the PR. | Chain | f64 path | this PR | |---|---|---| | 1,024 × `v_fma_mixlo_f16` (f16 sources) | 82.3 ms | 12.9 ms | | 1,024 × `v_fma_mix_f32` (f16 sources; doesn't use `Interpolate`, control) | 5.3 ms | 5.8 ms | | 2,048 × `v_fma_f32` (reference) | — | 6.6 ms | Per instruction, `v_fma_mixlo_f16` goes from about 25× a `v_fma_f32` to about 4×. Static size goes the other way. SASS per instruction from `APS5_PIPELINE_STATS` over chains of 16: `v_fma_mixlo/mixhi_f16` and `v_mad_mixlo/mixhi_f16` 106 → 129, `v_fma_mixlo_f16` with f32 sources 90 → 114. Registers drop from 40 to 36. Every instruction now carries the inline fast path plus a call to the shared exact function, and a static count includes both even though only one runs. ### Tested Windows 11, MinGW-w64 GCC 15.2.0, Release, `main` at bca7a29d. RTX 4060 Laptop (driver 591.86), Intel UHD Graphics (Raptor Lake, driver 101.6790), lavapipe from Mesa 25.2.8 (the version CI installs; Windows build, `GALLIUM_OVERRIDE_CPU_CAPS=avx`). - New `agc_driver_fma_mix_f16_exact`: - `v_fma_mixlo_f16` with f16, f16, f16 sources, f32, f16, f32, f16, f32, f16 and f32, f32, f32 (the last mostly takes the fallback). - 16 batches of 4,096 lanes, weighted toward near-cancellation, results near the f16 overflow threshold and products that land on f16 subnormals. - Run without and with `FP16_OVFL`, and compared with an exact oracle: the product in 80-bit precision, the sum rounded to odd, then rounded to f16. - 524,288 results identical on the RTX 4060 and on lavapipe. The Intel UHD has no `shaderFloat64`, which the f64 fallback still needs, so it skips this test as it skips `agc_driver_mix_precision`. - `agc_driver_interpolation_modes` (all three ops, both result types, against the 32 RDNA2 hardware rows in 8 float modes) and `agc_driver_mix_precision` pass on the RTX 4060 and lavapipe, `agc_driver_interpolation_f16` on the RTX 4060 (lavapipe and the Intel UHD skip it). `interpolation_modes` builds its module by hand, so there the fallback is inlined rather than called. - Negative check: rounding the f16 path's last addition to nearest instead of to odd makes the new test fail with a double-rounding error (`v_fma_mixlo_f16 (f32, f16, f32): a=0xabf337d7 b=0x4282b6f6 c=0x40fdd000 is 0x000047ee, expected 0x000047ef`). - Shader tests (`agc_driver_*`, `agc_shader_*`, `agc_spirv_*`, `vector_*`, `agc_masked_select`, `agc_validate_program`, `agc_denormal*`, `agc_float16_*`; 338 tests). The BDA tests that hang on driver 591.86 until #3291 lands are left out. - RTX 4060: everything passes except `agc_driver_prepared_shaders_registered_workgroup_sync`, which fails the same way (`expected preparation failure`) on `main`. - Intel UHD: the same, plus `agc_driver_ds_swizzle` and `agc_driver_f32_dot2_rounding` (no `Float64`; it throws instead of skipping), which also fail on `main`. - lavapipe 25.2.8: the same `registered_workgroup_sync` failure and the intermittent `agc_driver_depth_fast_clear_submit` segfault that also happens on `main`. **Why results don't change.** For eligible operands every step before the final rounding is exact or rounds to odd, so the result equals the f64 path's single rounding of the exact value. I checked the algorithm on the CPU against an exact oracle: about 19 million eligible random operand triples, f16 and f32 results, zero mismatches. That oracle computes the exact product in 80-bit precision, rounds the sum to odd, then rounds to f16 or f32. Leaving out either round-to-odd step makes it report mismatches (293 and 90). **Limits.** - I haven't run it on AMD hardware or on NVIDIA generations other than Ada. - The f32-result form (`v_interp_p1ll/p1lv_f16`) runs only in pixel shaders, so the random test covers it only through the shared code; `agc_driver_interpolation_f16` and `agc_driver_interpolation_modes` check it against the hardware rows. - Directed f16 rounding modes, both multiplicands with more than 11 significant bits (`v_fma_mix` with two f32 multiplicands), and NaN, infinity and extreme magnitudes keep the f64 path. ### 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 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: #4932 (I'll rebase onto `main` once it merges) - [x] Unblocks: none - [x] GPUs tested: NVIDIA GeForce RTX 4060 Laptop GPU (Ada), driver 591.86, Windows 11; Intel UHD Graphics (Raptor Lake), driver 101.6790, Windows 11; lavapipe (Mesa 25.2.8), Windows 11 - [x] AI-assisted: yes
adriadam10 commented 2026-10-13 11:06:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-13T11:06:36Z.

Review: CHANGES_REQUESTED


Checked the fast path against the diff and ran the build and the AGC/AgcDriver ctest on Linux (RADV): agc_driver_fma_mix_f16_exact passes, and rounding the last f16 addition to nearest instead of to odd makes it fail (0x47ee, expected 0x47ef), so the test does pin the double rounding. The algorithm itself looks right.

One blocker:

  • #4932 (open, older) also turns Interpolate in SpirvAluEmitterFloat64.cpp into a DontInline function emitted once per module, and touches SpirvEmitterState.hpp next to your interpolateExactFunctions. The two conflict, and the overlap isn't separable. Please list #4932 under Depends on and rebase once it merges (or tell us if you'd rather it be the other way round), then rerun agc_driver_fma_mix_f16_exact and agc_driver_interpolation_modes on the rebased tree.

Minor, for the same push:

  • The PR body says the other multiplicand is split with x·4097, but 0x45801000 is 4098 (2·2049). The split is still exact (13 + 11 bits against a ≤11-bit multiplicand), so only the description needs fixing.
  • SpirvAluEmitterFloat64.cpp:770 adds a second blank line.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5502563765 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4171#pullrequestreview-5502563765). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-13T11:06:36Z. **Review:** CHANGES_REQUESTED --- Checked the fast path against the diff and ran the build and the AGC/AgcDriver ctest on Linux (RADV): `agc_driver_fma_mix_f16_exact` passes, and rounding the last f16 addition to nearest instead of to odd makes it fail (`0x47ee`, expected `0x47ef`), so the test does pin the double rounding. The algorithm itself looks right. One blocker: - #4932 (open, older) also turns `Interpolate` in `SpirvAluEmitterFloat64.cpp` into a `DontInline` function emitted once per module, and touches `SpirvEmitterState.hpp` next to your `interpolateExactFunctions`. The two conflict, and the overlap isn't separable. Please list #4932 under **Depends on** and rebase once it merges (or tell us if you'd rather it be the other way round), then rerun `agc_driver_fma_mix_f16_exact` and `agc_driver_interpolation_modes` on the rebased tree. Minor, for the same push: - The PR body says the other multiplicand is split with `x·4097`, but `0x45801000` is 4098 (2·2049). The split is still exact (13 + 11 bits against a ≤11-bit multiplicand), so only the description needs fixing. - `SpirvAluEmitterFloat64.cpp:770` adds a second blank line.
karanvora2599 commented 2026-10-13 21:55:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-13T21:55:05Z.


Thanks @adriadam10. Fine to go after #4932; it's now listed under Depends on. Once it merges I'll rebase, putting the f32 fast path in front of its shared Interpolate function, and rerun agc_driver_fma_mix_f16_exact and agc_driver_interpolation_modes on the rebased tree.

  • Split constant: the description now says x·4098 (2·2049), with the 13 + 11-bit split.
  • Blank line: the extra one at SpirvAluEmitterFloat64.cpp:770 goes with that rebase.
  • #5455: the overlap the bot found today is only declarations next to each other in SpirvEmitterState.hpp, SpirvEmitterHelpers.hpp and SpirvModuleEmitter.cpp, so whichever merges second keeps both.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6156017610 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4171#issuecomment-6156017610). Originally posted by [karanvora2599](https://github.com/karanvora2599) on 2026-10-13T21:55:05Z. --- Thanks @adriadam10. Fine to go after #4932; it's now listed under **Depends on**. Once it merges I'll rebase, putting the f32 fast path in front of its shared `Interpolate` function, and rerun `agc_driver_fma_mix_f16_exact` and `agc_driver_interpolation_modes` on the rebased tree. - **Split constant:** the description now says `x·4098` (2·2049), with the 13 + 11-bit split. - **Blank line:** the extra one at `SpirvAluEmitterFloat64.cpp:770` goes with that rebase. - **#5455:** the overlap the bot found today is only declarations next to each other in `SpirvEmitterState.hpp`, `SpirvEmitterHelpers.hpp` and `SpirvModuleEmitter.cpp`, so whichever merges second keeps both.
oneandonlydean commented 2026-10-14 02:39:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T02:39:03Z.


Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6159658935 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4171#issuecomment-6159658935). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T02:39:03Z. --- Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.

Pull request closed

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!4171
No description provided.