Repository navigation
cpu: lower LOOP, LOOPE, LOOPNE, JRCXZ and their ECX forms - #403
Merged
Merged
Conversation
This was referenced Nov 22, 2026
Lower the RCX-counted branches in the shared lifter as a decrement and a conditional goto. None of them changes a flag; LOOPE and LOOPNE test ZF after the decrement, and JRCXZ only tests the count. The 67h forms (loopd, looped, loopned, jecxz) count in ECX, and their decrement is a 32-bit write that clears the upper half of RCX, which the native fixture confirms. cpu_aot_rcx_branches builds eight exports from MASM source and compares 7712 cases of RAX, RCX, the final pointer and ZF/CF across the native DLL, the translation and a Python reference. Three mutated translations (the ECX forms keeping the upper half of RCX, LOOPE/LOOPNE ignoring ZF, and JRCXZ inverted) must mismatch. AI-assisted (Claude Code).
denn0x
force-pushed
the
cpu/lower-rcx-branches
branch
from
November 22, 2026 02:07
4d377a8 to
ed260fa
Compare
Contributor
Author
|
Rebased 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 RCX-counted branches are an independent problem:
lower()rejectsloop,loope,loopne,jrcxzandjecxztoday, and no open PR or issue claims them. There is no prerequisite PR.The shared interface is
lower()intools/ffx_version_query.pyandCPU_FIXTURESintools/form_coverage.py. #287, #289, #291, #326, #328, #331, #335, #368, #379 and #386 also extend these. This change adds oneelifbranch at an anchor those PRs do not use. It also adds the sameCode as Cimport as #368, which merges as an identical change. A trial merge of #368, #379 and #386 into this branch is clean, and the combined tree passes all 46cpu_tests.Lowering.
loop,loopeandloopnelower to a decrement and a conditionalgoto.jrcxzlowers to a test of RCX.LOOP_REL8_64_ECX,LOOPE_REL8_64_ECX,LOOPNE_REL8_64_ECX,JECXZ_REL8_64). Their decrement is a 32-bit write, which clears the upper half of RCX. The native fixture confirms this on cases with noise in the upper half.loopeandloopnetest ZF after the decrement.Fixture
tests/cpu/rcx_branches.asmbuilds eight exports.emitasserts that each one contains the expected icedCode, so a misassembleddb 67hprefix cannot pass silently.loop_count,loopd_count: these count iterations withlea, which leaves flags alone, after one of four ZF/CF presets. The presets must surviveloop. Counts are 1 to 40 and 1000. Forloopd, RCX also carries upper halves of 0,0xabcdand0xffffffff.loope_scan,loopne_scan,looped_scan,loopned_scan: byte scans guarded byjrcxzorjecxz. They cover counts 0 to 24, seven start offsets and five values. Over the cases, they exit on the first element, on a later one and at count exhaustion. The ECX scans also get upper-half noise.jrcxz_probe,jecxz_probe: counts that differ only in the upper half of RCX, with each flag preset.tests/cpu/rcx_branches.py checkcompares RAX, RCX, the final pointer offset and ZF/CF for 7712 cases. Each case runs against the native DLL, the translation and a Python reference.Negative controls. Three translations of the same image are mutated after lowering, and each must mismatch:
noecxjecxztests all of RCXnozfloopeandloopneignore ZFjrcxzjrcxzandjecxzinvertedThe scan buffer ends in a zero byte. A control that skips the zero-count guard therefore still stops inside the buffer.
A first version of
noecxmade the ECX loops count in all of RCX. On the upper-half cases that runs for about 2^47 iterations, and the test hung, so the control now keeps the loop on ECX.On
main,lower()rejectse2 fe c3withunsupported query operation at 0x1000: loopande3 fe c3with... jrcxz.Verification
Tested at
4d377a8on4f6c39d. 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). The run includes the GPU tests. The failure isgpu_alias_lookup_writes_hardware, the RDNA 4 array-atomics probe tracked in gpu_alias_lookup_writes_hardware: array atomics probe reads 256 per buffer on RX 9070 XT #369, which fails the same way onmain. This change touches no GPU code.tests/form_coverage_check.pyandtests/docs_links_check.pypass.4d377a8in the fork'sWindows checksrun 44966447068.Coverage.
python tools/form_coverage.py out/test-results.xmlgives x64 60 of 948 forms, 55 withoutcpu_aot_rcx_branches. The five added forms areloop,loope,loopne,jrcxzandjecxz.Not measured: an operand-size (66h) prefix on these branches. iced decodes
66 e2as the plain 64-bit form, and the lowering, like the existing conditional jumps, does not treat it differently. I did not measure a prefixed branch.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).