cpu: lower forward MOVS and STOS string instructions with REP #364

Closed
denn0x wants to merge 1 commit from upstream-pr/370 into main
denn0x commented 2026-11-21 23:38:16 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-21T23:38:16Z.


Change

This advances integer-lifter coverage for CPU code: compilers emit rep movs and rep stos for inline memcpy and memset and for struct copies. It touches only the shared lower dispatch in tools/ffx_version_query.py. The new branch keys on MEMORY_ESRDI operands, so it cannot catch the SSE movsd. No open PR claims string instructions (#326 is movsx, #364 is pmovsx/pmovzx), and no prerequisite PR is needed.

  • movsb, movsw, movsd, movsq, stosb, stosw, stosd, stosq, with and without rep:
    • Elements are copied one at a time, from low addresses to high, as the hardware does. An overlapping copy whose destination is one element above its source therefore repeats the first element. It does not act like memmove.
    • rep counts RCX down to zero. RSI and RDI advance by the element size. Flags are unchanged.
  • cld: lowers to nothing, because the Windows x64 ABI enters with DF clear and only forward strings are lowered.
  • Still rejected:
    • std (backward strings);
    • repne on a move or store;
    • 32-bit address-size forms (67 prefix, ESI/EDI);
    • FS/GS sources (the existing prefix check);
    • cmps, scas, lods;
    • the SSE scalar movsd, which shares the mnemonic but has register operands.

Tests

  • tests/cpu/string_ops.asm: ten MASM exports.
    • The rep forms of all eight instructions, plus one movsq and one stosd without rep.
    • Each takes (dst, src_or_value, count) and returns ((final RCX << 20) + RSI advance) << 20 + RDI advance, so the counter and both pointer advances are observed as well as memory.
  • tests/cpu/string_ops.py: compares the native DLL, the translated DLL and an independent sequential Python model, on the full 512-byte buffer and the return value.
    • Moves: counts 0–9 and 17, with disjoint, forward-overlapping (destination = source + element) and backward-overlapping ranges.
    • Stores: the same counts, with four fill values including all-ones and mixed high bytes.
    • That gives 385 cases. Each of the 36 forward-overlap cases is asserted to differ from a memmove of the same range, so a block-copy lowering cannot pass.
    • Rejections: repne movsb, rep movsb and stosb with a 67 prefix, FS movsb, std, repe cmpsb, lodsb, repne scasb and SSE movsd xmm0, xmm1, each with its exact diagnostic.
  • Coverage: tools/form_coverage.py maps string_ops to cpu_aot_string_ops, and x64 coverage goes from 55 of 948 forms on 9bdbb20 to 64 at bd00df2 (the eight string forms plus cld), measured with tools/form_coverage.py over the JUnit of ctest --preset dev -R '^cpu_'.

Verification

Windows 11 build 26200, MSVC 19.50.35720, Windows SDK 10.0.26100.0, Python 3.13.14 with iced-x86 1.21.0. Commit bd00df2 (rebased on 9bdbb20).

cmake --build --preset dev --target cpu_string_ops_lifted
ctest --preset dev --no-tests=error -R '^cpu_aot_string_ops$' -V
95: 385 string move/store cases (36 forward-overlap) matched native and translated DLLs in memory, RCX, RSI and RDI
95: REPNE, 32-bit addressing, FS, STD, CMPS, LODS, SCAS and SSE MOVSD were rejected
1/1 Test #95: cpu_aot_string_ops ...............   Passed    0.18 sec

Mutation check (local, reverted): lowering rep movs as a single memmove fails at the first forward-overlap case:

AssertionError: lifted rep_movsb(dst=129, src=0x80, count=2) = 0x200002, expected 0x200002; first differing byte 130

Full suite (ctest --preset dev --no-tests=error), rebased on 9bdbb20:

  • 144 passed, 2 capability skips (bvh_wave64_warp, wave_shuffle_warp), 1 failed. ctest prints 99% tests passed, 1 tests failed out of 147 because it counts the skips as not failed.
  • gpu_alias_lookup_writes_hardware fails on the RX 9070 XT (driver 32.0.32015.7029). It is a GPU atomics test that arrived with #332, and this change does not touch it. I reported it with measurements in #369.

Before the rebase, on bc24dfd, the suite gave 145 passed and the same two skips, with no failures. All 43 cpu_ tests pass at bd00df2. tests/form_coverage_check.py passes.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • It includes a negative control (the memmove discrimination, the rejections and the mutation check).
  • docs/experiments.md is updated.
  • No game files, dumps or binaries.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6535862580 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/370). Originally posted by [denn0x](https://github.com/denn0x) on 2026-11-21T23:38:16Z. --- ## Change This advances integer-lifter coverage for CPU code: compilers emit `rep movs` and `rep stos` for inline `memcpy` and `memset` and for struct copies. It touches only the shared `lower` dispatch in `tools/ffx_version_query.py`. The new branch keys on `MEMORY_ESRDI` operands, so it cannot catch the SSE `movsd`. No open PR claims string instructions (#326 is `movsx`, #364 is `pmovsx`/`pmovzx`), and no prerequisite PR is needed. - **`movsb`, `movsw`, `movsd`, `movsq`, `stosb`, `stosw`, `stosd`, `stosq`, with and without `rep`:** - Elements are copied one at a time, from low addresses to high, as the hardware does. An overlapping copy whose destination is one element above its source therefore repeats the first element. It does not act like `memmove`. - `rep` counts RCX down to zero. RSI and RDI advance by the element size. Flags are unchanged. - **`cld`:** lowers to nothing, because the Windows x64 ABI enters with DF clear and only forward strings are lowered. - **Still rejected:** - `std` (backward strings); - `repne` on a move or store; - 32-bit address-size forms (`67` prefix, ESI/EDI); - FS/GS sources (the existing prefix check); - `cmps`, `scas`, `lods`; - the SSE scalar `movsd`, which shares the mnemonic but has register operands. ## Tests - **`tests/cpu/string_ops.asm`:** ten MASM exports. - The `rep` forms of all eight instructions, plus one `movsq` and one `stosd` without `rep`. - Each takes `(dst, src_or_value, count)` and returns `((final RCX << 20) + RSI advance) << 20 + RDI advance`, so the counter and both pointer advances are observed as well as memory. - **`tests/cpu/string_ops.py`:** compares the native DLL, the translated DLL and an independent sequential Python model, on the full 512-byte buffer and the return value. - Moves: counts 0–9 and 17, with disjoint, forward-overlapping (destination = source + element) and backward-overlapping ranges. - Stores: the same counts, with four fill values including all-ones and mixed high bytes. - That gives 385 cases. Each of the 36 forward-overlap cases is asserted to differ from a `memmove` of the same range, so a block-copy lowering cannot pass. - Rejections: `repne movsb`, `rep movsb` and `stosb` with a `67` prefix, FS `movsb`, `std`, `repe cmpsb`, `lodsb`, `repne scasb` and SSE `movsd xmm0, xmm1`, each with its exact diagnostic. - **Coverage:** `tools/form_coverage.py` maps `string_ops` to `cpu_aot_string_ops`, and x64 coverage goes from 55 of 948 forms on `9bdbb20` to 64 at `bd00df2` (the eight string forms plus `cld`), measured with `tools/form_coverage.py` over the JUnit of `ctest --preset dev -R '^cpu_'`. ## Verification Windows 11 build 26200, MSVC 19.50.35720, Windows SDK 10.0.26100.0, Python 3.13.14 with iced-x86 1.21.0. Commit `bd00df2` (rebased on `9bdbb20`). ```powershell cmake --build --preset dev --target cpu_string_ops_lifted ctest --preset dev --no-tests=error -R '^cpu_aot_string_ops$' -V ``` ```text 95: 385 string move/store cases (36 forward-overlap) matched native and translated DLLs in memory, RCX, RSI and RDI 95: REPNE, 32-bit addressing, FS, STD, CMPS, LODS, SCAS and SSE MOVSD were rejected 1/1 Test #95: cpu_aot_string_ops ............... Passed 0.18 sec ``` **Mutation check (local, reverted):** lowering `rep movs` as a single `memmove` fails at the first forward-overlap case: ```text AssertionError: lifted rep_movsb(dst=129, src=0x80, count=2) = 0x200002, expected 0x200002; first differing byte 130 ``` **Full suite** (`ctest --preset dev --no-tests=error`), rebased on `9bdbb20`: - 144 passed, 2 capability skips (`bvh_wave64_warp`, `wave_shuffle_warp`), 1 failed. ctest prints `99% tests passed, 1 tests failed out of 147` because it counts the skips as not failed. - `gpu_alias_lookup_writes_hardware` fails on the RX 9070 XT (driver 32.0.32015.7029). It is a GPU atomics test that arrived with #332, and this change does not touch it. I reported it with measurements in #369. Before the rebase, on `bc24dfd`, the suite gave 145 passed and the same two skips, with no failures. All 43 `cpu_` tests pass at `bd00df2`. `tests/form_coverage_check.py` passes. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] It includes a negative control (the memmove discrimination, the rejections and the mutation check). - [x] `docs/experiments.md` is updated. - [x] No game files, dumps or binaries. AI-assisted (Claude Code).
upstream-sync closed this pull request 2026-11-24 06:42:40 +00:00
denn0x commented 2026-11-24 06:48:12 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-21T23:42:24Z.


Closing as a duplicate of #368, which lowers the same forward movs/stos forms and also covers lods, scas, cmps and std. Sorry for the noise.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7071050812 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/370#issuecomment-7071050812). Originally posted by [denn0x](https://github.com/denn0x) on 2026-11-21T23:42:24Z. --- Closing as a duplicate of #368, which lowers the same forward `movs`/`stos` forms and also covers `lods`, `scas`, `cmps` and `std`. Sorry for the noise.

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