x64_recomp: rcl and rcr rotate-through-carry emitters (X64-RECOMP-RCL-RCR) #1211
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1211"
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 ubiquitarianism/leonida. Originally posted by quietfold on 2026-11-24T20:20:18Z.
Claimed in the coordination issue as: X64-RECOMP-RCL-RCR (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7143389193)
What and why
Implements the
rclandrcrrotate-through-carry emitters called out in Next Step #2 ofdocs/mobile.mdinsidetools/x64_recomp.py(independent of#1174,#1202, and#1205, and conflict-free with all openx64_recompPRs):_emit_rcl_rcr): supports 8/16/32/64-bit registers (including high-byteah/ch/dh/bh), memory operands, immediate count1/imm8, and variableCLcounts (includingdst == rcx, e.g.,rcl ecx, cl).& 0x3Ffor 64-bit,& 0x1Ffor 8/16/32-bit) and reduces modulowidth + 1(% 9for 8-bit,% 17for 16-bit) without clobberingNZCVso effective count0leaves both destination and all flags untouched.(orig >> 1) >> (width - c)(rcl) and(orig << 1) << (width - c)(rcr) soc = 1andc = widthnever trigger arm64 shift-by-64 wraparound.CFfromSUB_KIND(inverted armC),ADD_KIND(armC),ucomis*/comis*(lt), or materializedCTX_EFLAGS; writes outgoingCFwithout clobberingN/Z/V, and updatesOF(V) when effective count is1(MSB(res) ^ new_CFforrcl,MSB(res) ^ MSB-1(res)forrcr).tests/test_x64_recomp_rcl_rcr.py): covers translation of all 27 forms plus 4 end-to-end execution test suites exercising 8/16/32/64-bitimm/CL, mod-9/mod-17 wrap, zero-count no-op, multi-word shift chaining,subborrow chaining, high-byteah, memory operands,rcl ecx, cl, and count-1OF.Tested
main:tests/test_x64_recomp_rcl_rcr.py,tests/test_ci_registration.py,tests/test_text_write_line_endings.py,compileall,make_game.py --help,check_no_game_data.py: all passed.(width + 1)-bit rotation across all 7,168(width, value, cf, count)combinations: 100% match.#1174,#1202, and#1205in a scratch checkout):tests/test_x64_recomp_rcl_rcr.py5/5 passed,tests/test_x64_recomp_bitops.py9/9 passed,tests/test_x64_recomp_sse.py16/16 passed,tests/test_x64_recomp.py46/46 passed (76/76 total).git apply --check) against openx64_recompPRs#847,#950,#1120,#1121,#1174,#1202, and#1205.GPUs tested: n/a (CPU x86-64 -> arm64 recompiler; tested natively on Apple M5 arm64, macOS 27.2.1)
AI-assisted: yes
Checklist
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed.Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-24T20:20:28Z.
Review: COMMENTED
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
Imported from ubiquitarianism/leonida. Originally posted by kevincommits on 2026-11-25T23:38:03Z.
Independent run with QEMU. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew. I merged
mainat33bd07cf8into the branch locally (960 commits behind, no conflicts). Head tested:621aaeefc.The emitter matches a reference model in 640 of 656 cases, and the 16 that differ are a bug that is already on
main, not one this adds.python -I tests/test_x64_recomp_rcl_rcr.py: 5/5 under QEMU. This file callsx.run_resultand gates onHAS_QEMU, so its exec tests really run.tests/test_x64_recomp.py33/33,tests/test_recomp_census.py11/11. Full standalone suite: 513 run, 509 pass; the 4 that fail aremain's 4.Beyond the file, I ran 656 generated cases under QEMU against a small Python model of rotate-through-carry:
rclandrcr; widths 8, 16, 32, 64; theD0/D1(by 1),C0/C1 ibandD2/D3(by cl) forms; counts 1, 3, 9, 17, 31, 33, 63; carry-in set bystc,clc, a borrowingcmpand a non-borrowingcmp; register and[rbx+rsi*1+8]destinations. Each case checks the full 64-bit result (so the untouched upper bits too) and the carry out.640 match. That covers the modulo-9 and modulo-17 counts, count == width, the 64-bit shifts by 63, carry-in taken from a
cmpin borrow polarity, and the memory forms.16 differ, all the same shape: effective count 0 (
rcl al, 9,rcl ax, 17, either form) directly after acmp. The value is correctly left alone, but a followingsetcreads the carry inverted.shl/shr/sar/rolwith a zero count do exactly the same onmaintoday, so this is the existing behaviour of the flag analysis for a shift that writes no flags. I posted it with a reproducer as a finding in #1608. A fix there should cover these too.Not tested: the OF result for count 1, and
rcl/rcrfeedingadc/sbb. No game files used. AI-assisted.Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T23:01:21Z.
Native Windows 11 Verification & Flag-Liveness Review (
621aaeefc1)Diff-audited
tools/x64_recomp.py(+122/-1) andtests/test_x64_recomp_rcl_rcr.py(+238/-0) and verified merged ontoorigin/main(adfb3821a2) on Windows 11 (10.0.26200, Python3.12.10with-I).Test Execution Results
origin/mainadfb3821a2withtest_x64_recomp_rcl_rcr.py):FAILED (failures=1, skipped=4)(UnhandledInsn: rcl at 0x46000000: rcl).PR #1211merged ontoorigin/main):python -I tests/test_x64_recomp_rcl_rcr.py:Ran 5 tests in 0.001s — OK (skipped=4)(RclRcrTranslateTeststranslates all 26rcl/rcrforms +retwithunhandled == [];RclRcrExecTestsskipped on Windows x86-64 withoutqemu-system-aarch64).python -I tests/test_x64_recomp.py:Ran 45 tests in 0.001s — OK (skipped=32).scripts/check_no_game_data.py,tests/test_ci_registration.py(18/18), andtests/test_text_write_line_endings.py(3/3):OK.Three Flag-Liveness /
CTX_EFLAGSIssues Found intools/x64_recomp.pyInspecting the emitted ARM64 words from
x64_recomp.translate_function(...)surfaced three edge cases in flag tracking/materialization that existing tests did not hit:FLAG_WRITERS["rcl"]/FLAG_WRITERS["rcr"](line 63) still declares"C"instead of"CV"(breaksjo/jno/seto/cmovoafter 1-bitrcl/rcr):_emit_rcl_rcremits_set_rc_ofto set ARMV(OF) for count-1 rotates, andtest_count1_overflow_flagtestedOFviapushf(flags_r == "NZCVPA", which matched"C").V-only consumer (seto,jo,cmovo, whereflags_r == "V") skips pastrcl/rcrin_flag_analysis. Reproducer: Hereft.arm_path[seto]resolves toxor(LOGIC_KIND,V=0), soseto dlcompiles tomovz w0, #0; b +2(hardcodeddl = 0) even thoughrcl eax, 1setOF = 1.CL-variable or multi-bit immediatercl/rcr, if"V"is added toFLAG_WRITERS, multi-bit immediate rotates (cval != 1) only writeC(orOFis undefined per Intel SDM), whereasCL == 0is already handled by_cond_flag_writer.Immediate
cval = raw % (width + 1) == 0(rcl al, 9,rcr ax, 17) returns early without normalizingSUB_KINDborrow polarity:_classify(lines 452-458),is_imm_zerochecks(imm & mask) == 0without% (width + 1), sorcl al, 9(c0 d0 09) getsflags_w = "C"and becomesarm_pathproducer for a followingjc/setb._emit_rcl_rcr,rcl al, 9executescset w3, cc(reading invertedSUB_KINDborrow) and then returns atif cval == 0: returnwithout callingself._set_cf(T3). Reproducer:subleavesARM C = 0for borrow (CF = 1),rcl al, 9leavesARM Cuntouched, andsetb dl(arm_path == rcl, notSUB_KIND) emitsb.cs(0x54000002), inverting the carry! Either_classifyshould treatraw % (width + 1) == 0forrcl/rcrasis_imm_zero = True(flags_w = flags_r = ""), or_emit_rcl_rcrshould normalizeARM Cbefore returning.self._maybe_mkflags(ins)(rt_mkflags) clobbersSF,ZF,PF, andAFinctx.eflags:SF,ZF,PF, orAF. Unlikerol/ror(lines 2621-2624, 2686-2689) andstc/clc(lines 2405-2411) — which patchCTX_EFLAGSin place withbfi—_emit_rcl_rcrcallsself._maybe_mkflags(ins)(bl rt_mkflags), rebuilding the entireCTX_EFLAGSword from current ARMNZCVand staleCTX_RES/CTX_A/CTX_B(corrupting priorZF/SF/PF/AFacross basic blocks,popf, orCL-variable shifts). PatchingEF_CF(bit 0) and, whenc == 1,EF_OF(bit 11) directly intoCTX_EFLAGSviabfiavoids touching the other flags.AI-assisted.
Imported from ubiquitarianism/leonida. Originally posted by enlight3d on 2026-11-29T00:44:29Z.
Independent run of #1211 (python -I, no build, no game; GPU not used).
621aaeefc1829530dbd0(PR merged into it locally withgit merge --no-ff, clean,tools/x64_recomp.pyand the new test only)qemu-system-aarch64here and not Apple Silicon, so the execution tests are skipped (4 of 5 in the new file).Commands (one process each, from the repo root):
python -I tests/test_x64_recomp_rcl_rcr.py, the 16 othertests/test_x64_recomp*.py/test_recomp_census.pyfiles that exist on main, plustest_ci_registration.py,test_text_write_line_endings.py,test_tool_stdout_encoding.py,test_check_no_game_data.py.tools/x64_recomp.pyreverted to main and the new test kept: FAILED (failures=1, skipped=4). The failing test is the translation test,rcl/rcrare reported as unhandled instructions. New-feature red (instructions missing on main). The 4 skipped tests execute the result and were not exercised here.test_x64_recomp.py32 run OK (skipped=26) andtest_recomp_census.py7 OK (the head branch is based on an older main, the other x64 test files do not exist there).test_x64_recomp.py45 run, OK, skipped=32, merged and on main).test_tool_stdout_encoding.pyfails identically on plain main and merged (pre-existing, 4 programs outside this PR).Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-29T00:54:31Z.
Merged after enlight3d's red/green check (check) (RTX 3090, driver 621.40, Windows 11) and a local re-check here. Merged after local re-verification: this PR merged onto
maind63e0b94dwithout conflict, and its test file(s) (tests/test_x64_recomp_rcl_rcr.py) pass there. Earlier today the same tests failed onmainwithout the PR (red/green) and a bundle with other open PRs showed no new failures.check_no_game_data.pyis clean. Windows 11, Python 3.14; no GPU needed for these tests.AI-assisted (Claude Code, Claude Opus 5.5), merged for MoonEyes.