Recompile x64 switch dispatch through jump tables #85
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/86"
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 Vonsek on 2026-11-21T15:41:17Z.
Change
Adds
tools/cpu_switch_aot.py, which recompiles Windows x64 leaf functions that dispatch through a jump table. It supports both layouts compilers emit: MSVC image-relative entries read through__ImageBase, and LLVM signed offsets from the table. The translator follows branches and table entries from the export, so it never decodes table bytes stored after the code. It proves the unsigned bound check covers the index register, and tracks a RIP-relative table base across paths, which covers a base hoisted out of a loop. The generated C++ is aswitchthat jumps to labels at the decoded targets. Backward branches are supported, so a switch inside a loop translates too. Leaf functions without.pdataare bounded by their executable section and may not enter another function's unwind interval.The new CTest
cpu_aot_jump_tablesbuildstests/cpu/switch.asmand a swapped-table variant, translates both, and runstests/cpu/switch_check.py.Verification
Windows:
ctest --preset dev --no-tests=error -R '^cpu_aot_jump_tables$' -Vata15097a(ona2a62ee) on Windows 11 build 26200.9974, VS Build Tools 2022 17.14 (MSVC 14.44.35207), Windows SDK 10.0.26100, Python 3.12, RTX 5080 (driver 32.0.16.1692): passed. "600 selector pairs per table layout and 138 programs match on return, guarded store, RBX and RSI; swapped tables changed {'select_rva': 60, 'select_rel': 60, 'run_ops': 99}" and "13 rejection controls and the leaf-without-unwind translation passed".On Linux, LLVM 18 assembled and linked an equivalent GNU-syntax copy of the fixture, because
llvm-mllacksIMAGEREL. The original machine code ran in-process through anms_abicall and matched the lifted C++ (clang++-Wall -Wextra -Werror) and the Python reference: 600 selector/operand pairs per layout and 138 byte programs, for both the original and swapped tables. The swapped tables changed 60, 60 and 99 results. Clang C switch code forx86_64-pc-windows-msvcat -O1 and -O2 (leaves without.pdata, hoisted table base) also matched natively. The generated sources compile under clang-cl/W4 /WX.switch_check.py rejectionspasses 13 patched-image controls and a positive check that a leaf stripped of.pdatatranslates identically.Evidence and follow-up
The fixture is original MASM written for this repository under the MIT license; it uses no game files. Assumptions: three integer words in RCX/RDX/R8 with the result in RAX, leaf functions only (no calls or stack operations), no 8/16-bit register writes, and flags used only by the immediately following conditional jump. ML64 rejects
IMAGERELof a code label inside a memory operand, so the table labels are declared withLABEL DWORD. Follow-ups: check the translator against MSVC-compiled C switches, and extend the profile to non-leaf frames so it can combine withcpu_call_aot. Contributor-facing changes: README "CPU translation" row anddocs/experiments.md#switch-dispatch-through-jump-tables.Narrow memory shifts: the lifter applies x86 count masking (five bits below 64-bit operands) and shifts in 64 bits.
shift_narrowchecks byte and wordSHL/SHR/SARof a memory destination on 4,608 inputs against the original and a reference.Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T15:46:43Z.
Thanks for the jump-table work. The Windows job stops before running the comparison: MSVC MASM reports A2102 at tests/cpu/switch.asm lines 14 and 118 for both fixture variants. Please adjust those expressions and confirm that cpu_aot_jump_tables runs for both tables on Windows. Build log.
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T16:23:05Z.
I see this is now marked ready. GitHub reports conflicts and no check results on the current head. Please sync with main and post the Windows cpu_aot_jump_tables result for both fixture variants so we can review the updated MASM build.
Imported from R0Y00/Xbox2Windows. Originally posted by Vonsek on 2026-11-21T16:50:09Z.
Rebased on main (
a2a62ee) and force-pushed as4d2835d. The only conflict was the README "CPU translation" row; I kept the current row and added the jump-table sentence.The A2102 errors came from
IMAGERELof a code label inside a memory operand. The two table labels are now declared withLABEL DWORD, and ML64 14.44.35230 assembles both fixture variants (cpu_switch_original.dlland theSWAP_TABLEbuildcpu_switch_swapped.dll).Windows 11 26200.9974, MSVC 14.44.35207, SDK 10.0.26100, at
4d2835d:The full
ctest --preset dev --no-tests=erroron the same build: 79/79 passed, withbvh_wave64_hardwareandbvh_wave64_warpskipped (lane range 32..32).Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T16:53:08Z.
Thanks for fixing ML64 and posting the Windows comparisons. One correctness issue remains in Lifter.statement: SHIFTS uses width * 8 - 1 as the count mask for every destination. Byte and word memory destinations are accepted by write(), but x86 masks their shift count to five bits. For example, a byte in memory containing 1 shifted left by 8 becomes 0 on x86; the current generated code shifts it by 0 and leaves 1. Please either reject narrow memory shifts in this profile or implement their semantics without host-language overflow. Please add a source-built comparison or an explicit rejection test for both 8- and 16-bit destinations.
Imported from R0Y00/Xbox2Windows. Originally posted by Vonsek on 2026-11-21T17:03:27Z.
Fixed in
a15097aby implementing the x86 semantics rather than rejecting narrow shifts:static_cast<std::uint64_t>for SHL/SHR, sign-extendedstd::int64_tfor SAR), so a count up to 31 cannot overflow the host type. The existing store truncates to the destination width.shift_narrowintests/cpu/switch.asm: a six-entry LLVM-layout table that selectsshl/shr/sarofBYTE PTR [r8]andWORD PTR [r8]byCL.switch_check.pycompares the original, both lifted builds and a Python reference on 4,608 inputs: every count from 0 to 31 plus count bytes 0x21, 0x3f, 0x80 and 0xff, on low byte/word patterns including sign-bit cases, with and without upper register halves.shift_narrow(0, 0xab080001)(byte 1 shifted left by 8) returned0xab080001instead of0xab080000.Windows 11 26200.9974, MSVC 14.44.35207 / ML64 14.44.35230, at
a15097a:All
cpu_aot_*tests and the full suite (79/79, two wave64 skips) pass. The jump-table section ofdocs/experiments.mddescribes the new case.