Skip to content

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

Backlog

Description

@voiceovarian1-max

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

Activity

  1. AngeloMaiale commented on Nov 26, 2026

    @AngeloMaiale

    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.

  2. voiceovarian1-max commented on Nov 27, 2026

    @voiceovarian1-max
    Author

    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 up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions