Repository navigation
fix(relinker): honor effective REX prefixes in AVX scalar lowering - #24245
Conversation
PrivacyOnion
left a comment
There was a problem hiding this comment.
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against deaf2684ee:
- Conventions:
tools/check_conventions.pypassed. - Build:
count_lowering_tests,movbe_lowering_testsbuilt 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.hppreverted tomainand the new tests kept, the suite fails:Restoring the change makes it pass again.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 - 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
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
|
Tested head Setup: Windows 11 Pro 10.0.26200, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7, Release,
Whole batch: the full This is one combined tree, not a run of this PR alone. AI-assisted: yes (Claude Code) |
|
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 I added ten encodings with a REX prefix before a legacy prefix, which the hardware ignores: TZCNT and LZCNT (
Not tested: a CPU without these extensions, and no title. Update, emulated Sandy Bridge: I also ran this branch's AI-assisted (Claude). |
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-avxleft 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
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)