Skip to content

cpu: lower baseline 128-bit SSE3 and SSE4.1 floating-point instructions - #602

Open
MarlomMedeiros wants to merge 3 commits into
R0Y00:mainfrom
MarlomMedeiros:cpu-sse34-fp
Open

MarlomMedeiros wants to merge 3 commits into
R0Y00:mainfrom
MarlomMedeiros:cpu-sse34-fp

Conversation

@MarlomMedeiros

Copy link
Copy Markdown
Contributor

Summary

  • tools/ffx_version_query.py: lower 22 legacy-encoding 128-bit SSE3/SSE4.1 floating-point instructions for tracked xmm0-xmm2 (wide0-wide2): addsubps/pd, haddps/pd, hsubps/pd, movddup, movsldup, movshdup, lddqu, roundps/pd, roundss/sd, dpps, dppd, blendps/pd, blendvps/pd, insertps, extractps. FP arithmetic emits the matching host intrinsics; data movement is byte-level; upper YMM bytes are preserved. MMX, VEX, untracked XMM and movntdqa stay rejected.
  • tests/cpu/sse34_fp.{asm,def,py}: source-built MASM fixture with 33 exports, three-way differential check (native DLL, lifted DLL, independent IEEE-754 Fraction reference), NaN/denormal/-0.0 cases, immediate sweeps (all 256 insertps imm8 values, all blendps/blendpd immediates, 16+1 dppd and 18 representative dpps masks, all four rounding modes plus the MXCSR-select and NO_EXC bits), narrow memory widths (m32/m64/m128 guarded), 4 mutated lifted controls that must mismatch, rejection checks (MMX, VEX, xmm3+, movntdqa), and a CPUID SSE3+SSE4.1 probe that exits 77 on incapable hosts.
  • CMakeLists.txt: registers cpu_aot_sse34_fp with SKIP_RETURN_CODE 77, same shape as the SSE2 batches.
  • docs/experiments.md: records the forms, the fixture checks and the reproduction commands.

Test plan

Commit under test: 3ba5fec on cpu-sse34-fp (rebased on current main).

  • cmake --build --preset dev --target cpu_sse34_fp_lifted
  • ctest --preset dev --no-tests=error -R '^cpu_aot_sse34_fp$' -V → 1/1 Passed: 950 baseline 128-bit SSE3/SSE4.1 FP cases across 33 exports matched native DLL and reference; 4 mutated controls mismatched as required
  • Neighbours sharing lower(): ctest --preset dev --no-tests=error -R 'cpu_aot_(sse2_int|sse2_sat_mul|sse41|sse41_int|sse41_extend|sse41_42_misc|string_ops|version_query|wide_arith)' → 8/8 Passed.
  • Environment: x64 Windows, MSVC 19.52.37029.0 / ML64 14.52.37029.0, host CPU exposes SSE3+SSE4.1 (CPUID probe not skipped).

Copilot AI balanced review requested due to automatic review settings November 22, 2026 15:11

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@legacy-code-archaeologist

Copy link
Copy Markdown
Contributor

cpu_aot_sse34_fp fails at 3ba5fec on an Intel Core i7-6820HQ (Skylake), Windows 11 26100.9168, MSVC/ML64 19.52.37029, dev preset. The other 113 cpu_ tests pass, with the usual cpu_aot_crypto_sha capability skip.

ctest --preset dev --no-tests=error -R cpu_aot_sse34_fp -V
AssertionError: dpps_op(arg3=0xf4): a=4523c17f0000807f2301807f000080ff b=2143c5ff0000807f0000803f00000000
  expected=00000000000000004523c17f00000000 native=00000000000000000000c0ff00000000 lifted=00000000000000000000c0ff00000000

Native and translated agree; only the Fraction reference differs. Decoded lanes (imm 0xf4: all four products, result in lane 2):

lane a b product
0 0x7fc12345 QNaN 0xffc54321 QNaN QNaN from a
1 0x7f800000 +inf 0x7f800000 +inf +inf
2 0x7f800123 SNaN 0x3f800000 1.0 quieted SNaN
3 0xff800000 −inf 0x00000000 0.0 invalid → indefinite
  • Reference: it sums (t0+t1)+(t2+t3) with "first NaN operand wins" and returns lane 0's NaN, 0x7fc12345.
  • Skylake: it returns the indefinite 0xffc00000 produced by the −inf × 0 lane. Which NaN DPPS returns when several lanes are NaN is therefore vendor-dependent, and the reference encodes one vendor's choice. The description does not name the CPU of the passing run. If it was AMD, this would be the Intel/AMD difference.

Two related observations:

  1. The generated translation lowers DPPS to _mm_dp_ps(...) (out/build/dev/cpu_sse34_fp_lifted.cpp), so it runs the host's own DPPS. Native and translated therefore always agree on DPPS on the same host. The real check is against the reference, and on another vendor the translated result follows that vendor rather than the reference.
  2. To keep the check portable, the multi-NaN DPPS cases could accept the vendor-dependent choice (for example, require that the result is a NaN that native and translated share), or move to a separate, documented vendor-specific case. If the reference is meant to pin the guest CPU's behavior, the translation would need its own NaN selection instead of _mm_dp_ps.

The head also no longer merges cleanly with main (git merge-tree).

AI-assisted: run and analyzed with Claude Code; outputs copied from the local run.

@MarlomMedeiros

Copy link
Copy Markdown
Contributor Author

Confirmed, and thanks for the detailed report. The diagnosis was right: for dpps_op(arg3=0xf4) the product vector contains multiple NaN producers (0x7fc12345, 0x7fc00123 quieted sNaN, and 0xffc00000 from -inf * 0), and the Intel Core i7-6820HQ reduction picks a different legal NaN than the reference model's fixed "first NaN operand wins" order. Native and lifted agreed because the lifted code emits _mm_dp_ps; only the independent reference was over-strict.

Fix (pushed in e2526e1, rebased onto edc2e54): tests/cpu/sse34_fp.py now computes an allowable-result set for dpps_op/dppd_op. Disabled product lanes are masked to +0.0, then every operand ordering of each commutative multiply and of each pairwise add in the architectural (t0+t1)+(t2+t3) / t0+t1 trees is enumerated. The check requires: native output identical to lifted bit-for-bit, disabled destination lanes exactly +0.0, all enabled lanes holding one identical scalar, and that scalar inside the allowed set. For the reported inputs the set is {0x7fc00123, 0x7fc12345, 0xffc00000, 0xffc54321}, covering both the Skylake observation and the previous AMD result while still rejecting foreign payloads, unquieted sNaNs, disabled-lane leakage, and wrong reductions.

ctest --preset dev --no-tests=error -R '^cpu_aot_sse34_fp$' -V passes here: 950 baseline 128-bit SSE3/SSE4.1 FP cases across 33 exports matched native DLL and reference; 4 mutated controls mismatched as required (AMD Ryzen 7 5800X3D, native==lifted bit-exact). The rebase also resolved the CMakeLists.txt / docs/experiments.md / tools/ffx_version_query.py conflicts against current main.

AI-assisted: yes. All reported checks ran locally on the hardware listed.

R0Y00
R0Y00 previously requested changes Nov 23, 2026

@R0Y00 R0Y00 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

The improved dot-product reference is useful. The new packed memory path loads into an unaligned host temporary and then runs a register intrinsic, which can hide a guest alignment fault. A source-built native probe on an i9-14900HX (Windows build 26200) gave correct results for aligned DPPS and HADDPS sources, and both reported access violation reading 0xFFFFFFFFFFFFFFFF at a 4-byte misalignment inside readable storage. Please keep the register forms and the established narrow/unaligned moves, and restrict the packed memory forms unless their native alignment behavior is modeled and independently tested. Keep the existing width and NaN cases. This overlaps #375, so I am keeping the two changes in one review queue.

@R0Y00

R0Y00 commented Nov 23, 2026

Copy link
Copy Markdown
Owner

New independent evidence for e2526e1 after resolving the current main conflicts locally. On Windows build 26200.9974 (MSVC/ML64 19.51, dev preset), the native and lifted DPPS outputs agree at mask 0xff, but the test rejects their lane-specific NaNs. For a=4523c17f0000807f2301807f000080ff and b=2143c5ff0000807f0000803f00000000 both outputs were 4523c17f4523c17f0000c0ff2301c07f. The current check requires every enabled result lane to have the same payload. Please allow lane-specific quiet NaNs only for the already identified ambiguous multi-producer case, while continuing to compare native and lifted bits, disabled zero lanes and determinate results exactly.

I also expanded the original alignment probe with aligned and +4 pointers into readable storage. All 16 packed m128 arithmetic, duplication, rounding, dot-product and blend forms accepted the aligned pointer and raised access violation at +4. LDDQU accepted both. This supports keeping LDDQU and the narrow forms separate from the aligned full-width path. The existing changes request still applies.

@R0Y00
R0Y00 dismissed their stale review November 23, 2026 13:29

Resolved on 3007320: generation preserves the aligned packed source fault, and native, translated and reference checks passed in dev and release.

@R0Y00

R0Y00 commented Nov 23, 2026

Copy link
Copy Markdown
Owner

I pushed 3007320 to your branch. The full-width legacy SSE3/4.1 memory path checks the actual 16-byte alignment and reuses the existing Windows alignment-fault path. Four new +4 misalignment cases preserve the same native and translated read fault and the untouched output. LDDQU and narrow sources retain the previous behavior. The dot-product reference now checks each enabled lane against the allowed result set while keeping native/lifted equality and disabled-zero bits exact.

Windows dev and /O2 each passed the SSE3/4.1 fixture (950 comparisons, four mutations and the four new alignment controls) and the existing CMPXCHG fixture (120 comparisons plus its fault controls). The previous alignment review is resolved on this commit. GitHub CI is pending.

@R0Y00
R0Y00 enabled auto-merge November 23, 2026 13:37
@R0Y00
R0Y00 disabled auto-merge November 23, 2026 13:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants