PE loader: a title's CLZERO loops run as native code on a CPU without CLZERO #2892
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
goal:world-load
good first issue
help wanted
invalid
keep-open
off-goal
question
stale
test-only
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/xcl-dev--xcl!2892
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/2892/head"
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?
@Prime1997 on GitHub · 2026-11-26 22:16 UTC · original
Prime1997:contrib/nv-clzero-native-loops→nvidia-dev· +149 −0 · 2 files · 1 commits · merged 2026-11-26 22:58 UTCRequested 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>; jbloop raises an illegal-instruction exception thateh::emulate_console_onlyanswers: 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), andXCL_GPU_TIMESmeasured 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 withrep stosb, at least one line as the loop's first pass, rax and the flags as the loop's lastadd/cmpleave them, no other register changed),code_near(64 KiB acall rel32reaches),patch_clzero_loops(finds the 12-byte loops withinsn::decode, leaves an0x67-prefixed CLZERO and a loop against rsp alone, writescall+ a 7-byte nop).XCL_CLZERO_PATCH=0keeps 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.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
cargo test --locked --workspace --no-fail-fast -- --test-threads=4withXCL_DXC_DIRset to DXC 1.9.2609.5: 1817 passed, 9 failed, 48 ignored. The 9 failures are the same tests that fail onnvidia-deveae01502itself (1816passed,9failed,48ignored 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.Build and test (Windows, no GPU)fails here onhle::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 oneae01502, and fails the same way on the other open pull requests againstnvidia-dev(#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):The base of this PR,
nvidia-deveae01502:This PR's head:
Not tested
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
@saintwolf on GitHub · 2026-11-26 22:55 UTC · original
Tested and reviewed commit
f9362ed3(merged cleanly ontonvidia-deveae01502alongside#2893–#2897, zero conflicts).Tested
32.0.16.2140), AMD Ryzen 7 7800X3D, Windows 11 build 26300,rustc 1.100.0, debug test buildcargo 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_registerspassed)Review notes
end_reg(0..15, excludingrsp), and 64-byte line advance look sound.rustc 1.100.0:crates/xcl/src/eh.rs:201emitswarning: unnecessary unsafe block.PAGE_EXECUTE_READWRITErather than flipping toPAGE_EXECUTE_READafter emission, andcode_nearonly searches upward within+1 GiBof the module.Not tested
CLZEROpatching on a non-CLZERO CPU (Ryzen 7 7800X3D has nativeCLZERO, so only the unit test exercisedpatch_clzero_loops)AI use
@pickles976 on GitHub · 2026-11-26 23:52 UTC · original
Focused Intel/Wine verification of exact head
f9362ed33aa600622cec7bcdf7ab0752da9295e4: 3 passed, 0 failed, 0 ignored, 0.04 s test time; checkout production and tests unchanged. FreshCompiling xcl .../xcl-pr2892/crates/xclrecorded (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_CLZERO_PATCHunset. Touchedcrates/xcl/build.rsandcrates/xcl/src/lib.rsbefore 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.
@throny on GitHub · 2026-11-27 11:27 UTC · original
#2892 (CLZERO loops as native code on a CPU without CLZERO) turns the open world black on an Intel CPU. Found by bisecting
nvidia-devon an i9-12900K + RTX 4080 16 GB (driver 621.40, 2 GiB shader heap profile), same save, same switches for every step (XCL_COHERENT_HEAPS=sparse XCL_TEXTURE_LEVEL_PAGES=1 XCL_CS_PSO_WAIT_MS=200 XCL_TWO_PASS_MB=3072, each build + #684 and #2951 so the launches are quick), a frame dump every 15 frames, judged on the dumps later than 200 s after the first game frame:af2cecc4eae0150271b9f7b2(merge of #2896)7e63fc6d(merge of #2892)eff3bcab(merge of #2893)3f39671d(head)The two "pictures" in the black rows are the last loading-screen frames. From #2892 on, every dump in the world is 100 % black, or black with only the title's overlay text, for as long as the run lasts (15 minutes in three runs). The present rate is the same on both sides (about 2 per second here), there is no crash and no device removal, so nothing else points at it: the title just renders a black scene. On an AMD CPU the patch does nothing, which fits that most testers do not see this.
My guess from the commit message only (I have not debugged it): the stand-in does not zero exactly what the loop would have (a different length, alignment or end pointer in some loop shape), so the title's memset leaves or clears something the lighting depends on.
A run of the head with
XCL_CLZERO_PATCH=0is in progress to confirm from the other side; result follows here. If someone on Intel sees a lit world onnvidia-devat or after7e63fc6d, that would contradict this, so please say so.Numbers are from the logs of these runs. AI use: Claude Code (Claude Fable 5.2) ran the bisect and wrote this.
@Anon64 on GitHub · 2026-11-27 11:47 UTC · original
On @throny's guess that the stand-in zeroes something other than the loop: a code check, not a run. It rules that out for the retail title, so the cause is more likely a side effect of the patch.
clzero; add rax, 0x40; cmp rax, <r10 | r9 | r8 | rdx>; jb(the4C/48 39 C?form, sorax - end). Five run back to back, each starting at the previous one's end register (mov rax, r10; add r10, <size>between them), and the last is followed bysfence.clzero_loop_thunkonnvidia-dev): it zeroesceil((end - rax) / 64)whole lines fromrax & !63, at least one, the same lines the loop does. It leavesraxatstart + n * 64and the flags of the lastcmp rax, end, and it restoresrcxandrdi. The REX bits ofmov rcx, <end>and of the finalcmpare right forr8–r10.jmp/jcc/callwith a target inside the 12 patched bytes (offsets 1–11) found none, so no path lands in thecalloffset or thenoppadding.Two leads I have not tested:
code_near's arena: itVirtualAllocs 64 KiB in the first free place above each patched image. If the title or XCL later wants that range at a fixed address (the console's layout above the image), that allocation would now fail or move.emulate_console_only(thread dumps caught a thread there at every look during the world load). Now the memsets are instant, which can expose an ordering race somewhere else that the slow path hid.XCL_CLZERO_PATCH=0on the head, @throny's run, separates the patch from everything else. If it is the patch, keeping the patch but placing the arena differently would test lead 1.Tested: none at runtime. Reading
nvidia-deveh.rsplus a local scan of the title's code on an i9-14900KF (no CLZERO), nothing from the title posted.AI use: Claude Code (Claude Opus 5.5) read the code, ran the scan and wrote this comment.
@throny on GitHub · 2026-11-27 12:11 UTC · original
Follow-up, and it is less clean than I hoped.
XCL_CLZERO_PATCH=0on the head does not give back the behaviour of the commit before this PR on this machine:71b9f7b2(before #2892)7e63fc6dand the head3f39671d,XCL_CLZERO_PATCH=0(3 runs, two builds)So the bisect result stands (the merge of this PR is the first black commit), and @Anon64's reading that the stand-in zeroes the right lines may well be right, but the switch is not a clean way back: with it off, loading on the head is more than twice as slow as it was before the PR, although both should be the same emulation. Something else in this PR or after it depends on the patch being on, or the emulation path changed. I have not looked into which. If it helps, I can run
7e63fc6ditself with the switch off, or any instrumented build you give me, on this Intel + RTX 4080 machine.AI use: Claude Code (Claude Fable 5.2) ran the tests and wrote this.
@Anon64 on GitHub · 2026-11-27 12:23 UTC · original
@throny, on why
XCL_CLZERO_PATCH=0on the head is not the old behaviour: #2892 itself can't be the cause.git diff 71b9f7b2 7e63fc6dis additions only (+145 ineh.rs, +4 inloader.rs, 0 lines removed). The emulated CLZERO path inemulate_console_onlyis unchanged. The only new call is theloader.rsstep that callspatch_clzero_loops, and that returns before doing anything whenXCL_CLZERO_PATCH=0. So at7e63fc6dwith the switch off, the code that runs is71b9f7b2's.That means the slower loading you saw comes from the merges after it. On
nvidia-dev's first-parent history between7e63fc6dand3f39671dthat is #2893 (eff3bcab), #2894,8c3f1d10(16 MB fill a submit),e0821b12,ecc0bb57,7bf05109,6f8ccb4aand3f39671d(fills within 256 MB). Your table already hints at that: the head with the patch on also went black, and 2 of the 3 rows after7e63fc6dare later merges.The run that separates the two is the one you offered:
7e63fc6dwithXCL_CLZERO_PATCH=0. If it loads in about 200 s with a lit world, as71b9f7b2does, then the patch causes the black world, and the slow loading on the head is a separate regression in the later merges. If it is black, or as slow as the head, something outside the code is different between the runs (caches, driver state). Nothing in the diff would explain it.Tested: none at runtime. From
git diffandgit log --first-parentonnvidia-dev.AI use: Claude Code (Claude Opus 5.5) read the diffs and wrote this comment.