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

Manually merged
github merged 1 commit from gh-pr/290/head into gh-pr/290/base 2026-11-26 04:18:56 +00:00
Owner

@⁠AlpPesen on GitHub · 2026-11-23 22:28 UTC · original
AlpPesen:fix/clzero-loop-semantics → main · +140 −13 · 2 files · 1 commits · merged 2026-11-23 23:20 UTC

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.
**@⁠AlpPesen** on GitHub · 2026-11-23 22:28 UTC · [original](https://github.com/xcl-dev/xcl/pull/290) `AlpPesen:fix/clzero-loop-semantics` → `main` · +140 −13 · 2 files · 1 commits · merged 2026-11-23 23:20 UTC 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 #278.
Assisted-by: Antigravity (Gemini 4 Neon)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:18:55 +00:00
github manually merged commit 9e9551b0ec into gh-pr/290/base 2026-11-26 04:18:56 +00:00
Author
Owner

@⁠PHeitor1 on GitHub · 2026-11-23 22:47 UTC · original

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).

**@⁠PHeitor1** on GitHub · 2026-11-23 22:47 UTC · [original](https://github.com/xcl-dev/xcl/pull/290#issuecomment-7116191070) 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)._
Author
Owner

@⁠mastrr9 on GitHub · 2026-11-23 22:52 UTC · original

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)

**@⁠mastrr9** on GitHub · 2026-11-23 22:52 UTC · [original](https://github.com/xcl-dev/xcl/pull/290#issuecomment-7116289463) 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)*
This discussion has been locked. Commenting is limited to contributors.
No description provided.