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

Open
opened 2026-11-25 20:39:17 +00:00 by voiceovarian1-max · 2 comments
voiceovarian1-max commented 2026-11-25 20:39:17 +00:00 (Migrated from github.com)
Contributor

Imported from ivall/xcl. Originally posted by voiceovarian1-max on 2026-11-25T20:39:17Z.


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).

<!-- greyfinch-sync:ivall/xcl:issue:6613302271 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/issues/2497). Originally posted by [voiceovarian1-max](https://github.com/voiceovarian1-max) on 2026-11-25T20:39:17Z. --- **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).
AngeloMaiale commented 2026-11-26 21:19:03 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by AngeloMaiale on 2026-11-26T21:19:03Z.


Taking this on Windows 11 (Intel i7-12650H, no native CLZERO). Plan:

  • In eh.rs, when an 8-byte-aligned ClzeroLoop (with end_reg not in rax/rcx/rsp/rdi) follows a 10-byte 66 2E 0F 1F 84 00 00 00 00 00 NOP pad and XCL_CLZERO_PATCH != 0, emit a near-image 58-byte store-loop stub (registered with RtlAddFunctionTable for x64 unwind) and atomically patch [rip-10..rip+4] (66 90, jmp stub, jmp short rip-8) once under a lock.
  • Add unit tests executing rewritten loops natively (aligned/unaligned rax, end <= start, r8..r15 end registers, and excluded registers staying emulated) and measure on a retail title with smoke.ps1.

AI use: Antigravity with Gemini 4 Carbon inspected eh.rs and drafted this plan.

<!-- greyfinch-sync:ivall/xcl:comment:7203607815 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/issues/2497#issuecomment-7203607815). Originally posted by [AngeloMaiale](https://github.com/AngeloMaiale) on 2026-11-26T21:19:03Z. --- Taking this on Windows 11 (Intel i7-12650H, no native `CLZERO`). Plan: - In `eh.rs`, when an 8-byte-aligned `ClzeroLoop` (with `end_reg` not in `rax`/`rcx`/`rsp`/`rdi`) follows a 10-byte `66 2E 0F 1F 84 00 00 00 00 00` NOP pad and `XCL_CLZERO_PATCH != 0`, emit a near-image 58-byte store-loop stub (registered with `RtlAddFunctionTable` for x64 unwind) and atomically patch `[rip-10..rip+4]` (`66 90`, `jmp stub`, `jmp short rip-8`) once under a lock. - Add unit tests executing rewritten loops natively (aligned/unaligned `rax`, `end <= start`, `r8..r15` end registers, and excluded registers staying emulated) and measure on a retail title with `smoke.ps1`. **AI use:** Antigravity with Gemini 4 Carbon inspected `eh.rs` and drafted this plan.
voiceovarian1-max commented 2026-11-27 11:39:15 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by voiceovarian1-max on 2026-11-27T11:39:15Z.


Update: on nvidia-dev this is done at image load since #2892 (eh::patch_clzero_loops, not on main yet). We checked the retail title's executable (1.0.2.1): all 6 CLZERO loops in its code have the 12-byte form that load-time scan rewrites (end registers r8, r9, r10 and rdx). That includes the 5 our runs hit at run time. So on nvidia-dev nothing more is needed, and we dropped our trap-time rewrite in favour of it. For main, porting #2892 would cover it. @AngeloMaiale, in case it saves you the work.

The same title's monitorx / mwaitx faults are #2618.

Written with Claude Code (Claude Opus 5.5).

<!-- greyfinch-sync:ivall/xcl:comment:7217322389 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/issues/2497#issuecomment-7217322389). Originally posted by [voiceovarian1-max](https://github.com/voiceovarian1-max) on 2026-11-27T11:39:15Z. --- Update: on nvidia-dev this is done at image load since #2892 (`eh::patch_clzero_loops`, not on `main` yet). We checked the retail title's executable (1.0.2.1): all 6 CLZERO loops in its code have the 12-byte form that load-time scan rewrites (end registers r8, r9, r10 and rdx). That includes the 5 our runs hit at run time. So on nvidia-dev nothing more is needed, and we dropped our trap-time rewrite in favour of it. For `main`, porting #2892 would cover it. @AngeloMaiale, in case it saves you the work. The same title's `monitorx` / `mwaitx` faults are #2618. Written with Claude Code (Claude Opus 5.5).
Sign in to join this conversation.
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
xcl-dev/xcl#2487
No description provided.