Skip to content

perf(recompiler): bypass a source's result memo while it is not being hit - #14

Closed
savvaniss wants to merge 7 commits into
Get-fix:ps4/limbo-gpufrom
savvaniss:perf/ps4-result-memo-bypass
Closed

savvaniss wants to merge 7 commits into
Get-fix:ps4/limbo-gpufrom
savvaniss:perf/ps4-result-memo-bypass

Conversation

@savvaniss

@savvaniss savvaniss commented Oct 21, 2026 •

Copy link
Copy Markdown

What

materializePreparedMemoized keeps the last 256 results of each shader source and returns a kept one when a later capture has the same snapshot (snapshotHash: descriptors, flattened SRT, user data). Every lookup hashes the snapshot; every miss materializes, inserts the result and, once 256 are kept, destroys the oldest one. All of that runs on the queue worker, the eviction under the source's mutex.

When a title's draws change their descriptors on almost every draw, that is all cost and no reuse. On Monopoly Plus's language screen after #13, APS5_PROFILE_DRAW reports about 300,000 lookups per 10 s with 0.7% hits and as many evictions. A sampling profile of the worker put 21% of its time in materializePreparedMemoized, a third of that in destroying evicted results. With APS5_NO_RESULT_MEMO=1 the same screen ran at 27.7 fps instead of about 24.

This PR keeps the memo where it pays and stops it where it does not:

  • Each source counts its memo lookups in windows of ResultMemoWindow (256) and the hits among them.
  • A window with fewer than one hit in ResultMemoHitShare (8) lookups makes the next ResultMemoBypass (4096) materializations of that source skip the memo: no hash, no lookup, no insert, no eviction. The result is materialized as without the memo.
  • After the bypass the source looks its results up again, so a source whose snapshots start repeating gets the memo back within one window. Results kept before the bypass stay in the memo and can be hit again.
  • The memo report line counts the bypassed materializations.

Sources whose snapshots repeat (at least one hit in eight lookups) never bypass. Nothing depends on the title.

Tested

Windows 11 Pro 26300, WinLibs GCC 15.2.0 r7, Release, on boykopovar#6977's head 93eeee014 with #8, #10, #11, #12 and #13, which this branch stacks on.

  • agc_result_memo_bypass (new, no GPU): prepares, captures and materializes a compute shader whose snapshot changes with the V# in its user data.
    • A repeated snapshot returns the kept result.
    • After one window with a single hit, the same snapshot materialized twice returns two new results with the same variant, bindings and push constants.
    • After the bypass the memo returns the result kept before it.
    • A second source with seven hits in every eight lookups keeps returning its kept result for four windows.
    • It passes. With this PR's Recompiler.cpp reverted and only the new constants kept, it fails with "a source with 1 hit in 256 lookups still looked its results up and kept them".
  • Validation layer: 58 focused tests on the base (perf(agc): reuse a program's user-data callee link while the callees are unchanged #13's build, 7d2266933a) and the same 58 plus the new one here, on the RX 9070 XT (ANYPS5_GPU=AMD), with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1 and VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation. They are perf(agc): reuse a program's user-data callee link while the callees are unchanged #13's set: the shader memory, formatted buffer, vertex fetch, registration and prepared-module tests, every compute dispatch, user-data call and prepared-shader test, and agc_driver_user_data_link_reuse. All pass on both sides except agc_driver_oversized_dispatch, which skips itself on both.
    • Per test, both sides log the same VUIDs, already on the base: VUID-VkPipelineColorBlendStateCreateInfo-pAttachments-00605 (20 in agc_driver_prepared_exports) and VUID-vkDestroyDevice-device-05137 (2 in each of seven agc_driver_prepared_shaders modes). The new test logs none.
    • Every device the tests opened was the RX 9070 XT, and no log contains was not found but was requested by env var VK_INSTANCE_LAYERS.
  • tools/check_conventions.py --base 7d2266933a passes.
  • tools/pr_overlap.py --repo Get-fix/AnyPS5 --base ps4/limbo-gpu reports only fix(agc): PS4 display, textures and GCN instructions for CUSA03312 #1, on the ShaderDiskCache.cpp lines of perf(agc): bound vertex-indexed guest buffers by the draw's vertex range #8's commit.

Monopoly Plus (CUSA03312), RX 9070 XT, APS5_RESIDENT_LINEAR=1, on the local PS4 stack the title needs to start: boykopovar#6977's head with #1, #2, #5, #8, #10, #11, #12 and #13, plus local stand-ins. Base is that stack (60aac9783f), and change is the same stack with this commit cherry-picked (819cd3db26).

  • Alternating runs, APS5_TRACE_SUMMARY_EVERY=15 with a 120:15 window, both builds on the language screen:

    build fps frame p50 frame p90 GPU busy ms/present worker Driver.Packet ms/frame shader_compile_and_link ms/frame materialize ms/frame capture_resources ms/frame
    base 23.99, 24.61, 24.23 40.5 to 41.3 ms 41.8 to 43.5 ms 12.8 to 14.4 39.1 to 40.2 20.0 to 20.6 9.5 to 9.8 6.6 to 6.7
    change 27.07, 26.26, 27.28 36.6 to 37.9 ms 37.7 to 39.6 ms 13.1 to 13.9 35.1 to 36.6 15.7 to 16.6 5.2 to 5.5 6.8 to 7.2

    The language screen goes from 24.3 to 26.9 fps. materialize falls from about 9.6 to 5.4 ms per frame, close to the 4.7 ms the screen showed with the memo switched off. The GPU time does not change.

  • Summary lines of the first pair:

    • Base: {"commit":"60aac9783f36","dirty":false,"env":{"APS5_PAD_INPUT_FILE":"<path>","APS5_RESIDENT_LINEAR":"1","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"120:15","APS5_TRACE_SUMMARY_EVERY":"15"},"gpu":"AMD Radeon RX 9070 XT","driver":"AMD proprietary driver 26.8.1 (LLPC)","os":"Windows 10.0.26300","resolution":"1920x1080","window":{"spec":"seconds 120:15","first_present":3507,"ms":15004.453},"presents":360,"frame_ms":{"p50":41.262,"p90":43.536,"p99":51.933,"max":65.561},"gpu_busy_ms_per_present":14.435,"worker_busy_pct":{"queue worker 0x0":96.629},"top_phases_ms_per_frame":{"Driver.Packet":40.167,"Driver.Packet.draw":29.076,"Driver.Draw":28.770,"Driver.Draw.shader_compile_and_link":20.576,"Shader.DrawStage":18.870,"Shader.DrawStage.materialize":9.804,"Shader.DrawStage.capture_resources":6.737,"Driver.Packet.device_lock":6.392},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":3.381,"events_lost":885581,"totals_full":false}},"trace":"aps5-trace-60aac9783f36-35316.json.gz"}
    • Change: {"commit":"819cd3db2630","dirty":false,"env":{"APS5_PAD_INPUT_FILE":"<path>","APS5_RESIDENT_LINEAR":"1","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"120:15","APS5_TRACE_SUMMARY_EVERY":"15"},"gpu":"AMD Radeon RX 9070 XT","driver":"AMD proprietary driver 26.8.1 (LLPC)","os":"Windows 10.0.26300","resolution":"1920x1080","window":{"spec":"seconds 120:15","first_present":3771,"ms":15033.272},"presents":407,"frame_ms":{"p50":36.873,"p90":38.184,"p99":39.115,"max":51.218},"gpu_busy_ms_per_present":13.232,"worker_busy_pct":{"queue worker 0x0":96.237},"top_phases_ms_per_frame":{"Driver.Packet":35.441,"Driver.Packet.draw":24.430,"Driver.Draw":24.116,"Driver.Draw.shader_compile_and_link":15.989,"Shader.DrawStage":14.427,"Shader.DrawStage.capture_resources":6.891,"Driver.Packet.device_lock":6.361,"Shader.DrawStage.materialize":5.325},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":3.553,"events_lost":843514,"totals_full":false}},"trace":"aps5-trace-819cd3db2630-33756.json.gz"}
    • Both traces dropped the worker's slices partway; the summary totals cover the whole window.
  • tools/trace_digest.py of the same two traces (slices kept before the drop):

    • Base: 360 presents, frame p50 41.3 ms, GPU 14.4 ms busy per frame; queue worker busy 7.7 ms per frame. Largest slices: batch 14.4, Driver.Packet 7.7, Driver.Packet.draw 6.2, Driver.Draw.shader_compile_and_link 3.8, Driver.Packet.device_lock 1.2, Shader.DrawStage 1.1.
    • Change: 407 presents, frame p50 36.9 ms, GPU 13.2 ms busy per frame; queue worker busy 6.7 ms per frame. Largest slices: batch 13.2, Driver.Packet 6.7, Driver.Packet.draw 5.1, Driver.Draw.shader_compile_and_link 2.4, Driver.Packet.device_lock 1.3, Shader.DrawStage 0.5.
    • The traces are not attached. They stay on the test machine and can be shared.

Checklist

🤖 Generated with Claude Code

savvaniss and others added 3 commits October 20, 2026 12:09
A GCN vertex shader reaches its fetch subroutine through s_swappc. The
driver links that subroutine into the program, so its buffer loads stay
generic guest-buffer loads indexed by v0 rather than lifted vertex
attributes. Their V#s describe the whole vertex ring, so every draw bound
and, when the draw cache reused a draw at a moved base, snapshotted the
ring whole.

The resource tracker now proves, per buffer resource, that every access
is an IDXEN load whose index is the VertexIndex builtin and whose VGPR
offset and SOFFSET are immediates, and records how far past
index * stride those loads read (formatted loads count 16 bytes). The
value reaches the driver as DescriptorBinding::bufferVertexExtent (shader
disk cache format 37).

For a direct draw on the vertex path without mesh or tessellation
stages, a read-only element with that proof is bound, alias-checked and
snapshotted when its V# moves only up to maxIndex * stride plus the
extent, rounded up to 16 bytes and never past the V#'s own size. maxIndex
is the bound vertex attributes already use: the highest index plus the
base vertex, or the last vertex of an auto draw. A recipe hit whose
template was bound for another vertex range is a miss. Indirect draws,
written elements, swizzled or ADD_TID V#s and any other access keep the
whole range.

Monopoly Plus (CUSA03312) draws its quads from a 40 MB ring whose base
advances 0x60 bytes per draw. Its 0x27ffff0-byte draw snapshots are gone,
and its language screen runs at about 6.1 instead of 2.9 frames per
second on an RX 9070 XT.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…or the GPU

A COPY_DATA, DMA_DATA or DUMP_CONST_RAM store of up to 64 KiB that the
driver resolves while GPU work is open is recorded into the batch as a
vkCmdUpdateBuffer into the host import, and its range becomes a pending
write. The next draw's resource capture reads the dumped descriptor
table through the flush hook, which submits the batch and waits for the
GPU although the driver already holds the bytes.

The recorder now keeps the bytes of stores wider than the 64-byte label
table too, in record order, and flags them overwritten like table
entries when later GPU work writes over them. Recorder::LookupStoredBytes
composes a range from the newest store of each dword. It refuses a dword
without a store, one a later GPU write covers, a label behind a
completion action and a range under a queued label. Everything recorded
before the store lands first and the store lands before anything
recorded after it, so the bytes are what the next draw reads on the GPU.

classifyPendingWrite answers KnownValue for such a range when the caller
takes the bytes (captures, cached draw and dispatch validation), so the
capture reads them without the wait. Callers that pass no buffer, and
every other pending write, sync as before. APS5_NO_STORED_KNOWN_VALUES=1
turns the path off, and APS5_VERIFY_KNOWN_VALUES=1 checks each served
word against the GPU's; the [capture] profile line now reports both
counts.

Monopoly Plus (CUSA03312) dumps 144- to 512-byte descriptor tables from
constant RAM about 100 times per frame on its language screen, each
followed by a draw whose capture waited about 1 to 2 ms for the GPU.
That screen now runs at about 8.3 instead of 6.1 frames per second on
an RX 9070 XT.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A label or store into memory no host import covers made the recording
thread wait for all recorded work and write it with the CPU (boykopovar#1458).
VideoOut's buffer labels live in host memory no guest allocation
covers, so every PS4 flip label drained the device under the GPU lock:
the draw committer waited about 63 ms per frame and the queue worker
waited for the committer at the next DUMP_CONST_RAM.

Such memory is now imported for the batch that stores into it, when its
pages are committed private read-write memory of one allocation and no
guest range overlaps them. The GPU stores the label in stream order, as
the console's CP does. Memory that fails those checks or the import
keeps the wait.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
savvaniss and others added 4 commits October 21, 2026 14:07
…raining

A DMA_DATA copy into host memory that no import covers waits for the
batches that write its source (Recorder::SyncThrough) and copies on the
CPU, unless something can still store over its destination; then it
drains the device. Until the previous commit a label into such memory
was written by the CPU after a drain, so nothing was pending there. Now
that label is a GPU store in the open batch, and a copy behind a label
into its destination drained: agc_driver failed in its label case with
"a copy into host memory drained instead of syncing its source".

The copy now waits for the batches that store into its destination as
it does for its source, then checks again. Only what still stores there
after that wait (a storage image's write-back, a unit shadow, a label
another queue has not recorded yet) drains as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Registration modules read a GCN V#'s word 3 in the shader, and gcnWord3
mapped its DATA_FORMAT and NUM_FORMAT through a 128-entry Private array
that every buffer access stored again before indexing it. Drivers keep
that array in per-invocation scratch: on the RX 9070 XT each pixel shader
of Monopoly Plus's language screen used 528 bytes of scratch and wrote
the table up to 60 times per invocation.

The lookup is now a helper function that packs the table four entries
to a word and picks the word with a select tree, so it stays in
registers and each access calls it once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…are unchanged

InvocationFor linked the user-data callees of a compute shader or GCN
vertex shader on every draw and dispatch: it decoded the whole program,
searched it for s_swappc calls through user data, read each callee from
guest memory, measured it and linked the result. On Monopoly Plus's
language screen that decoding took about 20% of the queue worker.

The registered program's decoded call sites are now kept with its
prepared state, and so are its last eight links: the callee addresses,
the callee words and the linked code. A link is reused when the user
data points at the same addresses and the guest memory there still
holds the same words; otherwise the callees are read and linked as
before. Programs that load call targets from a table keep their call
sites but link every time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… hit

The result memo keeps the last 256 materialized results of each shader
source and reuses one when a later capture has the same snapshot. When a
title's draws change their descriptors every time, every lookup misses:
the snapshot is hashed, the new result inserted and the oldest one
destroyed, all on the queue worker under the source's mutex. On Monopoly
Plus's language screen 0.7% of about 300,000 lookups per 10 s hit, and
materializing took twice as long as without the memo.

Each source now counts its lookups in windows of 256. A window with
fewer than one hit in eight sends the next 4096 materializations of that
source straight to materializeResult without hashing, looking up or
inserting; the memo then probes again, so a source whose snapshots start
repeating gets it back.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@savvaniss
savvaniss force-pushed the perf/ps4-result-memo-bypass branch from c957431 to 8ff6666 Compare October 21, 2026 11:42
@Get-fix

Get-fix commented Oct 21, 2026

Copy link
Copy Markdown
Owner

Thanks @savvaniss. boykopovar#6977 is now a draft integration branch, and its own work is split into boykopovar#9429 to boykopovar#9433 (details). The maintainers asked that the branch not land anything without its own review, so could you open this upstream against main, straight on main for the changes that are not PS4-only, and on top of boykopovar#9430 or boykopovar#9431 for the rest, with Depends on? Once it's open, I'll merge it into ps4/limbo-gpu, list it as a member and run Limbo with it. ps4/limbo-gpu now moves to the split branches, so this pull request may show conflicts here. This one stacks on #13.

AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

@savvaniss

Copy link
Copy Markdown
Author

Not moving this one. On main, boykopovar#6733 replaced the result memo with a set-associative table, so the eviction churn this bypass avoided is gone. I'll measure the memo again on ps4/limbo-gpu before proposing anything. Closing.

@savvaniss savvaniss closed this Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants