loader: replace SSE4a EXTRQ with native trampolines on hosts without SSE4a #839
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!839
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/839/head"
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?
@ew-sudo on GitHub · 2026-09-25 21:11 UTC · original
ew-sudo:extrq-native-trampolines→main· +583 −150 · 4 files · 1 commits · merged 2026-10-03 15:40 UTCProblem
EXTRQ is an AMD-only SSE4a instruction. On CPUs without SSE4a (Intel), every EXTRQ raises an illegal-instruction exception and is emulated in the exception handler (
TryEmulateSse4ainx64InstructionEmulator.cpp). The result is correct but each execution costs a full exception round trip.Astro Bot (PPSA21564, 01.007) executes EXTRQ in hot code. With a temporary counter in the trap handler I measured about 810,000 EXTRQ traps per second on an Intel Core Ultra 7 155H, and the game spent its time in exception handling instead of reaching the title screen.
Fix
When the host lacks SSE4a, the red-zone patcher now also collects EXTRQ sites (both the immediate form and the XMM register form) and relocates them to a trampoline that computes the result with ordinary x86-64 instructions:
length + index > 64keeps the full mask, matching the existing trap emulator);MOVQ, which clears bits 64-127 like the trap emulator does (dest[1] = 0) and leaves the upper YMM half untouched.The generator is adapted from shadPS4's
GenerateEXTRQ(src/core/cpu_patches.cpp), the same projectredZonePatcher.cppalready comes from. The patcher logsSSE4a EXTRQ native patching: found=N, patched=Mper module. INSERTQ, MOVNTSS and MOVNTSD still go through the existing trap emulation.Testing
--redzone.found=97, patched=97; the trap counter dropped from ~810,000/s to 0 for EXTRQ, and the game reaches the title screen and gameplay.main.Notes
The native path only runs where the red-zone patcher runs (Windows, with
--redzoneor--amd-cpu). Without it, EXTRQ keeps using the trap emulator. I put it there because that patcher already has the decoding, relocation and trampoline machinery, which kept this PR small.Longer term, I don't think this belongs in the red-zone patcher. Replacing AMD-only instructions is a CPU-compatibility concern, not a red-zone one. I would suggest a separate "CPU compatibility" patching pass that:
--redzone;It could reuse the existing decoder/relocation code. I kept this PR minimal on purpose and would be happy to work on that split as a follow-up if you agree with the direction.
Related: #720 also improves Intel compatibility, but keeps EXTRQ trap-based (it protects the red zone around the trap). This PR removes the trap for EXTRQ instead.
I used Claude for this commit too, but I built and tested it myself and reviewed the code.
@coderabbitai[bot] on GitHub · 2026-09-25 21:17 UTC · original
Navigate logical layers of code changes, visualize relationships, and explore their blast radius.
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:
0e0e641a-a986-4e30-b6f1-ed1a21011a72📥 Commits
Reviewing files that changed from the base of the PR and between
497166009cand 8c4224908b758abd91cf3a3f36fbe1be1b2f5e0a.📒 Files selected for processing (1)
src/loader/redZonePatcher.cppIncluded review availability: Your plan provides up to 10 included reviews per hour; 8 remain after this review.
📝 Walkthrough
Walkthrough
The patcher now recognizes supported EXTRQ forms and generates replacement sequences to emulate them. On Windows hosts without SSE4a,
PatchGuestInstructionscollects these sites and applies the generated sequence during relocation. The sequence steps past the guest red zone, saves and restores the registers and flags it uses, and writes the result with legacy SSE2 MOVQ semantics. The patcher logs the number of sites found and the number already patched.Suggested reviewers:
nmzikPriority: ➖ Normal
Change: Bug fix
Merge Risk: ⚪ Minimal · up to
8c422No merge-blocking issue is identified; the change is ready for normal checks.
Security Architecture Review
Security architecture risk: 🔵 Low · up to
8c422The new path is limited to supported instructions on Windows hosts without SSE4a when guest patching is enabled. Review found no demonstrated new security vulnerability, but incomplete evidence about interrupted patch installation prevents a minimal-risk assessment.
Retained concerns
No architecture-level concerns identified.
Security review details
Security Blast Radius
Trust Boundaries and Controls
Resilience and Maintainability Implications
Hardening Proposals
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Comment
@coderabbitai helpto get the list of available commands.@wuboo on GitHub · 2026-10-02 19:22 UTC · original
Data point from Apple Silicon: the same problem hits macOS, because Rosetta 2 does not expose SSE4a either, so every EXTRQ traps there too.
Setup: M3 Max, macOS 26, Rosetta 2 + MoltenVK, Astro Bot (PPSA21564) on the macOS bring-up stack (#715 and related). I did not run this PR's code itself; I tested an equivalent patch (EXTRQ register and immediate forms, plus INSERTQ immediate, relocated into SSE2/GPR trampolines) on that stack.
ud2→ handler → return). The handler itself is ~0.5 µs; the rest is signal delivery.So +1 for this from the macOS side. One caveat: on current main
redZonePatcher.cpponly does real work under_WIN32(xbyak), so on macOS, where Rosetta has no SSE4a, EXTRQ would still go throughTryEmulateSse4a. If useful, I can follow up with a non-Windows variant; the trampoline I tested is written as raw bytes and does not need xbyak.@ew-sudo on GitHub · 2026-10-02 19:32 UTC · original
Yeah, I’m doing this with Xbyak since it already works with shadPS4. I just didn’t want to overcomplicate things.
@wuboo on GitHub · 2026-10-02 19:52 UTC · original
Makes sense, keeping xbyak is fine. xbyak is already linked on every platform (
xbyak::xbyakis in the main link list), so the only thing keeping the EXTRQ path off on macOS is the_WIN32guards inredZonePatcher.cpp, includingRegisterRedZonePatchModulebeing a no-op outside Windows. Once this lands, I can send a small follow-up that enables just the SSE4a patching on non-Windows hosts and test it under Rosetta on Astro Bot. If you would rather fold it in here, I'm happy to test a branch.@ew-sudo on GitHub · 2026-10-02 19:56 UTC · original
Yeah, I think a small follow-up PR would be cleaner. I don't have a Mac, so I wouldn't be able to test the macOS/Rosetta part myself. If you want to handle the non-Windows SSE4a patching in a follow-up, that works fine for me.
@Nmzik on GitHub · 2026-10-03 15:37 UTC · original
@ew-sudo Fixed. EXTRQ native trampoline works with --amd-cpu