Skip to content

[Codegen][CPU] Fix DataTiledMMAAttr::getUndistributedTileTypes to honor swizzle permutation. - #24437

Merged
bjacob merged 1 commit into
mainfrom
users/bjacob/cpu-mma-align-permutation-with-gpu
May 12, 2026
Merged

bjacob merged 1 commit into
mainfrom
users/bjacob/cpu-mma-align-permutation-with-gpu

Conversation

@bjacob

@bjacob bjacob commented May 11, 2026 •

Copy link
Copy Markdown
Collaborator

The swizzle's permutation is ground truth for memory layout — for pre-transposed intrinsics with both intrinsics_m > 1 and intrinsics_n > 1 on the ACC, the Codegen::expand insertion order produces a non-identity permutation that interleaves M and N dims, and that interleaved order is what the encoding materialization actually lays out in memory.

DataTiledMMAAttr::getUndistributedTileTypes was silently ignoring that permutation: it returned (prod(expandShape[0]), prod(expandShape[1])) — operand-natural row-major — which contradicted the swizzle's stated memory order. The mismatch was dormant when the permutation happened to be identity (all pre-pretransposed intrinsics) but produces wrong tile types for the M↔N-swapped enum values that the next branch in this stack introduces, and is the root cause of the RDNA4 regression that took down #24404.

Fix: lift the per-dim walk into a new shared utility Codegen::getTileVectorType(swizzle, elemType[, keepSize]) in IREECodegenTypes. The shape is sliceSwizzledShape(swizzle, keepSize) — every dim of expandShape in swizzle.permutation() order, with optional per-dim masking to size 1. A dim with a non-One symbolic multiplier maps to a scalable axis. CPU's DataTiledMMAAttr::getUndistributedTileTypes and GPU's DataTiledMMAInterfaceAttr::{getUndistributedTileTypes,getDistributedTileTypes} now all go through it (the GPU distributed variant passes a d.kind() != CrossThread mask to fold thread-distribution factors to size-1 axes).

For trivial CPU cases (every expandShape group is a single dim — the only configuration fixupSwizzle leaves after the base intrinsic build, where intrinsics_{m,n,k} haven't yet been expanded in) the result is the same rank-2 (outer, inner) shape CPU historically returned. For non-identity permutations introduced by combined intrinsics_m and intrinsics_n unrolling on ACC, the rank grows because the permutation interleaves M and N dims and there's no rank-2 collapse that faithfully represents the memory layout — and that's the whole point: the tile type now matches what's actually in memory.

getTileMxNxK in CPUEncodingExternalModels.cpp previously read M, N, K positionally from the tile vector shape, which is wrong once the permutation is honored (M can be spread across multiple dims and interleaved with N). It now goes through the LHS/RHS TileSwizzles directly: M is the product of LHS expandShape[0], K is the product of LHS expandShape[1], N is the product of RHS expandShape[0] — the per-operand [M, K] / [N, K] group partition is what getIntrinsicSwizzle establishes by construction, regardless of how the permutation later orders dims in memory.

Test fallout in this branch is limited to invalid.mlir (one expected error string updated to reflect the rank increase from honoring the permutation on a 16×1 LHS); other CPU lit tests on origin/main use trivial unrollings that keep the rank-2 shape.

Progress towards #24323.

@bjacob
bjacob force-pushed the users/bjacob/cpu-mma-align-permutation-with-gpu branch from e8d7312 to ff1f83a Compare May 11, 2026 19:29
bjacob added a commit that referenced this pull request May 11, 2026
…sed enums.

This is take two of #24404 which was reverted due to a RDNA4 regression
(#24427); see commit message below for what changed in the reland.

Each non-square hardware MMA now appears as two `MMAIntrinsic` enum values
— one per orientation — named after their actual MxNxK shape (e.g.
`MMA_X86_AVX512_1x16x1_F32_F32` and its M↔N-swapped sibling
`MMA_X86_AVX512_16x1x1_F32_F32`). The swapped sibling of `X` is `X | 1`
(bit 0 of D in the enum's encoding), so consecutive enum values form
natural/swapped pairs. The `transposed_intrinsic` boolean on
`DataTiledMMAAttr` is removed; both orientations show up as distinct
candidates in `getMmaIntrinsicsForTargetConfig` and the existing
`chooseIntrinsic` scoring picks between them.

A new `assert(isMmaIntrinsicArrayValid(...))` at the bottom of
`getMmaIntrinsicsForTargetConfig` enforces the convention: strictly
increasing, every swapped entry preceded by its natural sibling, and
square/type-symmetric singletons allowed (covers the generic-scalar
1×1×1 family).

Motivation: the boolean was originally there to avoid 2× enum growth,
but it leaked orientation-handling into every layer that touched MMA
layouts — swizzles, ACC encoding-info, the cross-intrinsic ACC index,
broadcast direction. It was also poised to get worse: asymmetric LLVM
intrinsics (vpdpbusd) and non-x86 targets (no broadcast) would each
need bespoke handling under the boolean. With separate enum values,
each orientation lowers independently.

Swapped enums introduce intrinsics where the ACC swizzle's
`Codegen::expand` insertion order leaves a non-identity permutation
(intrinsics_n then intrinsics_m, interleaving N and M dims in memory).
That permutation is what's actually stored, but the previous
`DataTiledMMAAttr::getUndistributedTileTypes` silently ignored it and
returned the operand-natural `(prod(group[0]), prod(group[1]))` shape
— wrong once the permutation is non-trivial, latent until now. #24437
fixes that at the source: tile types now reflect the swizzle's actual
memory order. With that in place, the `reshapeToSwizzleDistributed`
bridge in `MMAUtils.cpp` collapses to a plain `vector.shape_cast` —
same as it was before non-identity permutations existed. New CHECKs in
`lower_inner_tiled.mlir`'s `lower_avx512_16x1x1_f32` pin the
shape_cast pair on the first multi-non-unit-dim swapped-intrinsic
test case in the file.

RDNA4 regression delta from #24404: the reverted PR took a different
path — it left `getUndistributedTileTypes` returning the wrong shape and
introduced an explicit `vector.transpose` in `reshapeToSwizzleDistributed`
to compensate. That transpose was correct for CPU but on GPU stacked on
top of GPU's already-permuted distributed shape, scrambling per-lane
data on RDNA4 (the only target whose swizzle has a non-trivial permutation
today). Fixing the bogus shape at the `getUndistributedTileTypes`
source in #24437 removes the transpose entirely and keeps the shared
lowering body uniform across targets.

Progress towards #24323

Signed-off-by: Benoit Jacob <[email protected]>
…or swizzle permutation.

The swizzle's `permutation` is ground truth for CPU memory layout — for
pre-transposed intrinsics with both `intrinsics_m > 1` and
`intrinsics_n > 1` on the ACC, the `Codegen::expand` insertion order
produces a non-identity permutation that interleaves M and N dims, and
that interleaved order is what the encoding materialization actually
lays out in memory.

`DataTiledMMAAttr::getUndistributedTileTypes` was silently ignoring
that permutation: it returned `(prod(expandShape[0]), prod(expandShape[1]))`
— operand-natural row-major — which contradicted the swizzle's stated
memory order. The mismatch was dormant when the permutation happened
to be identity (all pre-pretransposed intrinsics) but produces wrong
tile types for the M↔N-swapped enum values that the next branch in
this stack introduces, and is the root cause of the RDNA4 regression
that took down #24404.

Fix: lift the per-dim walk into a new shared utility
`Codegen::getTileVectorType(swizzle, elemType[, keepSize])` in
`IREECodegenTypes`. The shape is `sliceSwizzledShape(swizzle, keepSize)`
— every dim of `expandShape` in `swizzle.permutation()` order, with
optional per-dim masking to size 1. A dim with a non-`One` symbolic
multiplier maps to a scalable axis. CPU's
`DataTiledMMAAttr::getUndistributedTileTypes` and GPU's
`DataTiledMMAInterfaceAttr::{getUndistributedTileTypes,getDistributedTileTypes}`
now all go through it (the GPU distributed variant passes a
`d.kind() != CrossThread` mask to fold thread-distribution factors to
size-1 axes).

For trivial CPU cases (every `expandShape` group is a single dim —
the only configuration `fixupSwizzle` leaves after the base intrinsic
build, where `intrinsics_{m,n,k}` haven't yet been expanded in) the
result is the same rank-2 `(outer, inner)` shape CPU historically
returned. For non-identity permutations introduced by combined
`intrinsics_m` and `intrinsics_n` unrolling on ACC, the rank grows
because the permutation interleaves M and N dims and there's no rank-2
collapse that faithfully represents the memory layout — and that's the
whole point: the tile type now matches what's actually in memory.

`getTileMxNxK` in `CPUEncodingExternalModels.cpp` previously read M, N,
K positionally from the tile vector shape, which is wrong once the
permutation is honored (M can be spread across multiple dims and
interleaved with N). It now goes through the LHS/RHS `TileSwizzle`s
directly: M is the product of LHS `expandShape[0]`, K is the product
of LHS `expandShape[1]`, N is the product of RHS `expandShape[0]` —
the per-operand `[M, K]` / `[N, K]` group partition is what
`getIntrinsicSwizzle` establishes by construction, regardless of how
the permutation later orders dims in memory.

Test fallout in this branch is limited to `invalid.mlir` (one expected
error string updated to reflect the rank increase from honoring the
permutation on a 16×1 LHS); other CPU lit tests on origin/main use
trivial unrollings that keep the rank-2 shape.

Signed-off-by: Benoit Jacob <[email protected]>
@bjacob
bjacob force-pushed the users/bjacob/cpu-mma-align-permutation-with-gpu branch from ff1f83a to b52079d Compare May 12, 2026 14:49
@bjacob
bjacob marked this pull request as ready for review May 12, 2026 15:26
@bjacob bjacob mentioned this pull request May 12, 2026
25 of 28 tasks
@bjacob
bjacob merged commit 462b817 into main May 12, 2026
64 of 65 checks passed
@bjacob
bjacob deleted the users/bjacob/cpu-mma-align-permutation-with-gpu branch May 12, 2026 17:29
bjacob added a commit that referenced this pull request May 12, 2026
…sed enums.

This is take two of #24404, which was reverted due to a RDNA4
regression (#24427); see the regression delta at the end.

Each non-square hardware MMA now appears as two `MMAIntrinsic` enum
values — one per orientation — named after their actual MxNxK shape
(e.g. `MMA_X86_AVX512_1x16x1_F32_F32` and its M↔N-swapped sibling
`MMA_X86_AVX512_16x1x1_F32_F32`). The swapped sibling of `X` is
`X | 1` (bit 0 of D in the enum's encoding), so consecutive enum
values form natural/swapped pairs. The `transposed_intrinsic`
boolean on `DataTiledMMAAttr` is removed; both orientations show up
as distinct candidates in `getMmaIntrinsicsForTargetConfig` and the
existing `chooseIntrinsic` scoring picks between them.

A new `assert(isMmaIntrinsicArrayValid(...))` at the bottom of
`getMmaIntrinsicsForTargetConfig` enforces the convention: strictly
increasing, every swapped entry preceded by its natural sibling, and
square/type-symmetric singletons allowed (covers the generic-scalar
1×1×1 family).

Motivation: the boolean was originally there to avoid 2× enum growth,
but it leaked orientation-handling into every layer that touched MMA
layouts — swizzles, ACC encoding-info, the cross-intrinsic ACC index,
broadcast direction. It was also poised to get worse: asymmetric LLVM
intrinsics (vpdpbusd) and non-x86 targets (no broadcast) would each
need bespoke handling under the boolean. With separate enum values,
each orientation lowers independently.

Swapped enums are the first intrinsics to exercise a non-identity ACC
swizzle permutation: `Codegen::expand` on intrinsics_n then
intrinsics_m interleaves N and M dims in memory. Two prerequisite
commits make that permutation observable consistently across the
type and encoder sides:

  - #24437 makes `DataTiledMMAAttr::getUndistributedTileTypes` honor
    the swizzle's permutation (rank-N tile type that reflects the
    actual memory order).
  - The predecessor commit on this branch attaches the swizzle to the
    encoder's `MaterializeEncodingInfo` (`info.swizzle = ...`) so the
    packed inner tile is the swizzle's permuted-expanded layout — the
    encoder-side counterpart to #24437.

With both in place, the `reshapeToSwizzleDistributed` bridge in
`MMAUtils.cpp` collapses to a plain `vector.shape_cast` — same as it
was before non-identity permutations existed. New CHECKs in
`lower_inner_tiled.mlir`'s `lower_avx512_16x1x1_f32` pin the
shape_cast pair on the first multi-non-unit-dim swapped-intrinsic
test case in the file.

RDNA4 regression delta from #24404: the reverted PR left
`getUndistributedTileTypes` returning the wrong shape and introduced
an explicit `vector.transpose` in `reshapeToSwizzleDistributed` to
compensate. That transpose was correct for CPU but on GPU stacked
on top of GPU's already-permuted distributed shape, scrambling
per-lane data on RDNA4 (the only target whose swizzle had a
non-trivial permutation at the time). Fixing the tile type at the
source removes the transpose and keeps the shared lowering body
uniform across targets.

Progress towards #24323.

Signed-off-by: Benoit Jacob <[email protected]>
bjacob added a commit that referenced this pull request May 13, 2026
Follow-up to #24437. That commit made `getUndistributedTileTypes` honor
the swizzle's permutation, but the encoder side
(`getInnerTiledEncodingInfo`) was still producing a rank-2 row-major
inner tile that ignored the swizzle's `expand_shape` and permutation.
For existing intrinsics the two sides happen to be compatible under
`matchTileTypes`' unit-dim filter, but the encoder's rank-2 shape
disagrees with the rank-N permutation-aware tile type as soon as any
swizzle has a non-identity permutation (e.g. pre-transposed intrinsics,
added in a follow-up).

Set `info.swizzle = IREE::CPU::getSwizzle(mma, operandIdx)` so the pack
pipeline's existing `getSwizzledShape` bridge applies the swizzle's
`expand_shape` + `transpose` to the packed inner tile, matching the tile
type `getUndistributedTileTypes` returns. Mirrors GPU's
`getEncodingInfoImpl`. Drop the stale "Only used on GPU" comment on the
`swizzle` field.

Test fallout: `materialize_encoding_x86_64.mlir`'s four
`_inner_tiled_avx512` CHECK groups now expect the
`tensor.expand_shape` after each pack and the
`tensor.collapse_shape` before the unpack, with the rank-N inner tile
that the swizzle materializes.

Progress towards #24323.

Signed-off-by: Benoit Jacob <[email protected]>
bjacob added a commit that referenced this pull request May 13, 2026
…sed enums.

This is take two of #24404, which was reverted due to a RDNA4
regression (#24427); see the regression delta at the end.

Each non-square hardware MMA now appears as two `MMAIntrinsic` enum
values — one per orientation — named after their actual MxNxK shape
(e.g. `MMA_X86_AVX512_1x16x1_F32_F32` and its M↔N-swapped sibling
`MMA_X86_AVX512_16x1x1_F32_F32`). The swapped sibling of `X` is
`X | 1` (bit 0 of D in the enum's encoding), so consecutive enum
values form natural/swapped pairs. The `transposed_intrinsic`
boolean on `DataTiledMMAAttr` is removed; both orientations show up
as distinct candidates in `getMmaIntrinsicsForTargetConfig` and the
existing `chooseIntrinsic` scoring picks between them.

A new `assert(isMmaIntrinsicArrayValid(...))` at the bottom of
`getMmaIntrinsicsForTargetConfig` enforces the convention: strictly
increasing, every swapped entry preceded by its natural sibling, and
square/type-symmetric singletons allowed (covers the generic-scalar
1×1×1 family).

Motivation: the boolean was originally there to avoid 2× enum growth,
but it leaked orientation-handling into every layer that touched MMA
layouts — swizzles, ACC encoding-info, the cross-intrinsic ACC index,
broadcast direction. It was also poised to get worse: asymmetric LLVM
intrinsics (vpdpbusd) and non-x86 targets (no broadcast) would each
need bespoke handling under the boolean. With separate enum values,
each orientation lowers independently.

Swapped enums are the first intrinsics to exercise a non-identity ACC
swizzle permutation: `Codegen::expand` on intrinsics_n then
intrinsics_m interleaves N and M dims in memory. Two prerequisite
commits make that permutation observable consistently across the
type and encoder sides:

  - #24437 makes `DataTiledMMAAttr::getUndistributedTileTypes` honor
    the swizzle's permutation (rank-N tile type that reflects the
    actual memory order).
  - The predecessor commit on this branch attaches the swizzle to the
    encoder's `MaterializeEncodingInfo` (`info.swizzle = ...`) so the
    packed inner tile is the swizzle's permuted-expanded layout — the
    encoder-side counterpart to #24437.

With both in place, the `reshapeToSwizzleDistributed` bridge in
`MMAUtils.cpp` collapses to a plain `vector.shape_cast` — same as it
was before non-identity permutations existed. New CHECKs in
`lower_inner_tiled.mlir`'s `lower_avx512_16x1x1_f32` pin the
shape_cast pair on the first multi-non-unit-dim swapped-intrinsic
test case in the file.

RDNA4 regression delta from #24404: the reverted PR left
`getUndistributedTileTypes` returning the wrong shape and introduced
an explicit `vector.transpose` in `reshapeToSwizzleDistributed` to
compensate. That transpose was correct for CPU but on GPU stacked
on top of GPU's already-permuted distributed shape, scrambling
per-lane data on RDNA4 (the only target whose swizzle had a
non-trivial permutation at the time). Fixing the tile type at the
source removes the transpose and keeps the shared lowering body
uniform across targets.

Progress towards #24323.

Signed-off-by: Benoit Jacob <[email protected]>
bjacob added a commit that referenced this pull request May 13, 2026
…sed enums. (#24456)

This is take two of #24404, which was reverted due to a RDNA4 regression
(#24427); see the regression delta at the end.

Each non-square hardware MMA now appears as two `MMAIntrinsic` enum
values — one per orientation — named after their actual MxNxK shape
(e.g. `MMA_X86_AVX512_1x16x1_F32_F32` and its M↔N-swapped sibling
`MMA_X86_AVX512_16x1x1_F32_F32`). The swapped sibling of `X` is `X | 1`
(bit 0 of D in the enum's encoding), so consecutive enum values form
natural/swapped pairs. The `transposed_intrinsic` boolean on
`DataTiledMMAAttr` is removed; both orientations show up as distinct
candidates in `getMmaIntrinsicsForTargetConfig` and the existing
`chooseIntrinsic` scoring picks between them.

A new `assert(isMmaIntrinsicArrayValid(...))` at the bottom of
`getMmaIntrinsicsForTargetConfig` enforces the convention: strictly
increasing, every swapped entry preceded by its natural sibling, and
square/type-symmetric singletons allowed (covers the generic-scalar
1×1×1 family).

Motivation: the boolean was originally there to avoid 2× enum growth,
but it leaked orientation-handling into every layer that touched MMA
layouts — swizzles, ACC encoding-info, the cross-intrinsic ACC index,
broadcast direction. It was also poised to get worse: asymmetric LLVM
intrinsics (vpdpbusd) and non-x86 targets (no broadcast) would each need
bespoke handling under the boolean. With separate enum values, each
orientation lowers independently.

Swapped enums are the first intrinsics to exercise a non-identity ACC
swizzle permutation: `Codegen::expand` on intrinsics_n then intrinsics_m
interleaves N and M dims in memory. Two prerequisite commits make that
permutation observable consistently across the type and encoder sides:

- #24437 makes `DataTiledMMAAttr::getUndistributedTileTypes` honor the
swizzle's permutation (rank-N tile type that reflects the actual memory
order).
- The predecessor commit on this branch attaches the swizzle to the
encoder's `MaterializeEncodingInfo` (`info.swizzle = ...`) so the packed
inner tile is the swizzle's permuted-expanded layout — the encoder-side
counterpart to #24437.

With both in place, the `reshapeToSwizzleDistributed` bridge in
`MMAUtils.cpp` collapses to a plain `vector.shape_cast` — same as it was
before non-identity permutations existed. New CHECKs in
`lower_inner_tiled.mlir`'s `lower_avx512_16x1x1_f32` pin the shape_cast
pair on the first multi-non-unit-dim swapped-intrinsic test case in the
file.

RDNA4 regression delta from #24404: the reverted PR left
`getUndistributedTileTypes` returning the wrong shape and introduced an
explicit `vector.transpose` in `reshapeToSwizzleDistributed` to
compensate. That transpose was correct for CPU but on GPU stacked on top
of GPU's already-permuted distributed shape, scrambling per-lane data on
RDNA4 (the only target whose swizzle had a non-trivial permutation at
the time). Fixing the tile type at the source removes the transpose and
keeps the shared lowering body uniform across targets.

Progress towards #24323.

Signed-off-by: Benoit Jacob <[email protected]>
bjacob added a commit that referenced this pull request May 14, 2026
The comment claimed CPU's swizzle permutation is always identity, but
that hasn't been true since #24456 introduced swapped MMA intrinsics:
when both `intrinsics_n > 1` and `intrinsics_m > 1`, `Codegen::expand`
on the ACC operand sets a non-identity permutation. The bridge still
collapses to a plain `shape_cast` not because the permutation is
identity, but because `getUndistributedTileTypes` (CPU, since #24437)
and `getDistributedTileTypes` (GPU) both produce shapes that already
reflect the swizzle's permutation — the reshape only adjusts rank.

Signed-off-by: Benoit Jacob <[email protected]>
bjacob added a commit that referenced this pull request May 14, 2026
The comment claimed CPU's swizzle permutation is always identity, but
that hasn't been true since #24456 introduced swapped MMA intrinsics:
when both `intrinsics_n > 1` and `intrinsics_m > 1`, `Codegen::expand`
on the ACC operand sets a non-identity permutation. The bridge still
collapses to a plain `shape_cast` not because the permutation is
identity, but because `getUndistributedTileTypes` (CPU, since #24437)
and `getDistributedTileTypes` (GPU) both produce shapes that already
reflect the swizzle's permutation — the reshape only adjusts rank.

Signed-off-by: Benoit Jacob <[email protected]>
bjacob added a commit that referenced this pull request May 15, 2026
The comment claimed CPU's swizzle permutation is always identity, but
that hasn't been true since #24456 introduced swapped MMA intrinsics:
when both `intrinsics_n > 1` and `intrinsics_m > 1`, `Codegen::expand`
on the ACC operand sets a non-identity permutation. The bridge still
collapses to a plain `shape_cast` not because the permutation is
identity, but because `getUndistributedTileTypes` (CPU, since #24437)
and `getDistributedTileTypes` (GPU) both produce shapes that already
reflect the swizzle's permutation — the reshape only adjusts rank.

Signed-off-by: Benoit Jacob <[email protected]>
bjacob added a commit that referenced this pull request May 15, 2026
The comment claimed CPU's swizzle permutation is always identity, but
that hasn't been true since #24456 introduced swapped MMA intrinsics:
when both `intrinsics_n > 1` and `intrinsics_m > 1`, `Codegen::expand`
on the ACC operand sets a non-identity permutation. The bridge still
collapses to a plain `shape_cast` not because the permutation is
identity, but because `getUndistributedTileTypes` (CPU, since #24437)
and `getDistributedTileTypes` (GPU) both produce shapes that already
reflect the swizzle's permutation — the reshape only adjusts rank.

Signed-off-by: Benoit Jacob <[email protected]>
bjacob added a commit that referenced this pull request May 15, 2026
The comment claimed CPU's swizzle permutation is always identity, but
that hasn't been true since #24456 introduced swapped MMA intrinsics:
when both `intrinsics_n > 1` and `intrinsics_m > 1`, `Codegen::expand`
on the ACC operand sets a non-identity permutation. The bridge still
collapses to a plain `shape_cast` not because the permutation is
identity, but because `getUndistributedTileTypes` (CPU, since #24437)
and `getDistributedTileTypes` (GPU) both produce shapes that already
reflect the swizzle's permutation — the reshape only adjusts rank.

Signed-off-by: Benoit Jacob <[email protected]>
bjacob added a commit that referenced this pull request May 15, 2026
The comment claimed CPU's swizzle permutation is always identity, but
that hasn't been true since #24456 introduced swapped MMA intrinsics:
when both `intrinsics_n > 1` and `intrinsics_m > 1`, `Codegen::expand`
on the ACC operand sets a non-identity permutation. The bridge still
collapses to a plain `shape_cast` not because the permutation is
identity, but because `getUndistributedTileTypes` (CPU, since #24437)
and `getDistributedTileTypes` (GPU) both produce shapes that already
reflect the swizzle's permutation — the reshape only adjusts rank.

Signed-off-by: Benoit Jacob <[email protected]>
bjacob added a commit that referenced this pull request May 15, 2026
The comment claimed CPU's swizzle permutation is always identity, but
that hasn't been true since #24456 introduced swapped MMA intrinsics:
when both `intrinsics_n > 1` and `intrinsics_m > 1`, `Codegen::expand`
on the ACC operand sets a non-identity permutation. The bridge still
collapses to a plain `shape_cast` not because the permutation is
identity, but because `getUndistributedTileTypes` (CPU, since #24437)
and `getDistributedTileTypes` (GPU) both produce shapes that already
reflect the swizzle's permutation — the reshape only adjusts rank.

Signed-off-by: Benoit Jacob <[email protected]>
RattataKing pushed a commit to RattataKing/iree that referenced this pull request May 27, 2026
…or swizzle permutation. (iree-org#24437)

The swizzle's `permutation` is ground truth for memory layout — for
pre-transposed intrinsics with both `intrinsics_m > 1` and `intrinsics_n
> 1` on the ACC, the `Codegen::expand` insertion order produces a
non-identity permutation that interleaves M and N dims, and that
interleaved order is what the encoding materialization actually lays out
in memory.

`DataTiledMMAAttr::getUndistributedTileTypes` was silently ignoring that
permutation: it returned `(prod(expandShape[0]), prod(expandShape[1]))`
— operand-natural row-major — which contradicted the swizzle's stated
memory order. The mismatch was dormant when the permutation happened to
be identity (all pre-pretransposed intrinsics) but produces wrong tile
types for the M↔N-swapped enum values that the next branch in this stack
introduces, and is the root cause of the RDNA4 regression that took down
iree-org#24404.

Fix: lift the per-dim walk into a new shared utility
`Codegen::getTileVectorType(swizzle, elemType[, keepSize])` in
`IREECodegenTypes`. The shape is `sliceSwizzledShape(swizzle, keepSize)`
— every dim of `expandShape` in `swizzle.permutation()` order, with
optional per-dim masking to size 1. A dim with a non-`One` symbolic
multiplier maps to a scalable axis. CPU's
`DataTiledMMAAttr::getUndistributedTileTypes` and GPU's
`DataTiledMMAInterfaceAttr::{getUndistributedTileTypes,getDistributedTileTypes}`
now all go through it (the GPU distributed variant passes a `d.kind() !=
CrossThread` mask to fold thread-distribution factors to size-1 axes).

For trivial CPU cases (every `expandShape` group is a single dim — the
only configuration `fixupSwizzle` leaves after the base intrinsic build,
where `intrinsics_{m,n,k}` haven't yet been expanded in) the result is
the same rank-2 `(outer, inner)` shape CPU historically returned. For
non-identity permutations introduced by combined `intrinsics_m` and
`intrinsics_n` unrolling on ACC, the rank grows because the permutation
interleaves M and N dims and there's no rank-2 collapse that faithfully
represents the memory layout — and that's the whole point: the tile type
now matches what's actually in memory.

`getTileMxNxK` in `CPUEncodingExternalModels.cpp` previously read M, N,
K positionally from the tile vector shape, which is wrong once the
permutation is honored (M can be spread across multiple dims and
interleaved with N). It now goes through the LHS/RHS `TileSwizzle`s
directly: M is the product of LHS `expandShape[0]`, K is the product of
LHS `expandShape[1]`, N is the product of RHS `expandShape[0]` — the
per-operand `[M, K]` / `[N, K]` group partition is what
`getIntrinsicSwizzle` establishes by construction, regardless of how the
permutation later orders dims in memory.

Test fallout in this branch is limited to `invalid.mlir` (one expected
error string updated to reflect the rank increase from honoring the
permutation on a 16×1 LHS); other CPU lit tests on origin/main use
trivial unrollings that keep the rank-2 shape.

Progress towards iree-org#24323.

Signed-off-by: Benoit Jacob <[email protected]>
RattataKing pushed a commit to RattataKing/iree that referenced this pull request May 27, 2026
…24455)

Follow-up to iree-org#24437. That commit made `getUndistributedTileTypes` honor
the swizzle's permutation, but the encoder side
(`getInnerTiledEncodingInfo`) was still producing a rank-2 row-major
inner tile that ignored the swizzle's `expand_shape` and permutation.
For existing intrinsics the two sides happen to be compatible under
`matchTileTypes`' unit-dim filter, but the encoder's rank-2 shape
disagrees with the rank-N permutation-aware tile type as soon as any
swizzle has a non-identity permutation (e.g. pre-transposed intrinsics,
added in a follow-up).

Set `info.swizzle = IREE::CPU::getSwizzle(mma, operandIdx)` so the pack
pipeline's existing `getSwizzledShape` bridge applies the swizzle's
`expand_shape` + `transpose` to the packed inner tile, matching the tile
type `getUndistributedTileTypes` returns. Mirrors GPU's
`getEncodingInfoImpl`. Drop the stale "Only used on GPU" comment on the
`swizzle` field.

Test fallout: `materialize_encoding_x86_64.mlir`'s four
`_inner_tiled_avx512` CHECK groups now expect the
`tensor.expand_shape` after each pack and the
`tensor.collapse_shape` before the unpack, with the rank-N inner tile
that the swizzle materializes.

Progress towards iree-org#24323.

Signed-off-by: Benoit Jacob <[email protected]>
RattataKing pushed a commit to RattataKing/iree that referenced this pull request May 27, 2026
…sed enums. (iree-org#24456)

This is take two of iree-org#24404, which was reverted due to a RDNA4 regression
(iree-org#24427); see the regression delta at the end.

Each non-square hardware MMA now appears as two `MMAIntrinsic` enum
values — one per orientation — named after their actual MxNxK shape
(e.g. `MMA_X86_AVX512_1x16x1_F32_F32` and its M↔N-swapped sibling
`MMA_X86_AVX512_16x1x1_F32_F32`). The swapped sibling of `X` is `X | 1`
(bit 0 of D in the enum's encoding), so consecutive enum values form
natural/swapped pairs. The `transposed_intrinsic` boolean on
`DataTiledMMAAttr` is removed; both orientations show up as distinct
candidates in `getMmaIntrinsicsForTargetConfig` and the existing
`chooseIntrinsic` scoring picks between them.

A new `assert(isMmaIntrinsicArrayValid(...))` at the bottom of
`getMmaIntrinsicsForTargetConfig` enforces the convention: strictly
increasing, every swapped entry preceded by its natural sibling, and
square/type-symmetric singletons allowed (covers the generic-scalar
1×1×1 family).

Motivation: the boolean was originally there to avoid 2× enum growth,
but it leaked orientation-handling into every layer that touched MMA
layouts — swizzles, ACC encoding-info, the cross-intrinsic ACC index,
broadcast direction. It was also poised to get worse: asymmetric LLVM
intrinsics (vpdpbusd) and non-x86 targets (no broadcast) would each need
bespoke handling under the boolean. With separate enum values, each
orientation lowers independently.

Swapped enums are the first intrinsics to exercise a non-identity ACC
swizzle permutation: `Codegen::expand` on intrinsics_n then intrinsics_m
interleaves N and M dims in memory. Two prerequisite commits make that
permutation observable consistently across the type and encoder sides:

- iree-org#24437 makes `DataTiledMMAAttr::getUndistributedTileTypes` honor the
swizzle's permutation (rank-N tile type that reflects the actual memory
order).
- The predecessor commit on this branch attaches the swizzle to the
encoder's `MaterializeEncodingInfo` (`info.swizzle = ...`) so the packed
inner tile is the swizzle's permuted-expanded layout — the encoder-side
counterpart to iree-org#24437.

With both in place, the `reshapeToSwizzleDistributed` bridge in
`MMAUtils.cpp` collapses to a plain `vector.shape_cast` — same as it was
before non-identity permutations existed. New CHECKs in
`lower_inner_tiled.mlir`'s `lower_avx512_16x1x1_f32` pin the shape_cast
pair on the first multi-non-unit-dim swapped-intrinsic test case in the
file.

RDNA4 regression delta from iree-org#24404: the reverted PR left
`getUndistributedTileTypes` returning the wrong shape and introduced an
explicit `vector.transpose` in `reshapeToSwizzleDistributed` to
compensate. That transpose was correct for CPU but on GPU stacked on top
of GPU's already-permuted distributed shape, scrambling per-lane data on
RDNA4 (the only target whose swizzle had a non-trivial permutation at
the time). Fixing the tile type at the source removes the transpose and
keeps the shared lowering body uniform across targets.

Progress towards iree-org#24323.

Signed-off-by: Benoit Jacob <[email protected]>
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