perf(shader): emit exact f32 add, subtract and multiply as fma on MoltenVK #8612
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8612"
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 imellag on 2026-10-20T18:24:05Z.
What
The recompiler marks every guest f32 add, subtract and multiply
NoContraction, because the guest rounds each one separately. SPIRV-Cross turns each such instruction into a call to a helper that Metal compiles with[[clang::optnone]], so on MoltenVK those calls dominate fragment time, and its multiply helper,fma(a, b, +0.0), also turns a -0.0 product into +0.0.On MoltenVK the three operations are now GLSL
Fmawhose second step is exact:a * bisfma(a, b, -0.0),a + bisfma(1.0, a, b)anda - bisfma(-1.0, b, a). Multiplying by ±1 and adding -0.0 are exact, so each form gives the separately rounded IEEE result, including the sign of a zero, whether the driver fusesFmaor not (Vulkan allows either). NaN results are still chosen on the bits (#2083). The sin, cos and log2 series (SpirvAluEmitterMath.cpp) and the offset image coordinates and gather texel scale (SpirvImageEmitter.cpp) use the same forms. The choice is aSpirvTargetflag,fusedExactArithmetic, set forVK_DRIVER_ID_MOLTENVKand part of the shader cache key and of the request serialization (version 42). Other drivers are unchanged.This is the inline fma of #7332 (
a243f0c93by @ycarogabriel-lgtm), split out as asked in the #7332 review, ported to currentmain, with the series and image follow-up posted there. The cost was first reported on #1.Tests:
agc_driver_f32_separate_rounding: a multiply feeding an add, a subtract and a reverse subtract, 8 results × 32 rows, with no float mode and IEEE=1. In 16 of the rows, fusing the multiply into the following add changes the result, so the test fails if a driver contracts the fused forms.agc_driver_f32_arithmetic_nan(rows measured on RDNA2 in #2083),agc_driver_f32_denormal_flushand the new test also run the fused forms on devices that do not use them, so lavapipe in CI checks them against the same rows. A device that uses the fused forms runs only those.agc_shader_memorygains the recompiler check from #7332 (with the flag, the f32 multiply, add and subtract becomeFmaand loseNoContraction); it andrequest_serializercover version 43 (mainuses 42 for the pixel sample order).The serialization version, the target flag and the
ShaderMemory.cppserialization checks share lines with #6639, #6801, #6879, #7888 and #8004, and the same files with #7410, #8489 and #8564, which conflict withmainnow; none of them changes the same arithmetic. #8658 changesEmitFPMul32on Apple hosts tofma(a, b, -0.0)and keepsNoContraction; with this PR, MoltenVK already gets that multiply without the decoration, so whichever lands second drops its version of that function.Tested
Apple M4 Pro, macOS 27.0.1, x86-64 under Rosetta 2, MoltenVK 1.4.2 (Vulkan SDK 1.4.363.0), Apple clang 21.0.0.
mainc188e01a42, where version 43 went to the vertex point fields: the flag is now written after them, as version 44. Linux x86-64 (Ubuntu 24.04 amd64 container on the same Mac, GCC 14.2, lavapipe from Mesa 25.2.8, validation layer 1.3.275,ANYPS5_REQUIRE_VULKAN=1,ANYPS5_NO_SHADER_CACHE=1):request_serializer,agc_shader_memory,agc_shader_control_flow(and itsiteration_selection_offandforced_fallbackvariants),agc_driver_graphics,agc_driver_depth_bounds_stateandagc_driver_pixel_barycentric_emulationpass onmainand on this branch, 8/8 each; both logs have the same singleVUID-VkDeviceCreateInfo-pNext-pNextline, frommain.main27bcd6a29(serialization version 43). Linux x86-64 (Ubuntu 24.04 amd64 container on the same Mac, GCC 14.2, lavapipe,ANYPS5_REQUIRE_VULKAN=1):request_serializer,agc_shader_memory,agc_driver_f32_separate_rounding,agc_driver_f32_arithmetic_nanandagc_driver_f32_denormal_flushpass. Negative control: reading the flag from version 42 makesrequest_serializerfail.mainb77bd5880:request_serializerpasses.main's libraries do not build on macOS, soagc_shader_memoryran onb77bd5880with a local branch that adds #4627, #8279 and three libc files andGuestMemory.cppfrom mugurc'sci/macos-series-2only to build libc: passes. Negative controls: without the fused multiply the new recompiler check fails; without writing the flag both tests fail.ci/macos-series-253db0badb (plus #7937's code commits, needed to build libkernel, and #8477), with this change ported except the serialization part (the series has an older request layout). AGC and shader suites on MoltenVK: 761/807 without the change, 775/807 with it. Fixed:dpp16_operands,f16_conversion_float_mode,f16_fma_denormal_modes,f32_output_modifier,float16_fma_rounding,float16_misc,float16_modifiers,float16_rounding,ldexp_f16_inline_constants,packed_alu,packed_f16_denormal_modes,sdwa_float_selectors,sdwa_ldexp_f16,vop2_modifier_forms; none broken. With the test changes there,f32_separate_roundingandf32_arithmetic_nanpass.f32_denormal_flushstill fails: without the change at lane 0 (v_mul_f32 v25, |v4|, -v5gives0x00000000, expected0x80000000, the lost -0.0), with it at lane 8 (v_fma_f32 v14, v4, v5, v6gives0x00000000, expected0x00800000): the guest fma, which this change does not touch. On an Apple M1 (macOS 27.0.1, MoltenVK 1.4.2), @Mauro42K ran the same port on top of #8578 (results): the same 14 tests fixed and none broken;f32_separate_roundingandf32_arithmetic_nanpass andf32_denormal_flushstops at the same lane 8.APS5_TRACE_SECONDS=140:5only, base and change alternated twice in one session. The base is the macOS integration head7cf8c610c(former #7332, which already hasa243f0c93) with the flag forced off, which is whatmainemits; the change is7cf8c610cwith the flag on plus the follow-up. Titles do not run frommainon macOS. The GPU time per frame falls by more than half and the frame rate rises by about 80%:Summary lines
God of War: Sons of Sparta (PPSA28997), a new game on Cadet with the dialogue advanced from a pad input file (a cross every 3 s from 46 to 106 s), so that the window at 130 s is the first Cyclops fight (checked with frame dumps), 1920x1080, warm caches,
APS5_TRACE_SECONDS=130:10only, the same two commits alternated twice in one session, the change first. The GPU time per frame falls from 64 to 27 ms and the frame rate rises from 15 to about 34.5 fps (+130%):Summary lines
In both change captures the queue worker's trace buffer filled after 8.5 and 8.8 s (
trace_dropped);totals_fullis false, so the summary numbers are complete and only the trace files lack that worker's last slices.Each trace is linked in its table. On the #7332 stack, menu and gameplay frames showed no artifacts with the fused forms, and frame dumps with and without the follow-up were the same apart from timing. Linux and Windows were not run; with other drivers the flag is off outside the three tests that force it.
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)Imported from boykopovar/AnyPS5. Originally posted by imellag on 2026-10-20T18:25:53Z.
Traces of the eight captures in the table above, in the same order (each file name matches the
tracefield of its summary line):1-base-1-aps5-trace-e3a7f5487a1b-9475.json.gz
2-change-1-aps5-trace-0d228a554942-11144.json.gz
3-base-2-aps5-trace-e3a7f5487a1b-12908.json.gz
4-change-2-aps5-trace-0d228a554942-14662.json.gz
5-gow-change-1-aps5-trace-0d228a554942-56608.json.gz
6-gow-base-1-aps5-trace-e3a7f5487a1b-59159.json.gz
7-gow-change-2-aps5-trace-0d228a554942-61538.json.gz
8-gow-base-2-aps5-trace-e3a7f5487a1b-64156.json.gz
They open in ui.perfetto.dev;
python3 tools/trace_digest.py <trace>gives the per-thread table the presenter wait comes from.Imported from boykopovar/AnyPS5. Originally posted by Mauro42K on 2026-10-21T00:19:50Z.
Compared the reference-profile results on a MacBook Pro (Apple M1, macOS 27.0.1), LunarG Vulkan SDK 1.4.363.0, MoltenVK 1.4.2. Both revisions used the filtered
^(agc|shader)CTest suite with-j2.a4a16a6f860afcdc5c2fbbd3d48c3701c33fbc27(baseline + #8578): 808 tests, 687 passed, 73 skipped, 48 failed/aborted.42c3fd09a2b67829df8491b5d478444b26b02b52(integration test commit with both PR changes): 809 tests, 702 passed, 73 skipped, 34 failed/aborted.The 14 reference-profile tests that changed from failed to passed were:
agc_driver_dpp16_operandsagc_driver_f16_conversion_float_modeagc_driver_f16_fma_denormal_modesagc_driver_f32_output_modifieragc_driver_float16_fma_roundingagc_driver_float16_miscagc_driver_float16_modifiersagc_driver_float16_roundingagc_driver_ldexp_f16_inline_constantsagc_driver_packed_aluagc_driver_packed_f16_denormal_modesagc_driver_sdwa_float_selectorsagc_driver_sdwa_ldexp_f16agc_driver_vop2_modifier_formsAt
42c3fd09a:agc_driver_f32_separate_rounding: passed.agc_driver_f32_arithmetic_nan: passed.agc_driver_f32_denormal_flush: failed at lane 8. First relevant output:AGC graphics: f32 denormal flush: fused lane 8 no float mode v_fma_f32 v14, v4, v5, v6 is 0x00000000, expected 0x00800000.[float-controls]lines in the captured run reporthardware (0 of 5472 probe results differ from the emulated flush).This lane-8 result matches the M4 Pro result reported in the coordination thread. The M4 Pro reference run reported 46, 46, and 32 failures at the corresponding three revisions; the M1 reference runs reported 51, 48, and 34. These are comparisons of the reported reference-profile totals, not validation-profile totals.
The validation profile is complementary evidence: failures decreased from 64 at
a4a16a6f8to 48 at42c3fd09a(16 tests changed to passing, with no new failures). It recorded no new VUID identifiers. The same 12 identifiers appeared at both revisions;VUID-VkImageViewCreateInfo-imageViewFormatReinterpretation-04466increased from 86 to 90 occurrences, while the other identifiers’ counts were unchanged.42c3fd09ais the integration commit prepared for this test series, not the final commit of PR #8612.Reference failures at a4a16a6f8 (48)
agc_driver_compute_fs_table_callsagc_driver_dpp16_operandsagc_driver_f16_conversion_float_modeagc_driver_f16_fma_denormal_modesagc_driver_f32_constant_factor_tininessagc_driver_f32_denormal_flushagc_driver_f32_output_modifieragc_driver_f64_add_mul_denormal_modesagc_driver_f64_rounding_denormal_modesagc_driver_float16_fma_roundingagc_driver_float16_miscagc_driver_float16_modifiersagc_driver_float16_roundingagc_driver_guest_memory_lifetimeagc_driver_guest_memory_lifetime_copyagc_driver_guest_memory_lifetime_directagc_driver_guest_memory_lifetime_heapagc_driver_guest_memory_lifetime_heap_uncachedagc_driver_guest_memory_lifetime_lockedagc_driver_image_cache_bitsagc_driver_image_plane_bias_on_single_level_volumeagc_driver_image_srgb_sample_copyagc_driver_image_srgb_sample_copy_forcedagc_driver_image_table_reuseagc_driver_image_texel_statusagc_driver_indirect_arguments_pending_produceragc_driver_ldexp_f16_inline_constantsagc_driver_multisample_retirementagc_driver_packed_aluagc_driver_packed_f16_denormal_modesagc_driver_pass_input_hazardagc_driver_pixel_interlockagc_driver_pixel_sample_coverageagc_driver_pixel_sample_iterationagc_driver_rect_list_indirectagc_driver_sample_mask_multisampleagc_driver_sdwa_float_selectorsagc_driver_sdwa_ldexp_f16agc_driver_stencil_ref_export_unusedagc_driver_tessellationagc_driver_uint8x4_color_targetagc_driver_vertex_clip_cull_distancesagc_driver_vertex_clip_cull_drawagc_driver_vertex_clip_cull_draw_no_keyagc_driver_vertex_user_data_tailagc_driver_vop2_modifier_formsagc_performance_overlay_statisticsagc_write_trackingReference failures at 42c3fd09a (34)
agc_driver_compute_fs_table_callsagc_driver_f32_constant_factor_tininessagc_driver_f32_denormal_flushagc_driver_f64_add_mul_denormal_modesagc_driver_f64_rounding_denormal_modesagc_driver_guest_memory_lifetimeagc_driver_guest_memory_lifetime_copyagc_driver_guest_memory_lifetime_directagc_driver_guest_memory_lifetime_heapagc_driver_guest_memory_lifetime_heap_uncachedagc_driver_guest_memory_lifetime_lockedagc_driver_image_cache_bitsagc_driver_image_plane_bias_on_single_level_volumeagc_driver_image_srgb_sample_copyagc_driver_image_srgb_sample_copy_forcedagc_driver_image_table_reuseagc_driver_image_texel_statusagc_driver_indirect_arguments_pending_produceragc_driver_multisample_retirementagc_driver_pass_input_hazardagc_driver_pixel_interlockagc_driver_pixel_sample_coverageagc_driver_pixel_sample_iterationagc_driver_rect_list_indirectagc_driver_sample_mask_multisampleagc_driver_stencil_ref_export_unusedagc_driver_tessellationagc_driver_uint8x4_color_targetagc_driver_vertex_clip_cull_distancesagc_driver_vertex_clip_cull_drawagc_driver_vertex_clip_cull_draw_no_keyagc_driver_vertex_user_data_tailagc_performance_overlay_statisticsagc_write_trackingTests were run with assistance from OpenAI Codex; results and conclusions were checked against the captured local logs. These test results do not measure game performance.
Imported from boykopovar/AnyPS5. Originally posted by krishalivaibhav on 2026-10-21T07:57:17Z.
M2 follow-up to the M1/M4 reports: I tested the published integration comparison on Apple M2 (16 GB), macOS 27.2, x86-64 under Rosetta 2, MoltenVK 1.4.2 / Vulkan SDK 1.4.363.0, Release. These are community macOS integration revisions, not standalone current
main.Focused correctness comparison
Baseline
a4a16a6f860a(includes #8578) vs candidate42c3fd09a2b6(integration port of this PR):agc_float32_flush_controlagc_driver_f32_arithmetic_nanagc_driver_f32_denormal_flushagc_driver_f32_separate_roundingBoth used
ANYPS5_REQUIRE_VULKAN=1,ANYPS5_NO_SHADER_CACHE=1, andVK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, with verbose CTest logs. Loader diagnostics confirm the layer actually loaded; neither log contains the missing-layer warning or anyVUID-line. This is a focused comparison, not a full-suite claim.The flush probe reports
hardware (0 of 5472 probe results differ from the emulated flush)in both. First failing lines:Preliminary Balatro observation — PPSA21402
At 1920×1080, the candidate reached roughly 49–50 FPS in the menu and about 30 FPS during active tutorial play; some tutorial sections remained 18–20 FPS. Baseline gameplay was about 15–20 FPS. The user observed a brief startup hitch in the candidate, without a gameplay crash in this bounded run.
tools/trace_digest.pyon the captures reports candidate active-play intervals of 165 and 153 presents per five seconds (33.0 and 30.6/sec), with 20.9 and 22.1 ms GPU busy per present. The baseline's late intervals average about 17.7 presents/sec, with roughly 55–57 ms GPU busy per present.Limits: one manual run per revision, unequal run lengths/progression timings and separately warming caches. These are not fixed-scene, warm-cache benchmark results, so I am not claiming a controlled speedup multiplier or sustained 60 FPS. Both runtime packages carried the same local bounded cache of generated barycentric tessellation stages; neither used our earlier private MSL hook. The focused correctness tests above used the clean integration revisions, before applying that common runtime cache. Two packaging attempts were excluded; the valid captures identify the intended commits.
Next: repeat warm-cache, same-scene measurements and investigate the remaining slow tutorial sections. This supports testing this existing PR rather than opening a duplicate arithmetic implementation.
AI-assisted: yes; human gameplay observations are distinguished from trace measurements above.
Imported from boykopovar/AnyPS5. Originally posted by imellag on 2026-10-21T13:34:15Z.
Thanks @Mauro42K and @krishalivaibhav for the M1 and M2 runs.
Both match the M4 Pro results. On the M1 the same 14 tests change from failing to passing and nothing new fails. On both chips
agc_driver_f32_denormal_flushgets past the signed-zero multiply at lane 0 and then stops at fused lane 8 (v_fma_f32). This PR does not change lane 8:v_fma_f32is already an fma, and that lane checks the guest fma's tininess, which is a separate gap.The Balatro numbers are a useful third title next to Planet of Lana and God of War: same direction, about 55 ms of GPU time per present down to about 21 ms in active play.
The branch conflicted with
mainagain, so I rebased it ontomain27bcd6a29(now 5e58f2e8a).maintook serialization version 42 for the pixel sample order, so the fused arithmetic flag moves to version 43; the code is otherwise unchanged. On Linux (GCC 14.2, lavapipe,ANYPS5_REQUIRE_VULKAN=1),request_serializer,agc_shader_memory,agc_driver_f32_separate_rounding,agc_driver_f32_arithmetic_nanandagc_driver_f32_denormal_flushpass, and reading the flag from version 42 makesrequest_serializerfail. The description now also lists #8658, which changesEmitFPMul32on Apple hosts.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by imellag on 2026-10-22T10:58:14Z.
Numbers on current
mainwith the macOS port, now that titles run there. The measurements in the description were on the closed #7332 stack.Setup: Apple M4 Pro, macOS 27.0.1, MoltenVK 1.4.2 (Vulkan SDK 1.4.363.0), x86-64 Release under Rosetta 2.
maincecb540a63+ #8658 + #9193 + #9263 (commitc66b8a2377in the summaries).68e5a0be30).APS5_TRACE_SUMMARY_EVERY, no other trace or profile switch. Arms alternated, scripted input, same scene every run.mainstackWith this PR, Lana is bound by the queue worker instead of the GPU (busiest worker 51% → 72%).
tools/trace_digest.pyrows from one run of each arm:Lana,
mainstack (c66b8a2377):Lana, + this PR (
68e5a0be30):God of War,
mainstack:God of War, + this PR:
(columns: interval, seconds, presents, frame p50, p90, p99, max in ms, GPU busy per present in ms, busiest worker, largest phase)
AI-assisted: yes (Claude Code). The runs are from my M4 Pro.