Repository navigation
[Codegen] Skip swizzle transpose when permutation is a no-op modulo unit dims. - #24490
Merged
Merged
Conversation
…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]>
25 of 28 tasks
Base automatically changed from
users/bjacob/cpu-generic-scalar-rank-n-tile
to
main
May 14, 2026 20:18
bjacob
marked this pull request as ready for review
May 14, 2026 20:32
bjacob
requested review from
MaheshRavishankar,
Max191,
hanhanW and
qedawkins
as code owners
May 14, 2026 20:32
hanhanW
approved these changes
May 14, 2026
| // CHECK-SAME: : tensor<1x1x2x4xf32> into tensor<1x1x2x4x1x1xf32> | ||
| // CHECK-NOT: linalg.transpose | ||
| // CHECK: return %[[EXPAND]] : tensor<1x1x2x4x1x1xf32> | ||
|
|
Contributor
There was a problem hiding this comment.
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
force-pushed
the
users/bjacob/codegen-skip-noop-swizzle-transpose
branch
from
May 15, 2026 14:04
e7cf2f6 to
68100a9
Compare
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
permutationto 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 hasexpandShape = [[CrossIntrinsic(M), Internal(1)], [CrossIntrinsic(N), Internal(1)]]withpermutation = [0, 2, 1, 3]— swapping theInternal(1)placeholders past theCrossIntrinsicnon-unit dims). The linearized memory layout of the unpermuted and permuted shapes is identical there, so thelinalg.transposeis 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), andgetSwizzleFoldedReassociation, which — given that predicate holds — builds the reassociation that drops thelinalg.transpose: thetensor.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'sinnerTileSizesone-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. HencegetSwizzleFoldedReassociationonly 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 intermediatetensor.emptybetween the pack and the transpose bufferizes to a stack alloca whose size comes fromValueBoundsConstraintSet::computeConstantBoundon the dynamic dim, which for a workload-ordinal dim with only the defaultindexumax constraint (2^53 - 1) produces a ~2^52-element static alloca, overflows i64 stride math, and lowers tollvm.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.