cpu: start a translated function at its entry when code below it is reached #278
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/282"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by bosseurdinternet on 2026-11-21T20:32:36Z.
Change
Fixes a wrong translation in
tools/cpu_switch_aot.py.lift()emits C++ in address order and relied on the entry being the lowest instruction. A leaf without unwind data may use any unowned bytes of its section, so a jump from the entry can continue in code assembled below it. The generated function then began at that lower code, returned, and left the entry unreachable.Example from MSVC
/O2:u32 tail_call(u32 x) { return helper_square(x + 1); }compiles toinc ecx; jmp helper_square, withhelper_squareat a lower address. On main the translation starts with the multiply and returnsx * x.The fix is two edits near the end of
lift(): when the lowest instruction is not the entry, the entry gets a label and the body starts withgoto loc_<entry>;. Output is unchanged for every function whose entry is its lowest instruction.Verification
Local runs used the same compiler build as CI (MSVC 14.52.37025:
cl,link,ml64), under Wine 11.0 on Linux, atc3842fdrebased onc832696.cpu_aot_entry_order: passed.inc_squareincrements RCX and jumps tosquare_tailbelow it;pick_tailbranches there on a comparison. Native and translated exports match a Python reference on return, guarded trace, RBX and RSI over 249 input pairs each, andpick_tailtakes both paths. The test prints one control line per export: entry RVA, lowest instruction RVA, and whether the translation starts with a jump to the entry.square_tail, whose entry is its lowest instruction, must start at the entry.inc_square(0, 0)returns 0 where the native export returns 1.cpu_*tests:cpu_aot_jump_tablesand the rest pass as on main. Under Wine,cpu_aot_import_call,cpu_aot_ordinal_import,cpu_native_reloc_freeandcpu_native_tlsfail on main too, because Wine's loader differs from Windows, andcpu_native_exe_starttimes out intermittently. None of them uses this translator path; the Windows job is the reference for those./O2from this repository's C++ sources plus one plain C++ file, and the/O1and/Odbuilds of the same sources, with main and with this branch. No function is lost. Of 1,004 functions lowered by both across the three levels, 1,002 have identical C++. The two that differ aretail_callat/O2and/O1, each gaining the label and the jump./O2corpus (each lowered function compiled withcland called beside the original on 64 inputs, comparing RAX and a seeded memory arena): main has 506 functions with every completed input matching and 1 mismatch,tail_call, wrong on all 64 inputs. This branch has 507 and 0. This is evidence on a few inputs per function, not a proof of equivalence.Not run locally: the D3D12 tests and anything that needs Windows loader behaviour.
Evidence and follow-up
tail_callwas the only one affected.docs/experiments.mdsays so.lift().AI-assisted (Claude Code).
🤖 Generated with Claude Code