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

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

📋 Pull Request Information

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

Base: main ← Head: feat/x64-recomp-lock-alu


📝 Commits (1)

  • e8b1173 x64_recomp: emit ARMv8.0 LL/SC atomics for LOCK ALU memory ops

📊 Changes

2 files changed (+547 additions, -0 deletions)

View changed files

➕ tests/test_x64_recomp_lock_alu.py (+340 -0)
📝 tools/x64_recomp.py (+207 -0)

📄 Description

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


🔄 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/1373 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/24/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `feat/x64-recomp-lock-alu` --- ### 📝 Commits (1) - [`e8b1173`](https://github.com/ubiquitarianism/leonida/commit/e8b1173e75dbe690f69cb942db11ddc69c57474e) x64_recomp: emit ARMv8.0 LL/SC atomics for LOCK ALU memory ops ### 📊 Changes **2 files changed** (+547 additions, -0 deletions) <details> <summary>View changed files</summary> ➕ `tests/test_x64_recomp_lock_alu.py` (+340 -0) 📝 `tools/x64_recomp.py` (+207 -0) </details> ### 📄 Description 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 --- <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#1403
No description provided.