Repository navigation
perf(agc): build the pipeline store key without per-field vector appends - #18863
Conversation
…ps none of its bytes The key is built into a reused buffer; this guards the shorter key against stale tail bytes of an earlier longer one. It passes on the base.
Each draw's pipeline lookup built its key with one vector insert per field. The key now goes through a writer that copies into the pre-sized thread-local buffer and bulk-copies the vertex bindings, attributes, layout words, blends and blend constants; the bytes are unchanged, the lookup compares them with memcmp. On the probe fixture (611-byte key, 12 bindings, 8 attributes, 2 colors, all hits) a store hit drops from 21116 to 6365 instructions and from about 900 to 390 ns, and key building from 14127 to 4404 instructions. A differential against the base key builder over 150k random states shows no mismatch.
The key writer checked and grew the buffer on every field. The buffer is now sized once from the state, context and per-item counts, which bound everything the key writes, so each field is a plain store. On the probe fixture a store hit drops from 6365 to 4309 instructions (about 390 to 330 ns) and key building from 4404 to 2351. The key bytes are unchanged: the differential against the base key builder shows no mismatch over 150k random states.
The key writer sized its buffer from State, Context and the per-item counts and only checked the written length after the last field, so a key field added without its share of the capacity would have written past the buffer before the check fired. Each write now checks the room left and throws before copying. On the lookup probe the hit keeps its cost: 277.4 ns before and 276.8 ns after (median of 5 alternating runs), with 249 more instructions per key (4,339 to 4,588).
|
This pull request is labeled |
|
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.
|
|
Windows check of head The 47 AGC driver tests that cover the pipeline-store key, built from
This is a correctness check only; I did not measure the key-building cost. AI-assisted: yes. |
MarcZX10
left a comment
There was a problem hiding this comment.
Reviewed — no blocking findings
Nothing blocks this; the points below are optional.
What I ran
Head aee4029f70d403064814ce45920682c338131962 (base main). No build and no tests in this review. I read the diff and the head source with the repository reader:
repo_read.py search keyedBlend core/libs/prx/libSceAgcDriver
repo_read.py search blendConstants core/libs/prx/libSceAgcDriver/Graphics
repo_read.py search mainKey core/libs/prx/libSceAgcDriver/Graphics/include
repo_read.py cat core/libs/prx/libSceAgcDriver/tests/Graphics.cpp 13490 13545
Results
| Check | Result | Note |
|---|---|---|
| Build and focused tests | not run | No build in this review |
| Author's focused test run (811 tests) | not run | Author-reported, macOS only |
check_conventions.py |
not run | Author-reported |
| Changed hunks read at head | passed | Read, not compiled |
| Key capacity bound | passed | Read against the fixed fields; no overflow found |
Findings
Blocking
None.
Non-blocking
-
core/libs/prx/libSceAgcDriver/Graphics/src/Pipeline.cpp:1064and:1071callstd::memcpy, and:1353callsstd::memcmp, but the include list ofPipeline.cpp(lines 1–29) has no<cstring>. It compiles only if a header already brings it in. Not built here, so this is unverified. Suggested fix, next to<cstdlib>:#include <cstdlib> #include <cstring>
The key bytes match the old field-by-field writes: the bulk copies are only used where the static asserts prove the struct layout (no padding, offsets as expected), and blendConstants is std::array<float, 4> and mainKey is a std::span<const std::uint32_t>, so sizeof and size_bytes() give the right byte counts.
Not verified
- The build, the compile, and every test. The author reports a focused run of 811 tests on macOS (703 pass, 71 skip, 36 fail, 1 timeout, against 704, 71, 35, 1 on
main), andcheck_conventions.pypassing. Neither was reproduced here. - The benchmark numbers (store hit 870 ns to 313 ns, key build 582 ns to 143 ns), and the differential run over 150,000 random states. Author-reported only.
- Linux and Windows: not tested by the author and not checked here.
- Callers of
BuildPipelineStoreKey(for example inPipelineJournal.cpp) were not read.BuildPipelineStoreKeynow copies out of a scratch buffer on every call; the cost of that was not measured. core/libs/prx/libSceAgcDriver/tests/Graphics.cppis 14,282 lines and is in the reviewed list, but only lines 13490–13545 were read: the new check and its neighbours. It was read through the reader, since its full text is not in the review folder.- The capacity bound: I checked the fixed fields outside
StateandContextagainst the 512-byte slack, and the per-element sizes for shaders, colours, blends and vertex descriptions. I did not check every nested field size insideState.
Read in full or in the hunks shown: Pipeline.cpp (the diff, plus lines 1–30, 1048–1087, 1095–1119 and 1140–1215 of the head); Graphics.cpp (lines 13490–13545 only). Excluded by rule: none.
To get this to a good state
Nothing is required.
Optional: add #include <cstring> to Pipeline.cpp (see the finding above), so the file does not depend on a transitive include.
Pipeline.cpp calls std::memcpy and std::memcmp and relied on a transitive include.
|
Thanks for the review. |
|
A Linux run, next to the Mac and RX 6700 XT Windows runs: RTX 3070 (8 GB), NVIDIA 610.57.04, Ryzen 5 5600X, Debian 13, kernel 6.12.115, GCC 13.3.0 Release, validation layer 1.4.335,
The same 47 tests also passed 3/3 at the previous head aee4029. This is a correctness check only; I did not measure timings. AI-assisted: yes |
|
Windows/AMD validation on RDNA 2: the new key case passes.
No AMD frame-time number here: the PR's measurement is on macOS/MoltenVK, and I only ran the functional side on a different vendor. |
MarcZX10
left a comment
There was a problem hiding this comment.
Reviewed — no blocking findings
The reviewed changes preserve the pipeline key’s field order and bound each write; I could not run the checks needed to justify approval.
What I ran
Read the prepared diff and changed code at head dadf5264319dc2aba1ec5c5a9f7670d3ac5e1cd9 through repo_read.py. No build or tests were run.
Results
| Check | Result | Note |
|---|---|---|
| Diff and changed code | passed | Both changed files inspected |
| Build and tests | not run | No execution performed |
| Negative control | not run | No test execution performed |
Findings
Blocking
None verified.
Non-blocking
None.
Not verified
No test or build was run. The description reports tests and performance measurements, but these were not independently verified. Pipeline.cpp and the changed lines in Graphics.cpp were read; Graphics.cpp was excluded from the prepared full-text bundle and was read through the repository reader. No other files were excluded according to the manifest.
To get this to a good state
Nothing is required.
What
Every draw resolves its pipeline through
CachedPipeline, which builds a byte key from the pipeline state: onestd::vectorinsert per field, about 600 bytes for a typical draw with 12 vertex bindings, 8 attributes and 2 colour targets. On a store hit that key construction is most of the cost.The key now goes through a writer that copies into a pre-sized thread-local buffer and bulk-copies the vertex bindings, attributes, layout words, blends and blend constants. The buffer is sized once per lookup from the state, the context and the per-item counts, which bound everything the key writes, and every write checks the room left and throws before it copies, so a key field added without its share of the capacity cannot write past the buffer. The key bytes, the hash and the stored identities are unchanged, and the
append(key, field)spelling of the call sites stays, so open PRs that add key fields rebase with at most the signature lines in conflict.On a probe that calls
CachedPipelinewith a 611-byte key (12 bindings, 8 attributes, 2 colour targets, every lookup a hit), 7 alternating runs of the parent and the change:The bounds check costs 249 instructions per key (277.4 against 276.8 ns on the lookup probe, median of 5 alternating runs). A differential run of the production key writer against the previous key builder, copied verbatim, over 150,000 random states (18,626 empty keys, keys up to 790 bytes) found no mismatch, for the final writer and for each intermediate one. At 3,000 draws per frame, all resolved through the store, the saving is about 1.7 ms of CPU per frame; no title trace was captured for this change.
agc_driver_graphicsgains a check that a key built after a longer one keeps none of the longer key's bytes.Tested
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 703 pass, 71 skip, 36 fail and 1 times out, against 704, 71, 35 and 1 onmain. The one test that differs isagc_driver_msaa_dcc_clear_2("multisampled DCC target reuse requires watched guest memory"), which is intermittent on this Mac with or without the change: run alone ten times in the same conditions, it passes 7 times onmainand 8 times with the change. 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.pyfound no open pull request that changes the pipeline store key building; the open pull requests that add key fields useappend(key, field)lines, which stay as they are.Not tested: Linux and Windows.
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)