fix(relinker): let short SHA and CLZERO instructions move a neighbour into their stub #27560
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/27560"
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 GiannisG1 on 2026-11-26T17:51:23Z.
What
A SHA-1 or SHA-256 instruction in register form is 4 bytes long and CLZERO is 3, too short for the
jmp rel32to its stub. The converter therefore absorbs the instructions after it into the site. When one of those instructions is a branch target, or cannot move (a return, say),--to-intelfails for the whole title. The 256-bit store, TZCNT/LZCNT, MOVBE and--to-avxlowerings avoid this with movable stubs (RelocatableWindow):WindowPlacertakes the instruction after the site or the one before it into the stub, or uses a nearby island. The SHA and CLZERO lowerings were not marked movable, although the matcher'sEmitOutOfLine, which the window path calls, already lowers them. This change marks_matchSha256,_matchSha1and_matchClzeromovable.Found with Mario Kart World: Pit Stop (PPSA99193, ps5-homebrew-catalog), whose conversion failed with
FAIL: Branch enters an AMD-only instruction (offset 0x13c6d)on an Intel host. CPUs without SHA extensions include this i5-7500 (Kaby Lake) and the Intel desktop CPUs before Rocket Lake. The title's SHA-256 block loop at VA 0xfc69:The forward site [0xfc69, 0xfc72) contains the branch target. The window placer now takes the
vpadddbefore it: the site is [0xfc65, 0xfc6d), and 0xfc6d keeps its instruction.CLZERO is the same case. The PPSA38500 report in #27523 says
--to-intelfails on that title withBranch enters an AMD-only instruction (offset 0x22e111), and that the title uses CLZERO in memory-zeroing loops (#27532 emulates CLZERO in the Windows crash handler instead). I have no copy of that title, so I can't check which instruction sits at that offset. A CLZERO whose next instruction is a branch target now takes the instruction before it.CLZERO is the first movable instruction shorter than four bytes, so a window of it and one 1-byte neighbour would be shorter than the 5-byte jump (found by CodeRabbit).
WindowPlacer::Windownow takes neighbours, forward or backward, until the placement spans the jump, and_relocaterefuses a shorter placement. Every lowering that was already movable is at least 4 bytes long: its first neighbour already reaches 5 bytes, so its placements do not change.A SHA instruction or CLZERO followed by a return now takes the return into its stub, as a 256-bit store already does ("A 256-bit store followed by a return was moved"). Before, its conversion failed. For every short SHA or CLZERO site that converted before,
Windowfirst tries the instruction after the site, which is the site the old path built. On 65 catalog titles the converted output is byte-identical (Tested).Tested
Linux Mint 22.3, kernel 7.0.0, GCC 13.3, Intel Core i5-7500 (no SHA extensions). Head on
main41dfbe4d8e; it merges cleanly withmaindebacdd4d9.amd64_only_converter(Amd64OnlyConverterTests.cpp): inconverterSha256andconverterSha1, "followed by a return" now expects a stub that ends with the return (wasrequireFailure). A new loop case (vpaddd, SHA256MSG2/SHA1MSG2, a store that a laterjmpenters,ret) expects one site at thevpaddd, 8 bytes long, whose stub starts with the movedvpaddd. RED onmain's matcher ("AMD-only instruction too short for a jump is followed by an instruction that cannot move"); GREEN 3/3.converterClzero, in the same way: "followed by a return" now expects a stub that ends with the return. A new loop case (add, CLZERO, acmpthat a laterjmpenters,ret) expects one site at theadd, 7 bytes long, whose stub starts with the movedadd.CLZERO; NOP; NOPexpects one 5-byte site. Two 1-byte pushes before a CLZERO whose next instruction is a branch target expect a 5-byte site starting at the first push.CLZERO; RETis still refused, as onmain: no placement spans the jump. RED on the previous headbd12071734, which placed a 4-byte site there; GREEN 3/3.-DANYPS5_RELINKER_ONLY=ON): all 1136 tests pass, no skips (check_skips.py), on the current head.--to-intel, the same build tree withmain's matcher and with this change, on every ps5-homebrew-catalog title whose relink lowers a SHA instruction (65 titles, up to 2141 short SHA sites each) plus PPSA99193. The outputs of all 65 are byte-identical (app.elfand the converted modules). PPSA99193 fails onmain(rc 2, the message above) and converts with the change: 8 SHA sites, 6 of them 4 bytes long. Rerun on the current head (main's matcher and converter against this change): again 65 byte-identical, and PPSA99193 rc 2 → 0. No catalog title's relink lowers a CLZERO.main41dfbe4d8e's libraries. It starts and runs 8 s, until its first mesh-shader recompile (shader 0x2028daa00 stage 7: ShaderRecompiler::Recompile: SPIRV-Tools: module version exceeds requested SPIR-V target). That is where this GPU, which has no mesh shaders, stops 47 other catalog titles too (#17611). I did not check whether this run executed the converted SHA-256 loop.e269ebc5d5plus #27649's change applied locally. Its eboot now converts completely with--to-intel(full instruction scan, PRX conversion, output image written, no FAIL; #27564, issuecomment-7206089884). Before this PR it stopped atBranch enters an AMD-only instruction (offset 0x22e111). Booted on the same machine with a warm shader cache, the converted title runs at 29.97 fps, against 0.90-0.92 fps when its AMD-only instructions are trapped and emulated at run time instead (issuecomment-7206202354): same boot depth, no new crashes.check_conventions.py,check_test_registration.py(relinker build),check_compile.py(2 translation units) andcheck_main_sync.py(merges cleanly) are clean.pr_overlap.py --branch: no overlap in the 13 pairs it compared. It could not compare #11525, #12678, #20389, #23636 and #23842, which conflict withmain. Their diffs change neither_matchSha256/_matchSha1norconverterSha256/converterSha1.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)--to-intelconversion (homebrew, no Game bring-up discussion); on a GPU without mesh shaders it then stops at its first mesh shader. With #27649, PPSA38500's Windows--to-intelconversion (#27564, Game bring-up #27523), which needs both🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-26T18:10:42Z.
Review: APPROVED
Reviewed — looks good
The SHA relocation change is supported by the inspected placement logic and added converter cases; I found no blocking defect. The exact reviewed head is
eb09d3cff4623c3b7651f9d58b4611602e5f8fb1.What I ran
repo_read.py.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
files.jsonwas not available through the reader; the manifest lists two changed files, both included and read. No other files were excluded in the supplied manifest.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-11-26T19:56:05Z.
Review: COMMENTED
Actionable comments posted: 1
bedad95e-ddd4-4c36-8fa4-0d69564895faReviewing files that changed from the base of the PR and between
eb09d3cff4andbd12071734.core/relinker/codegen/src/x86/Amd64OnlyInstructionMatcher.cppcore/relinker/codegen/tests/Amd64OnlyConverterTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-26T20:03:19Z.
Review: APPROVED
Reviewed — looks good
The change marks the SHA-1, SHA-256, and CLZERO short trampolines as eligible for movable-window placement; the inspected code supports the intended fallback when a following instruction cannot be absorbed.
What I ran
Reviewed head
bd12071734d0a122b544ea203ae86aac68c8bc80. I read the prepared diff and the changed lines in both changed files, then read the window-placement implementation and relevant surrounding converter and test code throughrepo_read.py. No tests or build commands were run.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
The full contents of the two changed files were not read; only all changed lines and relevant surrounding ranges were inspected. Other repository files were not read. Tests, builds, catalog conversions, and console behavior were not independently verified. The description reports these checks and outcomes, but they were not run in this review.
review/files.jsonwas not available;review/manifest.jsonlists both changed files as included, and the review folder listing showed noreview/files/files.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-26T20:17:41Z.
Review: APPROVED
Reviewed — looks good
The reviewed change at
e269ebc5d586adbd3ea15c43f066177d9171e598has no verified blocking defects; reported test results are author supplied and were not run in this review.What I ran
Read the prepared description, commits, manifest and diff, then the changed converter and matcher source at the pinned head. No tests or builds were run.
Files read:
core/relinker/codegen/src/Amd64OnlyConverter.cpp,core/relinker/codegen/src/x86/Amd64OnlyInstructionMatcher.cpp. All three changed files were listed as reviewed in the manifest; full text for each was included inreview/files/.files.jsonwas not available through the reader.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
The reported test suite, build, catalog A/B conversion and runtime behavior were not independently verified. Test source was listed in the manifest but not read.
files.jsonexclusions could not be inspected.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by Plyxen on 2026-11-27T05:17:38Z.
Review: APPROVED
Checked head
e269ebc5d5. I applied it tomainad7582bad4together with #27680, #27668 and #27649, which touch different files. On Windows 11 with WinLibs GCC 15.2 (Release),tools/check_conventions.py --headon each head andtools/debt_fold.py --checkpassed, the build succeeded, and all 4,930 CPU tests passed (I left out the GPU tests). The ten relinker and codegen tests I ran for these four (amd64_only_converter,amd64_only_windows,red_zone_framer,red_zone_framer_execution,lane_split_lowering,relinker_conversionand four more) passed. Applied alone tomain, it also passes the seven conversion tests (amd64_only_converter,amd64_only_windows,relinker_conversion,conversion_diagnostics,mulx_conversion,relr_conversionandreplaced_plt_stubs_conversion).With
main'sAmd64OnlyConverter.cppandAmd64OnlyInstructionMatcher.cpp,amd64_only_converterfails.The change marks
_matchSha256,_matchSha1and_matchClzeromovable, whoseEmitOutOfLinealready lowered them.WindowPlacerthen takes neighbours forward or backward until the placement spans the five-bytejmp rel32(_forwardEnd,_backwardStart), and_relocaterefuses a shorter one. That covers CodeRabbit's CLZERO point: CLZERO and one 1-byte neighbour make only four bytes.AI-assisted: yes (Claude Code)