cpu: lift VEX.128 shifts with XMM or memory count #1334

Merged
tedin7 merged 1 commit from upstream-pr/1341 into main 2026-11-24 09:06:15 +00:00
tedin7 commented 2026-11-24 08:37:56 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T08:37:56Z.


What

  • tools/cpu_vex_aot.py lifts vpsraw, vpsllw and vpsrlw when the count is an XMM register or a flat 16-byte memory operand ([rcx], [rdx], [r8]), in addition to the existing imm8 form. New C++ helper shift_count reads the low 64 bits of the count operand as one unsigned integer and saturates it to 16 before the existing shift helpers run, so no bit of the quadword is lost (2^32 and 2^32 + 1 clear a logical shift and fill vpsraw with the sign). The upper quadword is ignored. The memory form is one 16-byte load, as for the other sources.
  • tests/cpu/vex_words.asm/.def and the export list in CMakeLists.txt: six new functions sraw_count_reg, sraw_count_mem, sllw_count_reg, sllw_count_mem, srlw_count_reg, srlw_count_mem.
  • tests/cpu/vex_words_check.py: the new functions join the shared native vs lifted loops (4096 pairs, guards, aliasing, RCX == RDX, misaligned). A dedicated check runs 56 low counts (0 to 39, 255, 0xffff, 0x10000, 0x7fffffff, 0x80000000, 0xffffffff, 2^32, 2^32 + 1, 2^32 + 15, 2^33 + 3, 2^48, 2^63, 2^64 - 1 and more) with four upper quadwords over 48 inputs (10752 cases per function) against an independent per-lane integer model, native and lifted. It also asserts that the model of a count truncated to 32 bits differs from the correct model.
  • Negative controls: the pinned unsupported vpsraw xmm0,xmm1,xmm2 is replaced by vpsllw ymm0,ymm1,xmm2 (VEX.256), counts at [rbx] and [rcx+8], vpsrad (still unsupported) and a vpsllw with an undefined count register.
  • docs/experiments.md: supported forms, count semantics, test description, controls.

Why

These count forms were rejected as unsupported. The count semantics are those of the integer instructions: low quadword only, unsigned, saturating above 15. A narrower count type would turn 2^32 into 0 and silently change results. VEX.128 zeroes the upper YMM bits, which the profile never reads (unchanged).

Tested

Windows (not run locally):

cmake --build --preset dev --target cpu_vex_words_original_lifted cpu_vex_words_altered_lifted
ctest --preset dev --no-tests=error -R '^cpu_aot_vex_words$' -V

and the same with --preset release. MSVC/ML64 was NOT run locally: the MASM source is unassembled until CI.

Linux (this branch, rebased on origin/main):

  • The fixture bodies were mirrored to GNU as syntax. A PE was built with MinGW (as + gcc -shared, with SEH unwind data), translated with the real cpu_vex_aot.translate(), and the lifted C++ compiled with g++ at -O0 and -O2. The unmodified vex_words_check.py main() ran on Linux (AVX2 host) with the GNU as builds as native and the lifted shared objects, with only the library loader substituted: all checks passed at -O0 and -O2, including the 10752 count cases per function.
  • Under Wine, a small MinGW loader ran the native PE and the lifted MinGW DLL (-O0 and -O2) on 2112 count cases: native, lifted and a C model agreed.
  • Translator mutations, each detected: count truncated to 32 bits (first 2^32 case), truncated to 16 bits, upper quadword read, quadwords ORed. A clamp threshold change from 15 to 16 is equivalent, not a mutation.
  • python3 -m py_compile on the touched Python, check_rejected_encodings() (13 encodings), tests/docs_links_check.py, tests/bidi_controls_check.py, git diff --check; a scan found no local in vex_words_check.py that shadows a module name.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6577863928 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1341). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T08:37:56Z. --- ## What - `tools/cpu_vex_aot.py` lifts `vpsraw`, `vpsllw` and `vpsrlw` when the count is an XMM register or a flat 16-byte memory operand (`[rcx]`, `[rdx]`, `[r8]`), in addition to the existing imm8 form. New C++ helper `shift_count` reads the low 64 bits of the count operand as one unsigned integer and saturates it to 16 before the existing shift helpers run, so no bit of the quadword is lost (2^32 and 2^32 + 1 clear a logical shift and fill `vpsraw` with the sign). The upper quadword is ignored. The memory form is one 16-byte load, as for the other sources. - `tests/cpu/vex_words.asm`/`.def` and the export list in `CMakeLists.txt`: six new functions `sraw_count_reg`, `sraw_count_mem`, `sllw_count_reg`, `sllw_count_mem`, `srlw_count_reg`, `srlw_count_mem`. - `tests/cpu/vex_words_check.py`: the new functions join the shared native vs lifted loops (4096 pairs, guards, aliasing, RCX == RDX, misaligned). A dedicated check runs 56 low counts (0 to 39, 255, 0xffff, 0x10000, 0x7fffffff, 0x80000000, 0xffffffff, 2^32, 2^32 + 1, 2^32 + 15, 2^33 + 3, 2^48, 2^63, 2^64 - 1 and more) with four upper quadwords over 48 inputs (10752 cases per function) against an independent per-lane integer model, native and lifted. It also asserts that the model of a count truncated to 32 bits differs from the correct model. - Negative controls: the pinned unsupported `vpsraw xmm0,xmm1,xmm2` is replaced by `vpsllw ymm0,ymm1,xmm2` (VEX.256), counts at `[rbx]` and `[rcx+8]`, `vpsrad` (still unsupported) and a `vpsllw` with an undefined count register. - `docs/experiments.md`: supported forms, count semantics, test description, controls. ## Why These count forms were rejected as unsupported. The count semantics are those of the integer instructions: low quadword only, unsigned, saturating above 15. A narrower count type would turn 2^32 into 0 and silently change results. VEX.128 zeroes the upper YMM bits, which the profile never reads (unchanged). ## Tested Windows (not run locally): ``` cmake --build --preset dev --target cpu_vex_words_original_lifted cpu_vex_words_altered_lifted ctest --preset dev --no-tests=error -R '^cpu_aot_vex_words$' -V ``` and the same with `--preset release`. MSVC/ML64 was NOT run locally: the MASM source is unassembled until CI. Linux (this branch, rebased on origin/main): - The fixture bodies were mirrored to GNU as syntax. A PE was built with MinGW (as + gcc -shared, with SEH unwind data), translated with the real `cpu_vex_aot.translate()`, and the lifted C++ compiled with g++ at -O0 and -O2. The unmodified `vex_words_check.py main()` ran on Linux (AVX2 host) with the GNU as builds as native and the lifted shared objects, with only the library loader substituted: all checks passed at -O0 and -O2, including the 10752 count cases per function. - Under Wine, a small MinGW loader ran the native PE and the lifted MinGW DLL (-O0 and -O2) on 2112 count cases: native, lifted and a C model agreed. - Translator mutations, each detected: count truncated to 32 bits (first 2^32 case), truncated to 16 bits, upper quadword read, quadwords ORed. A clamp threshold change from 15 to 16 is equivalent, not a mutation. - `python3 -m py_compile` on the touched Python, `check_rejected_encodings()` (13 encodings), `tests/docs_links_check.py`, `tests/bidi_controls_check.py`, `git diff --check`; a scan found no local in `vex_words_check.py` that shadows a module name.
tedin7 commented 2026-11-24 09:04:47 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T08:46:36Z.


Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207) at commit 45e246e5 (head of PR):

  • Debug (--preset dev):
    • Build (cpu_vex_words_original, cpu_vex_words_original_lifted, cpu_vex_words_altered, cpu_vex_words_altered_lifted): PASS
    • Test (ctest --preset dev -R '^cpu_aot_vex_words$' --no-tests=error --output-on-failure): 1/1 passed (2.39s)
  • Release /O2 (--preset release):
    • Build (cpu_vex_words_*): 0.88s (configure 1.59s)
    • Test (ctest --preset release -R '^cpu_aot_vex_words$' --no-tests=error --output-on-failure): 1/1 passed (2.28s)
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7126548795 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1341#issuecomment-7126548795). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T08:46:36Z. --- Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207) at commit `45e246e5` (head of PR): - **Debug (`--preset dev`)**: - Build (`cpu_vex_words_original`, `cpu_vex_words_original_lifted`, `cpu_vex_words_altered`, `cpu_vex_words_altered_lifted`): PASS - Test (`ctest --preset dev -R '^cpu_aot_vex_words$' --no-tests=error --output-on-failure`): **1/1 passed** (`2.39s`) - **Release `/O2` (`--preset release`)**: - Build (`cpu_vex_words_*`): `0.88s` (configure `1.59s`) - Test (`ctest --preset release -R '^cpu_aot_vex_words$' --no-tests=error --output-on-failure`): **1/1 passed** (`2.28s`)
chatgpt-codex-connector[bot commented 2026-11-24 09:04:47 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-24T08:46:48Z.


Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-24T08:50:48.434839Z 45e246e Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7126552915 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1341#issuecomment-7126552915). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-24T08:46:48Z. --- <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"45e246e52b4113b63b7b5843aa3163c69e21bd06","has_any_findings":false,"has_unresolved_findings":false,"status":"completed"},"security_review":{"commit_sha":null,"has_any_findings":null,"has_unresolved_findings":null,"status":null}} --> <details> <summary><strong>Codex Review Summary</strong>: ✅ Completed</summary> This comment shows the latest Codex review activity on this pull request. | Review | Status | Commit | Review trigger | | --- | --- | --- | --- | | 📝 **Code Review** | ✅ **Completed** <relative-time datetime="2026-11-24T08:50:48.434839Z">2026-11-24T08:50:48.434839Z</relative-time> | `45e246e` | Draft marked ready | <details> <summary>ℹ️ About Codex in GitHub</summary> <br/> [Your team has set up Codex to review pull requests in this repo](https://chatgpt.com/codex/cloud/settings/general). Reviews are triggered when you - Open a pull request for review - Mark a draft as ready - Comment "@codex review" or "@codex security review". Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. </details> </details>
upstream-sync closed this pull request 2026-11-24 10:02:22 +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!1334
No description provided.