Repository navigation
cpu: lower CMPXCHG8B and CMPXCHG16B without LOCK - #628
Conversation
Each compares EDX:EAX (RDX:RAX) with its memory operand. When they are equal it sets ZF and stores ECX:EBX (RCX:RBX); otherwise it clears ZF and loads the operand into EDX:EAX (RDX:RAX), zero-extending the 32-bit halves of CMPXCHG8B. The other flags keep their values, and LOCK stays rejected by the prefix guard. A CMPXCHG16B operand that is not 16-byte aligned raises #GP(0), which Windows reports as an access violation reading address -1; the translation raises the same exception. cpu_aot_cmpxchg_wide compares memory, RAX, RDX, ZF and CF with the native DLL and a reference model over 120 cases, and checks that operands at offsets 4 and 8 raise the same exception in both DLLs. The fixture saves RBX in the home area with unwind data, so the fault restores it.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 79c93c8d71
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| "const uint64_t lo = load<uint64_t>(addr), hi = load<uint64_t>(addr + 8); " | ||
| "zero = lo == rax && hi == rdx; " | ||
| "if (zero) { store<uint64_t>(addr, rbx); store<uint64_t>(addr + 8, rcx); } " | ||
| "else { rax = lo; rdx = hi; } }") |
There was a problem hiding this comment.
Preserve writeback on a compare mismatch
When a CMPXCHG16B comparison fails, the instruction still writes the original operand back, but this arm only reloads RAX/RDX. On a read-only page, native code therefore raises an access violation while the lifted code returns normally; the CMPXCHG8B mismatch arm has the same gap. Write the original value back on mismatch so protected operands preserve this behavior.
Useful? React with 👍 / 👎.
|
Verification of I measured the open Codex finding (writeback on a compare mismatch) on hardware, and it reproduces. Setup:
The mismatch rows are a real native/translated divergence: the hardware performs the write cycle even when the comparison fails, and the translation does not. Writing the original operand back in the mismatch arm, as the review suggests, would close it. The same read-only-page setup would work as a regression case in AI-assisted: measured and summarized with Claude Code; outputs copied from the local runs. |
|
I pushed |
Change
The shared lowering in
tools/ffx_version_query.pynow acceptscmpxchg8bandcmpxchg16bwith a memory operand.LOCKstays rejected by the prefix guard, as forcmpxchg.cmpxchg8b. The other flags keep their values.cmpxchg16boperand that is not 16-byte aligned raises #GP(0), which Windows reports as an access violation reading address -1.INTEGER_HEADERgainsraise_cmpxchg16b_alignment, which raises the same exception with the same two parameters.They add 2 x64 forms to the coverage chart.
Verification
cpu_aot_cmpxchg_wide(tests/cpu/cmpxchg_wide.asm, 2 exports) loads RAX, RDX, RBX and RCX from a block, runs the instruction on an operand in the same block, and stores RAX, RDX, ZF and CF back. It compares the results with the native DLL and a reference model over 120 cases: equal operands, either half differing, register upper halves thatcmpxchg8bmust ignore on success and clear on failure, and both CF values. It also checks thatcmpxchg16boperands at offsets 4 and 8 raise the same exception in both DLLs. The fixture saves RBX in the home area with.saveregunwind data, so the faulting cases still restore the caller's RBX.lock cmpxchg8bandlock cmpxchg16bstay rejected.Windows 11 build 26300.9974, Ryzen 5 7600, MSVC 19.51.36256, Python 3.14.4, on
mainat 9b0df70:devandrelease(/O2)exception: access violation reading 0xFFFFFFFFFFFFFFFFin both DLLs; bothlockforms rejectedcpu_tests,devcpu_native_archive_readerandcpu_native_archive_blast, which need the Zig-builtarchive_reader.exe(ziglangis not installed on this machine). In one of three parallel (-j 8) runscpu_native_startupalso failed; it passed in the other two and in three runs on its own, and it does not use the shared loweringEvidence and follow-up
The upper-half behaviour of
cmpxchg8bfollows the SDM's 32-bit register writes. On success RAX and RDX keep bits 32 to 63; on failure both are zero-extended. The native runs confirm this. A non-lockedcmpxchg16bstill writes the operand back when the comparison fails, but that write cannot be observed in a single thread, so it is not modelled.enterandleavewould need a stack model that the shared lowering does not have (it has nopushorpop), so this PR leaves them out.Companion PRs, independent of this one: fences and cache hints (#626), and the x87 status-word forms (#627). x64 coverage goes from 551 (the published chart for 9b0df70) to 573 of 948 with all three.
AI-assisted.