Recompile x64 switch dispatch through jump tables #85

Merged
Vonsek merged 4 commits from upstream-pr/86 into main 2026-11-21 17:13:52 +00:00
Vonsek commented 2026-11-21 15:41:17 +00:00 (Migrated from github.com)

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 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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6530894719 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/86). Originally posted by [Vonsek](https://github.com/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 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.
upstream-sync closed this pull request 2026-11-24 06:41:15 +00:00
R0Y00 commented 2026-11-24 06:47:35 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7065135610 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/86#issuecomment-7065135610). Originally posted by [R0Y00](https://github.com/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](https://github.com/R0Y00/Xbox2Windows/actions/runs/44920506711/job/136895284518).
R0Y00 commented 2026-11-24 06:47:37 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7065606318 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/86#issuecomment-7065606318). Originally posted by [R0Y00](https://github.com/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.
Vonsek commented 2026-11-24 06:47:38 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Vonsek on 2026-11-21T16:50:09Z.


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).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7065965764 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/86#issuecomment-7065965764). Originally posted by [Vonsek](https://github.com/Vonsek) on 2026-11-21T16:50:09Z. --- 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).
R0Y00 commented 2026-11-24 06:47:38 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7066013794 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/86#issuecomment-7066013794). Originally posted by [R0Y00](https://github.com/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.
Vonsek commented 2026-11-24 06:47:39 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Vonsek on 2026-11-21T17:03:27Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7066175891 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/86#issuecomment-7066175891). Originally posted by [Vonsek](https://github.com/Vonsek) on 2026-11-21T17:03:27Z. --- 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.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!85
No description provided.