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

Merged
MarlomMedeiros merged 5 commits from upstream-pr/323 into main 2026-11-22 07:56:28 +00:00
MarlomMedeiros commented 2026-11-21 22:07:02 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by MarlomMedeiros on 2026-11-21T22:07:02Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6535015001 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/323). Originally posted by [MarlomMedeiros](https://github.com/MarlomMedeiros) on 2026-11-21T22:07:02Z. --- ## 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.
upstream-sync closed this pull request 2026-11-24 06:42:24 +00:00
copilot-pull-request-reviewer[bot commented 2026-11-24 06:42:25 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-21T22:07:16Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6008509885 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/323#pullrequestreview-6008509885). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/copilot-pull-request-reviewer%5Bbot%5D) on 2026-11-21T22:07:16Z. **Review:** COMMENTED --- 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.
Leyukaka commented 2026-11-24 06:48:09 +00:00 (Migrated from github.com)

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


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7070287649 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/323#issuecomment-7070287649). Originally posted by [Leyukaka](https://github.com/Leyukaka) on 2026-11-21T22:35:27Z. --- 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._
MarlomMedeiros commented 2026-11-24 06:48:09 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by MarlomMedeiros on 2026-11-21T22:37:16Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7070309038 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/323#issuecomment-7070309038). Originally posted by [MarlomMedeiros](https://github.com/MarlomMedeiros) on 2026-11-21T22:37:16Z. --- 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.
R0Y00 commented 2026-11-24 06:48:21 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-22T03:08:07Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7073347508 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/323#issuecomment-7073347508). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-22T03:08:07Z. --- 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.
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!318
No description provided.