Skip to content

cpu: translate recursion through direct calls in cpu_switch_aot - #2207

Merged
R0Y00 merged 3 commits into
R0Y00:mainfrom
dicnunz:cpu/switch-recursion
Nov 25, 2026
Merged

R0Y00 merged 3 commits into
R0Y00:mainfrom
dicnunz:cpu/switch-recursion

Conversation

@dicnunz

@dicnunz dicnunz commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Change

tools/cpu_switch_aot.py refused any self-recursive or mutually recursive function (self-recursion, mutual recursion: call cycle), and so was every function that called one. A CALL may now close a call cycle, including an export's call to its own entry. A call into a function whose translation is in progress uses an assumed return width and argument demands (at first 8 bytes and none). The cycle is translated again from the meet of the assumed and computed summaries until no computed width is lower and no demand is higher, the fixed-point return_width proposed in #317. Functions of a cycle are declared before their definitions and kept once its first function is done. A tail jump that closes a cycle stays refused, because the original runs such a cycle in constant stack and the generated C++ would not.

tests/cpu/calls_check.py and tests/cpu/bare_calls_check.py now expect the self- and mutual-recursion controls to translate, with a declared call cycle, and add a tail-jump cycle as the replacement refusal.

Refs #317.

Verification

Tested commit: 9e8d909 (tip of dicnunz:cpu/switch-recursion), rebased onto 166d866 (upstream #2176, 10:12 PM ET on 24 Nov 2026). The three-dot compare against current R0Y00/Xbox2Windows main is 3 commits ahead and 26 behind (main tip ab9a020 at 11:54 PM ET on 24 Nov 2026). The compare is 8 files, +763 / -30, limited to the translator, the new source-built fixture, existing call-test controls, CMake test wiring and docs/experiments.md.

Full Windows results are from a temporary fork branch ci/recursion-full (903aed9) that differed from the previous tip bd2c89b only by raising the Windows job timeouts from 20 minutes to 150 in .github/workflows/windows.yml. That timeout change is not in this PR branch. Those jobs have not been re-run on this tip; there are no GitHub Actions runs on 9e8d909 yet. Fork Windows checks workflow, workflow_dispatch, windows-2025 runners, 6:16 to 6:57 PM ET: https://github.com/dicnunz/Xbox2Windows/actions/runs/45508510468

Verified from the job logs:

Job Result
Build and test on Windows 924 tests, 905 passed, 18 skipped, 1 failed (call_evidence_check)
Build and test on Windows at /O2 924 tests, 905 passed, 18 skipped, 1 failed (call_evidence_check)
Run platform-independent Python checks on Linux passed
Assemble and translate gfx1013 fixtures passed
Publish the coverage chart skipped

In both Windows jobs cpu_aot_recursion passed. The 18 skips are capability or host skips (startup_view_heap_warp, startup_audio_output, startup_audio_pacing, startup_audio_provider, xg_bc64_tiling_reference, xg_swizzle_select, xg_swizzle_select_mutation, xg_swizzle_factory, xg_swizzle_factory_no_write, bvh_wave64_warp, wave_shuffle_warp, shader_wave32_warp, shader_wave64_warp, wave_startup_32_warp, startup_gamesave_submit_system, startup_gamesave_read_async_system, startup_logical_view_warp, bc7_mode6_multiblock_warp).

The one failure is tests/call_evidence_check.py, test_main_writes_expected_json_for_three_images at line 312: AssertionError comparing C:/Users/runneradmin/AppData/Local/Temp/... with the 8.3 short path C:/Users/RUNNER~1/AppData/Local/Temp/.... This branch does not touch tests/call_evidence_check.py or tools/call_evidence.py. The same assertion failed on upstream main in Windows checks run 45533450301 (push of #2141, 9:30 PM ET), so it is pre-existing and unrelated.

Failure-before, verified from the logs of fork run https://github.com/dicnunz/Xbox2Windows/actions/runs/45508518358 (ci/before-main e14f2a8, workflow conclusion success, Debug and /O2). That branch runs the new fixtures against main's translator only (plus a temporary workflow change). On main, building the lifted DLLs exited 1, and ctest of cpu_aot_recursion and cpu_aot_stack_arguments exited nonzero. before_main_check.py then reported: the native originals matched the Python reference (4 exports x 484 inputs; 8 exports x 449 inputs), all 12 of those exports were refused (self-recursion or mutual recursion: call cycle for the recursion exports; unsupported stack argument or self-recursion for the stack-argument exports), and check_structure and check_refusals failed. No unexpected pass.

cmake --build --preset dev --target cpu_recursion_lifted cpu_register_probe
ctest --preset dev --no-tests=error -R '^cpu_aot_recursion$' -V

Claude also reported a local Linux run (llvm-ml 18, lld-link, clang++ at -O0 and -O2) where the translation matched the original on 3,872 calls, and that generated C++ was byte-identical to main for 15 existing cpu_switch_aot fixtures and for the fuzz areas llvm-ml can assemble. I did not re-run those checks. From the fixture source, 4 exports over 484 input pairs is 1,936 translation-vs-original comparisons plus 1,936 original-vs-Python checks, which is the 3,872 figure.

Evidence and follow-up

tests/cpu/recursion.asm is original source written for this test (MIT), marked in the file as a source-owned differential fixture, with tests/cpu/recursion.def and tests/cpu/recursion_check.py. I found no game files, dumps or other proprietary material in the compare.

This branch was rebased onto 166d866, which already includes tools/cpu_switch_aot.py (47fa55b, register-indexed memory bit tests, 6:26 PM ET on 24 Nov 2026). The BIT_TESTS lowering on this tip is identical to that commit and to current main. Current main (ab9a020) is 26 commits ahead of the rebase base. Those commits do not touch tools/cpu_switch_aot.py. They do edit CMakeLists.txt and docs/experiments.md, which auto-merge cleanly onto current main (local merge-tree, no conflicts). A further rebase is optional.

Not verified here: upstream PR CI has not been run. The pr CTest preset is a small smoke set and does not include cpu_aot_recursion or call_evidence_check. A full-suite baseline on main (ci/main-full, run 45515371589) was still queued with no jobs as of 10:11 PM ET and has produced no results. Floating-point arguments, frame addresses passed to callees and indirect calls stay refused. Recursion depth depends on the host stack. Nothing here shows that a game starts.

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.

This change was written by Claude (Anthropic's Claude Code, Opus 5.5) and submitted by dicnunz after review.

🤖 Generated with Claude Code

A CALL may now close a call cycle, an export's call to its own entry
included. A call into a function whose translation is in progress uses
an assumed return width and argument demands, at first 8 bytes and none.
The cycle is translated again from the meet of the assumed and computed
summaries until no computed width is lower and no demand is higher, the
fixed-point return_width proposed in R0Y00#317. The functions of a cycle are
declared before their definitions and kept once its first function is
done. A tail jump that closes a cycle stays refused, because the
original runs such a cycle in constant stack.

cpu_aot_recursion translates four exports that reach self-recursion,
two- and three-function cycles, a cycle inside a cycle and an export that
calls its own entry, and compares them with the original and a Python
reference over 484 inputs each. Four exports must be refused, and two of
them only because the cycle is translated again. The direct-call
controls for self- and mutual recursion now translate, and a call that
closes a cycle with a tail jump replaces them as a refusal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014uAfgrqe3XyeqvkJrdnUK6
When a function of a call cycle goes on hold, the functions held during
its translation still recorded its stack depth as their dependence. A
later translation at the same depth that reused one of them was then
kept on its own, with the summary first assumed for the cycle, and a
tail jump to it was not seen to close the cycle. The held functions now
take on the dependence of the function that goes on hold.

stale_e in recursion.asm reaches that order and must be refused: its
callee reads all of RAX after a function that returns only AL through
the cycle. Before this change it was accepted with 8 bytes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014uAfgrqe3XyeqvkJrdnUK6
The call-cycle change stored (entry, internal) pairs in Program.active.
The planted call-retarget fault of cpu_aot_fuzz_calls compares
Program.active[-1] with an export's RVA, so it no longer planted, and the
test would report the fault as not detected. Program.active holds the
entries again, and a parallel list says which ones are internal.

The clean and planted translations of every fuzz area that llvm-ml
assembles, at the CMake default seeds, are byte-identical to main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014uAfgrqe3XyeqvkJrdnUK6

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of head 9e8d90905d275e333da1bba4219d096491f28158 (comment only, no approval).

Scope read: git diff --stat origin/main...HEAD shows 8 files, +763/-30: tools/cpu_switch_aot.py, the new tests/cpu/recursion.{asm,def} and recursion_check.py, the two existing call controls, CMakeLists.txt and docs/experiments.md. The title is neutral and the PR is MERGEABLE.

CI (gh pr checks 2207): "Run platform-independent Python checks on Linux" passes. "Build and test on Windows" passes. "Assemble and translate gfx1013 fixtures" passes. The /O2 job and the coverage chart job were skipped.

Commands executed locally (Linux):

  • python3 tests/docs_links_check.py passed: 18 Markdown files, 405 links.
  • python3 tests/bidi_controls_check.py passed: 1468 files, no bidirectional controls.
  • git diff --check origin/main...HEAD is clean.
  • python3 tests/cpu/calls_check.py could NOT run here. It stops at import pefile because the Linux host lacks the module. The recursion, calls and bare_calls checks execute PE fixtures through cpu_register_probe, so they need the Windows build. I therefore ran no mutation of the new logic. The Windows CI pass above is the only execution evidence I have. I did not queue a Win2025 run.

Static read of the fixed-point logic (Program.callee, lift):

  • Termination holds. On each retry the assumed return width only decreases, and the assumed demands only increase and are bounded by the 8 argument bytes of each of the 4 registers.
  • A failure inside a cycle drops its pending members (del self.pending[start:]) and clears the assumed summary, so no half-built member is kept.
  • Assumed carries .entry, so committed() is safe on it. A tail jump to a pending or assumed callee is refused. Pending members always have low < depth, so they are in a cycle, and the refusal is correct.

Findings:

  • P1 = 0.
  • P2 = 0.
  • nit 1: lift stores self.low as an attribute only so that callee can read it right after the call. Returning it from lift, or keeping it local, would avoid hidden state. This is not a correctness issue.
  • nit 2: MAX_CALLEES is now checked as len(callees) + len(pending). On a retry the pending entries are removed first, so the count is fine. A one-line test at the limit with a cycle would pin this down.
  • nit 3: The PR body says the main compare is 26 commits behind. Please re-run the Windows check after the next rebase, because the translator is shared by other pending CPU PRs.

AI-assisted; commands listed were executed locally.

@R0Y00
R0Y00 merged commit edbcbe5 into R0Y00:main Nov 25, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants