feat(relinker): rewrite AVX2 instructions with an AVX twin under --to-avx #17108

Merged
ycbuzi merged 3 commits from upstream-pr/17108 into main 2026-11-12 23:21:20 +00:00
ycbuzi commented 2026-11-08 10:19:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ycbuzi on 2026-11-08T10:19:21Z.


What

Under --to-avx (#17084), this rewrites in place the AVX2 instructions whose AVX form moves the same bits at the same encoding length:

AVX2 AVX replacement
VINSERTI128 VINSERTF128
VEXTRACTI128 VEXTRACTF128
VPERM2I128 VPERM2F128
VPBLENDD (xmm, ymm) VBLENDPS
VBROADCASTI128 m128 VBROADCASTF128
VPBROADCASTD m32 (xmm, ymm) VBROADCASTSS
VPBROADCASTQ m64 VBROADCASTSD (ymm), VMOVDDUP (xmm)

Only the opcode byte changes, plus the map and pp for VMOVDDUP. There is no stub, and the instruction can also be emitted inside a moved window. Register-source broadcasts, VPERMQ/VPERMD/VPERMPS/VPERMPD, variable shifts, gathers, masked moves and 256-bit integer arithmetic have no same-length twin; they are listed in docs/dev/debt/relinker.md for the following PRs.

Tested

  • macOS 13.7.8, MacBook Pro 8,1 (i5-2415M: AVX, no AVX2), LLVM 19.1.7, relinker-only:
    • avx2_twin_lowering passes. Its execution part runs each replacement on this CPU, with 11 immediates for the immediate forms, and compares ymm0 with the SDM result of the original AVX2 instruction, including the zeroed upper half of the 128-bit forms.
    • count_lowering and movbe_lowering pass.
  • Windows 11, MinGW GCC: avx2_twin_lowering (decoding, matching, out-of-line emission), amd64_only_converter, count_lowering and cli_args pass.
  • Real code: converted Celeste Classic's eboot (homebrew) with --macos --to-avx --skip-sce-module. Compared with --to-rosetta:
    • 2,365 VEXTRACTI128, 1,362 VINSERTI128, 3,472 VPBLENDD, 164 VPERM2I128, 73 VBROADCASTI128, 1,994 VPBROADCASTD and 918 VPBROADCASTQ were rewritten.
    • The 856 VPBROADCASTD and 284 VPBROADCASTQ left are register-source forms.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: #17084
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5210509917 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17108). Originally posted by [ycbuzi](https://github.com/ycbuzi) on 2026-11-08T10:19:21Z. --- ### What Under `--to-avx` (#17084), this rewrites in place the AVX2 instructions whose AVX form moves the same bits at the same encoding length: | AVX2 | AVX replacement | |---|---| | VINSERTI128 | VINSERTF128 | | VEXTRACTI128 | VEXTRACTF128 | | VPERM2I128 | VPERM2F128 | | VPBLENDD (xmm, ymm) | VBLENDPS | | VBROADCASTI128 m128 | VBROADCASTF128 | | VPBROADCASTD m32 (xmm, ymm) | VBROADCASTSS | | VPBROADCASTQ m64 | VBROADCASTSD (ymm), VMOVDDUP (xmm) | Only the opcode byte changes, plus the map and `pp` for VMOVDDUP. There is no stub, and the instruction can also be emitted inside a moved window. Register-source broadcasts, VPERMQ/VPERMD/VPERMPS/VPERMPD, variable shifts, gathers, masked moves and 256-bit integer arithmetic have no same-length twin; they are listed in `docs/dev/debt/relinker.md` for the following PRs. ### Tested - **macOS 13.7.8, MacBook Pro 8,1 (i5-2415M: AVX, no AVX2), LLVM 19.1.7, relinker-only:** - `avx2_twin_lowering` passes. Its execution part runs each replacement on this CPU, with 11 immediates for the immediate forms, and compares ymm0 with the SDM result of the original AVX2 instruction, including the zeroed upper half of the 128-bit forms. - `count_lowering` and `movbe_lowering` pass. - **Windows 11, MinGW GCC:** `avx2_twin_lowering` (decoding, matching, out-of-line emission), `amd64_only_converter`, `count_lowering` and `cli_args` pass. - **Real code:** converted Celeste Classic's eboot (homebrew) with `--macos --to-avx --skip-sce-module`. Compared with `--to-rosetta`: - 2,365 VEXTRACTI128, 1,362 VINSERTI128, 3,472 VPBLENDD, 164 VPERM2I128, 73 VBROADCASTI128, 1,994 VPBROADCASTD and 918 VPBROADCASTQ were rewritten. - The 856 VPBROADCASTD and 284 VPBROADCASTQ left are register-source forms. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: #17084 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
gabichuuuuu commented 2026-11-08 11:56:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-08T11:56:04Z.

Review: COMMENTED


Windows run of this head as part of #17132's head a7614d3fcf, which contains 1ac3219840 unchanged, merged onto main 105c9e4d91. Setup: Windows 11, i7-12800H (AVX2), WinLibs GCC 15.2, Release, relinker-only.

avx2_twin_lowering passes as pushed. Its execution part is compiled out on Windows. With a local, unpushed _WIN32 branch in LoweringExecution.hpp (VirtualAlloc/VirtualProtect and a sysv_abi call), it runs and passes here too. The details are in my comment on #17132.

This CPU has AVX2, so I also checked each twin against the original instruction instead of the SDM model. Each of the 10 cases in cases() ran, the original AVX2 bytes and the AVX replacement, for all 256 immediates. Each pair ran on 4 random states of ymm0–ymm2 and the memory operand, and I compared ymm0. That's 10,240 runs, all bit-identical. This includes the xmm forms, so it also covers the zeroed upper half, and VMOVDDUP for VPBROADCASTQ xmm, m64.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5817777009 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17108#pullrequestreview-5817777009). Originally posted by [gabichuuuuu](https://github.com/gabichuuuuu) on 2026-11-08T11:56:04Z. **Review:** COMMENTED --- Windows run of this head as part of #17132's head `a7614d3fcf`, which contains `1ac3219840` unchanged, merged onto `main` `105c9e4d91`. Setup: Windows 11, i7-12800H (AVX2), WinLibs GCC 15.2, Release, relinker-only. `avx2_twin_lowering` passes as pushed. Its execution part is compiled out on Windows. With a local, unpushed `_WIN32` branch in `LoweringExecution.hpp` (`VirtualAlloc`/`VirtualProtect` and a `sysv_abi` call), it runs and passes here too. The details are in my comment on #17132. This CPU has AVX2, so I also checked each twin against the original instruction instead of the SDM model. Each of the 10 cases in `cases()` ran, the original AVX2 bytes and the AVX replacement, for all 256 immediates. Each pair ran on 4 random states of `ymm0`–`ymm2` and the memory operand, and I compared `ymm0`. That's 10,240 runs, all bit-identical. This includes the xmm forms, so it also covers the zeroed upper half, and `VMOVDDUP` for `VPBROADCASTQ xmm, m64`.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!17108
No description provided.