cpu: lower bounded ADD/SUB/CMP to LAHF slice #1024
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1031"
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 Ammmae0x on 2026-11-23T22:11:11Z.
Change
Add a bounded leaf translator (
tools/cpu_integer_flags_aot.py), MASM differential fixture (tests/cpu/integer_flags.asm,tests/cpu/integer_flags.def), verification suite (tests/cpu/integer_flags_check.py), CMake registration (cpu_aot_integer_flags), and documentation entry (docs/experiments.md) covering five uncovered x64Integermnemonics fromdocs/coverage/x64_zen2.tsv:lahf: writes(SF<<7)|(ZF<<6)|(AF<<4)|(PF<<2)|0x02|CFtoAHwhile preservingALandRAX[63:16]pushf/popf: 2-byte stack stride (RSP -/+ 2) for low 16-bitFLAGS(CF,PF,AF,ZF,SF,DF,OF, fixed bit 1, and ring-3IF=0x0202, preservingID)pushfq/popfq: 8-byte stack stride (RSP -/+ 8) for 64-bitRFLAGS, additionally modelingID(bit 21)Each export uses the leaf ABI
void fn(word rcx, word rdx, word r8), modeling volatile GPRs (rax,rcx,rdx,r8,r9,r10,r11) across 8-, 16-, 32-, and 64-bit views (including high-byteah/ch/dh), a bounded 256-byte local stack balanced to entryrspatret, per-flag definedness tracking (and/or/xor/testleaveAFundefined,inc/decpreserveCF,sahfleavesOF/DF/IDunchanged,popfleavesIDunchanged), staticDF=0(cld) enforcement atret, andstd::atomic_signal_fencedead-read ordering. Nonvolatile GPRs, directrsparithmetic, branches/calls, prefixes, VEX/EVEX encodings, unbalancedrsp, stack overflow/underflow, non-zeroDFatret, undefined flag/GPR reads, and non-r8stores are rejected.Verification
Verified on both
dev(Debug) andrelease(/O2) presets (0 errors, 0 warnings):cmake --build --preset dev --clean-first --target cpu_integer_flags_original cpu_integer_flags_original_lifted cpu_integer_flags_altered cpu_integer_flags_altered_lifted cpu_integer_flags_controls: 0 errors, 0 warningscmake --build --preset release --clean-first --target cpu_integer_flags_original cpu_integer_flags_original_lifted cpu_integer_flags_altered cpu_integer_flags_altered_lifted cpu_integer_flags_controls: 0 errors, 0 warningsctest --preset dev --no-tests=error -R "^cpu_aot_integer_flags$" -V: 1/1 Passed (216 native/lifted/reference cases across 11 exports; 5ALTER_IMMcontrols diverging on 16/21/21/16/25 cases; 6 mutated-C++ controls diverging onctrl_wrong_af: 7,ctrl_wrong_pf: 13,ctrl_wrong_if: 16,ctrl_wrong_pushf_stride: 21,ctrl_wrong_popf_stride: 17,ctrl_lahf_clobber_rax: 21;op_discarded_readdead-read fault ordering; output-mutation control; 5 alternate-encoding lift checks; 41 unit rejection controls; 10 end-to-end PE-patched DLL rejection controls)ctest --preset release --no-tests=error -R "^cpu_aot_integer_flags$" -V: 1/1 Passed with identical counts.\out\pe-env\Scripts\python.exe -B tests/form_coverage_check.py: passed.\out\pe-env\Scripts\python.exe -B tests/docs_links_check.py: passed (13 Markdown files, 224 links).\out\pe-env\Scripts\python.exe -B tests/bidi_controls_check.py: passed (871 tracked files).\out\pe-env\Scripts\python.exe -B tools/form_coverage.pyoncpu_aot_integer_flagsJUnit output: 24/948 x64 forms covered (Integer: 24/191 includinglahf,pushf,popf,pushfq,popfq)Evidence and follow-up
All fixtures are original source-built MASM and Python tests submitted under the MIT License (
tests/cpu/integer_flags.asm,tests/cpu/integer_flags.def,tests/cpu/integer_flags_check.py) with synthetic nibble-carry/borrow boundaries,ID=0/ID=1/DF=0/DF=1flag masks (keepingTF=0/AC=0/IOPL=0/NT=0), 2-byte vs 8-byte stack stride sentinels, and negative controls (ALTER_IMM, 6 mutated-C++ controls, dead-read ordering, single-bit output mutation, 41 unit rejections, and 10 PE-patched DLL rejections).TF/AC/NTtrap or privilege side effects, control flow, and nonvolatile GPR prologues remain out of scope.Checklist
AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T22:18:11Z.
This is broader than the same-block LAHF slice in #1022. There is also a concrete input-contract problem in the stack forms: POPF/POPFQ accepts any initialized stack word (including a caller-loaded word), but the generated code silently drops TF/AC/NT while the native instruction can change observable execution. The test vectors intentionally never set those fields. Please make the first candidate the bounded ADD/SUB/CMP → LAHF slice in #1022, with the full RAX comparison, a live flag consumer, and CPUID gating for native LAHF. Preserve the flag-stack work separately. To revisit it, require a statically proven permitted input word or a checked caller contract with rejection before guest-visible effects.
Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-23T22:40:38Z.
Refs #1022. Narrowed
cpu/integer-flags(commit2e761c0) to the bounded same-block registerADD/SUB/CMP->LAHFslice and removedpushf/popf/pushfq/popfq(along withsahf/inc/dec/neg/adc/sbb/clc/stc/cmcand local stack forms) from this PR so flag-stack work stays separate:tools/cpu_integer_flags_aot.py): accepts register-destinationadd/sub/cmpacross 8-, 16-, 32-, and 64-bit register/immediate operands, same-blocklahf(requiringCF/PF/AF/ZF/SFandRAXdefined; preservingALandRAX[63:16]without modifying flags), andsetccconsumers; rejectspushf/popf/pushfq/popfq,and/xor/test/shl/imul, calls/branches, and undefined flag/GPR reads.tests/cpu/integer_flags.asm,tests/cpu/integer_flags_check.py,CMakeLists.txt): added 8/16/32/64-bitadd/sub/cmp->lahfexports comparing fullRAXplus 5 livesetccconsumers (setb,setp,sete,sets,seto) afterlahf; gated native execution onCPUID.80000001H:ECX[0]viahost_has_lahf()withSKIP_RETURN_CODE 77; added #1022 mutation controls (ctrl_af_bit4,ctrl_pf_full_width,ctrl_lahf_bit1_cleared,ctrl_lahf_clobber_rax,ctrl_lahf_keep_old_ah,ctrl_lahf_clobber_flags) and 48 unit + 10 PE-patched rejection controls.devandrelease(/O2) builds ofcpu_integer_flags_*(0 errors, 0 warnings);ctest --preset dev --no-tests=error -R "^cpu_aot_integer_flags$" -Vandreleaseboth passed (460 native/lifted/reference cases across 16 exports, 4ALTER_IMMcontrols, 6 C++ mutations, dead-read and output-mutation controls, 3 alt-encoding checks, 48 unit + 10 PE rejections);form_coverage.pycoverslahfinInteger;form_coverage_check.py,docs_links_check.py, andbidi_controls_check.pypassed.AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by zohaiblazuli on 2026-11-23T23:01:44Z.
Following the maintainer's note on #1022, I'm not opening a second PR. Two things for this one:
Review of AH preservation and AF/PF definedness (at
2e761c0, read only, not run):lahfemits(rax & ~0xff00) | pack_lahf(...) << 8, so AL and RAX[63:16] are kept. Theop_add8_al_lahfexport covers a writer on AL followed bylahf.require_flagschecks all five flags atlahf, and onlyadd/sub/cmpdefine AF and PF. Branches and calls are refused outright, so there are no joins to reason about. One small observation:lahfalso requires RAX to be defined, which refuseslahfstraight after entry even though nativelahfdoesn't read RAX. That's harmless for this slice, but worth stating as a translator contract.The shared-lowerer change. I have it as one commit on my fork:
235b10foncpu/lahf. It's based onmainat64ecc87. You're welcome to take any of it into this PR.tools/ffx_version_query.py:lahf_sources(body)accepts alahfonly when its flags come from a registeradd,suborcmp(register or immediate source) earlier in the same straight-line block, with that instruction as the last writer of any status flag. It refuses at the entry, at a branch target, across a branch, call or join, in a function with an indirect branch, and after any other flag writer. That writer then also assignsauxandparity, which are declared only in functions withlahf. Thelahfstatement replaces AH only. Refusals keep the "unsupported query operation" text, sox87_compareandx87_statusstill pass unchanged.tests/cpu/lahf.py(cpu_aot_lahf): 20 MASM exports, CPUID-gated, compared with native and a reference model on the whole RAX. Every RAX bit outside AH is preloaded, including all-set, so reserved bits 5, 3 and 1 show. There is asetl/jbeconsumer afterlahf, 19 refused shapes, and five mutated translations (AF never set, odd parity, whole-RAX write, bit 1 clear, OF changed before the consumer).cpu_tests (excluding_hardware) passed in bothdevandrelease, and the FFX WARP tests passed indev.If you'd rather keep your translator and its fixture as the integration point, I'm happy to review whatever you take over, or to send the lowering change as a PR into your branch. Whichever is easier for you.
Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-23T23:16:23Z.
Merged
upstream/main(b1d041c) intocpu/integer-flagsat096c61eto resolve thedocs/experiments.mdmerge conflict.Thanks @zohaiblazuli for reviewing
2e761c0! Confirmed the translator contract indocs/experiments.md:lahfrequiresRAXto be previously defined in the function soALandRAX[63:16]are preserved (andlahfat entry with undefinedRAXis rejected). To keep this PR focused on one reviewable behavior (cpu_integer_flags_aot.py), I'm keeping the sharedtools/ffx_version_query.pylowering work separate rather than folding235b10finto this branch.Verification at
096c61e:ctest --preset dev --no-tests=error -R '^cpu_aot_integer_flags$' -V: Passed (460 cases matched bit-for-bit, 3 alternate-encoding checks, 48 unit rejections, 10 PE-patched rejections, 4ALTER_IMM, 6 C++ mutations, dead-read & output-mutation controls).ctest --preset release --no-tests=error -R '^cpu_aot_integer_flags$' -V(/O2): Passed (same full suite).form_coverage_check.py,docs_links_check.py,bidi_controls_check.py: Passed.AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by zohaiblazuli on 2026-11-23T23:18:21Z.
Happy to help :)
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T23:25:23Z.
Thanks for resolving the conflict and for the AH review. The updated branch still adds a second production lowerer for the same ADD/SUB/CMP → LAHF slice. For #1022, please carry the existing shared patch into this PR with its attribution, and keep the native fixture with the full-RAX, live-flags and refusal controls. The fixture and shared change can be separate commits here. @Ammmae0x @zohaiblazuli, please rerun the fixture through the shared path after combining them. The separate flag-stack forms can keep their scope.
Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-23T23:51:45Z.
Updated cpu/integer-flags (551f97, 8c82b4) to address the review feedback (Refs #1022):
Verified locally on both dev and
elease (/O2) presets (0 errors, 0 warnings on cpu_integer_flags_* and cpu_version_query*):
AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T23:59:11Z.
The shared lowering and fixture are in place. I pushed
bfe43124after finding that a host_ops binding could replace the ADD with a no-op while LAHF still accepted it as the flag producer. The shared analysis now refuses an intervening opaque binding, with a direct regression. The LAHF and existing version-query CTests passed in both Windows presets, with all 460 native/lifted/reference rows retained.