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

Merged
grahamster2 merged 1 commit from upstream-pr/424 into main 2026-11-22 01:55:27 +00:00
grahamster2 commented 2026-11-22 01:28:43 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by grahamster2 on 2026-11-22T01:28:43Z.


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).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6536827807 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/424). Originally posted by [grahamster2](https://github.com/grahamster2) on 2026-11-22T01:28:43Z. --- ## 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)._
upstream-sync closed this pull request 2026-11-24 06:42:58 +00:00
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!418
No description provided.