cpu: lower POPCNT, LZCNT and TZCNT in cpu_switch_aot #1203
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1210"
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:34:03Z.
Change
tools/cpu_switch_aot.py(the lifter behindtools/exe_aot.py's console-stdio profile and the CPU fuzz smoke from #894) rejectedPOPCNT,LZCNTandTZCNT. They now lower for 16-, 32- and 64-bit register and memory sources through three portablePRELUDEhelpers (count_ones,leading_zeros,trailing_zeros), and act as flag producers:LZCNTandTZCNTreturn the operand width for a zero source.POPCNTsets ZF for a zero source and clears OF, SF, AF, CF and PF (Intel SDM Vol. 2B "POPCNT"), so all 16 conditions may read it.LZCNTandTZCNTset CF for a zero source and ZF for a zero result, and leave OF, SF, AF and PF undefined (SDM "LZCNT", "TZCNT"). OnlyB,AE,E,NE,BEandAmay read them; other conditions are refused withOF, SF and PF are undefined after LZCNT|TZCNT.BSFandBSR, whose destination the SDM leaves undefined for a zero source, stay unsupported.tools/cpu_sse_aot.pyreusesLifter, so its GPR path gets the same lowering.Verification
Tested commit:
f1d2c95, one commit on4a47793(upstreammain).Windows CI on my fork,
Windows checksworkflow onf1d2c95,windows-2025runner, MSVC 19.52.37029.0 and ML64 14.52.37029.0: https://github.com/dicnunz/Xbox2Windows/actions/runs/45311584822In both Windows jobs
cpu_aot_switch_bit_counts,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.TZCNTof 0 one short of the width (lifted_tzcnt16_reg(0x0, 0x0)), CF taken from the result afterLZCNT(lifted_lzcnt16_reg(0x0, 0x0)).tools/fuzz_cpu.py0xA902 20 leaf(12,520 comparisons) and0xF4A3 16 frames(10,016,reload_shiftmutant detected) pass with this change.A host whose CPUID lacks POPCNT, BMI1 or ABM runs
LZCNT/TZCNTasBSR/BSF, so the native original would not match the reference there. The fixture exportshost_bit_counts(native only, CPUID leaves 1, 7 and 0x80000001), and the test returns 77 (SKIP_RETURN_CODE), ascpu_aot_bit_opsdoes.Evidence and follow-up
tests/cpu/switch_bit_counts.asmis original source written for this test (MIT), registered with; coverage: source cpu_aot_switch_bit_counts. 22 exports: the three instructions at 16/32/64 bits with register and memory sources, every permitted condition throughSETcc,JB,JEandJNE, andCMOVAand a 32-bitCMOVLEafter a flag-neutralMOV. 9 refused exports, includingBSFandBSR.docs/experiments.mddocuments the rule and the command: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:32Z.
Review: COMMENTED
Reviewed
f1d2c956on Linux (Windows build/test and native/AOT execution were not run locally).Diff audit
tools/cpu_switch_aot.py: lowersPOPCNT,LZCNT, andTZCNTfor 16/32/64-bit register or memory sources (note(1)hoists memory reads;self.writehandles 32→64-bit zero-extension and 16-bit upper-byte preservation).LZCNT/TZCNTof0returnsizeof(T) * 8.POPCNTsetsZFfromsource == 0and clearsOF/SF/AF/CF/PF(all 16 conditions supported).LZCNT/TZCNTsetCFfromsource == 0andZFfromresult == 0, restricting conditions toB/AE/E/NE/BE/Aand rejectingOF/SF/PFconditions.BSF/BSRremain unsupported.CMakeLists.txt: configuresSKIP_RETURN_CODE 77for hosts lackingPOPCNT/BMI1/ABM.docs/experiments.md,tests/cpu/switch_bit_counts.{asm,def,_check.py}: consistent and title-neutral.tools/cpu_switch_aot.py,CMakeLists.txt, anddocs/experiments.mdwith #1208 and #1209; 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 (5 cases:popcnt,lzcnt,tzcnt,bsfrefused,bsrrefused): passed.ctest -R '^cpu_aot_switch_bit_counts$'not run locally.Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T04:21:53Z.
Independent Windows retest on commit
f1d2c956592ec42d8741a2ee53b4ab0f386e07b2(pull/1210/head):Environment
14.44.35207(cl.exe/ml64.exex64)WARP)Commands & Results
Debug (
devpreset):Result: 2/2 passed:
cpu_aot_jump_tables: Passed (1.11 sec)cpu_aot_switch_bit_counts: Passed (0.66 sec; 9 rejection controls passed, 19734 differential comparisons passed for POPCNT, LZCNT and TZCNT at 16/32/64 bits)Release (
/O2,releasepreset):Result: 2/2 passed:
cpu_aot_jump_tables: Passed (1.05 sec)cpu_aot_switch_bit_counts: Passed (0.55 sec; 9 rejection controls passed, 19734 differential comparisons passed for POPCNT, LZCNT and TZCNT at 16/32/64 bits)0 failed, 0 skipped.