Skip to content

[Codegen][CPU] Route inner_tiled broadcast into m_bcst-foldable slot. - #24516

Merged
bjacob merged 1 commit into
mainfrom
users/bjacob/cpu-mma-broadcast-mbcst-fold
Jun 2, 2026
Merged

bjacob merged 1 commit into
mainfrom
users/bjacob/cpu-mma-broadcast-mbcst-fold

Conversation

@bjacob

@bjacob bjacob commented May 20, 2026

Copy link
Copy Markdown
Collaborator

The CPU inner_tiled lowering replicates whichever of LHS/RHS has fewer lanes up to the other's lane count before calling the LLVM intrinsic. The previous lowering emitted this as vector.broadcast to a (replicate, K) 2-D shape followed by vector.shape_cast to flat, with a comment claiming the x86 backend's instruction selector would recover the {1toN} broadcast-from-memory form on its own.

Empirically that did not work for bf16 matmul codegen on Zen 4: every vdpbf16ps instruction was preceded by a separate vbroadcastss, doubling the per-row uop count of the hot inner loop. Two structural reasons:

  1. The IR shape mattered. LLVM's x86 ISel m_bcst patterns key on the canonical _mm512_set1_ps-style splat: a scalar fed into insertelement <N x T> poison, T, 0 followed by shufflevector <N x T>, poison, <N x i32> zeroinitializer, with T a float. Our vector.broadcast to a (replicate, K) 2-D shape + vector.shape_cast lowered to a different shufflevector pattern (or a direct <K x elem> -> <N*K x elem> interleaved shuffle) that did not pattern-match.

  2. The intrinsic operand position mattered. The ISA-level m_bcst EVEX operand is on the third source of dpbf16ps/vpdpwssd/ pmaddwd, and on the b operand (second multiplicand) of FMA's a*b+c. We passed the broadcasted operand into the LHS slot, putting it where ISel cannot fold a memory broadcast.

Rewrite the replication to bitcast the source to a 1-lane vector of width K * elem_bits (with a float lane type when that width is 32 or 64 bits, matching the _mm512_set1_ps shape), extract the scalar, vector.broadcast it to replicate lanes, then bitcast back. Track whether the broadcast landed on lhs and, for the symmetric LLVM intrinsics, route the broadcasted operand into the m_bcst-foldable slot. For K=1 the bitcast pair is a no-op LLVM elides. vpdpbusd is asymmetric (UI8 must stay in the second slot); its existing sign-aware routing happens to put the broadcast in the m_bcst slot precisely in the two orientations where the ISA allows the fold, so no change needed there.

Measured on a 4096×4096 dynamic-shape bf16×bf16 -> f32 matmul on Zen 4 (avx512_bf16, no AMX), with --iree-opt-data-tiling --iree-llvmcpu-enable-inner-tiled:

  • All 29 vdpbf16ps in the inner loop now use the {1to16} memory-broadcast form (vs 0 before); all 29 separate vbroadcastss are gone.
  • End-to-end matmul: 80.8 ms -> 62.7 ms (1.29x faster, 16.0 it/s -> from 12.4 it/s), closing ~60% of the gap to the precompiled mmt4d ukernel (50.5 ms).

Progress towards #24515.

@bjacob
bjacob force-pushed the users/bjacob/cpu-mma-broadcast-mbcst-fold branch from 7d900f4 to 10f904e Compare May 21, 2026 18:44
The CPU `inner_tiled` lowering replicates whichever of LHS/RHS has fewer
lanes up to the other's lane count before calling the LLVM intrinsic.
This reworks how that replication is emitted, for both performance and
correctness.

Previously the lowering emitted `vector.broadcast` to a `(replicate, K)`
2-D shape + `vector.shape_cast`, passed the broadcasted operand into the
LHS intrinsic slot, and -- for the `*_CASTF32` (f16->f32) and `*_CASTI16`
(i8->i16) variants -- widened the narrow operand to the intrinsic
element type *after* the broadcast.

Two problems:

1. The broadcast IR shape did not pattern-match LLVM's x86 `m_bcst`
   fold, so every `vdpbf16ps` was preceded by a separate `vbroadcastss`.
   ISel's `m_bcst` patterns key on the canonical `_mm512_set1_ps`-style
   splat (`insertelement ... 0` + `shufflevector ... zeroinitializer`),
   and the ISA-level broadcast EVEX operand is on the *third* source of
   dpbf16ps/vpdpwssd/pmaddwd.

2. Widening after the broadcast was both wasteful (the extend ran on the
   full replicated vector, once per unrolled M row) and, combined with
   the packed-lane splat idiom below, *miscompiled*: broadcast-then-widen
   forms `arith.extsi(vector.bitcast <N x i16> -> <2N x i8>)`, whose
   lowering drops the sign extension -- the i8 operand lanes come back
   zero-extended (a `vpand`/`vpsrlvw` mask-and-shift instead of
   `vpmovsxbw`). Negative i8 inputs then produce wrong matmul results.

Rework:
- Widen `*_CAST*` operands to the intrinsic element type *before*
  broadcasting, so the broadcast unit is already the intrinsic element
  type and no narrowing `vector.bitcast` feeds an `arith.extsi`.
- Emit the broadcast as the splat-of-a-single-packed-lane idiom: bitcast
  the source to a 1-lane vector of width `K * elem_bits`, extract the
  scalar, `vector.broadcast` to `replicate` lanes, bitcast back. This is
  the IR shape x86 ISel folds into the `m32bcst`/`m64bcst` EVEX
  broadcast operand.
- Route the broadcasted operand into the m_bcst-foldable slot for the
  symmetric intrinsics (third source of dpbf16ps/vpdpwssd/pmaddwd, `b`
  of FMA's `a*b+c`). vpdpbusd is asymmetric; its existing sign-aware
  routing already lands the broadcast in the foldable slot.

Measured on Zen 4 (4096x4096, dynamic shapes), inner_tiled:
  - bf16xbf16->f32: 80.8 ms -> 62.7 ms; all 29 `vdpbf16ps` now use the
    `{1to16}` memory-broadcast form (vs 0 before).
  - f16xf16->f32: 138 ms -> 126 ms (now faster than the mmt4d ukernel).
  - i8/i16/f32: essentially unchanged.

Progress towards #24515.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
Signed-off-by: Benoit Jacob <[email protected]>
@bjacob
bjacob force-pushed the users/bjacob/cpu-mma-broadcast-mbcst-fold branch from 10f904e to 3936fb5 Compare May 22, 2026 16:13
@bjacob
bjacob marked this pull request as ready for review May 25, 2026 15:12
@bjacob
bjacob requested a review from hanhanW as a code owner May 25, 2026 15:12
@bjacob
bjacob requested a review from kuhar May 25, 2026 15:12
@egebeysel
egebeysel self-requested a review May 28, 2026 12:17
@egebeysel
egebeysel removed their request for review June 2, 2026 06:51
@bjacob
bjacob merged commit cc50107 into main Jun 2, 2026
65 of 68 checks passed
@bjacob
bjacob deleted the users/bjacob/cpu-mma-broadcast-mbcst-fold branch June 2, 2026 16:08
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.

2 participants