Skip to content

[Codegen] Cap hoisted statically-bound allocations at the stack budget. - #24532

Closed
bjacob wants to merge 3 commits into
mainfrom
users/bjacob/cpu-hoist-alloc-cap
Closed

bjacob wants to merge 3 commits into
mainfrom
users/bjacob/cpu-hoist-alloc-cap

Conversation

@bjacob

@bjacob bjacob commented May 25, 2026

Copy link
Copy Markdown
Collaborator

hoistOneStaticallyBoundAllocation pads a dynamic allocation to its computed upper bound. An unconstrained dynamic dimension carries only the index type maximum as its bound, so padding yields a bogus multi- petabyte allocation (#24483). Decline to hoist past the target's max_stack_allocation_size; declining to hoist is always valid -- it simply leaves the IR's existing dynamic allocation in place.

Extent of the #24483 fix: this is partial. It implements the first of the three fixes proposed in that issue -- the hoist itself no longer turns an index-umax-derived bound into a bogus static alloca (which fed poison strides into codegen and silently dropped data). It does not on its own make the originally-affected pipeline -- dynamic-shape inner_tiled data-tiling -- compile: as #24483 notes, capping leaves a dynamic CPU stack allocation, which is itself rejected downstream. What this commit guarantees is only that the failure mode is no longer a silent miscompile. The dynamic-shape inner_tiled pipeline is fully unblocked by a later commit in this series ("Fold reshape-containing encoding relayouts to map_store"), which removes the whole-tensor pack so the unbounded allocation never arises. #24483 is therefore not fully resolved by this commit alone.

Progress towards #24515.

bjacob and others added 3 commits May 22, 2026 12:10
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]>
…nrolling.

`VectorTransferLoweringPass` runs the MLIR transfer-lowering patterns
with `maxTransferRank=1` plus full-unroll, fully unrolling any rank-N>1
transfer to one rank-1 transfer per outer index. For a packed tile
whose trailing dim is a tiny contiguous chunk that turns a single wide
load into many narrow ones plus a shuffle chain to rebuild the wide
register. Concretely, a bf16xbf16->f32 inner_tiled matmul (N=16,
K_inner=2) loads each `<16x2xbf16>` RHS K-step as 16 separate
`<2xbf16>` loads + a `vpermt2d`/`vpermt2q` chain -- ~3 cycles of extra
work per K-step on top of the 29 dpbf16ps.

Apply `populateFlattenVectorTransferPatterns` *before* rank reduction,
gated on the target's natural word size (the pointer size, via
`DataLayout`): flatten only when the trailing dim is *sub-word*.
Sub-word loads in bulk are pathological; word-and-up trailing dims
(`<2xf32>` ... `<16xf32>`) are already good standalone loads, and
flattening *them* fuses register-sized rows into an oversized 1-D
transfer + a `vector.shape_cast` re-split, regressing whole-model
.vmfb size. (Not `native_vector_size`: that is the *widest* useful
vector, not the smallest non-pathological load.)

Measured: bf16 4096x4096 inner_tiled matmul on Zen 4, 80.8 -> 67.1 ms
per fragment; combined with the m_bcst-fold broadcast routing in a
sibling commit, the full matmul reaches ukernel parity (~50 ms). The
`sdxl/clip_compstat_cpu` size guard is unchanged at 583k bytes / 2130
dispatches (golden 650k / 2130).

Test fallout: `transpose_mask` in vector_lowering now writes a constant
`vector<4x2xi1>` mask as a single flat `vector<8xi1>` store; updated
the CHECK lines.

Progress towards #24515.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
Signed-off-by: Benoit Jacob <[email protected]>
`hoistOneStaticallyBoundAllocation` pads a dynamic allocation to its
computed upper bound. An unconstrained dynamic dimension carries only the
`index` type maximum as its bound, so padding yields a bogus multi-
petabyte allocation (#24483). Decline to hoist past the
target's `max_stack_allocation_size`; declining to hoist is always valid
-- it simply leaves the IR's existing dynamic allocation in place.

Extent of the #24483 fix: this is partial. It implements the first of the
three fixes proposed in that issue -- the hoist itself no longer turns an
`index`-umax-derived bound into a bogus static `alloca` (which fed poison
strides into codegen and silently dropped data). It does not on its own
make the originally-affected pipeline -- dynamic-shape `inner_tiled`
data-tiling -- compile: as #24483 notes, capping leaves a dynamic CPU
stack allocation, which is itself rejected downstream. What this commit
guarantees is only that the failure mode is no longer a *silent
miscompile*. The dynamic-shape `inner_tiled` pipeline is fully unblocked
by a later commit in this series ("Fold reshape-containing encoding
relayouts to map_store"), which removes the whole-tensor `pack` so the
unbounded allocation never arises. #24483 is therefore not fully resolved
by this commit alone.

Progress towards #24515.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
Signed-off-by: Benoit Jacob <[email protected]>
@bjacob
bjacob marked this pull request as ready for review June 1, 2026 15:27
Base automatically changed from users/bjacob/cpu-flatten-transfer-before-rank-reduce to main June 2, 2026 16:08
@bjacob

bjacob commented Jun 3, 2026

Copy link
Copy Markdown
Collaborator Author

Made unnecessary by #24535.

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.

1 participant