Patch immediate-form SSE4a (insertq/extrq) in-place and reduce refresh-flush lock contention #367

Closed
github-bot wants to merge 0 commits from github-bot/gh-367 into main AGit
Owner

@⁠HDFreitas on GitHub · 2026-12-01 19:22 UTC · original
HDFreitas:perf/sse4a-patch-and-refresh-fastpath → main · +281 −32 · 4 files · 1 commits · closed without merging

Summary

This PR reduces CPU overhead on Intel systems and in the submitting thread's refresh flushes:

  1. In-place native stub patching for immediate-form SSE4a (insertq/extrq) (crates/victory-loader/src/console_insn.rs):

    • On Intel CPUs (which lack AMD's SSE4a extension), immediate-form insertq/extrq instructions (>= 6 bytes, e.g., insertq xmm0, xmm1, 16, 16) previously raised STATUS_ILLEGAL_INSTRUCTION on every execution — reaching 524,288+ kernel exceptions in ~6 minutes (~1,800–2,200 faults/s) during gameplay on an Intel i9-9900K.
    • Mirrors the existing clzero loop trampoline mechanism: replaces the first 5 bytes atomically via cmpxchg16b with a jmp rel32 to a small native stub in a nearby executable page (preserving all GPRs, other XMM registers, and RFLAGS, and zeroing the destination XMM's high quadword to match emulation / Zen 5).
    • Multi-page stub pool (Vec<(usize, usize)>) ensures modules >2 GiB apart can still allocate a reachable stub page.
    • Controlled by VICTORY_PATCH_SSE4A (on by default; set VICTORY_PATCH_SSE4A=0 to fall back to per-fault emulation).
  2. Lock-free fast path and lower journal RwLock contention in refresh flushes (crates/victory-gpu/src/shadow.rs, watch.rs, host.rs):

    • Tracks unfilled page count in Copied and a monotonic min_taken: AtomicU64 lower bound on Shadow once all pages have been copied at least once.
    • Adds watch::nothing_new_min so Shadow::nothing_new_in can confirm unchanged copies without locking Shadow::copied or scanning copied.pages.
    • Extends the O(1) written_fast lookup window from <= 4 to <= 8 pages (up to 512 KiB).
    • Increases wide_chunks batch size in refresh_front and flush_all from 32 to 128, cutting Journal RwLock read-lock acquisitions and atomic counter updates by 4x across worker threads.

Measurements (Intel i9-9900K @ 5 GHz, 32 GB RAM, RTX 3060 12 GB, Windows 11, Series S 1080p + DLSS)

  • Emulated console instructions (console_insn): dropped from 524,288 faults in 6 min to 1 single fault at startup (patched on first hit, 0 subsequent faults).
  • Submitting thread preparing: dropped from 57–65 ms/frame (stock v0.8.2) to 31.7–35.3 ms/frame (refreshes down from 14–16 ms to 10.5–12.0 ms).
  • frame_of_refreshes microbenchmark: 21.62 ms → 21.06 ms mean (all-copies submission 4.69 ms → 4.47 ms).

Verification

  • cargo clippy --locked -p victory-loader -p victory-gpu --tests -- -D warnings: clean (0 warnings).
  • cargo test --release --locked -p victory-loader console_insn: 6/6 passed, including new sse4a_immediate_becomes_native_stub test that executes patched insertq and extrq instructions natively and verifies low/high quadwords and RFLAGS preservation.
  • cargo test --release --locked -p victory-gpu refresh_tests: 6/6 passed.
  • Verified in-game across multiple sessions with no regressions or visual artifacts.
**@⁠HDFreitas** on GitHub · 2026-12-01 19:22 UTC · [original](https://github.com/readmet3xt-lab/foxhound/pull/367) `HDFreitas:perf/sse4a-patch-and-refresh-fastpath` → `main` · +281 −32 · 4 files · 1 commits · closed without merging ## Summary This PR reduces CPU overhead on Intel systems and in the submitting thread's refresh flushes: 1. **In-place native stub patching for immediate-form SSE4a (`insertq`/`extrq`)** (`crates/victory-loader/src/console_insn.rs`): - On Intel CPUs (which lack AMD's SSE4a extension), immediate-form `insertq`/`extrq` instructions (`>= 6` bytes, e.g., `insertq xmm0, xmm1, 16, 16`) previously raised `STATUS_ILLEGAL_INSTRUCTION` on every execution — reaching **524,288+ kernel exceptions in ~6 minutes (~1,800–2,200 faults/s)** during gameplay on an Intel i9-9900K. - Mirrors the existing `clzero` loop trampoline mechanism: replaces the first 5 bytes atomically via `cmpxchg16b` with a `jmp rel32` to a small native stub in a nearby executable page (preserving all GPRs, other XMM registers, and `RFLAGS`, and zeroing the destination XMM's high quadword to match emulation / Zen 5). - Multi-page stub pool (`Vec<(usize, usize)>`) ensures modules >2 GiB apart can still allocate a reachable stub page. - Controlled by `VICTORY_PATCH_SSE4A` (on by default; set `VICTORY_PATCH_SSE4A=0` to fall back to per-fault emulation). 2. **Lock-free fast path and lower journal RwLock contention in refresh flushes** (`crates/victory-gpu/src/shadow.rs`, `watch.rs`, `host.rs`): - Tracks `unfilled` page count in `Copied` and a monotonic `min_taken: AtomicU64` lower bound on `Shadow` once all pages have been copied at least once. - Adds `watch::nothing_new_min` so `Shadow::nothing_new_in` can confirm unchanged copies without locking `Shadow::copied` or scanning `copied.pages`. - Extends the O(1) `written_fast` lookup window from `<= 4` to `<= 8` pages (up to 512 KiB). - Increases `wide_chunks` batch size in `refresh_front` and `flush_all` from `32` to `128`, cutting `Journal` `RwLock` read-lock acquisitions and atomic counter updates by 4x across worker threads. ## Measurements (Intel i9-9900K @ 5 GHz, 32 GB RAM, RTX 3060 12 GB, Windows 11, Series S 1080p + DLSS) - **Emulated console instructions (`console_insn`)**: dropped from **524,288 faults in 6 min** to **1 single fault** at startup (patched on first hit, 0 subsequent faults). - **Submitting thread `preparing`**: dropped from **57–65 ms/frame** (stock v0.8.2) to **31.7–35.3 ms/frame** (`refreshes` down from **14–16 ms** to **10.5–12.0 ms**). - **`frame_of_refreshes` microbenchmark**: `21.62 ms` → `21.06 ms` mean (all-copies submission `4.69 ms` → `4.47 ms`). ## Verification - `cargo clippy --locked -p victory-loader -p victory-gpu --tests -- -D warnings`: clean (0 warnings). - `cargo test --release --locked -p victory-loader console_insn`: 6/6 passed, including new `sse4a_immediate_becomes_native_stub` test that executes patched `insertq` and `extrq` instructions natively and verifies low/high quadwords and `RFLAGS` preservation. - `cargo test --release --locked -p victory-gpu refresh_tests`: 6/6 passed. - Verified in-game across multiple sessions with no regressions or visual artifacts.
Replace faulting immediate-form SSE4a insertq/extrq instructions on Intel CPUs with a jmp rel32 trampoline to a register- and RFLAGS-preserving native stub (controlled by VICTORY_PATCH_SSE4A, on by default).

Add a lock-free min_taken fast path to Shadow::nothing_new_in / watch::nothing_new_min, extend written_fast lookups to 8 pages, and raise wide_chunks batch size from 32 to 128 to cut Journal RwLock contention.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-01 19:27:05 +00:00
Author
Owner

@⁠readmet3xt-lab on GitHub · 2026-12-01 21:09 UTC · original

Thank you. The refresh half is taken. Please split out the SSE4a half as its own pull request: the console-instruction emulation has since been rewritten, so that half would need rebasing on the new code.

**@⁠readmet3xt-lab** on GitHub · 2026-12-01 21:09 UTC · [original](https://github.com/readmet3xt-lab/foxhound/pull/367#issuecomment-7325827200) Thank you. The refresh half is taken. Please split out the SSE4a half as its own pull request: the console-instruction emulation has since been rewritten, so that half would need rebasing on the new code.
github-bot closed this pull request 2026-12-01 21:18:38 +00:00
Author
Owner

@⁠HDFreitas on GitHub · 2026-12-01 21:13 UTC · original

Split out the SSE4a half into #386 as requested (happy to rebase it once the console-instruction rewrite lands on main/next). Closing this PR since the refresh half has been taken.

**@⁠HDFreitas** on GitHub · 2026-12-01 21:13 UTC · [original](https://github.com/readmet3xt-lab/foxhound/pull/367#issuecomment-7325930559) Split out the SSE4a half into #386 as requested (happy to rebase it once the console-instruction rewrite lands on main/next). Closing this PR since the refresh half has been taken.

Pull request closed

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