fix(shader): honor directed f16 rounding in f16 conversions and ldexp #3012

Merged
CouchRiv merged 2 commits from upstream-pr/3012 into main 2026-10-11 06:35:15 +00:00
CouchRiv commented 2026-10-10 20:28:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CouchRiv on 2026-10-10T20:28:59Z.


What

AGC graphics: directed f16 rounding: mode 1 overflow 0 lane 2 column 1 is 0x5a5af800, expected 0x5a5af7ff

v_cvt_f16_f32, v_cvt_f16_i16, v_cvt_f16_u16 and v_ldexp_f16 ignored directed FLOAT_MODE rounding and always rounded to nearest even. On RDNA2 (gfx1036), 0 of 7,120 outputs depend on round32 (bits 1:0); the f16 result follows round16 (bits 3:2). LLVM AMDGPU SIModeRegister.cpp:247-251 and SIDefines.h:1502 put fptrunc.round f32→f16 in FP_ROUND_MODE_DP (bits 3:2).

The directed path now reads bits 3:2 and runs only when f16 denormals are preserved (bits 7:6 = 3). With f16 denormals flushed, behaviour stays as on main (nearest even); that combination is recorded in TechnicalDebt.

Shares the test file Float16Rounding.cpp with #2987; I rebase whichever lands second.

Tested

CachyOS Linux, RTX 4070 Ti (NVIDIA) and Radeon 610M (RADV gfx1036), lavapipe (Mesa llvmpipe).

  • agc_driver_float16_rounding passes on NVIDIA, RADV and lavapipe (each under 1 s).
  • Without the Translation change, the same test fails with the line above.
  • Sweep replay vs gfx1036: 281 rows fixed per driver (v_cvt_f16_f32 49, v_cvt_f16_i16 6, v_cvt_f16_u16 19, v_ldexp_f16 207), 0 regressions; round32-only followups unchanged (0 wrong on main and on this change).
  • python3 tools/check_conventions.py --base origin/main, git diff --check.

Not tested: denorm16 modes 1 and 2, clamp/omod/SDWA with directed rounding, gfx1013.

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:4814223310 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3012). Originally posted by [CouchRiv](https://github.com/CouchRiv) on 2026-10-10T20:28:59Z. --- ### What ``` AGC graphics: directed f16 rounding: mode 1 overflow 0 lane 2 column 1 is 0x5a5af800, expected 0x5a5af7ff ``` `v_cvt_f16_f32`, `v_cvt_f16_i16`, `v_cvt_f16_u16` and `v_ldexp_f16` ignored directed `FLOAT_MODE` rounding and always rounded to nearest even. On RDNA2 (gfx1036), 0 of 7,120 outputs depend on round32 (bits 1:0); the f16 result follows round16 (bits 3:2). LLVM AMDGPU `SIModeRegister.cpp:247-251` and `SIDefines.h:1502` put `fptrunc.round` f32→f16 in `FP_ROUND_MODE_DP` (bits 3:2). The directed path now reads bits 3:2 and runs only when f16 denormals are preserved (bits 7:6 = 3). With f16 denormals flushed, behaviour stays as on `main` (nearest even); that combination is recorded in TechnicalDebt. Shares the test file `Float16Rounding.cpp` with #2987; I rebase whichever lands second. ### Tested CachyOS Linux, RTX 4070 Ti (NVIDIA) and Radeon 610M (RADV gfx1036), lavapipe (Mesa llvmpipe). - `agc_driver_float16_rounding` passes on NVIDIA, RADV and lavapipe (each under 1 s). - Without the Translation change, the same test fails with the line above. - Sweep replay vs gfx1036: 281 rows fixed per driver (`v_cvt_f16_f32` 49, `v_cvt_f16_i16` 6, `v_cvt_f16_u16` 19, `v_ldexp_f16` 207), 0 regressions; round32-only followups unchanged (0 wrong on main and on this change). - `python3 tools/check_conventions.py --base origin/main`, `git diff --check`. Not tested: denorm16 modes 1 and 2, `clamp`/`omod`/SDWA with directed rounding, gfx1013. ### 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: none - [x] AI-assisted: yes
ArjenS-LA commented 2026-10-10 21:37:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ArjenS-LA on 2026-10-10T21:37:33Z.


Checked 95152666 against #3040 at d9b22a93 using git merge-tree. The overlap includes a content conflict in FloatInstructions.cpp’s vLdexpF16, alongside Float16Rounding.cpp.

When rebasing whichever lands second, the resolution needs to retain this PR’s convertF32ToF16Bits call together with #3040’s signed-zero input/output denormal flushing. Taking either side of the conflicting result-conversion block wholesale would lose part of the other fix.

Could the rebased version rerun both the directed-rounding and ldexp-denormal cases? This is an integration check; I haven’t independently reproduced the GPU results.

Review performed with Codex assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6102439854 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3012#issuecomment-6102439854). Originally posted by [ArjenS-LA](https://github.com/ArjenS-LA) on 2026-10-10T21:37:33Z. --- Checked `95152666` against \#3040 at `d9b22a93` using `git merge-tree`. The overlap includes a content conflict in `FloatInstructions.cpp`’s `vLdexpF16`, alongside `Float16Rounding.cpp`. When rebasing whichever lands second, the resolution needs to retain this PR’s `convertF32ToF16Bits` call together with \#3040’s signed-zero input/output denormal flushing. Taking either side of the conflicting result-conversion block wholesale would lose part of the other fix. Could the rebased version rerun both the directed-rounding and ldexp-denormal cases? This is an integration check; I haven’t independently reproduced the GPU results. Review performed with Codex assistance.
Dextroid17 commented 2026-10-10 23:53:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-10T23:53:33Z.

Review: COMMENTED


The citation is what makes this land: LLVM's SIModeRegister.cpp and SIDefines.h put fptrunc.round for f32→f16 in FP_ROUND_MODE_DP, bits 3:2 — so the directed path belongs there rather than on round32, and saying so turns the finding from a guess into a reference. Going the extra step and disproving the alternative — 0 of 7,120 outputs depending on bits 1:0 on gfx1036 — is what closes it.

The scoping is honest and worth keeping. Running the directed path only when f16 denormals are preserved, and leaving the flushed combination as it is on main, is a declared boundary rather than a silent one, and the entry carries it. It also connects to its siblings the way these should: Float16Rounding.cpp is shared with #2987 and #3040 — three open PRs on one test file — and the intent to rebase whichever lands second is exactly the discipline this series needed. With three parties rather than two, the order is worth naming explicitly once the first of them merges.

The verification is the strongest part. Three stacks — NVIDIA, RADV on gfx1036, and lavapipe — with the same test failing when the Translation change is reverted, plus a sweep replay fixing 281 rows per driver with a per-function breakdown. The negative control is what makes the other numbers mean something.

One question about the boundary. The directed path is gated on bits 7:6 being 3. If that is because the hardware needs denormals preserved for directed rounding to be observable at all, this is complete; if it is because the sweep happened to run in that mode, a single flushed-mode row would settle it the way the 7,120-row sweep settled bits 1:0.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5481241522 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3012#pullrequestreview-5481241522). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-10T23:53:33Z. **Review:** COMMENTED --- The citation is what makes this land: LLVM's `SIModeRegister.cpp` and `SIDefines.h` put `fptrunc.round` for f32→f16 in `FP_ROUND_MODE_DP`, bits 3:2 — so the directed path belongs there rather than on `round32`, and saying so turns the finding from a guess into a reference. Going the extra step and *disproving* the alternative — 0 of 7,120 outputs depending on bits 1:0 on gfx1036 — is what closes it. **The scoping is honest and worth keeping.** Running the directed path only when f16 denormals are preserved, and leaving the flushed combination as it is on `main`, is a declared boundary rather than a silent one, and the entry carries it. It also connects to its siblings the way these should: `Float16Rounding.cpp` is shared with #2987 **and** #3040 — three open PRs on one test file — and the intent to rebase whichever lands second is exactly the discipline this series needed. With three parties rather than two, the order is worth naming explicitly once the first of them merges. **The verification is the strongest part.** Three stacks — NVIDIA, RADV on gfx1036, and lavapipe — with the same test failing when the Translation change is reverted, plus a sweep replay fixing 281 rows per driver with a per-function breakdown. The negative control is what makes the other numbers mean something. **One question about the boundary.** The directed path is gated on bits 7:6 being 3. If that is because the hardware needs denormals preserved for directed rounding to be observable at all, this is complete; if it is because the sweep happened to run in that mode, a single flushed-mode row would settle it the way the 7,120-row sweep settled bits 1:0. Written with AI assistance.
oneandonlydean commented 2026-10-11 01:08:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-11T01:08:31Z.


#2987 is in, so this conflicts in Float16Rounding.cpp; please rebase. Whichever of this and #3040 goes second also needs to keep both vLdexpF16 changes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6104054085 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3012#issuecomment-6104054085). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-11T01:08:31Z. --- #2987 is in, so this conflicts in `Float16Rounding.cpp`; please rebase. Whichever of this and #3040 goes second also needs to keep both `vLdexpF16` changes.
Silverash-i8 commented 2026-10-11 02:16:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Silverash-i8 on 2026-10-11T02:16:25Z.


Windows run of the rebased head 7c4f9381, since the PR has no Windows results: Windows 11, WinLibs GCC 15.2.0 r7, Release, merged onto main 84cc975e, RTX 5060 Laptop and the Arrow Lake iGPU (ANYPS5_GPU=Intel).

  • agc_driver_float16_rounding passes on both GPUs, and so do the 6 other f16 and half tests I ran with it.

AI-assisted (Claude Code); I ran this on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6104535931 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3012#issuecomment-6104535931). Originally posted by [Silverash-i8](https://github.com/Silverash-i8) on 2026-10-11T02:16:25Z. --- Windows run of the rebased head `7c4f9381`, since the PR has no Windows results: Windows 11, WinLibs GCC 15.2.0 r7, Release, merged onto `main` 84cc975e, RTX 5060 Laptop and the Arrow Lake iGPU (`ANYPS5_GPU=Intel`). - `agc_driver_float16_rounding` passes on both GPUs, and so do the 6 other f16 and half tests I ran with it. AI-assisted (Claude Code); I ran this on my machine.
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!3012
No description provided.