x64_recomp: add cmpxchg, xadd, and atomic LL/SC memory xchg emitters (X64-RECOMP-ATOMICS) #1306

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

@⁠IlyassRS3 on GitHub · 2026-11-24 21:38 UTC · original
IlyassRS3:feat/x64-recomp-atomics → main · +644 −5 · 2 files · 1 commits · open

Item ID: X64-RECOMP-ATOMICS
AI-assisted: yes

Summary

Implements cmpxchg, xadd, and ARMv8.0 LL/SC (ldaxr*/stlxr*/clrex) memory atomics in tools/x64_recomp.py, replacing the UnhandledInsn("... needs atomics") stub in _emit_xchg:

  1. Flag classification fixes:
    • Registered "cmpxchg": "NZCVPA" and "xadd": "NZCVPA" in FLAG_WRITERS, and added "cmpxchg" to SUB_KIND (so cmpxchg's subs_r borrow polarity matches cmp across _arm_cond, _maybe_mkflags, _emit_adcsbb, and _emit_flagctl).
    • Removed the bogus ins.flags_r = "NZCVPA" on cmpxchg/xadd in _classify (neither reads flags).
  2. ARMv8.0 LL/SC encodings in Asm:
    • Added ldaxr(rt, rn, size) (ldaxrb/ldaxrh/ldaxr w/ldaxr x), stlxr(rs, rt, rn, size) (stlxrb/stlxrh/stlxr w/stlxr x, asserting rs != rt and rs != rn per ARMv8 CONSTRAINED UNPREDICTABLE rules), and clrex(). Verified bit-exact against clang -arch arm64 / otool.
  3. Memory xchg (implicit LOCK):
    • Uses an ARM64 ldaxr/stlxr retry loop for naturally aligned memory accesses (with an unaligned ldr/str fallback for size > 1 using and_i + cbnz so NZCV flags are never disturbed).
    • Computes ADDR_T and loads the source register before storing, keeping xchg [rax], rax safe.
  4. cmpxchg (0F B0 / 0F B1) across 8/16/32/64-bit (reg, hi8, mem, unlocked & LOCK):
    • Equal branch stores src into dst while leaving rax untouched (preserving upper 32/48/56 bits).
    • Mismatch branch stores old_dst into al/ax/eax/rax (zero-extending rax on 32-bit), emits clrex in LOCK LL/SC paths, and zero-extends 32-bit register dst per Intel SDM (DEST := TEMP).
    • Computes all 6 flags (SF, ZF, CF, OF, PF, AF) identically to CMP ACC, old_dst across all 4 widths, including CTX_RES/CTX_A/CTX_B for materialization.
  5. xadd (0F C0 / 0F C1) across 8/16/32/64-bit (reg, hi8, mem, unlocked & LOCK):
    • Stores old_dst to src before old_dst + old_src to dst, so xadd reg, reg yields 2 * reg, and computes ADDR_T before updating src so lock xadd [rax], rax works cleanly.
    • Computes all 6 flags (SF, ZF, CF, OF, PF, AF) identically to ADD old_dst, old_src across all 4 widths.

Merge compatibility

  • Leaves PRIMARY[0x90..0x97] and decode_one untouched (zero overlap with #847, #1120, #1121, #1246).
  • Verified 0 merge conflicts in an 8-way merge with #1174, #1202, #1205, #1211, #1213, #1279, and #1296.

Verification

Tested on Apple M5 arm64 (macOS 27.2.1, Python 3.14; CPU-only, no game files):

  • python3 -I tests/test_x64_recomp.py — 32 tests (6 ok, 26 skipped without runner)
  • python3 -I tests/test_x64_recomp_atomics.py -v — 10 tests (6 static ok, 4 exec skipped without runner)
  • Mutation proof: reverting tools/x64_recomp.py to upstream/main causes 6 failures/errors across all 6 static tests in tests/test_x64_recomp_atomics.py.
  • Native Apple M5 execution (scratch 8-way merge with #1174, #1202, #1205, #1211, #1213, #1279, #1296 — 0 merge conflicts):
    • tests/test_x64_recomp_atomics.py — 10/10 OK (all 4 execution suites + 6 static tests, 0 skips)
    • tests/test_x64_recomp.py — 46/46 OK
    • tests/test_x64_recomp_sse.py — 16/16 OK
    • tests/test_x64_recomp_bitops.py — 9/9 OK
    • tests/test_x64_recomp_rcl_rcr.py — 5/5 OK
    • tests/test_x64_recomp_std_str.py — 5/5 OK
    • tests/test_x64_recomp_muldiv.py — 10/10 OK
    • tests/test_x64_recomp_shifts.py — 9/9 OK
    • Total: 110/110 native arm64 tests passing.
  • python3 -m compileall -q tools/x64_recomp.py tests/test_x64_recomp_atomics.py — OK
  • python3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_atomics.py — OK
  • python3 -I tests/test_ci_registration.py — 15/15 OK
  • python3 -I tests/test_text_write_line_endings.py — 3/3 OK
**@⁠IlyassRS3** on GitHub · 2026-11-24 21:38 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1306) `IlyassRS3:feat/x64-recomp-atomics` → `main` · +644 −5 · 2 files · 1 commits · open Item ID: `X64-RECOMP-ATOMICS` AI-assisted: yes ## Summary Implements `cmpxchg`, `xadd`, and ARMv8.0 LL/SC (`ldaxr*`/`stlxr*`/`clrex`) memory atomics in `tools/x64_recomp.py`, replacing the `UnhandledInsn("... needs atomics")` stub in `_emit_xchg`: 1. **Flag classification fixes**: - Registered `"cmpxchg": "NZCVPA"` and `"xadd": "NZCVPA"` in `FLAG_WRITERS`, and added `"cmpxchg"` to `SUB_KIND` (so `cmpxchg`'s `subs_r` borrow polarity matches `cmp` across `_arm_cond`, `_maybe_mkflags`, `_emit_adcsbb`, and `_emit_flagctl`). - Removed the bogus `ins.flags_r = "NZCVPA"` on `cmpxchg`/`xadd` in `_classify` (neither reads flags). 2. **ARMv8.0 LL/SC encodings in `Asm`**: - Added `ldaxr(rt, rn, size)` (`ldaxrb`/`ldaxrh`/`ldaxr w`/`ldaxr x`), `stlxr(rs, rt, rn, size)` (`stlxrb`/`stlxrh`/`stlxr w`/`stlxr x`, asserting `rs != rt and rs != rn` per ARMv8 `CONSTRAINED UNPREDICTABLE` rules), and `clrex()`. Verified bit-exact against `clang -arch arm64` / `otool`. 3. **Memory `xchg` (implicit `LOCK`)**: - Uses an ARM64 `ldaxr`/`stlxr` retry loop for naturally aligned memory accesses (with an unaligned `ldr`/`str` fallback for `size > 1` using `and_i` + `cbnz` so `NZCV` flags are never disturbed). - Computes `ADDR_T` and loads the source register before storing, keeping `xchg [rax], rax` safe. 4. **`cmpxchg` (`0F B0` / `0F B1`) across 8/16/32/64-bit (reg, hi8, mem, unlocked & `LOCK`)**: - Equal branch stores `src` into `dst` while leaving `rax` untouched (preserving upper 32/48/56 bits). - Mismatch branch stores `old_dst` into `al`/`ax`/`eax`/`rax` (zero-extending `rax` on 32-bit), emits `clrex` in `LOCK` LL/SC paths, and zero-extends 32-bit register `dst` per Intel SDM (`DEST := TEMP`). - Computes all 6 flags (`SF, ZF, CF, OF, PF, AF`) identically to `CMP ACC, old_dst` across all 4 widths, including `CTX_RES`/`CTX_A`/`CTX_B` for materialization. 5. **`xadd` (`0F C0` / `0F C1`) across 8/16/32/64-bit (reg, hi8, mem, unlocked & `LOCK`)**: - Stores `old_dst` to `src` before `old_dst + old_src` to `dst`, so `xadd reg, reg` yields `2 * reg`, and computes `ADDR_T` before updating `src` so `lock xadd [rax], rax` works cleanly. - Computes all 6 flags (`SF, ZF, CF, OF, PF, AF`) identically to `ADD old_dst, old_src` across all 4 widths. ## Merge compatibility - Leaves `PRIMARY[0x90..0x97]` and `decode_one` untouched (zero overlap with `#847`, `#1120`, `#1121`, `#1246`). - Verified **0 merge conflicts** in an 8-way merge with `#1174`, `#1202`, `#1205`, `#1211`, `#1213`, `#1279`, and `#1296`. ## Verification Tested on Apple M5 arm64 (macOS 27.2.1, Python 3.14; CPU-only, no game files): - `python3 -I tests/test_x64_recomp.py` — 32 tests (6 ok, 26 skipped without runner) - `python3 -I tests/test_x64_recomp_atomics.py -v` — 10 tests (6 static ok, 4 exec skipped without runner) - Mutation proof: reverting `tools/x64_recomp.py` to `upstream/main` causes **6 failures/errors across all 6 static tests** in `tests/test_x64_recomp_atomics.py`. - Native Apple M5 execution (scratch 8-way merge with `#1174`, `#1202`, `#1205`, `#1211`, `#1213`, `#1279`, `#1296` — 0 merge conflicts): - `tests/test_x64_recomp_atomics.py` — **10/10 OK** (all 4 execution suites + 6 static tests, 0 skips) - `tests/test_x64_recomp.py` — **46/46 OK** - `tests/test_x64_recomp_sse.py` — **16/16 OK** - `tests/test_x64_recomp_bitops.py` — **9/9 OK** - `tests/test_x64_recomp_rcl_rcr.py` — **5/5 OK** - `tests/test_x64_recomp_std_str.py` — **5/5 OK** - `tests/test_x64_recomp_muldiv.py` — **10/10 OK** - `tests/test_x64_recomp_shifts.py` — **9/9 OK** - Total: **110/110 native arm64 tests passing**. - `python3 -m compileall -q tools/x64_recomp.py tests/test_x64_recomp_atomics.py` — OK - `python3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_atomics.py` — OK - `python3 -I tests/test_ci_registration.py` — 15/15 OK - `python3 -I tests/test_text_write_line_endings.py` — 3/3 OK
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:46:45 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-24 21:38 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 21:38 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1306#pullrequestreview-6047832870) · **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 01:59 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 ef3493186 into the branch locally (924 commits behind, no conflicts). Head tested: c88ff1962.

Against a reference model the register forms of cmpxchg and xadd are right in all 1,344 checks across 8, 16, 32 and 64 bits. The new test file is 10/10 once three things outside these emitters are accounted for.

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

    • as shipped under QEMU: 10 run, 26 failures, 3 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: the 26 failures are gone, 5 timeouts remain;
    • 4 of the 5 are the unaligned memory cases (32b lock unaligned +1 eq/ne, xadd 32b lock unaligned +1, xchg 32-bit unaligned +1). The emitted fallback is fine; run_kernel on main runs with the MMU off, so any unaligned guest access takes an alignment fault and hangs. That is the harness bug #2143 fixes;
    • the fifth (xadd 64b) loads a 64-bit all-ones constant, which hangs in Asm.li on main (the elif not nzc: movn(rd, 0) branch that #1339, #1205 and #1329 add);
    • with #2143 merged in locally and that one li branch patched: 10/10, including every aligned and unaligned lock case through ldaxr/stlxr.
  • Beyond the file, generated cases under QEMU, each compared against a small Python model:

    • cmpxchg r, r (0f b0/0f b1), hit and miss: the full 64-bit accumulator and destination (untouched upper bits at 8/16, zero-extension at 32), plus CF, ZF, SF, OF as a compare of accumulator against destination;
    • xadd r, r (0f c0/0f c1): both full registers, plus CF, ZF, SF, OF of the add;
    • operands 0, 1, 7, the largest positive value, the most negative value, all-ones minus one, and all-ones at the narrow widths.
    tool checks wrong
    this branch 1,344 0

    There is no main column: main raises UnhandledInsn ("needs atomics") for both instructions.

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

Not tested or not judged: memory forms beyond the file's own cases, real contention on the ldaxr/stlxr retry loop (single core here), AF and PF, and on a 32-bit register cmpxchg miss whether the upper half of the destination is cleared (I only compared its low 32 bits). No game files used. AI-assisted.

**@⁠kevincommits** on GitHub · 2026-11-26 01:59 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1306#issuecomment-7180342497) 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 `ef3493186` into the branch locally (924 commits behind, no conflicts). Head tested: `c88ff1962`. **Against a reference model the register forms of `cmpxchg` and `xadd` are right in all 1,344 checks across 8, 16, 32 and 64 bits. The new test file is 10/10 once three things outside these emitters are accounted for.** - The test file, step by step (nothing changed on disk, all patched in memory or merged locally): - as shipped under QEMU: 10 run, 26 failures, 3 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`: the 26 failures are gone, 5 timeouts remain; - 4 of the 5 are the unaligned memory cases (`32b lock unaligned +1 eq`/`ne`, xadd `32b lock unaligned +1`, xchg `32-bit unaligned +1`). The emitted fallback is fine; `run_kernel` on `main` runs with the MMU off, so any unaligned guest access takes an alignment fault and hangs. That is the harness bug #2143 fixes; - the fifth (xadd `64b`) loads a 64-bit all-ones constant, which hangs in `Asm.li` on `main` (the `elif not nzc: movn(rd, 0)` branch that #1339, #1205 and #1329 add); - with #2143 merged in locally and that one `li` branch patched: **10/10**, including every aligned and unaligned `lock` case through `ldaxr`/`stlxr`. - Beyond the file, generated cases under QEMU, each compared against a small Python model: - `cmpxchg r, r` (`0f b0`/`0f b1`), hit and miss: the full 64-bit accumulator and destination (untouched upper bits at 8/16, zero-extension at 32), plus CF, ZF, SF, OF as a compare of accumulator against destination; - `xadd r, r` (`0f c0`/`0f c1`): both full registers, plus CF, ZF, SF, OF of the add; - operands 0, 1, 7, the largest positive value, the most negative value, all-ones minus one, and all-ones at the narrow widths. | tool | checks | wrong | |---|---|---| | this branch | 1,344 | 0 | There is no `main` column: `main` raises `UnhandledInsn` ("needs atomics") for both instructions. - **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. Not tested or not judged: memory forms beyond the file's own cases, real contention on the `ldaxr`/`stlxr` retry loop (single core here), AF and PF, and on a 32-bit register `cmpxchg` miss whether the upper half of the destination is cleared (I only compared its low 32 bits). 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/1306/head:github-bot/gh-1306'
git switch 'github-bot/gh-1306'

Merge

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