Repository navigation
cpu: lower x64 string instructions in the shared lifter - #368
Merged
Merged
Conversation
This was referenced Nov 21, 2026
denn0x
force-pushed
the
cpu/lower-string-instructions
branch
from
November 22, 2026 02:07
a70c9ec to
791dbcb
Compare
Contributor
Author
|
Rebased onto AI-assisted (Claude Code). |
Lower MOVS, STOS, LODS, SCAS and CMPS in all four widths, bare and under REP, REPE and REPNE, together with CLD and STD. Dispatch is by iced Code because the SSE2 scalar MOVSD and CMPSD share the string mnemonics. Each iteration moves or compares one element and steps RSI and RDI by the width, downward when DF is set, so overlapping copies replicate as on the hardware. RCX is tested before each element and ZF after it, and a zero count leaves pointers, memory and flags unchanged. The 67h forms, REPNE on MOVS, STOS and LODS, and FS or GS sources are rejected. cpu_aot_string_ops builds 62 exports from MASM source and compares 39984 cases of memory, RAX, RSI, RDI, RCX, ZF and CF across the native DLL, the translation and a Python reference. Three mutated translations (memmove for REP MOVS, swapped CMPS operands, STD ignored) must mismatch. AI-assisted (Claude Code).
denn0x
force-pushed
the
cpu/lower-string-instructions
branch
from
November 22, 2026 02:45
791dbcb to
5b2fd02
Compare
Contributor
Author
|
Rebased again onto AI-assisted (Claude Code). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Change
Advances CPU translation coverage. The x64 string instructions are an independent problem: none of
movs,stos,lods,scas,cmps,cldorstdlowers today, and no open PR or issue claims them. There is no prerequisite PR. The shared interface islower()intools/ffx_version_query.pyandCPU_FIXTURESintools/form_coverage.py, which #287, #289, #291, #326, #328, #331 and #335 also extend. This change adds oneelifbranch, a separatestring_op()helper and a conditionalbool directiondeclaration on its own line. It leaves thezero/carrydeclaration and the existing branches untouched, so whichever of these lands later needs only a short merge.Lowering.
Code, not mnemonic. The SSE2 scalarmovsdandcmpsdshare their mnemonics and stay unsupported.rep movsbwith RDI = RSI + 1 therefore replicates the first byte as on the hardware.cmpscomputes [RSI] − [RDI] andscascomputes the accumulator − [RDI]. Like the rest oflower(), only ZF and CF are modelled. Whichever change later adds OF, SF or PF tolower()should extendscasandcmpstoo.lodsbandlodswmerge into RAX;lodsdzero-extends.cldandstdset adirectionflag that starts clear. The x64 software conventions expect DF clear on function exit and on entry to runtime and system calls.movs,stosandlods;Fixture
tests/cpu/string_ops.asmbuilds 62 exports:movs,stosandlodsunder REP;scasandcmpsunder REPE and REPNE;Each export takes destination, source, count and a state block. It sets RDI, RSI, RCX and RAX from them and sets the flags from a preset: CF only, ZF only, or neither. It reports ZF, CF, RAX, RSI, RDI and RCX. RSI and RDI are callee-saved and kept in R10 and R11.
tests/cpu/string_ops.py checkruns 39984 cases. Each one goes to the native DLL, the translation and a Python reference, which executes one element at a time.0x5a, between two 1 KiB guards. Memory is compared over the guards too.Negative controls. Three translations of the same image are mutated after lowering, and each must mismatch:
memmovemovsbecomes one whole-rangestd::memmoveswappedcmpsoperands swappedforwardstdignoredThe check also confirms each rejection by its diagnostic, using synthetic bytes:
67 f3 a4,67 ae,f2 a4,f2 48 ab,f2 ac,64 a4, and the SSE2movsd xmm0, xmm1andcmpeqsd. Onmain,lower()rejects the same bytes, for examplef3 a4 c3withunsupported query operation at 0x1000: rep movsb, soemitcannot translate the fixture.Verification
Tested at
a70c9econ9bdbb20. Windows 11 build 26200, MSVC 19.50.35720, Windows SDK 10.0.26100.0, Python 3.13, AMD Ryzen 7 7700 (Zen 4).ctest --preset dev --no-tests=error --output-junit <repo>/out/test-results.xmlgives 144 passed, 1 failed and 2 capability skips (bvh_wave64_warp,wave_shuffle_warp, as onmainon this machine). The run includes the GPU tests. The failure isgpu_alias_lookup_writes_hardware, which gpu: skip the title-address write fixture where array atomics are inexact #332 changed onmainjust before this rebase. This change touches no GPU code. On the RX 9070 XT (driver 32.0.32015.7029, debug layer enabled), the probe printsarray_atomics ... expected=64 actual=256,256,256,256, while the fixture's own add/sub and other checks reportmismatches=0. I'll follow that up separately. Before the rebase, onbc24dfd, the same suite gave 145 passed.ctest --preset dev --no-tests=error -R '^cpu_'gives 43/43 passed, including the existingcpu_lower_prefixesencodings.tests/form_coverage_check.pyandtests/docs_links_check.pypass.a70c9ecin the fork'sWindows checksrun 44962440518.Coverage.
python tools/form_coverage.py out/test-results.xmlgives x64 77 of 948 forms. The same JUnit file withoutcpu_aot_string_opsgives 55. The 22 added forms aremovs,stos,lods,scasandcmpsin four widths, pluscldandstd. The reference listsmovsdandcmpsdonce each, under Integer. A future SSE2 fixture that uses scalarmovsdwould therefore count the same form.Measured, not established: the fixture checks architectural results on one host CPU. It does not cover RCX wrap at 2^64, faults part-way through a repeated instruction, the restart behaviour after an interrupt, or the flags this lowering does not model (OF, SF, AF, PF).
Evidence
The fixture is synthetic and original to this repository (MIT).
docs/experiments.mddocuments the lowering, the fixture and its controls.Checklist
AI-assisted (Claude Code).