cpu: treat a three-operand IMUL destination as written, not read #424

Manually merged
github merged 1 commit from gh-pr/424/head into gh-pr/424/base 2026-11-26 21:50:52 +00:00
Owner

@⁠grahamster2 on GitHub · 2026-11-22 01:28 UTC · original
grahamster2:cpu/imul3-dest-write → main · +37 −7 · 6 files · 1 commits · merged 2026-11-22 01:55 UTC

Problem

Lifter.statement in tools/cpu_switch_aot.py called note(*range(ins.op_count)) for IMUL, so a three-operand imul r, r/m, imm recorded its destination (operand 0) as a read. That form computes dest = src * imm and only writes the destination. The definedness pass therefore refused functions whose IMUL destination was not defined beforehand. The generated expression for the three-operand case already used only operands 1 and 2.

Change

  • The three-operand form notes operands 1 and 2 only. The two-operand form imul r, r/m still notes operands 0 and 1, because it reads its destination. The emitted C++ is unchanged.
  • tests/cpu/narrow.asm gains imul3_fresh: imul r10, rdx, -5, imul r9w, cx, 7FFFh (followed by movzx r9d, r9w, which reads only the word) and imul eax, ecx, 1010101h, with none of R10, R9 or RAX defined before. It is added to the cpu_narrow translation list and to narrow_check.py with a Python reference.
  • Control imul2_undefined: imul eax, ecx at entry must still be rejected with undefined register rax.
  • docs/experiments.md, "Switch dispatch through jump tables": one sentence on IMUL definedness and one on the new export and control.

Evidence

Environment: Windows 11 Pro build 26200, VS Build Tools 2026 18.3 / MSVC 19.50.35725, Windows SDK 10.0.26100, Python 3.12. Commit 7cf8221 on main e5e0079.

  • ctest --preset dev --no-tests=error -E '^(conditional_import_paths|static_exe_intake|bundled_export_forwarders)$': 144 of 144 passed, with 3 capability skips (bvh_wave64_hardware, bvh_wave64_warp, wave_shuffle_warp). docs_links passed, and so did tests/bidi_controls_check.py.
  • cpu_aot_narrow_returns: 4,568 inputs matched the reference on the defined RAX bytes, the guarded trace, RBX and RSI (249 of them for imul3_fresh). All 8 rejection controls were rejected, including imul2_undefined (undefined register rax at RVA 0x114a).
  • Before/after, on the same cpu_narrow.dll: main's translator refuses imul3_fresh with undefined register r10 at RVA 0x112e (the first IMUL). This branch translates it, and the translation matches native. Both refuse imul2_undefined with undefined register rax.
  • Generated C++ from main's translator and from this branch is byte-identical for cpu_switch_original, cpu_switch_swapped, cpu_switch_addr32, cpu_entry_order, the existing cpu_narrow export list and cpu_fourth_arg. The new exports are placed after the existing ones, so the existing RVAs are unchanged.

Found by: a Release /O2 corpus run while reviewing #420, where offscreen_check.exe RVA 0x1b6a0 stopped at imul edi, ecx, 1010101h.

Refs #420

AI-assisted (Claude Code).

**@⁠grahamster2** on GitHub · 2026-11-22 01:28 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/424) `grahamster2:cpu/imul3-dest-write` → `main` · +37 −7 · 6 files · 1 commits · merged 2026-11-22 01:55 UTC ## Problem `Lifter.statement` in `tools/cpu_switch_aot.py` called `note(*range(ins.op_count))` for `IMUL`, so a three-operand `imul r, r/m, imm` recorded its destination (operand 0) as a read. That form computes dest = src * imm and only writes the destination. The definedness pass therefore refused functions whose `IMUL` destination was not defined beforehand. The generated expression for the three-operand case already used only operands 1 and 2. ## Change - The three-operand form notes operands 1 and 2 only. The two-operand form `imul r, r/m` still notes operands 0 and 1, because it reads its destination. The emitted C++ is unchanged. - `tests/cpu/narrow.asm` gains `imul3_fresh`: `imul r10, rdx, -5`, `imul r9w, cx, 7FFFh` (followed by `movzx r9d, r9w`, which reads only the word) and `imul eax, ecx, 1010101h`, with none of R10, R9 or RAX defined before. It is added to the `cpu_narrow` translation list and to `narrow_check.py` with a Python reference. - Control `imul2_undefined`: `imul eax, ecx` at entry must still be rejected with `undefined register rax`. - `docs/experiments.md`, "Switch dispatch through jump tables": one sentence on IMUL definedness and one on the new export and control. ## Evidence Environment: Windows 11 Pro build 26200, VS Build Tools 2026 18.3 / MSVC 19.50.35725, Windows SDK 10.0.26100, Python 3.12. Commit 7cf8221 on main e5e0079. - `ctest --preset dev --no-tests=error -E '^(conditional_import_paths|static_exe_intake|bundled_export_forwarders)$'`: 144 of 144 passed, with 3 capability skips (`bvh_wave64_hardware`, `bvh_wave64_warp`, `wave_shuffle_warp`). `docs_links` passed, and so did `tests/bidi_controls_check.py`. - `cpu_aot_narrow_returns`: 4,568 inputs matched the reference on the defined RAX bytes, the guarded trace, RBX and RSI (249 of them for `imul3_fresh`). All 8 rejection controls were rejected, including `imul2_undefined` (`undefined register rax at RVA 0x114a`). - Before/after, on the same `cpu_narrow.dll`: main's translator refuses `imul3_fresh` with `undefined register r10 at RVA 0x112e` (the first `IMUL`). This branch translates it, and the translation matches native. Both refuse `imul2_undefined` with `undefined register rax`. - Generated C++ from main's translator and from this branch is byte-identical for `cpu_switch_original`, `cpu_switch_swapped`, `cpu_switch_addr32`, `cpu_entry_order`, the existing `cpu_narrow` export list and `cpu_fourth_arg`. The new exports are placed after the existing ones, so the existing RVAs are unchanged. Found by: a Release /O2 corpus run while reviewing #420, where `offscreen_check.exe` RVA 0x1b6a0 stopped at `imul edi, ecx, 1010101h`. Refs #420 _AI-assisted (Claude Code)._
The jump-table translator recorded every IMUL operand as read, so
imul r, r/m, imm required its destination to be defined beforehand and
the definedness pass refused functions where it was not. The
three-operand form computes dest = src * imm and only writes dest; the
two-operand form still reads it.

narrow.asm gains imul3_fresh (64-, 16- and 32-bit three-operand IMUL
into R10, R9W and EAX, none defined before) and the control
imul2_undefined (imul eax, ecx at entry), which stays rejected.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 21:50:51 +00:00
github manually merged commit 7cf8221232 into gh-pr/424/base 2026-11-26 21:50:52 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.