Skip to content

Console CPU instructions: fix ClzeroLoop ModRM check and loop semantics - #290

Merged
ivall merged 1 commit into
ivall:mainfrom
AlpPesen:fix/clzero-loop-semantics
Nov 23, 2026
Merged

ivall merged 1 commit into
ivall:mainfrom
AlpPesen:fix/clzero-loop-semantics

Conversation

@AlpPesen

Copy link
Copy Markdown
Contributor

Fixes items 1 and 2 of #278 (ClzeroLoop decoder false-positive and loop emulation semantics).

insn::decode previously matched [48|4C] 39 <modrm> 72 F4 without checking (modrm & 0xC7) == 0xC0, allowing non-rax or memory cmp forms to decode as ClzeroLoop. In eh::emulate_console_only, ClzeroLoop also skipped 64-byte cache-line alignment, zeroed partial bytes instead of full 64-byte steps when end - start was not a multiple of 64, skipped the first clzero when end <= start while still overwriting ctx.Rax = end, and left ctx.EFlags stale.

Overlap note: touches crates/xcl/src/insn.rs and crates/xcl/src/eh.rs (same files as #234, disjoint hunks).

What is in it

  • crates/xcl/src/insn.rs: require (modrm & 0xC7) == 0xC0 and end_reg != 0 for ClzeroLoop, with negative decoder tests (cmp rdx, rcx, cmp [rax], rdx, cmp rax, rax).
  • crates/xcl/src/eh.rs: align ClzeroLoop to 64-byte lines, advance Rax in 64-byte steps, update arithmetic EFlags via cmp_eflags, and fall back to stepping the single clzero (len - 9) when end <= start or the full span cannot be fast-forwarded; adds unit tests for cmp_eflags and aligned/unaligned/end <= start ClzeroLoop cases.

Tested

  • Hardware: Windows 11 Pro for Workstations (Build 26300), Intel Core i9-9900K (Coffee Lake, pre-Ice Lake — no CLZERO / CLWB), AMD Radeon RX 6800 (16337 MiB VRAM, driver 32.0.21045.29003; note smoke.ps1's WMI query prints the first non-Basic adapter Parsec Virtual Display Adapter, while XCL renders on the RX 6800), rustc 1.100.0, DXC 1.9.2602.17 (debug for tests, release for smoke runs).
  • XCL_REQUIRE_GPU=wave64,mesh + .\scripts\test.ps1: 653 passed, 0 failed, 40 ignored, 0 GPU skipped (xcl lib: 613 passed / 20 ignored; integration/e2e: 40 passed / 20 ignored; XCL_SAMPLE_* unset).
  • Real title (YouKnowWho): confirmed ClzeroLoop { end_reg: 10 } ran on i9-9900K in xcl.log with 0 crashes and 0 device removals.

Regression run
Both runs below used the same warmed shader cache on YouKnowWho (60 s):

main (9ab7d763197c):

xcl-smoke v1
commit: 9ab7d763197c7f420bff10cd9bf1310f07e2a9aa
gpu: Parsec Virtual Display Adapter
title: YouKnowWho
seconds: 60
exit: 0x0
frames shown: 687 (PresentX 687)
presents per 2 s (last 3): 14 7 7
device removals: 0
crashes: 0
peak commit: 15890 MB

PR head (9e9551b0ec19):

xcl-smoke v1
commit: 9e9551b0ec198115e9661246ce01b69532c4683e
gpu: Parsec Virtual Display Adapter
title: YouKnowWho
seconds: 60
exit: 0x0
frames shown: 678 (PresentX 679)
presents per 2 s (last 3): 8 7 8
device removals: 0
crashes: 0
peak commit: 15910 MB

Not tested

  • AMD host CPU native execution path (no AMD host CPU here).
  • Sample-data tests (XCL_SAMPLE_* unset).

AI use

  • Implemented, tested, and verified with Antigravity (Gemini 4 Neon); every change and test result reviewed by the author on local hardware.

Decode ClzeroLoop only when the trailing cmp is register-register against rax
((modrm & 0xC7) == 0xC0) and end_reg != rax, so memory compares or compares on
other registers fall back to stepping the single clzero.

In emulate_console_only, align the zeroed range to 64-byte cache lines, round
the iteration count up to full 64-byte steps, update EFLAGS for cmp rax, end,
and fall back to stepping the single clzero (len - 9) when end <= start, the
range overflows/exceeds the fast-forward bound, or the full span is not
writable.

Part of ivall#278.
Assisted-by: Antigravity (Gemini 4 Neon)
T-Crypt added a commit to T-Crypt/bathyal that referenced this pull request Nov 23, 2026
@PHeitor1

Copy link
Copy Markdown

Tested 9e9551b merged onto main 9ab7d76 on RTX 5060 Ti 8 GB (Blackwell, 32-lane waves), NVIDIA 621.24, Ryzen 5 5600X (Zen 3), Windows 11 Pro 26300 (native), rustc 1.100.0 MSVC, debug, cargo test --workspace --no-fail-fast -- --test-threads=4, no XCL_SAMPLE_*. Baseline main 9ab7d76: 651 passed, 0 failed, 40 ignored.

Pass: 653 passed, 0 failed, 40 ignored. GPU skips: the same 2 as on main (no wave64).

Run and written with Claude Code (Claude Opus 5.5).

@ethandepstein-del

Copy link
Copy Markdown

Independent static code & x86-64 ISA audit of 9e9551b0e (code inspection only — cargo test not run on macOS; no game files):

Summary: Looks clean

(Note: overlaps with #295, which also addresses #278.)

  • cmp_eflags (eh.rs:269-293): Verified all 6 arithmetic flags (CF, PF, AF, ZF, SF, OF) match x86-64 cmp lhs, rhs while preserving non-arithmetic EFLAGS bits.
  • ModRM & fallback (insn.rs:125-134, eh.rs:351-376): (modrm & 0xC7) == 0xC0 + reg_idx != 0 cleanly rejects non-rax r/m, memory operands, and cmp rax, rax. Stepping the single clzero (step = len - 9) on fallback cleanly lets the CPU run add / cmp / jb natively for end <= start, wrapping, and cross-region spans.
  • Minor note (insn.rs:125): Requiring i == 0 (no prefixes at all before 0F 01 FC) for ClzeroLoop is slightly stricter than !code[..i].contains(&0x67) since jb -12 (72 F4) targets rip + i.

(AI-assisted)

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.

4 participants