perf(shader): leave out the both-zero select of f16 min and max #7516
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7516"
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 milescoviello on 2026-10-18T10:48:21Z.
What
TranslationContext::minMaxF16Bitspicks the result of an f16 min or max by comparing the halves as ordered integers: the low 16 bits sign-extended, with the 15 magnitude bits of a negative half inverted. After that it handled one more case. When both halves were zero (((a | b) & 0x7fff) == 0), it replaced the result witha & bfor max anda | bfor min.The ordered integers already handle zeros. +0 maps to 0 and −0 maps to −1, so the compare picks +0 for a max of +0 and −0, and −0 for a min. That is what
a & banda | bgive. Every caller passes halves with their upper 16 bits clear:readF16Bitsmasks them, and the packed path extracts them withBitFieldUExtract. The nested min/max ofmed3only feeds those halves back in. So the both-zero select never changes a bit. This PR removes it, which is five instructions fewer per pair (or, and, compare, and/or, select) in every f16 min, max, min3, max3 and med3, scalar and packed.I checked the equivalence exhaustively on the CPU. The C function below runs the old and the new body on all 2³² pairs of 16-bit halves, for max and for min. The result is
pairs=8589934592 diffs=0.check.c
Float16MinMaxBits.cpp's CPU reference keeps its both-zero rule. The test draws ±0 for about one operand in nine, and it now checks the shorter lowering against that rule on the GPU.Tested
Ubuntu 24.04 (Linux 6.8), GCC 13.3, NVIDIA RTX 4070 Ti, driver 595.99.02.
ctest -R "^agc_|^shader_ir"on9caecfd2d5: 709/709 pass (3 skipped: host import sparse, sparse dma-buf import, stencil ref export). Among them are the 35 tests matchingf16|float16|f32_min_max|half, includingagc_driver_float16_min_max_bitsandagc_driver_f16_min_max_nan.SPIR-V size, from
agc_shader_replay(Release,ANYPS5_ENABLE_SPIRV_TOOLSoff,ANYPS5_NO_SHADER_CACHE=1) on 420 shader requests I had dumped withAPS5_DUMP_SHADERS: 355 from MXGP 26 (PPSA30839), 61 from PPSA31162 and 4 from another app. Nine of them change, all from PPSA30839, with 100,279 fewer words in total (−0.28%). The largest isshader_10f9f30000, at 1,130,170 → 1,041,550 words (−7.8%). PPSA31162's largest compute shader (0x1e401c0000, from a separate dump) goes from 1,056,287 to 974,911 words (−7.7%).Replay CPU time (
perf stat -e task-clock), 25 runs per build, interleaved:main9caecfd2d50x1e401c00000x10f9f30000That is 12.4% and 12.8%, more than the size drop. The removed instructions are also work that the passes after translation no longer do.
GPU time of that dispatch. I ran PPSA31162 for 180 s per run with pad input and
APS5_PROFILE_GPU=1, using Release libraries ofmain9caecfd2d5and of this PR. I did one warm-up run with this PR first, since its SPIR-V makes new shader cache entries, then interleaved the builds. The figures add up theby programentries of every[gputime]line of a run.0x1e401c0000dispatchesmainmainThe last run reached a lighter part of the game (964 presents, 122.5 ms of GPU batch time per present against 137 to 145 ms in the others), so it doesn't compare directly. The two runs that reached the same part (614 and 618 presents) put this PR about 10% below
mainfor that dispatch. Amainrun from earlier the same night gave 20.63 ms over 373 dispatches. That is one comparable run per build, so I'm not claiming a frame time change.tools/check_conventions.py --base upstream/mainpasses.tools/pr_overlap.pylists only conflicting PRs that it could not compare onFloatInstructions.cpp. #4072 has the packed path'sminMaxLaneas a context line. #7332'sbothZerois in its own FMA emulation, not this function. #5021 doesn't touch either.Title capture
As the
perflabel asks, these are captures of one title and scene on one machine. MXGP26 (PPSA30839) ran from boot withAPS5_TRACE_SECONDS=0:60,ANYPS5_NO_SHADER_CACHE=1and a fresh run directory (cold AnyPS5 and NVIDIA caches), so the shaders compile inside the window. NVIDIA RTX 4070 Ti (595.99.02), Ubuntu 24.04, release libs, Xvfb display, no input.maindbac9fa15bwas captured five times: before the PR captures, after them, and three more times in a row after that. Each PR capture and the first twomaincaptures followed a rebuild of the release libraries; the last threemaincaptures did not, and they came out lower. The change ismainwith this PR merged on top as a local merge commit (d1c895bb2229). The time is the wall time of theShader.PrepareArtifactslices on theshader preparethreads in each trace.main1main2main3main4main5The five
maincaptures span 17.7% in total prepare time, and the 13 prepare threads run in parallel with the title, so a title capture can't resolve a recompiler change of a few percent. The replay measurement above is this change's measurement. This PR's capture is above all fivemaincaptures in total prepare time. With one capture of the change I can't tell that apart from noise, and the replay measurement above points the other way. I can repeat the capture if that would help.main1 trace, digest,main2 trace, digest,main3 trace, digest,main4 trace, digest,main5 trace, digest, this PR trace, digestGTA V capture, by @CPritch (comment): PPSA04263 01.010.002, NVIDIA GeForce RTX 5060 Laptop GPU, driver 615.71.09, Linux 7.2.8-zen1-2-zen, Performance mode, the Story prologue after Resume Story with no input.
maineaa5c9238againstmain+ this PR atf099292f8, two runs each in the order A B A B, frames 3500:700, warm caches. Frame p50 was 80.8 and 81.5 ms onmainand 79.4 and 79.9 ms with this PR (−1.5 ms, −1.8%). GPU busy per present was 64.5 and 63.3 ms. Other scenes, Fidelity mode and other GPUs were not tested. Summary lines (main, then this PR):VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ANYPS5_NO_SHADER_CACHE=1 ANYPS5_REQUIRE_VULKAN=1 ctest -R '^agc_' -Vonmaine2a2914aa1and onmain+ this PR (f099292f8c, a local merge).main: 815 of 815 pass. With this PR, 814 of 815 pass:agc_driver_compute_user_data_callsaborted in the dynamic loader before the test began (Inconsistency detected by ld.so: ... elf_machine_rela_relative: Assertion ... failed). A test in another PR's run hit the same assertion that evening, so it isn't this change. Rerun 5 times under the layer on the same merge,agc_driver_compute_user_data_callsand its_registeredform pass 5 of 5 with no VUIDs. Neither log reports the layer missing. VUIDs onmainand with this PR:VUID-vkCmdCopyBuffer-srcBuffer-00118×2,VUID-vkCmdDraw-multisampledRenderToSingleSampled-07285×2,VUID-vkCmdWaitEvents-srcStageMask-01158×2,VUID-VkImageViewCreateInfo-usage-02275×6,VUID-VkPipelineColorBlendStateCreateInfo-pAttachments-00605×18,VUID-vkSetEvent-event-09543×2. None is new with this PR.main1d4164552(and with #7520/#7522 in a second arm): the 41-test shader and f16/f32 min/max GPU set passes 3 of 3; identical shader-cache entries and replay hashes against twomainruns (Showcase PPSA99005, RetroArch PPSA99169, vkQuake PPSA99010), static frames bit-identical, and draw-path recompile time withinmain's spread.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 github-actions[bot] on 2026-10-18T11:01:55Z.
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.
core/shader/recompiler/Translation/src/FloatInstructions.cppcore/shader/recompiler/Translation/src/FloatInstructions.cppImported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-19T21:35:28Z.
The title capture the
perflabel asks for is now in the description, with both summary lines, the traces and the digests: MXGP26 from boot, a cold shader cache,maindbac9fa15bagainstmain+ this PR (d1c895bb2229). The fivemaincaptures span 17.7% in total shader prepare time, so the capture can't resolve this change, and the replay measurement in the description is still its measurement. This PR's capture is above all fivemaincaptures in total prepare time. With one capture of the change I can't tell that apart from noise, and the replay measurement above points the other way. I can repeat the capture if that would help.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by CPritch on 2026-10-20T07:25:56Z.
Ran this on GTA V (PPSA04263 01.010.002), Linux 7.2.8-zen1-2-zen, NVIDIA GeForce RTX 5060 Laptop GPU, NVIDIA 615.71.09, Graphics Mode Performance, the Story prologue after Resume Story, with no input.
main
eaa5c9238against maineaa5c9238+ #7516 atf099292f8, same machine and session, two runs each in the order A B A B, window frames 3500:700, caches warm, APS5_TRACE_EVENTS=4194304 (BUILD.md's remedy for full trace buffers), no other APS5_TRACE_* or APS5_PROFILE_* switch, log not redirected. The harness navigates the menus from frame dumps in boot and load to present 3300 (APS5_DUMP_FRAMES=110, every 30 presents), which stop before the window.Frame p50: main 80.8, 81.5 ms, with #7516 79.4, 79.9 ms (-1.5 ms, -1.8 %). GPU busy per present: 64.5 and 63.3 ms.
Not tested: other scenes, Fidelity mode, other GPUs.
Written with AI assistance (Claude Code); measured on the machine above.
Summary lines (main, then the PR):
{"commit":"eaa5c9238687","dirty":false,"env":{"APS5_DUMP_FRAMES":"110","APS5_DUMP_FRAMES_EVERY":"30","APS5_NO_PAD_INPUT":"1","APS5_PAD_INPUT_FILE":"","APS5_TRACE_EVENTS":"4194304","APS5_TRACE_FRAMES":"3500:700"},"gpu":"NVIDIA GeForce RTX 5060 Laptop GPU","driver":"NVIDIA 615.71.09","os":"Linux 7.2.8-zen1-2-zen","resolution":"3840x2160","window":{"spec":"frames 3500:700","first_present":3500,"ms":58017.608},"presents":700,"frame_ms":{"p50":80.761,"p90":92.46,"p99":113.753,"max":122.312},"gpu_busy_ms_per_present":64.223,"worker_busy_pct":{"queue worker 0x0":3675.407},"top_phases_ms_per_frame":{"Driver.EndSubmission":2979.433,"Driver.Packet":64.514,"Graphics.Draw":61.281,"Graphics.Draw.draw_and_resource_release":57.427,"Driver.Packet.draw":51.271,"Driver.Draw":51.055,"VideoOut.Flip":25.73,"Driver.Present":25.713},"trace_dropped":false,"trace":"aps5-trace-eaa5c9238687-1236137.json.gz"}
{"commit":"f4607d9a850d","dirty":false,"env":{"APS5_DUMP_FRAMES":"110","APS5_DUMP_FRAMES_EVERY":"30","APS5_NO_PAD_INPUT":"1","APS5_PAD_INPUT_FILE":"","APS5_TRACE_EVENTS":"4194304","APS5_TRACE_FRAMES":"3500:700"},"gpu":"NVIDIA GeForce RTX 5060 Laptop GPU","driver":"NVIDIA 615.71.09","os":"Linux 7.2.8-zen1-2-zen","resolution":"3840x2160","window":{"spec":"frames 3500:700","first_present":3500,"ms":56857.493},"presents":700,"frame_ms":{"p50":79.445,"p90":89.182,"p99":112.998,"max":131.95},"gpu_busy_ms_per_present":63.37,"worker_busy_pct":{"queue worker 0x0":3676.402},"top_phases_ms_per_frame":{"Driver.EndSubmission":2919.788,"Driver.Packet":64.085,"Graphics.Draw":59.569,"Graphics.Draw.draw_and_resource_release":55.942,"Driver.Packet.draw":51.03,"Driver.Draw":50.815,"VideoOut.Flip":24.949,"Driver.Present":24.936},"trace_dropped":false,"trace":"aps5-trace-f4607d9a850d-1244055.json.gz"}
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-20T07:29:44Z.
@CPritch thank you for the GTA V run. A B A B order with warm caches on the same session is exactly the comparison this change needed, and a second GPU and title help a lot after my MXGP captures couldn't resolve it. I added your numbers and both summary lines to the description, credited to you, with a link to your comment: frame p50 −1.5 ms (−1.8%) in the prologue on the RTX 5060 Laptop, plus what you list as not tested.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-20T15:01:45Z.
Second GPU and title run, Windows. Host: Windows 11 Pro 26220, NVIDIA GeForce RTX 4070 driver 596.21, Ryzen 7 5700X, WinLibs MinGW-w64 GCC 15.2 r7, Release. This PR at
f099292f8was merged with #7494 (0aab6c1f3; they share no file) onmain1d4164552(arm A). A second arm added #7520 (d484281b6) and #7522 (cabf6ad53). All merges were clean. Targeted build: the shader and AGC test targets pluslibs.--repeat until-fail:3, 41 tests: the^shader_|^agc_shader_set,agc_driver_float16_min_max_bits,agc_driver_f16_min_max_nan,agc_driver_f32_min_max_nan,agc_driver_float16_ternary_clamp,agc_driver_packed_float16_native,vector_min_max_med16,agc_ir_reclaim,agc_masked_select,agc_constant_phi. All pass 3 of 3 on both arms, none skipped. The exception wasshader_cache_runtime_sourceson arm A, which hit its 180 s limit once on a loaded host and passed 3 of 3 serially (68 to 108 s).mainruns, arm A and arm B. I matched keys with the source-version word masked and compared payloads with host pointers masked: 5 Showcase entries hold a host address and differ between twomainruns too. The two compute requests Showcase dumps withAPS5_DUMP_SHADERSreplay (agc_shader_replay --spv) to identical SHA-256 on all three builds. Since this PR changes the SPIR-V of every f16 min/max, none of these titles has one. So the title runs below are a no-regression check for this PR, not a measurement of it. The GPU tests above are what exercise the shorter lowering on this card.main, A,main, A, then one B run, 90 s each. Settings:ANYPS5_NO_SHADER_CACHE=1, a freshly relinked run directory each time,APS5_PROFILE_DRAW=1 APS5_PROFILE_GPU=1 APS5_TRACE_SUMMARY_EVERY=10,APS5_TRACE_SECONDS=0:50. 15 runs with no stop line; every run was still rendering at the timeout. These titles have noShader.PrepareArtifactslices: their shaders compile on the draw path. So I summed the draw-path "recompile" row of the[draw] driver phaseslines over each whole run:main2594.5 / 2223.2 ms (16.7% apart), arm A 2530.6 / 2473.1 ms (+3.9% on the mean), arm B 2245.5 ms. Presents: 6368 / 6463, A 6581 / 6515, B 6688.main46.8 / 35.0 ms, arm A 42.2 ms, arm B 39.7 ms. The other arm A run (14.1 ms, 126 presents against 700 to 805) ran during a load spike on the host and is not usable.mainspread, which here is about the 17.7% you saw on MXGP26. At this size of prepare work a title capture can't resolve the change on Windows either.APS5_DUMP_FRAMES, every 10 presents): vkQuake's four static console and loading screens are bit-identical betweenmainand both arms, and frame 0 is identical in all 9 runs. Later frames, and all of Showcase and RetroArch, are animated and differ between twomainruns as well, so they can't be compared.tools/check_conventions.py --base origin/mainandgit diff --check: pass on both arms.Not checked: a full build, the
agc_driver_GPU suite, a title whose shaders use f16 min/max, AMD/Intel, Linux.Written with Claude Code assistance (validation by a subagent, reviewed in this session).
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-20T15:03:41Z.
Thanks @screamm, the first Windows title runs of this one, and the identical cache entries and replay hashes are the right check for a bit-exact change. Added to the description.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-20T15:27:51Z.
Checked that every caller passes halves with the upper 16 bits clear (
readF16Bits,laneBitsviaBitFieldUExtract, nested min/max in med3), and that the ordered compare already yields +0 for max and −0 for min on ±0 pairs, so the both-zero select never changed a bit. Replay numbers and @CPritch's GTA V run back the gain; merged.Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-20T15:46:47Z.
Thanks @adriadam10 for checking the callers and for the merge.
AI-assisted: yes (Claude Code)