Skip to content

feat(relinker): record the host CPU features --to-avx output needs - #24915

Open
momgaroganuchi-crypto wants to merge 6 commits into
boykopovar:mainfrom
momgaroganuchi-crypto:feat/relinker-to-avx-host-cpu
Open

momgaroganuchi-crypto wants to merge 6 commits into
boykopovar:mainfrom
momgaroganuchi-crypto:feat/relinker-to-avx-host-cpu

Conversation

@momgaroganuchi-crypto

@momgaroganuchi-crypto momgaroganuchi-crypto commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

What

--to-avx lowers BMI1, BMI2, LZCNT and MOVBE for hosts with AVX but without AVX2, but only --macos output could start there: CheckHostCpu in libkernel required LZCNT, BMI1, BMI2, MOVBE, AVX2, FMA and F16C unconditionally and did not know what the conversion removed.

The relinker now records what the output still needs:

  • HostCpuFeatureClassifier maps one decoded instruction to LZCNT, BMI1, BMI2, MOVBE, AVX2, FMA, F16C, AES, PCLMULQDQ, RDRAND, RDSEED, ADX, SHA, CLFLUSHOPT, CLWB, RDPID, XSAVEOPT, XSAVEC, XSAVES, SSE4A, CLZERO, RDTSCP, FSGSBASE, AVX-VNNI, AVX-IFMA, AVX-VNNI-INT8 or AVX-NE-CONVERT, including the legacy encodings of the last sixteen and the VEX encodings of the AVX-VNNI, AVX-IFMA, AVX-VNNI-INT8 and AVX-NE-CONVERT families. A 256-bit VEX opcode outside the AVX1 list counts as AVX2, a 128-bit VEX opcode counts as AVX2 only for the listed AVX2-only forms, and FMA and F16C are matched before the width rule. The classification is deliberately conservative: data decoded as code can only require more. VEX.128 VPACKUSDW and the 256-bit VCVTDQ2PD, VCVTPD2DQ and VCVTTPD2DQ are AVX1, while only VEX.256 VPACKUSDW is AVX2. Register-source VBROADCASTSS/VBROADCASTSD are AVX2 while their memory forms are AVX1; the other 0F38/66 AVX1 opcodes (VPERMILPS, VPERMILPD, VTESTPS, VTESTPD, VPTEST, 0x0C-0x0F and 0x17) stay AVX1 in both forms. Legacy encodings, including TZCNT, LZCNT, MOVBE and the retained-extension forms, are recorded only after the full encoding passes X64InstructionDecoder; register and memory forms are distinguished so a fence is not mistaken for XSAVEOPT or CLWB.

  • Amd64OnlyConverter classifies every scanned instruction and unions the extensions of the instructions that no lowering replaced, in the executable and in every bundled module converted in the same run. GuestModuleBuilder returns that union.

  • Linux writes a 16-byte PT_OS_HOSTCPU program header pointing into the extra block; Windows writes a .hostcpu section next to .procpar. Both contain the magic APS5HCP1 and an eight-byte mask.

  • libc reads the record. libkernel requires the baseline (SSE through SSE4.2, POPCNT, CMPXCHG16B, LAHF/SAHF and AVX) plus the recorded bits, and keeps the existing all-extensions check when no record is present. The error names the missing recorded extensions. The runtime reads CPUID leaf 7 subleaf 1 for AVX-VNNI (EAX bit 4), AVX-IFMA (EAX bit 23), AVX-VNNI-INT8 (EDX bit 4) and AVX-NE-CONVERT (EDX bit 5), and leaf 80000001 EDX for RDTSCP.

  • --to-avx no longer requires --macos, and the CLI and user documentation describe the record. macOS output is unchanged: CheckHostCpu is not compiled there and no record is written.

The record is written only for --to-avx; other output keeps the old behavior. It is a union rather than per-module records, so a module loaded later from a separate conversion does not add bits. Implements the proposal in #22530. It overlaps the older #23279, which implements the same proposal; the two should be coordinated, not merged both.

Tested

Windows 11 26200, WinLibs GCC 16.2.0, AMD Ryzen AI 9 HX PRO 375 (Radeon 890M), build-full, based on main f5b7e1d1ac after rebasing:

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 and the licence notice of code adapted from upstream (see AGENTS.md)

  • 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: relinker, libkernel: let --to-avx output run on Linux and Windows by recording the CPU features it still needs #22530

  • Unblocks: none

  • GPUs tested: n/a

  • AI-assisted: yes

@1tsethan

Copy link
Copy Markdown
Contributor

Independent native Windows relinker verification with MSYS2 UCRT64 GCC 16.2 and Ninja. I applied the relinker diff and shared HostCpuFeatures.hpp from 28ac60476a to my local checkout 732c68c9fd, built the normal targets, and ran host_cpu_record, host_cpu_feature_classifier, amd64_only_converter, and cli_args. All 4 CTests passed.

host_cpu_record checked both ELF and PE metadata from the synthetic fixture: plain output had no host CPU record and --to-avx output recorded exactly FMA. This was a Windows-hosted relinker run covering the output records, without booting the converted fixtures.

AI-assisted: yes (OpenAI Codex).

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — comments

The review found a concrete host CPU feature classification gap that can let an AVX2 instruction run on a host without AVX2.

What I ran

Reviewed head 28ac60476a5f150bd0a2bdc9db8f15f455505fa4. I read the prepared description, commits, diff and manifest, and targeted classifier source. I did not run commands or tests.

Results

Check Result Note
Diff and targeted source review passed Classifier gap found
Tests not run No execution environment used

Findings

Blocking

None verified.

Non-blocking

  • The 128-bit AVX2-only opcode list omits AVX2 integer VPACKUSDW (VEX.128.66.0F38.WIG 2B /r). The classifier returns no AVX2 bit for this instruction, so a retained occurrence can be absent from the record and pass the start-up check on a host without AVX2. Add the opcode to isAvx2Vector128 and cover it with a classifier test. Evidence: core/relinker/codegen/src/x86/HostCpuFeatureClassifier.cpp:78.

Not verified

I read all changed files listed in the manifest as included, including classifier, conversion and test changes. No changed files were excluded. Tests and builds were not run; the author-reported CI and hardware results were not independently verified. I did not inspect unrelated repository context.

To get this to a good state

Nothing is required. The classifier coverage point above is optional feedback.

@PrivacyOnion PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 28ac60476a:

  • Conventions: tools/check_conventions.py passed.
  • Build: amd64_only_converter_tests, cli_args_tests, host_cpu_feature_classifier_tests, host_cpu_tests built cleanly.
  • CTest (host_cpu, host_cpu_feature_classifier, cli_args, amd64_only_converter):
    1/4 Test #5935: host_cpu .........................   Passed    0.03 sec
    2/4 Test #6258: host_cpu_feature_classifier ......   Passed    0.01 sec
    3/4 Test #6274: cli_args .........................   Passed    0.02 sec
    4/4 Test #6288: amd64_only_converter .............   Passed    0.08 sec
    100% tests passed, 0 tests failed out of 4
    
  • Negative control: with core/libs/prx/libc/include/ApplicationHeap.hpp, core/libs/prx/libc/src/ApplicationProcessParameters.cpp, core/libs/prx/libkernel/System/include/HostCpu.hpp, core/libs/prx/libkernel/System/src/HostCpu.cpp, core/relinker/cli/src/CliArgs.cpp, core/relinker/codegen/include/codegen/CodegenTypes.hpp, core/relinker/codegen/src/Amd64OnlyConverter.cpp, core/relinker/elfpatcher/include/elfpatcher/general/ElfConstants.hpp, core/relinker/elfpatcher/include/elfpatcher/general/IElfPatcher.hpp, core/relinker/elfpatcher/include/elfpatcher/general/ProgramHeaderLayoutRequest.hpp, core/relinker/elfpatcher/include/elfpatcher/linux/LinuxElfPatcher.hpp, core/relinker/elfpatcher/include/elfpatcher/macos/MacOsMachOPatcher.hpp, core/relinker/elfpatcher/include/elfpatcher/windows/WindowsElfPatcher.hpp, core/relinker/elfpatcher/src/general/ProgramHeaderLayoutBuilder.cpp, core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp, core/relinker/elfpatcher/src/macos/MacOsMachOPatcher.cpp, core/relinker/elfpatcher/src/windows/WindowsPePatcher.cpp, core/relinker/relinker/include/relinker/guest/GuestImage.hpp, core/relinker/relinker/src/guest/GuestModuleBuilder.cpp, core/relinker/relinker/src/pipeline/Conversion.cpp, core/relinker/codegen/src/x86/HostCpuFeatureClassifier.cpp reverted to main, the new tests do not build/link without the change; restoring it makes them build and pass.
  • Code reading:
    • Verified core/libs/prx/libc/src/ApplicationProcessParameters.cpp ApplicationHostCpuFeatures_nid_no_patch against buffer boundaries and dl_iterate_phdr mapping checks on Linux/Windows
    • Checked core/libs/prx/libkernel/System/src/HostCpu.cpp HostCpuFeatures and CheckHostCpu logic with and without recorded features
    • Analyzed core/relinker/codegen/src/x86/HostCpuFeatureClassifier.cpp instruction classification rules (VEX map/prefix matching, 256-bit AVX1 vs AVX2 opcodes, FMA, F16C, BMI1/2, MOVBE, LZCNT)
    • Inspected core/relinker/codegen/src/Amd64OnlyConverter.cpp integration to ensure lowered instructions are excluded from RequiredHostCpuFeatures
    • Verified core/relinker/elfpatcher/src/general/ProgramHeaderLayoutBuilder.cpp and LinuxElfPatcher.cpp PT_OS_HOSTCPU segment creation and slot bounds

Looks Good To Me

@momgaroganuchi-crypto

Copy link
Copy Markdown
Contributor Author

Added VPACKUSDW (VEX.128.66.0F38.WIG 2B /r) to isAvx2Vector128 and covered the encoding with a classifier test (C4 E2 71 2B D0, VPACKUSDW xmm). Re-ran host_cpu, host_cpu_feature_classifier, amd64_only_windows, cli_args, amd64_only_converter and host_cpu_record; all six pass.

@PrivacyOnion PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against e88c95e016:

  • Conventions: tools/check_conventions.py passed.
  • Build: amd64_only_converter_tests, cli_args_tests, host_cpu_feature_classifier_tests, host_cpu_tests built cleanly.
  • CTest (host_cpu, host_cpu_feature_classifier, cli_args, amd64_only_converter):
    1/4 Test #5963: host_cpu .........................   Passed    0.03 sec
    2/4 Test #6304: cli_args .........................   Passed    0.02 sec
    3/4 Test #6288: host_cpu_feature_classifier ......   Passed    0.01 sec
    4/4 Test #6318: amd64_only_converter .............   Passed    0.09 sec
    100% tests passed, 0 tests failed out of 4
    
  • Negative control: with core/libs/prx/libc/include/ApplicationHeap.hpp, core/libs/prx/libc/src/ApplicationProcessParameters.cpp, core/libs/prx/libkernel/System/include/HostCpu.hpp, core/libs/prx/libkernel/System/src/HostCpu.cpp, core/relinker/cli/src/CliArgs.cpp, core/relinker/codegen/include/codegen/CodegenTypes.hpp, core/relinker/codegen/src/Amd64OnlyConverter.cpp, core/relinker/elfpatcher/include/elfpatcher/general/ElfConstants.hpp, core/relinker/elfpatcher/include/elfpatcher/general/IElfPatcher.hpp, core/relinker/elfpatcher/include/elfpatcher/general/ProgramHeaderLayoutRequest.hpp, core/relinker/elfpatcher/include/elfpatcher/linux/LinuxElfPatcher.hpp, core/relinker/elfpatcher/include/elfpatcher/macos/MacOsMachOPatcher.hpp, core/relinker/elfpatcher/include/elfpatcher/windows/WindowsElfPatcher.hpp, core/relinker/elfpatcher/src/general/ProgramHeaderLayoutBuilder.cpp, core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp, core/relinker/elfpatcher/src/macos/MacOsMachOPatcher.cpp, core/relinker/elfpatcher/src/windows/WindowsPePatcher.cpp, core/relinker/relinker/include/relinker/guest/GuestImage.hpp, core/relinker/relinker/src/guest/GuestModuleBuilder.cpp, core/relinker/relinker/src/pipeline/Conversion.cpp, core/relinker/codegen/src/x86/HostCpuFeatureClassifier.cpp reverted to main, the new tests do not build/link without the change; restoring it makes them build and pass.
  • Code reading:
    • Verified ApplicationHostCpuFeatures_nid_no_patch dl_iterate_phdr parsing on Linux/Apple: validates p_type == 0x61000002, checks segment mapping within PT_LOAD boundaries, checks RecordSize (16 bytes), verifies magic APS5HCP1, and ensures reserved bits (~All) are zero.
    • Verified Windows PE implementation in ApplicationProcessParameters.cpp: searches section headers for .hostcpu, validates VirtualSize == 16, boundary against imageSize, magic match, and non-duplicate section logic.
    • Verified HostCpuFeatures and CheckHostCpu handling in libkernel HostCpu.cpp: correctly masks required status against recorded bits for Lzcnt, Bmi1, Bmi2, Movbe, Avx2, Fma, and F16c, and keeps baseline requirements intact when no record is present.
    • Verified HostCpuFeatureClassifier.cpp instruction decoding and classifications for VEX 256-bit AVX1 vs AVX2, VEX 128-bit AVX2 forms (including VPACKUSDW), FMA, F16C, BMI1/2, MOVBE, and LZCNT legacy prefix / opcode rules.
    • Verified Amd64OnlyConverter.cpp pipeline integration: matches are correctly classified, and instructions lowered via substitution, trampoline, or stub bodies are inserted into lowered set and excluded from RequiredHostCpuFeatures.

Looks Good To Me

@ocr1n ocr1n left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On e88c95e016, --to-avx marks four instructions emitted by GCC for Sandy Bridge as requiring AVX2. That makes the startup check reject a supported CPU in exactly the use case in #22530. I put the correction and regression tests in a single commit on top of this PR. Apply or inspect the patch.

I converted the register and memory forms of each instruction separately. All eight fixtures produced an AVX2 mask of 0x10 in both ELF and PE. With the patch, all eight produce 0. A retained AVX2 gather, FMA and BMI2 instruction still produce 0x10, 0x20 and 0x04, respectively.

GCC 13.3 with -O2 -march=sandybridge emitted all four register forms. I checked their arithmetic under QEMU 8.2.2 with -cpu SandyBridge (CPUID reports AVX=1, AVX2=0). I also copied the generated ELF records into a Linux harness linked with this PR’s HostCpu.cpp and ApplicationProcessParameters.cpp. The original records produced “lacks AVX2” for all four. With the corrected records the harness ran to completion, and startup still rejected each of the three unsupported-instruction controls.

Both new regression expectations failed on this PR head. With the patch, all 1,061 relinker-only CTests passed on Ubuntu 24.04 under WSL2, along with the conventions check. Windows coverage was inspection of the generated PE records. No game title or physical Sandy Bridge machine was used.

AI-assisted: yes (OpenAI Codex).

}

bool isAvx1Vector256(const std::uint8_t map, const std::uint8_t prefix, const std::uint8_t opcode) {
if (map == Map0F) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 256-bit forms of VCVTDQ2PD, VCVTPD2DQ and VCVTTPD2DQ all use map 0F, opcode E6, with F3, F2 and 66 respectively. They fall through to AVX2 here. GCC's Sandy Bridge encodings were c5 fe e6 c0, c5 ff e6 c0 and c5 fd e6 c0. This handles all three mandatory prefixes. Intel XED encodings.

Suggested change
if (map == Map0F) {
if (map == Map0F) {
if (opcode == 0xE6 && prefix != 0) return true;

Comment on lines +82 to +83
case 0x2B:
case 0x45: case 0x46: case 0x47:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The VEX.128 form of VPACKUSDW is AVX. The 256-bit form requires AVX2. Removing 0x2B from the 128-bit list lets Sandy Bridge run the former. The existing VPACKUSDW xmm unit test also needs to expect zero, as in the linked commit. Intel XED register and memory encodings.

Suggested change
case 0x2B:
case 0x45: case 0x46: case 0x47:
case 0x45: case 0x46: case 0x47:

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — changes requested

Changes are required before merge: prevent the classifier from treating invalid or incomplete instruction encodings as evidence that the host CPU feature is required.

What I ran

  • Inspected the prepared diff for head e88c95e01607d3a8f2ddb67e175a76f23407cd5b and targeted repository source using repo_read.py.
  • No build or tests were run during this review.

Results

Check Result Note
Source review failed Verified classifier defect
Build and tests not run No commands executed

Findings

Blocking

  • core/relinker/codegen/src/x86/HostCpuFeatureClassifier.cpp:154 and core/relinker/codegen/src/x86/HostCpuFeatureClassifier.cpp:156: the legacy fallback classifies TZCNT/LZCNT from F3 0F BC/BD and MOVBE from 0F 38 F0/F1 without validating the remaining encoding. Since each scanned instruction is passed with its decoded length, a truncated encoding can be labeled as requiring one of these features. The converter then unions those bits into the output record, and libkernel rejects hosts lacking the recorded feature even if the instruction is not valid code and would not execute. Validate the full instruction encoding before recording a requirement, or make this fallback conservative for incomplete or invalid encodings.

Non-blocking

None.

Not verified

  • No tests or build were run. The PR description reports test results, but they were not independently reproduced.
  • The changed source files read were core/libs/prx/libc/src/ApplicationProcessParameters.cpp, core/libs/prx/libkernel/System/src/HostCpu.cpp, core/relinker/codegen/src/x86/HostCpuFeatureClassifier.cpp, core/relinker/elfpatcher/src/general/ProgramHeaderLayoutBuilder.cpp, core/relinker/elfpatcher/src/windows/WindowsPePatcher.cpp, and core/relinker/codegen/include/codegen/x86/VexEncoding.hpp. The prepared diff was inspected; other changed files were not read from their full head contents.
  • Excluded files were not reviewed; the files.json listing was unavailable through the reader.

To get this to a good state

Validate legacy instruction encodings before including their feature bits in the record, then add or update focused classifier tests and rerun the relevant checks on the new head.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Nov 22, 2026
@eduardogeraldes

Copy link
Copy Markdown
Contributor

Independent check of HostCpuFeatureClassifier.cpp at e88c95e (Linux x86-64, GCC 13.3), to help settle the VPACKUSDW question. I compared RequiredHostCpuFeatures(bytes, length) with the CPUID features that iced-x86 1.22 reports, over every VEX2 and VEX3 encoding (maps 0F, 0F38 and 0F3A, all pp, W, L and opcodes, register and memory ModRM forms) and the legacy 0F, 0F38 and 0F3A opcodes with no prefix, 66, F2, F3 and REX.W: 60,885 distinct valid instructions.

Among the instructions a Zen 2 title can contain, the only wrong AVX2 requirements are the four that @ocr1n listed: VEX.128 VPACKUSDW (c4 e2 79 2b c1, AVX in the SDM and in iced; only the VEX.256 form is AVX2) and the 256-bit VCVTDQ2PD, VCVTPD2DQ and VCVTTPD2DQ (c5 fe e6, c5 ff e6, c5 fd e6). The matcher keeps all four under --to-avx, so the record would refuse a Sandy or Ivy Bridge host for them. Under QEMU 8.2.2 with -cpu SandyBridge and -cpu IvyBridge, all four run, while VEX.256 VPACKUSDW raises SIGILL.

With 0x2B removed from isAvx2Vector128 and 0xE6 added to the 66, F3 and F2 lists of isAvx1Vector256, the same sweep finds no wrong requirement. The only missed AVX2 forms left are register-source VBROADCASTSS (xmm and ymm) and VBROADCASTSD ymm, which --to-avx always lowers, so they never reach the record.

PR #23279, opened on Nov 19 and also based on #22530, records the same seven extensions for --to-avx output on Linux and Windows and has the runtime require only those. The two PRs look like overlapping implementations of the same proposal.

AI-assisted (Claude).

@momgaroganuchi-crypto
momgaroganuchi-crypto force-pushed the feat/relinker-to-avx-host-cpu branch from e88c95e to 58eb599 Compare November 22, 2026 13:17
@momgaroganuchi-crypto

Copy link
Copy Markdown
Contributor Author

Thanks for the sweep. Fixed in 58eb599f2f: 0x2B is gone from isAvx2Vector128, and 0xE6 is now in the 66, F3 and F2 AVX1 256-bit lists. Tests expect 0 for VEX.128 VPACKUSDW and VEX.256 VCVTDQ2PD/VCVTPD2DQ/VCVTTPD2DQ, and AVX2 for VEX.256 VPACKUSDW (c4 e2 7d 2b c1). Rebased on 24836104d0; host_cpu, host_cpu_feature_classifier, amd64_only_windows, cli_args and amd64_only_converter pass locally. host_cpu_record hit a local Windows PermissionError reading the converted file, so I will let CI confirm that one.

On #23279: agreed, it is an older implementation of the same #22530 proposal. I mentioned the overlap in the PR body; the maintainers should pick one rather than merging both.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 22, 2026

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — changes requested

Changes are required before merge: correct AVX1/AVX2 classification so the host CPU record does not miss retained instructions that require AVX2.

What I ran

Reviewed head 58eb599f2fe4c40f4a605d30b5a9becc38b0ff9f from the prepared diff and repository files read through repo_read.py. No commands or tests were run in this review; the test results in the PR description are author-reported.

Results

Check Result Note
Diff and targeted source review passed Reviewed the classifier, conversion flow, record parsing and output layout.
Tests in this review not run No tests were run.
Author-reported tests not run Description reports 12/12 selected tests passed.

Findings

Blocking

  • core/relinker/codegen/src/x86/HostCpuFeatureClassifier.cpp:16: The AVX1 allowlist treats all VEX.256 forms of several opcode ranges as AVX1 without accounting for mandatory prefix. For example, VEX.256 VPINSRW (VEX.66.0F C4) is AVX2, but opcode C4 matches the Prefix66 range at lines 24–30 and is classified as AVX1. If this instruction survives lowering, the record omits AVX2 and libkernel can allow startup on a host that cannot execute it. Restrict the allowlist entries to the exact opcode/prefix combinations defined by AVX, and add classifier cases for VEX.256 instructions that require AVX2.

Non-blocking

None.

Not verified

  • No tests or build commands were run. The PR description reports Windows 11 tests and byte-level record checks; those were not independently run here.
  • Sandy Bridge/Ivy Bridge startup behavior and behavior on a physical console were not verified.
  • Read the prepared description, commits, manifest and diff, plus targeted ranges of ApplicationProcessParameters.cpp, HostCpuFeatureClassifier.cpp, WindowsPePatcher.cpp, HostCpu.cpp, Conversion.cpp, LinuxElfPatcher.cpp, and ProgramHeaderLayoutBuilder.cpp. All 27 files listed as reviewed in the manifest were included in the bundle, but the full text of every changed file was not read. files.json exclusions were not inspected.

To get this to a good state

Correct the AVX1 opcode/prefix allowlist and add a regression test for a retained AVX2-only VEX.256 form such as VPINSRW; rerun the classifier and conversion tests on the updated head.

@momgaroganuchi-crypto
momgaroganuchi-crypto force-pushed the feat/relinker-to-avx-host-cpu branch from 19927a3 to 096b707 Compare November 22, 2026 13:56
@momgaroganuchi-crypto

Copy link
Copy Markdown
Contributor Author

Fixed in 096b707771 (rebased on e57e2b9930).

  • Legacy TZCNT/LZCNT/MOVBE are now recorded only when X64InstructionDecoder decodes the whole supplied length; truncated encodings return 0, with tests for F3 0F BC, F3 0F BD and 0F 38 F0.
  • For the AVX1 allowlist I swept every valid VEX.L=1 encoding with iced-x86 1.22. The remaining under-requirements were the register-source VBROADCASTSS/VBROADCASTSD forms (128- and 256-bit), while their memory forms are AVX1. isAvx1Vector256/isAvx2Vector128 now take the ModRM register/memory form into account, and tests cover VBROADCASTSS ymm, xmm, VBROADCASTSD ymm, xmm, VBROADCASTSS xmm, xmm, and both memory forms. The same sweep now finds zero instructions classified as AVX1 here but AVX2 by iced. VPINSRW with L=1 is invalid in iced; the classifier still treats it conservatively as AVX2 because it is not in the allowlist.
  • host_cpu, host_cpu_feature_classifier, amd64_only_windows, cli_args and amd64_only_converter pass locally. host_cpu_record still hits a local Windows PermissionError reading the converted output; CI will confirm it.

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — no blocking findings

The inspected changes show no verified blocking defect; the full changed-file review and programmatic checks were not completed.

What I ran

  • Reviewed prepared diff and targeted source at head 096b707771c9491170a619bf6bc082e946383ae6 using repo_read.py.
  • No tests or build checks were run.

Results

Check Result Note
Targeted source review passed No blocker verified in inspected code
Tests and build not run Author-reported results were not independently run
Full changed-file review not run Several changed files were not read

Findings

Blocking

None verified in the inspected code.

Non-blocking

  • Complete review of the classifier, output patchers, and record parsing would provide stronger confidence in the new cross-platform behavior.

Not verified

  • Read: review/description.md, review/commits.md, review/manifest.json, review/diff.patch; targeted portions of core/libs/prx/libc/src/ApplicationProcessParameters.cpp, core/libs/prx/libkernel/System/src/HostCpu.cpp, core/libs/tests/HostCpu.cpp, core/relinker/codegen/src/Amd64OnlyConverter.cpp, core/relinker/codegen/src/x86/HostCpuFeatureClassifier.cpp, core/relinker/elfpatcher/src/general/ProgramHeaderLayoutBuilder.cpp, core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp, core/relinker/elfpatcher/src/windows/WindowsPePatcher.cpp, core/relinker/relinker/src/guest/GuestModuleBuilder.cpp, core/relinker/relinker/src/pipeline/Conversion.cpp, and core/relinker/relinker/tests/test_hostcpu_record.py.
  • Excluded from direct reading: the remaining changed files listed in review/manifest.json; the targeted files above were not all read in full. review/files.json was unavailable through the reader.
  • Host CPU record behavior on physical hardware, supported host operating systems, and the author-reported test runs were not independently confirmed.

To get this to a good state

Nothing is required. Optional: complete review of the remaining changed files and run the relevant checks.

@eduardogeraldes

Copy link
Copy Markdown
Contributor

@momgaroganuchi-crypto Thanks for the fixes. I re-ran the same iced-x86 sweep on 096b707. The four forms from before are fixed, but the VBROADCAST change introduced a new wrong AVX2 requirement for five AVX1 instructions.

In isAvx1Vector256, the 0F38/66 cases are now:

case 0x0C: case 0x0D: case 0x0E: case 0x0F: case 0x17:
case 0x18: case 0x19:
    return !registerForm;

So 0C to 0F and 17 now fall through to the VBROADCAST rule. Their register forms return false and get AVX2:

  • VPERMILPS ymm, ymm, ymm (c4 e2 7d 0c c1)
  • VPERMILPD ymm, ymm, ymm (c4 e2 7d 0d c1)
  • VTESTPS ymm, ymm (c4 e2 7d 0e c1)
  • VTESTPD ymm, ymm (c4 e2 7d 0f c1)
  • VPTEST ymm, ymm (c4 e2 7d 17 c1)

The memory forms are still fine. These are all AVX1, and --to-avx keeps them. Under QEMU 8.2.2 with -cpu SandyBridge all five run, while VEX.256 VPACKUSDW raises SIGILL. So a title with, for example, a register-form VPTEST ymm (common in vectorised string and memory code) would be refused on the hosts this is for. At e88c95e these forms were classified correctly.

Moving case 0x0C to case 0x17 back to the return true group, and keeping only 0x18 and 0x19 on return !registerForm, would fix it. A register-form test for one of them would catch it next time.

Apart from these five, the sweep finds no other wrong requirement for the instructions a Zen 2 title can contain, and no missed one. The only other AVX2 over-requirement is 256-bit VPCLMULQDQ, which Zen 2 does not have.

AI-assisted (Claude).

Linux and Windows output now carries a 16-byte AnyPS5 record (magic and an eight-byte mask) in a PT_OS_HOSTCPU program header or a .hostcpu PE section. The classifier maps every kept instruction to LZCNT, BMI1, BMI2, MOVBE, AVX2, FMA or F16C, the converter unions the mask for the executable and every bundled module converted in the same run, and libkernel requires the baseline plus the recorded bits, keeping the old all-extensions check when no record is present. This lets --to-avx output start on a host with AVX but without AVX2, so the option no longer requires --macos. A retained extension the lowering does not cover is recorded and refuses start-up instead of raising an illegal instruction later. The classifier is conservative: a 256-bit VEX opcode outside the AVX1 list counts as AVX2, and every byte of an executable segment is decoded. Unit tests cover the classifier, the converter mask and the recorded-mask checks in libkernel; an end-to-end test converts a fixture with --to-avx and validates both output records.
VEX.128 VPACKUSDW is AVX and only its 256-bit form is AVX2; VCVTDQ2PD, VCVTPD2DQ and VCVTTPD2DQ are AVX in their 256-bit forms. Removes 0x2B from the 128-bit AVX2 list, adds 0xE6 to the AVX1 256-bit lists and covers the encodings.
Requires the full legacy instruction length before recording TZCNT, LZCNT or MOVBE, so truncated encodings add no requirement. Classifies register-source VBROADCASTSS and VBROADCASTSD as AVX2 while their memory forms stay AVX1, and covers the forms in the classifier tests.

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — changes requested

Changes are required before merge: fix the missing non-VEX host-feature classifications so --to-avx records every retained extension.

What I ran

  • Reviewed head b76859268c8e0f6da43f50fdc68f09bd3d26c919; base is main.
  • No build or tests were run. The description reports selected tests and an encoding sweep; those are author-reported, not runs performed in this review.
  • Read description.md, commits.md, manifest.json, and all 1,749 lines of diff.patch. Read changed source in ApplicationProcessParameters.cpp, HostCpu.cpp, Amd64OnlyConverter.cpp, HostCpuFeatureClassifier.cpp, LinuxElfPatcher.cpp, WindowsPePatcher.cpp, and Conversion.cpp; read files.json equivalent via the review-folder listing. All changed files were included in the prepared folder. Other changed files were not read in full.

Results

Check Result Note
Source review failed One feature-classification defect
Build and tests not run No execution environment available
Description claims not run Only checked against inspected source

Findings

Blocking

  • core/relinker/codegen/src/x86/HostCpuFeatureClassifier.cpp:102 — The VEX classifier does not record AVX-VNNI, AVX-IFMA, or AVX-VNNI-INT8/AVX-NE-CONVERT instructions. These extensions use VEX encodings, but their opcode forms are not in the BMI/FMA/F16C/AVX2 checks and fall through to 0 for VEX.128 or AVX1 classification for selected VEX.256 forms. The converter aggregates this mask for retained instructions, and libkernel then uses it to decide which CPU features are required, so a host lacking one of these extensions can pass startup and fault when such an instruction executes. Add bits and runtime CPUID checks for these supported instruction families, or conservatively classify their encodings as requiring an existing unsupported feature that causes startup rejection.

Non-blocking

None.

Not verified

  • Did not run the relinker build, unit tests, or end-to-end conversion fixture. The author reports tests on Windows 11 and a classifier sweep; these results were not independently confirmed.
  • Did not inspect every changed file's full text or validate behavior on Linux, Windows, macOS, or physical CPUs.
  • The description's claim that all retained host extensions are recorded is not supported by the classifier paths inspected.

To get this to a good state

  • Correct the classifier/runtime handling for the missing VEX instruction families, add regression coverage, and rerun the classifier and end-to-end record tests on the updated head.

Record AES, PCLMULQDQ, RDRAND, RDSEED, ADX, SHA, CLFLUSHOPT, CLWB, RDPID,
XSAVEOPT, XSAVEC, XSAVES, SSE4A, CLZERO, RDTSCP and FSGSBASE in the host
CPU record, detect the legacy encodings that carry them in the classifier,
and read CPUID leaf 80000001 EDX for RDTSCP. Extend the record, classifier
and runtime tests and the debt note.
@momgaroganuchi-crypto
momgaroganuchi-crypto force-pushed the feat/relinker-to-avx-host-cpu branch from b768592 to 4afb5db Compare November 25, 2026 20:24
@momgaroganuchi-crypto

Copy link
Copy Markdown
Contributor Author

@MarcZX10 fixed at 4afb5db799: the record now has four more bits, with real CPUID checks instead of a catch-all.

  • Runtime: HostCpuRegisters reads CPUID leaf 7 subleaf 1; the record adds AVX-VNNI (EAX bit 4), AVX-IFMA (EAX bit 23), AVX-VNNI-INT8 (EDX bit 4) and AVX-NE-CONVERT (EDX bit 5), each with its record mask. A record that sets one of them is refused on a host that lacks it, and the error names it.
  • Classifier: VEX 66 0F38 50-53 maps to AVX-VNNI; VEX 66 0F38 B4/B5 with W=1 maps to AVX-IFMA; VEX NP/F2/F3 0F38 50/51 maps to AVX-VNNI-INT8; VEX 0F38 B0, 66/F3 0F38 B1 and F3 0F38 72 map to AVX-NE-CONVERT. These are the VEX encodings from the Intel ISA reference, and they are checked before the width rule, so VEX.128 forms no longer fall through to zero.
  • Tests: HostCpuFeatureClassifierTests covers VPDPBUSD xmm/ymm, VPDPBSSD, VPMADD52LUQ, VCVTNEPS2BF16 and VBCSTNEBF162PS; core/libs/tests/HostCpu.cpp has a negative case for each of the four new bits and checks AVX-VNNI/AVX-IFMA/AVX-VNNI-INT8/AVX-NE-CONVERT are absent on the Zen 2 register set.

host_cpu_feature_classifier, amd64_only_converter and host_cpu pass on Windows 11, MinGW-w64 GCC 16.2, Release. These four families are not Zen 2 extensions, so a PS5 title cannot legitimately contain them; recording them makes the output refuse a host when such an instruction survives, which is the conservative direction the review asked for.

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — no blocking findings

I did not verify a blocking defect, but the review is incomplete because several changed files were not read.

What I ran

Reviewed head 4afb5db7993a3e0611771fcf45252ff074ac8046. I ran no commands or tests. The author-reported test results in the description were not independently verified.

Results

Check Result Note
Code review not run Review incomplete
Tests not run Author-reported results only
Negative control not run Not performed

Negative control

Could not run a negative control.

Findings

Blocking

None verified in the files read.

Non-blocking

Before treating this as approval, review the remaining changed files; the review did not cover every changed file.

Not verified

Read the prepared description, commits, manifest and diff, plus the full head contents of core/libs/prx/libc/src/ApplicationProcessParameters.cpp, core/libs/prx/libkernel/System/src/HostCpu.cpp, core/relinker/codegen/src/x86/HostCpuFeatureClassifier.cpp, core/relinker/codegen/src/Amd64OnlyConverter.cpp, core/relinker/codegen/src/x86/X64InstructionDecoder.cpp, core/relinker/codegen/tests/HostCpuFeatureClassifierTests.cpp, and portions of core/relinker/elfpatcher/src/general/ProgramHeaderLayoutBuilder.cpp, core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp, and core/relinker/elfpatcher/src/windows/WindowsPePatcher.cpp. Other changed files were not read. No files were excluded from the listing returned by the review reader. The CI results, runtime behavior on a host without AVX2, and physical-console behavior were not verified.

To get this to a good state

Nothing is required. Optional: complete review of the remaining changed files before making a formal approval decision.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@momgaroganuchi-crypto the four leaf 7 subleaf 1 families, their record bits and the CPUID reads in 4afb5db799 check out against the SDM, and the new bits are appended without moving the old ones.

One gap is left in the classifier: classifyVex (HostCpuFeatureClassifier.cpp:101-148) never records AES or PCLMULQDQ, only classifyLegacy does (:176-184). The VEX.128 forms need the same CPUID bits as the legacy ones (plus AVX):

  • VEX.128.66.0F38 DB-DF (VAESIMC, VAESENC, VAESENCLAST, VAESDEC, VAESDECLAST) -> Aes
  • VEX.128.66.0F3A DF (VAESKEYGENASSIST) -> Aes
  • VEX.128.66.0F3A 44 (VPCLMULQDQ) -> Pclmul

A znver2 toolchain emits AES intrinsics in these forms, and a host such as an i3-2100 or i3-3220 has AVX but no AES-NI, so --to-avx output would start there and then fault on the first VAESENC. The 256-bit forms belong to the separate VAES/VPCLMULQDQ bits, so keep the !Vector256 condition.

Tests that would cover it: C4 E2 71 DC C2 (VAESENC xmm) -> Aes, C4 E3 79 DF C1 00 (VAESKEYGENASSIST) -> Aes, C4 E3 71 44 C2 00 (VPCLMULQDQ xmm) -> Pclmul.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Nov 27, 2026

@ligmaSec ligmaSec left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Configured and built the submitted head on Linux with ANYPS5_RELINKER_ONLY=ON. The host_cpu_feature_classifier, cli_args, and amd64_only_converter CTests all passed. The test scope covered classification and relinker argument and converter behavior. It did not exercise the generated CPU requirement record through a native guest launch.

AI-assisted: yes.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build docs fix error Closes an issue (Fixes #N) libc libkernel needs rebase Conflicts with main, rebase needed relinker

Projects

None yet

Development

Successfully merging this pull request may close these issues.

relinker, libkernel: let --to-avx output run on Linux and Windows by recording the CPU features it still needs

8 participants