x64_recomp: add cmpxchg, xadd, and atomic LL/SC memory xchg emitters (X64-RECOMP-ATOMICS) #1306
Closed
leonida/mandy
quietfold wants to merge 0 commits from
feat/x64-recomp-atomics into main
pull from: feat/x64-recomp-atomics
merge into: leonida:main
leonida:main
leonida:github-pr/3295
leonida:beta
leonida:docs/changelog-3483-3500
leonida:github-pr/3294
leonida:github-pr/2975
leonida:github-pr/2815
leonida:feat/gate-driver-start
leonida:feat/skip-pipelines-release
leonida:perf/lazy-compare
leonida:perf/mwaitx-wait
leonida:test/prio-ceiling-main
leonida:fix/ab-cpu-stdout
leonida:fix/gate-old-crashes
leonida:diag/ab-cpu
leonida:test/main-0312
leonida:github-pr/3291
leonida:diag/indirect-clamp-census
leonida:feat/barrier-enhanced
leonida:perf/texture-frame-ceiling
leonida:perf/dstorage-threads
leonida:fix/console-queue-dma-tables
leonida:fix/port-descriptors-record-parts
leonida:fix/frame-waits-fence
leonida:fix/console-sampler-words
leonida:fix/graphics-userdata-spill
leonida:fix/xg-reserved-layout-rotation
leonida:fix/barrier-console-flags
leonida:fix/switches-wait-test-child
leonida:test/list-value-waits
leonida:test/lot-gxdk-0412
leonida:fix/bvh-compacted-size
leonida:docs/i18n-quickstarts
leonida:feat/native-pipeline-slots
leonida:fix/clippy-leonida-gpu
leonida:fix/list-value-producers
leonida:fix/descriptor-page-straddle
leonida:fix/xmem-low-window
leonida:fix/ffx-jitter-never-zero
leonida:fix/run-stdout-closed
leonida:github-pr/2892
leonida:github-pr/2896
leonida:feat/warm-save-every
leonida:github-pr/2980
leonida:github-pr/2994
leonida:github-pr/3005
leonida:github-pr/3010
leonida:github-pr/3136
leonida:github-pr/3303
leonida:github-pr/3220
leonida:github-pr/3143
leonida:github-pr/3330
leonida:github-pr/3226
leonida:github-pr/3187
leonida:carry/3395
leonida:github-pr/2946
leonida:github-pr/2947
leonida:github-pr/2945
leonida:github-pr/2983
leonida:github-pr/3001
leonida:github-pr/3372
leonida:github-pr/3128
leonida:github-pr/3296
leonida:github-pr/2588
leonida:github-pr/3119
leonida:github-pr/3378
leonida:fix/crash-diagnostics
leonida:feat/dlss-mip-bias
leonida:fix/retired-out-of-order
leonida:fix/dxr-postbuild-barrier
leonida:fix/amd-relay-default
leonida:fix/removed-report-wait
leonida:feat/heap-twin
leonida:feat/vram-margin
leonida:github-pr/2472
leonida:github-pr/3123
leonida:github-pr/3088
leonida:assets/annonce-v1
leonida:github-pr/2794
leonida:github-pr/3300
leonida:github-pr/3304
leonida:github-pr/3308
leonida:github-pr/3367
leonida:github-pr/3369
leonida:fix/launcher-dlss-diagnostics
leonida:github-pr/3269
leonida:github-pr/2957
leonida:github-pr/3205
leonida:fix/crash-zero-guard
leonida:github-pr/3185
leonida:github-pr/3099
leonida:github-pr/3104
leonida:github-pr/3348
leonida:github-pr/3379
leonida:github-pr/3377
leonida:perf/darner-title-priority-ceiling
leonida:github-pr/3376
leonida:gh-readonly-queue/main/pr-3311-b5c45b663dbc5a9c3532bb3f307e5f81b32faefa
leonida:gh-readonly-queue/main/pr-3088-af22e7847ff5808f72d1a1881c24be2fafd20b54
leonida:gh-readonly-queue/main/pr-3123-7f3fa026a94c6646e89244f28e1ce22290361867
leonida:github-pr/3373
leonida:github-pr/3368
leonida:github-pr/3198
leonida:gh-readonly-queue/main/pr-3354-637baa0b77ba47222e7280b17c1a59c6a1781d57
leonida:gh-readonly-queue/main/pr-3365-d7a0e0196a43834813014b4bd3aa5502a8263b14
leonida:gh-readonly-queue/main/pr-3361-5dfe8bbd05ed12fb2f2e1718d9af77a7c5f46fd8
leonida:gh-readonly-queue/main/pr-3366-8da9148204e9d12c585efa9ac2b7cc1ab08c7682
leonida:github-pr/3346
leonida:github-pr/3375
leonida:github-pr/2958
leonida:github-pr/3151
leonida:github-pr/3307
leonida:github-pr/3278
leonida:github-pr/3374
leonida:fix/runtime-test-conservative-fixtures
leonida:github-pr/3366
leonida:ci/shard-semantic-linux
leonida:github-pr/3354
leonida:docs/fg-limit-fps-switch
leonida:github-pr/3365
leonida:fix/read-file-hard-link
leonida:github-pr/3364
leonida:fix/book-slot-heap-never-read
leonida:github-pr/3361
leonida:github-pr/3263
leonida:github-pr/3360
leonida:github-pr/3353
leonida:github-pr/3331
leonida:fix/alias-later-queued-submissions
leonida:github-pr/3352
leonida:github-pr/3228
leonida:github-pr/3349
leonida:claude/gallant-sagan-aamatr
leonida:github-pr/3200
leonida:github-pr/3132
leonida:github-pr/3323
leonida:github-pr/3332
leonida:github-pr/3340
leonida:github-pr/3311
leonida:github-pr/3334
leonida:github-pr/3297
leonida:github-pr/3256
leonida:github-pr/2567
leonida:github-pr/2851
leonida:d3d12/agility-sdk-621
leonida:github-pr/3164
leonida:github-pr/3114
leonida:github-pr/3050
leonida:github-pr/3113
leonida:github-pr/3326
leonida:github-pr/3144
leonida:github-pr/2642
leonida:github-pr/3072
leonida:github-pr/2789
leonida:github-pr/2643
leonida:github-pr/3009
leonida:github-pr/3035
leonida:github-pr/3031
leonida:github-pr/2686
leonida:github-pr/2834
leonida:github-pr/3321
leonida:github-pr/3325
leonida:github-pr/3290
leonida:github-pr/3324
leonida:github-pr/3130
leonida:claude/fervent-curie-m19lnr
leonida:github-pr/3309
leonida:github-pr/3305
leonida:github-pr/3306
leonida:github-pr/3302
leonida:github-pr/3301
leonida:github-pr/3345
leonida:github-pr/3211
leonida:github-pr/3027
leonida:github-pr/3286
leonida:github-pr/3235
leonida:github-pr/3265
leonida:github-pr/3275
leonida:codex/skip-no-hit-ray-passes
leonida:github-pr/2823
leonida:github-pr/2911
leonida:github-pr/3262
leonida:github-pr/3125
leonida:github-pr/3206
leonida:github-pr/3250
leonida:github-pr/3238
leonida:github-pr/3188
leonida:github-pr/2952
leonida:github-pr/3217
leonida:github-pr/3168
leonida:t3/preserve-fsr-sdk-restart-path
leonida:t3/propagate-host-failure-hresult
leonida:github-pr/3209
leonida:t3/launcher-custom-parameters
leonida:github-pr/3207
leonida:github-pr/3183
leonida:github-pr/3160
leonida:github-pr/3201
leonida:ci/less-to-run
leonida:github-pr/2930
leonida:github-pr/2797
leonida:github-pr/2999
leonida:perf/native-compute-shader-forms
leonida:github-pr/3191
leonida:github-pr/3011
leonida:github-pr/3019
leonida:github-pr/2996
leonida:gh-readonly-queue/main/pr-2789-45dc41f95c1cd0dcc29c2c50582e0e9af8a77259
leonida:claude/blissful-franklin-ukp7ag
leonida:github-pr/3141
leonida:github-pr/3156
leonida:github-pr/3150
leonida:github-pr/3153
leonida:github-pr/3163
leonida:github-pr/3142
leonida:github-pr/3139
leonida:github-pr/2841
leonida:github-pr/3069
leonida:github-pr/3098
leonida:github-pr/3107
leonida:github-pr/3071
leonida:github-pr/2774
leonida:github-pr/2889
leonida:github-pr/3075
leonida:github-pr/2661
leonida:github-pr/3064
leonida:github-pr/3054
leonida:github-pr/3049
leonida:github-pr/2855
leonida:github-pr/2943
leonida:github-pr/3016
leonida:github-pr/2887
leonida:github-pr/3006
leonida:github-pr/3003
leonida:github-pr/2993
leonida:notes/card-time-and-plant-flicker
leonida:github-pr/2989
leonida:feat/vulkan-rs
leonida:github-pr/2985
leonida:github-pr/2951
leonida:github-pr/2982
leonida:github-pr/752
leonida:github-pr/792
leonida:github-pr/2877
leonida:github-pr/2654
leonida:github-pr/2893
leonida:github-pr/2948
leonida:github-pr/2944
leonida:github-pr/2919
leonida:github-pr/2915
leonida:archive/ci/less-to-run/20261202T1121Z
leonida:github-pr/2800
leonida:github-pr/2822
leonida:github-pr/2833
leonida:github-pr/2840
leonida:github-pr/2831
leonida:github-pr/2832
leonida:github-pr/2801
leonida:github-pr/2890
leonida:github-pr/2891
leonida:github-pr/2664
leonida:github-pr/2807
leonida:rt-call-trace
leonida:github-pr/2755
leonida:perf/ahead-split
leonida:github-pr/2587
leonida:github-pr/2698
leonida:github-pr/2609
leonida:github-pr/3219
leonida:docs/readme-runtime-proposal
leonida:github-pr/2017
leonida:github-pr/1741
leonida:github-pr/1667
leonida:codex/coord-claim-check-20261124-7e48
No reviewers
Assignees
Clear assignees
No assignees
Labels
Clear labels
bug
Something isn't working
dependencies
Pull requests that update a dependency file
documentation
Improvements or additions to documentation
enhancement
New feature or request
missing claim
Needs a claim in #14
needs rebase
Conflicts with the base branch: rebase it on main
needs-triage
Needs maintainer evaluation before the next triage state
question
Further information is requested
ready-for-agent
Fully specified and ready for an AFK agent
submodules
Pull requests that update submodules code
No labels
bug
dependencies
documentation
enhancement
missing claim
needs rebase
needs-triage
question
ready-for-agent
submodules
Projects
Clear projects
No items
No project
Milestone
Clear milestone
No items
No milestone
Dependencies
No dependencies set
Notifications
1 participant
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".
No due date set.
Reference
leonida/mandy!1306
Loading…
Reference in a new issue
No description provided.
Delete branch "feat/x64-recomp-atomics"
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?
Pull request closed
This pull request cannot be reopened because the branch was deleted.
Item ID:
X64-RECOMP-ATOMICSAI-assisted: yes
Summary
Implements
cmpxchg,xadd, and ARMv8.0 LL/SC (ldaxr*/stlxr*/clrex) memory atomics intools/x64_recomp.py, replacing theUnhandledInsn("... needs atomics")stub in_emit_xchg:"cmpxchg": "NZCVPA"and"xadd": "NZCVPA"inFLAG_WRITERS, and added"cmpxchg"toSUB_KIND(socmpxchg'ssubs_rborrow polarity matchescmpacross_arm_cond,_maybe_mkflags,_emit_adcsbb, and_emit_flagctl).ins.flags_r = "NZCVPA"oncmpxchg/xaddin_classify(neither reads flags).Asm:ldaxr(rt, rn, size)(ldaxrb/ldaxrh/ldaxr w/ldaxr x),stlxr(rs, rt, rn, size)(stlxrb/stlxrh/stlxr w/stlxr x, assertingrs != rt and rs != rnper ARMv8CONSTRAINED UNPREDICTABLErules), andclrex(). Verified bit-exact againstclang -arch arm64/otool.xchg(implicitLOCK):ldaxr/stlxrretry loop for naturally aligned memory accesses (with an unalignedldr/strfallback forsize > 1usingand_i+cbnzsoNZCVflags are never disturbed).ADDR_Tand loads the source register before storing, keepingxchg [rax], raxsafe.cmpxchg(0F B0/0F B1) across 8/16/32/64-bit (reg, hi8, mem, unlocked &LOCK):srcintodstwhile leavingraxuntouched (preserving upper 32/48/56 bits).old_dstintoal/ax/eax/rax(zero-extendingraxon 32-bit), emitsclrexinLOCKLL/SC paths, and zero-extends 32-bit registerdstper Intel SDM (DEST := TEMP).SF, ZF, CF, OF, PF, AF) identically toCMP ACC, old_dstacross all 4 widths, includingCTX_RES/CTX_A/CTX_Bfor materialization.xadd(0F C0/0F C1) across 8/16/32/64-bit (reg, hi8, mem, unlocked &LOCK):old_dsttosrcbeforeold_dst + old_srctodst, soxadd reg, regyields2 * reg, and computesADDR_Tbefore updatingsrcsolock xadd [rax], raxworks cleanly.SF, ZF, CF, OF, PF, AF) identically toADD old_dst, old_srcacross all 4 widths.Merge compatibility
PRIMARY[0x90..0x97]anddecode_oneuntouched (zero overlap with#847,#1120,#1121,#1246).#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)tools/x64_recomp.pytoupstream/maincauses 6 failures/errors across all 6 static tests intests/test_x64_recomp_atomics.py.#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 OKtests/test_x64_recomp_sse.py— 16/16 OKtests/test_x64_recomp_bitops.py— 9/9 OKtests/test_x64_recomp_rcl_rcr.py— 5/5 OKtests/test_x64_recomp_std_str.py— 5/5 OKtests/test_x64_recomp_muldiv.py— 10/10 OKtests/test_x64_recomp_shifts.py— 9/9 OKpython3 -m compileall -q tools/x64_recomp.py tests/test_x64_recomp_atomics.py— OKpython3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_atomics.py— OKpython3 -I tests/test_ci_registration.py— 15/15 OKpython3 -I tests/test_text_write_line_endings.py— 3/3 OKIndependent run with QEMU. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew. I merged
mainatef3493186into the branch locally (924 commits behind, no conflicts). Head tested:c88ff1962.Against a reference model the register forms of
cmpxchgandxaddare 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):
xr.run_kernelwherexr.run_resultis needed);RUNNERswapped forxr.run_result: the 26 failures are gone, 5 timeouts remain;32b lock unaligned +1 eq/ne, xadd32b lock unaligned +1, xchg32-bit unaligned +1). The emitted fallback is fine;run_kernelonmainruns with the MMU off, so any unaligned guest access takes an alignment fault and hangs. That is the harness bug #2143 fixes;64b) loads a 64-bit all-ones constant, which hangs inAsm.lionmain(theelif not nzc: movn(rd, 0)branch that #1339, #1205 and #1329 add);libranch patched: 10/10, including every aligned and unalignedlockcase throughldaxr/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;There is no
maincolumn:mainraisesUnhandledInsn("needs atomics") for both instructions.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.Not tested or not judged: memory forms beyond the file's own cases, real contention on the
ldaxr/stlxrretry loop (single core here), AF and PF, and on a 32-bit registercmpxchgmiss whether the upper half of the destination is cleared (I only compared its low 32 bits). No game files used. AI-assisted.#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',UnhandledInsnoncmpxchg/xadd, and non-LL/SC memoryxchg) | green on PR+main:10 ran, OK (skipped=4)(test_x64_recomp_atomics.py— all 6 static ARMv8.0ldaxr/stlxr/clrexencoding, flag-classification, and translation checks pass; QEMU/Mach-O exec tests skipped on Windows x64 withoutqemu-system-aarch64),45 ran, OK (skipped=32)(test_x64_recomp.py), and11 ran, OK(test_recomp_census.py) | Windows 11 (10.0.26200.9974), Python 3.12.10 | AI-assisted: yesAudited
c88ff19626(tools/x64_recomp.pyandtests/test_x64_recomp_atomics.py) merged cleanly ontomain(adfb3821a2) on Windows 11:Asm.li(0, 0xFFFFFFFFFFFFFFFF)onPR + main(adfb3821a2) now emits[0x92800000](movn x0, #0) since the all-onesAsm.libranch is already onmain, resolving the earlierxadd64ball-ones constant hang without any patch to this PR.cmpxchgmismatch writes backdto zero-extendr32per Intel SDM Vol. 2A, locked memorycmpxchgmismatch executesclrexwithout writing memory, memoryxchgemitsldaxr/stlxrwithout touchingNZCV, and a combined 3-way merge of#1213+#1306+#1329ontomain(adfb3821a2) merges cleanly with zero conflicts and passes all fivex64_recomp/recomp_censustest suites (plusscripts/check_no_game_data.py).tests/test_x64_recomp_atomics.pyline 55):_runner()'s QEMU branch returnsxr.run_kernel(code, GUEST_VA)(raw(bytes, tr)) instead ofxr.run_result(code, GUEST_VA)(parsed(int, tr)).Tested
c88ff1962merged (no commit) withmain829530dbd: Python 3.12.4, Windows 11 10.0.26300, CPU only.check_no_game_data.pyis clean.tests/test_x64_recomp_atomics.py, 10 tests pass (4 QEMU tests skipped).tools/x64_recomp.pyback tomain, 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).
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):git merge-tree), but the PR base is ~4150 commits behind main.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_rnalready passes on main (main hasldaxr/stlxrfrom lock_alu).git merge --no-ff): the 6 static tests pass, the 4 exec tests fail, skipped 0. The exec tests only skip when neitherxr.CAN_RUN_MACHO(not on main) norqemu-system-aarch64is available; here qemu is on PATH, so they run (this likely explains the "4 skipped" seen elsewhere (ASSUMED)).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.RUNNER->run_result, PR unchanged):test_cmpxchg_all_widths_reg_and_hi8passes; remaining:test_cmpxchg_memory_unlocked_and_locked_aligned_and_unaligned: 8 failures, all on CF read throughjmp+pushf(expected 0x44, got 0x45). The same CF behaviour reproduces on main withcmp al,bl(equal) +jmp+pushf, so the expectation is right for x86 but depends on a fix not in main.tests/test_x64_recomp*.py): identical on main and on the merge, no regression.Diff read (+644/-5):
flags_rfor cmpxchg/xadd is correct; 32-bit register CMPXCHG zero-extends DEST on mismatch as in the SDM.Asm.ldaxrandAsm.stlxrare defined twice (main already has them; identical bodies, F811): needs a rebase that drops the duplicate.dmb ishis weaker than x86 LOCK: probably fine for the recompiler, but worth a comment.Verdict: FAILS (exec tests red on the merge under qemu). Suggested: use
run_resultin 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
Correction (my earlier verdict here was incomplete): not ready to merge.
Tested
c88ff1962merged (no commit) withmain4364a4fa6: Python 3.12, Windows 11 10.0.26300, CPU only.tests/test_x64_recomp_atomics.py(decode/emit); red onmainwithtools/x64_recomp.pyback: 3 failures, 2 errors.main+ this PR (see the macOS run linked above).ldaxrandstlxrare defined twice intools/x64_recomp.py.AI-assisted (Claude Code).
Correction: this GREEN covers only the decode and emit half. The 4 execution tests of
tests/test_x64_recomp_atomics.pywere skipped here (no arm64 runner: they need qemu-system-aarch64 or Apple Silicon), so the result above does not cover them.c88ff1962): https://github.com/ProjectLeonida/mandy/pull/1306#issuecomment-72496013684364a4fa6+ this PR,tools/x64_recomp.pydefinesldaxrandstlxrtwice (lines 1243/1273 and 1247/1277), so the later pair silently replaces the earlier one.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.
tests/test_x64_recomp_atomics.pyruns 10 tests: OK, 4 skipped (no arm64 runner here, so the execution cases skip; the decode and encoding cases run).tools/x64_recomp.pyback 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_llscandtest_ldaxr_stlxr_clrex_encodings.tests/test_x64_recomp*.pyfiles give the same results on main and on the merge. Their skips are the same on both (arm64 runner or qemu-system-aarch64 needed).AI-assisted (Claude Code).