feat(relinker): lower SHLX, SHRX, SARX and RORX under --to-rosetta #10983
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/10983"
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-25T12:21:39Z.
What
--to-rosettalowers the BMI2 shifts SHLX, SHRX and SARX (VEX.0F38 F7,pp1, 3, 2) and the rotate RORX (VEX.F2.0F3A F0 ib), in their 32- and 64-bit register and memory forms, through the same out-of-line trampoline ANDN and BLS use (ShiftLowering.cpp). Balatro (PPSA21402, #1142) stopped onshlxq %rax, %rdx, %r8right after the ANDN lowering landed (#5784); this is that instruction, but the title has not been run with this change.Unlike ANDN and BLS, these four leave RFLAGS untouched where
shl,shr,sarandrorwrite them. The stub reserves a 160-byte frame, two slots more than the 144-byte one so that four slots sit below the red zone, saves the flags withpushfqinto the third slot, spills the scratch result register and RCX, loads the source (memory and RSP forms as ANDN), moves the count into RCX, shifts by CL or rotates by the immediate, writes the destination (through the RCX spill slot when the destination is RCX, through the fourth slot when it is RSP), thenpopfqs the saved flags and restores the spills. The source is loaded before RCX is overwritten, so a memory operand based on or indexed by RCX reads the caller's value. Both the BMI2 instruction and the scalar one mask the count to the operand size. BEXTR is thepp=0form of the same opcode and is left unmatched.ScalarLowering.hpp'sMemorytakes the frame size so a lowering can use a larger frame.tests/LoweringExecution.hppnow loadsState::InputFlagsinto RFLAGS before the stub and takes the number of bytes after a RIP-relative displacement, so RORX's immediate relocates correctly; the ANDN and BLS tests now run with all four defined flags set on entry and still pass, which proves they define rather than inherit them.ShiftLoweringTests.cppdecodes every register triple and width, the memory and RORX immediate forms, the rejections (VEX.L, BEXTR, wrong map, non-1111vvvvon RORX, missing or extra bytes), checks Intel versus Rosetta matching, and on an x86-64 host executes the three shifts × 2 widths × 16 destinations × 9 source/count pairs (RSP and RCX as source, count and destination, aliases) × 6 values × 7 counts (0, 1, 31, 32, 63, 64, 255) × 2 entry flag states, RORX × 6 immediates, and 6 memory addressing forms × 4 operations × 3 values, checking the result, every other register, that CF/ZF/SF/OF equal the entry flags, and the red zone.docs/user/USAGE.mdlists the lowering;docs/dev/debt/relinker.mdrecords it and the BMI instructions still not lowered (BEXTR, BZHI, PDEP and PEXT; MULX is lowered since #12255). The summary entries list MULX with the lowered instructions.Tested
macOS 15 arm64 relinker-only build, AppleClang 21:
ctest256/256 (decoding and matching; execution compiled out off x86-64). Arch Linux x86-64, GCC 16.2.1, relinker-only build:andn_lowering,bls_loweringandshift_loweringpass with the execution checks on the host CPU; the harness caught, in order, a flags slot inside the red zone, a stale expected value for an RSP source, and the RORX displacement patch before anything was pushed as final. Not run against a title.Current head
7f0b73575b(rebased onmainf7f03daf52), on the stack top6ad23bffdd, which contains this PR: macOS 27.2 (M4) x86_64 under Rosetta 516/516 with the execution checks on; arm64 513/513. Arch Linux not rerun after the rebase (506/506 on the previous headf5e1bbf4a5).tools/check_conventions.py --base origin/main: 0 errors, one warning, the length of the--to-rosettaentry inrelinker.md, which is already over the limit onmain.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)core/relinker/CMakeLists.txt,Amd64OnlyInstructionMatcher.cppanddocs/dev/debt/relinker.md, resolved by keeping both sides; this branch is rebased when they land)shlxqstop reported there; not run on the title)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T12:41:54Z.
Review: CHANGES_REQUESTED
The macOS x86_64 hosted job currently fails at the new
shift_loweringtest: the other 257 tests in that run pass, but CTest reportsSubprocess abortedfor test 90 and exits 8. I built and ranshift_lowering_testsfrom this exact head (b52a9b04ba718666e3982dc701a962f73b4d3bc5) on Linux x86_64; it passes there, so I cannot reproduce the abort locally.Since this PR explicitly has not been tested on a Rosetta host and this new test is what makes the macOS x86_64 job fail, please investigate the abort under that runner and either fix the incompatibility or gate only the execution portion when the host cannot run it, while retaining decoding/matching coverage. Please rerun the macOS x86_64 check before merge.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T14:04:15Z.
Review: CHANGES_REQUESTED
The RORX immediate/register-set fix resolves my earlier macOS x86_64 test concern. I independently configured and built the relinker-only Release tree;
andn_lowering,bls_loweringandshift_loweringpass locally. Conventions and whitespace pass. The full local suite completed with 266/268 passing;masked_store_loweringandamd64_only_converterboth terminate withIllegalon this host. Hosted Linux, Windows, macOS arm64, macOS x86_64, ASan, intake, eligibility, labels and conventions checks now pass; product-only jobs were skipped.One integration blocker remains. I reran
tools/pr_overlap.pyagainst current maine05ba6feand confirmedgit merge-treereports content conflicts with open #7173 incore/relinker/CMakeLists.txt,Amd64OnlyInstructionMatcher.cppanddocs/dev/debt/relinker.md; #7531 reports the same locations because it is stacked on #7173. #10861 conflicts indocs/dev/debt/relinker.md. I manually checked the scopes: ADX lowering and code-scan-boundary analysis are separate from these BMI shifts, so this is merge-order/integration work, not duplicate implementation. Please coordinate an order with the affected authors and record the merge-only dependencies (or the agreed integration plan) before merge, preserving the matcher registrations, test registrations and debt entries from each branch. #7531 already depends on #7173, so account for that follow-up when choosing the sequence.Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-29T12:54:09Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
The SHLX/SHRX/SARX/RORX lowering merges cleanly, builds, passes its tests, and the tests fail under four mutations. One blocking item remains: the new debt entry says MULX is not lowered, but MULX lowering (#12255) is already in this branch's base.
What I ran
25714e1ca83e81848e23ad8a31c72acc4d02da94(stacked on #10970 at3e29bdc6506d85d8d2875a344352646a504beced)main:aab650b4d39785f5af1402612d419bc52b839c07; the merge with the head was clean.BUILD_TESTING=ON, in a clean worktree.Results
maingit diff --checkcheck_conventions.pydebt-lengthonrelinker.md:48, which predates this PR, and on the new entry atrelinker.md:56)shift_lowering,bls_lowering,andn_lowering,mulx_lowering,amd64_only_converter,to_rosettashift_loweringtakes 0.09 s with the execution checks onctest
check_conventions.py
Negative control
The production code here is all new, so restoring it from
mainwould only break the build. I applied four separate mutations to the production files instead, rebuiltshift_lowering_testsafter each one, and restored the file before the next:DecodeShifthooks from_matchRosettaand_emitOutOfLineRosetta mode did not require a trampoline for SHLXpushfq/pop [rsp+16]/push [rsp+16]/popfqwith non-flag movesSHLX did not preserve the flags25714e1ca8RORX register set (immediate passed toUsedRegisters)register index 16 is out of range/7changed to SHR/5SARX 32 dst 0 src 6 count 2: register 0 is 1073741824, wanted 3221225472After the files were restored,
shift_loweringpassed again.Findings
Blocking
docs/dev/debt/relinker.md:55-56both end with "BEXTR, BZHI, MULX, PDEP and PEXT are not lowered". The PR description says the same. MULX lowering landed in #12255, and that merge (4004c97114) is an ancestor of this branch's base5454cd2e45. At this head the matcher already dispatches it (Amd64OnlyInstructionMatcher.cpp:287), andrelinker.md:5has its own entry. Line 56 is new in this PR, and line 55 is the BLS entry this PR rewrites. Both now give a wrong list of remaining gaps. Fix: drop MULX from both lists ("BEXTR, BZHI, PDEP and PEXT are not lowered"), and from the description.Non-blocking
relinker.md:48reads "ANDN, BLSI, BLSMSK, BLSR, SHLX, SHRX, SARX and RORX are lowered, but … the other BMI instructions still depend on the host". Line 49 reads "other than ANDN, BLSI, BLSMSK, BLSR, SHLX, SHRX, SARX, RORX, TZCNT and LZCNT". This gap comes from #12255, not from this PR. Since both lines are already rewritten here, adding MULX would make them match the code.relinker.md:56is 1123 characters, socheck_conventions.pywarns on it. The description mentions only therelinker.md:38warning. The sentence "Balatro (PPSA21402, #1142) stopped onshlxqafter the ANDN lowering; this lowering has not been run on that title." is a note about one title, which belongs in the PR, not the debt file. Removing it leaves 1006 characters, so a little more trimming would clear the warning.ShiftLowering.cpp:126-128emitslea rsp, [rsp+160]and thenmov rsp, [rsp-136]. Between those two instructions the slot lies 8 bytes below the 128-byte red zone, so an asynchronous signal frame delivered there could overwrite it before it is read. One instruction,mov rsp, [rsp+24](mov esp, [rsp+24]for the 32-bit form), produces the same final RSP with no such window. The ANDN and BLS stubs use the same two-instruction pattern (AndnLowering.cpp:59-61,BlsLowering.cpp:129-131), and an RSP destination for SHLX/RORX is unlikely in compiled code, so this need not block. It could be fixed here for the new stub or in a follow-up for all three.I checked the rest of the code against the Intel SDM encodings and found nothing wrong:
ppmapping (66 = SHLX, F3 = SARX, F2 = SHRX, F2.0F3A F0 = RORX), with BEXTR atpp=0left unmatched;/4/5/7/1digits;[rdx+rcx*8]with the count in RCX);pop m64/push m64with an RSP base (the effective address is taken after or before the RSP update respectively).Regarding the earlier review, I confirm the RORX
UsedRegistersfix from MarcZX10's first review: the third mutation above reproduces the original out-of-range index as a cleanCodegenException. The open merge-order coordination with #7173 and #7531 I leave to that thread.Not verified
To get this to a good state
docs/dev/debt/relinker.md:55-56and from the PR description.lea rsp+mov rsp, [rsp-136]pair with a singlemov rsp, [rsp+24]for the RSP destination.Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-29T13:34:06Z.
Review: APPROVED
Re-checked — approved
The new commit
4e207d1264drops MULX from the "not lowered" lists and adds it to the two summary entries. It also shortens the shift debt entry to 993 characters with the title note removed, and restores an RSP destination with a single load. Every finding from my previous review is resolved, and the merged tree builds and passes the relinker tests.What changed since my last review
8dddfe4200, which adds the same RSP-epilogue change and MULX corrections to the BLS stub.git range-diffshows the three shift commits unchanged apart from the debt text, plus one new fix commit:4e207d1264.4e207d1264changesShiftLowering.cpp(epilogue) anddocs/dev/debt/relinker.md(lines 48, 49, 55, 56).relinker.md:48warning, and the Tested section has runs on the stack topf5e1bbf4a5.What I ran
4e207d12648ff3e06b2c42673e9d2022829d224825714e1ca83e81848e23ad8a31c72acc4d02da94main:be2242b8c817895cf5ebc556b945d90e1c1198c7. The merge with the head was clean.BUILD_TESTING=ON, in a clean worktree.maingit diff --checkcheck_conventions.pydebt-lengthonrelinker.md:48, whichmainalready has)shift_lowering,bls_lowering,andn_lowering,mulx_lowering,amd64_only_converter,to_rosettactest
Mutations of the new epilogue: I rebuilt after each one and restored the file before the next.
ShiftLowering.cpp:127loads RSP from the flags slot instead ofkStackResultSlotshift_loweringfails:SHLX 32 dst 4 src 6 count 2: register 4 is 2755, wanted 0BlsLowering.cpp:130loads RSP from slot 0 instead of 8bls_loweringfailsShiftLowering.cpp:126-129reverted to the previouslea rsp+mov rsp, [rsp-136]pairshift_loweringpasses, as expected: the two forms give the same final RSP, and the signal window cannot be observed in this testAfter the restores, both tests passed again. This confirms the author's report that a wrong-slot mutation fails the lowering tests.
Previous findings
relinker.md:55-56now ends with "BEXTR, BZHI, PDEP and PEXT are not lowered" (the shift entry adds "(thepp=0form of the same opcode)" after BEXTR). The description is corrected.debt-lengthwarning left is the one on line 48 thatmainalready has.ShiftLowering.cpp:126-129now emits a singlemov rsp, [rsp+24](mov espfor the 32-bit form, which zero-extends as the instruction requires) while the frame is still reserved, and skips thelea. For a stack destination, the result is written to slot 24 with the operand width at line 117 and read back with the same width, so the final RSP is unchanged.BlsLowering.cpp:129-132gets the same change through #10970. Leaving ANDN onmainfor a separate change is reasonable.New findings
Blocking
None.
Non-blocking
None.
Not verified
f5e1bbf4a5; I accept these as author-reported.