feat(relinker): lower BLSI, BLSMSK and BLSR under --to-rosetta #10970
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/10970"
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 boykopovar/AnyPS5. Originally posted by AdnanKhan-ak47 on 2026-10-25T11:38:41Z.
What
--to-rosettalowers the three single-source BMI1 instructions thatdocs/dev/debt/relinker.mdlists as faulting with SIGILL under Rosetta on an M2 with macOS 14.8.9: BLSR (x & (x - 1)), BLSMSK (x ^ (x - 1)) and BLSI (x & -x), in their 32- and 64-bit register and memory forms, through the same out-of-line trampoline ANDN uses (BlsLowering.cpp;VEX.0F38 F3 /1, /2, /3).The stub spills two scratch registers below the 128-byte red zone, loads the source, and tests it. The nonzero branch computes the result with LEA and AND, XOR, or MOV/NEG/AND, so SF and ZF come from the result and CF and OF clear; the zero branch produces the SDM result directly (0, all ones, 0). STC on the branch whose CF the SDM defines as set gives CF for a zero source (BLSR, BLSMSK) or a nonzero source (BLSI); BLSMSK's ZF clears as the SDM states. RSP as source or destination and RIP-relative and stack-relative memory operands follow ANDN, including the throw for a stack displacement that no longer fits after the spill frame.
ScalarLowering.hppholds the encoding, scratch and memory-operand helpers ANDN had in its own file, so both lowerings share them;AndnLowering.cppproduces the same bytes as before.tests/LoweringExecution.hppholds the ANDN execution harness, generalised over the emitted stub, andBlsLoweringTests.cppruns decoding for every register pair and width, the memory-operand forms, the rejections, Intel versus Rosetta matching for all three names, and on an x86-64 host executes 3 operations × 2 widths × 16 destinations × 4 sources (including RSP and destination = source) × 8 values plus 6 memory forms × 3 values, checking the result, every other register, CF/ZF/SF/OF and the red zone. The harness caught a first version that stored the RSP-destination result in the red zone.docs/user/USAGE.mdlists the lowering;docs/dev/debt/relinker.mdrecords it and the BMI1/BMI2 instructions still not lowered.Tested
macOS 15 arm64 relinker-only build, AppleClang 21:
ctest255/255 (decoding and matching; the execution checks are compiled out off x86-64). Arch Linux x86-64, GCC 16.2.1, relinker-only build:andn_loweringandbls_loweringpass with the execution checks on the host CPU.tools/check_conventions.py --base origin/mainandtools/pr_overlap.pyclean. Not run on a Rosetta host or against a title.Current head
4ff00abf28(rebased onmainf7f03daf52), on an Apple M4 with macOS 27.2: the x86_64 relinker build run under Rosetta passes 514/514, withbls_loweringexecuting every lowered BLSI, BLSMSK and BLSR stub under Rosetta and comparing results, flags and untouched registers with the SDM model. Negative control: making the BLSMSK stub use OR instead of XOR failsbls_loweringthere.to_rosettachecks that--to-rosettaoutput no longer contains the three encodings. The SIGILL itself is not reproduced: Rosetta on this M4 executes BLSI, BLSMSK and BLSR natively, as on the M5 Max in the debt entry, and no host with the faulting Rosetta (M2, macOS 14.8.9) was available. Not run against a title.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T11:48:24Z.
Review: APPROVED
Reviewed the VEX decoding, operand and prefix handling, scratch-register preservation, stack/RIP-relative memory rewriting, result values, and the defined CF/ZF/SF behavior for BLSI, BLSMSK, and BLSR. The regression suite exercises all register encodings and widths, aliasing, stack and memory operands, malformed encodings, flag results, and red-zone preservation. GitHub relinker checks passed on Ubuntu, macOS (including x86_64), Windows, and ASan. I did not run the suite locally.
Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-10-25T12:40:33Z.
Review: APPROVED
Checked head 1c6d7e4 merged onto main
29ce6d8(clean merge), Linux x86-64 WSL2, GCC 13.3, relinker-only build.What I ran:
bls_lowering,andn_lowering,amd64_only_converterpass; full relinker ctest 267/267 on the merge, 266/266 on main (the only diff is the newbls_lowering).andn_loweringpasses on main too, so the ScalarLowering move kept ANDN green.LoweringExecution::Executionharness: once as the original BLSR/BLSMSK/BLSI bytes on the host CPU (BMI1), once asEmitBlsoutput. It compares all 16 registers, CF/ZF/SF/OF, and the red-zone sentinels.[rsp+d8],[rsp+rcx*8],[rsp+r12],[r12+d32],[r13+0],[r9+r10*8], no-base SIB with VEX.B 0/1, RIP with VEX.B=1,cs:,fs:, and twoaddr32forms with garbage in the upper halves of the registers.cmc, an r11 clobber, a red-zone write) were each caught on every case, so the probe can see these failures.tools/check_conventions.py --base origin/main: 0 errors, 1 warning (debt-length on relinker.md:38, which was already over the limit on main)._matchRosetta,_emitOutOfLine); the Intel target is untouched.Not checked: a Rosetta host or a title. AF/PF are not compared (undefined per the SDM).
Two small non-blocking things, take or leave:
core/relinker/relinker/tests/test_to_rosetta.py:57relinks ANDN through the CLI and checks that "ANDN" shows up in the report. A BLS case there, e.g.c4e2f8f3ce(blsr rax, rsi) +c3, would cover the converter/report path the unit tests don't.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T14:29:27Z.
Review: APPROVED
Re-reviewed the latest head after the CLI/report test and debt clarification were added. The three encodings in
test_to_rosetta.pymatch BLSR (/1), BLSMSK (/2) and BLSI (/3), and the test checks both Linux/macOS target selection and the three conversion modes, requiring unchanged instruction bytes except under--to-rosettaand a matching report name when lowered. The debt note now records the same disp32-overflow and fault-delivery limits as the ANDN lowering.I configured a relinker-only Release build with GCC 12.2, built
relinker, and ranctest -R to_rosetta: 1/1 passed.git diff --checkandtools/check_conventions.pypass. The hosted Ubuntu, macOS arm64/x86_64, Windows and ASan relinker jobs also pass. I found no remaining blocking issue.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T09:25:56Z.
Review: CHANGES_REQUESTED
Re-review — changes requested for current-main integration
The exact-head report on
8608ef692caddresses the platform test update: Rosetta reports 487/487 with execution checks enabled, and arm64 reports 484/485 with onlyprogress_reportfailing for the same Python-version reason on main. I found no new source-level issue in the scoped BLSI/BLSMSK/BLSR lowering from this update; I did not rerun the relinker suite locally.The branch is based on main
4c7d275bef, while current upstream main isaaccd0ba39b7. Please refresh this base and rerun the focused lowering tests and conventions on the exact new head. #10983 is stacked through this PR, so update that branch after this base is settled and preserve both debt entries and test registrations across the stack.Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-11-01T19:48:40Z.
Review: COMMENTED
The decoding and execution tests are solid, but the PR says "Not run on a Rosetta host or against a title". The debt entry this removes states the instructions fault with SIGILL under Rosetta, so the claim that the lowering fixes that is unconfirmed here. A run of the lowered output on a Rosetta host would settle it.