Repository navigation
cpu: lower XCHG in the shared lifter, atomically for memory - #386
Merged
Merged
Conversation
Lower register-register XCHG in all widths, reading both operands before either write. 32-bit forms zero-extend both registers, so 87 C0 clears the upper half of RAX while 90 and 66 90 stay NOPs. XCHG with a memory operand is atomic on x86 without a LOCK prefix, so it lowers to the host's _InterlockedExchange8/16//64 on the flat address instead of a load and a store. The redundant explicit LOCK XCHG stays rejected by the prefix check. cpu_aot_xchg compares 18 value exports with a reference and runs two token exports on eight threads, 100000 distinct tokens each: the values returned plus the final slot must be the tokens and the initial value, each once. A plain load/store control matches every value case but loses or duplicates tokens; a one-way register write control mismatches values. AI-assisted (Claude Code).
This was referenced Nov 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Change
Advances CPU translation coverage. XCHG is an independent problem:
lower()today accepts only the NOP spellings90and66 90, and rejects every real exchange. No open PR or issue claims it.There is no prerequisite PR. The shared interface is
lower()intools/ffx_version_query.py, itsINTEGER_HEADER, andCPU_FIXTURESintools/form_coverage.py. #287, #289, #291, #326, #328, #331, #335, #368 and #379 also extend these. The insertions sit at anchors those PRs do not use. A trial merge of #368 and #379 into this branch is clean, and the combined tree passes all 45cpu_tests.Lowering.
xchgin 8-, 16-, 32- and 64-bit widths, including the high-byte registers. Both operands are read before either is written.87 C0(xchg eax, eax) clears the upper half of RAX, while the one-byte90and66 90stay NOPs._InterlockedExchange8,_InterlockedExchange16,_InterlockedExchangeor_InterlockedExchange64on the flat address, instead of a flat load and store that would drop the atomicity.lock xchgstays rejected there.INTEGER_HEADERgains#include <intrin.h>.write()delegates register writes to a newwrite_register(), so both operands of a register swap can be written. Behaviour of existing callers is unchanged.Not modelled: the ordering of the flat loads and stores around the exchange. A lifted function still uses plain memory accesses for everything else.
Fixture
tests/cpu/xchg.asmbuilds 18 value exports and two token exports.Value exports. They cover
al/cl,ah/al,ah/ch,r8b/al,ax/cx,r9w/r8w,eax/ecx(one-byte form),ecx/r9d,87 C0,rax/rcxandr8/r9. The memory forms are byte (includingah), word, dword with either operand order, qword, and a misaligned qword.emitasserts that87 C0was assembled asXCHG_RM32_R32.Token exports (atomicity).
swap_tokens64andswap_tokens32loopxchgon one shared qword or dword.plaincontrol below shows that the check detects a non-atomic exchange.Negative controls. Two translations of the same image are mutated after lowering; each must mismatch.
plainonewayThe check also verifies rejections and NOPs on synthetic bytes:
f0 48 87 01(lock xchg) is rejected by the prefix check;90and66 90still lower as NOP;87 c0writes EAX.On
main,lower()rejects87 c0 c3withunsupported query operation.Verification
Tested at
85cd5e0on4f6c39d. Windows 11 build 26200, MSVC 19.50.35720, Windows SDK 10.0.26100.0, Python 3.13, AMD Ryzen 7 7700 (8 cores, 16 threads).ctest --preset dev --no-tests=error --output-junit <repo>/out/test-results.xmlgives 144 passed, 1 failed and 2 capability skips (bvh_wave64_warp,wave_shuffle_warp). The run includes the GPU tests. The failure isgpu_alias_lookup_writes_hardware, the RDNA 4 array-atomics probe tracked in gpu_alias_lookup_writes_hardware: array atomics probe reads 256 per buffer on RX 9070 XT #369, which fails the same way onmain. This change touches no GPU code.tests/form_coverage_check.pyandtests/docs_links_check.pypass.85cd5e0in the fork'sWindows checksrun 44964482313.plaincontrol vary from run to run: an earlier run gave 106564 and 79486. The check requires only that they are nonzero.Coverage.
python tools/form_coverage.py out/test-results.xmlgives x64 56 of 948 forms, 55 withoutcpu_aot_xchg. The added form isxchg.Measured, not established:
Evidence
The fixture is synthetic and original to this repository (MIT).
docs/experiments.mddocuments the lowering, the fixture and its controls.Checklist
AI-assisted (Claude Code).