feat(shader): allow directed rounding modes for packed f16 add #16739
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/16739"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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 theFLOAT_MODEf16 round field is not round-to-nearest-even:This PR runs
v_pk_add_f16in all four rounding modes. Packed mul, FMA and the other packed arithmetic still refuse directed rounding with the same message.Sources:
V_PK_ADD_F16as two independent half additions.VOP3PInstructions.td(llvmorg-19.1.0, lines 95–99) marksV_PK_ADD_F16withFPDPRounding = 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 aslhs * 1 + rhsthrough the existingFPInterpolateF16operation, whichv_interp_p2_f16andv_fma_mixlo/hi_f16already 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_f16debt entry indocs/dev/debt/shader.md, which describes that arithmetic, now listsv_pk_add_f16and its sources and records that directed packed add is not measured on gfx1013. That entry was already over 1000 characters onmain.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.0xbc003c00 + 0x90001000(low half1 + 2^-11, high half-1 - 2^-11) gives0xbc003c00at nearest-even and toward zero,0xbc003c01toward +∞ and0xbc013c00toward −∞;1 + 2^-24;op_sel,neg_lo/neg_hiand clamp.Linux, lavapipe (Mesa 26.0.8, LLVM 21.1.8, 8-lane subgroups), with
ANYPS5_REQUIRE_VULKAN=1,ANYPS5_NO_SHADER_CACHE=1andVK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation. The run covered the new tests plusagc_driver_packed_alu,agc_driver_packed_f16_denormal_modes,agc_driver_float16_min_max_bits,agc_driver_float16_roundingandagc_driver_fma_mix_f16_denormal_modes:main, 10/22 pass; the directed-mode cases fail with the message above;mainwith #15243 (46fd2eab74);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=layerconfirmed that the validation layer was inserted at instance and device level.agc_driver_f16_min_max_nan), and both the f64 path and the forced integer path give the exact results;4977f640, 11/23 pass; the 12 directed-mode cases fail withpacked f16 at pc 20 in FLOAT_MODE 52/56/60 (f16 rounding not to nearest even) is not implemented;VUID-lines and no missing-layer messages.Mutation check: forcing nearest-even fails with
bc003c00wherebc003c01is 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 onmain.python3 tools/pr_overlap.py --branch feat/shader-pk-add-f16-directed-rounding: 35 PRs trial-merged. Two gain a conflict:nativecondition, to add host-f16 NaNs, so the two conflict on that line. The fix is to keep both conditions.#15243, #14708 and #9889 merge cleanly.
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
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
46fd2eab74merged ontomain4e44ed59b2(clean merge).ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1,ANYPS5_GPUselecting each device (confirmed bySelected GPU). No validation layer on this machine.ctest -R agc_driver_packed_add_f16_rounding(17 tests)None were skipped, and each run printed
packed add f16 rounding: 4096 words passed.Reading the change:
RoundToPrecisionDirected, the 64-bit integer one throughInterpolateInteger. I transcribedInterpolateIntegerwith 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, includingx + (-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 inv_pk_min/max_f16". After this PRv_pk_add_f16doesn't throw either, and the entry this PR edits (line 152) already says so.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 inrhs(0x7e01and0xfe42are quiet), so the quieting and operand order of NaNs in the directed path aren't exercised.AI-assisted: yes.
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 currentmain6b733c4ef2(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'.Same as at
46fd2eab74: no failures on either GPU, now with the new rows included. Not run withVK_LAYER_KHRONOS_validation.AI-assisted: yes.