Skip to content

PE loader: a title's CLZERO loops run as native code on a CPU without CLZERO (port of #2892) - #3082

Closed
AngeloMaiale wants to merge 1 commit into
xcl-dev:mainfrom
AngeloMaiale:insn/clzero-loop-stub
Closed

AngeloMaiale wants to merge 1 commit into
xcl-dev:mainfrom
AngeloMaiale:insn/clzero-loop-stub

Conversation

@AngeloMaiale

Copy link
Copy Markdown
Contributor

Port of #2892 (f9362ed3 on nvidia-dev, by @Prime1997) to main.

On CPUs without native CLZERO (all Intel CPUs), each run of a title's 12-byte clzero; add rax, 0x40; cmp rax, <reg>; jb -12 loop raises STATUS_ILLEGAL_INSTRUCTION into eh::emulate_console_only. At PE image load on a CPU without CLZERO, replace each 12-byte ClzeroLoop (end_reg != rsp) with a call to a near-image rep stosb stand-in that zeroes the same whole 64-byte lines and leaves rax, flags, and all other registers as the loop does (XCL_CLZERO_PATCH=0 keeps trap emulation; lone clzero stays emulated).

Note: HANDOFF.md ("Console CPU instructions the PC lacks") describes clzero as always emulated by eh::emulate_console_only; 12-byte ClzeroLoop sites in mapped title images now run natively.

Fixes #2497

What is in it

  • crates/xcl/src/eh.rs: cpu_has_clzero, patch_clzero_loops (with clzero_loop_thunk and code_near), and unit test a_patched_clzero_loop_zeroes_whole_lines_and_keeps_registers, cherry-picked verbatim from PE loader: a title's CLZERO loops run as native code on a CPU without CLZERO #2892.
  • crates/xcl/src/loader.rs: calls eh::patch_clzero_loops on executable sections of mapped title images when !eh::cpu_has_clzero().

Tested

  • NVIDIA GeForce RTX 4050 Laptop GPU (driver 32.0.16.2124, wave32), 12th Gen Intel Core i7-12650H, 16 GB RAM, Windows 11 Home 26200, rustc 1.100.0 (stable-x86_64-pc-windows-msvc), debug (workspace test suite) and release (smoke.ps1).
  • .\scripts\test.ps1 (cargo build --workspace && cargo test --workspace -- --test-threads=4): 1467 passed, 0 failed, 47 ignored (28 SKIPPED lines: 20 no wave64, 3 #1426 wave32 per-lane f16/f32 refusals, 1 no heap windows, 1 no D3D12 debug layer, 3 e2e debug-layer/sample skips).
  • Retail title 419239bf (60 s run on Intel Core i7-12650H): logged 6 CLZERO loops run as native code on this CPU, which has no CLZERO at image load and zero ClzeroLoop emulation traps thereafter (versus ClzeroLoop trap emulation on main).

Regression run

main:

xcl-smoke v1
commit: 2c717a043c8488938179fca81da4bb9e7913a358
gpu: NVIDIA GeForce RTX 4050 Laptop GPU
title: 419239bf
seconds: 60
exit: 0x0
frames shown: 1455 (PresentX 1455)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14246 MB

PR:

xcl-smoke v1
commit: dd883f272eef8de380d18d96bc1d91e021ab69e2
gpu: NVIDIA GeForce RTX 4050 Laptop GPU
title: 419239bf
seconds: 60
exit: 0x0
frames shown: 1448 (PresentX 1449)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14301 MB

Not tested

  • AMD Zen+ CPUs (where cpu_has_clzero() is true and patch_clzero_loops is skipped).
  • World gameplay past the 60 s startup/menu window, and XCL_CLZERO_PATCH=0 on a live title.

AI use

… CLZERO

CLZERO is AMD's; on an Intel CPU each clzero raises an illegal-instruction
exception that XCL answers by emulating the loop it starts
(insn::ConsoleOnly::ClzeroLoop): one exception for every run of the loop. A
retail title's memset is such a loop. While its world loaded on an Intel CPU
one of its threads was in that exception at every thread dump (300, 360 and
400 s of several runs), and XCL_GPU_TIMES measured 0.17 to 0.51 s of GPU
work in world frames that took 17 to 43 s: the card stood idle while the
CPU side crawled.

When an image is mapped on a CPU without CLZERO, each such loop (12 bytes)
becomes a call of a stand-in next to the image that zeroes the same whole
lines with rep stosb and leaves rax, the flags and every other register as
the loop does. XCL_CLZERO_PATCH=0 keeps the emulation. A lone clzero is
still emulated.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@upipibi

upipibi commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

A second loop shape this PR may want to cover, read from both projects and not run (I don't run the title):

xcl's decoder knows one clzero loop: clzero; add rax, 0x40; cmp rax, <reg>; jb -12, as ConsoleOnly::ClzeroLoop (insn.rs:14-15, :117-132 on main 2c717a0). That is the shape this PR patches. VIctory's handler, for the same title, recognises a second shape the compiler builds around the instruction (crates/victory-loader/src/crash.rs:381-387 at 63bcca4, from VIctory#34):

top: mov rax, a
     add a, 0x40
     clzero
     cmp a, b
     jb  top

That is 15 bytes, with the counter in a rather than rax. A VIctory PR adds a dedicated fast path for it (VIctory#3278, VICTORY_CLZERO_LOOP15). In xcl, a loop of this shape decodes as a lone Clzero, so it stays at one exception per 64-byte line, which VIctory#34 measured at about 1 µs each on an i5-13600KF (448 exceptions, 0.40 ms for one 28 KiB clear). Neither project publishes a count of the title's sites by shape, so whether the 15-byte form occurs, and how often, is a guess on my part. patch_clzero_loops could log how many lone clzeros it skips, which would show it on an Intel run.

AI-assisted (Claude Code, Claude Opus 5.5 read both handlers).

@AngeloMaiale

Copy link
Copy Markdown
Contributor Author

Good catch — I scanned the executable sections of title 419239bf and its 7 bundled DLLs for 0F 01 FC:

There are 11 byte matches in total (all in the main .exe, 0 in the DLLs):

  • 1 false match inside the rel32 displacement of a call instruction (not an instruction boundary, so a raw byte-scan count of unpatched 0F 01 FCs would include it).
  • 6 of the 12-byte ClzeroLoop form (clzero; add rax, 0x40; cmp rax, <reg>; jb -12, with <reg> in rdx, r8, r9, r10) — the 6 patched here.
  • 4 of the exact 15-byte form you noted (mov rax, a; add a, 0x40; clzero; cmp a, b; jb -15, with (a, b) = (r10, r11), (r9, r10), (r8, r9), (rdx, r8)), right after the 12-byte loops in the same routine.
  • 0 standalone non-loop clzero sites.

None of the 4 15-byte loops fired during the 60 s menu smoke run (WARN_ONCE logged no lone Clzero), so those branches are only reached on other size/alignment tiers (e.g. during world load).

To keep #3082 as a verbatim cherry-pick of #2892 from nvidia-dev, I think it makes sense to handle the 15-byte loop shape (a advanced by N * 64, rax left at a - 64, flags from cmp a, b) in a small follow-up PR stacked on #3082 (happy to fold it in here instead if @Prime1997 prefers).

AI use: Antigravity (Gemini 4 Carbon) scanned the PE sections for 0F 01 FC shapes and drafted this reply.

@upipibi

upipibi commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

A second candidate for this patch-at-load approach, with numbers, in case it's useful for a follow-up (read from other trees' threads, not run):

xcl emulates SSE4a insertq/extrq on Intel through a trap on every execution (eh.rs:414-432). In VIctory, 101Leafy measured 191-491 of these faults a second in GTA VI gameplay on an i9-13900K, 442 a second at a single place (VIctory#3042, closed for design reasons). ScarlettPort#377 (merged, GPL-3) names its hot site as one in a half-float packing routine. It patches each faulting site to a jmp into an SSE2 stub on first fault, about 570 times faster by its release notes. It also links the fault cost to 13th-gen Intel crashes right after the first emulated insertq (ScarlettPort#350). Its test compares the stubs with the real instruction on an AMD CPU for 532,480 immediate forms, which settles the high quadword (zeroed).

That is the same kind of site as the 12-byte clzero loops here: a hot loop of console-only code that is cheap once patched and costly as a trap.

AI-assisted (Claude Code, Claude Opus 5.5 read VIctory#3042 and ScarlettPort#377).

@SlopTart

Copy link
Copy Markdown

Ran the clzero tests on both Linux/Wine lanes at head dd883f27, one CPU with native CLZERO and one without:

  • rig1: Ryzen 9 9900X, the clzero CPU flag IS present in /proc/cpuinfo, so the emulation path is dormant here. cargo test -p xcl --lib -- clzero: 3 passed / 0 failed, x3 runs, 0.03 s each.
  • rig2: i5-10600K, Intel, clzero flag count 0 in /proc/cpuinfo, so the patched-loop path is the live one here. Same command: 3 passed / 0 failed, x3 runs, 0.05 s each.

So on both vendors, under Wine, the thunk patch and the register preservation hold on the machine that actually takes the illegal-instruction path (rig2) and on the machine that skips it (rig1).

Tested: the 3 clzero-matching tests, x3 per lane at dd883f27.
Not tested: a real title's clzero loops end to end, native Windows, full suite on this head.
AI use: Hermes agent (local Qwen-class) ran the tests and drafted this; I checked it before posting.

@AngeloMaiale
AngeloMaiale marked this pull request as ready for review November 28, 2026 14:49
@throny

throny commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

Closing as a duplicate of #2892: #2892's change (CLZERO loops patched on a CPU without CLZERO, eh.rs / loader.rs) reached main through the nvidia-dev merge, and this PR's diff applies in reverse cleanly on main. I compared the diffs against current main (ff0c364b). If I got this wrong (something here is not in #2892), say so and reopen it, or ask and I will.

AI use: Claude Code (Claude Opus 5.5) checked the pair and wrote this; part of the duplicate sweep announced on #3041.

@throny throny added the duplicate This issue or pull request already exists label Nov 30, 2026
@throny throny closed this Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

duplicate This issue or pull request already exists

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

5 participants