Console CPU instructions: a CLZERO loop traps once per run; rewrite it to a stub once #2497

Open
opened 2026-11-26 05:32:09 +00:00 by github-bot · 0 comments
Owner

@⁠voiceovarian1-max on GitHub · 2026-11-25 20:39 UTC · original

On a CPU without CLZERO, a retail title's small clearing loops cost one illegal-instruction exception per run. Plan: the first time such a loop faults, rewrite it once to jump to a small stub that clears the same lines with plain stores.

#217 made a whole clzero; add rax,0x40; cmp rax,r; jb loop one exception (ConsoleOnly::ClzeroLoop) and hot-patches CLWB to NOPs; #647 makes each exception's is_writable cheaper. This title runs 5 such loops (0.9–7 KB each) in one function thousands of times a second. In a thread-dump sample of its world, title threads were in emulate_console_only in 17 of 344 samples. Exceptions are slow under Wine; on Windows they cost less, but are not free.

Plan (about 110 lines in eh.rs with a test, XCL_CLZERO_PATCH=0 to turn it off): only for a loop right after the 10-byte NOP compilers pad loops with (66 2E 0F 1F 84 00…).

  1. Write a 58-byte stub near the title's image. It clears the same lines with 8 stores per line (rcx and rdi saved), uses the title's own cmp bytes, and jumps back after the loop.
  2. Rewrite the pad's first 2 bytes to NOPs, its last 8 to jmp stub, then the loop's first 4 to jmp short. These are aligned stores, so another thread always sees code it can run.

Loops without the pad, not 8-byte aligned, or ending at rcx/rdi/rax/rsp stay emulated.

Measured so far: Linux, Proton's Wine, i7-14700K, RTX 4070 Ti. 4 of the 5 loops were rewritten and the title ran to the end of each run. The frame rate did not change measurably, because the main limits are elsewhere. A test runs a rewritten loop and checks the cleared lines and rax.

This extends #217's hot-patching from CLWB to these loops. Would a PR for it be welcome?

AI use: found, written and tested with Claude Code (Claude Opus 5.5).

**@⁠voiceovarian1-max** on GitHub · 2026-11-25 20:39 UTC · [original](https://github.com/xcl-dev/xcl/issues/2497) **On a CPU without CLZERO, a retail title's small clearing loops cost one illegal-instruction exception per run. Plan: the first time such a loop faults, rewrite it once to jump to a small stub that clears the same lines with plain stores.** #217 made a whole `clzero; add rax,0x40; cmp rax,r; jb` loop one exception (`ConsoleOnly::ClzeroLoop`) and hot-patches CLWB to NOPs; #647 makes each exception's `is_writable` cheaper. This title runs 5 such loops (0.9–7 KB each) in one function thousands of times a second. In a thread-dump sample of its world, title threads were in `emulate_console_only` in 17 of 344 samples. Exceptions are slow under Wine; on Windows they cost less, but are not free. **Plan (about 110 lines in `eh.rs` with a test, `XCL_CLZERO_PATCH=0` to turn it off):** only for a loop right after the 10-byte NOP compilers pad loops with (`66 2E 0F 1F 84 00…`). 1. Write a 58-byte stub near the title's image. It clears the same lines with 8 stores per line (rcx and rdi saved), uses the title's own `cmp` bytes, and jumps back after the loop. 2. Rewrite the pad's first 2 bytes to NOPs, its last 8 to `jmp stub`, then the loop's first 4 to `jmp short`. These are aligned stores, so another thread always sees code it can run. Loops without the pad, not 8-byte aligned, or ending at rcx/rdi/rax/rsp stay emulated. **Measured so far:** Linux, Proton's Wine, i7-14700K, RTX 4070 Ti. 4 of the 5 loops were rewritten and the title ran to the end of each run. The frame rate did not change measurably, because the main limits are elsewhere. A test runs a rewritten loop and checks the cleared lines and rax. This extends #217's hot-patching from CLWB to these loops. Would a PR for it be welcome? AI use: found, written and tested with Claude Code (Claude Opus 5.5).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:32:09 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.