Repository navigation
perf(shader): emit exact f32 add, subtract and multiply as fma on MoltenVK - #8612
Conversation
|
Traces of the eight captures in the table above, in the same order (each file name matches the 1-base-1-aps5-trace-e3a7f5487a1b-9475.json.gz 5-gow-change-1-aps5-trace-0d228a554942-56608.json.gz They open in ui.perfetto.dev; |
|
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
|
|
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
The 14 reference-profile tests that changed from failed to passed were:
At
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
Reference failures at a4a16a6 (48)
Reference failures at 42c3fd0 (34)
Tests 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. |
d9e4eed to
a87c0fc
Compare
|
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 Focused correctness comparisonBaseline
Both used The flush probe reports Preliminary Balatro observation — PPSA21402At 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.
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. |
a87c0fc to
5e58f2e
Compare
|
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 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 AI-assisted: yes |
|
Numbers on current Setup: Apple M4 Pro, macOS 27.0.1, MoltenVK 1.4.2 (Vulkan SDK 1.4.363.0), x86-64 Release under Rosetta 2.
With this PR, Lana is bound by the queue worker instead of the GPU (busiest worker 51% → 72%).
Lana, Lana, + this PR ( God of War, 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. |
…tenVK The recompiler marks every guest f32 add, subtract and multiply NoContraction, because the guest rounds each one separately. SPIRV-Cross turns each of them into a call to a helper that Metal compiles without optimization, which dominates fragment time on MoltenVK, and its multiply helper, fma(a, b, +0.0), turns a -0.0 product into +0.0. On MoltenVK they are now GLSL Fma whose second step is exact, which gives the separately rounded result: a * b is fma(a, b, -0.0), a + b is fma(1.0, a, b) and a - b is fma(-1.0, b, a). The sin, cos and log2 series and the offset image coordinates and gather texel scale use the same forms. The choice is a SpirvTarget flag set for VK_DRIVER_ID_MOLTENVK, part of the shader cache key and of the request serialization (version 44); other drivers are unchanged. agc_driver_f32_separate_rounding checks a multiply feeding an add or a subtract on rows where fusing them changes the result. It, agc_driver_f32_arithmetic_nan and agc_driver_f32_denormal_flush also run the fused forms on devices that do not use them, so lavapipe checks them against the same rows. Co-authored-by: Ycaro Gabriel <ycaro.gabriel@performait.com>
5e58f2e to
7410c37
Compare
|
Rechecked after the rebase onto current main: serialization version 44 is written and read consistently with the fused-arithmetic flag, the version 44/45 prefixes and the older-version fixtures account for the extra byte, and the fma forms and cache key are unchanged from what I checked before. MoltenVK numbers are the author's, with M1 and M2 confirmations; merged. |
The DB clamps a pixel shader's exported depth to PA_SC_VPORT_ZMIN_0 and ZMAX_0 unless DB_RENDER_OVERRIDE.DISABLE_VIEWPORT_CLAMP is set. Vulkan only clamps with depthClamp, which also drops clipping, so the exported value reached the depth attachment unclamped. The pixel stage info now carries the two bounds when the shader exports depth (0 and 1 otherwise), and the exported value is NClamped to them before it is stored to gl_FragDepth. With DISABLE_VIEWPORT_CLAMP the value is stored as exported; non-finite or inverted bounds are rejected. DB_RENDER_OVERRIDE joins the draw key registers. The bounds are part of the recompile cache key (only for depth-exporting shaders, so other keys do not change), the draw key and its register check, and recompile requests (serialization version 47). Tests: the pixel stage decode reads the bounds, passes the disable flag through and rejects bad bounds, and leaves a draw without depth export at 0 and 1; a recompiled depth export stores the NClamped value, and its bounds survive serialization and key the cache. Request version 47: main is at 44 (#8612) and open PRs also claim 44 to 46 (#8564, #7888, #9193 and the #9020, #9335, #9748 stack). Whichever of these lands second renumbers.
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 (a243f0c by @ycarogabriel-lgtm), split out as asked in the #7332 review, ported to current
main, 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 feat(shader): choose f32 arithmetic NaN results on the bits #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 feat: macOS on Apple Silicon host support (Rosetta 2 and MoltenVK) #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.main27bcd6a (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.mainb77bd58:request_serializerpasses.main's libraries do not build on macOS, soagc_shader_memoryran on b77bd58 with a local branch that adds fix(libc): build the host-facing libc code on macOS #4627, fix(libs): build getifaddrs and two tests on macOS hosts #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-253db0ba (plus feat(libkernel): JIT shared memory on macOS hosts #7937's code commits, needed to build libkernel, and fix(libkernel): keep host fault signals available on macOS #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 perf(agc): probe the f32 denormal flush on MoltenVK #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 head 7cf8c61 (former feat: macOS on Apple Silicon host support (Rosetta 2 and MoltenVK) #7332, which already has a243f0c) with the flag forced off, which is whatmainemits; the change is 7cf8c61 with 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
{"commit":"e3a7f5487a1b","dirty":false,"env":{"APS5_ASYNC_PIPELINES":"1","APS5_NO_PAD_INPUT":"1","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"140:5"},"gpu":"Apple M4 Pro","driver":"MoltenVK 1.4.2","os":"Darwin 27.0.0","resolution":"3840x2160","window":{"spec":"seconds 140:5","first_present":3067,"ms":5048.078},"presents":71,"frame_ms":{"p50":70.952,"p90":72.849,"p99":83.151,"max":83.151},"gpu_busy_ms_per_present":55.574,"worker_busy_pct":{"queue worker 0x0":43.168},"top_phases_ms_per_frame":{"VideoOut.Flip":39.228,"Driver.Present":38.898,"Driver.Present.render_fence_wait":38.777,"Vulkan.Retire":38.777,"Vulkan.Retire.wait":38.752,"VideoOut.Flip.present":38.343,"Driver.Packet":30.439,"Driver.Packet.draw":24.937},"trace_dropped":false,"trace":"aps5-trace-e3a7f5487a1b-9475.json.gz"} {"commit":"0d228a554942","dirty":false,"env":{"APS5_ASYNC_PIPELINES":"1","APS5_NO_PAD_INPUT":"1","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"140:5"},"gpu":"Apple M4 Pro","driver":"MoltenVK 1.4.2","os":"Darwin 27.0.0","resolution":"3840x2160","window":{"spec":"seconds 140:5","first_present":5218,"ms":5012.876},"presents":126,"frame_ms":{"p50":39.389,"p90":41.555,"p99":50.188,"max":54.344},"gpu_busy_ms_per_present":24.222,"worker_busy_pct":{"queue worker 0x0":75.114},"top_phases_ms_per_frame":{"Driver.Packet":29.599,"Driver.Packet.draw":23.987,"Driver.Draw":23.764,"Graphics.Draw":16.088,"Driver.Draw.shader_compile_and_link":15.076,"Graphics.Draw.draw_and_resource_release":10.560,"VideoOut.Flip":9.156,"Driver.Present":8.357},"trace_dropped":false,"trace":"aps5-trace-0d228a554942-11144.json.gz"} {"commit":"e3a7f5487a1b","dirty":false,"env":{"APS5_ASYNC_PIPELINES":"1","APS5_NO_PAD_INPUT":"1","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"140:5"},"gpu":"Apple M4 Pro","driver":"MoltenVK 1.4.2","os":"Darwin 27.0.0","resolution":"3840x2160","window":{"spec":"seconds 140:5","first_present":3100,"ms":5055.060},"presents":71,"frame_ms":{"p50":71.113,"p90":73.039,"p99":79.879,"max":79.879},"gpu_busy_ms_per_present":49.920,"worker_busy_pct":{"queue worker 0x0":42.259},"top_phases_ms_per_frame":{"VideoOut.Flip":39.878,"Driver.Present":39.586,"Driver.Present.render_fence_wait":39.468,"Vulkan.Retire":39.468,"Vulkan.Retire.wait":39.442,"VideoOut.Flip.present":39.022,"Driver.Packet":29.833,"Driver.Packet.draw":24.267},"trace_dropped":false,"trace":"aps5-trace-e3a7f5487a1b-12908.json.gz"} {"commit":"0d228a554942","dirty":false,"env":{"APS5_ASYNC_PIPELINES":"1","APS5_NO_PAD_INPUT":"1","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"140:5"},"gpu":"Apple M4 Pro","driver":"MoltenVK 1.4.2","os":"Darwin 27.0.0","resolution":"3840x2160","window":{"spec":"seconds 140:5","first_present":5206,"ms":5004.336},"presents":129,"frame_ms":{"p50":38.750,"p90":41.348,"p99":43.505,"max":47.391},"gpu_busy_ms_per_present":15.344,"worker_busy_pct":{"queue worker 0x0":71.816},"top_phases_ms_per_frame":{"Driver.Packet":27.621,"Driver.Packet.draw":22.527,"Driver.Draw":22.337,"Graphics.Draw":14.708,"Driver.Draw.shader_compile_and_link":14.423,"VideoOut.Flip":10.338,"Driver.Present":9.585,"Graphics.Draw.draw_and_resource_release":9.572},"trace_dropped":false,"trace":"aps5-trace-0d228a554942-14662.json.gz"}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
{"commit":"0d228a554942","dirty":false,"env":{"APS5_ASYNC_PIPELINES":"1","APS5_NO_PAD_INPUT":"1","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"130:10"},"gpu":"Apple M4 Pro","driver":"MoltenVK 1.4.2","os":"Darwin 27.0.0","resolution":"1920x1080","window":{"spec":"seconds 130:10","first_present":6210,"ms":10017.631},"presents":349,"frame_ms":{"p50":28.570,"p90":29.979,"p99":35.216,"max":39.624},"gpu_busy_ms_per_present":26.206,"worker_busy_pct":{"queue worker 0x0":39.168},"top_phases_ms_per_frame":{"VideoOut.Flip":17.046,"Driver.Present":16.281,"VideoOut.Flip.present":16.239,"Vulkan.Retire":16.166,"Driver.Present.render_fence_wait":16.166,"Vulkan.Retire.wait":16.148,"Driver.Packet":11.201,"Driver.Packet.draw":9.554},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":8.507,"events_lost":45442,"totals_full":false}},"trace":"aps5-trace-0d228a554942-56608.json.gz"} {"commit":"e3a7f5487a1b","dirty":false,"env":{"APS5_ASYNC_PIPELINES":"1","APS5_NO_PAD_INPUT":"1","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"130:10"},"gpu":"Apple M4 Pro","driver":"MoltenVK 1.4.2","os":"Darwin 27.0.0","resolution":"1920x1080","window":{"spec":"seconds 130:10","first_present":4125,"ms":10014.015},"presents":150,"frame_ms":{"p50":64.876,"p90":72.390,"p99":98.577,"max":102.458},"gpu_busy_ms_per_present":64.311,"worker_busy_pct":{"queue worker 0x0":18.577},"top_phases_ms_per_frame":{"VideoOut.Flip":53.715,"Driver.Present":53.094,"Driver.Present.render_fence_wait":52.974,"Vulkan.Retire":52.974,"Vulkan.Retire.wait":52.947,"VideoOut.Flip.present":52.752,"Driver.Packet":12.356,"Driver.Packet.draw":10.455},"trace_dropped":false,"trace":"aps5-trace-e3a7f5487a1b-59159.json.gz"} {"commit":"0d228a554942","dirty":false,"env":{"APS5_ASYNC_PIPELINES":"1","APS5_NO_PAD_INPUT":"1","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"130:10"},"gpu":"Apple M4 Pro","driver":"MoltenVK 1.4.2","os":"Darwin 27.0.0","resolution":"1920x1080","window":{"spec":"seconds 130:10","first_present":6190,"ms":10003.664},"presents":340,"frame_ms":{"p50":28.885,"p90":31.227,"p99":40.961,"max":43.108},"gpu_busy_ms_per_present":26.939,"worker_busy_pct":{"queue worker 0x0":38.931},"top_phases_ms_per_frame":{"VideoOut.Flip":17.588,"Driver.Present":16.812,"VideoOut.Flip.present":16.766,"Vulkan.Retire":16.701,"Driver.Present.render_fence_wait":16.700,"Vulkan.Retire.wait":16.683,"Driver.Packet":11.412,"Driver.Packet.draw":9.739},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":8.780,"events_lost":37946,"totals_full":false}},"trace":"aps5-trace-0d228a554942-61538.json.gz"} {"commit":"e3a7f5487a1b","dirty":false,"env":{"APS5_ASYNC_PIPELINES":"1","APS5_NO_PAD_INPUT":"1","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"130:10"},"gpu":"Apple M4 Pro","driver":"MoltenVK 1.4.2","os":"Darwin 27.0.0","resolution":"1920x1080","window":{"spec":"seconds 130:10","first_present":4143,"ms":10041.977},"presents":150,"frame_ms":{"p50":65.150,"p90":69.407,"p99":98.458,"max":101.327},"gpu_busy_ms_per_present":64.364,"worker_busy_pct":{"queue worker 0x0":19.575},"top_phases_ms_per_frame":{"VideoOut.Flip":53.103,"Driver.Present":52.436,"Vulkan.Retire":52.317,"Driver.Present.render_fence_wait":52.317,"Vulkan.Retire.wait":52.292,"VideoOut.Flip.present":52.099,"Driver.Packet":13.062,"Driver.Packet.draw":11.046},"trace_dropped":false,"trace":"aps5-trace-e3a7f5487a1b-64156.json.gz"}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)