perf(loader): binary-patch immediate SSE4a extrq/insertq to native stubs (D3D12-LOADER-PATCH-SSE4A) #3375
Open
Bilalatss wants to merge 1 commit from
upstream-pr/3375 into main
pull from: upstream-pr/3375
merge into: ProjectLeonida:main
ProjectLeonida:main
ProjectLeonida:claude/loop-total
ProjectLeonida:claude/pops-collision-seed
ProjectLeonida:claude/dstorage-hold-page-reads
ProjectLeonida:claude/host-image-copy-switch
ProjectLeonida:feat/gdk-lifecycle-window-hook
ProjectLeonida:claude/d16s8-uav-nvidia-fallback
ProjectLeonida:feat/mfplat-offline-shim
ProjectLeonida:claude/gameinput-slow-reader
ProjectLeonida:fix/rt-null-barrier-clear
ProjectLeonida:claude/depth-resolve-sample-positions
ProjectLeonida:integration/palmetto-parity
ProjectLeonida:claude/valu-literal-sext
ProjectLeonida:claude/slot74-pool-handle
ProjectLeonida:claude/rgb9e5-uint-alias-view
ProjectLeonida:claude/fssm-root-layout-tables
ProjectLeonida:feat/gdk-speech-display-lifecycle
ProjectLeonida:asset-audio-skip-current
ProjectLeonida:qemmhd/old-9940-rectangle-quad-lists
ProjectLeonida:feat/ws2-offline-shim
ProjectLeonida:qemmhd/rt-d16-unorm-uav
ProjectLeonida:feat/xs-device-stops-7-9
ProjectLeonida:qemmhd/rt-depth-same-base-shapes
ProjectLeonida:qemmhd/rt-lazy-attachment-variant
ProjectLeonida:feat/acphal-xma2-decoder
ProjectLeonida:asset-audio-skip
ProjectLeonida:codex/coord-claim-check-20261124-7e48
ProjectLeonida:feat/xs-frame-pacing-presentx
ProjectLeonida:feat/xs-device-vm-options
ProjectLeonida:old-pr/9844
ProjectLeonida:old-pr/9978
ProjectLeonida:old-pr/9815
ProjectLeonida:old-pr/10057
ProjectLeonida:old-pr/9874
ProjectLeonida:old-pr/9965
ProjectLeonida:old-pr/9863
ProjectLeonida:old-pr/10068
ProjectLeonida:old-pr/10066
ProjectLeonida:old-pr/10067
ProjectLeonida:old-pr/10007
ProjectLeonida:old-pr/10061
ProjectLeonida:old-pr/10065
ProjectLeonida:old-pr/10059
ProjectLeonida:old-pr/10063
ProjectLeonida:old-pr/10055
ProjectLeonida:old-pr/10062
ProjectLeonida:old-pr/10042
ProjectLeonida:old-pr/10041
ProjectLeonida:old-pr/10049
ProjectLeonida:old-pr/10047
ProjectLeonida:old-pr/10046
ProjectLeonida:old-pr/10045
ProjectLeonida:old-pr/10056
ProjectLeonida:old-pr/10043
ProjectLeonida:old-pr/10036
ProjectLeonida:old-pr/10034
ProjectLeonida:old-pr/9984
ProjectLeonida:old-pr/10052
ProjectLeonida:old-pr/10038
ProjectLeonida:old-pr/10033
ProjectLeonida:old-pr/10028
ProjectLeonida:old-pr/10050
ProjectLeonida:old-pr/9891
ProjectLeonida:old-pr/9889
ProjectLeonida:old-pr/10051
ProjectLeonida:old-pr/10019
ProjectLeonida:old-pr/10027
ProjectLeonida:old-pr/10029
ProjectLeonida:old-pr/10031
ProjectLeonida:old-pr/10026
ProjectLeonida:old-pr/9941
ProjectLeonida:old-pr/10030
ProjectLeonida:old-pr/10040
ProjectLeonida:old-pr/9990
ProjectLeonida:old-pr/10021
ProjectLeonida:old-pr/9962
ProjectLeonida:old-pr/10022
ProjectLeonida:old-pr/10020
ProjectLeonida:old-pr/10015
ProjectLeonida:old-pr/10025
ProjectLeonida:old-pr/9886
ProjectLeonida:old-pr/10048
ProjectLeonida:old-pr/10014
ProjectLeonida:old-pr/10011
ProjectLeonida:old-pr/9861
ProjectLeonida:old-pr/10010
ProjectLeonida:old-pr/10008
ProjectLeonida:old-pr/9860
ProjectLeonida:old-pr/10006
ProjectLeonida:old-pr/9920
ProjectLeonida:old-pr/9995
ProjectLeonida:old-pr/10000
ProjectLeonida:old-pr/10016
ProjectLeonida:old-pr/10004
ProjectLeonida:old-pr/10023
ProjectLeonida:old-pr/9982
ProjectLeonida:old-pr/9981
ProjectLeonida:old-pr/9983
ProjectLeonida:old-pr/9970
ProjectLeonida:old-pr/9969
ProjectLeonida:old-pr/9968
ProjectLeonida:old-pr/9971
ProjectLeonida:old-pr/9972
ProjectLeonida:old-pr/9942
ProjectLeonida:old-pr/9963
ProjectLeonida:old-pr/9960
ProjectLeonida:old-pr/9964
ProjectLeonida:old-pr/9985
ProjectLeonida:old-pr/9973
ProjectLeonida:old-pr/9862
ProjectLeonida:old-pr/9953
ProjectLeonida:old-pr/10017
ProjectLeonida:old-pr/9952
ProjectLeonida:old-pr/9955
ProjectLeonida:old-pr/9946
ProjectLeonida:old-pr/9954
ProjectLeonida:old-pr/9938
ProjectLeonida:old-pr/9956
ProjectLeonida:old-pr/9856
ProjectLeonida:old-pr/9947
ProjectLeonida:old-pr/9924
ProjectLeonida:old-pr/9961
ProjectLeonida:old-pr/9927
ProjectLeonida:old-pr/9979
ProjectLeonida:old-pr/10032
ProjectLeonida:old-pr/9926
ProjectLeonida:old-pr/5237
ProjectLeonida:old-pr/9951
ProjectLeonida:old-pr/9931
ProjectLeonida:old-pr/9902
ProjectLeonida:old-pr/9921
ProjectLeonida:old-pr/9994
ProjectLeonida:old-pr/9775
ProjectLeonida:old-pr/9768
ProjectLeonida:old-pr/9905
ProjectLeonida:old-pr/9936
ProjectLeonida:old-pr/9901
ProjectLeonida:old-pr/9904
ProjectLeonida:old-pr/9906
ProjectLeonida:old-pr/9988
ProjectLeonida:old-pr/9908
ProjectLeonida:old-pr/9909
ProjectLeonida:old-pr/9911
ProjectLeonida:old-pr/9912
ProjectLeonida:old-pr/9913
ProjectLeonida:old-pr/9887
ProjectLeonida:old-pr/9916
ProjectLeonida:old-pr/9919
ProjectLeonida:old-pr/9925
ProjectLeonida:old-pr/9933
ProjectLeonida:old-pr/9935
ProjectLeonida:old-pr/9918
ProjectLeonida:old-pr/9879
ProjectLeonida:old-pr/9880
ProjectLeonida:old-pr/9881
ProjectLeonida:old-pr/9996
ProjectLeonida:old-pr/9876
ProjectLeonida:old-pr/9892
ProjectLeonida:old-pr/9899
ProjectLeonida:old-pr/9915
ProjectLeonida:old-pr/9914
ProjectLeonida:old-pr/9872
ProjectLeonida:old-pr/9869
ProjectLeonida:old-pr/9866
ProjectLeonida:old-pr/9819
ProjectLeonida:old-pr/10012
ProjectLeonida:old-pr/9857
ProjectLeonida:old-pr/9893
ProjectLeonida:old-pr/9897
ProjectLeonida:old-pr/9928
ProjectLeonida:old-pr/9868
ProjectLeonida:old-pr/9917
ProjectLeonida:old-pr/9940
ProjectLeonida:old-pr/9873
ProjectLeonida:old-pr/9816
ProjectLeonida:old-pr/10009
ProjectLeonida:old-pr/9900
ProjectLeonida:old-pr/9966
ProjectLeonida:old-pr/9859
ProjectLeonida:old-pr/9843
ProjectLeonida:old-pr/9991
ProjectLeonida:old-pr/10058
ProjectLeonida:old-pr/9883
ProjectLeonida:old-pr/9838
ProjectLeonida:old-pr/9835
ProjectLeonida:old-pr/9959
ProjectLeonida:old-pr/9895
ProjectLeonida:old-pr/9837
ProjectLeonida:old-pr/9858
ProjectLeonida:old-pr/9865
ProjectLeonida:old-pr/9864
ProjectLeonida:old-pr/9840
ProjectLeonida:old-pr/9820
ProjectLeonida:old-pr/4669
ProjectLeonida:old-pr/9848
ProjectLeonida:old-pr/9850
ProjectLeonida:old-pr/9810
ProjectLeonida:old-pr/9799
ProjectLeonida:old-pr/9853
ProjectLeonida:old-pr/9875
ProjectLeonida:old-pr/9831
ProjectLeonida:old-pr/9796
ProjectLeonida:old-pr/9801
ProjectLeonida:old-pr/9813
ProjectLeonida:old-pr/9794
ProjectLeonida:old-pr/7417
ProjectLeonida:old-pr/9781
ProjectLeonida:old-pr/9806
ProjectLeonida:old-pr/9958
ProjectLeonida:old-pr/9809
ProjectLeonida:old-pr/9791
ProjectLeonida:old-pr/9792
ProjectLeonida:old-pr/10053
ProjectLeonida:old-pr/9784
ProjectLeonida:old-pr/9789
ProjectLeonida:old-pr/9849
ProjectLeonida:old-pr/9846
ProjectLeonida:old-pr/9773
ProjectLeonida:old-pr/9785
ProjectLeonida:old-pr/9803
ProjectLeonida:old-pr/9826
ProjectLeonida:old-pr/9839
ProjectLeonida:old-pr/9922
ProjectLeonida:old-pr/9948
ProjectLeonida:old-pr/7189
ProjectLeonida:old-pr/9957
ProjectLeonida:old-pr/9823
ProjectLeonida:old-pr/9807
ProjectLeonida:old-pr/9851
ProjectLeonida:old-pr/9774
ProjectLeonida:old-pr/9830
ProjectLeonida:old-pr/9836
ProjectLeonida:old-pr/9822
ProjectLeonida:old-pr/9783
ProjectLeonida:old-pr/9833
ProjectLeonida:old-pr/9890
ProjectLeonida:old-pr/9770
ProjectLeonida:old-pr/9769
ProjectLeonida:old-pr/9772
ProjectLeonida:old-pr/9787
ProjectLeonida:old-pr/9852
ProjectLeonida:old-pr/9834
ProjectLeonida:old-pr/9845
ProjectLeonida:old-pr/9871
ProjectLeonida:old-pr/9771
ProjectLeonida:old-pr/9776
ProjectLeonida:old-pr/10005
ProjectLeonida:old-pr/9949
ProjectLeonida:old-pr/9923
ProjectLeonida:old-pr/9805
ProjectLeonida:old-pr/9829
ProjectLeonida:old-pr/9790
ProjectLeonida:old-pr/9827
ProjectLeonida:old-pr/9929
ProjectLeonida:old-pr/9855
ProjectLeonida:old-pr/9934
ProjectLeonida:old-pr/9907
ProjectLeonida:old-pr/9910
ProjectLeonida:old-pr/9779
ProjectLeonida:old-pr/9780
ProjectLeonida:old-pr/9885
ProjectLeonida:old-pr/9882
ProjectLeonida:old-pr/9877
ProjectLeonida:old-pr/9998
ProjectLeonida:old-pr/4251
ProjectLeonida:old-pr/9987
ProjectLeonida:old-pr/9989
ProjectLeonida:old-pr/9950
ProjectLeonida:old-pr/9777
ProjectLeonida:old-pr/10044
ProjectLeonida:old-pr/9884
ProjectLeonida:old-pr/2906
ProjectLeonida:old-pr/5076
ProjectLeonida:old-pr/9937
ProjectLeonida:old-pr/9986
ProjectLeonida:old-pr/3014
ProjectLeonida:old-pr/9943
ProjectLeonida:old-pr/9939
ProjectLeonida:old-pr/4682
ProjectLeonida:old-pr/4765
No reviewers
Labels
Clear labels
No items
No labels
Milestone
Clear milestone
No items
No milestone
Assignees
Clear assignees
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".
No due date set.
Reference
ProjectLeonida/mandy!3375
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3375"
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?
Imported from ubiquitarianism/leonida. Originally posted by Bilalatss on 2026-12-03T17:44:02Z.
Claimed in the coordination issue as: D3D12-LOADER-PATCH-SSE4A (https://github.com/ProjectLeonida/mandy/issues/3312#issuecomment-7386960124)
What and why
leonida-loader::console_insnalready binary-patches wait instructions (monitorx/mwaitx) andclzeroloops after their first#UDtrap, but still emulated immediate-form SSE4aextrq(66 0F 78) andinsertq(F2 0F 78) on every execution. On CPUs lacking AMD SSE4a (such as Intel), hot immediateextrq/insertqsites incur a Windows kernel/VEH exception round-trip on every hit and contend on the VEH lock withwatch's page-guard handler.This change:
SSE4A_STUBS(Vec<(usize, usize)>) andalloc_sse4a_stub(withFlushInstructionCacheand bidirectionaljmp rel32reach checks), leavingpatch_clzeroandSTUBSuntouched so there is zero overlap withD3D12-CLZERO-STUB-REGIONS.sse4a_stub,patch_slice, andpatch_sse4ato atomically patch 5..=16 byte immediateextrq/insertqinstructions inside a single 16-byte aligned block viacmpxchg16btojmp rel32+ trailing NOPs, using flag-free SSE2 / SSE4.1-gated fast paths and a flag-preserving general bit-field path matchingdecode::extrq/decode::insertq(including upper-64-bit zeroing andxmm0..xmm15).[u8; 16]stack buffer inemulate(avoiding&[u8]aliasing againstcmpxchg16b) and guards concurrent#UDfaults at the same site (compiler_fence(SeqCst)+patched_jump_to_sse4a_stub+PatchOutcome::AlreadyPatched) so racing threads resume cleanly atripinstead of crashing or misdecoding torn bytes across an 8-byte boundary.LEONIDA_PATCH_SSE4A=0opt-out (matchingLEONIDA_PATCH_CLZERO=0andLEONIDA_PATCH_WAITS=0) and regeneratesd3d12/docs/switches.mdwithtools/d3d12_switches.py.Tested
python -I scripts/check_no_game_data.py— passed (exit 0).python -I tests/test_d3d12_switches.py— 21/21 passed.python -I scripts/red_green.py perf/loader-patch-sse4a --base origin/main --no-fetch— verdict:red then green(exit 0):leonida_loader::console_insn::tests::sse4a_stubs_match_emulation_and_preserve_flags(red -> green; live-executes 21 cases acrossxmm0..xmm15,len=8/16/32/64,len=0withidx>0,fidx+flen==64, bit-63 crossing,dst==src,len/idx >= 64, EFLAGS and source XMM preservation, concurrent re-entry at a patched site, plus straddling-16B and 4-byte register-form unpatched edges)pass on both: 94(0 failures).cargo clippy -p leonida-loader --all-targets --no-deps --locked -- -D warnings— passed (0 warnings).cargo clippy -p leonida-loader --features diagnostics --all-targets --no-deps --locked -- -D warnings— passed (0 warnings).cargo build --workspace --all-targets --locked— passed.cargo build -p leonida --features diagnostics,mimalloc --locked— passed.cargo test -p leonida-loader --locked -- --test-threads=1— 95/95 passed (92 unit + 2load_probe+ 1title_dlls).cargo test -p leonida-loader --features diagnostics --locked -- --test-threads=1— 98/98 passed (95 unit + 2load_probe+ 1title_dlls).cargo test -p leonida-gpu --features diagnostics --test replay --locked— 1/1 passed (a_frame_is_recorded_and_replayed).GPUs tested: Profile M (desktop, 16 GB VRAM), NVIDIA GeForce RTX 5070 Ti, driver 621.47 (32.0.16.2147), Windows 11 Pro (10.0.26200) (for
replay;leonida-loadertests are CPU-only)AI-assisted: yes (authored with Gemini 4 Neon; audited by an independent read-only reviewer subagent using Gemini 4 Neon)
Checklist
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed. (n/a — loader-only change)Imported from ubiquitarianism/leonida. Originally posted by Kal0s-design on 2026-12-03T17:59:08Z.
Ran natively on Windows 11, head
88c4db792merged into currentmain(0f0f268de, clean merge).The stubs are bit-exact. They matched
decode::extrq/decode::insertqin every case I ran, about 1.03 million, and on this machine they also matched the real instructions. Flags,raxand the other XMM registers came through unchanged, and the upper 64 bits were zeroed. Racing faults at one site always ended correctly. The one thing I found is small: every thread that loses the race still builds its own stub, and that stub is never used.The tests ran on a CPU with SSE4a, so the
#UDpath never fires there on its own. I droveemulatewith synthetic contexts, as the PR's test does, and ran the stubs and the original instructions natively from a harness.Your checks (measured)
cargo clippy -p leonida-loader --all-targets -- -D warnings--features diagnosticsrustfmt --checkonconsole_insn.rsmaineither (31 hunks). The merge has 52, so the PR adds about 21, all long lines in its new codecargo test -p leonida-loader --lib --locked, 4 threadsmain: 91 passed. Merge: 92 passed, the extra one beingsse4a_stubs_match_emulation_and_preserve_flags. With--features diagnostics: 95 passedLEONIDA_PATCH_SSE4A=0: 50/50Bit-exactness (throwaway harness, not in the PR). The harness loads all 16 XMM registers with random 128-bit values, sets random CF/PF/AF/ZF/SF/OF/DF with
popfqand a sentinel inrax, then runs three things on the same input: (a) the PR'ssse4a_stuboutput, reached byjmp rel32+ NOPs, (b) the original instruction run natively, and (c)decode::*. After each run it stores all 16 registers, the flags andrax. The seed is fixed.raxchangedlen0..63 ×idx0..63,extrq xmm3andinsertq xmm5, xmm9, 3 values eachdst×src0..15 forinsertq, everydstforextrq, 13 fields covering each path (16/32/8-bit aligned, 64@0,len=0,len=0withidx>0,idx+len=64,idx+len>64, 1@63)& 63matters), random values and flagslen=0is 64 in all three.idx+len=64is exact.idx+len>64is undefined per AMD. The stub gives what the emulator gives:extrqis a plain right shift byidx, andinsertqis the mask truncated at bit 63. The hardware here gave the same low qword in every case.pinsrb/pinsrdfast paths. The general-path fallback that those fields would take without SSE4.1 wasn't exercised. Other fields cover the general path.Concurrency (measured, throwaway tests)
emulateon the same unpatched site at once, behind a barrier. 50 sites, alternatinginsertq(general path) andextrqE9. In each round, 1 thread emulated and advancedripwith the right value, and the other 7 returnedCONTINUE_EXECUTIONwithripunchanged. 0 wrong. The patched site run natively afterwards gave the right resultemulate, 50 sitesraxstayed intact across the switch from instruction tojmpLEONIDA_PATCH_SSE4A=0, same 8-thread race, 10 sitesrip. No stub bytes usedLosing racers leak their stub. In the 8-thread race, the stub slab's used bytes grew by 13,824 for 50 sites. One stub per site would be about 2.4 KB: 64 B for the
insertqgeneral path and 32 B for thisextrq, after rounding to 16. So about 5–6 of the 8 racing threads each built a stub that nothing jumps to. From reading the code:patch_sse4acheckspatched_jump_to_sse4a_stub, then callsalloc_sse4a_stub, thenpatch_slice, and no lock covers the three. Each thread that faults before the winner'scmpxchg16blands builds and keeps a stub. The cost is bounded at one stub per racing thread per site, and it's only paid once per site, so it's cosmetic. HoldingPATCHING(or a per-site check underSSE4A_STUBS) across allocate-and-patch would close it. Alternatively, a losing thread could roll backusedif its stub is still the slab's last one.Read, not run
(at & 15) + insn_len > 16→Skipped). The PR's test covers this at offset 14. The 16-byte block never crosses a page, so a site near a page end is either patched within one page or skipped.near()probes 64 KBVirtualAllocs at 1 MB steps out to ±1.75 GB, so it never reserves a large range. If none is free, orjumpoverflows either way, the site isSkippedand emulated. One small leak: whennear()succeeds butbuildreturnsNone, the new 64 KB slab stays in the list unused. It gets reused for the next site in reach.PAGE_EXECUTE_READWRITEonly for thecmpxchg16b, then put back, and followed byFlushInstructionCache(unchanged frompatch). Stub slabs stay RWX for the life of the process, like the existingclzeroSTUBS.alloc_sse4a_stubflushes the stub before returning the jump.SSE4A_STUBSandPATCHINGare never held together.emulate, a re-read of*ripaftercompiler_fenceand beforedecode, relies on x86 not reordering loads with other loads, plus the single 16-byte atomic store. That reasoning holds. Thedecode→None→patched_jump_to_sse4a_stubfallback also covers it.rsp. A stack walk of a thread caught inside one would be wrong, the same as for theclzerostub. Nothing in the stub can fault except stack growth, which the kernel handles without unwinding.Conflicts. No other open PR touches
console_insn.rs. 45 open PRs touchswitches.md. Against this merge, 10 of them conflict (#3299, #3248, #3225, #3197, #3185, #3171, #3140, #3128, #3126, #3119), and all 10 already conflict withmain. This PR adds no conflicts.Not checked: the title, so no measured speedup, and no real
#UDon a CPU without SSE4a. That means the vectored-handler path end to end, including aCONTEXTcaptured by Windows rather than built by hand. The no-SSE4.1 fallback for 8/32-bit fields.--release. Slab exhaustion past 64 KB, where a second slab is used.GPUs tested: n/a (CPU-only tests).
AI-assisted: yes (Claude Code).
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.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.