loader: patch AMD-only instructions the host CPU lacks #1196

Closed
github-bot wants to merge 2 commits from gh-pr/1196/head into gh-pr/1196/base
Owner

@⁠akar995 on GitHub · 2026-10-08 09:15 UTC · original
akar995:perf/patch-missing-amd-instructions → main · +41 −19 · 4 files · 2 commits · closed without merging

Summary

Without --amd-cpu, AMD-only guest instructions run unpatched, and on an Intel host each execution raises #UD. Astro Bot runs SSE4a EXTRQ in hot loops: about 810,000 traps per second in Sky Garden. All of them go through the process-wide vectored exception handler lock, so the game's worker threads serialize behind it.

  • When the host lacks SSE4a, RDPID or CLWB, those instructions are patched even without --amd-cpu, and the patch is logged as Host CPU compatibility.
  • Instructions the host supports keep their native behaviour.
  • A site that cannot be patched safely keeps trapping as before instead of aborting.
  • --amd-cpu is unchanged.

Results

  • Log on the i9-9940X: Host CPU compatibility: eboot.bin patched (... EXTRQ: native=97, trapped=0, skipped=0 ...).
  • With my local branch, this removed the trap storm, roughly doubling Sky Garden fps together with my other changes at that time (4.7 to about 9).
  • Tests: no change.

Testing

Hardware and software:

  • CPU: Intel Core i9-9940X (14 cores, 28 threads)
  • GPU: AMD Radeon RX 6950 XT, driver 32.0.21045.5002 (amdvlk)
  • RAM: 32 GB
  • OS: Windows 11 Pro 10.0.26200
  • Build: Release, clang-cl, based on main at 5a880808

Tests: ctest -j 1 on this branch against unmodified main 5a880808 on the same machine. main fails 12 of 53 tests here. They were compared one by one, and each fails with the same output on this branch (listed below).

Game: Astro Bot (PPSA21567). On this machine, main renders at about 250 ms per frame and stays on the boot logos. So the stage-level effects described above were measured with this change as part of my larger local branch, not on main alone.

Tests that fail on both main and each branch, with the same output: kernel_file_system, save_data_memory, shader_recompiler_compute, graphics_pipeline_rasterization, texture_cache_layered_image, texture_cache_image_views, texture_cache_storage_sampled, compute_meta_clear_classification, texture_cache_image_overlap, texture_cache_htile_clear, buffer_cache_ranges (plus gpu_tiler, which the tiler shader PR "graphics: avoid vector spec-constant selects in tiler shaders" fixes).

AI use

I used Claude Code (Anthropic) to investigate, write and test these changes. I reviewed the diff and ran the tests above before submitting.

🤖 Generated with Claude Code

**@⁠akar995** on GitHub · 2026-10-08 09:15 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1196) `akar995:perf/patch-missing-amd-instructions` → `main` · +41 −19 · 4 files · 2 commits · closed without merging ## Summary Without `--amd-cpu`, AMD-only guest instructions run unpatched, and on an Intel host each execution raises #UD. Astro Bot runs SSE4a EXTRQ in hot loops: about 810,000 traps per second in Sky Garden. All of them go through the process-wide vectored exception handler lock, so the game's worker threads serialize behind it. - When the host lacks SSE4a, RDPID or CLWB, those instructions are patched even without `--amd-cpu`, and the patch is logged as `Host CPU compatibility`. - Instructions the host supports keep their native behaviour. - A site that cannot be patched safely keeps trapping as before instead of aborting. - `--amd-cpu` is unchanged. ## Results - Log on the i9-9940X: `Host CPU compatibility: eboot.bin patched (... EXTRQ: native=97, trapped=0, skipped=0 ...)`. - With my local branch, this removed the trap storm, roughly doubling Sky Garden fps together with my other changes at that time (4.7 to about 9). - Tests: no change. ## Testing Hardware and software: - CPU: Intel Core i9-9940X (14 cores, 28 threads) - GPU: AMD Radeon RX 6950 XT, driver 32.0.21045.5002 (amdvlk) - RAM: 32 GB - OS: Windows 11 Pro 10.0.26200 - Build: Release, clang-cl, based on main at 5a880808 Tests: `ctest -j 1` on this branch against unmodified main 5a880808 on the same machine. main fails 12 of 53 tests here. They were compared one by one, and each fails with the same output on this branch (listed below). Game: Astro Bot (PPSA21567). On this machine, main renders at about 250 ms per frame and stays on the boot logos. So the stage-level effects described above were measured with this change as part of my larger local branch, not on main alone. Tests that fail on both main and each branch, with the same output: kernel_file_system, save_data_memory, shader_recompiler_compute, graphics_pipeline_rasterization, texture_cache_layered_image, texture_cache_image_views, texture_cache_storage_sampled, compute_meta_clear_classification, texture_cache_image_overlap, texture_cache_htile_clear, buffer_cache_ranges (plus gpu_tiler, which the tiler shader PR "graphics: avoid vector spec-constant selects in tiler shaders" fixes). ## AI use I used Claude Code (Anthropic) to investigate, write and test these changes. I reviewed the diff and ran the tests above before submitting. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Without --amd-cpu, guest code that uses AMD-only instructions runs
unpatched, and on an Intel host every execution of one raises #UD.
Astro Bot (PPSA21567) runs SSE4a EXTRQ in hot loops: in its Sky Garden
stage that was about 810,000 traps per second. Each trap goes through
the process-wide vectored exception handler lock, so the game's worker
threads serialize behind it.

When the host lacks SSE4a, RDPID or CLWB, patch those instructions even
without --amd-cpu, and log it as "Host CPU compatibility". Instructions
the host supports (VRSQRTPS) keep their native behaviour, and a site
that cannot be patched safely keeps trapping as before instead of
aborting. --amd-cpu behaves as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Report modules without function boundaries whenever guest instructions
are patched, as main does, and label the warning Host CPU compatibility
when only the instructions the host lacks are patched.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:47:19 +00:00
github-bot closed this pull request 2026-11-26 09:47:19 +00:00
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-08 09:17 UTC · original · edited 2026-10-08 10:01 UTC

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4e765553-f5db-430c-8292-9b2c0bbc1708
📥 Commits

Reviewing files that changed from the base of the PR and between 1b72db28f3 and 46b25d9040.

📒 Files selected for processing (1)
  • src/loader/runtimeLinker.cpp

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 1 remain after this review.


📝 Walkthrough

Walkthrough

The instruction patcher adds a host-missing-only mode for AMD instruction selection and trap handling. The runtime linker enables AMD instruction patching when emulation is configured or when the host lacks SSE4A, RDPID, or CLWB. It passes host feature information and the missing-feature mode to the patcher. Patch totals and report labels now account for host-feature-driven patching. The --amd-cpu help text describes this behavior.

Suggested reviewers: nmzik

Priority: ➖ Normal

Change: Bug fix

Merge Risk: ⚪ Minimal · up to 46b25

Host-missing instructions are patched selectively, while sites that cannot be safely patched retain their trap behavior. No identified issue blocks merging.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 5 functions across 4 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: patching AMD-only guest instructions that the host CPU does not support.
Description check ✅ Passed The description directly explains host-missing instruction patching, native behavior for supported instructions, trapping for unsafe sites, logging, results, and testing.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Comment @coderabbitai help to get the list of available commands.

**@⁠coderabbitai[bot]** on GitHub · 2026-10-08 09:17 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1196#issuecomment-6056708337) · edited 2026-10-08 10:01 UTC <!-- This is an auto-generated comment: summarize by coderabbit.ai --> <!-- review_stack_entry_start --> <a href="https://app.coderabbit.ai/change-stack/KytyPS5/KytyPS5/pull/1196?scope=ghh_OusAJGA4HG8ljoOV0wJvVx8NmnZIjc2DdjmxOu0xZWA&amp;cs_source=review_comment"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui-dark.svg?v=2" alt="Review in Change Stack →" width="220" height="32"></a> <!-- review_stack_entry_end --> <!-- recent_review_start --> No actionable comments were generated in the recent review. 🎉 <details> <summary>ℹ️ Recent review info</summary> <details> <summary>⚙️ Run configuration</summary> - **Configuration used**: Repository: KytyPS5/KytyPS5/.coderabbit.yaml - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `4e765553-f5db-430c-8292-9b2c0bbc1708` </details> <details> <summary>📥 Commits</summary> Reviewing files that changed from the base of the PR and between 1b72db28f35d03e23c0f2676b7d364d106ddf31c and 46b25d90405cf82a32f990e1b63decdf2040304e. </details> <details> <summary>📒 Files selected for processing (1)</summary> * `src/loader/runtimeLinker.cpp` </details> **Included review availability:** This review used your included allowance. Your plan provides up to 8 included reviews per hour; 1 remain after this review. </details> --- <!-- recent_review_end --> <!-- walkthrough_start --> <details> <summary>📝 Walkthrough</summary> ## Walkthrough The instruction patcher adds a host-missing-only mode for AMD instruction selection and trap handling. The runtime linker enables AMD instruction patching when emulation is configured or when the host lacks SSE4A, RDPID, or CLWB. It passes host feature information and the missing-feature mode to the patcher. Patch totals and report labels now account for host-feature-driven patching. The `--amd-cpu` help text describes this behavior. **Suggested reviewers:** `nmzik` </details> <!-- walkthrough_end --> <!-- change_assessment_start --> **Priority:** ➖ Normal <!-- change_assessment_commit:"46b25d90405cf82a32f990e1b63decdf2040304e" --> **Change:** Bug fix <!-- change_assessment_end --> <!-- final_review_risk_start --> **Merge Risk:** **⚪ Minimal** · up to `46b25` <!-- final_review_risk_coverage:{"sourceCommitId":"46b25d90405cf82a32f990e1b63decdf2040304e","coveredCommitId":"46b25d90405cf82a32f990e1b63decdf2040304e","kind":"reviewed"} --> Host-missing instructions are patched selectively, while sites that cannot be safely patched retain their trap behavior. No identified issue blocks merging. <!-- final_review_risk_end --> <!-- pre_merge_checks_walkthrough_start --> <details> <summary>🚥 Pre-merge checks | ✅ 4 | ❌ 1</summary> ### ❌ Failed checks (1 warning) | Check name | Status | Explanation | Resolution | | :---------------- | :--------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------- | | Docstring Coverage | ⚠️ Warning | Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 5 functions across 4 files. | Write docstrings for the functions missing them to satisfy the coverage threshold. | <details> <summary>✅ Passed checks (4 passed)</summary> | Check name | Status | Explanation | | :------------------------ | :------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | Title check | ✅ Passed | The title clearly summarizes the main change: patching AMD-only guest instructions that the host CPU does not support. | | Description check | ✅ Passed | The description directly explains host-missing instruction patching, native behavior for supported instructions, trapping for unsafe sites, logging, results, and testing. | | Linked Issues check | ✅ Passed | Check skipped because no linked issues were found for this pull request. | | Out of Scope Changes check | ✅ Passed | Check skipped because no linked issues were found for this pull request. | </details> </details> <!-- pre_merge_checks_walkthrough_end --> <!-- autopilot:start --> - [ ] <!-- {"checkboxId":"2708ad07-9f24-4260-9c11-7dc76a49f2e3"} --> <strong title="Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts">Autopilot</strong> · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts <!-- autopilot:end --> <!-- tips_start --> --- Thanks for using [CodeRabbit](https://coderabbit.ai?utm_source=oss&utm_medium=github&utm_campaign=KytyPS5/KytyPS5&utm_content=1196)! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. <details> <summary>❤️ Share</summary> - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code) </details> <sub>Comment `@coderabbitai help` to get the list of available commands.</sub> <!-- tips_end -->
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-08 09:17 UTC · original · reviewed

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 2b68f2eb-f2dc-4f35-9832-969d0efd98c4
📥 Commits

Reviewing files that changed from the base of the PR and between 5a8808086a and 1b72db28f3.

📒 Files selected for processing (4)
  • src/loader/guestInstructionPatcher.cpp
  • src/loader/guestInstructionPatcher.h
  • src/loader/runtimeLinker.cpp
  • src/main.cpp

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review.

**@⁠coderabbitai[bot]** on GitHub · 2026-10-08 09:17 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1196#pullrequestreview-5454374776) · **reviewed** **Actionable comments posted: 1** --- <details> <summary>ℹ️ Review info</summary> <details> <summary>⚙️ Run configuration</summary> - **Configuration used**: Repository: KytyPS5/KytyPS5/.coderabbit.yaml - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `2b68f2eb-f2dc-4f35-9832-969d0efd98c4` </details> <details> <summary>📥 Commits</summary> Reviewing files that changed from the base of the PR and between 5a8808086a9815b67d4822cd20cf1e8a7a05f002 and 1b72db28f35d03e23c0f2676b7d364d106ddf31c. </details> <details> <summary>📒 Files selected for processing (4)</summary> * `src/loader/guestInstructionPatcher.cpp` * `src/loader/guestInstructionPatcher.h` * `src/loader/runtimeLinker.cpp` * `src/main.cpp` </details> **Included review availability:** This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review. </details> <!-- This is an auto-generated comment by CodeRabbit for review status --> <!-- coderabbit-review-publication v1 publication=835c1a50-58a1-45a7-84a8-dafd9496dea3 attempt=129e9584-944d-4f28-9308-ddb4ef3fda0d batch=1/1 -->
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-08 09:17 UTC · original · edited 2026-10-08 09:54 UTC
On src/loader/runtimeLinker.cpp line 1800 (outdated):

@@ -1791,7 +1797,7 @@ void RuntimeLinker::LoadProgramToMemory(Program* program) {
 		                                &function_starts) &&
 		    !function_starts.empty();
 		const auto module_name = Common::PathToString(program->file_name.filename());
-		if (!have_function_starts) {
+		if (!have_function_starts && (emulate_amd || protect_memory_faults)) {

🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

Report unavailable function boundaries in host-missing mode.

When patch_host_missing is true and function boundaries are unavailable, PatchGuestInstructions returns without patching. This condition suppresses the warning, and no later patch report appears. Include patch_host_missing in the condition and label the warning Host CPU compatibility.

✅ Addressed in commit 46b25d9

**@⁠coderabbitai[bot]** on GitHub · 2026-10-08 09:17 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1196#discussion_r4217141412) · edited 2026-10-08 09:54 UTC On `src/loader/runtimeLinker.cpp` line 1800 (outdated): ```diff @@ -1791,7 +1797,7 @@ void RuntimeLinker::LoadProgramToMemory(Program* program) { &function_starts) && !function_starts.empty(); const auto module_name = Common::PathToString(program->file_name.filename()); - if (!have_function_starts) { + if (!have_function_starts && (emulate_amd || protect_memory_faults)) { ``` **🩺 Stability & Availability** | **🟡 Minor** | **⚡ Quick win** **Report unavailable function boundaries in host-missing mode.** When `patch_host_missing` is true and function boundaries are unavailable, `PatchGuestInstructions` returns without patching. This condition suppresses the warning, and no later patch report appears. Include `patch_host_missing` in the condition and label the warning `Host CPU compatibility`. <!-- fingerprinting:phantom:poseidon:wombat --> <!-- cr-indicator-types:potential_issue --> <!-- cr-comment:v1:2e4b2b9506293b73aad9d0be --> <!-- This is an auto-generated comment by CodeRabbit --> ✅ Addressed in commit 46b25d9
Author
Owner

@⁠Nmzik on GitHub · 2026-10-08 15:40 UTC · original

thats why we need --amd-cpu patch. Otherwise we will patch each game even if it doesnt use any of these instructions (it takes some boot time)

**@⁠Nmzik** on GitHub · 2026-10-08 15:40 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1196#issuecomment-6063533383) thats why we need --amd-cpu patch. Otherwise we will patch each game even if it doesnt use any of these instructions (it takes some boot time)

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.