cpu: lower VEX.128 16-bit word abs, sign, rounding mul-high, and hadd #2104

Merged
Ammmae0x merged 1 commit from upstream-pr/2111 into main 2026-11-25 08:48:32 +00:00
Ammmae0x commented 2026-11-25 00:35:57 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-25T00:35:57Z.


Change

Extend tools/cpu_vex_aot.py to lower four additional VEX.128 16-bit packed-word instructions (vpabsw, vpsignw, vpmulhrsw, vphaddw) for volatile XMM0..XMM5 registers and [rcx]/[rdx]/[r8] 16-byte memory operands, and add a source-built differential fixture and CTest (cpu_aot_vex_words_ext):

  • Add UNARY dispatch (M.VPABSW -> pabsw) and register-definition tracking in lift() so 2-operand VEX.128 unary forms enforce the same defined-source and volatile-XMM rules as BINARY/SHIFTS.
  • Add M.VPSIGNW -> psignw, M.VPMULHRSW -> pmulhrsw, and M.VPHADDW -> phaddw in BINARY, with explicit per-lane C++ helpers in PRELUDE avoiding signed-negation UB on INT16_MIN (0x8000) and spelling out rounding ((int32(a) * b) + 0x4000) >> 15 and pairwise horizontal addition.
  • Export host_has_avx() (CPUID.01H:ECX[27:28] + XGETBV(0) & 6 == 6) in PRELUDE so differential harnesses can gate native AVX execution cleanly (exiting 77 if unsupported while still running AOT lowering/rejection checks).
  • Add MASM fixture tests/cpu/vex_words_ext.asm (registered ; coverage: source cpu_aot_vex_words_ext), tests/cpu/vex_words_ext.def, differential harness tests/cpu/vex_words_ext_check.py, and CMake targets/test cpu_aot_vex_words_ext with SKIP_RETURN_CODE 77 and FORM_COVERAGE_RECORD.

Verification

Commit: 25ae80d0 (rebased onto upstream/main 30888b67).

  • cmake --preset dev && cmake --build --preset dev --target cpu_vex_words_original cpu_vex_words_original_lifted cpu_vex_words_altered cpu_vex_words_altered_lifted cpu_vex_words_ext_original cpu_vex_words_ext_original_lifted cpu_vex_words_ext_altered cpu_vex_words_ext_altered_lifted -> exit 0, 0 errors, 0 warnings.
  • cmake --preset release && cmake --build --preset release --target cpu_vex_words_original cpu_vex_words_original_lifted cpu_vex_words_altered cpu_vex_words_altered_lifted cpu_vex_words_ext_original cpu_vex_words_ext_original_lifted cpu_vex_words_ext_altered cpu_vex_words_ext_altered_lifted -> exit 0, 0 errors, 0 warnings.
  • ctest --preset dev -N -> Total Tests: 1319 (includes cpu_aot_vex_words and cpu_aot_vex_words_ext).
  • ctest --preset dev --no-tests=error -R "^cpu_aot_vex_words" -E "cpu_native_archive_" -V -> 2/2 Passed (0 capability skips).
  • ctest --preset release --no-tests=error -R "^cpu_aot_vex_words" -E "cpu_native_archive_" -V -> 2/2 Passed (0 capability skips).
  • FORM_COVERAGE_RECORD (dev and release) -> ret, vmovdqa, vmovdqu, vpabsw, vphaddw, vpmulhrsw, vpsignw, 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.
  • out/pe-env/Scripts/python.exe -B tests/bidi_controls_check.py -> exit 0.
  • Spot-mutation verification: independently mutating UNARY defined-register tracking in lift(), pmulhrsw rounding bias (+ 0x4000 -> + 0), and psignw zero-mask branch (0 -> word(a, i)) each caused ctest --preset dev -R "^cpu_aot_vex_words_ext$" to fail (exit 8).

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

Evidence and follow-up

tests/cpu/vex_words_ext.asm and tests/cpu/vex_words_ext.def are source-built MASM fixtures authored directly in this repository under the MIT license. The harness checks 10 exports across 1024 input pairs (including edge pairings, INT16_MIN, exact output aliases, RCX == RDX, and 8-byte misalignment on vmovdqu-only exports), 29 rejected encodings (VEX.256, EVEX, legacy SSSE3, nonvolatile XMM6..XMM15, undefined sources, displaced/indexed/invalid memory operands, invalid stores), 3 patched-PE rejections, 4 faulty reference mutants, and 3 ALTER_EXT build controls.

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:6594382430 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2111). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-25T00:35:57Z. --- ## Change Extend `tools/cpu_vex_aot.py` to lower four additional VEX.128 16-bit packed-word instructions (`vpabsw`, `vpsignw`, `vpmulhrsw`, `vphaddw`) for volatile `XMM0..XMM5` registers and `[rcx]`/`[rdx]`/`[r8]` 16-byte memory operands, and add a source-built differential fixture and CTest (`cpu_aot_vex_words_ext`): - Add `UNARY` dispatch (`M.VPABSW -> pabsw`) and register-definition tracking in `lift()` so 2-operand VEX.128 unary forms enforce the same defined-source and volatile-XMM rules as `BINARY`/`SHIFTS`. - Add `M.VPSIGNW -> psignw`, `M.VPMULHRSW -> pmulhrsw`, and `M.VPHADDW -> phaddw` in `BINARY`, with explicit per-lane C++ helpers in `PRELUDE` avoiding signed-negation UB on `INT16_MIN` (`0x8000`) and spelling out rounding `((int32(a) * b) + 0x4000) >> 15` and pairwise horizontal addition. - Export `host_has_avx()` (`CPUID.01H:ECX[27:28]` + `XGETBV(0) & 6 == 6`) in `PRELUDE` so differential harnesses can gate native AVX execution cleanly (exiting 77 if unsupported while still running AOT lowering/rejection checks). - Add MASM fixture `tests/cpu/vex_words_ext.asm` (registered `; coverage: source cpu_aot_vex_words_ext`), `tests/cpu/vex_words_ext.def`, differential harness `tests/cpu/vex_words_ext_check.py`, and CMake targets/test `cpu_aot_vex_words_ext` with `SKIP_RETURN_CODE 77` and `FORM_COVERAGE_RECORD`. ## Verification Commit: `25ae80d0` (rebased onto `upstream/main` `30888b67`). - `cmake --preset dev && cmake --build --preset dev --target cpu_vex_words_original cpu_vex_words_original_lifted cpu_vex_words_altered cpu_vex_words_altered_lifted cpu_vex_words_ext_original cpu_vex_words_ext_original_lifted cpu_vex_words_ext_altered cpu_vex_words_ext_altered_lifted` -> exit `0`, 0 errors, 0 warnings. - `cmake --preset release && cmake --build --preset release --target cpu_vex_words_original cpu_vex_words_original_lifted cpu_vex_words_altered cpu_vex_words_altered_lifted cpu_vex_words_ext_original cpu_vex_words_ext_original_lifted cpu_vex_words_ext_altered cpu_vex_words_ext_altered_lifted` -> exit `0`, 0 errors, 0 warnings. - `ctest --preset dev -N` -> `Total Tests: 1319` (includes `cpu_aot_vex_words` and `cpu_aot_vex_words_ext`). - `ctest --preset dev --no-tests=error -R "^cpu_aot_vex_words" -E "cpu_native_archive_" -V` -> `2/2 Passed` (0 capability skips). - `ctest --preset release --no-tests=error -R "^cpu_aot_vex_words" -E "cpu_native_archive_" -V` -> `2/2 Passed` (0 capability skips). - `FORM_COVERAGE_RECORD` (`dev` and `release`) -> `ret`, `vmovdqa`, `vmovdqu`, `vpabsw`, `vphaddw`, `vpmulhrsw`, `vpsignw`, `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`. - `out/pe-env/Scripts/python.exe -B tests/bidi_controls_check.py` -> exit `0`. - Spot-mutation verification: independently mutating `UNARY` defined-register tracking in `lift()`, `pmulhrsw` rounding bias (`+ 0x4000 -> + 0`), and `psignw` zero-mask branch (`0 -> word(a, i)`) each caused `ctest --preset dev -R "^cpu_aot_vex_words_ext$"` to fail (exit `8`). No capability skips (exit 77) observed on this AVX-capable host. ## Evidence and follow-up `tests/cpu/vex_words_ext.asm` and `tests/cpu/vex_words_ext.def` are source-built MASM fixtures authored directly in this repository under the MIT license. The harness checks 10 exports across 1024 input pairs (including edge pairings, `INT16_MIN`, exact output aliases, `RCX == RDX`, and 8-byte misalignment on `vmovdqu`-only exports), 29 rejected encodings (VEX.256, EVEX, legacy SSSE3, nonvolatile `XMM6..XMM15`, undefined sources, displaced/indexed/invalid memory operands, invalid stores), 3 patched-PE rejections, 4 faulty reference mutants, and 3 `ALTER_EXT` build controls. ## 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:56 +00:00 (Migrated from github.com)

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

Review: COMMENTED


Review of head 25ae80d0a9

Result: no P1, no P2. Two nits.

Commands run locally (Linux worktree of the PR head):

  • git diff --stat origin/main...HEAD: 5 files, +610/-1 (CMakeLists.txt, tools/cpu_vex_aot.py, 3 new files under tests/cpu/)
  • python3 tests/docs_links_check.py: Self-test passed. Checked 18 Markdown files and 395 links.
  • python3 tests/bidi_controls_check.py: Self-test passed. 1425 files, no bidirectional controls.
  • git diff --check origin/main...HEAD: clean
  • gh pr checks 2111: Windows build and test pass, gfx1013 fixtures pass, Linux Python checks pass (the /O2 job was skipped)
  • Title is neutral and the content looks clean-room.

Semantics checked by hand against the ISA text (tools/cpu_vex_aot.py PRELUDE):

  • pabsw: 0x8000 stays 0x8000 through the unsigned negate. Correct, and there is no signed-overflow UB.
  • psignw: negative b gives -a, zero b gives 0, positive b gives a. Matches. The -0x8000 case wraps correctly.
  • pmulhrsw: (a*b + 0x4000) >> 15 is identical to the Intel form ((a*b >> 14) + 1) >> 1, because floor((floor(x/2^14)+1)/2) = floor((x+2^14)/2^15). The 0x8000*0x8000 case gives 0x8000 after the 16-bit truncation, which is also what the hardware returns.
  • phaddw: pairwise sums, a lanes in the low half and b lanes in the high half. The wrap-around goes through set_word. Matches.
  • lift(): UNARY is added to the defined-source check and the destination tracking. The undefined-XMM-source rule applies to vpabsw as it does to BINARY.

Not run:

  • ctest -R cpu_aot_vex_words_ext and the fixture need MASM, MSVC and iced_x86, so they cannot run on this Linux box (the check script fails to import iced_x86 here). I did not run the fixture or any mutation of the new lowering. I am relying on the green Windows CI job for that. No Win2025 run was started, since the change is CPU lowering and not shader or C++ GPU work.

Nits:

  1. tools/cpu_vex_aot.py (host_has_avx): <intrin.h>, __cpuid and _xgetbv are MSVC-specific. That fits the existing __declspec(dllexport) use, but a short comment saying the lifted output targets MSVC only would help.
  2. The PR body should say plainly which of the new differential cases the Windows CI host exercised natively, and which hit the exit-77 skip. That way the green check cannot hide a skipped native-AVX comparison.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6050554703 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2111#pullrequestreview-6050554703). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T02:05:39Z. **Review:** COMMENTED --- Review of head 25ae80d0a9706207059371d4f4055aae809a52f2 Result: no P1, no P2. Two nits. Commands run locally (Linux worktree of the PR head): - `git diff --stat origin/main...HEAD`: 5 files, +610/-1 (CMakeLists.txt, tools/cpu_vex_aot.py, 3 new files under tests/cpu/) - `python3 tests/docs_links_check.py`: Self-test passed. Checked 18 Markdown files and 395 links. - `python3 tests/bidi_controls_check.py`: Self-test passed. 1425 files, no bidirectional controls. - `git diff --check origin/main...HEAD`: clean - `gh pr checks 2111`: Windows build and test pass, gfx1013 fixtures pass, Linux Python checks pass (the /O2 job was skipped) - Title is neutral and the content looks clean-room. Semantics checked by hand against the ISA text (tools/cpu_vex_aot.py PRELUDE): - `pabsw`: 0x8000 stays 0x8000 through the unsigned negate. Correct, and there is no signed-overflow UB. - `psignw`: negative b gives -a, zero b gives 0, positive b gives a. Matches. The -0x8000 case wraps correctly. - `pmulhrsw`: `(a*b + 0x4000) >> 15` is identical to the Intel form `((a*b >> 14) + 1) >> 1`, because floor((floor(x/2^14)+1)/2) = floor((x+2^14)/2^15). The 0x8000*0x8000 case gives 0x8000 after the 16-bit truncation, which is also what the hardware returns. - `phaddw`: pairwise sums, a lanes in the low half and b lanes in the high half. The wrap-around goes through set_word. Matches. - `lift()`: UNARY is added to the defined-source check and the destination tracking. The undefined-XMM-source rule applies to vpabsw as it does to BINARY. Not run: - `ctest -R cpu_aot_vex_words_ext` and the fixture need MASM, MSVC and iced_x86, so they cannot run on this Linux box (the check script fails to import iced_x86 here). I did not run the fixture or any mutation of the new lowering. I am relying on the green Windows CI job for that. No Win2025 run was started, since the change is CPU lowering and not shader or C++ GPU work. Nits: 1. tools/cpu_vex_aot.py (`host_has_avx`): `<intrin.h>`, `__cpuid` and `_xgetbv` are MSVC-specific. That fits the existing `__declspec(dllexport)` use, but a short comment saying the lifted output targets MSVC only would help. 2. The PR body should say plainly which of the new differential cases the Windows CI host exercised natively, and which hit the exit-77 skip. That way the green check cannot hide a skipped native-AVX comparison. AI-assisted; commands listed were executed locally.
upstream-sync closed this pull request 2026-11-25 09:03:54 +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!2104
No description provided.