cpu: keep memory reads whose value is unused in the chained-range translator #668

Merged
grahamster2 merged 2 commits from upstream-pr/674 into main 2026-11-22 21:13:41 +00:00
grahamster2 commented 2026-11-22 19:06:01 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by grahamster2 on 2026-11-22T19:06:01Z.


Problem

#437 made cpu_switch_aot keep every executed guest memory read. It follows the rule set on that PR: an executed read keeps its fault behaviour even when its value is unused, and each source memory operand is evaluated once, at its own width, in guest order. A private frame read may stay optimizable when it is provably valid.

The survey on #437 found the same bug in cpu_chained_aot. Its MOV r64, [reg + disp] load becomes const word load_X = read_word(addr);, and read_word is a memcpy. When nothing uses the loaded value, /O2 drops the read. A null or unmapped operand then returns status 1 where the original raises an access violation.

Change

tools/cpu_chained_aot.py:

  • Each guest MOV load is emitted as const word load_X = load_always<word>(addr);.
    • load_always is #437's single volatile access at the operand's width (*reinterpret_cast<const volatile T*>(address)).
    • The load is declared where the guest instruction executes, so reads stay once per operand and in guest order. This was already true, and the translator emits no guest stores.
  • A load whose value nothing uses gets static_cast<void>(load_X); on the same line, as in #437's CMP/TEST case.
  • read_word stays only for the binding checks of --callback-offset and --ffx-destroy, where the table or allocator word is always compared. It is emitted only in those two modes, which removes the C4505 warning from plain translations.
  • No reads are emitted for the modelled stack slots ([rsp + n] home and frame slots), so there is no private frame read to exempt.

New fixture tests/cpu/chained_dead_read.{asm,def} (first line ; coverage: source cpu_aot_chained_dead_reads) and test tests/cpu/chained_dead_read_check.py, registered as cpu_aot_chained_dead_reads. The translator lifts one entry per call, so each export is translated separately: once by the preset's build, and once by a separate cl /O2 /W4 /LD command, as #437 does. The exports are:

  • read_dead: mov rax, [rcx], then the value is overwritten by mov rax, rdx.
  • disp_dead: mov r8, [rcx + 8], into a register nothing reads.
  • pair_dead: two loads, [rcx] and [rcx + 8], neither used.
  • read_skip: test rdx, rdx; jz around a dead load.
  • read_live: the control, which returns the value it reads.

The check calls the original and both translations through guarded_call (__try/GetExceptionCode()). Each call gets a fresh page that is followed by a PAGE_NOACCESS page, and fresh guard words around the translation's output word. It compares the exception code and the result.

Not covered: the instruction subset has no CMP/TEST on memory, no CMOVcc and no read-modify-write, so the fixture has none. Those shapes are in #437 and #666. The translation writes no memory, so this fixture uses no trace markers; ordering is checked by the single declaration per operand.

docs/experiments.md: a paragraph and the command under "Chained function ranges and early returns".

Evidence

Environment: Windows 11 Pro build 26200, VS Build Tools 2026 18.3 / MSVC 19.50.35725, ML64, Windows SDK 10.0.26100, Python 3.12. Commit 308e5da on main 2bb9ee0.

Before and after. I translated the fixture with main's tools/cpu_chained_aot.py and with this branch, and compiled each export with /Od and /O2. Each export was called with a null RCX under guarded_call:

export original main /Od main /O2 branch (Debug preset) branch /O2
read_dead (overwritten load) 0xc0000005 0xc0000005 returns 5 0xc0000005 0xc0000005
disp_dead ([rcx+8], unused) 0xc0000005 0xc0000005 returns 5 0xc0000005 0xc0000005
pair_dead (two unused loads) 0xc0000005 0xc0000005 returns 5 0xc0000005 0xc0000005
read_skip (y = 5, read taken) 0xc0000005 0xc0000005 returns 5 0xc0000005 0xc0000005
read_live (control) 0xc0000005 0xc0000005 0xc0000005 0xc0000005 0xc0000005

Run against main's translations, the new check fails at the first dead case: AssertionError: /O2 translation read_dead, null operand: code 0x0, 5; original code 0xc0000005, None. Its source control also fails on main, because the loads are read_word.

The new test on this branch. ctest --preset dev --no-tests=error -R '^cpu_aot_chained_dead_reads$' -V passes, with 5 source-control lines and 29 comparisons. Every export is tested with:

  • a readable operand;
  • a null operand, which faults;
  • reads ending exactly at the inaccessible page, which return;
  • the last read crossing one byte into that page, which faults.

Two further cases:

  • pair_dead with its second operand entirely in the inaccessible page faults.
  • read_skip with a null operand or an operand inside the inaccessible page returns normally when y = 0, and faults when y = 7.

The same test passes under --preset release.

Full suites, serial, with -E '^(conditional_import_paths|static_exe_intake|bundled_export_forwarders)$':

  • cmake --preset dev -DINTAKE_PYTHON=C:/Users/graha/src/Xbox2Windows/out/pe-env/Scripts/python.exe, cmake --build --preset dev, ctest --preset dev --no-tests=error -E ...: 100%, 290 of 290 passed, with 3 capability skips (bvh_wave64_hardware, bvh_wave64_warp, wave_shuffle_warp).
  • cmake --preset release -DINTAKE_PYTHON=..., cmake --build --preset release, ctest --preset release --no-tests=error -E ...: 100%, 290 of 290 passed, with the same 3 capability skips.
  • pyflakes on the changed Python files, tests/docs_links_check.py, tests/form_coverage_check.py: clean / pass.

Generated C++ change. I regenerated the seven existing cpu_chained_aot translations (cpu_chained, _jne, _zero, _imm, _frame, cpu_callback and cpu_destroy) with main and with this branch and diffed them.

  • In every function body, the only change is read_word( becoming load_always<word>( on the MOV loads: one load in each choose_value variant, two in cpu_callback and three in cpu_destroy.
  • The header gains the load_always template. It keeps read_word only for cpu_callback and cpu_destroy, whose binding checks still use it.
  • No existing translation gains static_cast<void>, because each of their loads is used.

cpu_aot_chained_ranges, cpu_aot_object_callback and cpu_aot_destroy_contract pass unchanged.

Evidence and follow-up

The fixture is source-built MASM in this PR (MIT), with no external data. I did not check whether the local FidelityFX loader translations contain a dead load. The shared lowering (ffx_version_query.lower) is #666.

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.

Refs #437

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6546476311 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/674). Originally posted by [grahamster2](https://github.com/grahamster2) on 2026-11-22T19:06:01Z. --- ## Problem #437 made `cpu_switch_aot` keep every executed guest memory read. It follows the rule set on that PR: an executed read keeps its fault behaviour even when its value is unused, and each source memory operand is evaluated once, at its own width, in guest order. A private frame read may stay optimizable when it is provably valid. The [survey on #437](https://github.com/R0Y00/Xbox2Windows/pull/437#issuecomment-7073467319) found the same bug in `cpu_chained_aot`. Its `MOV r64, [reg + disp]` load becomes `const word load_X = read_word(addr);`, and `read_word` is a `memcpy`. When nothing uses the loaded value, `/O2` drops the read. A null or unmapped operand then returns status 1 where the original raises an access violation. ## Change `tools/cpu_chained_aot.py`: - Each guest `MOV` load is emitted as `const word load_X = load_always<word>(addr);`. - `load_always` is #437's single `volatile` access at the operand's width (`*reinterpret_cast<const volatile T*>(address)`). - The load is declared where the guest instruction executes, so reads stay once per operand and in guest order. This was already true, and the translator emits no guest stores. - A load whose value nothing uses gets `static_cast<void>(load_X);` on the same line, as in #437's `CMP`/`TEST` case. - `read_word` stays only for the binding checks of `--callback-offset` and `--ffx-destroy`, where the table or allocator word is always compared. It is emitted only in those two modes, which removes the C4505 warning from plain translations. - No reads are emitted for the modelled stack slots (`[rsp + n]` home and frame slots), so there is no private frame read to exempt. New fixture `tests/cpu/chained_dead_read.{asm,def}` (first line `; coverage: source cpu_aot_chained_dead_reads`) and test `tests/cpu/chained_dead_read_check.py`, registered as `cpu_aot_chained_dead_reads`. The translator lifts one entry per call, so each export is translated separately: once by the preset's build, and once by a separate `cl /O2 /W4 /LD` command, as #437 does. The exports are: - `read_dead`: `mov rax, [rcx]`, then the value is overwritten by `mov rax, rdx`. - `disp_dead`: `mov r8, [rcx + 8]`, into a register nothing reads. - `pair_dead`: two loads, `[rcx]` and `[rcx + 8]`, neither used. - `read_skip`: `test rdx, rdx; jz` around a dead load. - `read_live`: the control, which returns the value it reads. The check calls the original and both translations through `guarded_call` (`__try`/`GetExceptionCode()`). Each call gets a fresh page that is followed by a `PAGE_NOACCESS` page, and fresh guard words around the translation's output word. It compares the exception code and the result. Not covered: the instruction subset has no `CMP`/`TEST` on memory, no `CMOVcc` and no read-modify-write, so the fixture has none. Those shapes are in #437 and #666. The translation writes no memory, so this fixture uses no trace markers; ordering is checked by the single declaration per operand. `docs/experiments.md`: a paragraph and the command under "Chained function ranges and early returns". ## Evidence Environment: Windows 11 Pro build 26200, VS Build Tools 2026 18.3 / MSVC 19.50.35725, ML64, Windows SDK 10.0.26100, Python 3.12. Commit 308e5da on main 2bb9ee0. **Before and after.** I translated the fixture with `main`'s `tools/cpu_chained_aot.py` and with this branch, and compiled each export with `/Od` and `/O2`. Each export was called with a null RCX under `guarded_call`: | export | original | main `/Od` | main `/O2` | branch (Debug preset) | branch `/O2` | |---|---|---|---|---|---| | `read_dead` (overwritten load) | `0xc0000005` | `0xc0000005` | **returns 5** | `0xc0000005` | `0xc0000005` | | `disp_dead` (`[rcx+8]`, unused) | `0xc0000005` | `0xc0000005` | **returns 5** | `0xc0000005` | `0xc0000005` | | `pair_dead` (two unused loads) | `0xc0000005` | `0xc0000005` | **returns 5** | `0xc0000005` | `0xc0000005` | | `read_skip` (y = 5, read taken) | `0xc0000005` | `0xc0000005` | **returns 5** | `0xc0000005` | `0xc0000005` | | `read_live` (control) | `0xc0000005` | `0xc0000005` | `0xc0000005` | `0xc0000005` | `0xc0000005` | Run against `main`'s translations, the new check fails at the first dead case: `AssertionError: /O2 translation read_dead, null operand: code 0x0, 5; original code 0xc0000005, None`. Its source control also fails on `main`, because the loads are `read_word`. **The new test on this branch.** `ctest --preset dev --no-tests=error -R '^cpu_aot_chained_dead_reads$' -V` passes, with 5 source-control lines and 29 comparisons. Every export is tested with: - a readable operand; - a null operand, which faults; - reads ending exactly at the inaccessible page, which return; - the last read crossing one byte into that page, which faults. Two further cases: - `pair_dead` with its second operand entirely in the inaccessible page faults. - `read_skip` with a null operand or an operand inside the inaccessible page returns normally when y = 0, and faults when y = 7. The same test passes under `--preset release`. **Full suites,** serial, with `-E '^(conditional_import_paths|static_exe_intake|bundled_export_forwarders)$'`: - `cmake --preset dev -DINTAKE_PYTHON=C:/Users/graha/src/Xbox2Windows/out/pe-env/Scripts/python.exe`, `cmake --build --preset dev`, `ctest --preset dev --no-tests=error -E ...`: 100%, 290 of 290 passed, with 3 capability skips (`bvh_wave64_hardware`, `bvh_wave64_warp`, `wave_shuffle_warp`). - `cmake --preset release -DINTAKE_PYTHON=...`, `cmake --build --preset release`, `ctest --preset release --no-tests=error -E ...`: 100%, 290 of 290 passed, with the same 3 capability skips. - `pyflakes` on the changed Python files, `tests/docs_links_check.py`, `tests/form_coverage_check.py`: clean / pass. **Generated C++ change.** I regenerated the seven existing `cpu_chained_aot` translations (`cpu_chained`, `_jne`, `_zero`, `_imm`, `_frame`, `cpu_callback` and `cpu_destroy`) with `main` and with this branch and diffed them. - In every function body, the only change is `read_word(` becoming `load_always<word>(` on the `MOV` loads: one load in each `choose_value` variant, two in `cpu_callback` and three in `cpu_destroy`. - The header gains the `load_always` template. It keeps `read_word` only for `cpu_callback` and `cpu_destroy`, whose binding checks still use it. - No existing translation gains `static_cast<void>`, because each of their loads is used. `cpu_aot_chained_ranges`, `cpu_aot_object_callback` and `cpu_aot_destroy_contract` pass unchanged. ## Evidence and follow-up The fixture is source-built MASM in this PR (MIT), with no external data. I did not check whether the local FidelityFX loader translations contain a dead load. The shared lowering (`ffx_version_query.lower`) is #666. ## 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. Refs #437 _AI-assisted (Claude Code)._
upstream-sync closed this pull request 2026-11-24 06:44:15 +00:00
legacy-code-archaeologist commented 2026-11-24 06:48:43 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-22T19:20:42Z.


Verified 308e5da on an Intel Core i7-6820HQ (Skylake), Windows 11 26100.9168, MSVC/ML64 19.52.37029, dev preset. It merges cleanly with main at ff7f68a. ctest --preset dev --no-tests=error -R "^cpu_" -j 4: 100% tests passed, 0 failed out of 119. This is the chained-range counterpart of #666, which I checked on the same host.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7084316081 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/674#issuecomment-7084316081). Originally posted by [legacy-code-archaeologist](https://github.com/legacy-code-archaeologist) on 2026-11-22T19:20:42Z. --- Verified `308e5da` on an Intel Core i7-6820HQ (Skylake), Windows 11 26100.9168, MSVC/ML64 19.52.37029, `dev` preset. It merges cleanly with `main` at `ff7f68a`. `ctest --preset dev --no-tests=error -R "^cpu_" -j 4`: 100% tests passed, 0 failed out of 119. This is the chained-range counterpart of #666, which I checked on the same host. AI-assisted (Claude Code).
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!668
No description provided.