Skip to content

PE loader: a title's CLZERO loops run as native code on a CPU without CLZERO - #2892

Merged
Prime1997 merged 1 commit into
xcl-dev:nvidia-devfrom
Prime1997:contrib/nv-clzero-native-loops
Nov 26, 2026
Merged

Prime1997 merged 1 commit into
xcl-dev:nvidia-devfrom
Prime1997:contrib/nv-clzero-native-loops

Conversation

@Prime1997

@Prime1997 Prime1997 commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

Requested by Prime · project thread

On a CPU without CLZERO (every Intel CPU), each run of a title's clzero; add rax, 0x40; cmp rax, <reg>; jb loop raises an illegal-instruction exception that eh::emulate_console_only answers: one exception for every run of the loop. A retail title's memset is such a loop. This patches those loops once, when the image is mapped, into a call of a native stand-in next to the image that zeroes the same whole lines and leaves rax, the flags and every other register as the loop does.

Part of #2688.

Why
On an Intel i5-10400, while a retail title's world loaded, one of its threads stood in the exception path (eh::cpp_exception_logger / is_writable) at every thread dump (XCL_THREAD_DUMP_AT, 300, 360 and 400 s, 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 GPU stood idle while the CPU side crawled. With the loops patched (6 in that title) the same run's world frames took 1.05 to 1.81 s and it issued 102,739 dispatches where it had issued 12,000 to 24,000. In the 60 s regression run below the title shows 1393 frames against the base's 459.

What is in it

  • crates/xcl/src/eh.rs: cpu_has_clzero (CPUID 8000_0008h EBX bit 0), clzero_loop_thunk (the stand-in for one end register: zero [rax & !63 ..) in whole lines up to it with rep stosb, at least one line as the loop's first pass, rax and the flags as the loop's last add/cmp leave them, no other register changed), code_near (64 KiB a call rel32 reaches), patch_clzero_loops (finds the 12-byte loops with insn::decode, leaves an 0x67-prefixed CLZERO and a loop against rsp alone, writes call + a 7-byte nop). XCL_CLZERO_PATCH=0 keeps the emulation; a lone CLZERO is still emulated.
  • crates/xcl/src/loader.rs: step 9 of mapping an image, after the stack-cookie hook, on a CPU without CLZERO only.
  • Test eh::tests::a_patched_clzero_loop_zeroes_whole_lines_and_keeps_registers: patches a real loop in executable memory and runs it (lines zeroed, bytes around them kept, rax where the loop leaves it, rcx/rdi/rdx unchanged, end at or before the start zeroing one line).

Tested

  • NVIDIA GeForce RTX 3060 Ti 8 GB (driver 32.0.16.1714), Intel Core i5-10400, Windows 11 26200, rustc 1.100.0; debug tests, release smoke.
  • cargo test --locked --workspace --no-fail-fast -- --test-threads=4 with XCL_DXC_DIR set to DXC 1.9.2609.5: 1817 passed, 9 failed, 48 ignored. The 9 failures are the same tests that fail on nvidia-dev eae01502 itself (1816 passed, 9 failed, 48 ignored there, same command): async_compute_pso_registers_real_pso_and_not_standin, console_ms_in_as_slot_and_ms_with_as_are_refused_cleanly, gpu::recomp::tests::gpu_compute_thread_limits_leave_the_lanes_past_them_out, hle::d3d12x::texture_upload::tests::gpu_parts_of_a_texture_go_up_and_the_rest_stays, hle::d3d12x::texture_upload::tests::pieces_hold_the_texels_of_their_levels, split_compute_prefers_the_offline_whole_pipeline, two_pass_driver_cache_reuses_both_passes_and_recovers_corrupt_blobs.
  • SKIPPED on this adapter: 27 (no wave64: 20, per-lane f16 form refused: 3, no dxc.exe: 1, no heap windows (opt-in: XCL_HEAP_WINDOWS=1): 1, cannot disable the driver cache (Windows Developer Mode is required): The application requested an operation that depend: 1, no heap windows: 1).
  • GitHub's Build and test (Windows, no GPU) fails here on hle::d3d12x::sparse::tests::a_gpu_write_into_a_gpu_view_lands_in_its_cpu_view (the WARP device is removed, 0x887a0005). That test passes on the card above, on this branch and on eae01502, and fails the same way on the other open pull requests against nvidia-dev (kernelx: physical page homes in window 0 first, from the top, keeping 64 MB free (#2867) #2905, for one).

Regression run
A commit of main (b49be183, the last one before the shader preparation screen of #2555, which prepares every record before the title and so does not fit a 60 s run):

xcl-smoke v1
commit: b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7
gpu: NVIDIA GeForce RTX 3060 Ti
title: 419239bf
seconds: 60
exit: 0x0
frames shown: 363 (PresentX 363)
presents per 2 s (last 3): 16 17 16
device removals: 0
crashes: 0
peak commit: 15320 MB

The base of this PR, nvidia-dev eae01502:

xcl-smoke v1
commit: eae015021c639866ab061cdff2e9daf8ba9daac5
gpu: NVIDIA GeForce RTX 3060 Ti
title: 419239bf
seconds: 60
exit: 0x0
frames shown: 459 (PresentX 459)
presents per 2 s (last 3): 16 17 17
device removals: 0
crashes: 0
peak commit: 15366 MB

This PR's head:

xcl-smoke v1
commit: f9362ed33aa600622cec7bcdf7ab0752da9295e4
gpu: NVIDIA GeForce RTX 3060 Ti
title: 419239bf
seconds: 60
exit: 0x0
frames shown: 1393 (PresentX 1393)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15346 MB

Not tested

  • An AMD CPU: the patch is not applied there, so that path is unchanged; not run.
  • A title whose CLZERO loop has no free address range within 1 GiB (the loops then stay emulated and a line is logged).

AI use
Claude Code (Claude Fable 5.2 and Claude Opus 5.5) wrote the code, ran the suite and the regression runs on the hardware above, and wrote this description; Prime asked for it and owns the account. We coordinate the 8 GB work on the XCL Discord too: https://discord.gg/gK3y7WRRY

🤖 Generated with Claude Code

… 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>
@Prime1997 Prime1997 self-assigned this Nov 26, 2026
@Prime1997
Prime1997 marked this pull request as ready for review November 26, 2026 22:48
@saintwolf

Copy link
Copy Markdown

Tested and reviewed commit f9362ed3 (merged cleanly onto nvidia-dev eae01502 alongside #2893–#2897, zero conflicts).

Tested

  • Hardware: NVIDIA GeForce RTX 4070 SUPER (12 GiB, driver 32.0.16.2140), AMD Ryzen 7 7800X3D, Windows 11 build 26300, rustc 1.100.0, debug test build
  • cargo test -p xcl --lib eh::tests::a_patched_clzero_loop -- --test-threads=1: 1 passed, 0 failed, 0 ignored, 0 skipped (a_patched_clzero_loop_zeroes_whole_lines_and_keeps_registers passed)

Review notes

  • Patching logic, register preservation across end_reg (0..15, excluding rsp), and 64-byte line advance look sound.
  • Minor compiler warning on rustc 1.100.0: crates/xcl/src/eh.rs:201 emits warning: unnecessary unsafe block.
  • Minor note: thunk arena pages remain PAGE_EXECUTE_READWRITE rather than flipping to PAGE_EXECUTE_READ after emission, and code_near only searches upward within +1 GiB of the module.

Not tested

  • Runtime CLZERO patching on a non-CLZERO CPU (Ryzen 7 7800X3D has native CLZERO, so only the unit test exercised patch_clzero_loops)
  • Full workspace test suite or retail-title smoke run on this PR

AI use

  • Antigravity (exact model name not provided to the agent): reviewed the diff, ran the build/test command, and drafted this report (checked before posting).

@Prime1997
Prime1997 merged commit 7e63fc6 into xcl-dev:nvidia-dev Nov 26, 2026
2 of 4 checks passed
rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 26, 2026
@pickles976

Copy link
Copy Markdown

Focused Intel/Wine verification of exact head f9362ed33aa600622cec7bcdf7ab0752da9295e4: 3 passed, 0 failed, 0 ignored, 0.04 s test time; checkout production and tests unchanged. Fresh Compiling xcl .../xcl-pr2892/crates/xcl recorded (41.41 s build).

Tested: Intel Core i7-13620H, 16 GiB system RAM, Linux 6.17 / Wine 11.0 / Rust 1.98.1, Windows MSVC target. This is CPU execution, not a GPU fixture; the laptop has an RTX 4060 but no rendering claim is made. Existing external 9 GiB/512 MiB scope and RAM/GPU/disk safety guards retained.

XCL_SOURCE_DIR="$PWD/xcl-pr2892" /usr/bin/bash .compat-tools/cargo-xcl-wine.sh \
  test --locked --target x86_64-pc-windows-msvc -p xcl --lib clzero \
  -- --nocapture --test-threads=1

XCL_CLZERO_PATCH unset. Touched crates/xcl/build.rs and crates/xcl/src/lib.rs before the build to defeat the shared-target cross-worktree cache ambiguity.

Passing owner fixtures:

  • a_patched_clzero_loop_zeroes_whole_lines_and_keeps_registers: patches and actually executes the native replacement on Intel. Checks unaligned-start whole-line zeroing with untouched neighboring lines, final RAX, RCX/RDI/RDX values and the observed carry flag; also the end-before-start one-line case. Log: 1 CLZERO loops run as native code on this CPU.
  • clzero_loop_aligns_updates_flags_and_falls_back_when_end_le_start: existing emulator alignment/flags/one-line fallback contract.
  • clzero_with_an_address_size_prefix_zeroes_at_eax: address-size prefix targets the low-address alias rather than the corresponding high 64-bit address.

Evidence: .compat-runtime/logs/verify-pr2892-clzero-20261126T235036Z/ (console, command/environment, clean provenance and resource samples), guarded command exit 0 in 42.13 s.

Not tested: full suite, every possible end register, retail loader patch-site discovery, opt-out integration, matched-main title regression, loading time/FPS, or gameplay. No title was launched. This clears the selected native-loop/emulator correctness checks here; it does not establish that this fixes our separate Game-Init stalled-fence watchdog, nor certify the complete nvidia-dev integration. The useful next comparison is a controlled title main/head run after the independent Wine synchronization/safety blockers are cleared, not an unchanged crashing launch.

AI-assisted verification/report: omp, OpenAI GPT-6 Astra; results above are from executed local commands.

JR-Wiederkehr pushed a commit to JR-Wiederkehr/xcl that referenced this pull request Nov 27, 2026
…st and version

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants