PE loader: a title's CLZERO loops run as native code on a CPU without CLZERO (port of #2892) #3082
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3082"
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 AngeloMaiale on 2026-11-27T23:27:51Z.
Port of #2892 (
f9362ed3onnvidia-dev, by @Prime1997) tomain.On CPUs without native
CLZERO(all Intel CPUs), each run of a title's 12-byteclzero; add rax, 0x40; cmp rax, <reg>; jb -12loop raisesSTATUS_ILLEGAL_INSTRUCTIONintoeh::emulate_console_only. At PE image load on a CPU withoutCLZERO, replace each 12-byteClzeroLoop(end_reg != rsp) with acallto a near-imagerep stosbstand-in that zeroes the same whole 64-byte lines and leavesrax, flags, and all other registers as the loop does (XCL_CLZERO_PATCH=0keeps trap emulation; loneclzerostays emulated).Note:
HANDOFF.md("Console CPU instructions the PC lacks") describesclzeroas always emulated byeh::emulate_console_only; 12-byteClzeroLoopsites in mapped title images now run natively.Fixes #2497
What is in it
crates/xcl/src/eh.rs:cpu_has_clzero,patch_clzero_loops(withclzero_loop_thunkandcode_near), and unit testa_patched_clzero_loop_zeroes_whole_lines_and_keeps_registers, cherry-picked verbatim from #2892.crates/xcl/src/loader.rs: callseh::patch_clzero_loopson executable sections of mapped title images when!eh::cpu_has_clzero().Tested
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 (28SKIPPEDlines: 20no wave64, 3#1426wave32 per-lane f16/f32 refusals, 1no heap windows, 1no D3D12 debug layer, 3e2edebug-layer/sample skips).419239bf(60 s run on Intel Core i7-12650H): logged6 CLZERO loops run as native code on this CPU, which has no CLZEROat image load and zeroClzeroLoopemulation traps thereafter (versusClzeroLooptrap emulation onmain).Regression run
main:
PR:
Not tested
cpu_has_clzero()is true andpatch_clzero_loopsis skipped).XCL_CLZERO_PATCH=0on a live title.AI use
dd883f27is cherry-picked verbatim from #2892 by @Prime1997 (Co-Authored-By: Claude Fable 5.2). Antigravity (Gemini 4 Carbon) cherry-picked the commit ontomain, ran the test suite and paired regression smoke runs, and wrote this PR description.Imported from ivall/xcl. Originally posted by upipibi on 2026-11-27T23:34:23Z.
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
clzeroloop:clzero; add rax, 0x40; cmp rax, <reg>; jb -12, asConsoleOnly::ClzeroLoop(insn.rs:14-15,:117-132onmain2c717a0). 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-387at63bcca4, from VIctory#34):That is 15 bytes, with the counter in
arather thanrax. 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 loneClzero, 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_loopscould log how many loneclzeros it skips, which would show it on an Intel run.AI-assisted (Claude Code, Claude Opus 5.5 read both handlers).
Imported from ivall/xcl. Originally posted by AngeloMaiale on 2026-11-27T23:45:15Z.
Good catch — I scanned the executable sections of title
419239bfand its 7 bundled DLLs for0F 01 FC:There are 11 byte matches in total (all in the main
.exe, 0 in the DLLs):rel32displacement of acallinstruction (not an instruction boundary, so a raw byte-scan count of unpatched0F 01 FCs would include it).ClzeroLoopform (clzero; add rax, 0x40; cmp rax, <reg>; jb -12, with<reg>inrdx,r8,r9,r10) — the 6 patched here.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.clzerosites.None of the 4 15-byte loops fired during the 60 s menu smoke run (
WARN_ONCElogged no loneClzero), 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 (aadvanced byN * 64,raxleft ata - 64, flags fromcmp 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 FCshapes and drafted this reply.Imported from ivall/xcl. Originally posted by upipibi on 2026-11-28T02:15:30Z.
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/extrqon 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 ajmpinto 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 emulatedinsertq(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
clzeroloops 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).
Imported from ivall/xcl. Originally posted by SlopTart on 2026-11-28T03:21:04Z.
Ran the clzero tests on both Linux/Wine lanes at head
dd883f27, one CPU with native CLZERO and one without:clzeroCPU 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.clzeroflag 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.
Imported from ivall/xcl. Originally posted by throny on 2026-11-30T19:54:34Z.
Closing as a duplicate of #2892: #2892's change (CLZERO loops patched on a CPU without CLZERO,
eh.rs/loader.rs) reachedmainthrough the nvidia-dev merge, and this PR's diff applies in reverse cleanly onmain. I compared the diffs against currentmain(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.
Pull request closed