cpu: lower SAL instructions in cpu_switch_aot #1526
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1533"
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 Ammmae0x on 2026-11-24T13:43:36Z.
Change
Extends
tools/cpu_switch_aot.py(SHIFTSandLifter.lower) to lower x64SAL(the ModR/M/6encoding alias ofSHLacrossdocs/coverage/x64_zen2.tsvIntegersal) across 8-, 16-, 32-, and 64-bit register and memory destinations with1,imm8, andCLcounts:SALmasks the shift count to 5 bits (6 bits for 64-bit operands), preserves upper register/memory bytes for 8- and 16-bit writes, and zero-extends 32-bit register writes.SALproducesCF,ZF,SF, andPFfor immediate masked counts1 .. width*8 - 1(andOFwhen the masked count is1), rejectingCLcounts, masked count0, counts>= width*8, andOF-reading conditions (O/NO/L/GE/LE/G) after counts> 1.tests/cpu/switch_sal.asm,tests/cpu/switch_sal.def,tests/cpu/switch_sal_check.py), registerscpu_switch_sal/cpu_switch_sal_lifted/cpu_aot_switch_salinCMakeLists.txtwithFORM_COVERAGE_RECORD, and documents measured results indocs/experiments.md.Verification
Verified on commit
1d461d0e(rebased ontoupstream/main1ca0dec1):& .\tools\dev.ps1; ctest --preset dev -N->Total Tests: 981cmake --build --preset dev --target cpu_register_probe cpu_switch_sal cpu_switch_sal_lifted cpu_switch_shift_flags cpu_switch_shift_flags_lifted-> exit 0 (0 errors, 0 warnings)cmake --build --preset release --target cpu_register_probe cpu_switch_sal cpu_switch_sal_lifted cpu_switch_shift_flags cpu_switch_shift_flags_lifted-> exit 0 (0 errors, 0 warnings)ctest --preset dev --no-tests=error -R "^(cpu_aot_switch_sal|cpu_aot_switch_shift_flags)$" -E "cpu_native_archive_" -V-> 2 passed, 0 skipped, 0 failed (cpu_aot_switch_shift_flags: 28,548 differential checks + 16 rejections;cpu_aot_switch_sal: 28,024 dev + 28,024/O2differential checks across all 12SAL/6Codeencodings + 4/O2C++ mutants + 11 MASM / 4 unit / 7 PE-patched rejections;salrecorded inform_coverage/cpu_aot_switch_sal.txt)ctest --preset release --no-tests=error -R "^(cpu_aot_switch_sal|cpu_aot_switch_shift_flags)$" -E "cpu_native_archive_" -V-> 2 passed, 0 skipped, 0 failed (same differential, mutant, and rejection counts;salrecorded inform_coverage/cpu_aot_switch_sal.txt).\out\pe-env\Scripts\python.exe -B tests/form_coverage_check.py->form coverage checks passed(exit 0).\out\pe-env\Scripts\python.exe -B tests/docs_links_check.py->Checked 16 Markdown files and 343 links.(exit 0).\out\pe-env\Scripts\python.exe -B tests/bidi_controls_check.py->Checked 1242 tracked source, script and build/CI files: no bidirectional controls.(exit 0)Evidence and follow-up
tests/cpu/switch_sal.asmis an original MIT-licensed MASM fixture written for this repository. Because ML64 assembles thesalmnemonic as/4(SHL), allSALinstructions are emitted as explicit/6byte sequences and verified viaiced_x86.Decoder(Code.SAL_RM{8,16,32,64}_{1,CL,IMM8}with zeroSHLinstructions in the translated exports).SALoutside leaf/switch-lifted functions, and title CPU code execution.Checklist
AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T15:37:07Z.
Review: COMMENTED
Technical review (Linux only; the Windows pipeline was not run). The differential test
cpu_aot_switch_salneeds the MASM-built images and Windows execution, so the numbers quoted in the docs (28,024 comparisons, the four C++ mutation controls, the PE-patched rejections) are not reproduced here.Checked out the PR head in a clean tree. What I could verify independently on Linux:
SHIFTSfeedsFLAG_PRODUCERS, the producer lookup incondition()and the shift branch oflower(); the only explicitM.SHLtests (carry position and OF for count 1) are the two the PR widens. No other place intools/cpu_switch_aot.pynamesM.SHLalone./6encodings of 8/16/32/64-bit register forms with imm8, count 1 and CL, and for[rax]memory forms,Lifter().lower()produces text identical to the matching/4(SHL) encoding, both withsets_flagsFalse and True (144 register pairs plus the memory pairs; every/6decodes asSAL). So SAL is lowered exactly as SHL, which is what the PR claims.sal eax, cl(CL may leave flags unchanged),sal al, 8(count at or above the width),sal eax, 32(masked count 0) andsal ah, 1are refused with the same reasons the test expects.sal eax, 1,sal eax, 2andsal rax, 63lower with flags; OF is computed only for count 1./6: I ran the raw/6and/4encodings natively on an x86-64 host (8/16/32/64-bit, every count 1..width-1, 200 random operands each, 23,200 runs). Result and CF/PF/ZF/SF always match, and OF matches for count 1. This supports treating/6as an alias of SHL on this CPU family. It is one host, not a statement about every CPU.Local checks of the reference model in the test:
sal_flags()(CF = bitbits-count, OF = MSB(result) xor CF for count 1) matches the lowering and the native runs above.No problems found in the diff. Small observations, none blocking:
docs/experiments.mdsays SAL is added to "SHIFTSandLifter.lower";lower()itself only changed in the carry/OF conditions. Accurate enough, just worded broadly.salas/4is the reason the fixture usesdbbyte sequences; that choice is explained in the.asmheader and is the right call, since asalmnemonic in the source would silently test/4.AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T17:17:57Z.
Review: COMMENTED
Re-review of head
e671150aea(comment only, not an approval). My earlier review was at2c8391916d.No blocking findings. Since
2c83919the head is only a merge of upstream main:git diff 2c83919 e671150is empty for tools/cpu_switch_aot.py and tests/cpu/switch_sal*; only CMakeLists.txt and docs/experiments.md differ, with upstream content.Earlier points
/6bytes in the fixture.What I ran
cpu_aot_switch_salneeds MASM-built images, a Windowsctypes.CDLLand an MSVC/O2compile, so the 28,024-comparison differential, the 4 C++ mutants and the 7 PE-patched rejections were not reproduced here./6lowers identically to/4for 14 pairs; and raw/6bytes executed natively on this x86-64 host for every count 1..bits-1 at all four widths matched the PR'ssal_flags()reference (11,600 runs: result, CF, ZF, SF, PF, and OF at count 1).shift_count(ins) >= width * 8->>: the PR's unit negatives fail withaccepted sal al, 8. Restored, green./6 == /4comparison caught it (in the PR itself only the Windows differential would). Restored, green.Non-blocking
/6lowers the same as/4") would let the Linux job catch mutation 2.${CMAKE_CXX_COMPILER}with MSVC flags (/O2 /LD /Fo /Fe); is MSVC-only intended?No overlap with other open PRs found.
Linux limits: no Windows, MASM, MSVC, DXC or D3D12 device here; LLVM 19 only (CI uses LLVM 15). Anything that needs them was not run.
AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T19:28:35Z.
Review: COMMENTED
Review at
50d0f17fa1830ac6a7d611f754bdb8405f875329(comment only, not an approval).What the change does
Adds
M.SAL(the ModR/M /6 alias of SHL) toSHIFTSintools/cpu_switch_aot.pyand to the two SHL-only branches that compute CF and OF for a count of 1 (:1332,:1334). A MASM fixture, a differential check, CMake registration and a doc section are added. The flag-consumer path (:1040) andFLAG_PRODUCERS(:259) already iterateSHIFTS, so SAL reaches them without further edits.Commands run on this branch (Linux, no Windows)
SALdecodes fromd1 f0asMnemonic.SALwith iced-x86 in the review venv.[rbx]and[rbx+8]; count 1, CL and imm 0, 1, 2, 5, 7, 15, 31, 32, 33, 63; with and without flags) I lowered the /6 bytes and the same bytes with /4 (SHL) throughLifter().lowerand compared the result. Result: 128 compared, 0 differences, including identical refusals (CL count, zero count, count >= width).origin/main'scpu_switch_aot.py: 128 of 128 differ (every SAL form refused), so the script does detect the missing feature.cpu_switch_aot.py, each caught by that script and reverted withgit checkout -- .: SAL dropped from the CF expression (40 differ), SAL dropped from the OF expression (19 differ), SAL mapped to>>(104 differ).git diff --check,tests/docs_links_check.py,tests/bidi_controls_check.py: pass.tests/cpu/switch_sal_check.pybyte-compiles..asm,.defor check file.Review of the semantics
docs/coverage/x64_zen2.tsv). I did not check that against hardware or a vendor manual.Not run
cpu_aot_switch_saland its 28,024-comparison differential: it needs the MASM-built native and lifted DLLs andcpu_register_probe, so it did not run here. The four mutated-C++ controls and the PE-patched rejection controls were therefore not run by me either; I read them.Findings
Verdict
The change is small and the SAL lowering is identical to the SHL lowering for all 128 forms I tried, including every refusal. The Windows differential still has to run before merge.
AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T02:29:45Z.
Review: COMMENTED
Review of head
65f7a05631db950caa0e1af6aed0fc3503f62276(cpu: lower SAL instructions in cpu_switch_aot).Verdict: no blocking findings. P1=0, P2=0, 1 nit.
Checks
tools/cpu_switch_aot.py(+12/-12 lines), a new fixture/check pair, a CMake test entry and a docs section.SAL(/6) is the same operation asSHL. AddingM.SALtoSHIFTS(tools/cpu_switch_aot.py:225) feeds every place that keys onSHIFTS: flag producers (:260), the Jcc/SETcc/CMOV table (:1041) and the count checks (:1316-1328). The carry and overflow selection now treats SAL like SHL (:1355,:1357). This matches the SDM: CF is the last bit out, OF is defined only for count 1, and CF is undefined for counts of the operand width or more.salas /4, so the.asmemits explicit /6 bytes withdb. That is the right way to get real /6 coverage. The check also has mutated-C++ controls (tests/cpu/switch_sal_check.py:258).git diff --checkis clean.Commands run locally (Linux)
python3 tests/docs_links_check.py->Checked 18 Markdown files and 397 links.python3 tests/bidi_controls_check.py->Checked 1422 tracked ... files: no bidirectional controls.git diff --check origin/main...HEAD-> no output, rc=0python3 tests/cpu/switch_sal_check.py-> could not run:ModuleNotFoundError: pefile. It needs the Windows-built PE andiced_x86, which are not on this Linux box. I relied on the Windows CI result above.Nit
tools/cpu_switch_aot.py:1326and:1328: the comment and the error text still say "SHL and SHR" only. Adding "SAL" would keep the refusal message accurate. It does not change behavior.AI-assisted; commands listed were executed locally.