perf(agc): cut the CPU work of PM4 register writes, submission copies and validation #18914
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/18914"
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 atinseau on 2026-11-12T13:23:11Z.
What
The CPU path of a PM4 submission does more work than it needs at four points: writing register ranges, copying and scanning the command buffer, looking up the indirect register lists, and validating register writes. This PR removes that work while preserving packet validation, ordering and register values.
CbSet*RegistersDirectand the register range and list writers oflibSceAgccopied their values into astd::vectorbefore writing. They now write straight into the command buffer, split by the same runs. A single register write goes from 56 to 22 ns (1,239 to 595 instructions, one allocation to none); a 12-register list in 6 runs from 1,051 to 183 ns (17 allocations to none).RegisterLists) instead of astd::map;at()still throwsstd::out_of_rangefor a missing cursor (the diagnostic text depends on the standard library on main), andReferencedProgramsand the dispatch-ahead scan read the new type.copyCommandssizes its buffer up front.copySegmentrecords the cursors of the flip, rendering-wait, end-of-pipe, label-write and wait packets and copies runs of ordinary packets with one insert;waitForFlipRoom,holdFlipBehindWorkerandreserveOutputsvisit only the recorded cursors, and fall back to the full scan when the copy met a non-type-3 or truncated tail. Copying 26 draws goes from 2.69 to 0.80 us (60.5k to 18.5k instructions); the three scans from 727, 731 and 1,175 ns to 3.1, 4.7 and 4.4 ns. A whole 26-draw submission (copy and scans) costs 9.7 us before and 5.1 us after (190k to 113k instructions, 9 allocations to 1); with three indirect register packets per draw, 29.4 and 16.3 us.Pm4::Validatechecks the four register write opcodes (80 % of a draw's packets) first, and an opcode table replaces theUnsupportedReasonswitch for opcodes that have no reason. A register write validates in 4.8 ns instead of 11.3 ns.Behaviour locks: new tests pin the register writers across buffer growth and run splitting, the lookup of absent register list keys and the PM4 validation messages of the register write opcodes and of every unsupported opcode (all pass before and after the change). A differential of the new validation against the previous one over 4 million random and edge packets found no mismatch, and a digest over 8,000 random PM4 streams (valid ones and ones with truncated or non-type-3 tails, indirect packets, flips, rendering waits, end-of-pipe releases, label writes and conditional ends) covering copy, validate, lists,
waitForFlipRoom,holdFlipBehindWorkerandreserveOutputsis identical before and after. The figures are single-process fixture measurements (alternating runs of the parent and the change) from the older stack the commits were written on, not a title trace.The register writer tests cover range lengths 64, 65 and 130, and register lists of 32, 33, 64, 65 and 130 entries. Register writes share the accepted flag mask with the general validator. The tests check rejection reasons for every opcode and flag byte, and exercise the four register-write opcodes at every flag byte.
Tested
Linux retest on the rebased head. Base
01c550960a9; change144ab00b45f. The marker NOP fast path on the previous head rejected0x68754000in GCN mode. Marker NOPs now go through general validation. A focused reproducer on Linux fails on6a8befbcf85and passes on both main and the new head: GCN accepts the ordinary marker on both queues, legacy flip markers retain their specific error, and non-GCN rejects typed markers.Ubuntu 25.10 x86-64, GCC 15.2 Release, BUILD_TESTING=ON. The same 11 CTests ran serially on main and the PR under QEMU user 10.0.13 (AVX2), using Mesa llvmpipe 25.2.8 (LLVM 20.1.8) and Khronos validation 1.4.335. Both runs: 11 passed, 0 failed, 0 skipped. Both logs show the loader inserting the validation layer, no missing-layer warning and no
VUID-.agc_driver_pm4ran and passed on the new head. The suite also covers register writer boundaries, context register RMW, REWIND copy/size/trace, prepared shaders (default, indirect, debug) and both GPU-written indirect-buffer cases.The REWIND test includes twelve deliberate waits and produces a one-present trace. First lines of
tools/trace_digest.pyfor the same synthetic fixture (not used as performance evidence):I checked the indirect-buffer merge: replacement keeps the lists before the splice, takes the re-read lists within it, and shifts later cursors by the size difference. After a splice the submission falls back to the full scan. Output reservations capture waits before processing the next relevant packet, including trailing segments. The rebase preserved that code unchanged.
Paired CPU fixture on this head: one three-word SET_SH_REG (opcode
0x76, register offset0x10, increasing payload) validated on the graphics queue. Linux x86-64 via OrbStack on an Apple M3 Max, GCC 15.2,-O2, no LTO. Both binaries linked the same harness and GCN decoder with their revision's production PM4 validator. Eight processes per arm in alternating order, 250,000 warmups and 10 million calls per process, no other compilation or tests running in the container. Median 13.18 ns/call on main (13.13–13.72) and 6.52 ns/call on this head (6.45–6.60). This measures CPU packet validation only. On an earlier revision the Cyberpunk intro comparison found no measurable frame-rate change. No paired title trace was captured on the new head.Earlier macOS run:
macOS 27.0.1, Apple M3 Max (36 GB), MoltenVK 1.4.2 from Vulkan SDK 1.4.363.0, Release build with
-DBUILD_TESTING=ON, x86_64 under Rosetta 2.maindoes not run its libraries on macOS, so both sides aremain3b7f31fa7with the macOS port (the squash of #8658, which its author upstreams in parts) and local macOS build fixes for tests ofmainthat Apple clang refuses; the change side adds this PR's commits.VK_LAYER_KHRONOS_validation,VK_LOADER_DEBUG=layer,ANYPS5_REQUIRE_VULKAN=1andANYPS5_NO_SHADER_CACHE=1(ctest --test-dir build-macos -R '<811 test names>' -V): on the change 702 pass, 71 skip, 37 fail and 1 times out, against 704, 71, 35 and 1 onmain. The two tests that differ,agc_driver_graphicsandagc_graphics_profile_draw, both stop on "two ranges under a budget for two were not imported" (the host refusing a memory import under load), which is intermittent on this Mac with or without the change: run alone ten times in the same conditions,agc_driver_graphicspasses 8 times onmainand 9 times with the change, andagc_graphics_profile_draw9 times and 10 times. Every other test has the same status as onmain; the failures are the onesmainhas on macOS (MoltenVK limits and macOS-only gaps of the tests). Both runs report the same 77VUID-errors test by test, andwas not found but was requested by env var VK_INSTANCE_LAYERSappears only in the 12 lines that print the failure regex of theagc_driver_image_table_packed*tests. The logs show the loader inserting the layer (Insert instance layer) in 490 tests.python3 tools/check_conventions.py --base origin/mainpasses.tools/pr_overlap.pylists six open pull requests that change lines this PR changes: #17411 (copySegmentloads each REWIND control once with acquire), #18072 (queues a flip submission before holding the title), #16054 (a 0xffffffff register offset names no register), #16878 (CONTEXT_REG_RMW), #11346 (runs an indirect buffer as it is) and #15136 (frame capture deltas). None implements the same optimization; whichever merges second rebases over the other. It could not compare eleven more that conflict withmain(for example #6188, #10249, #11436).No new Windows run on this head.
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)Summary by CodeRabbit
Imported from boykopovar/AnyPS5. Originally posted by Ethan-Hainline on 2026-11-12T22:19:11Z.
Review: COMMENTED
I traced the three paths that carry the "unchanged" claim on
a95bfaad31.Direct register writes.
WriteRegisterRangestill leaves the payload untouched whenvalues == nullptr(Packet.cpp:154passes the null through), but that is unchanged frommain: the oldsnapshot.resize(count)was inside the samevalues != nullptrbranch, and the batch path writes nothing in that case. The run splitting inWriteRegistersreproduces the oldcount != 0,count <= 0x3fff,offset <= 0xffffandcount <= 0x10000 - offsetchecks.Register lists.
readRegisterListsclearssubmission.registerListsbefore it appends (Submission.cpp:353), and it walkscommandsin ascending order, so the sorted vector is never fed out of order. The reuse path incopyCommandsis safe because the only caller clears first.Scan cursors. Every scanned packet is recorded: the batch path records the first packet and each following one at
first + (end - cursor); the default path records at the insertion point; the predicated-indirect path inserts the 4-word packet and the target goes through the samecopyLink; the truncated tail is the only path that setsscanAll = true. I did not find an insertion that leavesscanAll == falsewith a missing cursor.Two small test gaps, neither blocking:
testRegisterWritesDoNotAllocateuses 12 registers and 8 values, below the 32/64 inline limits ofSmallSnapshot; it would still pass if the spill thresholds regressed. Add 64/65-entry cases (and a contiguous run longer than 64) and assert the contents.Pm4::Validate'sWithoutReasoncache duplicates the flag check: it is equivalent today, but a later narrowing ofallowedFlagswould be accepted by the cache. A shared helper, or a test that both paths agree on every opcode/flag pair, would pin it.The four independent hardware reports cover the functional side; I did not find a behavior change the PR misses.
AI-assisted review.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-22T08:30:29Z.
Review: APPROVED
Reviewed — looks good
The reviewed changes appear sound on the inspected paths; author-reported tests passed, but I did not run checks in this review.
What I ran
Read the prepared diff and changed source at head
144ab00b45f172b9dd8ffd5be874ae9f585fd4ffusingrepo_read.pyreader tools. No commands or tests were run.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
I did not run tests, builds, or benchmarks. I read all files listed as reviewed in
manifest.json; no files were excluded in that manifest.files.jsonwas not available through the reader, so additional exclusions are not verified.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-11-29T22:54:16Z.
Review: COMMENTED
Architecture findings (1)
Advisory architecture findings (1)
3099cc99-3c4f-40fd-b47b-1ad89b1441d2Reviewing files that changed from the base of the PR and between
1f27e2abfaand4d5207a4c5.core/libs/prx/libSceAgc/Command/src/Packet.cppcore/libs/prx/libSceAgc/tests/RegisterCommands.cppcore/libs/prx/libSceAgcDriver/Execution/include/Driver/Queues/Submission.hppcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Dispatch/DispatchAhead.cppcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Packets/CommandValidation.cppcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cppcore/libs/prx/libSceAgcDriver/Execution/src/Pm4.cppcore/libs/prx/libSceAgcDriver/tests/Pm4.cppcore/libs/prx/libSceAgcDriver/tests/execution/PreparedShaders.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-29T23:06:44Z.
Review: COMMENTED
Reviewed — comments
The diff and all nine changed files were reviewed at head
4d5207a4c557837589c10b4dc177db1602b5c147; no blocking defect was verified, but I did not run the checks needed to justify approval.What I ran
I inspected the PR description, commit list, manifest, and complete diff at the stated head. No commands or tests were run by me. Test results in the description are author-reported.
Results
Findings
Blocking
None verified.
Non-blocking
No code-level findings. The reported test results and performance measurements remain author-reported for this review.
Not verified
I did not run tests, build the change, or independently verify the author-reported test and benchmark results. No files were excluded from the changed-file review; the diff excludes files omitted by the prepared review rules.
To get this to a good state
Nothing is required. The author-reported checks and performance measurements are optional context, not independently verified findings.