cpu: produce flags from SHL, SHR and SAR by an immediate in cpu_switch_aot #1201
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1208"
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 dicnunz on 2026-11-24T03:33:23Z.
Change
tools/cpu_switch_aot.py(the lifter behindtools/exe_aot.py's console-stdio profile and the CPU fuzz smoke from #894) refused every shift as a flag producer, soshr eax, 1; jb,shl rax, 1; joorsar ecx, 4; jsstopped a translation.Jcc,SETccandCMOVccmay now read the flags ofSHL,SHRandSARby an immediate whose masked count (five bits, six for 64-bit) is 1 to the operand width minus 1:SHL, MSB(operand) forSHR, 0 forSAR(Intel SDM Vol. 2B "SAL/SAR/SHL/SHR—Shift", AMD APM Vol. 3 SAL/SAR/SHL/SHR).unsupported flag producerprefix: a shift byCL(a masked count of 0 would pass the earlier flags through), a masked count of 0, and a count of at least the operand width (CF undefined forSHL/SHR;SARis refused with them).O,NO,L,GE,LEandGafter a count above 1 are refused withOF is undefined after a shift by more than 1. Rotates are unchanged.tools/cpu_sse_aot.pyreusesLifterandFLAG_PRODUCERS, so its GPR path accepts the same immediate shifts before aJcc; CL shifts there are still refused withunsupported flag producer.One existing control changes:
tests/cpu/switch_check.pyplacedSHL EAX, 1betweenCMPandCMOVGEand expected a refusal. That shift is now a valid producer, so the control placesROL EAX, 1(d1 c0), which also writes CF and OF and is still refused, keeping what the control checks.Verification
Tested commit:
e91c53e, one commit on4a47793(upstreammain).Windows CI on my fork,
Windows checksworkflow one91c53e,windows-2025runner, MSVC 19.52.37029.0 and ML64 14.52.37029.0: https://github.com/dicnunz/Xbox2Windows/actions/runs/45311577260 The first attempt'sBuild and test on Windowsjob was cancelled by the job's 20-minute timeout at test 460/482 with no failure (every test it reached,cpu_aot_switch_shift_flagsincluded, had passed); attempt 2 reran it. The same timeout cancelled both Windows jobs of the fork'smainrun at4a47793that started alongside, so it is runner time, not this change.In both Windows jobs
cpu_aot_switch_shift_flags,cpu_aot_switch_rotates,cpu_aot_jump_tables,cpu_aot_flag_conditions,cpu_aot_fuzz_smokeandcpu_aot_fuzz_framespassed. The 7 skips are the capability skipsxg_swizzle_select,xg_swizzle_select_mutation,xg_swizzle_factory,xg_swizzle_factory_no_write,bvh_wave64_warp,wave_shuffle_warpandbc7_mode6_multiblock_warp, the same in every run.Also measured on Linux x86-64, which gives the counts below: the original fixture assembled by
llvm-ml18 and linked bylld-linkinto a PE32+ DLL, mapped into executable memory and called through a SysV-to-Windows-x64 thunk and the unchangedregister_probe.asm; the translation built byclang++18 (-fms-extensions, exportsms_abi) at-O0and-O2; the check script run unchanged:unsupported flag producer at RVA 0x1003.SHR/SAR(lifted_shr1_8(0x0, 0x1)), OF taken as the result MSB forSHL(lifted_shl1_8(0x0, 0x40)).cpu_aot_switch_rotates(8,363 comparisons, 12 controls) passes;tools/fuzz_cpu.py0xA902 20 leaf(12,520 comparisons) and0xF4A3 16 frames(10,016 comparisons,reload_shiftmutant 4,945/5,008 mismatches) pass.SHL RAX, CL(theflag_conditions_check.pycontrol) still raisesunsupported flag producer ....docs_links_check.pyandform_coverage_check.py.pushfqon hardware: 2,388,000 comparisons, 0 mismatches, also under-fsanitize=undefined.Evidence and follow-up
tests/cpu/switch_shift_flags.asmis original source written for this test (MIT), registered with; coverage: source cpu_aot_switch_shift_flags. 36 exports: every condition after a count of 1 and the ten OF-free conditions after larger counts up to the width minus 1, 8/16/32/64-bit register and memory destinations, masked counts (sar al, 33,sar eax, 37,shl r10, 65), sevenJccconsumers andCMOVccafter a flag-neutralMOV, with a 32-bit destination and a memory source. 13 refused exports plus 3 directLiftercontrols.docs/experiments.mddocuments the rule and the reproduction command:Follow-up for another contributor: shifts by
CLas producers would need the earlier producer's flags when the masked count is 0.Overlap
The three PRs below touch the same lines of
tools/cpu_switch_aot.py(the docstring paragraph after the flag rules,FLAG_PRODUCERS, the start ofLifter.condition(), the branch before shifts inLifter.lower()),CMakeLists.txtaftercpu_aot_switch_rotatesanddocs/experiments.mdafter the rotates command. Each is independent and based onmain; whichever lands later needs a rebase that keeps both sides (FLAG_PRODUCERS = {..., *SHIFTS, *BIT_TESTS, *BIT_COUNTS}). The combination (e6c6b1eon4a47793, branchclaude/blissful-darwin-l83vcqof my fork) passes all three fixtures and the leaf fuzz area on Linux.Checklist
This change was AI-assisted (Claude Code), including the fixture, the reference model and the review.
🤖 Generated with Claude Code
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T03:43:29Z.
Review: COMMENTED
Reviewed
e91c53edon Linux (Windows build/test and native/AOT execution were not run locally).Diff audit
tools/cpu_switch_aot.py:SHL,SHR, andSARby an immediate are added toFLAG_PRODUCERSwhen the masked count (& 31, or& 63for 64-bit) is1 .. width * 8 - 1.CFcaptures the last bit shifted out ((wide >> (bits - n)) & 1uforSHL,(wide >> (n - 1)) & 1uforSHR/SAR);ZF,SF, andPFare computed from the width-truncatedfa.OFis modeled only forcount == 1(MSB(result) ^ CFforSHL,MSB(operand)forSHR,0forSAR), andO/NO/L/GE/LE/Gaftercount > 1are rejected withOF is undefined after a shift by more than 1.CL, masked count0, and counts>= width * 8remain rejected withunsupported flag producer.tests/cpu/switch_check.py: updates theCMP+CMOVGEnegative control fromSHL EAX, 1(d1 e0) toROL EAX, 1(d1 c0), preserving theunsupported flag producercheck.docs/experiments.md,CMakeLists.txt,tests/cpu/switch_shift_flags.{asm,def,_check.py}: consistent and title-neutral.tools/cpu_switch_aot.py,CMakeLists.txt, anddocs/experiments.mdwith #1209 and #1210; whichever lands later will need a rebase.Linux checks (
/root/.local/share/x2w-rev-venv/bin/python -B)tests/form_coverage_check.py: passedtests/docs_links_check.py: passed (15 Markdown files, 257 links)tests/bidi_controls_check.py: passed (957 files)tests/intake/{import_forms,paths,routing}_check.py: passedcpu_switch_aot.Lifter().lower(..., sets_flags=True)spot checks (6 cases:shl eax,1,shl eax,2,shr eax,32,shl al,8,shl eax,cl,rol eax,1): passed.ctest -R '^cpu_aot_switch_shift_flags$'not run locally.