loader: replace SSE4a extrq/insertq with SSE2 stubs on first fault #1653

Merged
JeremRuiz merged 1 commit from upstream-pr/1653 into main 2026-11-29 08:16:55 +00:00
JeremRuiz commented 2026-11-28 23:05:02 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by JeremRuiz on 2026-11-28T23:05:02Z.


Related: #1603

What it changes

extrq and insertq are AMD-only (SSE4a). On an Intel CPU each one raises an illegal-instruction exception, and console_insn::emulate steps over it every time. The title has one hot site, insertq xmm0, xmm1, 16, 16 (6 bytes, F2 0F 78 C1 10 10), the only SSE4a form seen in seven logs on an i5-9600K. #1603 reports about 1,200 of these a second on an i7-13700K.

This replaces the immediate forms at their first fault, as clzero loops already are: the instruction becomes a jmp rel32 (plus NOPs to its length) to a stub in the existing STUBS regions, and the stub jumps back past it.

  • extrq xmm, len, idx: psrlq then pand with the field mask.
  • insertq xmm1, xmm2, len, idx: a third XMM register is borrowed and kept on the stack (lea rsp, so the flags are untouched); pand and psllq on the source, pand with the hole mask on the destination, then por.
  • The low quadword is what decode::extrq / decode::insertq give; the high quadword is zeroed, as the emulation already does.
  • The masks are 16-aligned constants after the jump back (pand from memory needs the alignment).
  • The register forms (4-5 bytes, no room for a jump) stay emulated. So does any site whose bytes cross a 16-byte boundary, as patch already requires.
  • VICTORY_PATCH_SSE4A=0 keeps the old path.

The new put_stub helper writes a stub into a STUBS region within reach of the site. It reserves a new region when none reaches, as patch_clzero does. patch_clzero itself is unchanged.

How it was tested

  • GPU and profile: RTX 3080 Ti 12 GB, Intel i5-9600K (6 cores, 6 threads), Series S, DLSS quality, no frame generation, otherwise victory play's default switches.
  • Before (v2026.11.28.03 release exe, a 41 min run into the open world): the "console CPU instructions emulated so far" counter passed 65,536 after 3.7 min. It counted 1,406 a second at the start and 731 a second over the last 24 minutes.
  • After (the same release with this commit, a 20 min run, same launcher): the counter never reached 65,536, so fewer than 55 a second on average. No crash.
  • Frame rate on this machine: median 11.4 before and 11.2 after, on two different paths through the game. The exception cost was real but is not what limits this CPU. I am not claiming an fps gain.
  • New unit test an_sse4a_instruction_becomes_sse2: six forms (insertq with distinct registers, with destination equal to source, length 0 and length 4 at index 60; extrq on xmm0 and xmm1) are faulted once through emulate and then run natively. The results are checked against decode::insertq / decode::extrq, and the borrowed register is checked unchanged. It fails when the stub's shift is broken on purpose.
  • For shader changes, the wave sizes tested: not a shader change.
  • cargo test passed for the crates it touches: yes. cargo test -p victory-loader on d4b5d51: 47 passed. cargo clippy -p victory-loader --all-targets with and without --all-features, -D warnings: clean. The full workspace tests ran on the v2026.11.28.03 base, not on main.

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches. Related: #1603. A search for "insertq", "extrq" and "SSE4a" on 2026-11-29 found no pull request. Main had already turned STUBS into a list of regions (no conflict left; this builds on it).
<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5545460165 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1653). Originally posted by [JeremRuiz](https://github.com/JeremRuiz) on 2026-11-28T23:05:02Z. --- Related: #1603 ## What it changes `extrq` and `insertq` are AMD-only (SSE4a). On an Intel CPU each one raises an illegal-instruction exception, and `console_insn::emulate` steps over it every time. The title has one hot site, `insertq xmm0, xmm1, 16, 16` (6 bytes, `F2 0F 78 C1 10 10`), the only SSE4a form seen in seven logs on an i5-9600K. #1603 reports about 1,200 of these a second on an i7-13700K. This replaces the immediate forms at their first fault, as clzero loops already are: the instruction becomes a `jmp rel32` (plus NOPs to its length) to a stub in the existing `STUBS` regions, and the stub jumps back past it. - `extrq xmm, len, idx`: `psrlq` then `pand` with the field mask. - `insertq xmm1, xmm2, len, idx`: a third XMM register is borrowed and kept on the stack (`lea rsp`, so the flags are untouched); `pand` and `psllq` on the source, `pand` with the hole mask on the destination, then `por`. - The low quadword is what `decode::extrq` / `decode::insertq` give; the high quadword is zeroed, as the emulation already does. - The masks are 16-aligned constants after the jump back (`pand` from memory needs the alignment). - The register forms (4-5 bytes, no room for a jump) stay emulated. So does any site whose bytes cross a 16-byte boundary, as `patch` already requires. - `VICTORY_PATCH_SSE4A=0` keeps the old path. The new `put_stub` helper writes a stub into a `STUBS` region within reach of the site. It reserves a new region when none reaches, as `patch_clzero` does. `patch_clzero` itself is unchanged. ## How it was tested - GPU and profile: RTX 3080 Ti 12 GB, Intel i5-9600K (6 cores, 6 threads), Series S, DLSS quality, no frame generation, otherwise `victory play`'s default switches. - Before (v2026.11.28.03 release exe, a 41 min run into the open world): the "console CPU instructions emulated so far" counter passed 65,536 after 3.7 min. It counted 1,406 a second at the start and 731 a second over the last 24 minutes. - After (the same release with this commit, a 20 min run, same launcher): the counter never reached 65,536, so fewer than 55 a second on average. No crash. - Frame rate on this machine: median 11.4 before and 11.2 after, on two different paths through the game. The exception cost was real but is not what limits this CPU. I am not claiming an fps gain. - New unit test `an_sse4a_instruction_becomes_sse2`: six forms (insertq with distinct registers, with destination equal to source, length 0 and length 4 at index 60; extrq on xmm0 and xmm1) are faulted once through `emulate` and then run natively. The results are checked against `decode::insertq` / `decode::extrq`, and the borrowed register is checked unchanged. It fails when the stub's shift is broken on purpose. - For shader changes, the wave sizes tested: not a shader change. - `cargo test` passed for the crates it touches: yes. `cargo test -p victory-loader` on d4b5d51: 47 passed. `cargo clippy -p victory-loader --all-targets` with and without `--all-features`, `-D warnings`: clean. The full workspace tests ran on the v2026.11.28.03 base, not on main. ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. Related: #1603. A search for "insertq", "extrq" and "SSE4a" on 2026-11-29 found no pull request. Main had already turned `STUBS` into a list of regions (no conflict left; this builds on it).
Sign in to join this conversation.
No reviewers
No labels
No milestone
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
trustno1-coder/VIctory-NVIDIA!1653
No description provided.