Repository navigation
PE loader: a title's CLZERO loops run as native code on a CPU without CLZERO - #2892
Conversation
… 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>
|
Tested and reviewed commit Tested
Review notes
Not tested
AI use
|
…SKIP, probe past xcl-dev#2891)
|
Focused Intel/Wine verification of exact head 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.
Passing owner fixtures:
Evidence: 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. |
…st and version Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… their first trap On a CPU without them these instructions trap at every run, and exception handling is slow under Wine: a retail title's wait loop trapped on MONITORX/ MWAITX ~6,000 times a second (a core's worth of handling), and its CLZERO loops ran thousands of times a second. - MONITORX / MWAITX: rewritten in place at the first trap to a NOP of the same length (`0F 01 FA/FB` -> `0F 1F FA/FB`, one byte, one atomic store); the title's loop polls its memory itself. `XCL_MWAITX_PATCH=0`: emulated as before. - A CLZERO loop (`clzero; add rax,0x40; cmp rax,r; jb`, after the 10-byte NOP pad compilers put before loops) is rewritten once to jump to a 58-byte stub near the image that zeroes the same lines with plain stores. `XCL_CLZERO_PATCH=0`: emulated as before. - A thread that trapped before another thread's rewrite runs the new code. nvidia-dev already rewrites CLZERO loops at image load (xcl-dev#2892, `patch_clzero_loops`); this rewrite acts at a loop's first trap, for the loops that still trap. Which loops still reach it on this branch is not measured. Upstream issue xcl-dev#2497 (ours) describes the CLZERO case. Tests included. Platform: all Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
#2892 (CLZERO loops as native code on a CPU without CLZERO) turns the open world black on an Intel CPU. Found by bisecting
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 Numbers are from the logs of these runs. AI use: Claude Code (Claude Fable 5.2) ran the bisect and wrote this. |
…time rewrite nvidia-dev rewrites a title image's CLZERO loops when it is loaded (xcl-dev#2892, `patch_clzero_loops`); our commit 719be72 also rewrote them at their first trap. Checked against the retail title's executable: the five loops our runs rewrote (end registers r10, r10, r9, r8, rdx) all have the 12-byte form the load-time scan rewrites, as do all 6 loops in its code. So our trap-time CLZERO rewrite and its stub are taken out; the MONITORX / MWAITX rewrite at the first trap stays (nvidia-dev emulates those at every pass). Platform: all Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
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.
Two leads I have not tested:
Tested: none at runtime. Reading AI use: Claude Code (Claude Opus 5.5) read the code, ran the scan and wrote this comment. |
|
Follow-up, and it is less clean than I hoped.
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 AI use: Claude Code (Claude Fable 5.2) ran the tests and wrote this. |
|
@throny, on why That means the slower loading you saw comes from the merges after it. On The run that separates the two is the one you offered: Tested: none at runtime. From AI use: Claude Code (Claude Opus 5.5) read the diffs and wrote this comment. |
|
Correction: my bisect above is wrong, and #2892 is not shown to blacken anything. I am sorry for the noise. What I got wrong: I judged "visible" from frame dumps taken later than 200 s after the first game frame, assuming the title is in the world by then. That holds only for builds with this PR. Without it (the commits before, and What the data does show:
@Anon64's reading of the stand-in stands, and I withdraw the guess about it. I have corrected the pointers on #505, #2572 and #3011. AI use: Claude Code (Claude Fable 5.2) made the mistake and wrote this correction. |
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>; 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(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):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