Recompile x64 switch dispatch through jump tables #86

Manually merged
github merged 4 commits from gh-pr/86/head into gh-pr/86/base 2026-11-26 21:26:15 +00:00
Owner

@⁠Vonsek on GitHub · 2026-11-21 15:41 UTC · original
Vonsek:cpu-aot-coverage → main · +1142 −1 · 7 files · 4 commits · merged 2026-11-21 17:13 UTC

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 a switch that jumps to labels at the decoded targets. Backward branches are supported, so a switch inside a loop translates too. Leaf functions without .pdata are bounded by their executable section and may not enter another function's unwind interval.

The new CTest cpu_aot_jump_tables builds tests/cpu/switch.asm and a swapped-table variant, translates both, and runs tests/cpu/switch_check.py.

Verification

Windows: ctest --preset dev --no-tests=error -R '^cpu_aot_jump_tables$' -V at a15097a (on a2a62ee) 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-ml lacks IMAGEREL. The original machine code ran in-process through an ms_abi call 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 for x86_64-pc-windows-msvc at -O1 and -O2 (leaves without .pdata, hoisted table base) also matched natively. The generated sources compile under clang-cl /W4 /WX. switch_check.py rejections passes 13 patched-image controls and a positive check that a leaf stripped of .pdata translates 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 IMAGEREL of a code label inside a memory operand, so the table labels are declared with LABEL DWORD. Follow-ups: check the translator against MSVC-compiled C switches, and extend the profile to non-leaf frames so it can combine with cpu_call_aot. Contributor-facing changes: README "CPU translation" row and docs/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_narrow checks byte and word SHL/SHR/SAR of a memory destination on 4,608 inputs against the original and a reference.

**@⁠Vonsek** on GitHub · 2026-11-21 15:41 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/86) `Vonsek:cpu-aot-coverage` → `main` · +1142 −1 · 7 files · 4 commits · merged 2026-11-21 17:13 UTC ## 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 a `switch` that jumps to labels at the decoded targets. Backward branches are supported, so a switch inside a loop translates too. Leaf functions without `.pdata` are bounded by their executable section and may not enter another function's unwind interval. The new CTest `cpu_aot_jump_tables` builds `tests/cpu/switch.asm` and a swapped-table variant, translates both, and runs `tests/cpu/switch_check.py`. ## Verification Windows: `ctest --preset dev --no-tests=error -R '^cpu_aot_jump_tables$' -V` at a15097a (on a2a62ee) 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-ml` lacks `IMAGEREL`. The original machine code ran in-process through an `ms_abi` call 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 for `x86_64-pc-windows-msvc` at -O1 and -O2 (leaves without `.pdata`, hoisted table base) also matched natively. The generated sources compile under clang-cl `/W4 /WX`. `switch_check.py rejections` passes 13 patched-image controls and a positive check that a leaf stripped of `.pdata` translates 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 `IMAGEREL` of a code label inside a memory operand, so the table labels are declared with `LABEL DWORD`. Follow-ups: check the translator against MSVC-compiled C switches, and extend the profile to non-leaf frames so it can combine with `cpu_call_aot`. Contributor-facing changes: README "CPU translation" row and `docs/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_narrow` checks byte and word `SHL`/`SHR`/`SAR` of a memory destination on 4,608 inputs against the original and a reference.
Add a translator for leaf functions that dispatch through MSVC image-relative and LLVM table-relative jump tables. It proves the table bound against the index register, tracks a RIP-relative base across paths so a base hoisted out of a loop is accepted, and emits a C++ switch over the decoded targets. Leaf functions without .pdata are bounded by their section and may not enter another function's unwind interval.

Add a MASM fixture with both layouts and a bytecode loop, a swapped-table control, and thirteen patched-image rejection controls registered as cpu_aot_jump_tables.
The jump-table lifter masked shift counts with width * 8 - 1, so a byte
or word memory destination used a three- or four-bit count. x86 masks
the count to five bits for 8-, 16- and 32-bit operands and to six bits
for 64-bit ones: a byte holding 1 shifted left by 8 becomes 0, but the
generated code shifted it by 0 and left 1.

Shifts now use the x86 mask and are computed in 64 bits, so a count up
to 31 cannot overflow the host type; the store truncates the result.

shift_narrow adds byte and word SHL, SHR and SAR of a memory
destination by CL behind a six-entry table. switch_check compares the
original, both lifted builds and a Python reference on 4,608 inputs,
covering counts 0 to 31 and count bytes above 31. With the previous
mask, the translation fails at shift_narrow(0, 0xab080001).
# Conflicts:
#	README.md
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 21:26:14 +00:00
github manually merged commit 4aa2de8093 into gh-pr/86/base 2026-11-26 21:26:15 +00:00
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-21 15:46 UTC · original

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.

**@⁠R0Y00** on GitHub · 2026-11-21 15:46 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/86#issuecomment-7065135610) 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](https://github.com/R0Y00/Xbox2Windows/actions/runs/44920506711/job/136895284518).
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-21 16:23 UTC · original

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.

**@⁠R0Y00** on GitHub · 2026-11-21 16:23 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/86#issuecomment-7065606318) 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.
Author
Owner

@⁠Vonsek on GitHub · 2026-11-21 16:50 UTC · original

Rebased on main (a2a62ee) and force-pushed as 4d2835d. 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 IMAGEREL of a code label inside a memory operand. The two table labels are now declared with LABEL DWORD, and ML64 14.44.35230 assembles both fixture variants (cpu_switch_original.dll and the SWAP_TABLE build cpu_switch_swapped.dll).

Windows 11 26200.9974, MSVC 14.44.35207, SDK 10.0.26100, at 4d2835d:

ctest --preset dev --no-tests=error -R '^cpu_aot_jump_tables$' -V
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}
13 rejection controls and the leaf-without-unwind translation passed
1/1 Test #49: cpu_aot_jump_tables ..............   Passed    0.38 sec

The full ctest --preset dev --no-tests=error on the same build: 79/79 passed, with bvh_wave64_hardware and bvh_wave64_warp skipped (lane range 32..32).

**@⁠Vonsek** on GitHub · 2026-11-21 16:50 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/86#issuecomment-7065965764) Rebased on main (a2a62ee) and force-pushed as 4d2835d. 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 `IMAGEREL` of a code label inside a memory operand. The two table labels are now declared with `LABEL DWORD`, and ML64 14.44.35230 assembles both fixture variants (`cpu_switch_original.dll` and the `SWAP_TABLE` build `cpu_switch_swapped.dll`). Windows 11 26200.9974, MSVC 14.44.35207, SDK 10.0.26100, at 4d2835d: ``` ctest --preset dev --no-tests=error -R '^cpu_aot_jump_tables$' -V 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} 13 rejection controls and the leaf-without-unwind translation passed 1/1 Test #49: cpu_aot_jump_tables .............. Passed 0.38 sec ``` The full `ctest --preset dev --no-tests=error` on the same build: 79/79 passed, with `bvh_wave64_hardware` and `bvh_wave64_warp` skipped (lane range 32..32).
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-21 16:53 UTC · original

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.

**@⁠R0Y00** on GitHub · 2026-11-21 16:53 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/86#issuecomment-7066013794) 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.
Author
Owner

@⁠Vonsek on GitHub · 2026-11-21 17:03 UTC · original

Fixed in a15097a by implementing the x86 semantics rather than rejecting narrow shifts:

  • The lifter now masks the count to five bits for 8-, 16- and 32-bit destinations and to six bits for 64-bit ones, and performs the shift in 64 bits (static_cast<std::uint64_t> for SHL/SHR, sign-extended std::int64_t for SAR), so a count up to 31 cannot overflow the host type. The existing store truncates to the destination width.
  • New source-built export shift_narrow in tests/cpu/switch.asm: a six-entry LLVM-layout table that selects shl/shr/sar of BYTE PTR [r8] and WORD PTR [r8] by CL. switch_check.py compares 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.
  • With the previous mask the translation fails at your example: shift_narrow(0, 0xab080001) (byte 1 shifted left by 8) returned 0xab080001 instead of 0xab080000.

Windows 11 26200.9974, MSVC 14.44.35207 / ML64 14.44.35230, at a15097a:

ctest --preset dev --no-tests=error -R '^cpu_aot_jump_tables$' -V
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}; 4608 byte and word memory shifts match x86 count masking
13 rejection controls and the leaf-without-unwind translation passed

All cpu_aot_* tests and the full suite (79/79, two wave64 skips) pass. The jump-table section of docs/experiments.md describes the new case.

**@⁠Vonsek** on GitHub · 2026-11-21 17:03 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/86#issuecomment-7066175891) Fixed in a15097a by implementing the x86 semantics rather than rejecting narrow shifts: - The lifter now masks the count to five bits for 8-, 16- and 32-bit destinations and to six bits for 64-bit ones, and performs the shift in 64 bits (`static_cast<std::uint64_t>` for SHL/SHR, sign-extended `std::int64_t` for SAR), so a count up to 31 cannot overflow the host type. The existing store truncates to the destination width. - New source-built export `shift_narrow` in `tests/cpu/switch.asm`: a six-entry LLVM-layout table that selects `shl`/`shr`/`sar` of `BYTE PTR [r8]` and `WORD PTR [r8]` by `CL`. `switch_check.py` compares 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. - With the previous mask the translation fails at your example: `shift_narrow(0, 0xab080001)` (byte 1 shifted left by 8) returned `0xab080001` instead of `0xab080000`. Windows 11 26200.9974, MSVC 14.44.35207 / ML64 14.44.35230, at a15097a: ``` ctest --preset dev --no-tests=error -R '^cpu_aot_jump_tables$' -V 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}; 4608 byte and word memory shifts match x86 count masking 13 rejection controls and the leaf-without-unwind translation passed ``` All `cpu_aot_*` tests and the full suite (79/79, two wave64 skips) pass. The jump-table section of `docs/experiments.md` describes the new case.
This discussion has been locked. Commenting is limited to contributors.
No description provided.