feat(shader): allow directed rounding modes for packed f16 add #16739

Merged
AlexWhitehouse merged 4 commits from upstream-pr/16739 into main 2026-11-11 04:51:52 +00:00
AlexWhitehouse commented 2026-11-07 14:12:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlexWhitehouse on 2026-11-07T14:12:48Z.


What

TranslationContext::packedFloat16 (core/shader/recompiler/Translation/src/FloatInstructions.cpp) refused every packed f16 instruction except min/max (#14793) when the FLOAT_MODE f16 round field is not round-to-nearest-even:

packed f16 at pc 20 in FLOAT_MODE 52 (f16 rounding not to nearest even) is not implemented

This PR runs v_pk_add_f16 in all four rounding modes. Packed mul, FMA and the other packed arithmetic still refuse directed rounding with the same message.

Sources:

  • The RDNA2 ISA §3.5, Table 4, defines the f16 round field (nearest even, +∞, −∞, zero). §12.10 defines V_PK_ADD_F16 as two independent half additions.
  • LLVM VOP3PInstructions.td (llvmorg-19.1.0, lines 95–99) marks V_PK_ADD_F16 with FPDPRounding = 1.

How it rounds. Each half must be the exact sum rounded once. Rounding an f32 sum would round twice and lose the increment for inputs such as 1 + 2^-24. So each half is computed as lhs * 1 + rhs through the existing FPInterpolateF16 operation, which v_interp_p2_f16 and v_fma_mixlo/hi_f16 already use. It rounds an exact product-sum once to f16 in the selected mode, in f64 or, on a device without the Float64 capability, in 64-bit integers. The packed path's existing steps are unchanged: source selection (op_sel, op_sel_hi, neg_lo, neg_hi), the f16 denormal flags, NaN selection, the FP16 overflow clamp and the clamp modifier. A directed-rounding add doesn't take the native-f16 path, since the native instruction rounds to nearest. Nearest-even adds take the same path as before.

The v_fma_mixlo_f16 debt entry in docs/dev/debt/shader.md, which describes that arithmetic, now lists v_pk_add_f16 and its sources and records that directed packed add is not measured on gfx1013. That entry was already over 1000 characters on main.

Tested

  • New test agc_driver_packed_add_f16_rounding (core/libs/prx/libSceAgcDriver/tests/execution/PackedAddF16Rounding.cpp). It is registered for wave32 and wave64 × the four round modes × the f64 and integer arithmetic, 16 ctests in all, and doesn't depend on subgroup width.

    • Exact results, per lane and per mode:
      • 0xbc003c00 + 0x90001000 (low half 1 + 2^-11, high half -1 - 2^-11) gives 0xbc003c00 at nearest-even and toward zero, 0xbc003c01 toward +∞ and 0xbc013c00 toward −∞;
      • 1 + 2^-24;
      • exact cancellation, which gives −0 toward −∞;
      • overflow to ∞ or the largest finite value per mode;
      • subnormal results, infinities and NaNs.
    • Modifiers: op_sel, neg_lo/neg_hi and clamp.
    • Volume: 69,632 packed results are checked.
  • Linux, lavapipe (Mesa 26.0.8, LLVM 21.1.8, 8-lane subgroups), with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1 and VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation. The run covered the new tests plus agc_driver_packed_alu, agc_driver_packed_f16_denormal_modes, agc_driver_float16_min_max_bits, agc_driver_float16_rounding and agc_driver_fma_mix_f16_denormal_modes:

    • on main, 10/22 pass; the directed-mode cases fail with the message above;
    • with this PR, 22/22 pass, at deeb211d71 and again after rebasing onto main with #15243 (46fd2eab74);
    • no VUID- lines and no missing-layer messages.
  • Windows 11, NVIDIA RTX 3060 (driver 610.88, Vulkan 1.4.341, 32-lane subgroups, Float64 available), with the same three variables. VK_LOADER_DEBUG=layer confirmed that the validation layer was inserted at instance and device level.

    • with this PR (deeb211d71), 23/23 pass (the set above plus agc_driver_f16_min_max_nan), and both the f64 path and the forced integer path give the exact results;
    • on the merge base 4977f640, 11/23 pass; the 12 directed-mode cases fail with packed f16 at pc 20 in FLOAT_MODE 52/56/60 (f16 rounding not to nearest even) is not implemented;
    • no VUID- lines and no missing-layer messages.
  • Mutation check: forcing nearest-even fails with bc003c00 where bc003c01 is expected.

  • Unchanged assertion: agc_driver_packed_alu's check that a shader with packed FMA under directed rounding is refused is unchanged and still passes.

  • python3 tools/check_conventions.py --base origin/main: no errors. The one warning is about the debt entry's length, which was already over 1000 characters on main.

  • python3 tools/pr_overlap.py --branch feat/shader-pk-add-f16-directed-rounding: 35 PRs trial-merged. Two gain a conflict:

    • #15719 edits the same native condition, to add host-f16 NaNs, so the two conflict on that line. The fix is to keep both conditions.
    • #14212 rewrites the debt entry's spacing.

    #15243, #14708 and #9889 merge cleanly.

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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: NVIDIA GeForce RTX 3060, driver 610.88, Windows 11 (build 26300); Mesa lavapipe 26.0.8 (LLVM 21.1.8), Ubuntu 26.04
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5202418786 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16739). Originally posted by [AlexWhitehouse](https://github.com/AlexWhitehouse) on 2026-11-07T14:12:48Z. --- ### What `TranslationContext::packedFloat16` (`core/shader/recompiler/Translation/src/FloatInstructions.cpp`) refused every packed f16 instruction except min/max (#14793) when the `FLOAT_MODE` f16 round field is not round-to-nearest-even: ``` packed f16 at pc 20 in FLOAT_MODE 52 (f16 rounding not to nearest even) is not implemented ``` This PR runs `v_pk_add_f16` in all four rounding modes. Packed mul, FMA and the other packed arithmetic still refuse directed rounding with the same message. **Sources:** - The [RDNA2 ISA](https://gpuopen.com/download/rdna2-shader-instruction-set-architecture.pdf) §3.5, Table 4, defines the f16 round field (nearest even, +∞, −∞, zero). §12.10 defines `V_PK_ADD_F16` as two independent half additions. - LLVM [`VOP3PInstructions.td`](https://github.com/llvm/llvm-project/blob/llvmorg-19.1.0/llvm/lib/Target/AMDGPU/VOP3PInstructions.td#L95-L99) (llvmorg-19.1.0, lines 95–99) marks `V_PK_ADD_F16` with `FPDPRounding = 1`. **How it rounds.** Each half must be the exact sum rounded once. Rounding an f32 sum would round twice and lose the increment for inputs such as `1 + 2^-24`. So each half is computed as `lhs * 1 + rhs` through the existing `FPInterpolateF16` operation, which `v_interp_p2_f16` and `v_fma_mixlo/hi_f16` already use. It rounds an exact product-sum once to f16 in the selected mode, in f64 or, on a device without the Float64 capability, in 64-bit integers. The packed path's existing steps are unchanged: source selection (`op_sel`, `op_sel_hi`, `neg_lo`, `neg_hi`), the f16 denormal flags, NaN selection, the FP16 overflow clamp and the clamp modifier. A directed-rounding add doesn't take the native-f16 path, since the native instruction rounds to nearest. Nearest-even adds take the same path as before. The `v_fma_mixlo_f16` debt entry in `docs/dev/debt/shader.md`, which describes that arithmetic, now lists `v_pk_add_f16` and its sources and records that directed packed add is not measured on gfx1013. That entry was already over 1000 characters on `main`. ### Tested - **New test `agc_driver_packed_add_f16_rounding`** (`core/libs/prx/libSceAgcDriver/tests/execution/PackedAddF16Rounding.cpp`). It is registered for wave32 and wave64 × the four round modes × the f64 and integer arithmetic, 16 ctests in all, and doesn't depend on subgroup width. - **Exact results**, per lane and per mode: - `0xbc003c00 + 0x90001000` (low half `1 + 2^-11`, high half `-1 - 2^-11`) gives `0xbc003c00` at nearest-even and toward zero, `0xbc003c01` toward +∞ and `0xbc013c00` toward −∞; - `1 + 2^-24`; - exact cancellation, which gives −0 toward −∞; - overflow to ∞ or the largest finite value per mode; - subnormal results, infinities and NaNs. - **Modifiers:** `op_sel`, `neg_lo`/`neg_hi` and clamp. - **Volume:** 69,632 packed results are checked. - **Linux, lavapipe** (Mesa 26.0.8, LLVM 21.1.8, 8-lane subgroups), with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1` and `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`. The run covered the new tests plus `agc_driver_packed_alu`, `agc_driver_packed_f16_denormal_modes`, `agc_driver_float16_min_max_bits`, `agc_driver_float16_rounding` and `agc_driver_fma_mix_f16_denormal_modes`: - on `main`, 10/22 pass; the directed-mode cases fail with the message above; - with this PR, 22/22 pass, at deeb211d71 and again after rebasing onto `main` with #15243 (46fd2eab74); - no `VUID-` lines and no missing-layer messages. - **Windows 11, NVIDIA RTX 3060** (driver 610.88, Vulkan 1.4.341, 32-lane subgroups, Float64 available), with the same three variables. `VK_LOADER_DEBUG=layer` confirmed that the validation layer was inserted at instance and device level. - with this PR (deeb211d71), 23/23 pass (the set above plus `agc_driver_f16_min_max_nan`), and both the f64 path and the forced integer path give the exact results; - on the merge base 4977f640, 11/23 pass; the 12 directed-mode cases fail with `packed f16 at pc 20 in FLOAT_MODE 52/56/60 (f16 rounding not to nearest even) is not implemented`; - no `VUID-` lines and no missing-layer messages. - **Mutation check:** forcing nearest-even fails with `bc003c00` where `bc003c01` is expected. - **Unchanged assertion:** `agc_driver_packed_alu`'s check that a shader with packed FMA under directed rounding is refused is unchanged and still passes. - `python3 tools/check_conventions.py --base origin/main`: no errors. The one warning is about the debt entry's length, which was already over 1000 characters on `main`. - **`python3 tools/pr_overlap.py --branch feat/shader-pk-add-f16-directed-rounding`:** 35 PRs trial-merged. Two gain a conflict: - **#15719** edits the same `native` condition, to add host-f16 NaNs, so the two conflict on that line. The fix is to keep both conditions. - **#14212** rewrites the debt entry's spacing. #15243, #14708 and #9889 merge cleanly. ### 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 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: NVIDIA GeForce RTX 3060, driver 610.88, Windows 11 (build 26300); Mesa lavapipe 26.0.8 (LLVM 21.1.8), Ubuntu 26.04 - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
gabichuuuuu commented 2026-11-07 15:46:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-07T15:46:22Z.

Review: COMMENTED


A Windows run on NVIDIA and Intel, and two notes from reading the change.

Setup: Windows 11 x64, WinLibs GCC 15.2.0 r7 UCRT, Release. Head 46fd2eab74 merged onto main 4e44ed59b2 (clean merge). ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1, ANYPS5_GPU selecting each device (confirmed by Selected GPU). No validation layer on this machine.

GPU ctest -R agc_driver_packed_add_f16_rounding (17 tests)
NVIDIA laptop GPU (Turing), driver 610 17 of 17, in 3 of 3 runs
Intel iGPU 17 of 17, in 3 of 3 runs

None were skipped, and each run printed packed add f16 rounding: 4096 words passed.

Reading the change:

  • I found no correctness problem in the directed-rounding add. Both lowerings round the exact half sum once. The f64 one does it through RoundToPrecisionDirected, the 64-bit integer one through InterpolateInteger. I transcribed InterpolateInteger with the coordinate fixed at 1.0 and compared it with exact rounding (fractions.Fraction) over 90,483 finite f16 pairs in modes 1–3: ±0, subnormals, near-cancellations, the 65504 region and 60,000 random pairs. There were 0 differences, including x + (-x) = −0 only toward −∞.
  • docs/dev/debt/shader.md, the "Packed f16 NaN results" entry (line 157 at this head), still says "a mode with another f16 rounding throws, except in v_pk_min/max_f16". After this PR v_pk_add_f16 doesn't throw either, and the entry this PR edits (line 152) already says so.
  • The table has overflow only far past the limit (row 4, 0x7bff + 0x7bff), where every mode overflows. A translation that decided infinity with the round-to-nearest threshold (exact ≥ 65520) in every mode would still pass. A row just past 65504 tells the modes apart. In mode order nearest, +∞, −∞, zero, with the overflow clamp off, it would be: {0xfbff7bffu, 0xbc003c00u, {0xfbff7bffu, 0xfbff7c00u, 0xfc007bffu, 0xfbff7bffu}}, that is 65504 + 1 and −65504 − 1. The current code gives these values. There are also no signaling NaNs and no NaN only in rhs (0x7e01 and 0xfe42 are quiet), so the quieting and operand order of NaNs in the directed path aren't exercised.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5813361665 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16739#pullrequestreview-5813361665). Originally posted by [gabichuuuuu](https://github.com/gabichuuuuu) on 2026-11-07T15:46:22Z. **Review:** COMMENTED --- A Windows run on NVIDIA and Intel, and two notes from reading the change. **Setup:** Windows 11 x64, WinLibs GCC 15.2.0 r7 UCRT, Release. Head `46fd2eab74` merged onto `main` `4e44ed59b2` (clean merge). `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1`, `ANYPS5_GPU` selecting each device (confirmed by `Selected GPU`). No validation layer on this machine. | GPU | `ctest -R agc_driver_packed_add_f16_rounding` (17 tests) | |---|---| | NVIDIA laptop GPU (Turing), driver 610 | 17 of 17, in 3 of 3 runs | | Intel iGPU | 17 of 17, in 3 of 3 runs | None were skipped, and each run printed `packed add f16 rounding: 4096 words passed`. **Reading the change:** - I found no correctness problem in the directed-rounding add. Both lowerings round the exact half sum once. The f64 one does it through `RoundToPrecisionDirected`, the 64-bit integer one through `InterpolateInteger`. I transcribed `InterpolateInteger` with the coordinate fixed at 1.0 and compared it with exact rounding (`fractions.Fraction`) over 90,483 finite f16 pairs in modes 1–3: ±0, subnormals, near-cancellations, the 65504 region and 60,000 random pairs. There were 0 differences, including `x + (-x)` = −0 only toward −∞. - `docs/dev/debt/shader.md`, the "Packed f16 NaN results" entry (line 157 at this head), still says "a mode with another f16 rounding throws, except in `v_pk_min/max_f16`". After this PR `v_pk_add_f16` doesn't throw either, and the entry this PR edits (line 152) already says so. - The table has overflow only far past the limit (row 4, `0x7bff + 0x7bff`), where every mode overflows. A translation that decided infinity with the round-to-nearest threshold (exact ≥ 65520) in every mode would still pass. A row just past 65504 tells the modes apart. In mode order nearest, +∞, −∞, zero, with the overflow clamp off, it would be: `{0xfbff7bffu, 0xbc003c00u, {0xfbff7bffu, 0xfbff7c00u, 0xfc007bffu, 0xfbff7bffu}}`, that is 65504 + 1 and −65504 − 1. The current code gives these values. There are also no signaling NaNs and no NaN only in `rhs` (`0x7e01` and `0xfe42` are quiet), so the quieting and operand order of NaNs in the directed path aren't exercised. AI-assisted: yes.
gabichuuuuu commented 2026-11-07 20:21:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-07T20:21:40Z.

Review: COMMENTED


Rerun at the new head 9d64280025 (the commit with the overflow and NaN operand rows), merged onto current main 6b733c4ef2 (clean). Windows 11 x64, WinLibs GCC 15.2.0 r7, Release, ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 ctest -R '^agc_driver_packed_add_f16_rounding'.

runs result
NVIDIA laptop GPU, driver 610 3 all 17 pass, 3 of 3
Intel iGPU, same machine 3 all 17 pass, 3 of 3

Same as at 46fd2eab74: no failures on either GPU, now with the new rows included. Not run with VK_LAYER_KHRONOS_validation.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5814760214 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16739#pullrequestreview-5814760214). Originally posted by [gabichuuuuu](https://github.com/gabichuuuuu) on 2026-11-07T20:21:40Z. **Review:** COMMENTED --- Rerun at the new head `9d64280025` (the commit with the overflow and NaN operand rows), merged onto current `main` `6b733c4ef2` (clean). Windows 11 x64, WinLibs GCC 15.2.0 r7, Release, `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 ctest -R '^agc_driver_packed_add_f16_rounding'`. | | runs | result | |---|---|---| | NVIDIA laptop GPU, driver 610 | 3 | all 17 pass, 3 of 3 | | Intel iGPU, same machine | 3 | all 17 pass, 3 of 3 | Same as at `46fd2eab74`: no failures on either GPU, now with the new rows included. Not run with `VK_LAYER_KHRONOS_validation`. AI-assisted: yes.
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!16739
No description provided.