cpu: lower all 60 VEX FMA3 scalar and packed instructions #497

Merged
PrivacyOnion merged 2 commits from upstream-pr/503 into main 2026-11-23 13:09:11 +00:00
PrivacyOnion commented 2026-11-22 08:21:30 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by PrivacyOnion on 2026-11-22T08:21:30Z.


### Summary
Adds AOT lowering in tools/cpu_avx2_aot.py for all 60 VEX-encoded x64 FMA3 instructions listed in docs/coverage/x64_zen2.tsv group FMA (96 iced_x86.Code variants across scalar ss/sd and packed 128-bit/256-bit ps/pd, supporting both register and flat memory op2 operands):

  • Families: vfmadd, vfmsub, vfnmadd, vfnmsub (ss, sd, ps, pd) and vfmaddsub, vfmsubadd (ps, pd)
  • Operand orders: 132, 213, 231
  • Hardware-exact semantics: single-rounding std::fmaf/std::fma, x86 semantic (a, b, c) NaN priority with QNaN quietening and sign preservation on negated-product forms, scalar upper-lane preservation (op0[127:32] / op0[127:64]), and VEX 128-bit upper-lane zeroing ([255:128]).

Fixture & Coverage

  • Added source-built MASM fixture tests/cpu/fma3_leaf.asm (; coverage: source cpu_aot_fma3) + tests/cpu/fma3_leaf.def with 102 exports (all 96 register variants + 6 memory-operand variants covering m32, m64, m128, m256) and an ALTER_FMA mutation variant.
  • Added tests/cpu/fma3_leaf_check.py running 714 bit-exact differential comparisons (native hardware DLL vs lifted C++ DLL) covering single-rounding witnesses, signed zeros, infinities, subnormals, QNaN/SNaN priority, randomized inputs, mutation control, and 5 rejection controls (undefined op0, nonvolatile ymm6, EVEX FMA3, unsupported RSP memory base, and AMD FMA4).
  • Added check_fma3_leaf() to tests/form_coverage_check.py verifying 60/60 FMA coverage.

Test Commands & Results

  • ctest --preset dev --no-tests=error -R "^(cpu_aot_fma3|cpu_aot_avx2_regs)$" -V -> 2/2 passed (cpu_aot_fma3 passed (102 exports, 714 bit-exact differential evaluations))
  • ctest --preset release --no-tests=error -R "^cpu_aot_fma3$" -V -> 1/1 passed at /O2
  • .\out\pe-env\Scripts\python.exe -B tests/form_coverage_check.py -> passed (FMA 60/60)
  • .\out\pe-env\Scripts\python.exe -B tests/bidi_controls_check.py -> passed
  • .\out\pe-env\Scripts\python.exe -B tests/docs_links_check.py -> passed
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6540213609 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/503). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-22T08:21:30Z. --- ### Summary Adds AOT lowering in `tools/cpu_avx2_aot.py` for all **60 VEX-encoded x64 FMA3 instructions** listed in `docs/coverage/x64_zen2.tsv` group `FMA` (96 `iced_x86.Code` variants across scalar `ss`/`sd` and packed 128-bit/256-bit `ps`/`pd`, supporting both register and flat memory `op2` operands): - **Families**: `vfmadd`, `vfmsub`, `vfnmadd`, `vfnmsub` (`ss`, `sd`, `ps`, `pd`) and `vfmaddsub`, `vfmsubadd` (`ps`, `pd`) - **Operand orders**: `132`, `213`, `231` - **Hardware-exact semantics**: single-rounding `std::fmaf`/`std::fma`, x86 semantic `(a, b, c)` NaN priority with QNaN quietening and sign preservation on negated-product forms, scalar upper-lane preservation (`op0[127:32]` / `op0[127:64]`), and VEX 128-bit upper-lane zeroing (`[255:128]`). ### Fixture & Coverage - Added source-built MASM fixture `tests/cpu/fma3_leaf.asm` (`; coverage: source cpu_aot_fma3`) + `tests/cpu/fma3_leaf.def` with 102 exports (all 96 register variants + 6 memory-operand variants covering `m32`, `m64`, `m128`, `m256`) and an `ALTER_FMA` mutation variant. - Added `tests/cpu/fma3_leaf_check.py` running 714 bit-exact differential comparisons (native hardware DLL vs lifted C++ DLL) covering single-rounding witnesses, signed zeros, infinities, subnormals, QNaN/SNaN priority, randomized inputs, mutation control, and 5 rejection controls (undefined `op0`, nonvolatile `ymm6`, EVEX FMA3, unsupported `RSP` memory base, and AMD FMA4). - Added `check_fma3_leaf()` to `tests/form_coverage_check.py` verifying 60/60 `FMA` coverage. ### Test Commands & Results - `ctest --preset dev --no-tests=error -R "^(cpu_aot_fma3|cpu_aot_avx2_regs)$" -V` -> **2/2 passed** (`cpu_aot_fma3 passed (102 exports, 714 bit-exact differential evaluations)`) - `ctest --preset release --no-tests=error -R "^cpu_aot_fma3$" -V` -> **1/1 passed** at `/O2` - `.\out\pe-env\Scripts\python.exe -B tests/form_coverage_check.py` -> **passed** (FMA 60/60) - `.\out\pe-env\Scripts\python.exe -B tests/bidi_controls_check.py` -> **passed** - `.\out\pe-env\Scripts\python.exe -B tests/docs_links_check.py` -> **passed**
upstream-sync closed this pull request 2026-11-24 06:43:23 +00:00
legacy-code-archaeologist commented 2026-11-24 06:48:33 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-22T14:53:05Z.


Verification of c5626ec on an Intel Core i7-6820HQ (Skylake; FMA3 and AVX2, no AVX-512). Windows 11 26100.9168, MSVC 19.52.37029, dev preset:

ctest --preset dev --no-tests=error -R "^cpu_aot_(fma|avx2)"
cpu_aot_fma3 passed (102 exports, 714 bit-exact differential evaluations)
1/2 Test #103: cpu_aot_avx2_regs ................   Passed    0.51 sec
2/2 Test #107: cpu_aot_fma3 .....................   Passed    0.20 sec

python -B tests/form_coverage_check.py also reports "form coverage checks passed". The FMA results come from a CPU generation different from the Zen 2 coverage table, and all 714 evaluations match bit for bit.

Two notes for coordination:

  • c5626ec no longer merges cleanly with main at f8d11ad. git merge-tree reports a conflict in CMakeLists.txt. tests/form_coverage_check.py merges automatically.
  • #349 also lowers the 36 packed VEX FMA3 forms, in the other translator (tools/ffx_version_query.py). This PR covers them in tools/cpu_avx2_aot.py. The code does not overlap. Both PRs do claim the same FMA rows of docs/coverage/x64_zen2.tsv, so whichever lands second may want to state which translator its coverage entry refers to.

AI-assisted: run and summarized with Claude Code; outputs copied from the local runs.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7080877963 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/503#issuecomment-7080877963). Originally posted by [legacy-code-archaeologist](https://github.com/legacy-code-archaeologist) on 2026-11-22T14:53:05Z. --- Verification of `c5626ec` on an Intel Core i7-6820HQ (Skylake; FMA3 and AVX2, no AVX-512). Windows 11 26100.9168, MSVC 19.52.37029, `dev` preset: ```text ctest --preset dev --no-tests=error -R "^cpu_aot_(fma|avx2)" cpu_aot_fma3 passed (102 exports, 714 bit-exact differential evaluations) 1/2 Test #103: cpu_aot_avx2_regs ................ Passed 0.51 sec 2/2 Test #107: cpu_aot_fma3 ..................... Passed 0.20 sec ``` `python -B tests/form_coverage_check.py` also reports "form coverage checks passed". The FMA results come from a CPU generation different from the Zen 2 coverage table, and all 714 evaluations match bit for bit. Two notes for coordination: - `c5626ec` no longer merges cleanly with `main` at `f8d11ad`. `git merge-tree` reports a conflict in `CMakeLists.txt`. `tests/form_coverage_check.py` merges automatically. - #349 also lowers the 36 packed VEX FMA3 forms, in the other translator (`tools/ffx_version_query.py`). This PR covers them in `tools/cpu_avx2_aot.py`. The code does not overlap. Both PRs do claim the same `FMA` rows of `docs/coverage/x64_zen2.tsv`, so whichever lands second may want to state which translator its coverage entry refers to. AI-assisted: run and summarized with Claude Code; outputs copied from the local runs.
PrivacyOnion commented 2026-11-24 06:48:33 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by PrivacyOnion on 2026-11-22T15:07:05Z.


Rebased onto current main (c8a3fd9); the only conflict was in CMakeLists.txt (both sides appended independent CPU test blocks, both kept). No code change otherwise.

Thanks for the independent Skylake verification.

Coordination with #349: that PR lowers the 36 packed VEX FMA3 forms in tools/ffx_version_query.py. This PR covers all 60 FMA forms in tools/cpu_avx2_aot.py. The code does not overlap. Both claim the same FMA rows of docs/coverage/x64_zen2.tsv; this PR's coverage entry (cpu_aot_fma3) refers to the cpu_avx2_aot translator.

Re-run after rebase (dev preset, Windows 11, MSVC):

  • ctest --preset dev --no-tests=error -R "^(cpu_aot_fma3|cpu_aot_avx2_regs)$" -> 2/2 passed
  • tests/form_coverage_check.py, tests/bidi_controls_check.py -> passed
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7081056579 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/503#issuecomment-7081056579). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-22T15:07:05Z. --- Rebased onto current `main` (c8a3fd9); the only conflict was in `CMakeLists.txt` (both sides appended independent CPU test blocks, both kept). No code change otherwise. Thanks for the independent Skylake verification. Coordination with #349: that PR lowers the 36 packed VEX FMA3 forms in `tools/ffx_version_query.py`. This PR covers all 60 FMA forms in `tools/cpu_avx2_aot.py`. The code does not overlap. Both claim the same FMA rows of `docs/coverage/x64_zen2.tsv`; this PR's coverage entry (`cpu_aot_fma3`) refers to the `cpu_avx2_aot` translator. Re-run after rebase (dev preset, Windows 11, MSVC): - `ctest --preset dev --no-tests=error -R "^(cpu_aot_fma3|cpu_aot_avx2_regs)$"` -> 2/2 passed - `tests/form_coverage_check.py`, `tests/bidi_controls_check.py` -> passed
legacy-code-archaeologist commented 2026-11-24 06:48:34 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-22T15:21:34Z.


Re-checked the rebased head d9382c3 on the same Skylake i7-6820HQ with a fresh configure and build. The FMA and AVX2 tests already on main pass alongside this PR's test:

ctest --preset dev --no-tests=error -R "^cpu_aot_(fma|avx2)" -V
1/4 Test #103: cpu_aot_avx2_regs ................   Passed
cpu_aot_fma3 passed (102 exports, 714 bit-exact differential evaluations)
2/4 Test #109: cpu_aot_fma3 .....................   Passed
3/4 Test #137: cpu_aot_avx2_shifts ..............   Passed
4/4 Test #142: cpu_aot_fma_scalar ...............   Passed
100% tests passed, 0 tests failed out of 4

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7081241908 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/503#issuecomment-7081241908). Originally posted by [legacy-code-archaeologist](https://github.com/legacy-code-archaeologist) on 2026-11-22T15:21:34Z. --- Re-checked the rebased head `d9382c3` on the same Skylake i7-6820HQ with a fresh configure and build. The FMA and AVX2 tests already on `main` pass alongside this PR's test: ```text ctest --preset dev --no-tests=error -R "^cpu_aot_(fma|avx2)" -V 1/4 Test #103: cpu_aot_avx2_regs ................ Passed cpu_aot_fma3 passed (102 exports, 714 bit-exact differential evaluations) 2/4 Test #109: cpu_aot_fma3 ..................... Passed 3/4 Test #137: cpu_aot_avx2_shifts .............. Passed 4/4 Test #142: cpu_aot_fma_scalar ............... Passed 100% tests passed, 0 tests failed out of 4 ``` AI-assisted (Claude Code).
R0Y00 commented 2026-11-24 06:49:10 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T12:56:07Z.


I resolved the coverage-test conflict on your original branch in 1dce507. The FMA3 and shader coverage checks both remain registered. The native FMA3 test now reuses the existing CPUID/XGETBV probe and reports a capability skip when FMA or OS-enabled XMM/YMM state is unavailable.

On Windows with MSVC 19.51, both the dev and release presets passed ctest --preset <preset> --no-tests=error -R "^(cpu_aot_fma3|cpu_aot_fma_packed)$" -V. The leaf fixture matched 714 comparisons across 102 exports, and the existing packed fixture matched 196. The form coverage check also passed. GitHub CI is running on the updated head.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7101826764 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/503#issuecomment-7101826764). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T12:56:07Z. --- I resolved the coverage-test conflict on your original branch in 1dce507. The FMA3 and shader coverage checks both remain registered. The native FMA3 test now reuses the existing CPUID/XGETBV probe and reports a capability skip when FMA or OS-enabled XMM/YMM state is unavailable. On Windows with MSVC 19.51, both the dev and release presets passed `ctest --preset <preset> --no-tests=error -R "^(cpu_aot_fma3|cpu_aot_fma_packed)$" -V`. The leaf fixture matched 714 comparisons across 102 exports, and the existing packed fixture matched 196. The form coverage check also passed. GitHub CI is running on the updated head.
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!497
No description provided.