x64_recomp: add cmpxchg8b and cmpxchg16b emitters #1357

Open
github-bot wants to merge 1 commit from github-bot/gh-1357 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-24 22:24 UTC · original
IlyassRS3:feat/x64-recomp-cmpxchg8b-16b → main · +590 −2 · 2 files · 1 commits · open

Item ID: X64-RECOMP-CMPXCHG8B-16B

Summary

Adds decoding, classification, ARMv8.0 exclusive-pair assembler helpers, and code generation for x86-64 double-width compare-and-exchange instructions cmpxchg8b (0F C7 /1) and cmpxchg16b (REX.W + 0F C7 /1) in tools/x64_recomp.py:

  • Decoder (OF[0xC7] / _finish_group grp9):
    • Decodes 0F C7 /1 with mod != 3 as cmpxchg8b (m64, size 8) or, when REX.W is set, cmpxchg16b (m128, size 16).
    • Resolves RIP-relative abs targets, ignores REX.R for the /1 opcode extension and ignores 66 operand-size prefix per Intel SDM, and marks mod == 3 or non-/1 group 9 forms (rdrand, rdseed, vmptrld, etc.) as unknown with accurate instruction lengths.
  • Flag tracking & classification:
    • Registers "cmpxchg8b": "Z" and "cmpxchg16b": "Z" in FLAG_WRITERS and classifies them as ins.kind = "cas2", ins.flags_w = "Z".
  • ARMv8.0 pair exclusive helpers (Asm.ldaxp, Asm.stlxp, Asm.clrex):
    • Bit-verified against clang -arch arm64 + otool -t: LDAXP (0x887F8000 / 0xC87F8000), STLXP (0x88208000 / 0xC8208000), and CLREX (0xD5033F5F), with register-constraint assertions (rt1 != rt2, rs not in (rt1, rt2, rn)).
  • Emitter (_emit_cas2):
    • Computes effective address before touching RAX/RDX (safe when the base/index register aliases RAX/RDX, e.g., lock cmpxchg8b [rax]).
    • For LOCK-prefixed instructions, checks alignment via non-flag-setting and_i + cbnz (8-byte for cmpxchg8b, 16-byte for cmpxchg16b), runs an ARMv8.0 LDAXP/STLXP retry loop (calling CLREX on mismatch), and falls back to non-exclusive ldr/str pairs on unaligned addresses to avoid SIGBUS. Unlocked instructions emit direct ldr/str pairs without LL/SC overhead.
    • Compares without clobbering NZCV (eor_r + orr_r + cbnz). For cmpxchg8b, compares low 32 bits (EDX:EAX), preserves upper 32 bits of RAX/RDX on match, and zero-extends RAX/RDX on mismatch.
    • Updates only ZF: patches bit 30 of ARM NZCV via mrs_nzcv + bfi + msr_nzcv when Z is live, and patches bit 6 (EF_ZF) of ctx.eflags when materialized — preserving CF, SF, OF, PF, and AF (including when ZF is dead).

Tests (tests/test_x64_recomp_cmpxchg16b.py)

  • 3 static tests: group-9 decode/rejection/RIP-relative checks, bit-exact LDAXP/STLXP/CLREX encodings and constraint assertions, and LOCK vs unlocked LL/SC emission checks.
  • 3 hardware execution suites: cmpxchg8b match/mismatch/upper-32-bit zero-extension/unaligned +1/+4/[rax] aliasing, cmpxchg16b 128-bit match/bit-63 mismatch/unaligned +8/Windows _InterlockedCompareExchange128 retry loop, and CF/SF/OF/PF preservation across same-block ARM NZCV, cross-block materialized ctx.eflags, and dead-ZF paths.

Verification

  • Host: macOS (Apple M5, arm64), Python 3.
  • Standalone checks (no game files):
    • python3 -I tests/test_x64_recomp_cmpxchg16b.py — 6 tests (3 static OK, 3 exec skipped without runner)
    • python3 -I tests/test_x64_recomp.py — OK
    • python3 -m compileall -q make_game.py tools tests — OK
    • python3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_cmpxchg16b.py — OK
    • python3 -I tests/test_ci_registration.py — 15/15 OK
    • python3 -I tests/test_text_write_line_endings.py — 3/3 OK
  • Mutation proof: reverting tools/x64_recomp.py to upstream/main fails all static test methods (10 failures, 1 error).
  • Native Apple M5 hardware execution (14-way scratch merge with #1174 and all open x64_recomp PRs, 0 conflicts):
    • tests/test_x64_recomp_cmpxchg16b.py: 6/6 passed (0 skipped)
    • All 13 test_x64_recomp_*.py suites combined: 108/108 passed (0 skipped)

AI-assisted: yes

**@⁠IlyassRS3** on GitHub · 2026-11-24 22:24 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1357) `IlyassRS3:feat/x64-recomp-cmpxchg8b-16b` → `main` · +590 −2 · 2 files · 1 commits · open Item ID: `X64-RECOMP-CMPXCHG8B-16B` ### Summary Adds decoding, classification, ARMv8.0 exclusive-pair assembler helpers, and code generation for x86-64 double-width compare-and-exchange instructions `cmpxchg8b` (`0F C7 /1`) and `cmpxchg16b` (`REX.W + 0F C7 /1`) in `tools/x64_recomp.py`: - **Decoder (`OF[0xC7]` / `_finish_group` `grp9`)**: - Decodes `0F C7 /1` with `mod != 3` as `cmpxchg8b` (`m64`, size 8) or, when `REX.W` is set, `cmpxchg16b` (`m128`, size 16). - Resolves RIP-relative `abs` targets, ignores `REX.R` for the `/1` opcode extension and ignores `66` operand-size prefix per Intel SDM, and marks `mod == 3` or non-`/1` group 9 forms (`rdrand`, `rdseed`, `vmptrld`, etc.) as `unknown` with accurate instruction lengths. - **Flag tracking & classification**: - Registers `"cmpxchg8b": "Z"` and `"cmpxchg16b": "Z"` in `FLAG_WRITERS` and classifies them as `ins.kind = "cas2"`, `ins.flags_w = "Z"`. - **ARMv8.0 pair exclusive helpers (`Asm.ldaxp`, `Asm.stlxp`, `Asm.clrex`)**: - Bit-verified against `clang -arch arm64` + `otool -t`: `LDAXP` (`0x887F8000` / `0xC87F8000`), `STLXP` (`0x88208000` / `0xC8208000`), and `CLREX` (`0xD5033F5F`), with register-constraint assertions (`rt1 != rt2`, `rs not in (rt1, rt2, rn)`). - **Emitter (`_emit_cas2`)**: - Computes effective address before touching `RAX`/`RDX` (safe when the base/index register aliases `RAX`/`RDX`, e.g., `lock cmpxchg8b [rax]`). - For `LOCK`-prefixed instructions, checks alignment via non-flag-setting `and_i + cbnz` (8-byte for `cmpxchg8b`, 16-byte for `cmpxchg16b`), runs an ARMv8.0 `LDAXP`/`STLXP` retry loop (calling `CLREX` on mismatch), and falls back to non-exclusive `ldr`/`str` pairs on unaligned addresses to avoid `SIGBUS`. Unlocked instructions emit direct `ldr`/`str` pairs without LL/SC overhead. - Compares without clobbering `NZCV` (`eor_r + orr_r + cbnz`). For `cmpxchg8b`, compares low 32 bits (`EDX:EAX`), preserves upper 32 bits of `RAX`/`RDX` on match, and zero-extends `RAX`/`RDX` on mismatch. - Updates **only** `ZF`: patches bit 30 of ARM `NZCV` via `mrs_nzcv + bfi + msr_nzcv` when `Z` is live, and patches bit 6 (`EF_ZF`) of `ctx.eflags` when materialized — preserving `CF`, `SF`, `OF`, `PF`, and `AF` (including when `ZF` is dead). ### Tests (`tests/test_x64_recomp_cmpxchg16b.py`) - 3 static tests: group-9 decode/rejection/RIP-relative checks, bit-exact `LDAXP`/`STLXP`/`CLREX` encodings and constraint assertions, and LOCK vs unlocked LL/SC emission checks. - 3 hardware execution suites: `cmpxchg8b` match/mismatch/upper-32-bit zero-extension/unaligned `+1`/`+4`/`[rax]` aliasing, `cmpxchg16b` 128-bit match/bit-63 mismatch/unaligned `+8`/Windows `_InterlockedCompareExchange128` retry loop, and `CF`/`SF`/`OF`/`PF` preservation across same-block ARM `NZCV`, cross-block materialized `ctx.eflags`, and dead-`ZF` paths. ### Verification - **Host**: macOS (Apple M5, arm64), Python 3. - **Standalone checks (no game files)**: - `python3 -I tests/test_x64_recomp_cmpxchg16b.py` — 6 tests (3 static OK, 3 exec skipped without runner) - `python3 -I tests/test_x64_recomp.py` — OK - `python3 -m compileall -q make_game.py tools tests` — OK - `python3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_cmpxchg16b.py` — OK - `python3 -I tests/test_ci_registration.py` — 15/15 OK - `python3 -I tests/test_text_write_line_endings.py` — 3/3 OK - **Mutation proof**: reverting `tools/x64_recomp.py` to `upstream/main` fails all static test methods (10 failures, 1 error). - **Native Apple M5 hardware execution (14-way scratch merge with #1174 and all open `x64_recomp` PRs, 0 conflicts)**: - `tests/test_x64_recomp_cmpxchg16b.py`: **6/6 passed** (0 skipped) - All 13 `test_x64_recomp_*.py` suites combined: **108/108 passed** (0 skipped) AI-assisted: yes
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:48:38 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-24 22:24 UTC · original · reviewed

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-24 22:24 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1357#pullrequestreview-6048361463) · **reviewed** Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
Author
Owner

@⁠kevincommits on GitHub · 2026-11-26 02:57 UTC · original

Independent run with QEMU. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew. I merged main at 7f3d0095a into the branch locally (940 commits behind, no conflicts). Head tested: 750fcb669.

Against a reference model cmpxchg8b and cmpxchg16b are right in all 200 checks, with and without lock. The new test file is 6/6 once two things outside these emitters are accounted for.

  • The test file, step by step (nothing changed on disk; patched in memory or merged locally):

    • as shipped under QEMU: 6 run, 21 failures, 2 errors. The failures are the wiring bug from #1409 (xr.run_kernel where xr.run_result is needed);
    • with RUNNER swapped for xr.run_result: no failures, 2 errors left, both QEMU timeouts, both the cmpxchg8b unaligned cases (unaligned_disp=1 and =4). run_kernel on main runs with the MMU off, so an unaligned guest access takes an alignment fault and hangs (harness bug, #2143);
    • with #2143 merged in locally (and the Asm.li all-ones branch from #1339/#1205/#1329 patched in memory, applied together with it): 6/6.
  • Beyond the file, generated cases under QEMU against a small Python model, operand at [rsp]:

    • cmpxchg8b and cmpxchg16b, each plain and with lock, each after stc and after clc;
    • five cases each: match, low half differs, high half differs, both differ, and an all-zero match;
    • for cmpxchg8b the upper halves of rax and rdx hold junk, which the compare must ignore;
    • checked per case: both memory qwords (the second must stay untouched for cmpxchg8b), the full rax and rdx (unchanged on a match, loaded and zero-extended on a miss), ZF, and that CF is kept.
    tool checks wrong
    this branch 200 0

    There is no main column: main does not decode 0f c7.

  • Counts. tests/test_x64_recomp.py 44/44 and tests/test_recomp_census.py 11/11, the same as main. Full standalone suite: 514 run, 512 pass; the 2 that fail are main's test_mod_pack_output_alias and the new file, for the reasons above.

  • Overlap. clrex is also added by #1306 with the same encoding; whichever lands second needs a small rebase.

Not tested: memory operands other than [rsp] beyond the file's own cases, real contention on the ldaxp/stlxp retry loop (single core here), and flags other than ZF and CF. No game files used. AI-assisted.

**@⁠kevincommits** on GitHub · 2026-11-26 02:57 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1357#issuecomment-7181270014) Independent run with QEMU. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew. I merged `main` at `7f3d0095a` into the branch locally (940 commits behind, no conflicts). Head tested: `750fcb669`. **Against a reference model `cmpxchg8b` and `cmpxchg16b` are right in all 200 checks, with and without `lock`. The new test file is 6/6 once two things outside these emitters are accounted for.** - The test file, step by step (nothing changed on disk; patched in memory or merged locally): - as shipped under QEMU: 6 run, 21 failures, 2 errors. The failures are the wiring bug from #1409 (`xr.run_kernel` where `xr.run_result` is needed); - with `RUNNER` swapped for `xr.run_result`: no failures, 2 errors left, both QEMU timeouts, both the `cmpxchg8b` unaligned cases (`unaligned_disp=1` and `=4`). `run_kernel` on `main` runs with the MMU off, so an unaligned guest access takes an alignment fault and hangs (harness bug, #2143); - with #2143 merged in locally (and the `Asm.li` all-ones branch from #1339/#1205/#1329 patched in memory, applied together with it): **6/6**. - Beyond the file, generated cases under QEMU against a small Python model, operand at `[rsp]`: - `cmpxchg8b` and `cmpxchg16b`, each plain and with `lock`, each after `stc` and after `clc`; - five cases each: match, low half differs, high half differs, both differ, and an all-zero match; - for `cmpxchg8b` the upper halves of `rax` and `rdx` hold junk, which the compare must ignore; - checked per case: both memory qwords (the second must stay untouched for `cmpxchg8b`), the full `rax` and `rdx` (unchanged on a match, loaded and zero-extended on a miss), ZF, and that CF is kept. | tool | checks | wrong | |---|---|---| | this branch | 200 | 0 | There is no `main` column: `main` does not decode `0f c7`. - **Counts.** `tests/test_x64_recomp.py` 44/44 and `tests/test_recomp_census.py` 11/11, the same as `main`. Full standalone suite: 514 run, 512 pass; the 2 that fail are `main`'s `test_mod_pack_output_alias` and the new file, for the reasons above. - **Overlap.** `clrex` is also added by #1306 with the same encoding; whichever lands second needs a small rebase. Not tested: memory operands other than `[rsp]` beyond the file's own cases, real contention on the `ldaxp`/`stlxp` retry loop (single core here), and flags other than ZF and CF. No game files used. AI-assisted.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1357/head:github-bot/gh-1357'
git switch 'github-bot/gh-1357'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1357'
git switch 'github-bot/gh-1357'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1357'
git switch 'github-bot/gh-1357'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1357'
git switch 'main'
git merge --squash 'github-bot/gh-1357'
git switch 'main'
git merge --ff-only 'github-bot/gh-1357'
git switch 'main'
git merge 'github-bot/gh-1357'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.