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

Closed
quietfold wants to merge 1 commit from upstream-pr/1306 into main
quietfold commented 2026-11-24 21:38:37 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by quietfold on 2026-11-24T21:38:37Z.


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
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5480770063 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1306). Originally posted by [quietfold](https://github.com/quietfold) on 2026-11-24T21:38:37Z. --- 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
copilot-pull-request-reviewer[bot] commented 2026-11-24 21:38:47 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-24T21:38:47Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6047832870 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1306#pullrequestreview-6047832870). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-24T21:38:47Z. **Review:** COMMENTED --- 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.
kevincommits commented 2026-11-26 01:59:03 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by kevincommits on 2026-11-26T01:59:03Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7180342497 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1306#issuecomment-7180342497). Originally posted by [kevincommits](https://github.com/kevincommits) on 2026-11-26T01:59:03Z. --- 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.
zmasi commented 2026-11-28 23:19:15 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T23:19:15Z.


#1306 | head c88ff19626 (merged cleanly with main adfb3821a2) | python -I tests/test_x64_recomp_atomics.py -v + Asm.li(-1) & LL/SC bare-metal kernel probe + python -I tests/test_x64_recomp.py + python -I tests/test_recomp_census.py | red on main: FAILED (failures=3, errors=2, skipped=4) (AttributeError: 'Asm' object has no attribute 'clrex', KeyError: 'cmpxchg', UnhandledInsn on cmpxchg/xadd, and non-LL/SC memory xchg) | green on PR+main: 10 ran, OK (skipped=4) (test_x64_recomp_atomics.py — all 6 static ARMv8.0 ldaxr/stlxr/clrex encoding, flag-classification, and translation checks pass; QEMU/Mach-O exec tests skipped on Windows x64 without qemu-system-aarch64), 45 ran, OK (skipped=32) (test_x64_recomp.py), and 11 ran, OK (test_recomp_census.py) | Windows 11 (10.0.26200.9974), Python 3.12.10 | AI-assisted: yes

Audited c88ff19626 (tools/x64_recomp.py and tests/test_x64_recomp_atomics.py) merged cleanly onto main (adfb3821a2) on Windows 11:

  • Confirmed Asm.li(0, 0xFFFFFFFFFFFFFFFF) on PR + main (adfb3821a2) now emits [0x92800000] (movn x0, #0) since the all-ones Asm.li branch is already on main, resolving the earlier xadd 64b all-ones constant hang without any patch to this PR.
  • Confirmed 32-bit register cmpxchg mismatch writes back d to zero-extend r32 per Intel SDM Vol. 2A, locked memory cmpxchg mismatch executes clrex without writing memory, memory xchg emits ldaxr/stlxr without touching NZCV, and a combined 3-way merge of #1213 + #1306 + #1329 onto main (adfb3821a2) merges cleanly with zero conflicts and passes all five x64_recomp / recomp_census test suites (plus scripts/check_no_game_data.py).
  • Note in test harness (tests/test_x64_recomp_atomics.py line 55): _runner()'s QEMU branch returns xr.run_kernel(code, GUEST_VA) (raw (bytes, tr)) instead of xr.run_result(code, GUEST_VA) (parsed (int, tr)).
<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7247350911 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1306#issuecomment-7247350911). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-28T23:19:15Z. --- #1306 | head c88ff1962648665f890ce218a3bbae39cc8b7a34 (merged cleanly with main adfb3821a2) | `python -I tests/test_x64_recomp_atomics.py -v` + `Asm.li(-1)` & LL/SC bare-metal kernel probe + `python -I tests/test_x64_recomp.py` + `python -I tests/test_recomp_census.py` | red on main: `FAILED (failures=3, errors=2, skipped=4)` (`AttributeError: 'Asm' object has no attribute 'clrex'`, `KeyError: 'cmpxchg'`, `UnhandledInsn` on `cmpxchg`/`xadd`, and non-LL/SC memory `xchg`) | green on PR+main: `10 ran, OK (skipped=4)` (`test_x64_recomp_atomics.py` — all 6 static ARMv8.0 `ldaxr`/`stlxr`/`clrex` encoding, flag-classification, and translation checks pass; QEMU/Mach-O exec tests skipped on Windows x64 without `qemu-system-aarch64`), `45 ran, OK (skipped=32)` (`test_x64_recomp.py`), and `11 ran, OK` (`test_recomp_census.py`) | Windows 11 (`10.0.26200.9974`), Python 3.12.10 | AI-assisted: yes Audited `c88ff19626` (`tools/x64_recomp.py` and `tests/test_x64_recomp_atomics.py`) merged cleanly onto `main` (`adfb3821a2`) on Windows 11: - Confirmed `Asm.li(0, 0xFFFFFFFFFFFFFFFF)` on `PR + main` (`adfb3821a2`) now emits `[0x92800000]` (`movn x0, #0`) since the all-ones `Asm.li` branch is already on `main`, resolving the earlier `xadd` `64b` all-ones constant hang without any patch to this PR. - Confirmed 32-bit register `cmpxchg` mismatch writes back `d` to zero-extend `r32` per Intel SDM Vol. 2A, locked memory `cmpxchg` mismatch executes `clrex` without writing memory, memory `xchg` emits `ldaxr`/`stlxr` without touching `NZCV`, and a combined 3-way merge of `#1213` + `#1306` + `#1329` onto `main` (`adfb3821a2`) merges cleanly with zero conflicts and passes all five `x64_recomp` / `recomp_census` test suites (plus `scripts/check_no_game_data.py`). - Note in test harness (`tests/test_x64_recomp_atomics.py` line 55): `_runner()`'s QEMU branch returns `xr.run_kernel(code, GUEST_VA)` (raw `(bytes, tr)`) instead of `xr.run_result(code, GUEST_VA)` (parsed `(int, tr)`).
TheGeogeo commented 2026-11-29 00:17:40 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by TheGeogeo on 2026-11-29T00:17:40Z.


Tested c88ff1962 merged (no commit) with main 829530dbd: Python 3.12.4, Windows 11 10.0.26300, CPU only. check_no_game_data.py is clean.

  • Green: tests/test_x64_recomp_atomics.py, 10 tests pass (4 QEMU tests skipped).
  • Red: with tools/x64_recomp.py back to main, 3 failures and 2 errors (memory xchg must emit ldaxr). The QEMU run itself is not checked here.

No blocker from my side.

AI-assisted (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7248011798 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1306#issuecomment-7248011798). Originally posted by [TheGeogeo](https://github.com/TheGeogeo) on 2026-11-29T00:17:40Z. --- Tested `c88ff1962` merged (no commit) with `main` `829530dbd`: Python 3.12.4, Windows 11 10.0.26300, CPU only. `check_no_game_data.py` is clean. - Green: `tests/test_x64_recomp_atomics.py`, 10 tests pass (4 QEMU tests skipped). - Red: with `tools/x64_recomp.py` back to `main`, 3 failures and 2 errors (`memory xchg must emit ldaxr`). The QEMU run itself is not checked here. No blocker from my side. AI-assisted (Claude Code).
OreoFR commented 2026-11-29 02:42:55 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-29T02:42:55Z.


Verified by OreoFR on macOS 27.2.1 (26B131), Apple M5, APFS (case-insensitive), Python 3.12.16, qemu-system-aarch64 11.1.3 (Homebrew), main 02944d4ca, PR head c88ff1962 : red 4 failures + 30 errors / green 27 failures + 2 errors

python3.12 -I tests/test_x64_recomp_atomics.py (10 tests; the file does not exist on main):

  • Merge: clean (git merge-tree), but the PR base is ~4150 commits behind main.
  • Red (main + only this test file): 5 of 6 static tests fail (test_ldaxr_stlxr_clrex_encodings, test_all_atomic_forms_translate_cleanly, test_classify_flags_and_producer_tables, test_lock_vs_unlocked_cmpxchg_and_xadd_llsc, test_memory_xchg_uses_llsc_and_preserves_nzcv), and the 4 exec tests fail. test_stlxr_rejects_status_aliasing_rt_or_rn already passes on main (main has ldaxr/stlxr from lock_alu).
  • Green (main + git merge --no-ff): the 6 static tests pass, the 4 exec tests fail, skipped 0. The exec tests only skip when neither xr.CAN_RUN_MACHO (not on main) nor qemu-system-aarch64 is available; here qemu is on PATH, so they run (this likely explains the "4 skipped" seen elsewhere (ASSUMED)).
    • (READ IN CODE) In qemu mode the test calls xr.run_kernel, which returns raw bytes, so every assertion compares bytes to int, e.g. AssertionError: b'R=00000111\n' != 273. xr.run_result (parsed) looks like the intended call.
    • With a local harness tweak only (RUNNER -> run_result, PR unchanged): test_cmpxchg_all_widths_reg_and_hi8 passes; remaining:
      • test_cmpxchg_memory_unlocked_and_locked_aligned_and_unaligned: 8 failures, all on CF read through jmp + pushf (expected 0x44, got 0x45). The same CF behaviour reproduces on main with cmp al,bl (equal) + jmp + pushf, so the expectation is right for x86 but depends on a fix not in main.
      • 4 timeouts (15 s) on the "32b unaligned +1" sub-cases (cmpxchg x2, xadd, xchg): bare-metal qemu without MMU faults on unaligned ldr/str and hangs.
  • (MEASURED n=3) each failing exec test rerun alone 3 times: identical results.
  • Neighbour tests (the other 18 tests/test_x64_recomp*.py): identical on main and on the merge, no regression.

Diff read (+644/-5):

  • ldaxr/stlxr/clrex encodings are bit-exact (checked with llvm-mc); dropping flags_r for cmpxchg/xadd is correct; 32-bit register CMPXCHG zero-extends DEST on mismatch as in the SDM.
  • After merge, Asm.ldaxr and Asm.stlxr are defined twice (main already has them; identical bodies, F811): needs a rebase that drops the duplicate.
  • Unaligned paths (memory xchg, lock cmpxchg, lock xadd) use plain ldr/str (not atomic), and LDAXR/STLXR without dmb ish is weaker than x86 LOCK: probably fine for the recompiler, but worth a comment.
  • No game data in the diff (synthetic constants only).

Verdict: FAILS (exec tests red on the merge under qemu). Suggested: use run_result in the test, rebase and drop the duplicate ldaxr/stlxr, and either avoid the unaligned / inter-block pushf sub-cases under qemu or land the inter-block CF fix first.

AI-assisted: Claude Code

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7249601368 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1306#issuecomment-7249601368). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-29T02:42:55Z. --- Verified by OreoFR on macOS 27.2.1 (26B131), Apple M5, APFS (case-insensitive), Python 3.12.16, qemu-system-aarch64 11.1.3 (Homebrew), main 02944d4ca, PR head c88ff1962 : red 4 failures + 30 errors / green 27 failures + 2 errors `python3.12 -I tests/test_x64_recomp_atomics.py` (10 tests; the file does not exist on main): - **Merge**: clean (`git merge-tree`), but the PR base is ~4150 commits behind main. - **Red** (main + only this test file): 5 of 6 static tests fail (`test_ldaxr_stlxr_clrex_encodings`, `test_all_atomic_forms_translate_cleanly`, `test_classify_flags_and_producer_tables`, `test_lock_vs_unlocked_cmpxchg_and_xadd_llsc`, `test_memory_xchg_uses_llsc_and_preserves_nzcv`), and the 4 exec tests fail. `test_stlxr_rejects_status_aliasing_rt_or_rn` already passes on main (main has `ldaxr`/`stlxr` from lock_alu). - **Green** (main + `git merge --no-ff`): the 6 static tests pass, **the 4 exec tests fail**, skipped 0. The exec tests only skip when neither `xr.CAN_RUN_MACHO` (not on main) nor `qemu-system-aarch64` is available; here qemu is on PATH, so they run (this likely explains the "4 skipped" seen elsewhere (ASSUMED)). - (READ IN CODE) In qemu mode the test calls `xr.run_kernel`, which returns raw bytes, so every assertion compares bytes to int, e.g. `AssertionError: b'R=00000111\n' != 273`. `xr.run_result` (parsed) looks like the intended call. - With a local harness tweak only (`RUNNER` -> `run_result`, PR unchanged): `test_cmpxchg_all_widths_reg_and_hi8` passes; remaining: - `test_cmpxchg_memory_unlocked_and_locked_aligned_and_unaligned`: 8 failures, all on CF read through `jmp` + `pushf` (expected 0x44, got 0x45). The same CF behaviour reproduces on main with `cmp al,bl` (equal) + `jmp` + `pushf`, so the expectation is right for x86 but depends on a fix not in main. - 4 timeouts (15 s) on the "32b unaligned +1" sub-cases (cmpxchg x2, xadd, xchg): bare-metal qemu without MMU faults on unaligned ldr/str and hangs. - (MEASURED n=3) each failing exec test rerun alone 3 times: identical results. - Neighbour tests (the other 18 `tests/test_x64_recomp*.py`): identical on main and on the merge, no regression. Diff read (+644/-5): - ldaxr/stlxr/clrex encodings are bit-exact (checked with llvm-mc); dropping `flags_r` for cmpxchg/xadd is correct; 32-bit register CMPXCHG zero-extends DEST on mismatch as in the SDM. - After merge, `Asm.ldaxr` and `Asm.stlxr` are defined twice (main already has them; identical bodies, F811): needs a rebase that drops the duplicate. - Unaligned paths (memory xchg, lock cmpxchg, lock xadd) use plain ldr/str (not atomic), and LDAXR/STLXR without `dmb ish` is weaker than x86 LOCK: probably fine for the recompiler, but worth a comment. - No game data in the diff (synthetic constants only). **Verdict: FAILS** (exec tests red on the merge under qemu). Suggested: use `run_result` in the test, rebase and drop the duplicate ldaxr/stlxr, and either avoid the unaligned / inter-block pushf sub-cases under qemu or land the inter-block CF fix first. AI-assisted: Claude Code
MoonEyes commented 2026-11-29 17:08:51 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-29T17:08:51Z.


Correction (my earlier verdict here was incomplete): not ready to merge.

Tested c88ff1962 merged (no commit) with main 4364a4fa6: Python 3.12, Windows 11 10.0.26300, CPU only.

  • Green on 6 of 10 tests in tests/test_x64_recomp_atomics.py (decode/emit); red on main with tools/x64_recomp.py back: 3 failures, 2 errors.
  • The 4 skipped tests are the execution tests (they need qemu-system-aarch64 or Apple Silicon). They are not covered here, and they fail on Apple Silicon on main + this PR (see the macOS run linked above).
  • After the merge, ldaxr and stlxr are defined twice in tools/x64_recomp.py.

AI-assisted (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7259459648 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1306#issuecomment-7259459648). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-29T17:08:51Z. --- **Correction (my earlier verdict here was incomplete):** not ready to merge. Tested `c88ff1962` merged (no commit) with `main` `4364a4fa6`: Python 3.12, Windows 11 10.0.26300, CPU only. - Green on 6 of 10 tests in `tests/test_x64_recomp_atomics.py` (decode/emit); red on `main` with `tools/x64_recomp.py` back: 3 failures, 2 errors. - The 4 skipped tests are the execution tests (they need qemu-system-aarch64 or Apple Silicon). They are not covered here, and they fail on Apple Silicon on `main` + this PR (see the macOS run linked above). - After the merge, `ldaxr` and `stlxr` are defined twice in `tools/x64_recomp.py`. AI-assisted (Claude Code).
TheGeogeo commented 2026-11-29 17:11:39 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by TheGeogeo on 2026-11-29T17:11:39Z.


Correction: this GREEN covers only the decode and emit half. The 4 execution tests of tests/test_x64_recomp_atomics.py were skipped here (no arm64 runner: they need qemu-system-aarch64 or Apple Silicon), so the result above does not cover them.

  • On Apple Silicon they FAIL on main + this PR (measured n=3, same head c88ff1962): https://github.com/ProjectLeonida/mandy/pull/1306#issuecomment-7249601368
  • Also checked here: on main 4364a4fa6 + this PR, tools/x64_recomp.py defines ldaxr and stlxr twice (lines 1243/1273 and 1247/1277), so the later pair silently replaces the earlier one.
  • Not mergeable on this result.

AI-assisted (Claude Code).


Earlier comment, superseded by the correction above:

Tested c88ff1962 merged (no commit) with main 4364a4fa6: Python 3.12.4, Windows 11 10.0.26300, CPU only.

  • Green: tests/test_x64_recomp_atomics.py runs 10 tests: OK, 4 skipped (no arm64 runner here, so the execution cases skip; the decode and encoding cases run).
  • Red: with tools/x64_recomp.py back to main, it fails with 3 failures and 2 errors, starting with "memory xchg must emit ldaxr" (test_memory_xchg_uses_llsc_and_preserves_nzcv), test_lock_vs_unlocked_cmpxchg_and_xadd_llsc and test_ldaxr_stlxr_clrex_encodings.
  • No regression: the other 18 tests/test_x64_recomp*.py files give the same results on main and on the merge. Their skips are the same on both (arm64 runner or qemu-system-aarch64 needed).
  • No game data check passes.

AI-assisted (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7259497315 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1306#issuecomment-7259497315). Originally posted by [TheGeogeo](https://github.com/TheGeogeo) on 2026-11-29T17:11:39Z. --- **Correction: this GREEN covers only the decode and emit half.** The 4 execution tests of `tests/test_x64_recomp_atomics.py` were skipped here (no arm64 runner: they need qemu-system-aarch64 or Apple Silicon), so the result above does not cover them. - On Apple Silicon they FAIL on main + this PR (measured n=3, same head `c88ff1962`): https://github.com/ProjectLeonida/mandy/pull/1306#issuecomment-7249601368 - Also checked here: on main `4364a4fa6` + this PR, `tools/x64_recomp.py` defines `ldaxr` and `stlxr` twice (lines 1243/1273 and 1247/1277), so the later pair silently replaces the earlier one. - Not mergeable on this result. AI-assisted (Claude Code). --- *Earlier comment, superseded by the correction above:* Tested c88ff1962 merged (no commit) with main 4364a4fa6: Python 3.12.4, Windows 11 10.0.26300, CPU only. - Green: `tests/test_x64_recomp_atomics.py` runs 10 tests: OK, 4 skipped (no arm64 runner here, so the execution cases skip; the decode and encoding cases run). - Red: with `tools/x64_recomp.py` back to main, it fails with 3 failures and 2 errors, starting with "memory xchg must emit ldaxr" (`test_memory_xchg_uses_llsc_and_preserves_nzcv`), `test_lock_vs_unlocked_cmpxchg_and_xadd_llsc` and `test_ldaxr_stlxr_clrex_encodings`. - No regression: the other 18 `tests/test_x64_recomp*.py` files give the same results on main and on the merge. Their skips are the same on both (arm64 runner or qemu-system-aarch64 needed). - No game data check passes. AI-assisted (Claude Code).
aslan2142 commented 2026-12-01 11:45:46 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T11:45:46Z.

Review: CHANGES_REQUESTED


Blocking issues on head c88ff19 against current main:

  1. Duplicate method definitions after merge: tools/x64_recomp.py:1101 (def ldaxr) and tools/x64_recomp.py:1105 (def stlxr) duplicate Asm.ldaxr / Asm.stlxr already on main (tools/x64_recomp.py:1245-1252), silently shadowing main's definitions.
  2. Failing ARM64 execution tests: tests/test_x64_recomp_atomics.py:36 calls xr.run_kernel(code, GUEST_VA) instead of xr.run_result, and after fixing that wiring, the 4 unaligned LL/SC memory cases still fail/time out under qemu-system-aarch64 (independently reproduced on Apple Silicon).
  3. No CI run on current head.

Please rebase onto current main, reconcile ldaxr/stlxr into a single definition, fix the execution tests and unaligned LL/SC behavior, and get a green CI run.

(AI-assisted review.)

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6130585825 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1306#pullrequestreview-6130585825). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T11:45:46Z. **Review:** CHANGES_REQUESTED --- Blocking issues on head `c88ff19` against current `main`: 1. **Duplicate method definitions after merge**: `tools/x64_recomp.py:1101` (`def ldaxr`) and `tools/x64_recomp.py:1105` (`def stlxr`) duplicate `Asm.ldaxr` / `Asm.stlxr` already on `main` (`tools/x64_recomp.py:1245-1252`), silently shadowing `main`'s definitions. 2. **Failing ARM64 execution tests**: `tests/test_x64_recomp_atomics.py:36` calls `xr.run_kernel(code, GUEST_VA)` instead of `xr.run_result`, and after fixing that wiring, the 4 unaligned LL/SC memory cases still fail/time out under `qemu-system-aarch64` (independently reproduced on Apple Silicon). 3. **No CI run** on current head. Please rebase onto current `main`, reconcile `ldaxr`/`stlxr` into a single definition, fix the execution tests and unaligned LL/SC behavior, and get a green CI run. *(AI-assisted review.)*

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!1306
No description provided.