cpu: lower opt-in memory-operand AVX2 forms (--mem-ops) in cpu_avx2_aot #2120

Merged
Ammmae0x merged 1 commit from upstream-pr/2128 into main 2026-11-25 11:33:02 +00:00
Ammmae0x commented 2026-11-25 01:00:59 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-25T01:00:59Z.


Change

Add opt-in memory-operand lowering (allow_memory=False default / --mem-ops CLI flag) for all 17 register-domain AVX2 forms in tools/cpu_avx2_aot.py, and add a source-built differential fixture and CTest (cpu_aot_avx2_mem):

  • Preserve existing register-only default behavior (allow_memory=False) so cpu_aot_avx2_regs continues rejecting all 17 memory forms unchanged.
  • Under --mem-ops, lower memory forms for vpbroadcastb (m8), vpbroadcastw (m16), vpbroadcastd (m32), and vpbroadcastq (m64) across XMM/YMM (with upper-128 zeroing on XMM writes), vinserti128 (m128), vextracti128 (m128 store enforcing R8 destination via flat_address(..., store=True)), and vperm2i128/vpermd/vpermpd/vpermps/vpermq/vpblendd/vpsllvd/vpsllvq/vpsravd/vpsrlvd/vpsrlvq (m128/m256 sources).
  • Preserve unaligned memory loads and discarded-read fault ordering across /O2 via volatile + __unaligned + std::atomic_signal_fence(std::memory_order_seq_cst) helpers (load8/load16/load32/load64/load128/load256 and store128/store256). All callers of tools/cpu_avx2_aot.py intentionally share the PRELUDE unaligned/volatile/signal-fence load and fenced store helpers so /O2 preserves unaligned reads and discarded-read fault ordering across both --mem-ops and non---mem-ops paths (re-verified across cpu_aot_avx2_regs, cpu_aot_avx2_mem, cpu_aot_avx2_shifts, cpu_aot_avx2_permutes, and cpu_aot_fma3 on both dev Debug and release /O2).
  • Export host_has_avx2() (CPUID.01H:ECX[27:28], _xgetbv(0) & 6 == 6, CPUID.00H:EAX >= 7, CPUID.07H.0:EBX[5]) for clean exit-77 skipping on non-AVX2 hosts.
  • Add MASM fixture tests/cpu/avx2_mem.asm (registered ; coverage: source cpu_aot_avx2_mem), tests/cpu/avx2_mem.def, differential harness tests/cpu/avx2_mem_check.py, and CMake targets/test cpu_aot_avx2_mem with SKIP_RETURN_CODE 77 and FORM_COVERAGE_RECORD.

Verification

Commit: fdac8751 (based on upstream/main a81beb55).

  • cmake --preset dev && cmake --build --preset dev --target cpu_avx2_regs_original cpu_avx2_regs_original_lifted cpu_avx2_regs_altered_lifted cpu_avx2_mem_original cpu_avx2_mem_original_lifted cpu_avx2_mem_altered cpu_avx2_mem_altered_lifted cpu_avx2_permutes cpu_avx2_permutes_lifted cpu_avx2_shifts cpu_avx2_shifts_lifted -> exit 0, 0 errors, 0 warnings.
  • cmake --preset release && cmake --build --preset release --target ... (same 11 targets) -> exit 0, 0 errors, 0 warnings.
  • ctest --preset dev --no-tests=error -R "^cpu_aot_avx2" -E "cpu_native_archive_" -V -> 4/4 Passed (0 capability skips).
  • ctest --preset release --no-tests=error -R "^cpu_aot_avx2" -E "cpu_native_archive_" -V -> 4/4 Passed (cpu_aot_avx2_regs 0.44s, cpu_aot_avx2_mem 0.37s, cpu_aot_avx2_shifts 0.43s, cpu_aot_avx2_permutes 0.43s; 0 capability skips).
  • FORM_COVERAGE_RECORD (dev and release) -> all 17 AVX2 mnemonics (vextracti128, vinserti128, vpblendd, vpbroadcastb/w/d/q, vperm2i128, vpermd, vpermpd, vpermps, vpermq, vpsllvd/q, vpsravd, vpsrlvd/q) plus ret, vmovdqu, vzeroupper.
  • out/pe-env/Scripts/python.exe -B tests/form_coverage_check.py -> exit 0 (form coverage checks passed).
  • out/pe-env/Scripts/python.exe -B tests/docs_links_check.py -> exit 0 (Checked 18 Markdown files and 396 links.).
  • out/pe-env/Scripts/python.exe -B tests/bidi_controls_check.py -> exit 0 (Checked 1432 tracked source, script and build/CI files: no bidirectional controls.).
  • Spot-mutation verification (4/4 killed, exit 8): disabling allow_memory=False default rejection, relaxing vextracti128 store=True R8 check, replacing load_vec32 volatile load with non-volatile memcpy on release /O2 (caught by discarded-read fault test), and dropping upper-128 zeroing in op_vpbroadcastb.

No capability skips (exit 77) observed on this AVX2-capable host.

Evidence and follow-up

tests/cpu/avx2_mem.asm and tests/cpu/avx2_mem.def are source-built MASM fixtures authored directly in this repository under the MIT license. The harness validates 32 exports across 622 native/lifted/reference cases (including unaligned displacements, XMM upper-128 zero-extension, discarded-read fault ordering, and ALTER_IMM differential controls), 17 default allow_memory=False rejections, and 12 unit + 4 patched-PE --mem-ops rejections.

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:6594680545 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2128). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-25T01:00:59Z. --- ## Change Add opt-in memory-operand lowering (`allow_memory=False` default / `--mem-ops` CLI flag) for all 17 register-domain AVX2 forms in `tools/cpu_avx2_aot.py`, and add a source-built differential fixture and CTest (`cpu_aot_avx2_mem`): - Preserve existing register-only default behavior (`allow_memory=False`) so `cpu_aot_avx2_regs` continues rejecting all 17 memory forms unchanged. - Under `--mem-ops`, lower memory forms for `vpbroadcastb` (`m8`), `vpbroadcastw` (`m16`), `vpbroadcastd` (`m32`), and `vpbroadcastq` (`m64`) across XMM/YMM (with upper-128 zeroing on XMM writes), `vinserti128` (`m128`), `vextracti128` (`m128` store enforcing `R8` destination via `flat_address(..., store=True)`), and `vperm2i128`/`vpermd`/`vpermpd`/`vpermps`/`vpermq`/`vpblendd`/`vpsllvd`/`vpsllvq`/`vpsravd`/`vpsrlvd`/`vpsrlvq` (`m128`/`m256` sources). - Preserve unaligned memory loads and discarded-read fault ordering across `/O2` via `volatile` + `__unaligned` + `std::atomic_signal_fence(std::memory_order_seq_cst)` helpers (`load8`/`load16`/`load32`/`load64`/`load128`/`load256` and `store128`/`store256`). All callers of `tools/cpu_avx2_aot.py` intentionally share the `PRELUDE` unaligned/volatile/signal-fence load and fenced store helpers so `/O2` preserves unaligned reads and discarded-read fault ordering across both `--mem-ops` and non-`--mem-ops` paths (re-verified across `cpu_aot_avx2_regs`, `cpu_aot_avx2_mem`, `cpu_aot_avx2_shifts`, `cpu_aot_avx2_permutes`, and `cpu_aot_fma3` on both `dev` Debug and `release` `/O2`). - Export `host_has_avx2()` (`CPUID.01H:ECX[27:28]`, `_xgetbv(0) & 6 == 6`, `CPUID.00H:EAX >= 7`, `CPUID.07H.0:EBX[5]`) for clean exit-77 skipping on non-AVX2 hosts. - Add MASM fixture `tests/cpu/avx2_mem.asm` (registered `; coverage: source cpu_aot_avx2_mem`), `tests/cpu/avx2_mem.def`, differential harness `tests/cpu/avx2_mem_check.py`, and CMake targets/test `cpu_aot_avx2_mem` with `SKIP_RETURN_CODE 77` and `FORM_COVERAGE_RECORD`. ## Verification Commit: `fdac8751` (based on `upstream/main` `a81beb55`). - `cmake --preset dev && cmake --build --preset dev --target cpu_avx2_regs_original cpu_avx2_regs_original_lifted cpu_avx2_regs_altered_lifted cpu_avx2_mem_original cpu_avx2_mem_original_lifted cpu_avx2_mem_altered cpu_avx2_mem_altered_lifted cpu_avx2_permutes cpu_avx2_permutes_lifted cpu_avx2_shifts cpu_avx2_shifts_lifted` -> exit `0`, 0 errors, 0 warnings. - `cmake --preset release && cmake --build --preset release --target ...` (same 11 targets) -> exit `0`, 0 errors, 0 warnings. - `ctest --preset dev --no-tests=error -R "^cpu_aot_avx2" -E "cpu_native_archive_" -V` -> `4/4 Passed` (0 capability skips). - `ctest --preset release --no-tests=error -R "^cpu_aot_avx2" -E "cpu_native_archive_" -V` -> `4/4 Passed` (`cpu_aot_avx2_regs` 0.44s, `cpu_aot_avx2_mem` 0.37s, `cpu_aot_avx2_shifts` 0.43s, `cpu_aot_avx2_permutes` 0.43s; 0 capability skips). - `FORM_COVERAGE_RECORD` (`dev` and `release`) -> all 17 AVX2 mnemonics (`vextracti128`, `vinserti128`, `vpblendd`, `vpbroadcastb/w/d/q`, `vperm2i128`, `vpermd`, `vpermpd`, `vpermps`, `vpermq`, `vpsllvd/q`, `vpsravd`, `vpsrlvd/q`) plus `ret`, `vmovdqu`, `vzeroupper`. - `out/pe-env/Scripts/python.exe -B tests/form_coverage_check.py` -> exit `0` (`form coverage checks passed`). - `out/pe-env/Scripts/python.exe -B tests/docs_links_check.py` -> exit `0` (`Checked 18 Markdown files and 396 links.`). - `out/pe-env/Scripts/python.exe -B tests/bidi_controls_check.py` -> exit `0` (`Checked 1432 tracked source, script and build/CI files: no bidirectional controls.`). - Spot-mutation verification (4/4 killed, exit `8`): disabling `allow_memory=False` default rejection, relaxing `vextracti128` `store=True` `R8` check, replacing `load_vec32` volatile load with non-volatile `memcpy` on `release` `/O2` (caught by discarded-read fault test), and dropping upper-128 zeroing in `op_vpbroadcastb`. No capability skips (exit 77) observed on this AVX2-capable host. ## Evidence and follow-up `tests/cpu/avx2_mem.asm` and `tests/cpu/avx2_mem.def` are source-built MASM fixtures authored directly in this repository under the MIT license. The harness validates 32 exports across 622 native/lifted/reference cases (including unaligned displacements, XMM upper-128 zero-extension, discarded-read fault ordering, and `ALTER_IMM` differential controls), 17 default `allow_memory=False` rejections, and 12 unit + 4 patched-PE `--mem-ops` rejections. ## 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.
tedin7 commented 2026-11-25 03:00:59 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T02:25:09Z.

Review: COMMENTED


Review of head fdac8751f3cb8dc43649f35071d54d5a846bc093 (comment only, no approval).

Scope: tools/cpu_avx2_aot.py (opt-in --mem-ops), new tests/cpu/avx2_mem.{asm,def}, tests/cpu/avx2_mem_check.py, CMakeLists.txt (+30).

Commands executed locally (Linux):

  • git diff --stat origin/main...HEAD: 5 files, +915/-51
  • python3 tests/docs_links_check.py: pass (18 files, 396 links)
  • python3 tests/bidi_controls_check.py: pass
  • git diff --check origin/main...HEAD: clean
  • python3 -m py_compile tools/cpu_avx2_aot.py tests/cpu/avx2_mem_check.py: pass
  • gh pr checks 2128: Linux Python checks, Windows build/test and gfx1013 fixtures pass. The /O2 job and the chart job are skipped.

Not run locally: cpu_aot_avx2_mem. It needs the MSVC-assembled fixture PE and a native DLL load, which this Linux host cannot build. For the same reason I could not run the required logic mutation here. I rely on the Windows CI job, and I could not read its log to confirm that this test ran and did not exit 77 (skip). Please confirm the job log shows cpu_aot_avx2_mem as Passed, not Skipped.

Findings

P1: none.

P2

  1. tools/cpu_avx2_aot.py PRELUDE: the change is not limited to --mem-ops. Every lifted file now gets volatile, signal-fenced load128/load256/load32/load64 and fenced store128/store256. Existing fixtures and outputs change behavior (they cannot be elided or merged, and an unaligned type is used on MSVC). Please say in the PR body that this is intended for all callers, or gate it. Also check that existing differential suites were rerun on Win2025 for the non-mem path.
  2. The new exported host_has_avx2 is only emitted under _MSC_VER. On other compilers the lifted DLL has no such export. If the test helper (host_has_avx2(lifted_lib)) is ever run against a non-MSVC build, it will fail at symbol lookup and not skip. Low risk today (CMake uses MSVC); a short comment would help.

Nits

  • flat_address(store=True) allows only r8 as store base while loads allow rcx/rdx/r8. That looks deliberate (output pointer convention), but add one line saying why.
  • store128/store256 still use memcpy while loads use volatile. Fine, but the asymmetry deserves a comment.
  • Memory forms are allowed for every mnemonic in REGISTER_ONLY_AVX2 when allow_memory is set. Unhandled mnemonics fall into read_vec, which rejects memory operands. Good. Confirm a rejection case exists in check_translator (the file says it has rejection checks).

Semantics read (static): broadcast loaders read exactly 1/2/4/8 bytes; VEX.128 shift and blend memory forms read 16 bytes and 256-bit forms read 32; vpermq/vpermpd read 32; vinserti128 reads 16; vextracti128 stores 16. These match the ISA operand sizes. The wide flag now comes from src1 and the destination width is used for the helper, which is consistent.

Clean-room and title: neutral title, MIT only, no model names seen in the diff.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6050796247 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2128#pullrequestreview-6050796247). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T02:25:09Z. **Review:** COMMENTED --- Review of head `fdac8751f3cb8dc43649f35071d54d5a846bc093` (comment only, no approval). Scope: `tools/cpu_avx2_aot.py` (opt-in `--mem-ops`), new `tests/cpu/avx2_mem.{asm,def}`, `tests/cpu/avx2_mem_check.py`, `CMakeLists.txt` (+30). Commands executed locally (Linux): - `git diff --stat origin/main...HEAD`: 5 files, +915/-51 - `python3 tests/docs_links_check.py`: pass (18 files, 396 links) - `python3 tests/bidi_controls_check.py`: pass - `git diff --check origin/main...HEAD`: clean - `python3 -m py_compile tools/cpu_avx2_aot.py tests/cpu/avx2_mem_check.py`: pass - `gh pr checks 2128`: Linux Python checks, Windows build/test and gfx1013 fixtures pass. The /O2 job and the chart job are skipped. Not run locally: `cpu_aot_avx2_mem`. It needs the MSVC-assembled fixture PE and a native DLL load, which this Linux host cannot build. For the same reason I could not run the required logic mutation here. I rely on the Windows CI job, and I could not read its log to confirm that this test ran and did not exit 77 (skip). Please confirm the job log shows `cpu_aot_avx2_mem` as Passed, not Skipped. Findings P1: none. P2 1. `tools/cpu_avx2_aot.py` PRELUDE: the change is not limited to `--mem-ops`. Every lifted file now gets volatile, signal-fenced `load128/load256/load32/load64` and fenced `store128/store256`. Existing fixtures and outputs change behavior (they cannot be elided or merged, and an unaligned type is used on MSVC). Please say in the PR body that this is intended for all callers, or gate it. Also check that existing differential suites were rerun on Win2025 for the non-mem path. 2. The new exported `host_has_avx2` is only emitted under `_MSC_VER`. On other compilers the lifted DLL has no such export. If the test helper (`host_has_avx2(lifted_lib)`) is ever run against a non-MSVC build, it will fail at symbol lookup and not skip. Low risk today (CMake uses MSVC); a short comment would help. Nits - `flat_address(store=True)` allows only `r8` as store base while loads allow `rcx/rdx/r8`. That looks deliberate (output pointer convention), but add one line saying why. - `store128/store256` still use `memcpy` while loads use volatile. Fine, but the asymmetry deserves a comment. - Memory forms are allowed for every mnemonic in `REGISTER_ONLY_AVX2` when `allow_memory` is set. Unhandled mnemonics fall into `read_vec`, which rejects memory operands. Good. Confirm a rejection case exists in `check_translator` (the file says it has rejection checks). Semantics read (static): broadcast loaders read exactly 1/2/4/8 bytes; VEX.128 shift and blend memory forms read 16 bytes and 256-bit forms read 32; vpermq/vpermpd read 32; vinserti128 reads 16; vextracti128 stores 16. These match the ISA operand sizes. The `wide` flag now comes from src1 and the destination width is used for the helper, which is consistent. Clean-room and title: neutral title, MIT only, no model names seen in the diff. AI-assisted; commands listed were executed locally.
upstream-sync closed this pull request 2026-11-25 12:01:34 +00:00
Ammmae0x commented 2026-11-25 12:01:54 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-25T11:36:44Z.


Updated PR #2128 (commit 7e8bc38a) to address the review findings:

  1. P2-1 (shared PRELUDE + non-mem re-verification): Documented in the PRELUDE comment and PR body that the volatile/unaligned/fenced helpers are intentional for all callers (including non---mem-ops paths). Re-ran all cpu_avx2_* + cpu_fma3_leaf targets and ctest -R "^(cpu_aot_avx2|cpu_aot_fma3)" on Windows x64 for both dev (Debug) and release (/O2): 5/5 Passed (cpu_aot_avx2_regs, cpu_aot_avx2_mem, cpu_aot_fma3, cpu_aot_avx2_shifts, cpu_aot_avx2_permutes), 0 capability skips (exit 77) — cpu_aot_avx2_mem executed 622 differential cases + discarded-read + 13 unit / 4 PE rejections and passed.
  2. P2-2 (_MSC_VER guard): Added comments at #if defined(_MSC_VER) (tools/cpu_avx2_aot.py) and host_has_avx2() (tests/cpu/avx2_mem_check.py) noting that non-MSVC builds omit the export and host_has_avx2() checks hasattr(lifted_lib, "host_has_avx2") before falling back.
  3. Nits: Documented why flat_address(store=True) restricts stores to r8 (3-arg (rcx, rdx, r8) ABI output convention) and why store* uses fenced memcpy while load* uses volatile; added unsupported_mem_op (VEX_VBROADCASTSS_YMM_M32) to mem_rejects in check_translator().
  • Spot-mutation check (disabling store=True R8 guard) killed by cpu_aot_avx2_mem (exit 8); form_coverage_check.py, docs_links_check.py, and bidi_controls_check.py all exit 0.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7161271617 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2128#issuecomment-7161271617). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-25T11:36:44Z. --- Updated PR #2128 (commit `7e8bc38a`) to address the review findings: 1. **P2-1 (shared PRELUDE + non-mem re-verification)**: Documented in the `PRELUDE` comment and PR body that the volatile/unaligned/fenced helpers are intentional for all callers (including non-`--mem-ops` paths). Re-ran all `cpu_avx2_*` + `cpu_fma3_leaf` targets and `ctest -R "^(cpu_aot_avx2|cpu_aot_fma3)"` on Windows x64 for both `dev` (Debug) and `release` (`/O2`): **5/5 Passed** (`cpu_aot_avx2_regs`, `cpu_aot_avx2_mem`, `cpu_aot_fma3`, `cpu_aot_avx2_shifts`, `cpu_aot_avx2_permutes`), 0 capability skips (exit 77) — `cpu_aot_avx2_mem` executed 622 differential cases + discarded-read + 13 unit / 4 PE rejections and passed. 2. **P2-2 (`_MSC_VER` guard)**: Added comments at `#if defined(_MSC_VER)` (`tools/cpu_avx2_aot.py`) and `host_has_avx2()` (`tests/cpu/avx2_mem_check.py`) noting that non-MSVC builds omit the export and `host_has_avx2()` checks `hasattr(lifted_lib, "host_has_avx2")` before falling back. 3. **Nits**: Documented why `flat_address(store=True)` restricts stores to `r8` (3-arg `(rcx, rdx, r8)` ABI output convention) and why `store*` uses fenced `memcpy` while `load*` uses `volatile`; added `unsupported_mem_op` (`VEX_VBROADCASTSS_YMM_M32`) to `mem_rejects` in `check_translator()`. - Spot-mutation check (disabling `store=True` `R8` guard) killed by `cpu_aot_avx2_mem` (exit 8); `form_coverage_check.py`, `docs_links_check.py`, and `bidi_controls_check.py` all exit 0. AI-assisted.
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!2120
No description provided.