x64_recomp: emit ARMv8.0 LL/SC atomics for LOCK ALU memory ops #1373

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

@⁠IlyassRS3 on GitHub · 2026-11-24 22:48 UTC · original
IlyassRS3:feat/x64-recomp-lock-alu → main · +547 −0 · 2 files · 1 commits · open

Item ID: X64-RECOMP-LOCK-ALU

Summary

tools/x64_recomp.py previously ignored the LOCK prefix on memory ALU instructions (lock inc, lock dec, lock add, lock sub, lock and, lock or, lock xor, lock neg, lock not, lock adc, lock sbb — what MSVC _InterlockedIncrement/Decrement/Add/And/Or/Xor compile to), emitting non-atomic load-modify-store sequences.

This PR adds atomic ARMv8.0 LL/SC lowering (_emit_lock_alu) for all 11 LOCK-valid memory ALU operations across 8/16/32/64-bit operands:

  • ARMv8.0 exclusives (Asm.ldaxr, Asm.stlxr): 8/16/32/64-bit acquire-load / release-store exclusives with register-constraint assertions (rs != rt and rs != rn).
  • Retry-safe LL/SC loop (_emit_lock_alu):
    • Loads source operand (properly sign-extending negative immediates including -1) and computes the effective address before entering the loop.
    • For lock adc/lock sbb, captures incoming x86 CF (0 or 1, handling ADD_KIND, SUB_KIND, LOGIC_KIND, and ctx.eflags) into GTMP before the loop so store-exclusive retries always re-read the original incoming carry.
    • Runs the LDAXR -> compute -> STLXR -> CBNZ retry loop using exclusively non-flag-setting instructions, with a non-flag-setting alignment check (and_i + cbnz) and plain ldr/str fallback on unaligned 16/32/64-bit addresses so unaligned LOCK ops never raise SIGBUS.
    • Computes flags once, after the atomic store commits:
      • lock not preserves all flags (NZCV and ctx.eflags).
      • lock inc/lock dec widen narrow 8/16-bit operands (1 << sh) for exact SF/ZF/OF while preserving both live ARM C and materialized ctx.eflags bit 0 (CF).
      • lock adc/lock sbb handle 8/16-bit flags via widened ADCS/SBCS with low-bit fill when CF == 1 (using non-csetm arithmetic so it works standalone on main).
      • lock add/sub/and/or/xor/neg emit exact widened SF/ZF/CF/OF/PF/AF across 8/16/32/64-bit.
  • Unlocked memory ALU instructions continue to use the non-atomic path without LDAXR/STLXR overhead.

Tests (tests/test_x64_recomp_lock_alu.py)

  • Static test verifying LDAXR/STLXR emission across all 4 widths (8/16/32/64-bit) and all 11 LOCK ALU ops (including negative immediate lock add qword [rdi], -1), plus confirming unlocked memory ALU ops do not emit LDAXR/STLXR.
  • 3 hardware execution suites:
    • 8/16/32/64-bit lock add/sub/and/or/xor/neg, negative sign-extended immediates, adjacent sentinel-byte isolation, and unaligned [rsp+1] fallback.
    • lock inc/lock dec CF preservation (both same-block SUB_KIND/clc and cross-block ctx.eflags) and lock not full flag preservation.
    • lock adc/lock sbb across 8/16/32/64-bit (from both SUB_KIND and stc) plus chained 128-bit atomic lock add + lock adc.

Verification

  • Host: macOS (Apple M5, arm64), Python 3.
  • Standalone checks (no game files):
    • python3 -I tests/test_x64_recomp_lock_alu.py — 4 tests (1 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_lock_alu.py — OK
    • python3 -I tests/test_text_write_line_endings.py — 3/3 OK
  • Mutation proof: reverting tools/x64_recomp.py to upstream/main fails the static test across all 15 LOCK subtests (15 failures).
  • Native Apple M5 hardware execution (17-way scratch merge with #1174 and all open x64_recomp PRs, 0 conflicts):
    • tests/test_x64_recomp_lock_alu.py: 4/4 passed (0 skipped)
    • All 16 test_x64_recomp_*.py suites combined: 123/123 passed (0 skipped)

AI-assisted: yes

**@⁠IlyassRS3** on GitHub · 2026-11-24 22:48 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1373) `IlyassRS3:feat/x64-recomp-lock-alu` → `main` · +547 −0 · 2 files · 1 commits · open Item ID: `X64-RECOMP-LOCK-ALU` ### Summary `tools/x64_recomp.py` previously ignored the `LOCK` prefix on memory ALU instructions (`lock inc`, `lock dec`, `lock add`, `lock sub`, `lock and`, `lock or`, `lock xor`, `lock neg`, `lock not`, `lock adc`, `lock sbb` — what MSVC `_InterlockedIncrement`/`Decrement`/`Add`/`And`/`Or`/`Xor` compile to), emitting non-atomic load-modify-store sequences. This PR adds atomic ARMv8.0 LL/SC lowering (`_emit_lock_alu`) for all 11 LOCK-valid memory ALU operations across 8/16/32/64-bit operands: - **ARMv8.0 exclusives (`Asm.ldaxr`, `Asm.stlxr`)**: 8/16/32/64-bit acquire-load / release-store exclusives with register-constraint assertions (`rs != rt and rs != rn`). - **Retry-safe LL/SC loop (`_emit_lock_alu`)**: - Loads source operand (properly sign-extending negative immediates including `-1`) and computes the effective address before entering the loop. - For `lock adc`/`lock sbb`, captures incoming x86 `CF` (`0` or `1`, handling `ADD_KIND`, `SUB_KIND`, `LOGIC_KIND`, and `ctx.eflags`) into `GTMP` before the loop so store-exclusive retries always re-read the original incoming carry. - Runs the `LDAXR` -> compute -> `STLXR` -> `CBNZ` retry loop using exclusively non-flag-setting instructions, with a non-flag-setting alignment check (`and_i + cbnz`) and plain `ldr`/`str` fallback on unaligned 16/32/64-bit addresses so unaligned `LOCK` ops never raise `SIGBUS`. - Computes flags **once**, after the atomic store commits: - `lock not` preserves all flags (`NZCV` and `ctx.eflags`). - `lock inc`/`lock dec` widen narrow 8/16-bit operands (`1 << sh`) for exact `SF`/`ZF`/`OF` while preserving both live ARM `C` and materialized `ctx.eflags` bit 0 (`CF`). - `lock adc`/`lock sbb` handle 8/16-bit flags via widened `ADCS`/`SBCS` with low-bit fill when `CF == 1` (using non-`csetm` arithmetic so it works standalone on `main`). - `lock add`/`sub`/`and`/`or`/`xor`/`neg` emit exact widened `SF`/`ZF`/`CF`/`OF`/`PF`/`AF` across 8/16/32/64-bit. - Unlocked memory ALU instructions continue to use the non-atomic path without `LDAXR`/`STLXR` overhead. ### Tests (`tests/test_x64_recomp_lock_alu.py`) - Static test verifying `LDAXR`/`STLXR` emission across all 4 widths (8/16/32/64-bit) and all 11 `LOCK` ALU ops (including negative immediate `lock add qword [rdi], -1`), plus confirming unlocked memory ALU ops do not emit `LDAXR`/`STLXR`. - 3 hardware execution suites: - 8/16/32/64-bit `lock add`/`sub`/`and`/`or`/`xor`/`neg`, negative sign-extended immediates, adjacent sentinel-byte isolation, and unaligned `[rsp+1]` fallback. - `lock inc`/`lock dec` `CF` preservation (both same-block `SUB_KIND`/`clc` and cross-block `ctx.eflags`) and `lock not` full flag preservation. - `lock adc`/`lock sbb` across 8/16/32/64-bit (from both `SUB_KIND` and `stc`) plus chained 128-bit atomic `lock add` + `lock adc`. ### Verification - **Host**: macOS (Apple M5, arm64), Python 3. - **Standalone checks (no game files)**: - `python3 -I tests/test_x64_recomp_lock_alu.py` — 4 tests (1 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_lock_alu.py` — OK - `python3 -I tests/test_text_write_line_endings.py` — 3/3 OK - **Mutation proof**: reverting `tools/x64_recomp.py` to `upstream/main` fails the static test across all 15 `LOCK` subtests (15 failures). - **Native Apple M5 hardware execution (17-way scratch merge with #1174 and all open `x64_recomp` PRs, 0 conflicts)**: - `tests/test_x64_recomp_lock_alu.py`: **4/4 passed** (0 skipped) - All 16 `test_x64_recomp_*.py` suites combined: **123/123 passed** (0 skipped) AI-assisted: yes
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:49:14 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-24 22:48 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:48 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1373#pullrequestreview-6048617423) · **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:33 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 88b290fcb into the branch locally (881 commits behind, no conflicts). Head tested: e8b1173e7.

Against a reference model this branch is right in all 1,672 checks of aligned lock memory ops across 8, 16, 32 and 64 bits. main gets 90 of 1,480 wrong on the forms it can translate, all of them inc/dec.

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

    • as shipped under QEMU: 4 run, 11 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: no failures, 4 errors left, all QEMU timeouts;
    • two causes on main produce timeouts like these: unaligned guest accesses hang in run_kernel because it runs with the MMU off (harness bug, #2143), and a 64-bit all-ones constant hangs in Asm.li (fixed in #1339, #1205, #1329). I applied both together and did not split the 4 between them;
    • with #2143 merged in locally and that one li branch patched in memory: 4/4.
  • Beyond the file, generated cases under QEMU against a small Python model, memory operand at an aligned [rsp]:

    • lock add/sub/and/or/xor [m], r; lock adc/sbb [m], r with carry-in from stc and clc; lock inc/dec/neg/not [m] after stc and after clc;
    • checked per case: the full 64-bit memory word (neighbouring bytes untouched at 8/16/32), and CF, ZF, SF, OF, including CF kept across inc/dec/not;
    • operands 0, 1, 7, the largest positive value, the most negative value, all-ones minus one, and all-ones at the narrow widths.
    tool forms checks wrong
    this branch all of the above 1,672 0
    main the same minus 8/16-bit adc/sbb 1,480 90

    main raises UnhandledInsn for 8/16-bit adc/sbb, so those are left out of its row. Its 90 mismatches are not about atomicity; they are the plain inc/dec bugs that #1313 fixes, and they show with or without the lock prefix: 8- and 16-bit inc [m] leaves memory unchanged (28 checks), and CF is not kept across inc/dec at any width (62 checks). Because lock forms get their own emitter here, this branch is right for them even without #1313.

  • 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. ldaxr/stlxr are also added by #1306 with identical encodings; whichever lands second will need a small rebase.

Not tested: unaligned operands beyond the file's own cases, immediate sources beyond the file's own cases, real contention on the retry loop (single core here), AF and PF. No game files used. AI-assisted.

**@⁠kevincommits** on GitHub · 2026-11-26 02:33 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1373#issuecomment-7180917607) 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 `88b290fcb` into the branch locally (881 commits behind, no conflicts). Head tested: `e8b1173e7`. **Against a reference model this branch is right in all 1,672 checks of aligned `lock` memory ops across 8, 16, 32 and 64 bits. `main` gets 90 of 1,480 wrong on the forms it can translate, all of them `inc`/`dec`.** - The test file, step by step (nothing changed on disk; patched in memory or merged locally): - as shipped under QEMU: 4 run, 11 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`: no failures, 4 errors left, all QEMU timeouts; - two causes on `main` produce timeouts like these: unaligned guest accesses hang in `run_kernel` because it runs with the MMU off (harness bug, #2143), and a 64-bit all-ones constant hangs in `Asm.li` (fixed in #1339, #1205, #1329). I applied both together and did not split the 4 between them; - with #2143 merged in locally and that one `li` branch patched in memory: **4/4**. - Beyond the file, generated cases under QEMU against a small Python model, memory operand at an aligned `[rsp]`: - `lock add/sub/and/or/xor [m], r`; `lock adc/sbb [m], r` with carry-in from `stc` and `clc`; `lock inc/dec/neg/not [m]` after `stc` and after `clc`; - checked per case: the full 64-bit memory word (neighbouring bytes untouched at 8/16/32), and CF, ZF, SF, OF, including CF kept across `inc`/`dec`/`not`; - operands 0, 1, 7, the largest positive value, the most negative value, all-ones minus one, and all-ones at the narrow widths. | tool | forms | checks | wrong | |---|---|---|---| | this branch | all of the above | 1,672 | 0 | | `main` | the same minus 8/16-bit `adc`/`sbb` | 1,480 | 90 | `main` raises `UnhandledInsn` for 8/16-bit `adc`/`sbb`, so those are left out of its row. Its 90 mismatches are not about atomicity; they are the plain `inc`/`dec` bugs that #1313 fixes, and they show with or without the `lock` prefix: 8- and 16-bit `inc [m]` leaves memory unchanged (28 checks), and CF is not kept across `inc`/`dec` at any width (62 checks). Because `lock` forms get their own emitter here, this branch is right for them even without #1313. - **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.** `ldaxr`/`stlxr` are also added by #1306 with identical encodings; whichever lands second will need a small rebase. Not tested: unaligned operands beyond the file's own cases, immediate sources beyond the file's own cases, real contention on the retry loop (single core here), AF and PF. 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/1373/head:github-bot/gh-1373'
git switch 'github-bot/gh-1373'

Merge

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