Repository navigation
perf(shader): leave out the both-zero select of f16 min and max - #7516
Conversation
minMaxF16Bits compares the halves as ordered integers (the low 16 bits sign-extended, with the magnitude bits of a negative half inverted), then replaced the result when both halves were zero: max took a & b and min took a | b. The ordered integers already put -0 (-1) below +0 (0), so the compare picks +0 for max and -0 for min, which is what the both-zero select produced. Every caller passes halves with their upper 16 bits clear (readF16Bits masks them, the packed path extracts them), and for those all 2^32 pairs of each give the same bits with and without the select. Each f16 min, max, min3, max3 and med3 now emits five instructions fewer per pair. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
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.
|
|
The title capture the AI-assisted: yes (Claude Code) |
|
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. 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. Summary lines (main, then the PR): |
|
@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) |
|
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
Not checked: a full build, the Written with Claude Code assistance (validation by a subagent, reviewed in this session). |
|
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) |
|
Checked that every caller passes halves with the upper 16 bits clear ( |
|
Thanks @adriadam10 for checking the callers and for the merge. AI-assisted: yes (Claude Code) |
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"on 9caecfd: 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:main9caecfd0x1e401c00000x10f9f30000That 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 ofmain9caecfd and 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.maindbac9fa was 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, digest{"commit":"dbac9fa15b61","dirty":false,"env":{"APS5_TRACE_SECONDS":"0:60"},"gpu":"NVIDIA GeForce RTX 4070 Ti","driver":"NVIDIA 595.99.02","os":"Linux 6.8.0-147-generic","resolution":"3840x2160","window":{"spec":"seconds 0:60","first_present":0,"ms":120148.301},"presents":194,"frame_ms":{"p50":16.686,"p90":32.765,"p99":2921.590,"max":112847.624},"gpu_busy_ms_per_present":6.483,"worker_busy_pct":{"queue worker 0x0":88.481,"queue worker 0x20":73.127},"top_phases_ms_per_frame":{"Driver.Packet":1000.862,"Driver.Packet.dispatch":503.561,"Driver.Dispatch":503.451,"Shader.PrepareArtifact":450.125,"Driver.Packet.memory_wait":424.003,"Vulkan.GraphicsPipeline":132.519,"Vulkan.GraphicsPipelineLibrary":126.497,"Vulkan.GraphicsPipelinePrebuild":77.312},"trace_dropped":false,"trace":"aps5-trace-dbac9fa15b61-3.json.gz"} {"commit":"d1c895bb2229","dirty":false,"env":{"APS5_TRACE_SECONDS":"0:60"},"gpu":"NVIDIA GeForce RTX 4070 Ti","driver":"NVIDIA 595.99.02","os":"Linux 6.8.0-147-generic","resolution":"3840x2160","window":{"spec":"seconds 0:60","first_present":0,"ms":114990.441},"presents":185,"frame_ms":{"p50":16.695,"p90":36.741,"p99":2157.530,"max":108482.339},"gpu_busy_ms_per_present":5.337,"worker_busy_pct":{"queue worker 0x0":88.570,"queue worker 0x20":73.887},"top_phases_ms_per_frame":{"Driver.Packet":1009.773,"Shader.PrepareArtifact":508.047,"Driver.Packet.dispatch":502.516,"Driver.Dispatch":502.453,"Driver.Packet.memory_wait":429.927,"Vulkan.GraphicsPipeline":133.420,"Vulkan.GraphicsPipelineLibrary":126.557,"Vulkan.GraphicsPipelinePrebuild":76.412},"trace_dropped":false,"trace":"aps5-trace-d1c895bb2229-3.json.gz"}GTA 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.
maineaa5c92 againstmain+ this PR at f099292, 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):{"commit":"eaa5c9238687","dirty":false,"env":{"APS5_DUMP_FRAMES":"110","APS5_DUMP_FRAMES_EVERY":"30","APS5_NO_PAD_INPUT":"1","APS5_PAD_INPUT_FILE":"<path>","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":"<path>","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"}VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ANYPS5_NO_SHADER_CACHE=1 ANYPS5_REQUIRE_VULKAN=1 ctest -R '^agc_' -Vonmaine2a2914 and onmain+ this PR (f099292, 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.main1d41645 (and with perf(shader): keep one use list per IR value #7520/perf(shader): leave out the dead code pass after masked-select elimination #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