Console CPU instructions: a CLZERO loop traps once per run; rewrite it to a stub once #2487
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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 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; jbloop one exception (ConsoleOnly::ClzeroLoop) and hot-patches CLWB to NOPs; #647 makes each exception'sis_writablecheaper. 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 inemulate_console_onlyin 17 of 344 samples. Exceptions are slow under Wine; on Windows they cost less, but are not free.Plan (about 110 lines in
eh.rswith a test,XCL_CLZERO_PATCH=0to turn it off): only for a loop right after the 10-byte NOP compilers pad loops with (66 2E 0F 1F 84 00…).cmpbytes, and jumps back after the loop.jmp stub, then the loop's first 4 tojmp 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).
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:eh.rs, when an 8-byte-alignedClzeroLoop(withend_regnot inrax/rcx/rsp/rdi) follows a 10-byte66 2E 0F 1F 84 00 00 00 00 00NOP pad andXCL_CLZERO_PATCH != 0, emit a near-image 58-byte store-loop stub (registered withRtlAddFunctionTablefor x64 unwind) and atomically patch[rip-10..rip+4](66 90,jmp stub,jmp short rip-8) once under a lock.rax,end <= start,r8..r15end registers, and excluded registers staying emulated) and measure on a retail title withsmoke.ps1.AI use: Antigravity with Gemini 4 Carbon inspected
eh.rsand drafted this plan.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 onmainyet). 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. Formain, porting #2892 would cover it. @AngeloMaiale, in case it saves you the work.The same title's
monitorx/mwaitxfaults are #2618.Written with Claude Code (Claude Opus 5.5).