cpu: lift RDX:RAX multiplies, divides and sign extensions in the jump-table translator #316
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/321"
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-21T22:02:33Z.
Change
Builds on #285 (merged) and is independent of #309.
tools/cpu_switch_aot.pyrefusedCBW,CWDE,CDQE,CDQ,CQO, one-operandMULandIMUL, andDIVandIDIV, which MSVC emits for 64-bit multiply-high, division and modulo.consttemporary before RAX or RDX is written, since the source may be one of them (mul rdx) or be addressed through them.std::uint64_tarithmetic, with no signed conversion and no shift of a negative value. An 8-bit multiply writes AX; a 16-bit multiply merges into AX and DX._umul128and_mul128. Quotients use_udiv64,_div64,_udiv128and_div128, which executeDIVorIDIV, so a zero divisor or a quotient that does not fit raises the same divide error as the original. No C++/or%is emitted. The generated source now includes<intrin.h>.unsupported divide width. A flag consumer after a multiply or divide stays rejected. The sign extensions, which the Intel SDM lists with "Flags Affected: None", may sit between a flag producer and its consumer.MULorCQOthat overwrites a table base or index before the dispatch would be accepted. Two controls cover it.Limitation, stated in the docs: at
/O2MSVC removes a divide whose quotient and remainder are both unused, and its divide error is then not raised. The same already happens to a load whose value is unused, for every load except aCMOVccmemory source.Generated C++ for functions that already translate is unchanged apart from the include line.
Verification
Local runs used the same compiler build as CI (MSVC 14.52.37025:
cl,link,ml64) under Wine 11.0 on Linux, ata3cc509on main1292d15. I reran the items below on this head unless a line says otherwise.ctest --no-tests=error -R '^cpu_aot_wide_arithmetic$' -V: passed. 13 exports and 2,929 non-faulting inputs; each original and translated export is compared through the register probe with a Python reference, which rounds signed quotients toward zero itself, on RAX, the guarded trace, RBX and RSI.OSErrorand leave the same trace. Under Wine every divide error readsexception: integer divide by zero. Whether Windows reports the overflow cases asinteger overflowwas not observed; the test accepts either message when both sides agree.div r9dpatched todiv r9band todiv cx; aJccright after a divide;cdqreplaced byclcbetween a compare and its consumer; RAX or RDX undefined at a multiply or divide (four cases, one of them RDX holding a guest address); a flag consumer after a multiply; a multiply that overwrites a jump-table base; a sign extension that overwrites a jump-table index.cpu_aot_jump_tables,cpu_aot_narrow_returns,cpu_aot_entry_order: pass unchanged. Othercpu_*tests: same results as 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 (#281). 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:/O2642 → 650,/O1639 → 646,/Od15 → 15. No function is lost. All 1,296 functions translated by both have identical C++; the only shared difference is the<intrin.h>line.cland called beside the original on 64 inputs, comparing the defined RAX bytes, a seeded memory arena and whether a fault occurred): the 8 functions gained at/O2and the 7 at/O1match on every input, and so do all 650 translated at/O2that could be run. This is evidence on a few inputs per function, not a proof of equivalence. With signed forms lowered as unsigned, 2 of the 8 mismatch.cl /O2 /FAsshows each divide of the fixture as one inlinedivoridivwith no call or added check; all 2,566 earlier translations, library code included, compile to identical code with and without<intrin.h>; and 20 manual mutants, of which 19 fail the test. The one that survives keeps the generic operand-0 write for a one-operand form, which changes no definedness state.pyflakes,tests/docs_links_check.pyandtests/form_coverage_check.pypassed.Not run locally: the D3D12 tests and anything that needs Windows loader behaviour.
Evidence and follow-up
tests/cpu/wide_arith.asmis original MASM assembled in the build. No binaries were added.wide_arithis classified intools/form_coverage.py.gcd32,gcd64,udiv64,sdiv10,smod64,div7, a checksum and one method).MOVSX,SARand signed-condition code relies on the same.CMakeLists.txt,docs/experiments.mdandtools/form_coverage.pynext to each other. I will rebase whichever lands second.CDQEas a sign extension in the table-index proof; classifynarrowandentry_orderintools/form_coverage.py.AI-assisted (Claude Code).
🤖 Generated with Claude Code
Imported from R0Y00/Xbox2Windows. Originally posted by bosseurdinternet on 2026-11-21T22:43:33Z.
Rebased on
b860dd5after #309 landed; head is nowace1b2b. The conflicts were adjacent additions inCMakeLists.txt,docs/experiments.mdandtools/form_coverage.py, and both sides are kept (cpu_aot_fourth_argument, thencpu_aot_wide_arithmetic). The translator merged without conflict.Rerun on this head with MSVC 14.52.37025 under Wine:
cpu_aot_wide_arithmetic,cpu_aot_fourth_argument,cpu_aot_narrow_returns,cpu_aot_jump_tables,cpu_aot_entry_orderandcpu_aot_bmi1pass. Corpus against main:/O2642 → 650,/O1639 → 646, no function lost, all 1,296 functions translated by both have identical C++, and the 8 gained at/O2match their originals on every input in differential execution.AI-assisted (Claude Code).
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-22T03:33:35Z.
#420, #425 and #430 have landed, so please take #321 as the next branch in your jump-table translator queue. A trial merge against
dec705fconflicts intools/cpu_switch_aot.py,CMakeLists.txt,tools/form_coverage.pyanddocs/experiments.md. Please retain main’s frame and call handling and re-run the multiply/divide fixture and shared translator regressions after reconciliation. The other branches can wait until this one lands.Imported from R0Y00/Xbox2Windows. Originally posted by bosseurdinternet on 2026-11-22T03:42:36Z.
Rebased onto
mainat3a0f33bas one commit,10060ec.Reconciliation.
main's frame, call, bound-callee and flag-condition handling is kept as it is. The only textual conflict intools/cpu_switch_aot.pywas the module docstring, where I kept #330's sentence on overflow and parity and added this PR's sentences. InCMakeLists.txtandtools/form_coverage.pyboth sides' entries are kept. Indocs/experiments.mdmain's paragraphs are kept and this PR's two paragraphs follow the call sections; the one sentence both sides edited (instructions that leave the flags unchanged) carries both edits. This PR's code hunks applied without conflict.Rerun on
10060ec, MSVC 14.52.37025 (cl,link,ml64) under Wine 11.0:cpu_aot_wide_arithmetic: passed.cpu_aot_jump_tables,cpu_aot_flag_conditions,cpu_aot_switch_addr32,cpu_aot_entry_order,cpu_aot_narrow_returns,cpu_aot_fourth_argument,cpu_aot_stack_frames,cpu_aot_direct_calls,cpu_aot_call_entriesandcpu_aot_bound_calleespassed.cpu_*tests: same results asmain. Under Wine,cpu_aot_import_call,cpu_aot_ordinal_import,cpu_native_reloc_freeandcpu_native_tlsfail onmaintoo, andcpu_native_exe_starttimed out (#281).main: 647 → 655, 643 → 651 and 391 → 400 functions at/O2,/O1and/Od. No function is lost, and the 1,681 translationsmainproduces keep identical C++. The shared header gains its one#include <intrin.h>line.pyflakes,tests/docs_links_check.pyandtests/form_coverage_check.pypassed.Not run locally: Windows itself; the Windows job is running on this head.
AI-assisted (Claude Code).
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-22T04:02:42Z.
Contract decision for the supported DIV/IDIV forms: reaching the instruction must preserve its observable divide error even when both result registers are overwritten before use. A conditional path that skips the divide must still complete normally. Please add a Windows MSVC /O2 differential fixture with dead quotient and remainder. Compare native and translated exception codes and the trace before/after the instruction for zero divisors and quotient overflow, plus a valid divide and a branch that skips the faulting input. Use the native Windows result as the oracle. Please keep the fix local to divide lowering. This is independent of #437.
Imported from R0Y00/Xbox2Windows. Originally posted by bosseurdinternet on 2026-11-22T04:45:09Z.
Done in
75543e1, on top of the rebased10060ec.Contract. Reaching a supported
DIVorIDIVraises its divide error even when both result registers are overwritten before use. The change is local to the divide lowering: after the intrinsic, the quotient is also stored to avolatilelocal, so the compiler has to perform the divide. Multiplies and everything else are untouched, and nothing depends on #437.Fixture.
tests/cpu/dead_div.asm, original MASM. Each export stores 1 to the trace before its divide and 2 after it, then overwrites RAX and RDX:div32_dead(div r9d),idiv32_dead(cdq; idiv r9d),udiv128_dead(div r9with RDX:RAX = x:x),idiv64_dead(cqo; idiv r9);div_skip, wheretest r9, r9; jzgoes around the divide for a zero divisor and both paths store 3 to a third trace word.Test,
ctest --preset dev --no-tests=error -R '^cpu_aot_dead_divides$' -V:cl /O2 /W4 /LDcommand on the same generated source.tests/cpu/guarded_call.cpp) calls the function inside__tryand returnsGetExceptionCode().0xC0000094or0xC0000095and leaves the second trace word unwritten.ywhose low 32 bits are zero), quotients that do not fit (unsigned 32-bit and 128/64-bit,-2^31 / -1,-2^63 / -1), valid divides at the boundaries, and the skipped divide with two inputs. Inputs that do not fault are also compared through the register probe, for RBX and RSI.Results.
cpu_aot_dead_dividesandcpu_aot_wide_arithmeticpassed, and the new/O2Windows job passed as a whole. The Debug Windows job is red only forcpu_native_exe_start("native launch does not run the TLS callback (null reserved) before the entry point"), the intermittent failure of #281; I cannot rerun the job from the fork.0xC0000094, so the distinction between the two codes is only exercised on Windows./O2. The test then fails on the first faulting input:/O2 translation div32_dead(0x5, 0x0), zero divisor: code 0x0, with both trace words written, where the original raises0xC0000094after the first./O2,/O1and/Od; no status or message changes; every translation is identical to10060ec's once the added statement is removed (4, 4 and 7 functions contain a divide). The 8/O2functions this PR adds overmainstill execute with no mismatch against their originals.pyflakes,tests/docs_links_check.pyandtests/form_coverage_check.pypassed.Not covered: a divide whose memory operand is inaccessible is #437's subject, not this one; 8- and 16-bit divides stay rejected.
AI-assisted (Claude Code).
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-22T04:52:16Z.
I reran the failed Windows job on
75543e1. The first run passed cpu_aot_dead_divides and the separate /O2 job; its only failed CTest was cpu_native_exe_start (30.25 s). The required Windows check is running again. No branch update is needed for this retry.