x64_recomp: emit ARMv8.0 LL/SC atomics for LOCK ALU memory ops #1373
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1373"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ubiquitarianism/leonida. Originally posted by quietfold on 2026-11-24T22:48:36Z.
Item ID:
X64-RECOMP-LOCK-ALUSummary
tools/x64_recomp.pypreviously ignored theLOCKprefix 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/Xorcompile 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:Asm.ldaxr,Asm.stlxr): 8/16/32/64-bit acquire-load / release-store exclusives with register-constraint assertions (rs != rt and rs != rn)._emit_lock_alu):-1) and computes the effective address before entering the loop.lock adc/lock sbb, captures incoming x86CF(0or1, handlingADD_KIND,SUB_KIND,LOGIC_KIND, andctx.eflags) intoGTMPbefore the loop so store-exclusive retries always re-read the original incoming carry.LDAXR-> compute ->STLXR->CBNZretry loop using exclusively non-flag-setting instructions, with a non-flag-setting alignment check (and_i + cbnz) and plainldr/strfallback on unaligned 16/32/64-bit addresses so unalignedLOCKops never raiseSIGBUS.lock notpreserves all flags (NZCVandctx.eflags).lock inc/lock decwiden narrow 8/16-bit operands (1 << sh) for exactSF/ZF/OFwhile preserving both live ARMCand materializedctx.eflagsbit 0 (CF).lock adc/lock sbbhandle 8/16-bit flags via widenedADCS/SBCSwith low-bit fill whenCF == 1(using non-csetmarithmetic so it works standalone onmain).lock add/sub/and/or/xor/negemit exact widenedSF/ZF/CF/OF/PF/AFacross 8/16/32/64-bit.LDAXR/STLXRoverhead.Tests (
tests/test_x64_recomp_lock_alu.py)LDAXR/STLXRemission across all 4 widths (8/16/32/64-bit) and all 11LOCKALU ops (including negative immediatelock add qword [rdi], -1), plus confirming unlocked memory ALU ops do not emitLDAXR/STLXR.lock add/sub/and/or/xor/neg, negative sign-extended immediates, adjacent sentinel-byte isolation, and unaligned[rsp+1]fallback.lock inc/lock decCFpreservation (both same-blockSUB_KIND/clcand cross-blockctx.eflags) andlock notfull flag preservation.lock adc/lock sbbacross 8/16/32/64-bit (from bothSUB_KINDandstc) plus chained 128-bit atomiclock add+lock adc.Verification
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— OKpython3 -m compileall -q make_game.py tools tests— OKpython3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_lock_alu.py— OKpython3 -I tests/test_text_write_line_endings.py— 3/3 OKtools/x64_recomp.pytoupstream/mainfails the static test across all 15LOCKsubtests (15 failures).x64_recompPRs, 0 conflicts):tests/test_x64_recomp_lock_alu.py: 4/4 passed (0 skipped)test_x64_recomp_*.pysuites combined: 123/123 passed (0 skipped)AI-assisted: yes
Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-24T22:48:48Z.
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.
Imported from ubiquitarianism/leonida. Originally posted by kevincommits on 2026-11-26T02:33:48Z.
Independent run with QEMU. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew. I merged
mainat88b290fcbinto 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
lockmemory ops across 8, 16, 32 and 64 bits.maingets 90 of 1,480 wrong on the forms it can translate, all of theminc/dec.The test file, step by step (nothing changed on disk; patched in memory or merged locally):
xr.run_kernelwherexr.run_resultis needed);RUNNERswapped forxr.run_result: no failures, 4 errors left, all QEMU timeouts;mainproduce timeouts like these: unaligned guest accesses hang inrun_kernelbecause it runs with the MMU off (harness bug, #2143), and a 64-bit all-ones constant hangs inAsm.li(fixed in #1339, #1205, #1329). I applied both together and did not split the 4 between them;libranch 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], rwith carry-in fromstcandclc;lock inc/dec/neg/not [m]afterstcand afterclc;inc/dec/not;mainadc/sbbmainraisesUnhandledInsnfor 8/16-bitadc/sbb, so those are left out of its row. Its 90 mismatches are not about atomicity; they are the plaininc/decbugs that #1313 fixes, and they show with or without thelockprefix: 8- and 16-bitinc [m]leaves memory unchanged (28 checks), and CF is not kept acrossinc/decat any width (62 checks). Becauselockforms get their own emitter here, this branch is right for them even without #1313.Counts.
tests/test_x64_recomp.py44/44 andtests/test_recomp_census.py11/11, the same asmain. Full standalone suite: 514 run, 512 pass; the 2 that fail aremain'stest_mod_pack_output_aliasand the new file, for the reasons above.Overlap.
ldaxr/stlxrare 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.
Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T14:33:34Z.
Red/green: classified READY by dada-lotF (red on main, green on head merged with main); head
e8b1173e75unchanged since 2026-11-24, before that run.Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main
d48a957bbin GitHub Actions: no new failure.AI-assisted (Claude Code, Claude Opus 5.5)