test(relinker): skip host instructions the CPU cannot execute #16236

Merged
ycbuzi merged 3 commits from upstream-pr/16236 into main 2026-11-10 07:51:44 +00:00
ycbuzi commented 2026-11-05 21:38:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ycbuzi on 2026-11-05T21:38:34Z.


What

masked_store_lowering, amd64_only_converter and red_zone_framer_execution die with SIGILL on an x86-64 host that cannot execute an instruction they run (reported on #1). This PR makes those execution cases skip on such a host instead.

  • LoweringExecution::HostExecutes (core/relinker/codegen/tests/LoweringExecution.hpp) runs one instruction followed by ret on an executable page, under a temporary SIGILL handler (sigsetjmp/siglongjmp), with RDI pointing at a 64-byte scratch buffer. It does not read CPUID, because Rosetta executes AVX2, FMA, BMI and others without reporting them there (docs/dev/debt/relinker.md). A CPUID check would drop these cases on Apple Silicon, where they run and pass today.
  • masked_store_lowering: each form and width (VMASKMOVPS/PD, VPMASKMOVD/Q, 128 and 256-bit) runs its execution case only if the host executes that original instruction. Skipped forms are printed as skipped: ....
  • amd64_only_converter:
    • xsaveoptExecution cleared YMM3 with vpxor %ymm3,%ymm3,%ymm3, which is AVX2. It now uses vxorps %ymm3,%ymm3,%ymm3 (AVX), which clears the same 256 bits, so XRSTOR still has to restore the whole pattern for the check to pass.
    • The cases that run AVX themselves (the VEX reciprocal sites and VRSQRTPS unit check, the 256-bit store stub, the XSAVEOPT replacement) now first probe vxorps %ymm0,%ymm0,%ymm0.
    • rdseedExecution probes the replacement itself (RDRAND, the Rosetta lowering of RDSEED).
    • Each skip is printed. All other execution cases run as before.
  • red_zone_framer_execution: the leaf whose widened exit carries a VEX ANDN is still framed and counted, but it is executed only when the host reports BMI1 (__builtin_cpu_supports("bmi")). Otherwise it prints framed but not executed, this CPU cannot execute an instruction in it. This test runs only on x86-64 Linux and Windows, never under Rosetta, so CPUID is reliable there. HostExecutes is not used because it is Linux-only, and this test also runs on Windows.

The tests still pass when parts are skipped (exit 0, not ctest's skip code 77), because the rest of each test still runs. On a host that executes everything, nothing changes.

python3 tools/pr_overlap.py failed closed on #11192 and #13486, which conflict with main, so I compared them by hand. #13486 does not change these files. #11192 adds scalar RCPSS/RSQRTSS cases after the VRSQRTPS unit check in reciprocalExecution, which is next to this change's if (!avx) return; but not on the same lines. With both merged, its new cases run only where AVX executes, which its VEX sites need anyway. #15639 and #11525 also change Amd64OnlyConverterTests.cpp, adding new cases and their main() calls elsewhere in the file, not these lines. #16432 adds leaves to RedZoneFramerExecutionTests.cpp in other places. Its new vmovq leaf is AVX, so with both merged it would crash the same way on a host without AVX; whichever lands second can give it the HostRuns field added here with a __builtin_cpu_supports("avx") check.

Tested

Machine: MacBookPro8,1, Intel Core i5-2415M (Sandy Bridge, AVX1 only: no AVX2, BMI, FMA or RDRAND), macOS 13.7.8, LLVM clang 19.1.7 (official llvm.org macOS-X64 build) with the system libc++. Intel Mac, x86-64 native, no Rosetta. Commit 77d3a86021 on main 38fe81a14a.

cmake -S . -B build -G Ninja -DANYPS5_RELINKER_ONLY=ON -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON
ninja -C build -j2
ctest --test-dir build -R '^(masked_store_lowering|amd64_only_converter)$' -V
ctest --test-dir build -j2 --output-on-failure
  • Before (on main d23df92b38): both tests ended with SIGILL (rc 132). lldb showed vpmaskmovd in the generated stub, the test's own vpxor %ymm3, and the RDRAND replacement of RDSEED.
  • After: both pass.
    • masked_store_lowering prints skipped: 128-bit VPMASKMOVD execution, the host does not execute it, and the same for 256-bit VPMASKMOVD and both VPMASKMOVQ widths. It runs 864 masked store executions of VMASKMOVPS/PD.
    • amd64_only_converter prints skipped: RDSEED replacement execution, the host does not execute RDRAND. The AVX cases run here, so the probe's success path is covered.
  • Full ctest: 693/697 passed. The 4 failures are unrelated and already fail on main on this Mac: shader_ir_use_rewrite and shader_ssa_pass_reads don't link (the macOS 13 libc++ has no std::pmr), and the two check_compile_modes tests fail because clang/gcc on PATH is Apple clang 14.
  • python3 tools/check_conventions.py --base origin/main: all checks passed.

Second machine, without AVX or BMI (11-06): Pentium Silver N6000 (Tremont, SSE4.2 only), Debian 13 in ChromeOS's Linux container, GCC 14.2.0, same relinker-only configure. Head 926bd92fae (this branch merged with main 7b7edf657b).

  • Before, on main 9f2b939606: all three tests end with SIGILL (red_zone_framer_execution rc 132, Illegal instruction).
  • After: red_zone_framer_execution, masked_store_lowering and amd64_only_converter all pass (rc 0). red_zone_framer_execution prints the skip line for the ANDN leaf and runs every other leaf. masked_store_lowering skips all eight forms (skipped: 128-bit VMASKMOVPS execution, the host does not execute it, and so on), and amd64_only_converter skips the VRSQRTPS/VRCPPS stub, the XSAVEOPT replacement and the 256-bit store stub, because the host does not execute AVX.
  • Windows 11, MinGW-w64 GCC, Ryzen 9 7900 (has BMI1): red_zone_framer_execution runs every leaf, ANDN included, and passes.

AVX2 hosts are also covered by CI: there the probes succeed and every case runs as before.

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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5179114009 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16236). Originally posted by [ycbuzi](https://github.com/ycbuzi) on 2026-11-05T21:38:34Z. --- ### What `masked_store_lowering`, `amd64_only_converter` and `red_zone_framer_execution` die with SIGILL on an x86-64 host that cannot execute an instruction they run (reported on #1). This PR makes those execution cases skip on such a host instead. - `LoweringExecution::HostExecutes` (`core/relinker/codegen/tests/LoweringExecution.hpp`) runs one instruction followed by `ret` on an executable page, under a temporary SIGILL handler (`sigsetjmp`/`siglongjmp`), with RDI pointing at a 64-byte scratch buffer. It does not read CPUID, because Rosetta executes AVX2, FMA, BMI and others without reporting them there (docs/dev/debt/relinker.md). A CPUID check would drop these cases on Apple Silicon, where they run and pass today. - `masked_store_lowering`: each form and width (VMASKMOVPS/PD, VPMASKMOVD/Q, 128 and 256-bit) runs its execution case only if the host executes that original instruction. Skipped forms are printed as `skipped: ...`. - `amd64_only_converter`: - `xsaveoptExecution` cleared YMM3 with `vpxor %ymm3,%ymm3,%ymm3`, which is AVX2. It now uses `vxorps %ymm3,%ymm3,%ymm3` (AVX), which clears the same 256 bits, so XRSTOR still has to restore the whole pattern for the check to pass. - The cases that run AVX themselves (the VEX reciprocal sites and VRSQRTPS unit check, the 256-bit store stub, the XSAVEOPT replacement) now first probe `vxorps %ymm0,%ymm0,%ymm0`. - `rdseedExecution` probes the replacement itself (RDRAND, the Rosetta lowering of RDSEED). - Each skip is printed. All other execution cases run as before. - `red_zone_framer_execution`: the leaf whose widened exit carries a VEX ANDN is still framed and counted, but it is executed only when the host reports BMI1 (`__builtin_cpu_supports("bmi")`). Otherwise it prints `framed but not executed, this CPU cannot execute an instruction in it`. This test runs only on x86-64 Linux and Windows, never under Rosetta, so CPUID is reliable there. `HostExecutes` is not used because it is Linux-only, and this test also runs on Windows. The tests still pass when parts are skipped (exit 0, not ctest's skip code 77), because the rest of each test still runs. On a host that executes everything, nothing changes. `python3 tools/pr_overlap.py` failed closed on #11192 and #13486, which conflict with `main`, so I compared them by hand. #13486 does not change these files. #11192 adds scalar RCPSS/RSQRTSS cases after the VRSQRTPS unit check in `reciprocalExecution`, which is next to this change's `if (!avx) return;` but not on the same lines. With both merged, its new cases run only where AVX executes, which its VEX sites need anyway. #15639 and #11525 also change `Amd64OnlyConverterTests.cpp`, adding new cases and their `main()` calls elsewhere in the file, not these lines. #16432 adds leaves to `RedZoneFramerExecutionTests.cpp` in other places. Its new `vmovq` leaf is AVX, so with both merged it would crash the same way on a host without AVX; whichever lands second can give it the `HostRuns` field added here with a `__builtin_cpu_supports("avx")` check. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Machine: MacBookPro8,1, Intel Core i5-2415M (Sandy Bridge, AVX1 only: no AVX2, BMI, FMA or RDRAND), macOS 13.7.8, LLVM clang 19.1.7 (official llvm.org macOS-X64 build) with the system libc++. Intel Mac, x86-64 native, no Rosetta. Commit 77d3a86021 on `main` 38fe81a14a. ``` cmake -S . -B build -G Ninja -DANYPS5_RELINKER_ONLY=ON -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON ninja -C build -j2 ctest --test-dir build -R '^(masked_store_lowering|amd64_only_converter)$' -V ctest --test-dir build -j2 --output-on-failure ``` - Before (on `main` d23df92b38): both tests ended with SIGILL (rc 132). lldb showed `vpmaskmovd` in the generated stub, the test's own `vpxor %ymm3`, and the RDRAND replacement of RDSEED. - After: both pass. - `masked_store_lowering` prints `skipped: 128-bit VPMASKMOVD execution, the host does not execute it`, and the same for 256-bit VPMASKMOVD and both VPMASKMOVQ widths. It runs 864 masked store executions of VMASKMOVPS/PD. - `amd64_only_converter` prints `skipped: RDSEED replacement execution, the host does not execute RDRAND`. The AVX cases run here, so the probe's success path is covered. - Full ctest: 693/697 passed. The 4 failures are unrelated and already fail on `main` on this Mac: `shader_ir_use_rewrite` and `shader_ssa_pass_reads` don't link (the macOS 13 libc++ has no `std::pmr`), and the two `check_compile_modes` tests fail because `clang`/`gcc` on PATH is Apple clang 14. - `python3 tools/check_conventions.py --base origin/main`: all checks passed. Second machine, without AVX or BMI (11-06): Pentium Silver N6000 (Tremont, SSE4.2 only), Debian 13 in ChromeOS's Linux container, GCC 14.2.0, same relinker-only configure. Head `926bd92fae` (this branch merged with `main` 7b7edf657b). - Before, on `main` 9f2b939606: all three tests end with SIGILL (`red_zone_framer_execution` rc 132, `Illegal instruction`). - After: `red_zone_framer_execution`, `masked_store_lowering` and `amd64_only_converter` all pass (rc 0). `red_zone_framer_execution` prints the skip line for the ANDN leaf and runs every other leaf. `masked_store_lowering` skips all eight forms (`skipped: 128-bit VMASKMOVPS execution, the host does not execute it`, and so on), and `amd64_only_converter` skips the VRSQRTPS/VRCPPS stub, the XSAVEOPT replacement and the 256-bit store stub, because the host does not execute AVX. - Windows 11, MinGW-w64 GCC, Ryzen 9 7900 (has BMI1): `red_zone_framer_execution` runs every leaf, ANDN included, and passes. AVX2 hosts are also covered by CI: there the probes succeed and every case runs as before. ### 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: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
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!16236
No description provided.