cpu: lower XCHG in the shared lifter, atomically for memory #380
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/386"
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 R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-22T00:03:17Z.
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 #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).