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

Open
opened 2026-11-26 20:20:54 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1306
Author: @IlyassRS3
Created: 11/24/2026
Status: 🔄 Open

Base: main ← Head: feat/x64-recomp-atomics


📝 Commits (1)

  • c88ff19 x64_recomp: add cmpxchg, xadd, and atomic LL/SC memory xchg emitters

📊 Changes

2 files changed (+644 additions, -5 deletions)

View changed files

➕ tests/test_x64_recomp_atomics.py (+471 -0)
📝 tools/x64_recomp.py (+173 -5)

📄 Description

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

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1306 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/24/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `feat/x64-recomp-atomics` --- ### 📝 Commits (1) - [`c88ff19`](https://github.com/ubiquitarianism/leonida/commit/c88ff1962648665f890ce218a3bbae39cc8b7a34) x64_recomp: add cmpxchg, xadd, and atomic LL/SC memory xchg emitters ### 📊 Changes **2 files changed** (+644 additions, -5 deletions) <details> <summary>View changed files</summary> ➕ `tests/test_x64_recomp_atomics.py` (+471 -0) 📝 `tools/x64_recomp.py` (+173 -5) </details> ### 📄 Description 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 --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#1336
No description provided.