Skip to content

[Codegen] Skip swizzle transpose when permutation is a no-op modulo unit dims. - #24490

Merged
bjacob merged 2 commits into
mainfrom
users/bjacob/codegen-skip-noop-swizzle-transpose
May 15, 2026
Merged

bjacob merged 2 commits into
mainfrom
users/bjacob/codegen-skip-noop-swizzle-transpose

Conversation

@bjacob

@bjacob bjacob commented May 14, 2026

Copy link
Copy Markdown
Collaborator

The set_encoding pack lowering (and symmetrically the unset_encoding unpack) currently always emits

linalg.pack → tensor.expand_shape → linalg.transpose

to apply the swizzle's permutation to the packed inner tile. For many swizzles the permutation only reorders unit dims relative to non-unit dims (e.g. the generic-scalar 1×1×1 MMA ACC swizzle has expandShape = [[CrossIntrinsic(M), Internal(1)], [CrossIntrinsic(N), Internal(1)]] with permutation = [0, 2, 1, 3] — swapping the Internal(1) placeholders past the CrossIntrinsic non-unit dims). The linearized memory layout of the unpermuted and permuted shapes is identical there, so the linalg.transpose is a no-op in terms of data movement.

Add isSwizzlePermutationLayoutNoOp, a predicate that detects this case (the permutation, restricted to non-unit source dims, is order-preserving), and getSwizzleFoldedReassociation, which — given that predicate holds — builds the reassociation that drops the linalg.transpose: the tensor.expand_shape (pack side) / tensor.collapse_shape (unpack side) is instead given a reassociation that lands directly on the permuted shape, partitioning the permuted inner dims into contiguous slices matching the pack's innerTileSizes one-to-one. Fall through to the existing expand + transpose lowering when the permutation actually reorders non-unit dims — the gfx950 scaled-matmul case for instance, where the permutation moves a size-16 dim past two size-4 dims, must keep the transpose.

The folded reshape still carries the swizzle's unit dims: the permuted inner shape is the materialized encoding type fixed by getEncodingInfo, which dialect conversion requires the replacement value to match exactly — dropping unit dims is not a local choice here. Hence getSwizzleFoldedReassociation only picks a reassociation reaching that already-fixed shape; whether the materialized type should carry unit dims at all is a separate, cross-cutting question.

This was motivated by a separate bug in
hoistOneStaticallyBoundAllocation (#24483): under dynamic shapes the intermediate tensor.empty between the pack and the transpose bufferizes to a stack alloca whose size comes from ValueBoundsConstraintSet::computeConstantBound on the dynamic dim, which for a workload-ordinal dim with only the default index umax constraint (2^53 - 1) produces a ~2^52-element static alloca, overflows i64 stride math, and lowers to llvm.alloca poison. Eliminating the intermediate via this fast path sidesteps the bug for the cases that hit this path; the hoist bug is real and the fix here is in addition to fixing it at the source.

Progress towards #24323.

…r tiles.

After #24455 attaches the per-operand swizzle to the encoder info, the
`inner_tiled` op for the generic-scalar 1×1×1 MMA family receives
operands whose non-unit dim (size = `intrinsics_{m,n,k}`) can sit at
any position in the rank-N tile, not just at the start. The previous
`dropTrailingUnitDims` reduction relied on non-unit dims being leading,
so e.g. an LHS tile of shape `vector<1x1x4x1x1xf32>` (intrinsics_m=4
with N=K=1) collapsed to rank-3 `vector<1x1x4>` instead of the rank-2
`(M, K)` shape `vector.contract` needs — and the wrapper code then
emitted a malformed `vector.broadcast` of `vector<4x1>` into
`vector<1x1x4x1x1>` to glue the result back to the inner_tiled's
output tile.

Switch to a `vector.shape_cast` directly to the canonical
`(M, K) / (N, K) / (M, N)` rank-2 shape: by construction (1×1×1 base
intrinsic) every non-unit dim contributing to an operand tile came
from an `intrinsics_*` cross-intrinsic factor, so the total element
count of each operand is exactly `intrinsics_m * intrinsics_k` (etc.)
and the shape_cast is element-preserving. `shape_cast` the contract
result back to the inner_tiled op's original ACC tile shape so
verification matches downstream.

Progress towards #24323.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Signed-off-by: Benoit Jacob <[email protected]>
@bjacob bjacob mentioned this pull request May 14, 2026
25 of 28 tasks
Base automatically changed from users/bjacob/cpu-generic-scalar-rank-n-tile to main May 14, 2026 20:18
@bjacob
bjacob marked this pull request as ready for review May 14, 2026 20:32
// CHECK-SAME: : tensor<1x1x2x4xf32> into tensor<1x1x2x4x1x1xf32>
// CHECK-NOT: linalg.transpose
// CHECK: return %[[EXPAND]] : tensor<1x1x2x4x1x1xf32>

@hanhanW hanhanW May 14, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm thinking if we still add a seperator (i.e., // -----) between two test cases. IIUC, you don't make the seperator because they share the same affine maps and encodings. They are just four lines, so I think we can still have the seperator.

The other option is keeping it as what it is, and I think you should declare an alias for executable target. So it can be used in both cases.

…nit dims.

The set_encoding pack lowering (and symmetrically the unset_encoding
unpack) currently always emits

  linalg.pack → tensor.expand_shape → linalg.transpose

to apply the swizzle's `permutation` to the packed inner tile. For
many swizzles the permutation only reorders unit dims relative to
non-unit dims (e.g. the generic-scalar 1×1×1 MMA ACC swizzle has
`expandShape = [[CrossIntrinsic(M), Internal(1)], [CrossIntrinsic(N),
Internal(1)]]` with `permutation = [0, 2, 1, 3]` — swapping the
`Internal(1)` placeholders past the `CrossIntrinsic` non-unit dims).
The linearized memory layout of the unpermuted and permuted shapes
is identical there, so the `linalg.transpose` is a no-op in terms of
data movement.

Add `isSwizzlePermutationLayoutNoOp`, a predicate that detects this
case (the permutation, restricted to non-unit source dims, is
order-preserving), and `getSwizzleFoldedReassociation`, which — when
the fold is expressible as a single reshape — builds a reassociation
that drops the `linalg.transpose`: the `tensor.expand_shape` (pack
side) / `tensor.collapse_shape` (unpack side) is instead given a
reassociation that lands directly on the permuted shape, partitioning
the permuted inner dims into contiguous slices matching the pack's
`innerTileSizes` one-to-one. Fall through to the existing expand +
transpose lowering when the permutation actually reorders non-unit
dims — the gfx950 scaled-matmul case for instance, where the
permutation moves a size-16 dim past two size-4 dims, must keep the
transpose — or when the layout-no-op result still isn't a single
reshape (a later group's non-unit dim positioned before an earlier
group's content; e.g. data-tiled i8 generic matmul with an all-unit
M group swapped past N's size-7 non-unit dim).

The folded reshape still carries the swizzle's unit dims: the
permuted inner shape is the materialized encoding type fixed by
`getEncodingInfo`, which dialect conversion requires the replacement
value to match exactly — dropping unit dims is not a local choice
here. Hence `getSwizzleFoldedReassociation` only picks a reassociation
reaching that already-fixed shape; whether the materialized type
should carry unit dims at all is a separate, cross-cutting question.

This was motivated by a separate bug in
`hoistOneStaticallyBoundAllocation` (#24483): under dynamic shapes
the intermediate `tensor.empty` between the pack and the transpose
bufferizes to a stack alloca whose size comes from
`ValueBoundsConstraintSet::computeConstantBound` on the dynamic dim,
which for a workload-ordinal dim with only the default `index` umax
constraint (2^53 - 1) produces a ~2^52-element static alloca,
overflows i64 stride math, and lowers to `llvm.alloca poison`.
Eliminating the intermediate via this fast path sidesteps the bug
for the cases that hit this path; the hoist bug is real and the
fix here is in addition to fixing it at the source.

Progress towards #24323.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Signed-off-by: Benoit Jacob <[email protected]>
@bjacob
bjacob force-pushed the users/bjacob/codegen-skip-noop-swizzle-transpose branch from e7cf2f6 to 68100a9 Compare May 15, 2026 14:04
@bjacob
bjacob merged commit 40431d9 into main May 15, 2026
65 checks passed
@bjacob
bjacob deleted the users/bjacob/codegen-skip-noop-swizzle-transpose branch May 15, 2026 14:54
RattataKing pushed a commit to RattataKing/iree that referenced this pull request May 27, 2026
…nit dims. (iree-org#24490)

The set_encoding pack lowering (and symmetrically the unset_encoding
unpack) currently always emits

  linalg.pack → tensor.expand_shape → linalg.transpose

to apply the swizzle's `permutation` to the packed inner tile. For many
swizzles the permutation only reorders unit dims relative to non-unit
dims (e.g. the generic-scalar 1×1×1 MMA ACC swizzle has `expandShape =
[[CrossIntrinsic(M), Internal(1)], [CrossIntrinsic(N), Internal(1)]]`
with `permutation = [0, 2, 1, 3]` — swapping the `Internal(1)`
placeholders past the `CrossIntrinsic` non-unit dims). The linearized
memory layout of the unpermuted and permuted shapes is identical there,
so the `linalg.transpose` is a no-op in terms of data movement.

Add `isSwizzlePermutationLayoutNoOp`, a predicate that detects this case
(the permutation, restricted to non-unit source dims, is
order-preserving), and `getSwizzleFoldedReassociation`, which — given
that predicate holds — builds the reassociation that drops the
`linalg.transpose`: the `tensor.expand_shape` (pack side) /
`tensor.collapse_shape` (unpack side) is instead given a reassociation
that lands directly on the permuted shape, partitioning the permuted
inner dims into contiguous slices matching the pack's `innerTileSizes`
one-to-one. Fall through to the existing expand + transpose lowering
when the permutation actually reorders non-unit dims — the gfx950
scaled-matmul case for instance, where the permutation moves a size-16
dim past two size-4 dims, must keep the transpose.

The folded reshape still carries the swizzle's unit dims: the permuted
inner shape is the materialized encoding type fixed by
`getEncodingInfo`, which dialect conversion requires the replacement
value to match exactly — dropping unit dims is not a local choice here.
Hence `getSwizzleFoldedReassociation` only picks a reassociation
reaching that already-fixed shape; whether the materialized type should
carry unit dims at all is a separate, cross-cutting question.

This was motivated by a separate bug in
`hoistOneStaticallyBoundAllocation` (iree-org#24483): under dynamic shapes the
intermediate `tensor.empty` between the pack and the transpose
bufferizes to a stack alloca whose size comes from
`ValueBoundsConstraintSet::computeConstantBound` on the dynamic dim,
which for a workload-ordinal dim with only the default `index` umax
constraint (2^53 - 1) produces a ~2^52-element static alloca, overflows
i64 stride math, and lowers to `llvm.alloca poison`. Eliminating the
intermediate via this fast path sidesteps the bug for the cases that hit
this path; the hoist bug is real and the fix here is in addition to
fixing it at the source.

Progress towards iree-org#24323.

---------

Signed-off-by: Benoit Jacob <[email protected]>
Co-authored-by: Claude Opus 4.7 (1M context) <[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