Loader: patch a module's functions faster at load #2260
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!2260
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2260"
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?
@jgoyvaerts on GitHub · 2026-11-13 19:10 UTC · original
jgoyvaerts:port/fast-patch→main· +518 −20 · 4 files · 1 commits · openFaster AMD instruction patch at load (also with
--guest-tsc):Modules with at least 1024 functions are scanned on one thread per CPU through byte filters of the rewritten instruction forms. Those are VRSQRTPS xmm, xmm, EXTRQ/INSERTQ, RDPID, CLWB, and RDTSC/RDTSCP when a guest TSC rate is set. The workers decode only the candidates, at most 64 ahead, and the loading thread patches them in address order as before, so the patched code is the same. A function whose jump table lies outside its own bytes is decoded again at its turn on the loading thread.
Astro Bot (252 MB eboot.bin, 312697 functions): ~7-9 s of patching becomes ~1-2 s. Measured with the same change in a Jetsku-based build: identical patched code and trampolines (hash) and counts.
Tested:
guest_instruction_patch_scan_tests(new) compares code, trampolines and counters with a single-threaded pass at 2, 5 and 16 threads. It runs AMD only, AMD with RDTSC, RDTSC only, and red-zone protection, with a switch whose jump table lies in the next function. It also checks the filter on the rewritten encodings and look-alikes.AI use: the code, tests and this description were written with an LLM (Claude). The tests listed ran on a Linux x86-64 build.
🤖 Generated with Claude Code
@coderabbitai[bot] on GitHub · 2026-11-13 19:17 UTC · original · edited 2026-11-13 19:35 UTC
Walkthrough
The patcher adds an AMD-instruction byte filter and a configurable scan-thread count. It scans function candidates serially or in parallel, then decodes and patches candidates in address order. It tracks jump-table reads outside a function and re-analyzes affected functions before patching. New tests compare patched code, trampolines, and counters across thread counts and patching configurations.
Suggested reviewers:
nmzik,thecruzChange: Refactor
Merge Risk:
Moderate · up to
6f0a8The faster parallel patching at load time should reduce load times. An unsynchronized read of another function's bytes during jump-table resolution may still overlap with patch writes. Resolve or explicitly accept that race before merging. The red-zone test fixture should also be strengthened so it actually exercises a protected memory rewrite.
Pre-merge checks:
4
Full details: Title check
Explanation
The title names the main change: the loader now patches a module's functions faster at load time. The changeset adds parallel scanning for modules with at least 1,024 functions, and the objectives describe the same goal. The title is specific and accurate.
Full details: Description check
Explanation
The description states the purpose of the change: faster AMD instruction patching at load through parallel byte-filter scanning for modules with at least 1024 functions. It lists the rewritten instruction forms, the 64-candidate decode window, the in-order patching on the loading thread, the performance measurements (Astro Bot, about 7-9 s to 1-2 s), and the new guest_instruction_patch_scan_tests with its configurations. This matches the raw summary of CMakeLists.txt, guestInstructionPatcher.cpp/.h, and the new test file. The description is related to the changeset and is not off-topic.
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Share
@ -1604,0 +1809,4 @@auto function = std::make_unique<DecodedSites>();function->worker = worker;analyze(candidates[candidate_index], function->counts, function->function,function->rewrite_sites);@coderabbitai[bot] on GitHub · 2026-11-13 19:17 UTC · original · edited 2026-11-13 19:37 UTC
Keep jump-table probes away from bytes that the patch pipeline can rewrite.
ResolveBoundedJumpTablereads each table candidate across the whole segment. The read is bounded only by the segment, not the function. A candidate can point into another function. The worker that decodes that function can run up to 64 functions ahead of the loading thread. The loading thread can rewrite those bytes in the meantime.decoded_mutexprotects only the pipeline state, not the guest bytes.A probe that reads rewritten bytes races with the write. The probe can also see different bytes from the single-threaded pass, which has already patched them. The resolved targets, and therefore the decoded instructions and rewrite set, can then differ from run to run. The comment at lines 1702-1705 assumes that scan and decode see the same bytes. These probe reads break that assumption.
Decide how the probe handles bytes that another function's rewrite can change. Either limit the probe to bytes that no rewrite can change, or make the decode of such a candidate wait until the function that owns those bytes is patched. Both options change which tables resolve or how the workers are scheduled, so the chosen option needs a decision.
Source: Learnings
@coderabbitai[bot] on GitHub · 2026-11-13 19:17 UTC · original · reviewed
Actionable comments posted: 1
99054c7e-98c0-47b9-b036-cda7a2f68fc7Reviewing files that changed from the base of the PR and between
58ca8d1d7fand 91ea604becb3d0103172621beb63ea6ebe35cae2.CMakeLists.txtsrc/loader/guestInstructionPatcher.cppsrc/loader/guestInstructionPatcher.htests/GuestInstructionPatchScanTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
@ -1604,0 +1809,4 @@auto function = std::make_unique<DecodedSites>();function->worker = worker;analyze(candidates[candidate_index], function->counts, function->function,function->rewrite_sites);@jgoyvaerts on GitHub · 2026-11-13 19:28 UTC · original
Agreed, fixed in
6f0a88fc.DecodeFunctionnow marks a function whose jump-table read leaves its own bytes (reads_outside_function). The loading thread decodes such a function again at its turn, so it sees exactly the bytes a single-threaded pass sees; with red-zone protection the scan keeps it as a candidate instead of trusting the early decode. The test fixture adds a switch whose table lies in the next function and whose VRSQRTPS is reachable only through the table; dropping the re-decode's patch fails 27 checks.@jgoyvaerts on GitHub · 2026-11-13 19:29 UTC · original
Nitpick addressed in
6f0a88fc:guest_instruction_patch_scan_testsadds a red-zone protection mode (protect_memory=true) with a function that stores and loads in the red zone. It compares code, trampolines and counters with the serial pass at 2, 5 and 16 threads.@coderabbitai[bot] on GitHub · 2026-11-13 19:33 UTC · original · reviewed
db7cced1-697a-4f35-b048-d4b3203133a3Reviewing files that changed from the base of the PR and between 91ea604becb3d0103172621beb63ea6ebe35cae2 and
6f0a88fc2c.src/loader/guestInstructionPatcher.cpptests/GuestInstructionPatchScanTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 5 remain after this review.
@ -1604,0 +1809,4 @@auto function = std::make_unique<DecodedSites>();function->worker = worker;analyze(candidates[candidate_index], function->counts, function->function,function->rewrite_sites);@coderabbitai[bot] on GitHub · 2026-11-13 19:38 UTC · original
Analysis chain
Script executed:
Length of output: 15594
@jgoyvaertsThanks for the change. The ordered re-decode addresses stale results, but the original data race remains.At Line 491, a worker still reads an external table before the loading thread checks
reads_outside_function. If that table points into an earlier function while the loading thread patches those bytes, the read and write can overlap. A later re-decode cannot undo that race.Please defer external table reads until the loading thread handles the candidate, or synchronize those reads with patch writes. The new fixture places the table in the next function; it does not cover this earlier-function overlap. A regression test under ThreadSanitizer would help check it.
I’m not marking the finding as addressed.
If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!
You are interacting with an AI system.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.