Skip to content

fix(relinker): honor effective REX prefixes in AVX scalar lowering - #24245

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
u-instinct11:fix/avx-legacy-rex-order
Nov 27, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
u-instinct11:fix/avx-legacy-rex-order

Conversation

@u-instinct11

Copy link
Copy Markdown
Contributor

What

The x86 decoder already treats a REX prefix followed by a legacy prefix as ineffective. The AVX lowering operand reader stopped at that REX byte. As a result, --to-avx left some TZCNT, LZCNT and MOVBE instructions in the output. On a host lacking BMI1 or LZCNT, these count instructions can execute as BSF or BSR and return a different result.

Make the lowering reader follow the same prefix order as the instruction decoder. A later legacy prefix clears an earlier REX, while a REX immediately before the opcode still selects the operand width and extended registers.

Tested

Windows 11 x86-64, Intel Core Ultra 9 275HX, MinGW GCC 15.2. Both new operand tests failed with the original reader. The count, MOVBE, BLS, decoder-length and AMD64 converter CTest suites passed with the change. The conventions check passed.

Three synthetic ELF inputs exercised relinker --macos --to-avx, each with two copies of one instruction. Main emitted zero substitutions for each input. The change emitted both expected substitutions for each. A native CPU control independently checked the 16-bit count results and the MOVBE byte order. These checks use synthetic inputs.

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

@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 deaf2684ee:

  • Conventions: tools/check_conventions.py passed.
  • Build: count_lowering_tests, movbe_lowering_tests built cleanly.
  • CTest (count_lowering, movbe_lowering):
    1/2 Test #6129: count_lowering ...................   Passed    0.18 sec
    2/2 Test #6130: movbe_lowering ...................   Passed    0.25 sec
    100% tests passed, 0 tests failed out of 2
    
  • Negative control: with core/relinker/codegen/include/codegen/x86/ScalarPrefixes.hpp reverted to main and the new tests kept, the suite fails:
    1/2 Test #6130: movbe_lowering ...................***Failed    0.02 sec
    2/2 Test #6129: count_lowering ...................***Failed    0.01 sec
    0% tests passed, 2 tests failed out of 2
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified ReadLegacyEncoding prefix parsing in core/relinker/codegen/include/codegen/x86/ScalarPrefixes.hpp correctly captures the last adjacent REX prefix and clears earlier REX bytes when followed by legacy prefixes (0x66, 0xF3, 0xF2, or segment prefixes).
    • Verified boundary conditions: loop index bounds (index < length) are strictly respected and Opcode index offset correctly marks the byte following all processed prefixes.
    • Verified decode logic in CountLoweringTests.cpp (decoding test with ineffective REX byte 0x4D followed by F3 66 0F ...) and MovbeLoweringTests.cpp (prefixed test with 0x4D 0x67 0x48 0x0F ...) ensuring correct operand width, register extension, and memory prefix parsing.
    • Verified AGENTS.md compliance: no comments added to code, no unnecessary debt files or unapproved dependencies, clean check_conventions and diff checks.

Looks Good To Me

@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 — looks good

The reviewed change correctly tracks REX prefixes across legacy prefixes, and I found no blocking defect in the code inspected.

What I ran

Reviewed head deaf2684eef397dcbc0296fb97adbf72a6dc7b64. Read the prepared diff and all three changed files with the pinned repo_read.py reader. No tests were run.

Results

Check Result Note
Changed code review passed Three reviewed files
Tests not run Author-reported results not independently checked

Findings

Blocking

None.

Non-blocking

None.

Sources

Not verified

Tests and author-reported platform results were not independently verified. Read: ScalarPrefixes.hpp, CountLoweringTests.cpp, and MovbeLoweringTests.cpp; excluded: none of the changed files. No separate files.json was available; the reader's listing reported all three changed files included.

To get this to a good state

Nothing is required.

@edglz edglz 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.

Summary

Correct fix. Per the Intel SDM (Vol. 2, 2.2.1), a REX prefix only takes effect when it immediately precedes the opcode or the 0F escape; a REX followed by a legacy prefix is ignored. Recording REX inside the prefix loop and clearing it on any later legacy prefix implements exactly that, and the ignored byte no longer leaks into the operand decoding.

Verification

Environment: macOS 26, Apple M4 (arm64), Apple clang 21.0.0, Python 3.15.2, relinker-only build.

Check Result
count_lowering, movbe_lowering on this head Pass
Same test files against main Both fail: An ineffective REX prefix changed the count operands / MOVBE did not use only the REX prefix adjacent to the opcode
CI relinker jobs (Linux, Windows, macOS, ASan) Green

Notes

  • With several REX bytes before the opcode the last one wins, which matches hardware behaviour for this undefined-but-observed case.
  • Not verified: the execution cases, unavailable on arm64.

Verdict

Approved.

@kiwe147

kiwe147 commented Nov 21, 2026

Copy link
Copy Markdown

Tested head deaf2684ee, applied with git apply on main fd4aeed483 together with the other 29 PRs of one batch (#24576, #24603, #14196, #14527, #17718, #18671, #22753, #22792, #23503, #24480, #24553, #24714, #21236, #21765, #21987, #22030, #22107, #22397, #22571, #23158, #23816, #24067, #18401, #19101, #19398, #19657, #21563, #24584, #24628). All applied without conflicts and the whole tree built.

Setup: Windows 11 Pro 10.0.26200, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON. GPUs: NVIDIA GeForce RTX 3080 (driver 621.24) and the AMD Radeon iGPU of the CPU (gfx1036, driver 32.0.21036.18). No validation layer. iGPU run: ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 ANYPS5_GPU="AMD Radeon", agc_/video tests only.

tests RTX 3080 iGPU
count_lowering, movbe_lowering (2 tests, matched by name to the test file this PR changes) all pass outside the iGPU subset

Whole batch: the full ctest -j 12 (6523 tests) on the RTX 3080 has 7 failing tests; the 4 that also fail on main fd4aeed483 are agc_driver_stencil_plane_memory, agc_driver_msaa_dcc_unwatch, agc_storage_write_back_host_retile, and pr_overlap_names, which needs a newer Python than my 3.10. Intermittent in this run (pass when rerun alone, or known flaky): agc_driver_store_over_untracked, agc_driver_store_over_untracked, agc_storage_write_back_host_retile. Traced to other PRs of the batch, each by reverting it and testing again: #19101 (guest_video_out_window_margins_bounds); none is in this PR's area. Failing tests I could not trace to a single PR (reverting the likely candidates did not clear them): agc_driver_image_gather_lod. The 2125 agc_/video tests on the iGPU have 5 failing: agc_driver_image_gather_lod, agc_driver_stencil_plane_memory, agc_driver_store_over_untracked, agc_storage_write_back_host_retile, guest_video_out_window_margins_bounds (agc_driver_stencil_plane_memory also fails on main).

This is one combined tree, not a run of this PR alone.

AI-assisted: yes (Claude Code)

This was referenced Nov 21, 2026
@eduardogeraldes

eduardogeraldes commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Independent hardware comparison of deaf268 against main (d3a7b0b) on Linux x86-64 (Ubuntu 24.04, GCC 13.3), on a host with BMI1, LZCNT and MOVBE. I built each tree's codegen sources and took every lowering the --to-avx and --to-rosetta matchers emit over a sweep of VEX and legacy encodings. Each stub and the native instruction run from the same state, and the harness compares all 16 YMM registers, the 15 general registers other than RSP, RFLAGS (masking the flags the SDM leaves undefined), MXCSR and a 64 KiB memory area.

I added ten encodings with a REX prefix before a legacy prefix, which the hardware ignores: TZCNT and LZCNT (4D F3 0F BC C1, 4D F3 66 0F BC C1, 49 66 F3 0F BD C1, 41 F3 48 0F BD D9, ...) and MOVBE (4C 66 0F 38 F0 18, 4C 2E 0F 38 F0 18, ...).

  • On main, --to-avx leaves nine of the ten unconverted, so on a CPU without LZCNT and BMI1 the counts would run as BSR and BSF with wrong results, and MOVBE would fault.
  • With this PR all ten are lowered and match the hardware in 500 random cases each. The rest of the sweep (1,548 --to-avx and 322 --to-rosetta encodings) still gives no differences.

Not tested: a CPU without these extensions, and no title.

Update, emulated Sandy Bridge: I also ran this branch's --to-avx stubs, including the ten REX-before-prefix forms above, under QEMU 8.2.2 user mode with -cpu SandyBridge, which raises #UD for MOVBE, BMI1/2 and AVX2 and runs LZCNT and TZCNT as BSR and BSF. The native instruction ran on the host and the stub under QEMU from the same state (6,076 lowered encodings with aliased registers, 97,520 cases): no stub faulted and every result matched the native one, so the lowered counts and byte swaps are correct on that CPU class.

AI-assisted (Claude).

@oneandonlydean
oneandonlydean merged commit e72de71 into boykopovar:main Nov 27, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants