cpu: lower AVX2 integer broadcasts and variable per-lane shifts #334

Merged
Vonsek merged 5 commits from upstream-pr/339 into main 2026-11-22 09:31:02 +00:00
Vonsek commented 2026-11-21 22:41:05 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Vonsek on 2026-11-21T22:41:05Z.


Change

  • Lower 128-bit and 256-bit AVX2 integer broadcasts (vpbroadcastb, vpbroadcastw, vpbroadcastd, vpbroadcastq) and variable per-lane vector shifts (vpsllvd, vpsrlvd, vpsravd, vpsllvq, vpsrlvq) in tools/ffx_version_query.py, zeroing the upper 128 bits on 128-bit VEX destinations and clamping out-of-range shift counts to match hardware.
  • Add a source-built MASM fixture (tests/cpu/avx2_shifts.asm, tests/cpu/avx2_shifts.def) and differential test (tests/cpu/avx2_shifts.py) wired into CMakeLists.txt as cpu_aot_avx2_shifts with a host_has_avx2() check (OSXSAVE, XCR0 YMM state, and CPUID leaf 7 EBX bit 5 AVX2) and SKIP_RETURN_CODE 77 on unsupported hosts.
  • Verify all 10 exports (256-bit memory broadcasts, 128-bit register broadcast with upper-lane zeroing, and 256-bit 32/64-bit variable shifts including counts >= 32/64) against both the native DLL and a reference model.
  • Classify avx2_shifts -> cpu_aot_avx2_shifts in tools/form_coverage.py (+9 AVX2 mnemonics, raising AVX2 from 0/28 to 9/28 in docs/coverage/x64_zen2.tsv) and document the test in docs/experiments.md.

Verification

Tested on Windows 11 (VS 2022 Build Tools 17.14, MSVC 19.44, ML64 14.44, Windows SDK 10.0.26100, Python 3.12):

  • Before the change, cmake --build --preset dev --target cpu_avx2_shifts_lifted failed with:
    ValueError: unsupported query operation at 0x1000: vpbroadcastb ymm0,byte ptr [rcx]
    
  • After the change:
    • cmake --build --preset dev --target cpu_avx2_shifts_lifted succeeded.
    • ctest --preset dev --no-tests=error -R "^(cpu_aot_avx2_shifts|cpu_aot_bmi1|cpu_aot_shift_count|cpu_aot_version_query|cpu_aot_address_size)$" -V: 5/5 passed (12 AVX2 broadcast and variable-shift vector cases matched native and translated DLLs).
    • python -B tests/form_coverage_check.py: form coverage checks passed.

Evidence and follow-up

  • Adds nine AVX2 mnemonics (vpbroadcastb, vpbroadcastw, vpbroadcastd, vpbroadcastq, vpsllvd, vpsrlvd, vpsravd, vpsllvq, vpsrlvq) to docs/coverage/x64_zen2.tsv coverage.
  • All binaries are source-built via MASM and CMake; no game files or external dumps are included.

Checklist

  • Focused, single-purpose change
  • Source-built test that fails without the change and passes with it
  • Verified locally on Windows
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6535342016 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/339). Originally posted by [Vonsek](https://github.com/Vonsek) on 2026-11-21T22:41:05Z. --- ## Change - Lower 128-bit and 256-bit AVX2 integer broadcasts (`vpbroadcastb`, `vpbroadcastw`, `vpbroadcastd`, `vpbroadcastq`) and variable per-lane vector shifts (`vpsllvd`, `vpsrlvd`, `vpsravd`, `vpsllvq`, `vpsrlvq`) in `tools/ffx_version_query.py`, zeroing the upper 128 bits on 128-bit VEX destinations and clamping out-of-range shift counts to match hardware. - Add a source-built MASM fixture (`tests/cpu/avx2_shifts.asm`, `tests/cpu/avx2_shifts.def`) and differential test (`tests/cpu/avx2_shifts.py`) wired into `CMakeLists.txt` as `cpu_aot_avx2_shifts` with a `host_has_avx2()` check (`OSXSAVE`, `XCR0` YMM state, and CPUID leaf 7 EBX bit 5 `AVX2`) and `SKIP_RETURN_CODE 77` on unsupported hosts. - Verify all 10 exports (256-bit memory broadcasts, 128-bit register broadcast with upper-lane zeroing, and 256-bit 32/64-bit variable shifts including counts >= 32/64) against both the native DLL and a reference model. - Classify `avx2_shifts` -> `cpu_aot_avx2_shifts` in `tools/form_coverage.py` (+9 AVX2 mnemonics, raising AVX2 from 0/28 to 9/28 in `docs/coverage/x64_zen2.tsv`) and document the test in `docs/experiments.md`. ## Verification Tested on Windows 11 (VS 2022 Build Tools 17.14, MSVC 19.44, ML64 14.44, Windows SDK 10.0.26100, Python 3.12): - Before the change, `cmake --build --preset dev --target cpu_avx2_shifts_lifted` failed with: ```text ValueError: unsupported query operation at 0x1000: vpbroadcastb ymm0,byte ptr [rcx] ``` - After the change: - `cmake --build --preset dev --target cpu_avx2_shifts_lifted` succeeded. - `ctest --preset dev --no-tests=error -R "^(cpu_aot_avx2_shifts|cpu_aot_bmi1|cpu_aot_shift_count|cpu_aot_version_query|cpu_aot_address_size)$" -V`: 5/5 passed (`12 AVX2 broadcast and variable-shift vector cases matched native and translated DLLs`). - `python -B tests/form_coverage_check.py`: `form coverage checks passed`. ## Evidence and follow-up - Adds nine AVX2 mnemonics (`vpbroadcastb`, `vpbroadcastw`, `vpbroadcastd`, `vpbroadcastq`, `vpsllvd`, `vpsrlvd`, `vpsravd`, `vpsllvq`, `vpsrlvq`) to `docs/coverage/x64_zen2.tsv` coverage. - All binaries are source-built via MASM and CMake; no game files or external dumps are included. ## Checklist - [x] Focused, single-purpose change - [x] Source-built test that fails without the change and passes with it - [x] Verified locally on Windows
upstream-sync closed this pull request 2026-11-24 06:42:30 +00:00
chatgpt-codex-connector[bot commented 2026-11-24 06:42:31 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-21T22:49:49Z.

Review: COMMENTED


💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8706760643

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6008697089 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/339#pullrequestreview-6008697089). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-21T22:49:49Z. **Review:** COMMENTED --- <!-- codex-review-receipt:v1 --> ### 💡 Codex Review Here are some automated review suggestions for this pull request. **Reviewed commit:** `8706760643` <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". If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". </details>
chatgpt-codex-connector[bot commented 2026-11-24 06:48:10 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-21T22:44:51Z.


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-21T22:49:52.100171Z 8706760 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:7070397455 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/339#issuecomment-7070397455). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-21T22:44:51Z. --- <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"87067606437144cca51b4eb760c6baae1467f83b","has_any_findings":true,"has_unresolved_findings":true,"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-21T22:49:52.100171Z">2026-11-21T22:49:52.100171Z</relative-time> | `8706760` | 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>
Vonsek commented 2026-11-24 06:48:10 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Vonsek on 2026-11-21T22:53:11Z.


Guarded the AVX2 broadcast and variable-shift handlers with i.encoding == EncodingKind.VEX so EVEX/broadcast/opmask forms are rejected, and added a synthetic EVEX {1to8} vpsllvd rejection check in tests/cpu/avx2_shifts.py.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7070495223 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/339#issuecomment-7070495223). Originally posted by [Vonsek](https://github.com/Vonsek) on 2026-11-21T22:53:11Z. --- Guarded the AVX2 broadcast and variable-shift handlers with `i.encoding == EncodingKind.VEX` so EVEX/broadcast/opmask forms are rejected, and added a synthetic EVEX `{1to8}` `vpsllvd` rejection check in `tests/cpu/avx2_shifts.py`.
chatgpt-codex-connector[bot commented 2026-11-24 06:50:37 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-21T22:49:50Z.

Code review location: tools/ffx_version_query.py; line 232

@@ -210,6 +213,36 @@ def write(expr):
             stmt = (f"{{ auto input = {src}; {dst} = {{}}; for (size_t n=0; n<16; n+=4) {{ "
                     f"auto v = load<uint32_t>(reinterpret_cast<word>(input.data()+n)) {operation}; "
                     f"memcpy({dst}.data()+n, &v, 4); }} }}")
+        elif has_wide and op in (M.VPBROADCASTB, M.VPBROADCASTW, M.VPBROADCASTD, M.VPBROADCASTQ):
+            elem_size = {M.VPBROADCASTB: 1, M.VPBROADCASTW: 2, M.VPBROADCASTD: 4, M.VPBROADCASTQ: 8}[op]
+            vec_size = RegisterExt.size(i.op0_register)
+            w = UINT[elem_size]
+            dst = wide_any[i.op0_register]
+            src_expr = (f"load<{w}>({address()})" if i.op1_kind == K.MEMORY
+                        else f"load<{w}>(reinterpret_cast<word>({wide_any[i.op1_register]}.data()))")
+            stmt = (f"{{ const {w} elem = {src_expr}; {dst} = {{}}; "
+                    f"for (size_t n = 0; n < {vec_size}; n += {elem_size}) memcpy({dst}.data() + n, &elem, {elem_size}); }}")
+        elif has_wide and op in (M.VPSLLVD, M.VPSRLVD, M.VPSRAVD, M.VPSLLVQ, M.VPSRLVQ):
+            elem_size = 8 if op in (M.VPSLLVQ, M.VPSRLVQ) else 4
+            bits, w = 8 * elem_size, UINT[elem_size]
+            vec_size = RegisterExt.size(i.op0_register)
+            dst, src1 = wide_any[i.op0_register], wide_any[i.op1_register]
+            if i.op2_kind == K.MEMORY:
+                cnt_init = f"const word caddr = {address()};"
+                cnt_expr = f"load<{w}>(caddr + n)"

P2 Badge Reject EVEX scalar-broadcast operands before lowering shifts

For an EVEX input such as vpsllvd ymm0, ymm1, dword ptr [rdx]{1to8}, iced reports the same mnemonic, so this new branch accepts it without checking the encoding or broadcast modifier. Hardware reuses one 32-bit count for all lanes, but caddr + n reads eight different counts, silently changing the result and potentially accessing memory beyond the four-byte operand. Reject non-VEX/broadcast encodings before emitting this loop, or implement their semantics, and cover the rejection with a synthetic-byte case.

Useful? React with 👍 / 👎.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4655396474 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/339#discussion_r4655396474). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-21T22:49:50Z. **Code review location:** `tools/ffx_version_query.py`; line 232 ~~~~diff @@ -210,6 +213,36 @@ def write(expr): stmt = (f"{{ auto input = {src}; {dst} = {{}}; for (size_t n=0; n<16; n+=4) {{ " f"auto v = load<uint32_t>(reinterpret_cast<word>(input.data()+n)) {operation}; " f"memcpy({dst}.data()+n, &v, 4); }} }}") + elif has_wide and op in (M.VPBROADCASTB, M.VPBROADCASTW, M.VPBROADCASTD, M.VPBROADCASTQ): + elem_size = {M.VPBROADCASTB: 1, M.VPBROADCASTW: 2, M.VPBROADCASTD: 4, M.VPBROADCASTQ: 8}[op] + vec_size = RegisterExt.size(i.op0_register) + w = UINT[elem_size] + dst = wide_any[i.op0_register] + src_expr = (f"load<{w}>({address()})" if i.op1_kind == K.MEMORY + else f"load<{w}>(reinterpret_cast<word>({wide_any[i.op1_register]}.data()))") + stmt = (f"{{ const {w} elem = {src_expr}; {dst} = {{}}; " + f"for (size_t n = 0; n < {vec_size}; n += {elem_size}) memcpy({dst}.data() + n, &elem, {elem_size}); }}") + elif has_wide and op in (M.VPSLLVD, M.VPSRLVD, M.VPSRAVD, M.VPSLLVQ, M.VPSRLVQ): + elem_size = 8 if op in (M.VPSLLVQ, M.VPSRLVQ) else 4 + bits, w = 8 * elem_size, UINT[elem_size] + vec_size = RegisterExt.size(i.op0_register) + dst, src1 = wide_any[i.op0_register], wide_any[i.op1_register] + if i.op2_kind == K.MEMORY: + cnt_init = f"const word caddr = {address()};" + cnt_expr = f"load<{w}>(caddr + n)" ~~~~ --- **<sub><sub>![P2 Badge](https://img.shields.io/badge/P2-yellow?style=flat)</sub></sub> Reject EVEX scalar-broadcast operands before lowering shifts** For an EVEX input such as `vpsllvd ymm0, ymm1, dword ptr [rdx]{1to8}`, iced reports the same mnemonic, so this new branch accepts it without checking the encoding or broadcast modifier. Hardware reuses one 32-bit count for all lanes, but `caddr + n` reads eight different counts, silently changing the result and potentially accessing memory beyond the four-byte operand. Reject non-VEX/broadcast encodings before emitting this loop, or implement their semantics, and cover the rejection with a synthetic-byte case. Useful? React with 👍 / 👎. <!-- codex-code-review-finding:v1 -->
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!334
No description provided.