x64_recomp: clear logic OF & high eflags in rt_mkflags, fix parity/ctx_jcc NZCV & T0 clobber (X64-RECOMP-PARITY-LOGIC-EFLAGS) #1401
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1401"
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-24T23:29:22Z.
Item ID:
X64-RECOMP-PARITY-LOGIC-EFLAGSSummary
Fixes four flag-materialization and condition-testing bugs in
tools/x64_recomp.py:rt_mkflags(kind == 2,LOGIC_KIND) leaked staleOF: ARM64ands/tstleavesNZCV.Vunchanged from prior arithmetic, andrt_mkflagscopiedNZCV.VintoEF_OF(bit 11) even whenkind == 2(and/or/xor/test), corrupting cross-blockjo/jno/jl/jge/jle/jg. Now clearsOF = 0forkind == 2, matchingCF = 0.rt_mkflagswrote only 32 bits ofCTX_EFLAGS:a.str(2, CTX_REG, CTX_EFLAGS, size=4)left stale upper 32 bits written bypopfq(size=8), leaking them into subsequentpushfq. Changed tosize=8._parity_jccignoredCTX_EFLAGSafterpopf/sahf/materialized paths and clobbered ARMNZCV: previously always folded staleCTX_RESand rantst_i(T0, 1), corruptingjp/setp/cmovpafterpopf/cross-block paths AND destroying live ARMNZCVfor any subsequent same-blocksetcc/cmov/jcc. Now readsEF_PF(bit 2) fromCTX_EFLAGSwhenprod is None,prod.mnem in ("popf", "sahf"), orprod.addr in self.mat, and usestbz/tbnz(never modifyingNZCV)._parity_jccand_ctx_jccclobberedT0(holding_emit_cmov's loaded source operand) and_ctx_jcc_regclobberedNZCV: everycmovp/cmovnpand every cross-blockcmovccloadedsrcintoT0, then called_parity_jcc/_ctx_jccwhich overwroteT0withctx.eflagsor foldedctx.resbeforea.mov(g, T0, sf). Switching_parity_jcc/_ctx_jccto scratchT2and usingtbz/tbnzfor single-bit conditions in_ctx_jcc_regpreserves bothT0and live ARMNZCV.Tests & Verification
python3 -m compileall -q make_game.py tools tests— OKpython3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_parity_logic_eflags.py— OKpython3 -I tests/test_x64_recomp_parity_logic_eflags.py -v— 2 static OK (5 exec skip cleanly without#1174runner)python3 -I tests/test_x64_recomp.py— 6 static OK (26 exec skipped)python3 -I tests/test_text_write_line_endings.py— 3/3 OKtests/test_x64_recomp_parity_logic_eflags.pyon unpatchedupstream/main+#1174): 5 failures (FAILED (failures=5)).upstream/maincdb003ea1with#1174+ all 19 other openx64_recompPR branches):tests/test_x64_recomp_parity_logic_eflags.py: 7/7 OK (0 skipped) on native Apple M5 arm64tests/test_x64_recomp_*.pysuites combined: 145/145 OK (0 skipped)Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-24T23:29:35Z.
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-26T04:15:51Z.
Independent run with QEMU. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew. I merged
mainata73a57e13into the branch locally (902 commits behind, no conflicts). Head tested:df07ec2bd.Against a reference model
maingets 70 of 212 condition-code checks wrong. This branch fixes 62 of them; the 8 it leaves are a separate bug that #1202 already fixes.Test file. As shipped under QEMU,
python -I tests/test_x64_recomp_parity_logic_eflags.pyis 7 run, 5 failures, from the wiring bug in #1409 (xr.run_kernelwherexr.run_resultis needed). WithRUNNERswapped forxr.run_resultin memory (no file changed): 7/7.Beyond the file, generated cases under QEMU against a small Python model:
cmp,test,add,sub,and,xor) by seven operand pairs, each in the same block and after ajmp +0, preceded by an overflowingaddso a stale OF would show. Thensetp, sete, sets, setb, setl, seto, setnp, setgein that order, parity first, so anything the parity check clobbers shows in the ones after it (84 checks);cmovcc edi, edxfor all 16 conditions aftercmpon four operand pairs, same block and after ajmp +0(128 checks).cmovbe/cmovaafter a block boundary, 4 pairs each (timeout)mainsetp:cmp7,sub7,add7,xor5,and2,test2;cmovccacross a block boundary: 28 (plus the same 8be/a);cmovccin the same block: 4The 8 left over are not this PR. On
main,_ctx_jcc_reghandlesbe/awithtst_i(w, EF_CF | EF_ZF);0x41is not an arm64 bitmask immediate, so the translator recordssetcc@…: 0x41 not a bitmask immas unhandled and the code traps at run time. That affects everyjbe/ja/setbe/seta/cmovbe/cmovathat reads flags fromctx.eflags. This branch keeps thattst_i; #1202 replaces it withmovz+tst_r. The two PRs touch neighbouring lines of_ctx_jcc_reg, so whichever lands second needs a small rebase.Counts.
tests/test_x64_recomp.py44/44 andtests/test_recomp_census.py11/11, the same asmain. Full standalone suite: 514 run, 512 pass; the 2 that fail aremain'stest_mod_pack_output_aliasand the new file, for the wiring reason.Not tested:
sahfas a producer, AF, thert_mkflagsupper-32-bit clear beyond the file's own case, and 8/16/64-bit producers. No game files used. AI-assisted.Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T14:37:03Z.
Red/green: classified READY by dada-lotF (red on main, green on head merged with main); head
df07ec2bd0unchanged since 2026-11-24, before that run.Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main
d48a957bbin GitHub Actions: no new failure.AI-assisted (Claude Code, Claude Opus 5.5)