fix(relinker): lower ADCX and ADOX for Rosetta #7173
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7173"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-17T14:53:15Z.
What
Lower ADCX and ADOX instructions in
--to-rosettaoutput using baseline ADC while restoring unrelated arithmetic flags. ADCX maps ADC's carry-out to CF; ADOX maps that carry-out to OF, as required by the two independent carry chains. The lowering handles 32-bit and 64-bit register or memory sources, REX extensions, stack-relative operands, and RIP-relative relocation; it rejects RSP as the destination because the trampoline cannot safely change its own stack pointer. Adds decoder, converter, and generated-stub regression coverage, and records the unsupported RSP-destination case in relinker debt.Tested
check_conventions.py --base upstream/mainpassed.pr_overlap.py --branch codex/rosetta-adx-flags-7157reported no overlap with open PRs; unmergeable shared-file candidates were manually reviewed and no duplicate lowering was found.b9f5cd818f35a4aa7424fedcebd6a3885ecfd8da. New head65b4f1fc693e50ac8f14a32a7c7825479f31f40fis based onmain0e9dc7bc4c9755c3ce803f6bbdff9dc9f25d68bb;git diff --checkand the merge preview passed. Bothadx_loweringandguest_load_rangesremain registered in CMake. The conventions check had 0 errors and 1 debt-length warning. Build and CTest were not rerun after rebasing.Checklist
main; no other open PR implements the same functions.mdfiles or images added to the repositoryImported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-17T15:03:37Z.
Thanks @MarcZX100. The Rosetta gating looks right: both hooks return early unless the target is Rosetta, so x86 codegen is unchanged. The decode, RSP handling and flag-preservation checks look good too, and ADCX is correct.
One blocker on ADOX:
ADOX writes the wrong OF. For ADOX,
EmitAdxmasks ADC's flags with0x800, so the result OF is ADC's OF, which is signed overflow. ADOX's OF output is the unsigned carry-out (OF:DEST ← DEST + SRC + OF), and ADC leaves that in CF. Checked against nativeadoxon an ADX host (gcc -madx):0x7FFFFFFFFFFFFFFF + 0, OF=10x8000000000000000, OF=0 (CF preserved)0xFFFFFFFFFFFFFFFF + 1, OF=00, OF=1So any ADOX carry chain (multi-precision add/multiply in bignum, crypto or hashing code) gets wrong sums from the second limb on, silently. A fix: for ADOX, take the carry from bit 0 and move it to bit 11, e.g. replace
and scratch, 0x800withand scratch, 1+shl scratch, 11.The test currently encodes the same mistake.
AdxLoweringTests.cppcomputes the expected ADOX flag as signed overflow (~(d^s) & (d^r) & sign). Its only execution input,0x7FFF…F + 0with carry 1, is exactly where signed overflow and unsigned carry differ, so it passes because of the bug. Could you:0xFFFF…F + 1and a no-carry case for each width;adcx/adoxwhen CPUID.(EAX=7,ECX=0):EBX[19] (ADX) is set, over a small grid of (dst, src, CF, OF). The Linux CI runners have ADX.Once that's on a new head we'll re-review and run full CI.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-17T15:05:13Z.
Follow-up manual overlap review after
mainadvanced: the overlap tool reports unmergeable shared files in #5021, #6044, #6111 and #6314. #6111 changes RDTSCP handling in the same matcher but does not implement ADCX/ADOX; #6314 adds game-patch sources/tests to CMake; #6044 reorganizes CTest registration and its stale branch removes older ANDN entries, so the ADCX/ADOX registrations must be retained if it is integrated; #5021 is a broad AOT change with older CMake/debt edits. I found no duplicate ADCX/ADOX lowering. These are manual file reviews because the bot cannot compare those branches cleanly; a rebase/reconciliation may be needed.Imported from boykopovar/AnyPS5. Originally posted by Destroyers212x on 2026-10-17T15:06:32Z.
Rosetta results for this PR (
63c454aa6), M1 with macOS 26.6.2, relinker-only Release build with-DCMAKE_OSX_ARCHITECTURES=x86_64, so the tests run under Rosetta: 179/179 passed, includingadx_lowering(its execution part runs on macOS x86-64) andto_rosetta.One semantic problem in ADOX's output flag, though. ADOX writes the unsigned carry-out of
DEST + SRC + OFto OF: it is ADCX with OF in place of CF, which is what lets the two carry chains interleave. The lowering here writes the signed overflow of the ADC to OF instead, and the test encodes the same rule (AdxLoweringTests.cpp:146,overflow = ~(d ^ s) & (d ^ r) & sign), so both agree and the test passes.Two 32-bit cases with OF clear on entry that show it:
0x56789000 + 0x5d4c306a0xffffffff + 0xffffffffI ran an independent execution test against this PR's
EmitAdxunder Rosetta. It covers every destination except RSP, sources 6, the destination itself, 11 and RSP, 32/64-bit, all-zero, all-one and mixed inputs, the memory forms ([rsi],[rsi+16],[rsp+0x40],[rdx+rcx*8],[rbp+16], RIP-relative), and entry flags0x2,0x8D7, CF only and OF only. The results and all other flags are right, and ADCX passes every case. ADOX fails 708 cases in both widths, all on OF only. The test is on Destroyers212x/AnyPS5feat/rosetta-adx-lowering(check()computes the expected flags) if you want to reuse it. I won't open a competing PR.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-17T15:15:45Z.
Fixed in
f5e857c48. The ADOX path now takes ADC’s unsigned carry-out from CF and maps it into OF; it no longer copies ADC’s signed overflow. The execution test now checks 32- and 64-bit cases across boundary operands and both carry-in values, and includes a native ADX differential check when CPUID reports ADX support. This host has no ADX, so that optional differential path was skipped here.adx_lowering_testspasses, the full Linux relinker-only CTest suite passes 188/188,git diff --checkpasses, and conventions pass.Imported from boykopovar/AnyPS5. Originally posted by Destroyers212x on 2026-10-17T15:17:57Z.
Re-ran
f5e857c48under Rosetta (M1, macOS 26.6.2, x86-64 relinker-only build):adx_loweringandto_rosettapass, and the independent execution test that failed 708 ADOX cases on63c454aa6now passes every case for both instructions, both widths, all register pairs, the memory forms and CF/OF set and clear on entry.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-17T15:18:44Z.
Follow-up: after the broader run, I made the carry-to-OF shift explicitly encode
SHL r64, 11(C1 /4) and reran the suite on the pushed head. The full Linux CTest suite again passes 188/188; the focused ADX test and conventions also pass. The native ADX differential path remains CPUID-gated and skipped on this host.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-17T15:23:10Z.
Thanks for rerunning the independent Rosetta matrix. Your results on
f5e857c48confirm the ADOX carry mapping across the reported cases. I then made the one-bit extraction explicitly useSHL r64, 11(instead of a rotate that produced the same single-bit result) in8e83a61a2; the focused test and full Linux CTest suite pass again on that pushed head. Hosted cross-platform relinker jobs are still running.Imported from boykopovar/AnyPS5. Originally posted by TheSovietWaffle on 2026-10-17T15:23:51Z.
Windows data point: head
8e83a61a2as-is, Release, Windows 10 Pro 22H2 (19045), WinLibs GCC 15.2.0 r7, x86-64 host (no Rosetta).adx_loweringpasses 5 of 5, including its generated-stub execution checks.Not checked: the rest of the relinker suite, Rosetta.
AI-assisted: yes (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-17T15:24:54Z.
Thanks for checking the final head on Windows. That confirms the generated-stub execution checks pass there as well; the Windows hosted relinker job is still pending, and your result covers the focused test rather than the full suite.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-17T15:26:43Z.
Hosted relinker CI has now completed on head
8e83a61a2: macOS, Linux and Windows jobs all pass, as do conventions and eligibility. The broader Linux/Windows/register-context/write-watch jobs were skipped by path filters; the separate local full Linux relinker-only suite passed 188/188.Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-18T02:33:41Z.
Review: COMMENTED
Native ADX run of the CPUID-gated differential, which no run here has exercised yet: the earlier hosts had no ADX (Ivy Bridge Xeon, Rosetta on M1).
Setup: head
8e83a61a2(merges cleanly intomain195d0d08a), Linux x86-64, i7-8700 (adxin/proc/cpuinfo), GCC 15.2, relinker-only Release.adx_lowering_tests: 3/3 pass, with thehasAdxbranch taken. The relinker suite passes 188/188.AdxLowering.cppfrom63c454aa6(ADOX OF = ADC signed overflow) back and kept the current test:One note on the Windows data point above: the execution section of
AdxLoweringTests.cppis under#if (defined(__linux__) || defined(__APPLE__)) && defined(__x86_64__). So on Windows only the decode and conversion checks run, not the generated-stub execution.Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-18T05:37:38Z.
Thanks for running the current head on native ADX hardware. This confirms both the reference-flag check and the native differential catch the ADOX carry regression; the Windows scope note is useful too. I have no further validation request.
Imported from boykopovar/AnyPS5. Originally posted by Destroyers212x on 2026-10-18T11:09:23Z.
Rosetta results for this PR (
8e83a61), M1 with macOS 26.6.2, relinker-only Release build:CMAKE_OSX_ARCHITECTURES=x86_64): 179/179 CTest passed.adx_loweringran its execution checks: the(__linux__ || __APPLE__) && __x86_64__guard is active, and the binary run directly prints "ADX lowering tests passed". Rosetta does not report ADX (CPUID.(EAX=7,ECX=0):EBX[19] = 0), so the native ADX differential path was skipped here too.adx_loweringruns only the decode and convert checks).I also wrote a separate execution check under Rosetta, linked against
build-x64/core/relinker/librelinker_core.a. Its reference model follows the ADCX/ADOX entries in Intel SDM Vol. 2A: dest + src + CF/OF, the carry-out goes only to CF/OF, and the 32-bit form zero-extends. It covered both instructions, both widths, every destination except RSP, every source register including RSP, and[rsp+8]and[rsp-8]memory sources (the second one is inside the red zone). Each case started with random values in all 16 registers and random CF/PF/AF/ZF/SF/OF. 6480 cases, 0 failures. In every case the destination, CF/PF/AF/ZF/SF/DF/OF and all the other GPRs matched, and the 128 bytes below the original RSP were unchanged.A gap in the PR's test: in the 32-bit cases every destination value is ≤ 0xFFFFFFFF, so the test doesn't check that bits 63:32 of the destination get cleared. The lowering does clear them (my check started with random upper bits), but a destination like
0xDEADBEEF00000001invalueswould cover it.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-18T12:24:50Z.
Added
0xDEADBEEF00000001to both the generated-stub and CPUID-gated native differential operand matrices. In the 32-bit cases it supplies nonzero upper destination/source bits while the expected result remains zero-extended, so those checks now catch a failure to clear bits 63:32. The focused Linux test passes (1/1); conventions andgit diff --checkpass. Commit:010ce9b02.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-19T14:57:50Z.
Rebased on
mainatc92eb8da1e8d311c29a9de86ec5328f8a83d841c. The update preserves the remote-added upper-bits regression commit and combines the ADCX/ADOX lowering with the recent RDTSCP matcher change.Validation on Linux x86-64 with GCC 12.2, Release,
ANYPS5_RELINKER_ONLY=ON:adx_loweringand theamd64_only_converterregression suite.check_conventions.pyreported zero errors and one existingdebt-lengthwarning for the long--to-rosettasummary indocs/dev/debt/relinker.md.The rebased head is
fd7342a5109fdb2c3a80253a6c0c88b08f151719; GitHub checks are starting now.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-02T04:56:52Z.
Review: COMMENTED
Replies checked — EIP-relative ADCX/ADOX bug still present, docs duplicate confirmed
The code at this head still drops the RIP-relative relocation for address-size-prefixed ADCX/ADOX, and the docs entry is duplicated after a merge with main.
What I ran
65b4f1fc693e50ac8f14a32a7c7825479f31f40f(OPEN).gh apicomments, review comments and reviews on #7173, newer than 2026-10-19T14:57:50Z, excluding MarcZX10 and bots.gh pr view 7173 --json statusCheckRollup;git show 65b4f1fc:<path>for the three files below.git merge-tree --write-tree upstream/main 65b4f1fc, then a grep count of the mergedrelinker.md.Results
conventions,relinker(ubuntu, macOS, windows),relinker-asanwindowsaggregate,linux,register-context,write-watchSKIPPEDFindings
DecodeAdxclearsRipRelativewhen the 0x67 prefix is present, andEmitAdxthen copies the instruction without a relocation. Confirmed by reading:AdxLowering.cppL168-L170 clears the flag, and L204-L206 relocate only when it is set. The test at L46 pins!RipRelative, so the current tests lock in the gap. Answer needed: fix it (preserve the target including 32-bit wrap) and add execution tests for ADCX and ADOX at a moved address. Not reproduced here.AdxLoweringTests.cppL55-L62. Answer needed: remove it and keep the other checks.--to-rosettalowers what Rosetta lacks" entries after a merge with main. Confirmed:relinker.mdL49-L50 at head, and two entries in the merged tree. Answer needed: keep one entry with the ADCX/ADOX wording on main's current line.Sources
github.com/boykopovar/AnyPS5@65b4f1fc69/core/relinker/codegen/src/x86/AdxLowering.cpp (L168-L170)github.com/boykopovar/AnyPS5@65b4f1fc69/core/relinker/codegen/src/x86/AdxLowering.cpp (L204-L206)github.com/boykopovar/AnyPS5@65b4f1fc69/core/relinker/codegen/tests/AdxLoweringTests.cpp (L46)github.com/boykopovar/AnyPS5@65b4f1fc69/core/relinker/codegen/tests/AdxLoweringTests.cpp (L55-L62)github.com/boykopovar/AnyPS5@65b4f1fc69/docs/dev/debt/relinker.md (L49-L50)Not verified
Imported from boykopovar/AnyPS5. Originally posted by catminou4 on 2026-11-25T05:48:51Z.
Review: CHANGES_REQUESTED
Reviewed merged into current
main(61d110125e, merge is textually clean) on Ubuntu 22.04, GCC 11.4, relinker-only build (-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON).ctest -R 'adx|amd64_only|rosetta|converter'(adx_lowering,to_rosetta, ...) all pass. Full relinker-onlyctest -j8: 1116/1117 pass; the one failure,tools_headers_manifest, is a Python tools test that does not touch these files.EmitAdx: the 144-byte frame stays below the red zone, the saved RFLAGS at[rsp+8]gets only CF (ADCX) or OF (ADOX) replaced from theADCresult, theBT [rsp+8], 11seeds CF from OF for ADOX, and RSP-based memory sources get +144. That matches the SDM semantics.Changes requested, in the debt only:
docs/dev/debt/relinker.mddirectly. CONTRIBUTING says debt entries are added and changed through a fragment indocs/dev/debt/changes/.--to-rosettaentry is an old copy of the line. Merged intomainit drops text already onmain: "ANDN, BLSI, BLSMSK, BLSR, SHLX, SHRX, SARX, RORX, BZHI, BEXTR and MULX are lowered" (it becomes "ANDN is lowered"), the 200,000-random-input BMI1/BMI2 result on the M5, and "SHA and AVX-512F" in the M5 fault list. It also leaves "The conversions are covered by the converter tests." twice.check_conventions.pyon the merge warnsdebt-lengthfor that entry (1842 characters).Please rebase, drop the edit of the existing line, and put the ADCX/ADOX change (remove "ADCX/ADOX fault under Rosetta and are not converted ..." and add the new entry) in a fragment. #7531 carries the same commits plus more tests; one of the two should be closed.
Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T12:01:07Z.
Review: COMMENTED
Built this head with
ANYPS5_RELINKER_ONLY=ONand ranadx_loweringon Linux x86-64. It passed the ADCX and ADOX lowering cases. Native Rosetta execution remains outside this run.AI-assisted: yes.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.