cpu: lower LOOP, LOOPE, LOOPNE, JRCXZ and their ECX forms #397
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/403"
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 denn0x on 2026-11-22T00:27:32Z.
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 #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).
Imported from R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-22T02:08:01Z.
Rebased onto
c2fc84cased260fa, resolving thedocs/experiments.mdoverlap with #415 that I mentioned in both PRs. The RCX-branch paragraphs now follow the XLATB paragraph after the address-size text, with no change to either text.ctest --preset dev --no-tests=error -R '^cpu_' -j 8gives 46 of 46 passed, includingcpu_aot_rcx_branchesandcpu_aot_xlatb.tests/docs_links_check.pypasses.AI-assisted (Claude Code).