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 from upstream-pr/3082 into main
AngeloMaiale commented 2026-11-27 23:27:51 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by AngeloMaiale on 2026-11-27T23:27:51Z.


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 #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

  • Commit dd883f27 is cherry-picked verbatim from #2892 by @Prime1997 (Co-Authored-By: Claude Fable 5.2). Antigravity (Gemini 4 Carbon) cherry-picked the commit onto main, ran the test suite and paired regression smoke runs, and wrote this PR description.
<!-- greyfinch-sync:ivall/xcl:issue:5534418012 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3082). Originally posted by [AngeloMaiale](https://github.com/AngeloMaiale) on 2026-11-27T23:27:51Z. --- 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 #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** - Commit `dd883f27` is cherry-picked verbatim from #2892 by @Prime1997 (`Co-Authored-By: Claude Fable 5.2`). Antigravity (Gemini 4 Carbon) cherry-picked the commit onto `main`, ran the test suite and paired regression smoke runs, and wrote this PR description.
upipibi commented 2026-11-27 23:34:23 +00:00 (Migrated from github.com)

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

<!-- greyfinch-sync:ivall/xcl:comment:7230243426 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3082#issuecomment-7230243426). Originally posted by [upipibi](https://github.com/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 `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 `clzero`s it skips, which would show it on an Intel run. AI-assisted (Claude Code, Claude Opus 5.5 read both handlers).
AngeloMaiale commented 2026-11-27 23:45:15 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by AngeloMaiale on 2026-11-27T23:45:15Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7230382364 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3082#issuecomment-7230382364). Originally posted by [AngeloMaiale](https://github.com/AngeloMaiale) on 2026-11-27T23:45:15Z. --- 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 FC`s 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 commented 2026-11-28 02:15:30 +00:00 (Migrated from github.com)

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

<!-- greyfinch-sync:ivall/xcl:comment:7232240263 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3082#issuecomment-7232240263). Originally posted by [upipibi](https://github.com/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`/`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 commented 2026-11-28 03:21:04 +00:00 (Migrated from github.com)

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:

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

<!-- greyfinch-sync:ivall/xcl:comment:7233002372 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3082#issuecomment-7233002372). Originally posted by [SlopTart](https://github.com/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: - 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.
throny commented 2026-11-30 19:54:34 +00:00 (Migrated from github.com)

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

<!-- greyfinch-sync:ivall/xcl:comment:7290118425 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3082#issuecomment-7290118425). Originally posted by [throny](https://github.com/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`) 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.

Pull request closed

Sign in to join this conversation.
No reviewers
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!3082
No description provided.