perf(shader): compute v_pk_fma_f16 and v_pk_fmac_f16 with OpFmaKHR #4072
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4072"
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 queeeef on 2026-10-11T22:43:31Z.
What
v_pk_fma_f16andv_pk_fmac_f16use one native f16vec2 fused multiply-add when the device hasVK_KHR_shader_fmawithshaderFmaFloat16. Before, each half was converted to f32 and summed with round-to-odd (#2000).This extends #3531, which does the same for
v_pk_add_f16andv_pk_mul_f16. #3531 kept the fma on the f32 path because GLSLFmais not required to be fused, and it namedOpFmaKHR(VK_KHR_shader_fma,shaderFmaFloat16) as the way to get a guaranteed fused f16 fma. NVIDIA 610.74 on Windows exposes it, withshaderFmaFloat16,shaderFmaFloat32andshaderFmaFloat64all true on an RTX 2080 Ti.VulkanDevice: queriesVkPhysicalDeviceShaderFmaFeaturesKHRonly when the f16 path from #3531 is already enabled (Float16, RTE, preserved denormals, preserved signed zero, inf and NaN). IfshaderFmaFloat16is supported, it enables the extension and the feature and adds theFMAKHRcapability andSPV_KHR_fma. Graphics shader validation accepts both only when the device enabledshaderFmaFloat16(Context::fmaFloat16), as for the other optional extensions.FPFma16x2, and the existing NaN,FP16_OVFLand clamp rules run on the result bits unchanged.FPFma16x2is emitted asOpFmaKHRon f16vec2, decoratedNoContraction.spv::OpFmaKHRandspv::CapabilityFMAKHRcome from SPIRV-Headers, and spirv-val accepts them, from #5319 on. This PR depends on it.Why the results don't change:
OpFmaKHRas "Correctly rounded".OpFmaKHRis listed among the instructions that must respectDenormPreserve. The definition of correctly rounded appliesRoundingModeRTE, which this module declares for 16-bit floats.a * b + crounded once to f16, round to nearest even, denormals kept. That is the value the round-to-odd f32 sum produces.This PR is one commit on top of #5319.
Tested
Windows 11 Pro 26300, RTX 2080 Ti (NVIDIA 610.74), MinGW-w64 GCC 15.2.0 (WinLibs r7). Configured like the Linux CI job (
-G Ninja -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON -DANYPS5_ENABLE_SPIRV_TOOLS=ON), so spirv-val checks every module, at28cebd82bon #5319 (main0214069e3).agc_driver_packed_float16_native(#3531's test, extended). Each FLOAT_MODE compiles the test kernel three ways:OpFmaKHRwhen it hasFMAKHR;FMAKHR, which is #3531's path;Float16, the f32 path.Both native builds must give the f32 path's bits for all 8 instructions. Four of them are fma:
v_pk_fma_f16with op_sel/neg modifiers and with clamp, andv_pk_fmac_f16. The inputs are random halves weighted toward zero, denormals, infinities, NaNs and values near overflow. The test also checks thatOpFmaKHRis emitted exactly when the capability is present. Here:packed f16 native tests passed (589824 halves, fused f16 fma).Hardware oracle:
agc_driver_float16_fma_rounding, which checks against values measured on hardware, passes on theOpFmaKHRpath.The tests catch a double-rounded fma. With the emitter changed to
OpFmaKHR(round(a * b), 1.0, c), three tests fail:agc_driver_float16_fma_rounding:v_pk_fma_f16 ... is 0xf430, expected 0xf431;agc_driver_packed_alu;agc_driver_packed_float16_native.The six tests from the review (
agc_driver_packed_float16_native,agc_driver_float16_fma_rounding,agc_driver_packed_alu,agc_driver_dx10_clamp,agc_driver_dpp8_pk_fmac,agc_driver_dpp16_operands) pass on theOpFmaKHRpath with SPIRV-Tools enabled.Full suite:
ctest --timeout 120 --parallel 12 -E '^agc_driver_bda_device$': 1102 tests passed, 0 failed, 8 skipped.check_skips.py --job windowsflags one of them,guest_video_out_high_frame_rate(current display does not provide 120 Hz), a property of this monitor.agc_driver_bda_devicewas not run. On this machine an earlier run on an older base lost the device, which also resets the display.Repo checks:
check_test_registration.py,check_conventions.pyandgit diff --checkpass.Not tested: Linux, and devices without
VK_KHR_shader_fmaother than by removing the capability from the target, which the test does.Performance
Palworld (PPSA23126) title screen, idle, RTX 2080 Ti,
APS5_PROFILE_GPU, shader cache off. Four runs alternated with and without theFMAKHRcapability (a local switch, not in this PR), three 10 s windows each:OpFmaKHR(run 1 / run 2)0x1bc1…0x1a01…0x1bc1…takes about 2 ms (21%) less per frame in both pairs.0x1a01…shows no consistent change. In both shaders, most of what is left per packed instruction is the NaN rule on the result bits, as in #3531 (see #4042).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 queeeef on 2026-10-12T03:55:37Z.
Rebased onto
main34a16b7f0. #3531 is merged, so this is one commit and Depends on is none. The full Windows suite passes: 918 tests, 913 passed, 5 expected skips.The six PRs in the overlap table don't need to be dependencies. I trial-merged each with this branch. Each conflicts only on lines next to this PR's, and none of them is something this PR builds on:
docs/dev/TechnicalDebt.mdShaderValidation.cppRequirelineVulkanDevice.cppWhichever merges second needs a rebase of those lines. If one of them merges first, I'll rebase this one.
AI-assisted (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-12T13:37:13Z.
Rebase handoff — rebased onto current
main(only aTechnicalDebt.mdunion conflict; both entries kept), authorship unchanged.Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release,
BUILD_TESTING=ON, AMD RX 9070 XT / RADV):tools/check_conventions.py --base origin/mainpassesTo pick it up:
Head is
7498a8498.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-12T20:27:36Z.
Thanks for the rebase handoff. I’ve pushed a packed-f16 special-rules candidate on current upstream as
codex/f16-special-rules-4042-current. Queeeef asked that the guard also cover this PR’sOpFmaKHRpath. The candidate wraps the fast result for packed add, multiply, and FMA, and its all-lanes test compares the guarded fallback with the existing rule expansion.This overlaps the translation and SPIR-V math emitter changed here. I have not opened the PR yet. Would you prefer that I stack it on the
rebase/4072handoff branch (and mark the dependency), or keep it based on currentmainand rebase after #4072 lands? The current-main candidate builds and its four focused Linux/lavapipe tests pass; I’ll rerun after resolving the merge order.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-13T10:02:15Z.
I opened #5057 for #4042. It moves the existing packed-f16 special-value repair behind a guard, including the current f32 FMA fallback. Your PR adds the native
OpFmaKHRpath in the same translation function; that path will also need this guard so the repair stays exact without running for ordinary lanes.Could we land #5057 first, then rebase #4072 and carry the guard over its native-FMA result? If you prefer the reverse order, I can rebase #5057 after your change and add the native-FMA coverage here. I have not measured the Palworld performance change; the issue reporter offered to run that A/B.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-13T10:53:47Z.
Review: CHANGES_REQUESTED
Thanks. I ran this on Linux with RADV (Radeon 660M, Mesa exposes
VK_KHR_shader_fmawithshaderFmaFloat16 = true), configured like the Linux CI job (-DANYPS5_ENABLE_SPIRV_TOOLS=ON):agc_driver_packed_float16_native,agc_driver_float16_fma_rounding,agc_driver_packed_alu,agc_driver_dx10_clamp,agc_driver_dpp8_pk_fmac,agc_driver_dpp16_operands. All of them fail withShaderRecompiler::Recompile: SPIR-V validation before optimization failed: Invalid capability operand: 6030.3rdparty/SPIRV-Toolssubmodule (v2025.4). Like SPIRV-Headers, it predatesSPV_KHR_fma, so spirv-val rejects theFMAKHRcapability. Every shader with a packed f16 fma then fails to recompile on any device that hasshaderFmaFloat16(current RADV, NVIDIA) in a SPIRV-Tools build.VK_KHR_shader_fma, so it never takes the new path. Your Windows run doesn't enable SPIRV-Tools.3rdparty/SPIRV-Headersand3rdparty/SPIRV-Toolsto versions that knowSPV_KHR_fma(Headers 9268f3057, SPIRV-Tools 8f9381775 "spirv-val: Add validation for SPV_KHR_fma", in v2026.1 and later), ideally in a separate PR this one depends on. That also letsSpirvFma.hppand its TechnicalDebt entry go, sincespv::OpFmaKHR/spv::CapabilityFMAKHRwould exist. If the bump has to wait, don't advertiseFMAKHRwhen SPIRV-Tools is enabled.ShaderValidation.cpp:247,:295: theFMAKHRcapability andSPV_KHR_fmaare accepted whether or not the device enabledshaderFmaFloat16. The other optional extensions on thatRequireline are gated on their feature (imageInt64Atomics,descriptorIndexing, ...); please gate this one on the enabled feature too.main(ShaderValidation.cpp,docs/dev/TechnicalDebt.md). Also, #5057 changes the samepackedFloat16path and asks to land first; please agree on an order with @MarcZX100 so the guard covers theOpFmaKHRresult.Imported from boykopovar/AnyPS5. Originally posted by queeeef on 2026-10-13T17:06:30Z.
Thanks for running it on RADV with SPIRV-Tools, and sorry for the slow reply, busy day. All three points are addressed:
8f9381775, and SPIRV-Headers tob824a462d, the revision v2025.5'sDEPSpins, which contains9268f3057. This PR now builds on it:SpirvFma.hppand its technical-debt entry are gone, and the code usesspv::OpFmaKHRandspv::CapabilityFMAKHR.FMAKHRcapability andSPV_KHR_fmaare accepted only when the device enabledshaderFmaFloat16, through a newContext::fmaFloat16.main0214069e3.Tested with
-DANYPS5_ENABLE_SPIRV_TOOLS=ONon an RTX 2080 Ti (NVIDIA 610.74, which exposesshaderFmaFloat16). The six tests you listed pass on theOpFmaKHRpath, and the full suite passes: 1102 tests, 0 failed. One skip,guest_video_out_high_frame_rate, is because this monitor has no 120 Hz mode.@MarcZX100 Fine with #5057 landing first. I'll rebase this on it and put the native fma result behind the guard as well. The A/B for #5057 is on that PR.
AI-assisted (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-13T22:22:46Z.
Thanks for the update. I checked the current diff: it now gates
FMAKHRcapability/extension acceptance on the enabledshaderFmaFloat16feature, and the dependency update is present with #5319 listed. That addresses those two review points. The packed native-FMA result is still unguarded in this head; your plan to carry #5057’s special-value guard after #5057 lands is the remaining correctness item. Since #5057 is also a prerequisite for that rebase/order, please include it on theDepends online while it is open. After #5319 and #5057 land, rebase this branch on currentmain, carry the guard through the nativeOpFmaKHRpath, and rerun the focused tests. I will leave review pending until that head is available.Imported from boykopovar/AnyPS5. Originally posted by CPritch on 2026-10-21T07:54:09Z.
I ran this change on NVIDIA, where
VK_KHR_shader_fma(revision 1) is exposed withshaderFmaFloat16 = true: RTX 5060 Laptop GPU, driver 615.71.09, Linux 7.2.8. #3531's conditions hold too (shaderFloat16,shaderRoundingModeRTEFloat16,shaderDenormPreserveFloat16,shaderSignedZeroInfNanPreserveFloat16).28cebd82bno longer applies to main. Cherry-picked onto2fee39f1bit conflicts in 10 files, each on neighbouring lines that main added (the per-lane source cache and!flushResultsin FloatInstructions.cpp, new members and setters, newValidateShadersandInspectparameters). I have a resolution that keeps main's code and adds this PR's lines; it cherry-picks without conflicts ontoe25333e39(current main as of 2026-10-20 22:26 UTC). I can share it as a branch or a patch if you want it.The PR also needs #5319's submodule pins, which main no longer has:
1db31b9c9(#6134, merged 2026-10-18) moved SPIRV-Headers back to 01e057791 and SPIRV-Tools back to 7f2d9ee92 (v2025.4). Without #5319's pins the build fails with'CapabilityFMAKHR' is not a member of 'spv'. So both sides below aree25333e39+ #5319's commit (the two pins only); B adds the resolution.Tests, built with
-DBUILD_TESTING=ON -DANYPS5_ENABLE_SPIRV_TOOLS=ONand run with the validation layer andANYPS5_NO_SHADER_CACHE=1: the six from adriadam10's review, every test with f16, float16, fp16 or half in its name, and agc_fma_mix_once, 41 in all. All 41 pass on both sides with no validation messages. agc_driver_packed_float16_native prints "packed f16 native tests passed (294912 halves)" without this PR and "packed f16 native tests passed (589824 halves, fused f16 fma)" with it.GTA V (PPSA04263 01.010.002), Story Mode after Resume Story (the bank), Graphics Mode Performance, 3840x2160. Each side's caches were warmed until its last 60 s built no pipelines. The window is 60 s (
APS5_TRACE_FRAMES=3600:700,APS5_TRACE_EVENTS=4194304, no other trace or profile switch), the runs went in the order A B A B, no window had a pipeline build, the allocator was glibc and the laptop was on AC.p50 is 7.0 ms lower (-8.8 %), against a spread of 0.6 ms on each side, and GPU time per present is 6.8 ms lower. With
APS5_PROFILE_GPU=1(one run per side, not timing data), the largest dispatch program, 0x25b340600, goes from 19.42 to 13.19 ms per present.Summary lines (A, A, B, B)
The summary lines' Driver.EndSubmission (about 2,700 to 2,900 ms per frame) and worker_busbout 3,700 %) are not costs: on the pipelined path the Driver.EndSubmission timer staysopen through the rewind tail, so nested tails are counted many times over (#8092, fixed on main by #8108).
Not tested: other GPUs and drivers (RADV included), Windows, other titles and scenes.
AI-assisted: yes
Pull request closed