cpu: lift vector data moves in the control-flow lifter #905

Closed
github-bot wants to merge 3 commits from gh-pr/905/head into gh-pr/905/base
Owner

@⁠copycraft on GitHub · 2026-11-23 16:37 UTC · original
copycraft:cpu/switch-simd-moves → main · +1177 −31 · 8 files · 3 commits · closed without merging

Change

The control-flow CPU lifter (tools/cpu_switch_aot.py) refused every function that named an XMM or YMM register. This PR lets it translate vector instructions that only move or zero data:

  • MOVAPS, MOVUPS, MOVAPD, MOVUPD, MOVDQA and MOVDQU, legacy and VEX (XMM and YMM). They move data between registers, the frame, guest memory and RIP-relative image data.
  • The XORPS/XORPD/PXOR zeroing idiom (both sources the same register), legacy and VEX, and VZEROUPPER.

Semantics:

  • Upper bytes. Each of XMM0–15 is a 32-byte value. A legacy write keeps bytes 16–31; a VEX write of an XMM register clears them.
  • Alignment. Aligned forms raise STATUS_ACCESS_VIOLATION before the access when a guest operand is misaligned. A RIP-relative operand is checked statically by its RVA. Frame accesses, through RSP or an RBP anchor, are proven aligned from their offset to entry RSP (8 mod 16). A 32-byte aligned frame access is refused, because the frame's alignment mod 32 is unknown.
  • Saved registers. The low 16 bytes of XMM6–15 join the saved-register proof, with 16-byte save slots. A slot may be written through RSP or through an RBP frame anchor (#889).
  • Definedness. The definedness pass tracks each register's defined width. After a call, XMM0–5 are undefined and only the low halves of XMM6–15 stay defined. After VZEROUPPER, only the low halves stay defined.
  • Still refused: EVEX, registers 16–31, vector arithmetic and shuffles, and transfers between vector and integer registers.

Why: in a local sample of 2,000 functions from an inspected Scarlett executable, vector register use was this lifter's most frequent first refusal. After this change, the most frequent first refusal in the same sample is taking the address of a stack slot (lea reg,[rsp+N]), which this PR does not change. Those counts come from a local survey script that isn't part of this PR. They only explain the motivation; the public fixture verifies the behavior.

Shared code: only the lifter's own output changes.

  • The generated-C++ prelude gains vec, vector_alignment, load_vector<N>, store_vector<N> and a RaiseException declaration.
  • prove_frame and the definedness pass now cover the vector registers as well as the general-purpose registers.

Overlap: this branch is rebased onto #889 (RBP frame anchors).

  • The frame alignment check uses #889's stack_base, so an anchored access is measured from the anchor.
  • The vector save-slot detection accepts RBP, as #889 does for general-purpose registers.
  • The anchored_vectors export and the bad_anchored_misaligned control pin both. Their RSP and RBP differ by 8 mod 16, so measuring from RSP would refuse the first and accept the second.

#895 also changes tools/cpu_switch_aot.py. A test merge with this branch conflicts in prove_frame: #895 adds stack outputs where this branch builds the saved-register status over the vector registers. Whichever lands second needs a small rebase there.

Verification

Branch head 6b82ce2, rebased onto 279a7fb (main after #889). Windows 11 build 26300, MSVC/ML64 19.44.35207, Python 3.12.14, dev preset (Ninja, Debug; the fixture also builds an /O2 translation).

cmake --build --preset dev --target cpu_switch_vectors_lifted cpu_switch_vectors_lifted_o2 cpu_switch_vectors_mutants cpu_guarded_call
ctest --preset dev --no-tests=error -R '^cpu_aot_switch_vectors$' -V

Passed:

  • Comparisons: 1,848 native, translated (/Od and /O2) and byte-reference comparisons matched, over 11 exports and source/destination offsets 0–31. Each comparison covers the whole destination with 32 guard bytes on each side, plus the return value.
  • Faults: 24 access violations matched the native code in both the exception and the memory left behind. They cover misaligned MOVAPS loads and stores, a 16-mod-32 VMOVDQA load, a fault after a completed store, and loads and stores that straddle or reach a protected page.
  • Unbound image: both translations raise the unbound-image exception before binding.
  • Mutants: all three /O2 mutants fail, as they must: a legacy write that clears bytes 16–31, an aligned move without its check, and a store without its last-byte write-back.

The check also reported 16 whole-PE refusal controls and 5 unsupported forms rejected.

On the rebased branch:

ctest --preset dev --no-tests=error -j 6 -R '^cpu_|docs_links|form_coverage|bidi_controls'
python -B tests/form_coverage_check.py
python -B tests/bidi_controls_check.py
python -B tests/docs_links_check.py

100% tests passed, 0 tests failed out of 162. This includes cpu_aot_switch_vectors and #889's cpu_aot_stack_frames. All three Python checks passed: form coverage, no bidirectional controls in 725 files, and 180 links in 13 Markdown files.

Full suite before the rebase (base fc89b9e), ctest --preset dev --no-tests=error -j 6, 452 tests:

  • 428 passed and 17 were skipped for capability reasons (SPIR-V tools not installed, wave64 or wave shuffle unsupported).
  • 7 failed, all _hardware GPU tests on an Intel Arc 140V (driver 32.0.101.9028, debug layer unavailable):
    • Six DXR tests (dxr_query, dxr_pipeline, dxr_update, dxr_update_pipeline, dxr_facing, dxr_facing_pipeline) hit the CTest timeout.
    • gpu_alias_lookup_writes_hardware reported array_atomics expected=64 actual=256,192,128,64, the InterlockedAdd adapter defect the docs already record for another adapter.
  • None of these tests builds or runs this lifter's output. They are an adapter result, reported separately from this change.

Evidence and follow-up

Fixture: original MIT source. tests/cpu/switch_vectors.asm is assembled with ML64, and the reference model in tests/cpu/switch_vectors_check.py is independent Python on source-built buffers. Its first line is ; coverage: record cpu_aot_switch_vectors, so the forms of the compared functions reach the coverage dashboard.

Negative controls:

  • 14 refusal exports, each translated alone and required to fail with its specific reason:
    • an undefined register at entry;
    • a volatile register read after a call;
    • bytes 16–31 read after a call, after VZEROUPPER and after a legacy write;
    • XMM6 not restored, and XMM6 stored while it still holds the caller's value;
    • saved XMM6 bytes read back as data;
    • misaligned 16-byte and 32-byte aligned frame accesses;
    • a misaligned aligned access through an RBP anchor, at an address that would look aligned if measured from RSP;
    • a misaligned aligned image operand;
    • a non-idiomatic XORPS;
    • a MOVQ from an integer register.
  • Two patched images, one with the XMM6 restore removed and one with the save removed.
  • Five byte forms: EVEX, an FS-prefixed move, EVEX register 16+, and legacy and VEX non-idiomatic XOR.
  • Three /O2 mutants.

Assumptions: no vector register is defined at entry, since floating-point arguments are not supported. Entry RSP is 8 mod 16. Not modeled: nontemporal stores, memory ordering against other threads, exception resumption, and the physical state of XMM6–15 inside the translation.

Docs: Vector data moves in the control-flow CPU lifter has the full contract and reproduction command. The README CPU sentence links to it.

Follow-up: taking the address of a stack slot and passing it on (lea rcx,[rsp+N] before a call) is now the most frequent first refusal in the local sample. This PR does not change it.

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.

This change was AI-assisted: it was written with Claude Code.

🤖 Generated with Claude Code

**@⁠copycraft** on GitHub · 2026-11-23 16:37 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/905) `copycraft:cpu/switch-simd-moves` → `main` · +1177 −31 · 8 files · 3 commits · closed without merging ## Change The control-flow CPU lifter (`tools/cpu_switch_aot.py`) refused every function that named an XMM or YMM register. This PR lets it translate vector instructions that only move or zero data: - MOVAPS, MOVUPS, MOVAPD, MOVUPD, MOVDQA and MOVDQU, legacy and VEX (XMM and YMM). They move data between registers, the frame, guest memory and RIP-relative image data. - The XORPS/XORPD/PXOR zeroing idiom (both sources the same register), legacy and VEX, and VZEROUPPER. Semantics: - **Upper bytes.** Each of XMM0–15 is a 32-byte value. A legacy write keeps bytes 16–31; a VEX write of an XMM register clears them. - **Alignment.** Aligned forms raise `STATUS_ACCESS_VIOLATION` before the access when a guest operand is misaligned. A RIP-relative operand is checked statically by its RVA. Frame accesses, through RSP or an RBP anchor, are proven aligned from their offset to entry RSP (8 mod 16). A 32-byte aligned frame access is refused, because the frame's alignment mod 32 is unknown. - **Saved registers.** The low 16 bytes of XMM6–15 join the saved-register proof, with 16-byte save slots. A slot may be written through RSP or through an RBP frame anchor (#889). - **Definedness.** The definedness pass tracks each register's defined width. After a call, XMM0–5 are undefined and only the low halves of XMM6–15 stay defined. After VZEROUPPER, only the low halves stay defined. - **Still refused:** EVEX, registers 16–31, vector arithmetic and shuffles, and transfers between vector and integer registers. Why: in a local sample of 2,000 functions from an inspected Scarlett executable, vector register use was this lifter's most frequent first refusal. After this change, the most frequent first refusal in the same sample is taking the address of a stack slot (`lea reg,[rsp+N]`), which this PR does not change. Those counts come from a local survey script that isn't part of this PR. They only explain the motivation; the public fixture verifies the behavior. **Shared code:** only the lifter's own output changes. - The generated-C++ prelude gains `vec`, `vector_alignment`, `load_vector<N>`, `store_vector<N>` and a `RaiseException` declaration. - `prove_frame` and the definedness pass now cover the vector registers as well as the general-purpose registers. **Overlap:** this branch is rebased onto #889 (RBP frame anchors). - The frame alignment check uses #889's `stack_base`, so an anchored access is measured from the anchor. - The vector save-slot detection accepts RBP, as #889 does for general-purpose registers. - The `anchored_vectors` export and the `bad_anchored_misaligned` control pin both. Their RSP and RBP differ by 8 mod 16, so measuring from RSP would refuse the first and accept the second. #895 also changes `tools/cpu_switch_aot.py`. A test merge with this branch conflicts in `prove_frame`: #895 adds stack outputs where this branch builds the saved-register status over the vector registers. Whichever lands second needs a small rebase there. ## Verification Branch head `6b82ce2`, rebased onto `279a7fb` (main after #889). Windows 11 build 26300, MSVC/ML64 19.44.35207, Python 3.12.14, `dev` preset (Ninja, Debug; the fixture also builds an `/O2` translation). ```powershell cmake --build --preset dev --target cpu_switch_vectors_lifted cpu_switch_vectors_lifted_o2 cpu_switch_vectors_mutants cpu_guarded_call ctest --preset dev --no-tests=error -R '^cpu_aot_switch_vectors$' -V ``` Passed: - **Comparisons:** 1,848 native, translated (`/Od` and `/O2`) and byte-reference comparisons matched, over 11 exports and source/destination offsets 0–31. Each comparison covers the whole destination with 32 guard bytes on each side, plus the return value. - **Faults:** 24 access violations matched the native code in both the exception and the memory left behind. They cover misaligned MOVAPS loads and stores, a 16-mod-32 VMOVDQA load, a fault after a completed store, and loads and stores that straddle or reach a protected page. - **Unbound image:** both translations raise the unbound-image exception before binding. - **Mutants:** all three `/O2` mutants fail, as they must: a legacy write that clears bytes 16–31, an aligned move without its check, and a store without its last-byte write-back. The check also reported `16 whole-PE refusal controls and 5 unsupported forms rejected`. On the rebased branch: ```powershell ctest --preset dev --no-tests=error -j 6 -R '^cpu_|docs_links|form_coverage|bidi_controls' python -B tests/form_coverage_check.py python -B tests/bidi_controls_check.py python -B tests/docs_links_check.py ``` `100% tests passed, 0 tests failed out of 162`. This includes `cpu_aot_switch_vectors` and #889's `cpu_aot_stack_frames`. All three Python checks passed: form coverage, no bidirectional controls in 725 files, and 180 links in 13 Markdown files. Full suite before the rebase (base `fc89b9e`), `ctest --preset dev --no-tests=error -j 6`, 452 tests: - 428 passed and 17 were skipped for capability reasons (SPIR-V tools not installed, wave64 or wave shuffle unsupported). - 7 failed, all `_hardware` GPU tests on an Intel Arc 140V (driver 32.0.101.9028, debug layer unavailable): - Six DXR tests (`dxr_query`, `dxr_pipeline`, `dxr_update`, `dxr_update_pipeline`, `dxr_facing`, `dxr_facing_pipeline`) hit the CTest timeout. - `gpu_alias_lookup_writes_hardware` reported `array_atomics expected=64 actual=256,192,128,64`, the InterlockedAdd adapter defect the docs already record for another adapter. - None of these tests builds or runs this lifter's output. They are an adapter result, reported separately from this change. ## Evidence and follow-up **Fixture:** original MIT source. `tests/cpu/switch_vectors.asm` is assembled with ML64, and the reference model in `tests/cpu/switch_vectors_check.py` is independent Python on source-built buffers. Its first line is `; coverage: record cpu_aot_switch_vectors`, so the forms of the compared functions reach the coverage dashboard. **Negative controls:** - 14 refusal exports, each translated alone and required to fail with its specific reason: - an undefined register at entry; - a volatile register read after a call; - bytes 16–31 read after a call, after VZEROUPPER and after a legacy write; - XMM6 not restored, and XMM6 stored while it still holds the caller's value; - saved XMM6 bytes read back as data; - misaligned 16-byte and 32-byte aligned frame accesses; - a misaligned aligned access through an RBP anchor, at an address that would look aligned if measured from RSP; - a misaligned aligned image operand; - a non-idiomatic XORPS; - a MOVQ from an integer register. - Two patched images, one with the XMM6 restore removed and one with the save removed. - Five byte forms: EVEX, an FS-prefixed move, EVEX register 16+, and legacy and VEX non-idiomatic XOR. - Three `/O2` mutants. **Assumptions:** no vector register is defined at entry, since floating-point arguments are not supported. Entry RSP is 8 mod 16. **Not modeled:** nontemporal stores, memory ordering against other threads, exception resumption, and the physical state of XMM6–15 inside the translation. **Docs:** [Vector data moves in the control-flow CPU lifter](docs/experiments.md#vector-data-moves-in-the-control-flow-cpu-lifter) has the full contract and reproduction command. The README CPU sentence links to it. **Follow-up:** taking the address of a stack slot and passing it on (`lea rcx,[rsp+N]` before a call) is now the most frequent first refusal in the local sample. This PR does not change it. ## 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. This change was AI-assisted: it was written with Claude Code. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
The control-flow lifter refused every function that named an XMM or YMM
register. It now tracks XMM0-15/YMM0-15 as 256-bit values and lowers the
data-movement subset: MOVAPS/MOVUPS/MOVAPD/MOVUPD/MOVDQA/MOVDQU and their
VEX forms between registers, the frame and guest memory, the XORPS/XORPD/
PXOR zero idioms and VZEROUPPER.

- Legacy SSE writes keep bits 128-255; VEX.128 writes zero them.
- Aligned forms raise the same access violation on a misaligned guest
  address. Frame accesses, through RSP or an RBP anchor, are instead
  proven aligned from their offset to entry RSP.
- XMM6-15 join the saved-register proof with 16-byte save slots, saved
  through RSP or an RBP anchor. Their upper halves are volatile across
  calls, as in the Windows x64 ABI.
- The definedness pass tracks the defined width of each vector register,
  so reading an upper half after a call or a legacy write is refused.
- EVEX, prefixed, non-zeroing XOR and integer-transfer forms stay refused.

tests/cpu/switch_vectors.asm adds 12 accepted exports, including an
RBP-anchored frame whose RSP and RBP differ by 8 mod 16, and 14 refusal
controls. They are compared against the native code and a byte reference
model at six buffer offsets, with guarded faults and three mutant
translations.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Review of #905: store_vector read and wrote back the destination's last
byte before the store. Against a PAGE_NOACCESS page that reported a read
access violation where the guest store reports a write, and it added an
observable write before the guest store.

store_vector is now a single unaligned store of the guest's width
(_mm_storeu_si128 or _mm256_storeu_si256), with no read of the
destination and no earlier write.

guarded_call_detail also returns the exception's access type and
inaccessible address. The vector check now compares both with the native
fault, alongside the code and the memory left behind, and adds 16- and
32-byte stores that straddle a protected page at its start and its end,
in both the /Od and /O2 translations.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 22:27:18 +00:00
github-bot closed this pull request 2026-11-26 22:27:18 +00:00
Author
Owner

@⁠copycraft on GitHub · 2026-11-23 16:38 UTC · original

this was vibecoded... but it did follow all the rules. (this is the disclosure)

**@⁠copycraft** on GitHub · 2026-11-23 16:38 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/905#issuecomment-7107452957) this *was* vibecoded... but it did follow all the rules. (this is the disclosure)
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-23 16:49 UTC · original

Please keep any locally obtained executable or other game files private. Please do not send or upload them, including via a private message. For #905 review, a source-built fixture with a commit, exact command and the first differing output or diagnostic is enough. Thanks for offering to help verify the changes.

**@⁠R0Y00** on GitHub · 2026-11-23 16:49 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/905#issuecomment-7107733990) Please keep any locally obtained executable or other game files private. Please do not send or upload them, including via a private message. For #905 review, a source-built fixture with a commit, exact command and the first differing output or diagnostic is enough. Thanks for offering to help verify the changes.
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-23 16:54 UTC · original

The vector-store lowering introduces a volatile read and write-back of the last destination byte before the actual store. A native vector store does not read its destination. On a PAGE_NOACCESS second page, that extra read can report a read AV where the guest instruction reports a write AV. It also adds an observable write before the guest store. Please remove the write-back probe, preserve the single memory operation and its width/order, or explicitly refuse cases where that behavior cannot be established. Extend the guarded native/lifted comparison to check exception access type and target address, with a boundary-crossing write case in both builds. Please coordinate the shared Frame changes with #895 when refreshing.

**@⁠R0Y00** on GitHub · 2026-11-23 16:54 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/905#issuecomment-7107870529) The vector-store lowering introduces a volatile read and write-back of the last destination byte before the actual store. A native vector store does not read its destination. On a PAGE_NOACCESS second page, that extra read can report a read AV where the guest instruction reports a write AV. It also adds an observable write before the guest store. Please remove the write-back probe, preserve the single memory operation and its width/order, or explicitly refuse cases where that behavior cannot be established. Extend the guarded native/lifted comparison to check exception access type and target address, with a boundary-crossing write case in both builds. Please coordinate the shared Frame changes with #895 when refreshing.
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-23 17:26 UTC · original

I integrated the single-width store and fault details from d31c199 with #895 in eeb5738. The shared frame analysis keeps explicit import output bytes and saved vector-register state. The AVX capability check runs before native vector comparisons, so a host lacking AVX can report a capability skip. Both Windows presets passed the vector, stack frame, stack output and archive regressions (4/4 each). Fresh CI is running.

**@⁠R0Y00** on GitHub · 2026-11-23 17:26 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/905#issuecomment-7108662390) I integrated the single-width store and fault details from d31c199 with #895 in eeb5738. The shared frame analysis keeps explicit import output bytes and saved vector-register state. The AVX capability check runs before native vector comparisons, so a host lacking AVX can report a capability skip. Both Windows presets passed the vector, stack frame, stack output and archive regressions (4/4 each). Fresh CI is running.
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-23 17:37 UTC · original

I saw this was closed after the integration tests passed. Is there a successor PR or a reason we should leave the vector move slice out? If you are superseding it, please link the new PR here so its review can reuse the cross-page write-fault and saved-register checks.

**@⁠R0Y00** on GitHub · 2026-11-23 17:37 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/905#issuecomment-7108943647) I saw this was closed after the integration tests passed. Is there a successor PR or a reason we should leave the vector move slice out? If you are superseding it, please link the new PR here so its review can reuse the cross-page write-fault and saved-register checks.
Author
Owner

@⁠copycraft on GitHub · 2026-11-23 17:50 UTC · original

You can use the thing if you want.. i just feel like this isnt ready, and i personally thing i shouldhave started with a smaller change, just to get in the flow, and also like id like to understand the repo more before vibecoding some bs.

**@⁠copycraft** on GitHub · 2026-11-23 17:50 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/905#issuecomment-7109253241) You can use the thing if you want.. i just feel like this isnt ready, and i personally thing i shouldhave started with a smaller change, just to get in the flow, and also like id like to understand the repo more before vibecoding some bs.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.