feat(relinker): split 256-bit lane-local integer instructions under --to-avx #17132

Merged
ycbuzi merged 4 commits from upstream-pr/17132 into main 2026-11-12 23:27:49 +00:00
ycbuzi commented 2026-11-08 10:50:33 +00:00 (Migrated from github.com)

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


What

An AVX host has no 256-bit integer instructions. Under --to-avx (#17084, #17108), a VEX.256 integer instruction whose result lanes depend only on the same source lanes now runs in a stub as two VEX.128 instructions:

  1. VEXTRACTF128 copies the upper halves into scratch YMM registers. Three of them are saved with VMOVDQU in a 224-byte frame below the red zone.
  2. The 128-bit form of the same opcode computes each half. A memory source is loaded into a scratch register first.
  3. VINSERTF128 joins the two halves.

RFLAGS is not written. The stub uses relocatable-window placement, because most of these instructions are 4 bytes long.

Covered: the 0F, 0F38 and 0F3A opcodes in _shape:

  • additions, subtractions and multiplies
  • logic and compares
  • packs and unpacks
  • PSHUFB, PALIGNR, PBLENDW, PSHUFD/PSHUFHW/PSHUFLW
  • PABS, PSIGN and horizontal adds
  • shifts by an immediate or by an XMM count

Not covered yet, listed in docs/dev/debt/relinker.md for the next PRs: cross-lane VPERMD/VPERMQ/VPERMPS/VPERMPD, widening PMOVZX/PMOVSX, variable shifts, VPBLENDVB, VMPSADBW, VPMOVMSKB, gathers, masked moves and register-source broadcasts.

Tested

  • macOS 13.7.8, MacBook Pro 8,1 (i5-2415M: AVX, no AVX2), LLVM 19.1.7, relinker-only:
    • lane_split_lowering passes. Its execution part runs each split on this CPU for 20 representative forms, 7 register patterns (including destination = source and all three registers equal) and 4 immediates, and compares the result with the 128-bit instruction run on each half.
    • The test also checks that every other YMM register and the red zone are unchanged.
    • avx2_twin_lowering passes.
  • Windows 11, MinGW GCC: lane_split_lowering (decoding and matching), avx2_twin_lowering and amd64_only_converter pass.
  • Real code: converted Celeste Classic's eboot (homebrew) with --macos --to-avx --skip-sce-module. Counting AVX2 instructions with Capstone over the executable segments, 43,568 remain after --to-rosetta and 13,299 after this. The rest are the uncovered groups above: 6,906 cross-lane permutes, about 2,900 widening moves, about 2,100 register broadcasts and about 1,200 variable shifts.

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: #17108
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5210695446 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17132). Originally posted by [ycbuzi](https://github.com/ycbuzi) on 2026-11-08T10:50:33Z. --- ### What An AVX host has no 256-bit integer instructions. Under `--to-avx` (#17084, #17108), a VEX.256 integer instruction whose result lanes depend only on the same source lanes now runs in a stub as two VEX.128 instructions: 1. VEXTRACTF128 copies the upper halves into scratch YMM registers. Three of them are saved with VMOVDQU in a 224-byte frame below the red zone. 2. The 128-bit form of the same opcode computes each half. A memory source is loaded into a scratch register first. 3. VINSERTF128 joins the two halves. RFLAGS is not written. The stub uses relocatable-window placement, because most of these instructions are 4 bytes long. **Covered:** the 0F, 0F38 and 0F3A opcodes in `_shape`: - additions, subtractions and multiplies - logic and compares - packs and unpacks - PSHUFB, PALIGNR, PBLENDW, PSHUFD/PSHUFHW/PSHUFLW - PABS, PSIGN and horizontal adds - shifts by an immediate or by an XMM count **Not covered yet,** listed in `docs/dev/debt/relinker.md` for the next PRs: cross-lane VPERMD/VPERMQ/VPERMPS/VPERMPD, widening PMOVZX/PMOVSX, variable shifts, VPBLENDVB, VMPSADBW, VPMOVMSKB, gathers, masked moves and register-source broadcasts. ### Tested - **macOS 13.7.8, MacBook Pro 8,1 (i5-2415M: AVX, no AVX2), LLVM 19.1.7, relinker-only:** - `lane_split_lowering` passes. Its execution part runs each split on this CPU for 20 representative forms, 7 register patterns (including destination = source and all three registers equal) and 4 immediates, and compares the result with the 128-bit instruction run on each half. - The test also checks that every other YMM register and the red zone are unchanged. - `avx2_twin_lowering` passes. - **Windows 11, MinGW GCC:** `lane_split_lowering` (decoding and matching), `avx2_twin_lowering` and `amd64_only_converter` pass. - **Real code:** converted Celeste Classic's eboot (homebrew) with `--macos --to-avx --skip-sce-module`. Counting AVX2 instructions with Capstone over the executable segments, 43,568 remain after `--to-rosetta` and 13,299 after this. The rest are the uncovered groups above: 6,906 cross-lane permutes, about 2,900 widening moves, about 2,100 register broadcasts and about 1,200 variable shifts. ### 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: #17108 - [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:06 +00:00 (Migrated from github.com)

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

Review: COMMENTED


Windows run of head a7614d3fcf (which contains #17108 and #17084), merged onto main 105c9e4d91 (clean merge), with the execution part turned on and checked against native AVX2.

Setup: Windows 11, i7-12800H (AVX2, BMI1/2, LZCNT, MOVBE), WinLibs GCC 15.2, Release, relinker-only with BUILD_TESTING=ON.

As pushed: lane_split_lowering, avx2_twin_lowering, count_lowering, movbe_lowering, amd64_only_converter, cli_args and the other lowering tests pass, but the execution parts are compiled out on Windows.

Execution on Windows (local change only, not for this PR): I gave LoweringExecution.hpp a _WIN32 branch: VirtualAlloc/VirtualProtect in place of mmap/mprotect, and the generated code called through a __attribute__((sysv_abi)) pointer, since the wrappers take their argument in rdi. With that, the execution part of every lowering test runs and passes here, yours included.

Native oracle. Your execution check compares the stub with the 128-bit instruction run on each half. That can't catch an opcode in _shape that isn't really lane-local, and it runs only register sources. This CPU has AVX2, so I added a local check that runs the original VEX.256 instruction and the stub on the same random machine state. It then compares all 16 YMM registers, the memory operand and the red zone.

  • Forms: every map 0F/0F38/0F3A, pp and opcode, plus each /ext for the shifts, that DecodeLaneSplit accepts. That is 100 forms.
  • Registers: 9 register patterns, including dst = src and all three the same, with 11 immediates for the immediate forms.
  • Memory: for every non-shift-immediate form, a memory source through [rax] and through [rsp-128]. The second exercises the kFrame adjustment and the 128-bit load for shift counts.
  • Inputs: 6 random states per site, half of them with small low qwords, so the shift counts land in range.
  • Result: 20,220 runs, all bit-identical to native.

To check that this oracle can fail, I made _shape take VPBROADCASTB ymm, xmm (0F38 78) as Unary. It then stops at once on native mismatch for C4 E2 7D 78 C2. With the source restored, it passes.

Full relinker-only suite: 813 / 819. The 6 failures are select_tests_* and stubgen, Python tests in tools/ that fail on symlink and temp-file handling on this machine (WinError 183, No usable temporary file name found). This stack doesn't touch tools/.

The local test changes aren't pushed anywhere. If you want the Windows execution path in the tree, the LoweringExecution.hpp change above is all it takes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5817777106 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17132#pullrequestreview-5817777106). Originally posted by [gabichuuuuu](https://github.com/gabichuuuuu) on 2026-11-08T11:56:06Z. **Review:** COMMENTED --- Windows run of head `a7614d3fcf` (which contains #17108 and #17084), merged onto `main` `105c9e4d91` (clean merge), with the execution part turned on and checked against native AVX2. **Setup:** Windows 11, i7-12800H (AVX2, BMI1/2, LZCNT, MOVBE), WinLibs GCC 15.2, Release, relinker-only with `BUILD_TESTING=ON`. **As pushed:** `lane_split_lowering`, `avx2_twin_lowering`, `count_lowering`, `movbe_lowering`, `amd64_only_converter`, `cli_args` and the other lowering tests pass, but the execution parts are compiled out on Windows. **Execution on Windows (local change only, not for this PR):** I gave `LoweringExecution.hpp` a `_WIN32` branch: `VirtualAlloc`/`VirtualProtect` in place of `mmap`/`mprotect`, and the generated code called through a `__attribute__((sysv_abi))` pointer, since the wrappers take their argument in `rdi`. With that, the execution part of every lowering test runs and passes here, yours included. **Native oracle.** Your execution check compares the stub with the 128-bit instruction run on each half. That can't catch an opcode in `_shape` that isn't really lane-local, and it runs only register sources. This CPU has AVX2, so I added a local check that runs the original VEX.256 instruction and the stub on the same random machine state. It then compares all 16 YMM registers, the memory operand and the red zone. - **Forms:** every map 0F/0F38/0F3A, `pp` and opcode, plus each `/ext` for the shifts, that `DecodeLaneSplit` accepts. That is 100 forms. - **Registers:** 9 register patterns, including dst = src and all three the same, with 11 immediates for the immediate forms. - **Memory:** for every non-shift-immediate form, a memory source through `[rax]` and through `[rsp-128]`. The second exercises the `kFrame` adjustment and the 128-bit load for shift counts. - **Inputs:** 6 random states per site, half of them with small low qwords, so the shift counts land in range. - **Result:** 20,220 runs, all bit-identical to native. To check that this oracle can fail, I made `_shape` take `VPBROADCASTB ymm, xmm` (0F38 78) as `Unary`. It then stops at once on `native mismatch for C4 E2 7D 78 C2`. With the source restored, it passes. **Full relinker-only suite:** 813 / 819. The 6 failures are `select_tests_*` and `stubgen`, Python tests in `tools/` that fail on symlink and temp-file handling on this machine (`WinError 183`, `No usable temporary file name found`). This stack doesn't touch `tools/`. The local test changes aren't pushed anywhere. If you want the Windows execution path in the tree, the `LoweringExecution.hpp` change above is all it takes.
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!17132
No description provided.