Skip to content

cpu: lower AVX broadcast, permute, shuffle, blend, dot, round, and vzeroall instructions - #390

Open
evilclanker9000 wants to merge 2 commits into
R0Y00:mainfrom
evilclanker9000:ag/cpu-lower-avx-shuffle-blend
Open

evilclanker9000 wants to merge 2 commits into
R0Y00:mainfrom
evilclanker9000:ag/cpu-lower-avx-shuffle-blend

Conversation

@evilclanker9000

Copy link
Copy Markdown
Contributor

Change

  • Lower 26 VEX 128-bit and 256-bit AVX broadcast, lane extract/insert, permute, shuffle, unpack, blend, dot-product, round, and vzeroall instructions in tools/ffx_version_query.py:
    • Broadcasts and 128-bit lane ops: vbroadcastss, vbroadcastsd, vbroadcastf128, vextractf128, vinsertf128, vperm2f128
    • Permutes, shuffles, unpacks, and duplicates: vpermilps, vpermilpd, vshufps, vshufpd, vunpcklps, vunpckhps, vunpcklpd, vunpckhpd, vmovddup, vmovsldup, vmovshdup
    • Blends, dot products, rounding, and state clear: vblendps, vblendpd, vblendvps, vblendvpd, vdpps, vdppd, vroundps, vroundpd, vzeroall
  • Restrict lowering to EncodingKind.VEX (rejecting EVEX and MXCSR rounding mode imm8 & 4 != 0) and zero upper YMM bytes for 128-bit XMM destinations.
  • Probe CPUID leaf 1 ECX bits 27 (OSXSAVE) and 28 (AVX) plus XCR0 XMM/YMM state before invoking the native fixture (exiting 77 with SKIP_RETURN_CODE 77 on hosts without AVX).
  • Add source-built MASM differential fixture tests/cpu/avx_shuffle_blend.{asm,def,py}, CMake target cpu_avx_shuffle_blend_lifted, CTest cpu_aot_avx_shuffle_blend, coverage mapping in tools/form_coverage.py, and documentation in docs/experiments.md.

Verification

  • Confirmed pre-fix failure on cmake --build --preset dev --target cpu_avx_shuffle_blend_lifted:
    ValueError: unsupported query operation at 0x1000: vbroadcastss ymm0,dword ptr [rcx]
  • Built and tested on Windows 11 x64 (VS 2022 Build Tools):
    • cmake --build --preset dev --target cpu_avx_shuffle_blend_lifted succeeded with 0 warnings
    • ctest --preset dev --no-tests=error -R "^(cpu_aot_avx_shuffle_blend|cpu_aot_bmi1)$" -V: 2/2 passed (26 AVX broadcast/permute/shuffle/blend/dot/round/zeroall cases matched native and translated DLLs + MXCSR-mode vroundps rejection control)
    • .\out\pe-env\Scripts\python.exe -B tests/form_coverage_check.py: form coverage checks passed

Evidence and follow-up

  • Covers 26 AVX-grouped mnemonics in docs/coverage/x64_zen2.tsv using source-built fixtures, gated by a runtime CPUID/XCR0 check.

@evilclanker9000
evilclanker9000 marked this pull request as ready for review November 22, 2026 00:10
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-22T00:20:13.905996Z a0881a1 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a0881a1512

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

stmt = (f"{{ auto lhs = {src1}; {s2_init} for (size_t l=0; l<{nbytes}; l+=16) {{ "
f"{ftype} a[{lanes}], b[{lanes}], s = {ftype}(0); "
f"memcpy(a, lhs.data()+l, 16); memcpy(b, rhs.data()+l, 16); "
f"for (size_t k=0; k<{lanes}; ++k) {{ if (({imm}u >> (4u + k)) & 1u) s += a[k] * b[k]; }} "

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve the pairwise reduction order of VDPPS

For cancellation-sensitive inputs, this sequential accumulation produces different results from the instruction. With the fixture's existing 0xd5 immediate, source lanes [1e20f, 0, -1e20f, 3] dotted with [1, 1, 1, 1] produce 0 natively but 3 in the emitted code, reproduced with a source-built AVX probe under default MXCSR. VDPPS reduces the masked products as (p0 + p1) + (p2 + p3), with intermediate rounding, rather than accumulating left to right. Emit that reduction tree and add a cancellation-sensitive differential case.

Useful? React with 👍 / 👎.

nbytes = 32 if i.op0_register in wide else 16
zero_hi = f" memset({dst}.data()+16, 0, 16);" if nbytes == 16 else ""
s_init = (f"std::array<uint8_t,32> rhs = {wide_any[i.op1_register]};" if i.op1_kind == K.REGISTER
else f"std::array<uint8_t,32> rhs{{}}; memcpy(rhs.data(), reinterpret_cast<const void*>({address()}), {nbytes});")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Limit XMM VMOVDDUP memory loads to eight bytes

For vmovddup xmm0, qword ptr [rcx], the architectural memory operand is only eight bytes, but nbytes is 16 because it describes the destination width. This initializer consequently reads eight bytes beyond the operand. When the source occupies the last eight readable bytes before a protected page, the native instruction succeeds while the emitted code faults in an unoptimized build; this was reproduced with a guarded-page probe. Derive the read size from the memory operand or special-case the 128-bit VMOVDDUP form instead of using the output width.

Useful? React with 👍 / 👎.

vzeroall_op PROC FRAME
.endprolog
vmovups ymm0, ymmword ptr [rcx]
vzeroall

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve Win64 nonvolatile XMM registers around VZEROALL

On the x64 Windows target, vzeroall clears the low 128 bits of XMM6–XMM15, which are callee-saved, but this export returns without restoring them. The native fixture is called directly through ctypes in check(), so any live caller state in those registers is lost even when the output comparison passes. A source-built register probe confirms that an XMM6 sentinel returns as zero. Save and restore the nonvolatile XMM halves, or invoke this fixture through an ABI-preserving native wrapper.

AGENTS.md reference: AGENTS.md:L37-L38

Useful? React with 👍 / 👎.

memcpy(reinterpret_cast<void*>(address), &v, sizeof v);
}
template<class T> T round_x86(T x, unsigned mode) {
if (!std::isfinite(x) || x == T(0)) return x;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Quiet signaling NaNs in the packed-rounding helper

When a source lane contains a signaling NaN and invalid exceptions are masked, VROUNDPS/VROUNDPD return a quiet NaN rather than preserving the signaling representation. This early return leaves the input bits unchanged: with the fixture's vroundps immediate 8, a source-built probe produced native bits 0x7fc00001 versus lowered bits 0x7f800001 for input 0x7f800001. Handle signaling NaNs explicitly so the helper quiets them while preserving the appropriate payload.

Useful? React with 👍 / 👎.

@legacy-code-archaeologist

Copy link
Copy Markdown
Contributor

Verified afc5773 on an Intel Core i7-6820HQ (Skylake), Windows 11 26100.9168, MSVC/ML64 19.52.37029, dev preset. ctest --preset dev --no-tests=error -R "^cpu_" -j 4 passes 81 of 81 on this branch's base.

Two notes:

  • tests/cpu/avx_shuffle_blend.py exercises vdpps_op/vdppd_op only with finite inputs; there are no NaN or infinity lanes. In cpu: lower baseline 128-bit SSE3 and SSE4.1 floating-point instructions #602 a DPPS case with several NaN products failed on this Intel host because the reference model encodes one vendor's NaN choice, and the translation runs the host's own dot-product instruction. The same case does not arise here yet, but a multi-NaN dot product would need the same vendor-tolerant check.
  • The branch no longer merges cleanly with main at ff7f68a (git merge-tree).

AI-assisted (Claude Code).

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.

3 participants