cpu: recompile a caller that repeats one direct call #176

Merged
denn0x merged 1 commit from upstream-pr/177 into main 2026-11-21 18:12:40 +00:00
denn0x commented 2026-11-21 18:00:48 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-21T18:00:48Z.


Change

cpu_call_aot accepted exactly one call per caller, and the direct-call section listed "one direct call" as a coverage limit. It now accepts repeated direct calls that share one target, and lifts that callee once. A second distinct target is rejected, and import calls stay limited to one per caller.

Only RBX and RSI survive a call in this profile, so a caller that calls twice must keep its output pointer in one of them. The translator now decodes 64-bit moves between R8 and the profile registers. It handles both the opcode 8B form that ML64 emits (49 8B F0, 4C 8B C6) and the opcode 89 form (4C 89 C6, 49 89 F0). It tracks which registers still hold the entry R8 value, so R8 can only be reloaded from such a copy. Every [r8+disp] store therefore still goes to the caller's trace. After a call, R8 is undefined until it is reloaded.

The new source-built fixture tests/cpu/repeat_call.asm has a caller call_twice that parks R8 in RSI. Its second call takes first + x and x, so it depends on the first result. The callee has the same body as mix_args in direct.asm. direct.asm and its check are unchanged.

Verification

Commit e27cfe9 on ecb9160. Windows 11 build 26200, MSVC 19.50.35720 with ML64, Windows SDK 10.0.26100.0, Python 3.13.

ctest --preset dev --no-tests=error -R '^cpu_aot_repeated_call$' -V
249 input pairs each for caller and callee: arguments, guarded writes, RBX, RSI and returns match; every altered callee changes the caller's return and trace; the register clobber is detected
both call sites and trace-pointer moves found; opcode 89 moves translate identically; 4 redirected calls, the omitted R8 reload and an R8 reload from RBX are rejected
a direct call and an import call are rejected in either order
  • Differential: the caller and callee are compared with the original DLL and with independent arithmetic. The comparison covers the return, the trace written by the second call, the guards and the RBX/RSI sentinels, for the same 249 pairs as cpu_aot_direct_call.
  • Altered callee: a callee that returns one more changes the caller's return (by exactly 2) and its trace on every pair. An earlier draft combined the first result with XOR. This control caught that the altered return then equals the original on some inputs (for example x=1, y=0), so the fixture uses ADD.
  • Linked calls: the original and both lifted DLLs each contain two rel32 calls to their callee.
  • Encodings: patching both R8 moves to their opcode 89 encodings leaves the generated C++ unchanged.
  • Rejected by translation:
    • a copy assembled without mov r8, rsi ("undefined register");
    • the first or the second call redirected into the callee interior ("call to a second target");
    • both calls redirected there ("no unique function interval");
    • both calls redirected to the caller ("overlapping call target");
    • mov r8, rsi patched to mov r8, rbx, where RBX holds an input word ("R8 reloaded without the output pointer"). Without the pointer tracking, this copy translated and its stores would have used an integer as the trace address.
  • Mixed calls: hand-built callers with a direct call and an import call are rejected in both orders. Direct then import gives "unexpected additional call"; import then direct gives "call to a second target".
  • Before the change: the translator on main rejects call_twice at mov rsi, r8 with "unsupported or incomplete instruction".
  • Existing outputs: the generated cpu_call_original, cpu_call_altered, cpu_import_original and cpu_ordinal_original sources are byte-identical before and after the change.
  • Full suite: ctest --preset dev --no-tests=error on an RX 9070 XT (driver 32.0.32015.7029) gives 103 tests: 100 passed, 2 capability skips (bvh_wave64_warp, wave_shuffle_warp) and 1 failure. The failure is pixel_aot_hardware, which also fails on main at 583fee5 without this change, with the same first mismatch. The failing case is the comparison-predicate draw added in #158: v_cmp_nlg_f32 with a NaN operand returns 0 on that adapter, while pixel_aot_warp passes. This change touches no shader code.

Evidence and follow-up

The fixture is original MASM written for this test (MIT). The README CPU row and the direct-call section of docs/experiments.md are updated with the command. The fixture exercises the R8 moves only through RSI. Two calls to different callees would need the translator to lift and name more than one callee, and remain open. Follows the direct-call work in #22.

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.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6532465352 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/177). Originally posted by [denn0x](https://github.com/denn0x) on 2026-11-21T18:00:48Z. --- ## Change `cpu_call_aot` accepted exactly one call per caller, and the direct-call section listed "one direct call" as a coverage limit. It now accepts repeated direct calls that share one target, and lifts that callee once. A second distinct target is rejected, and import calls stay limited to one per caller. Only RBX and RSI survive a call in this profile, so a caller that calls twice must keep its output pointer in one of them. The translator now decodes 64-bit moves between R8 and the profile registers. It handles both the opcode 8B form that ML64 emits (`49 8B F0`, `4C 8B C6`) and the opcode 89 form (`4C 89 C6`, `49 89 F0`). It tracks which registers still hold the entry R8 value, so R8 can only be reloaded from such a copy. Every `[r8+disp]` store therefore still goes to the caller's trace. After a call, R8 is undefined until it is reloaded. The new source-built fixture `tests/cpu/repeat_call.asm` has a caller `call_twice` that parks R8 in RSI. Its second call takes `first + x` and `x`, so it depends on the first result. The callee has the same body as `mix_args` in `direct.asm`. `direct.asm` and its check are unchanged. ## Verification Commit `e27cfe9` on `ecb9160`. Windows 11 build 26200, MSVC 19.50.35720 with ML64, Windows SDK 10.0.26100.0, Python 3.13. ```powershell ctest --preset dev --no-tests=error -R '^cpu_aot_repeated_call$' -V ``` ```text 249 input pairs each for caller and callee: arguments, guarded writes, RBX, RSI and returns match; every altered callee changes the caller's return and trace; the register clobber is detected both call sites and trace-pointer moves found; opcode 89 moves translate identically; 4 redirected calls, the omitted R8 reload and an R8 reload from RBX are rejected a direct call and an import call are rejected in either order ``` - **Differential:** the caller and callee are compared with the original DLL and with independent arithmetic. The comparison covers the return, the trace written by the second call, the guards and the RBX/RSI sentinels, for the same 249 pairs as `cpu_aot_direct_call`. - **Altered callee:** a callee that returns one more changes the caller's return (by exactly 2) and its trace on every pair. An earlier draft combined the first result with XOR. This control caught that the altered return then equals the original on some inputs (for example x=1, y=0), so the fixture uses ADD. - **Linked calls:** the original and both lifted DLLs each contain two rel32 calls to their callee. - **Encodings:** patching both R8 moves to their opcode 89 encodings leaves the generated C++ unchanged. - **Rejected by translation:** - a copy assembled without `mov r8, rsi` ("undefined register"); - the first or the second call redirected into the callee interior ("call to a second target"); - both calls redirected there ("no unique function interval"); - both calls redirected to the caller ("overlapping call target"); - `mov r8, rsi` patched to `mov r8, rbx`, where RBX holds an input word ("R8 reloaded without the output pointer"). Without the pointer tracking, this copy translated and its stores would have used an integer as the trace address. - **Mixed calls:** hand-built callers with a direct call and an import call are rejected in both orders. Direct then import gives "unexpected additional call"; import then direct gives "call to a second target". - **Before the change:** the translator on `main` rejects `call_twice` at `mov rsi, r8` with "unsupported or incomplete instruction". - **Existing outputs:** the generated `cpu_call_original`, `cpu_call_altered`, `cpu_import_original` and `cpu_ordinal_original` sources are byte-identical before and after the change. - **Full suite:** `ctest --preset dev --no-tests=error` on an RX 9070 XT (driver 32.0.32015.7029) gives 103 tests: 100 passed, 2 capability skips (`bvh_wave64_warp`, `wave_shuffle_warp`) and 1 failure. The failure is `pixel_aot_hardware`, which also fails on `main` at `583fee5` without this change, with the same first mismatch. The failing case is the comparison-predicate draw added in #158: `v_cmp_nlg_f32` with a NaN operand returns 0 on that adapter, while `pixel_aot_warp` passes. This change touches no shader code. ## Evidence and follow-up The fixture is original MASM written for this test (MIT). The README CPU row and the direct-call section of `docs/experiments.md` are updated with the command. The fixture exercises the R8 moves only through RSI. Two calls to different callees would need the translator to lift and name more than one callee, and remain open. Follows the direct-call work in #22. ## 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. _AI-assisted (Claude Code)._
upstream-sync closed this pull request 2026-11-24 06:41:41 +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!176
No description provided.