Skip to content

cpu_emulation: the executable's CLZERO zeroing loop is finished in one trap (retail frame loop on Intel: about 2.4 to 46 frames a second; depends on #7111) - #8783

Closed
joroyt wants to merge 7 commits into
ProjectLeonida:mainfrom
joroyt:clzero-loop
Closed

joroyt wants to merge 7 commits into
ProjectLeonida:mainfrom
joroyt:clzero-loop

Conversation

@joroyt

@joroyt joroyt commented Nov 23, 2026 •

Copy link
Copy Markdown

Claimed in the coordination issue as: new RT item (RT-500 follow-up), #8683 (comment)

Depends on: #7111 (this branch is on its head 39b757a6d; review only the last commit, 58f7d1ca6)

What and why

#7111 emulates CLZERO from the #UD handler, one instruction per trap. The executable's 10 CLZERO sites are one routine that zeroes memory a line at a time with the same four instructions:

clzero
add  rax, 64
cmp  rax, <register>
jb   <the clzero>

so every 64 bytes cost one exception round trip, about 1.9 us here. A boot reaches this loop, and the title's frame loop runs it all the time: on a retail boot one title thread sat at 100 % of a core with its RIP on the CLZERO (4 of 24 stack samples), and the title presented about 2.4 frames a second.

finish_clzero_loop (runtime/src/cpu_emulation.h): after a CLZERO has been emulated, when the bytes at RIP are exactly that tail, the handler zeroes every further line the loop would, sets RAX, the arithmetic flags (those of the last cmp) and RIP as the loop leaves them, and continues after the branch. A line that is not committed, is guarded or cannot be written ends it early with RIP back at the CLZERO and RAX in that line, so that iteration traps again and raises its access violation as before (a page that is committed on first touch still gets its fault this way). Any other code after a CLZERO is emulated as before. Nothing is patched in the image.

Measured

Intel Core i9-14900KF (no SSE4a, CLZERO, MONITORX), NVIDIA RTX 5070 (driver 32.0.16.2124), Windows 11, MSVC 14.51 RelWithDebInfo, LEONIDA_D3D12=vulkan, own 1.0.2.1 copy.

Before After
1 MiB loop in cpu_emulation_test (16,376 lines) 31.8 ms 0.02 to 0.04 ms
Retail frame loop, frames presented a second about 2.4 (167 in 68 s) about 46 (6,000 in 129 s)

The retail numbers are from a local integration stack (tibzejoker's rt5-integration plus the open boot-chain pull requests up to a frame loop that presents, with #7111 and this change), not from this branch alone. The first loop the boot ran through it zeroed 229 lines in one trap. It does not change what the title draws: the frames were black before and after. Counts only, nothing from the executable.

For #8537's table: its boot stopped about 4 s in, before the loop is reached, so it saw no CLZERO. A boot that reaches the frame loop spends its time there.

Tested

  • cpu_emulation_test: all checks pass on the i9-14900KF (every form through the handler). New: test_clzero_loop (the lines zeroed, RAX/RIP/flags for a limit on and inside a line, cmp rax, rcx, nothing left to do, another instruction after the CLZERO, a branch that does not go back to the CLZERO, a read-only page) and, live, the whole loop over 1 MiB and into a read-only page (access violation, with the lines before it zeroed). The live cases run natively on a host with CLZERO, so there they check the instruction itself; I have no such host.
  • With the call in the handler removed, the same test binary takes 31.8 ms for the 1 MiB loop and still passes: the result is the same either way, only the time differs.
  • gta6rt builds; the retail run above.
  • python -I scripts/check_no_game_data.py on the four changed files: clean.
  • tests/test_docs_*.py: the same three fail as on RT-500: gta6rt emulates the Zen 2 instructions an Intel CPU lacks (INSERTQ/EXTRQ, CLZERO, MONITORX/MWAITX, CLWB) #7111's head (test_docs_cli_flags, test_docs_design_rows, test_docs_links); the others pass.

Not shown

  • Not run on an AMD host (the native path), under Wine, or with clang-cl.
  • Only this loop shape is recognised. The INSERTQ/EXTRQ sites in vector code are still one trap each; how often a boot reaches them is not measured here.

AI-assisted: yes (Claude, in OpenCode).

Krashgamer and others added 7 commits November 23, 2026 16:44
… desktop CPU may lack

The runtime runs the Xbox Series (Zen 2) executable natively, so an instruction the host CPU lacks raises #UD
where it is reached. tools/cpu_feature_scan.py streams dumpbin /disasm like inline_scan.py, maps each
instruction to its CPUID feature or privilege class (AMD-only, newer-Intel-only, not on Zen 2, privileged,
kernel call, host query, baseline), and splits the counts by .pdata range.

docs/cpu_features.md records the 1.0.2.1 result (counts only): 19 SSE4a, 10 CLZERO and 4 MONITORX/MWAITX
sites (AMD only) and 8 CLWB sites, all real code; the privileged hits are jump-table data.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… the first

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…Q/EXTRQ, CLZERO, MONITORX/MWAITX, CLWB)

The executable uses SSE4a INSERTQ, CLZERO and MONITORX/MWAITX (AMD only) and CLWB (Intel since Ice Lake)
without asking CPUID (docs/cpu_features.md). A host without one raises #UD there. src/cpu_emulation.h adds
a vectored handler that recognises those encodings at the faulting RIP, does the same work on the CONTEXT
and steps past the instruction; gta6rt installs it before any image code runs, after the startup trace's
handler so it runs first. One [rt] line names what the host lacks, one more per instruction on first use.

cpu_emulation_test checks the decoder on hand-written encodings, the bit-field helpers and apply() on a
synthetic CONTEXT, then runs every form for real from an executable buffer: natively on a Zen host,
through the handler on an Intel one, with the same expected values.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ult; accept 66 on 0F 01 FC/FA/FB

A line CLZERO cannot write now reaches the next handlers as the access violation (or guard-page
violation) the instruction raises, with the write flag and the line address, instead of the #UD.
The 66 prefix and segment overrides are accepted on CLZERO/MONITORX/MWAITX; F2/F3 (other
instructions there, F3 0F 01 FA is MCOMMIT) and LOCK stay #UD. Points raised by SirKingg on ItsNotRexed#7055.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… xmm1[127:64]; AMD prefix forms; no SEH

- INSERTQ xmm, xmm took the length and index from swapped fields. The APM (vol. 4) has the length in
  xmm2[69:64] and the index in xmm2[77:72]; a Ryzen 9800X3D and a Ryzen 5700X agree (TheHoracio,
  Mayzkl on ItsNotRexed#7111). The measured 0x0810 case is a unit check now.
- INSERTQ/EXTRQ zero xmm1[127:64], as the Zen 3 measured in the same review does in all four forms.
- Decode REX bytes among the legacy prefixes (a non-final REX is ignored) and accept F2/F3 on CLZERO,
  as Zen 3 runs them (ItsNotRexed#7055's reviews); F2/F3 stay #UD on MONITORX/MWAITX.
- Check the CLZERO line's page and the instruction bytes' readability with VirtualQuery instead of
  catching a fault in a local __try, which clang-cl without /EHa does not cover for inline stores
  (venetanji on ItsNotRexed#7111).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…on and xg_xs test targets)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…e trap (retail frame loop on Intel: about 2.4 to 46 frames a second)

@abnalstlll-byte abnalstlll-byte left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on Windows 11.

  • Massive performance optimization for Intel CPUs: recognizes the hot memory-zeroing loop tail (\clzero; add rax, 64; cmp rax, limit; jb) in the #UD exception filter and finishes remaining iterations in a single trap via \memset.
  • Accurately reconstructs CPU flags (CF, PF, AF, ZF, SF, OF) matching the final \cmp rax, limit\ instruction.
  • Safe page boundary protection: halts early and steps back to the \clzero\ instruction if encountering uncommitted, read-only, or guard pages so the kernel raises the authentic access/guard violation.
  • \ ools/cpu_feature_scan.py\ passes compileall and cleanly lists Zen 2 instructions.
  • Clean-room check and \git diff --check\ pass on all changed files.

LGTM!

@lohant27-netizen

Copy link
Copy Markdown

Tested 58f7d1ca6 on Windows 11 (10.0.26300), NVIDIA GeForce RTX 5070 (driver 32.0.16.2101, subgroup 32), MSVC with SDK 10.0.26100, Release, merged onto main 867d922f0 together with #8815, #8814, #8809, #8784, #8783 and #8813 (no conflicts): pass.

@BatSmacker84

Copy link
Copy Markdown

Checked 58f7d1ca6 (on #7111 at 39b757a6d; merges cleanly onto main 6312c057f) on Windows 11 26200, AMD Ryzen 7 9800X3D, MSVC. Static checks against the retail executable; the runtime was not built and the game was not run.

Pass

  • Count and place: a full dumpbin sweep decodes 10 CLZERO sites, 0x4e8aa70 through 0x4e8abb4, clustered in what looks like one leaf routine outside .pdata. One further raw opcode match does not decode as CLZERO.
  • The standalone CPU tests build and pass; on this CPU the live cases use native CLZERO.

Needs changes

  • Not every site has the loop shape the handler recognizes. Suggested: "Six of the ten CLZERO sites have the exact tail recognized by the handler. The other four (0x4e8ab53, 0x4e8ab72, 0x4e8ab91, 0x4e8abb4) compare immediately after CLZERO and branch to an earlier instruction, so they keep per-instruction emulation."
  • The boot and rendering claims go beyond the reported runs. Suggest "a zeroing loop reached in the measured boot" for "the one such loop a boot reaches", and "frames were black in both measured runs" for the drawing claim.

Not checked here: Intel timings, retail reachability (including #8537), the samples, presentation rates. That needs cpu_emulation_test.exe on Intel with and without batching, then a gta6rt --run <exe> --trace-d3d12 boot with LEONIDA_D3D12=vulkan on both builds.

AI-assisted: Codex GPT-6.2 Sol ran the checks, Claude Opus 5.5 reviewed the report. The executable was read in place (SHA-256 e95020b2…8993); only counts and offsets are given.

@Anarbb

Anarbb commented Nov 23, 2026

Copy link
Copy Markdown

checked: #878358f7d1c (the last commit, on top of #7111) | with: the same cross-build as my #7111 check | on: Linux 6.18, Proton-CachyOS 11.0 (wine-11.0), Intel Core i5-12400F (no CLZERO), RTX 4060, driver 620.70.06 | result: pass under Wine. The retail frame loop runs with PresentX called about 200 times a second (the frames are still black, as reported on #8851), and a 330 s run ended without an exception. Base as in my #7111 check. No A/B against #7111 alone.
agent: yes

AlexLopezDevelop added a commit to AlexLopezDevelop/Leonida that referenced this pull request Nov 23, 2026
… docs: three hosts and the frame loop

--bench timed CLWB on every host and called the native instruction "the
emulation cost" where CLWB is native (towade, fiestorra, SpaghettiMaster545
on ItsNotRexed#8537). It now times each of CLWB, CLZERO and EXTRQ that the host lacks,
against a NOP of the same length, and says there is nothing to time on a host
that has all three.

cpu_features.md: the Raptor Lake and Zen 4/5 rows from the reviews, the
measured scope (up to the slot 41 assert), the CLZERO sites a boot that reaches
the frame loop adds, and ItsNotRexed#8783 for the hot CLZERO loop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@iliassgm

Copy link
Copy Markdown
Contributor

checked: #878358f7d1c (last commit, stacked on #711139b757a) | base: main@5d74c2723 (clean 3-way merge) | on: native Windows 11 x64, Intel Core i5-10400F (Comet Lake: SSE4a 0, MONITORX 0, CLZERO 0, CLWB 0), MSVC 19.44, Windows SDK 10.0.26100, RelWithDebInfo, synthetic tests only | result: pass.

  • Build: cpu_emulation_test and gta6rt compile cleanly (0 warnings in modified files).
  • Live #UD emulation on i5-10400F (cpu_emulation_test.exe, exit 0): all 4 Zen-2 features emulated live (insertq imm, extrq, clzero, clzero loop: 16376 lines in 0.044 ms (emulated), monitorx/mwaitx, clwb; cpu_emulation: all checks passed).
  • python -I tests/test_cpu_feature_scan.py: 22/22 OK.
  • Negative control (restoring #7111 base runtime/src/cpu_emulation.h): fails to compile (error C2039: 'finish_clzero_loop').
  • scripts/check_no_game_data.py on modified files: clean.

agent: yes

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.

8 participants