feat(relinker): lower BZHI and BEXTR under --to-rosetta #11107
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/11107"
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-25T17:46:36Z.
What
--to-rosettalowers BZHI and BEXTR (VEX.0F38F5andF7withpp=0, both widths, register and memory sources, including RIP-relative and RSP operands), after ANDN, BLS (#10970) and the BMI2 shifts (#10983). Rosetta does not implement them, so a title reaching one stopped with an illegal instruction;docs/dev/debt/relinker.mdlisted both as not lowered.core/relinker/codegen/src/x86/BitfieldLowering.cpp(DecodeBitfield,EmitBitfield,BitfieldName) uses the sameScalarLowering.hpphelpers and 160-byte frame as the shifts: RCX is spilled to carry the control operand, two scratch registers hold the result and the mask. The control's low byte is compared against the operand size, because the SDM's behaviour there is not a masked shift count: BZHI with an index at or past the operand size returns the source unchanged with CF set (testthenstc), and BEXTR with a start at or past it returns zero (xor). On the normal path the mask is-1 << countinverted withnotand applied withand, which also produces ZF and SF from the result and clears CF and OF; for BEXTR the mask is skipped when the length is at or past the operand size, andmov cl, chmoves the length into the count register. BEXTR's SF is undefined in the SDM and follows the result here. Thepp=0opcodes PDEP, PEXT and MULX, and theF7shift forms, are left to their own decoders.The matcher registers the decoder next to the shifts (both the trampoline and the out-of-line path), the three
CMakeLists.txtsource lists get the file andbitfield_lowering_testsis added.test_to_rosetta.pygains BZHI and BEXTR relinks (unchanged bytes except under--to-rosetta, report names present);docs/user/USAGE.mdanddocs/dev/debt/relinker.mdrecord the lowering and remove both from the not-lowered lists.BitfieldLoweringTests.cpp: decoding of all 2 × 2 × 16 × 16 × 16 register forms, a prefixed memory form, rejection of SHLX, PDEP, PEXT, MULX, BLSR, VEX.L=1, trailing bytes, a truncated ModRM and an overflowing stack displacement; matching in Intel and Rosetta modes; and, on an x86-64 host, execution of every destination against 9 source/control register pairs (overlaps, RSP, RCX), 6 values and 20 control patterns (index, start and length at 0, 1, 31, 32, 63, 64, 255, high garbage above the control byte) with the flags preloaded all set and all clear, plus six memory addressing forms. The expected result and flags come from a model of the SDM entries.Tested
macOS 27 arm64, AppleClang:
ctest --test-dir build-relinker -j8257/257 (bitfield_lowering,to_rosettaincluded). Same tree built for x86_64 and run under Rosetta:ctest --test-dir build-rosetta -j8257/257, with the execution checks enabled. Arch Linux x86-64 (Zen 2, GCC 16.2.1):ctest --test-dir build-relinker -j8269/269,bitfield_lowering_testsnatively with the execution checks enabled, no warnings from the new files.The SDM model the test compares against was checked against the hardware instructions on the Zen 2 with a throwaway program: for both widths, 6 sources, the 20 control patterns and every value of the control's low byte,
bzhi/bextrresults and the defined flags (CF ZF SF OFfor BZHI,CF ZF OFfor BEXTR) matched the model on 122,880 cases, 0 mismatches. The stub itself was not compared with the hardware instruction in the same run; the debt entry says so. Not run against a title.tools/check_conventions.py --base origin/main0 errors;tools/pr_overlap.pyrun before opening.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-25T18:34:33Z.
Review: APPROVED
Reviewed exact head . The BZHI/BEXTR decoder checks VEX map/prefix/length and operand extent, and the lowering preserves register and stack operands while handling out-of-range index/start and length cases. The execution regression covers 32/64-bit forms, register overlap, memory addressing, flags where defined, and red-zone preservation. I ran the focused local suite (, , , ): 4/4 passed with execution checks enabled. Hosted relinker jobs for Linux, Windows, macOS arm64/x86-64 and ASan are green; product Linux/Windows/register-context/write-watch jobs are marked skipped, so this approval is based on the relinker coverage shown.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T08:47:54Z.
Review: APPROVED
Re-reviewed exact head
313892fa5e7b6e78afa1d1280a1fabf6ae11b797. The author reports the lowering implementation is byte-identical to the previously reviewed head; the new delta is the debt entry updated to include BZHI/BEXTR and preserve the stack order through #10970/#10983. The current-main merge preview is clean and main has no newer changes in the PR’s paths. The author reports Rosetta execution tests 487/487 and arm64 484/485, with the sole failureprogress_reportalso failing on main; Linux, Windows, and ASan hosted relinker jobs pass. The hosted macOS arm64 and x86_64 jobs remain queued. I did not rerun the suite locally.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-30T00:54:25Z.
Review: APPROVED
Re-checked — new head, looks good
The requested stack-result epilogue now loads the computed RSP destination from its saved frame slot before leaving the frame, and the updated debt text accurately limits what the tests establish.
What I ran
Reviewed exact head
f5e1bbf4a52ea48ec79adb08c01bb9eae7860c5eagainst currentmainf6fddd60d826831f70f7c62e25423610ee742bcc; the merge preview was clean (dada4b5f129f367a197cf188a48c15f2391a0fdb). Environment: Linux; I did not build or run the tests locally.Results
--to-rosettadebt paragraph, 2,094 characters at this head (2,023 on current main). The separate BZHI/BEXTR entry is 997 characters.progress_reportis the arm64 failure and is reported to fail on main too. I did not rerun these suites.Unedited local check output
Negative control
bitfield_loweringfails; I did not independently run this mutation.Findings
Blocking
None found in the requested epilogue and debt-entry update.
Non-blocking
The convention warning is on the existing combined
--to-rosettaparagraph, which is 2,023 characters on currentmain; this PR adds 71 characters to it. The new BZHI/BEXTR entry itself is below 1,000 characters.Sources
Not verified
No local build, execution suite, or title run was performed. The hardware model comparison and cross-platform test results are author-reported; the hosted relinker matrix passed on the exact reviewed head.