perf(shader): fold masks, shifts and field extracts through bits they do not use #7673
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7673"
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 tibzejoker on 2026-10-18T21:34:12Z.
What
The translator splits and merges the 16-bit halves of 32-bit registers on every f16 or packed instruction. As a result, the IR carries many masks, shifts and field extracts that do not change any bit their user reads. This change teaches the constant folder which bits a value can hold (masks, merges, shifts, field extracts, 16- and 8-bit widenings, selects) and which bits a user reads. It applies these exact rewrites:
And(And(x, a), m)becomesAnd(x, m)whenmkeeps no bit outsidea. A rewritten mask is folded again:Foldvisits each instruction once, so chains used to survive.ShiftRightLogical(ShiftLeftLogical(x, k), k)becomesAnd(x, ~0u >> k).BitFieldUExtract(x, 0, n)isxwhenxhas no bit at or aboven.Orof two masked parts of one word becomes that word under the union of the masks, or the word itself when the masks cover it.Every rewrite gives the same bits, so shader results do not change.
Tested
Windows 11, MinGW GCC 15.2, RTX 4090 (616.92):
agc_constant_bit_testgets six new cases: repeated, narrower and wider masks, a mask under a merge, half extractions, halves that rejoin. All six fail onmain.ctest -R ^agc_: 726/727. The failure isagc_driver_bda_device, which fails onmaintoo.Size and compile time, GTA V (PPSA04264) compute shaders dumped with
APS5_DUMP_SHADERS:agc_shader_replay)How the compile times were measured:
APS5_DUMP_COMPUTE_PIPELINESthrough the driver's dispatch path.agc_compute_pipeline_replay.pipeline_createis the median of 3 replays, each with a distinctOpSourceExtensionso that the NVIDIA driver cache misses.Output check: the same rewrites were run on a local test branch, where 71 of these compute shaders were dispatched on synthetic memory with and without them. Every byte they wrote was identical on the RTX 4090.
Not attached: frame traces. This changes pipeline creation time (stutter while shaders compile), not the steady-state frame.
Performance capture
GTA V (PPSA04264), open world from the same save, RTX 4090, one run each, same session. Base =
main47a266cf9+ #6488 + #6848 + a local bench aid (keyboard pad input while an IME session is open), because GTA V does not run on plainmain;cdeb62157e5eis this PR on top of it. Traces: fork release perf-7673-7677-20261019.These frame times do not measure this change. The trace buffer of queue worker 0 fills after about 3.7 s (
trace_dropped), and in the open world frame time follows where the save respawns. This PR shortens shader compiles: the SPIR-V word counts and the cold compile times under Tested are the measurement.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
https://claude.ai/code/session_01XBXM4pgkMHYEtqN1EQni2P
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-18T21:34:24Z.
This pull request is labeled
perf, so it needs the summary line of a capture onmainand one with the change, from the same title, scene and machine, as the Performance report form asks. Please paste bothaps5-summary.jsonllines, attach the two traces and add the output oftools/trace_digest.py.Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-18T21:46:29Z.
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/libs/prx/libSceAgcDriver/tests/ConstantBitTest.cppcore/shader/recompiler/Optimization/src/ConstantFolder.cppcore/shader/recompiler/Optimization/src/ConstantFolder.cppImported from boykopovar/AnyPS5. Originally posted by LeandroLP23 on 2026-10-18T22:33:41Z.
AMD run, since the description covers an RTX 4090. Radeon 860M (RDNA 3.5, driver 32.0.13058.8001), Windows 11 26300, WinLibs GCC 15.2.0 r7, Release,
BUILD_TESTING=ON. Build:main8a456334fwith #7677, #7673, #7671, #7666, #7676, #7675, #7672, #7668, #7664 and #7658 merged together (each merge clean, built once).agc_constant_bit_testandagc_driver_bda_devicepass 3 runs of 3 on AMD's proprietary driver.AI-assisted
Imported from boykopovar/AnyPS5. Originally posted by tibzejoker on 2026-10-19T00:10:19Z.
About the performance-report request: I captured the requested warm-cache traces (GTA V PPSA04264, open world, RTX 4090). Two things make them a poor fit for this change:
trace_dropped: true, about 3.3 M events lost), so the per-phase totals are not usable. Only the frame percentiles survive.Driver.Draw.shader_compile_and_link), depending on which new shaders that run meets.This change only makes modules smaller. Its effect is on pipeline creation, so I measured that directly: cold NVIDIA
pipeline_create, median of 3agc_compute_pipeline_replayruns, each with a distinctOpSourceExtensionso the driver cache misses. The numbers are in the description. If you'd rather have a specific trace or scene, tell me which and I'll capture it. The three traces I took are here, for completeness: https://github.com/tibzejoker/AnyPS5/releases/tag/perf-7673-7677-20261019AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by savvaniss on 2026-10-19T06:36:11Z.
Intel and AMD Windows data:
agc_constant_bit_testfails onmainand passes with this PR, on an Intel UHD 770 and an AMD RX 9070 XT.Windows 11 Pro 26300, WinLibs GCC 15.2.0 r7, Release, every execution test built,
ANYPS5_NO_SHADER_CACHE=1, serial CTest. Intel UHD Graphics 770 (8086:a780), driver 32.0.101.7088. AMD Radeon RX 9070 XT (1002:7550), driver 32.0.31041.1004, withVkCacheemptied first. This head55bb2c793merged intomain07377573f. "Without" means this PR's test files onmain07377573f, with none of its other changes.agc_constant_bit_testThis is a correctness check only; I measured no speed.
I also ran full CTest with this PR and the 13 other open AGC and shader PRs I tested in the same batch merged together (#7668, #7659, #7504, #7432, #7426, #7413, #7480, #7465, #7466, #7464, #7449, #7489, #7560). Neither GPU showed a failure beyond
main's own: the five image-table tests (#6647) andagc_driver_sample_multisample_target(#6957) on the UHD 770, andagc_driver_layered_volume_vertex_no_gpl(#4768) on the RX 9070 XT.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by tibzejoker on 2026-10-19T06:40:48Z.
Thank you @savvaniss: RDNA4 (RX 9070 XT) and the UHD 770 both fail on
mainand pass with the head, which is the behaviour the test is meant to pin down. Good to see it holds with the 13 other PRs merged together too.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-20T00:41:18Z.
Checked the demanded-bits rewrites (and/or/xor merges, shifts, field extracts, 16-bit truncation, SRL(SLL) and masked-part merging) against the IR semantics: each keeps every bit its user reads. The six new
agc_constant_bit_testcases pin the folded shape and fail onmain; the NVIDIA, AMD and Intel runs in the thread agree; merged.