runtime: lower REX-prefixed and register-form SSE4a EXTRQ/INSERTQ stubs in host_cpu #1839
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1846"
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 R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-24T19:26:23Z.
Change
Extends
sse4a_stub()andstub_for()intools/startup/host_cpu.pyto decode and lower:EXTRQ(66 [REX] 0F 78 /0 ib ib) andINSERTQ(F2 [REX] 0F 78 /r ib ib) acrossxmm0..xmm15,EXTRQ(66 [REX] 0F 79 /r) andINSERTQ(F2 [REX] 0F 79 /r) acrossxmm0..xmm15, reading the bitfield width ([5:0], with0meaning64) and bit offset ([13:8]) fromxmm_src[63:0]forEXTRQandxmm_src[127:64]forINSERTQ.Both immediate and register stubs preserve
RFLAGS, all general-purpose registers, and destinationxmm[127:64](includingdst == src), while_sse4a_decode()rejects invalid forms (mod != 3, non-zero/regon66 0F 78,offset + width > 64, missing/duplicate prefixes, and truncated encodings) by returningNone.Extends
tests/startup_host_cpu_check.pywith REXxmm8..xmm15immediate and0F 79register-form positive controls (includingdst == src,length == 0as 64, upper-64/GPR/CF preservation, and CPUID-gated native comparison whensse4ais present) as well as negative controls for invalid encodings and wrong-offset/wrong-register stubs.Verification
Commit:
f738af81.\out\pe-env\Scripts\python.exe -B tests/startup_host_cpu_check.py:PASS (1162 insertq/extrq results, clzero with two moved-instruction forms, 5 clwb and 2 wait rewrites, clwb/clflushopt NOPs for every addressing form, 4 non-temporal stores, rdpru, rdpid into 7 registers, sha-1 and sha-256 of 4 messages through the seven sha operations with 112 native results; native comparison: rdpid, sha)(host CPU lackssse4aandclzero, so native comparison ran forrdpidandsha; all 1162insertq/extrqchecks ran against the software bitfield model).ctest --preset dev -N:Total Tests: 1025.ctest --preset dev --no-tests=error -R "^startup_host_cpu$" -V:1/1 Test #309: startup_host_cpu ... Passed(0.22 sec).ctest --preset release --no-tests=error -R "^startup_host_cpu$" -V:1/1 Test #309: startup_host_cpu ... Passed(0.17 sec)..\out\pe-env\Scripts\python.exe -B tests/form_coverage_check.py:form coverage checks passed..\out\pe-env\Scripts\python.exe -B tests/docs_links_check.py:Self-test passed. Checked 18 Markdown files and 369 links..\out\pe-env\Scripts\python.exe -B tests/bidi_controls_check.py:Self-test passed. Checked 1320 tracked source, script and build/CI files: no bidirectional controls.Evidence and follow-up
All fixtures in
tests/startup_host_cpu_check.pyare assembled in-memory in Python without external game files or dumps. Native SSE4a hardware comparison remains gated on CPUID leaf0x80000001ECX[6] (sse4a) and was skipped on this host becausesse4ais absent.Checklist
AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T19:31:37Z.
Review: COMMENTED
Peer review of head
f738af8161f786b25a1aed79615b1adf8c27d522.Result: no P1/P2 defects found in the local verification. One documentation mismatch (P3) below.
Read in full: the diff of
tools/startup/host_cpu.pyandtests/startup_host_cpu_check.py, plus the caller intools/startup/debug_launch.py(rewrite_for_host, which now receives the length fromstub_for). Checked by hand against the encodings:F2or66, then an optional REX, then0F 78(immediate) or0F 79(register). Lengths come out as 6/7 and 4/5. Register-form destination is ModRM.reg with REX.R, control source is ModRM.rm with REX.B. The immediate EXTRQ takes /0 with the operand in rm plus REX.B. Anything with mod != 3, a wrong prefix, a REX before the legacy prefix, a duplicate prefix or a truncated encoding returns no stub.pextrq rdi, xmm_src, 1. The mask is built withshr rsi, clafterneg ecx, so length 0 gives a full 64-bit mask; the index comes from bits 13:8. The same register as source and destination is handled. RSI and RDI are saved and restored, and RAX, RCX, RDX and the flags as before.pinsrq. I decoded the byte strings for both stubs and they match the comments.Win2025 (coordinator run, Windows Server 2025 build 26100):
tests/startup_host_cpu_check.pyexited 0 andctest -R '^startup_host_cpu$'passed 1/1 in Debugdev(0.16 s) and Release/O2(0.15 s).Linux checks run locally in a clean worktree at this head:
git diff --check origin/main...HEAD: cleantests/bidi_controls_check.py: passed (1320 files)tests/startup_host_cpu_check.py: passed, 1162 INSERTQ/EXTRQ results. This host has SSE4a, so the native-instruction comparison ran too (the output listssse4aamong the native comparisons), which checks the stubs against the processor and not only against the model.Mutations (local, each reverted with
git checkout -- .; tree clean). Each madestartup_host_cpu_check.pyexit 1:insertq xmm0, xmm9, 16, 16gives0x123456789abcdef, expected0x12345673210cdef.shr rcx, 8changed toshr rcx, 9:reg insertq xmm0, xmm1 (16, 16)mismatch.shr rdx, clchanged toshl rdx, cl:reg extrq xmm0, xmm1 (16, 16)returns 0.P3 (documentation):
docs/startup-runtime.mdstill describes the old limits. Line 946 lists the instructions as "(xmm0-7)", line 955 says the refused forms include "register-countINSERTQ/EXTRQ, xmm8-15", and line 958 says "288INSERTQ/EXTRQresults" (the check now reports 1162). The module docstring inhost_cpu.pywas updated, but this section was not, so the docs now contradict the code.docs_links_check.pydoes not cover this.AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T19:51:01Z.
Review: COMMENTED
Review of head
b2abbcc32a7854bdf7af685d10ec8915c55f2404(two commits:f738af81,b2abbcc3; baseorigin/main).Verdict: no P1, no P2. Two P3 notes. This is the first time I run the register forms against the real instruction: the host I used has SSE4a (AMD), so the check's native comparison was live.
What I read The whole diff of
tools/startup/host_cpu.py,tests/startup_host_cpu_check.pyanddocs/startup-runtime.md, and the one caller ofstub_for(tools/startup/debug_launch.py).Semantics, checked against the encodings and the stubs byte by byte
F2|66 [REX] 0F 78|79 /r. Register forms: length 3 + prefix (+1 with REX); immediate forms: 5 + prefix (+1 with REX).ModRM.modmust be 3 (memory operands refused). ImmediateEXTRQneedsModRM.reg == 0and usesrm | REX.Bas both operands. Every other form takes the destination fromreg | REX.Rand the source or control fromrm | REX.B. That is the operand order LLVM gives these instructions (MRMSrcReg: first operand inreg, mask/source inrm).EXTRQ xmm, xmm. The control comes from the low 64 bits of the source: length = bits 5:0 (0 means 64), index = bits 13:8. The stub doesshr rdx, clwithrcx = ctrl >> 8(the shift count is taken modulo 64, so bits 13:8), then masks with-1 >> ((-len) & 63). Bytes decoded by hand:48 89 c1; 48 c1 e9 08; 48 d3 ea; 89 c1; f7 d9; 48 c7 c0 ff ff ff ff; 48 d3 e8; 48 21 c2: correct.INSERTQ xmm, xmm. Data is the low qword of the source; length is bits 5:0 and index bits 13:8 of the source's high qword (bits 69:64 and 77:72 of the register). The stub reads the high qword withpextrq rdi, xmm_src, 1, builds the mask the same way, shifts data and mask by the index, clears the field in the destination and ORs the data. Bytes decoded by hand:89 f9; f7 d9; 48 c7 c6 ff ff ff ff; 48 d3 ee; 48 21 f0; 48 89 f9; 48 c1 e9 08; 48 d3 e0; 48 d3 e6; 48 f7 d6; 48 21 f2; 48 09 c2: correct.rsiandrdiare pushed and popped in the right order (push ... rsi, rdi/pop rdi, rsi, rdx, rcx, rax, popfq).index + length > 64stay refused. For the register forms the control is only known at run time, so the stub computes some value there; the processor's result is undefined too, which the docstring already says._xmm_modrmsets REX.W and REX.R/REX.B from the register numbers, so xmm8-15 andr8-r15are addressed correctly; a REX before the legacy prefix, two REX bytes, an F3 prefix and no prefix are all refused.stub_fornow returns the real length (4 to 7).plan_replacementmoved the following instructions for a 4-byte and a 5-byte case in my check and padded the rest with NOPs.Commands run locally on Linux (Python 3.14.8; iced-x86 installed in a scratch folder outside the repository; CPU reports
sse4a)python3 -B tests/startup_host_cpu_check.py: PASS (1162 insertq/extrq results, CLZERO, CLWB, wait, non-temporal stores, RDPRU, RDPID, SHA; "native comparison: clzero, rdpid, sha, sse4a").stub_for(all four forms with and without REX, memory operand, immediate EXTRQ withreg != 0, truncated, F3, no prefix, REX first, double REX,len 0 idx 1,2121which is 33+33 > 64, trailing bytes): each result is the expected length orNone.python3 -B tests/docs_links_check.py(18 files, 369 links),python3 -B tests/bidi_controls_check.py(1320 files): pass.git diff --check origin/main...HEAD: clean.Local mutations of
tools/startup/host_cpu.py(each restored withgit checkout -- ., tree clean afterwards), each caught bystartup_host_cpu_check.pyModRM.reg != 0accepted.Findings
INSERTQ xmm, xmmwith the same register on both sides the test expects the control word to survive in the high qword. Both are undefined on the processor, so nothing depends on it, but one sentence in the doc would say it.Not run: the debugger path that writes the stubs into a live process (
debug_launch.py) and a Windows run; I only exercised the stub generation,plan_replacementand the stubs on this host.AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-24T19:43:51Z.
Updated
docs/startup-runtime.mdto reflect REX (xmm0-15) and register-formINSERTQ/EXTRQsupport, the current refused forms, and the 1162-result check count (tests/startup_host_cpu_check.py,tests/docs_links_check.py,tests/bidi_controls_check.py, andstartup_host_cpuindevandreleaseall pass). AI-assisted.