cpu: lift VEX.128 16-bit packed-integer leaf functions #320

Merged
Leyukaka merged 6 commits from upstream-pr/325 into main 2026-11-22 08:52:25 +00:00
Leyukaka commented 2026-11-21 22:15:19 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-21T22:15:19Z.


Change

Fixes #314 within the scope approved there. tools/cpu_vex_aot.py translates Windows x64 leaf functions that use VEX.128 16-bit packed-integer forms into per-lane C++:

  • vpaddw, vpsubw, vpmaxsw, vpminsw, vpmulhw and vpavgw;
  • vpsraw, vpsllw and vpsrlw with an immediate count;
  • byte and word unpacks, and vpackuswb;
  • vmovdqu/vmovdqa, vpxor/vpand/vpor and vzeroupper.

The ABI is specific to this profile: RCX and RDX point to 16-byte inputs, R8 points to a 16-byte output, and no return bits are defined. Memory operands must be exactly [RCX], [RDX] or [R8] with a 16-byte access, and stores go only to [R8]. Only VEX encodings and the volatile XMM0-XMM5 are accepted. Unmodelled forms, EVEX forms such as a masked vpaddw, legacy SSE encodings, XMM6-XMM15, VEX.256 operands and other operand shapes are rejected with their RVA. Decoding uses iced-x86, and function bounds come from cpu_leaf_aot.bounded_code.

The module docstring and the new docs/experiments.md section document alignment and aliases:

  • Alignment. vmovdqu takes any address. vmovdqa needs 16-byte alignment, a precondition the generated C++ does not check.
  • Aliases. The output may alias either input exactly, and the two inputs may be the same buffer. Reads and the store stay in program order, each as one 16-byte copy. Partial overlaps are not tested.

Registration points shared with #269, #323 (AVX2) and #286, #287, #289, #291 and #309: one CPU_FIXTURES entry in tools/form_coverage.py and one block in CMakeLists.txt next to cpu_heap_layout. I will rebase around whichever lands first.

Verification

On 2ff1c14 (branch on 6ee2406), Windows 11 build 26300.9974, MSVC 14.44.35207, Python 3.14:

ctest --preset dev --no-tests=error -R '^cpu_aot_vex_words$' -V
widen_mix: 4096 input pairs match bit for bit, guards intact
word_ops: 4096 input pairs match bit for bit, guards intact
shift_edges: 4096 input pairs match bit for bit, guards intact
store_reload: 4096 input pairs match bit for bit, guards intact
aligned_ops: 4096 input pairs match bit for bit, guards intact
aliased outputs match for all functions; store_reload reads its own store in 955 cases
RCX == RDX matches for all functions, with a separate output and with the output aliasing it
vmovdqu functions match on buffers 8 bytes off 16-byte alignment
vpsraw replaced by vpsrlw: 4044 of 4096 shift_edges outputs differ
rejected: a VEX.256 load, a VEX.256 store and vpaddd
rejected 8 excluded encodings, including an EVEX masked vpaddw

The full suite passes 145 of 145 tests.

Hand-encoded rejections. Eight bodies are each checked against their iced disassembly before translation, so a wrong encoding fails the test instead of passing it:

  • an EVEX vpaddw xmm0{k1};
  • a legacy paddw;
  • xmm6 as destination and as source;
  • [rcx+10h] and [rax];
  • a store to [rcx];
  • vpsraw with an XMM count.

Inputs. Each call runs in one 16-byte-aligned arena where the inputs and the output are separated by 0xcc guard zones. The whole arena is compared. The 4096 pairs are:

  • every pairing of eight edge words, including 0x7fff, 0x8000, 0xffff and 0x00ff, in all lanes;
  • the edge words rotated across lanes;
  • seeded random lanes.

Arithmetic shifts use counts 1, 15 and 16, left shifts 1, 2 and 16, and logical right shifts 0, 15 (in the control build) and 16.

Mutations. Sixteen translator mutations each fail the test:

  • vpavgw without rounding;
  • vpmulhw keeping bit 15;
  • vpsraw counts above 15 not clamped;
  • vpsllw and vpsrlw counts above 15 not cleared;
  • vpackuswb without saturation;
  • vpunpckhbw and vpunpckhwd reading the low half;
  • vpmaxsw comparing unsigned;
  • either YMM check removed;
  • the VEX-encoding check removed;
  • either nonvolatile-register check removed;
  • stores deferred to the end of the function;
  • input reads cached.

Only the alias checks catch the last two.

Evidence and follow-up

The fixture is source-built MASM. The fixture uses only XMM0-XMM5, so it never touches the nonvolatile XMM registers of the calling process. The shift-count form with an XMM count operand and the other AVX word forms, such as vpaddsw, vpmulhuw and vpcmpeqw, are rejected and left for follow-ups.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6535098360 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/325). Originally posted by [Leyukaka](https://github.com/Leyukaka) on 2026-11-21T22:15:19Z. --- ## Change Fixes #314 within the scope approved there. `tools/cpu_vex_aot.py` translates Windows x64 leaf functions that use VEX.128 16-bit packed-integer forms into per-lane C++: - `vpaddw`, `vpsubw`, `vpmaxsw`, `vpminsw`, `vpmulhw` and `vpavgw`; - `vpsraw`, `vpsllw` and `vpsrlw` with an immediate count; - byte and word unpacks, and `vpackuswb`; - `vmovdqu`/`vmovdqa`, `vpxor`/`vpand`/`vpor` and `vzeroupper`. The ABI is specific to this profile: RCX and RDX point to 16-byte inputs, R8 points to a 16-byte output, and no return bits are defined. Memory operands must be exactly `[RCX]`, `[RDX]` or `[R8]` with a 16-byte access, and stores go only to `[R8]`. Only VEX encodings and the volatile XMM0-XMM5 are accepted. Unmodelled forms, EVEX forms such as a masked `vpaddw`, legacy SSE encodings, XMM6-XMM15, VEX.256 operands and other operand shapes are rejected with their RVA. Decoding uses iced-x86, and function bounds come from `cpu_leaf_aot.bounded_code`. The module docstring and the new `docs/experiments.md` section document alignment and aliases: - **Alignment.** `vmovdqu` takes any address. `vmovdqa` needs 16-byte alignment, a precondition the generated C++ does not check. - **Aliases.** The output may alias either input exactly, and the two inputs may be the same buffer. Reads and the store stay in program order, each as one 16-byte copy. Partial overlaps are not tested. Registration points shared with #269, #323 (AVX2) and #286, #287, #289, #291 and #309: one `CPU_FIXTURES` entry in `tools/form_coverage.py` and one block in `CMakeLists.txt` next to `cpu_heap_layout`. I will rebase around whichever lands first. ## Verification On `2ff1c14` (branch on `6ee2406`), Windows 11 build 26300.9974, MSVC 14.44.35207, Python 3.14: ```text ctest --preset dev --no-tests=error -R '^cpu_aot_vex_words$' -V widen_mix: 4096 input pairs match bit for bit, guards intact word_ops: 4096 input pairs match bit for bit, guards intact shift_edges: 4096 input pairs match bit for bit, guards intact store_reload: 4096 input pairs match bit for bit, guards intact aligned_ops: 4096 input pairs match bit for bit, guards intact aliased outputs match for all functions; store_reload reads its own store in 955 cases RCX == RDX matches for all functions, with a separate output and with the output aliasing it vmovdqu functions match on buffers 8 bytes off 16-byte alignment vpsraw replaced by vpsrlw: 4044 of 4096 shift_edges outputs differ rejected: a VEX.256 load, a VEX.256 store and vpaddd rejected 8 excluded encodings, including an EVEX masked vpaddw ``` The full suite passes 145 of 145 tests. **Hand-encoded rejections.** Eight bodies are each checked against their iced disassembly before translation, so a wrong encoding fails the test instead of passing it: - an EVEX `vpaddw xmm0{k1}`; - a legacy `paddw`; - `xmm6` as destination and as source; - `[rcx+10h]` and `[rax]`; - a store to `[rcx]`; - `vpsraw` with an XMM count. **Inputs.** Each call runs in one 16-byte-aligned arena where the inputs and the output are separated by 0xcc guard zones. The whole arena is compared. The 4096 pairs are: - every pairing of eight edge words, including 0x7fff, 0x8000, 0xffff and 0x00ff, in all lanes; - the edge words rotated across lanes; - seeded random lanes. Arithmetic shifts use counts 1, 15 and 16, left shifts 1, 2 and 16, and logical right shifts 0, 15 (in the control build) and 16. **Mutations.** Sixteen translator mutations each fail the test: - `vpavgw` without rounding; - `vpmulhw` keeping bit 15; - `vpsraw` counts above 15 not clamped; - `vpsllw` and `vpsrlw` counts above 15 not cleared; - `vpackuswb` without saturation; - `vpunpckhbw` and `vpunpckhwd` reading the low half; - `vpmaxsw` comparing unsigned; - either YMM check removed; - the VEX-encoding check removed; - either nonvolatile-register check removed; - stores deferred to the end of the function; - input reads cached. Only the alias checks catch the last two. ## Evidence and follow-up The fixture is source-built MASM. The fixture uses only XMM0-XMM5, so it never touches the nonvolatile XMM registers of the calling process. The shift-count form with an XMM count operand and the other AVX word forms, such as `vpaddsw`, `vpmulhuw` and `vpcmpeqw`, are rejected and left for follow-ups. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted.
upstream-sync closed this pull request 2026-11-24 06:42:25 +00:00
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!320
No description provided.