cpu: add AVX2 leaf AOT translator covering all 28 forms #323

Manually merged
github merged 5 commits from gh-pr/323/head into gh-pr/323/base 2026-11-26 21:42:54 +00:00
Owner

@⁠MarlomMedeiros on GitHub · 2026-11-21 22:07 UTC · original
cpu-avx2-registers → main · +2900 −1 · 9 files · 5 commits · merged 2026-11-22 07:56 UTC

Summary

Adds a standalone CPU AOT translator (tools/cpu_avx2_aot.py) that lifts bounded straight-line Windows x64 leaf functions covering all 28 of 28 forms in docs/coverage/x64_zen2.tsv group AVX2, with a source-built MASM fixture, three-way differential test suite (native MASM DLL / lifted C++ / independent Python ISA reference), PAGE_NOACCESS fault-suppression controls, and form coverage assertions.

Scope

  • 128-bit lane broadcast/permute: vbroadcasti128 (m128 source), vextracti128, vinserti128, vperm2i128
  • Element broadcasts (XMM and YMM): vpbroadcastb, vpbroadcastw, vpbroadcastd, vpbroadcastq
  • Cross-lane permutes: vpermd, vpermpd, vpermps, vpermq
  • Blend: vpblendd (XMM and YMM)
  • Variable shifts: vpsllvd, vpsllvq, vpsravd, vpsrlvd, vpsrlvq
  • Indexed gathers, all 16 VEX VSIB encodings: vgatherdps, vgatherdpd, vgatherqps, vgatherqpd, vpgatherdd, vpgatherdq, vpgatherqd, vpgatherqq across vm32x/vm32y/vm64x/vm64y
  • Masked moves, all 8 VEX encodings: vpmaskmovd, vpmaskmovq load and store forms
  • Transport/ABI glue: vmovdqu (128/256-bit flat load, store, register copy), vzeroupper, terminal ret

State model and memory semantics

  • ymm0..ymm5 modeled as a portable struct Vec256 { std::uint64_t q[4]; } value type rather than MSVC AVX2 intrinsics: generated C++ builds with default x64 flags (no /arch:AVX2) and avoids C++ UB on shift counts >= 32 / >= 64.
  • Gathers follow Intel SDM rules exactly: per-lane if-guarded loads (fault suppression), sign-extended 32-bit indices, SIB scales {1,2,4,8}, masked-off lanes preserve prior destination value, upper destination bits zeroed beyond the gathered element count, and the 256-bit mask register reads as zero afterwards.
  • vpmaskmov loads zero masked-off lanes (unlike gathers) and leave the mask register unchanged; stores write only MSB-set lanes.
  • Explicit rejections: overlapping gather dest/index/mask registers (including XMM/YMM aliases, #UD), illegal VSIB scales, non-argument/32-bit/rsp bases, RIP-relative or GPR-indexed operands, undefined vector register reads, memory operands on the 17 register-only forms, EVEX encodings, prefixes, nonvolatile xmm6..xmm15, branches and out-of-scope instructions.

Measured results

Tested at commit 0c4cc3e on Windows 11 (10.0.26300.0), AMD Ryzen 7 5800X3D, MSVC cl 19.52.37029 x64, ml64 14.52.37029.0, Python 3.14.10:

.\tools\dev.ps1
cmake --preset dev
cmake --build --preset dev
ctest --preset dev --no-tests=error -R '^cpu_aot_avx2_regs$' -V
  • cpu_aot_avx2_regs: passed. 1,120 differential cases (772 register-domain + 348 memory-domain across 68 exports, guarded source/output buffers) plus 34 VirtualAlloc/PAGE_NOACCESS guard-page fault-suppression cases matched native MASM, lifted C++ and independent Python reference bit-for-bit.
  • Negative controls: ALTER_IMM builds diverge on op_perm2i128_mix (14), op_permq (14), op_pblendd_ymm (16), op_gatherdps_ymm (12), op_pmaskmovd_load_ymm (10); output-mutation control caught; 59 unit rejection controls, 4 invalid-scale checks (0/3/5/16) and 9 PE-patched end-to-end rejections pass.
  • Full CPU suite: ctest --preset dev --no-tests=error -R '^cpu_' -E '_hardware$' -> 40/40 passed, 0 skipped.
  • tests/form_coverage_check.py passed: avx2_regs.asm accounts for all 28/28 AVX2 forms.
  • tests/docs_links_check.py and tests/bidi_controls_check.py passed.

Not covered

Memory operands on the 17 register-only AVX2 forms, control flow or calls mixed with AVX2 state, nonvolatile XMM6..XMM15 save/restore prologues, and title CPU code execution remain outside this change.

**@⁠MarlomMedeiros** on GitHub · 2026-11-21 22:07 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/323) `cpu-avx2-registers` → `main` · +2900 −1 · 9 files · 5 commits · merged 2026-11-22 07:56 UTC ## Summary Adds a standalone CPU AOT translator (`tools/cpu_avx2_aot.py`) that lifts bounded straight-line Windows x64 leaf functions covering **all 28 of 28 forms** in `docs/coverage/x64_zen2.tsv` group `AVX2`, with a source-built MASM fixture, three-way differential test suite (native MASM DLL / lifted C++ / independent Python ISA reference), `PAGE_NOACCESS` fault-suppression controls, and form coverage assertions. ### Scope - 128-bit lane broadcast/permute: `vbroadcasti128` (`m128` source), `vextracti128`, `vinserti128`, `vperm2i128` - Element broadcasts (XMM and YMM): `vpbroadcastb`, `vpbroadcastw`, `vpbroadcastd`, `vpbroadcastq` - Cross-lane permutes: `vpermd`, `vpermpd`, `vpermps`, `vpermq` - Blend: `vpblendd` (XMM and YMM) - Variable shifts: `vpsllvd`, `vpsllvq`, `vpsravd`, `vpsrlvd`, `vpsrlvq` - Indexed gathers, all 16 VEX VSIB encodings: `vgatherdps`, `vgatherdpd`, `vgatherqps`, `vgatherqpd`, `vpgatherdd`, `vpgatherdq`, `vpgatherqd`, `vpgatherqq` across `vm32x`/`vm32y`/`vm64x`/`vm64y` - Masked moves, all 8 VEX encodings: `vpmaskmovd`, `vpmaskmovq` load and store forms - Transport/ABI glue: `vmovdqu` (128/256-bit flat load, store, register copy), `vzeroupper`, terminal `ret` ### State model and memory semantics - `ymm0`..`ymm5` modeled as a portable `struct Vec256 { std::uint64_t q[4]; }` value type rather than MSVC AVX2 intrinsics: generated C++ builds with default x64 flags (no `/arch:AVX2`) and avoids C++ UB on shift counts `>= 32` / `>= 64`. - Gathers follow Intel SDM rules exactly: per-lane `if`-guarded loads (fault suppression), sign-extended 32-bit indices, SIB scales `{1,2,4,8}`, masked-off lanes preserve prior destination value, upper destination bits zeroed beyond the gathered element count, and the 256-bit mask register reads as zero afterwards. - `vpmaskmov` loads zero masked-off lanes (unlike gathers) and leave the mask register unchanged; stores write only MSB-set lanes. - Explicit rejections: overlapping gather dest/index/mask registers (including XMM/YMM aliases, `#UD`), illegal VSIB scales, non-argument/32-bit/`rsp` bases, RIP-relative or GPR-indexed operands, undefined vector register reads, memory operands on the 17 register-only forms, EVEX encodings, prefixes, nonvolatile `xmm6`..`xmm15`, branches and out-of-scope instructions. ### Measured results Tested at commit `0c4cc3e` on Windows 11 (`10.0.26300.0`), AMD Ryzen 7 5800X3D, MSVC `cl` 19.52.37029 x64, `ml64` 14.52.37029.0, Python 3.14.10: ```powershell .\tools\dev.ps1 cmake --preset dev cmake --build --preset dev ctest --preset dev --no-tests=error -R '^cpu_aot_avx2_regs$' -V ``` - `cpu_aot_avx2_regs`: **passed**. 1,120 differential cases (772 register-domain + 348 memory-domain across 68 exports, guarded source/output buffers) plus 34 `VirtualAlloc`/`PAGE_NOACCESS` guard-page fault-suppression cases matched native MASM, lifted C++ and independent Python reference bit-for-bit. - Negative controls: `ALTER_IMM` builds diverge on `op_perm2i128_mix` (14), `op_permq` (14), `op_pblendd_ymm` (16), `op_gatherdps_ymm` (12), `op_pmaskmovd_load_ymm` (10); output-mutation control caught; 59 unit rejection controls, 4 invalid-scale checks (`0`/`3`/`5`/`16`) and 9 PE-patched end-to-end rejections pass. - Full CPU suite: `ctest --preset dev --no-tests=error -R '^cpu_' -E '_hardware$'` -> **40/40 passed**, 0 skipped. - `tests/form_coverage_check.py` passed: `avx2_regs.asm` accounts for all 28/28 `AVX2` forms. - `tests/docs_links_check.py` and `tests/bidi_controls_check.py` passed. ### Not covered Memory operands on the 17 register-only AVX2 forms, control flow or calls mixed with AVX2 state, nonvolatile `XMM6`..`XMM15` save/restore prologues, and title CPU code execution remain outside this change.
Add tools/cpu_avx2_aot.py to lift bounded straight-line Windows x64 leaf functions covering all 18 register-domain AVX2 forms in docs/coverage/x64_zen2.tsv (vbroadcasti128, vextracti128, vinserti128, vperm2i128, vpbroadcastb/w/d/q, vpermd, vpermpd, vpermps, vpermq, vpblendd, vpsllvd/q, vpsravd, vpsrlvd/q) using a portable 256-bit Vec256 value model. Explicitly reject all 10 memory-domain AVX2 forms (vgather*, vpgather*, vpmaskmov*), memory operands on the 17 register-only AVX2 forms, EVEX encodings, nonvolatile vector registers and out-of-scope operations. Add tests/cpu/avx2_regs.asm, tests/cpu/avx2_regs.def, tests/cpu/avx2_regs_check.py, CMakeLists.txt, form coverage and docs/experiments.md registrations.
Extend tools/cpu_avx2_aot.py and tests/cpu/avx2_regs.asm to cover all 10 memory-domain AVX2 forms (vgatherdps/dpd/qps/qpd, vpgatherdd/dq/qd/qq across all 16 VEX VSIB encodings, and vpmaskmovd/q across all 8 VEX load/store encodings), bringing AVX2 group coverage in docs/coverage/x64_zen2.tsv to 28/28. Model exact Intel SDM lane/index/mask semantics including sign-extended 32/64-bit VSIB indices, fault-suppressing if-guarded loads/stores, upper-bit destination zeroing beyond gathered element count (bits 64..255 on vm64x vgatherqps/vpgatherqd), and post-gather mask register clearing. Reject overlapping dest/index/mask gather registers, illegal VSIB scales, unsupported addressing, undefined registers, and EVEX encodings. Add 25 MASM exports, differential and VirtualAlloc PAGE_NOACCESS guard-page checks, altered displacement controls, and updated docs/coverage assertions.
Exercise the previously dead store128 and copy128 codegen paths with op_movdqu_store128/op_movdqu_copy128 exports (the copy asserts upper-128 zeroing of the ymm destination), and add op_pgatherdd_ymm_rdx so VSIB base selection through rdx is covered alongside rcx. Differential harness now passes ctrl/base in swapped argument order for rdx-based gathers in both the main loop and the guard-page fault-suppression check.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 21:42:52 +00:00
github manually merged commit 4a5fb10413 into gh-pr/323/base 2026-11-26 21:42:54 +00:00
Author
Owner

@⁠Leyukaka on GitHub · 2026-11-21 22:35 UTC · original

A coverage note from reading the diff at 0c4cc3e, without building it.

op_movdqu_copy128 is the only XMM register-to-register copy in the fixture. It cannot tell a correct VEX.128 copy, which zeroes bits 128–255 of ymm1, from two wrong lowerings:

  • copying the full source YMM. The source xmm0 was just loaded by a VEX.128 vmovdqu, so its upper half is already zero.
  • merging only the low half. ymm1 has no prior write, and the generated Vec256 ymm1{} starts at zero.

Both would pass. Loading both registers first, with vmovdqu ymm0, YMMWORD PTR [rcx] and vmovdqu ymm1, YMMWORD PTR [rdx], then running vmovdqu xmm1, xmm0 and the existing ymm1 store would expose either one. The expected value, a[:16] followed by zeros, stays the same.

AI-assisted.

**@⁠Leyukaka** on GitHub · 2026-11-21 22:35 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/323#issuecomment-7070287649) A coverage note from reading the diff at `0c4cc3e`, without building it. `op_movdqu_copy128` is the only XMM register-to-register copy in the fixture. It cannot tell a correct VEX.128 copy, which zeroes bits 128–255 of `ymm1`, from two wrong lowerings: - **copying the full source YMM.** The source `xmm0` was just loaded by a VEX.128 `vmovdqu`, so its upper half is already zero. - **merging only the low half.** `ymm1` has no prior write, and the generated `Vec256 ymm1{}` starts at zero. Both would pass. Loading both registers first, with `vmovdqu ymm0, YMMWORD PTR [rcx]` and `vmovdqu ymm1, YMMWORD PTR [rdx]`, then running `vmovdqu xmm1, xmm0` and the existing `ymm1` store would expose either one. The expected value, `a[:16]` followed by zeros, stays the same. _AI-assisted._
Author
Owner

@⁠MarlomMedeiros on GitHub · 2026-11-21 22:37 UTC · original

Good catch — with both regs pre-zeroed, the three lowerings really do produce identical output. Fixed in 36cacde: ymm0 and ymm1 now load the full 256 bits before the xmm copy, so a full-ymm lowering leaks a[16:32] into the high half and a merge-only-lowering leaks b[16:32]. Expected output is unchanged, and the test still passes on the correct lowering.

**@⁠MarlomMedeiros** on GitHub · 2026-11-21 22:37 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/323#issuecomment-7070309038) Good catch — with both regs pre-zeroed, the three lowerings really do produce identical output. Fixed in 36cacde: ymm0 and ymm1 now load the full 256 bits before the xmm copy, so a full-ymm lowering leaks a[16:32] into the high half and a merge-only-lowering leaks b[16:32]. Expected output is unchanged, and the test still passes on the correct lowering.
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-22 03:08 UTC · original

For the AVX2 and AVX leaf fixtures, please focus the next update on #323, including the outstanding upper-lane control discussed here. Hold the #373 rebase until #323 lands so the CMake and coverage registrations can be reconciled against that base.

**@⁠R0Y00** on GitHub · 2026-11-22 03:08 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/323#issuecomment-7073347508) For the AVX2 and AVX leaf fixtures, please focus the next update on #323, including the outstanding upper-lane control discussed here. Hold the #373 rebase until #323 lands so the CMake and coverage registrations can be reconciled against that base.
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-21 22:07 UTC · original · reviewed

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-21 22:07 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/323#pullrequestreview-6008509885) · **reviewed** Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
This discussion has been locked. Commenting is limited to contributors.
No description provided.