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

Closed
copycraft wants to merge 3 commits from upstream-pr/905 into main
copycraft commented 2026-11-23 16:37:50 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by copycraft on 2026-11-23T16:37:50Z.


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

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6564034330 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/905). Originally posted by [copycraft](https://github.com/copycraft) on 2026-11-23T16:37:50Z. --- ## 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)
upstream-sync closed this pull request 2026-11-24 06:45:20 +00:00
copycraft commented 2026-11-24 06:49:18 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by copycraft on 2026-11-23T16:38:41Z.


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

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7107452957 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/905#issuecomment-7107452957). Originally posted by [copycraft](https://github.com/copycraft) on 2026-11-23T16:38:41Z. --- this *was* vibecoded... but it did follow all the rules. (this is the disclosure)
R0Y00 commented 2026-11-24 06:49:19 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T16:49:44Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7107733990 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/905#issuecomment-7107733990). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T16:49:44Z. --- 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 commented 2026-11-24 06:49:20 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T16:54:58Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7107870529 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/905#issuecomment-7107870529). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T16:54:58Z. --- 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 commented 2026-11-24 06:49:23 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T17:26:30Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7108662390 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/905#issuecomment-7108662390). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T17:26:30Z. --- 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 commented 2026-11-24 06:49:25 +00:00 (Migrated from github.com)

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


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7108943647 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/905#issuecomment-7108943647). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T17:37:56Z. --- 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.
copycraft commented 2026-11-24 06:49:25 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by copycraft on 2026-11-23T17:50:14Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7109253241 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/905#issuecomment-7109253241). Originally posted by [copycraft](https://github.com/copycraft) on 2026-11-23T17:50:14Z. --- 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

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!898
No description provided.