Skip to content

[Codegen] Add SwapExtractWithCollapsePattern - #21419

Merged
yzhang93 merged 5 commits into
iree-org:mainfrom
yzhang93:swap_collapse_with_slice
Jul 24, 2025
Merged

yzhang93 merged 5 commits into
iree-org:mainfrom
yzhang93:swap_collapse_with_slice

Conversation

@yzhang93

Copy link
Copy Markdown
Contributor

This PR adds a pattern to swap the collapse_shape with the extract_slice op to enable more loop fusion opportunity. Note this pattern is adapted from the upstream BubbleUpCollapseShapeThroughExtractSlice by allowing some special cases we've seen through the IGEMM path.

The special case is processed under CASE #1 session with the corresponding tests mainly focusing on the changes. Note that some strict conditions are added to match the special case for convolutions which maybe not general and robust enough to add to upstream. In addition, we break the upstream single pattern into two separate ones to be able to apply them separately in different passes.

@yzhang93
yzhang93 force-pushed the swap_collapse_with_slice branch from 0a411b0 to bd3b05d Compare July 18, 2025 22:10
if (isa<Value>(collapsedSize) || isa<Value>(collapsedOffset)) {
// Special case especially for collapse shape of convolution filter in
// IGEMM, while the offset is dynamic and the size is static.
if (isa<Attribute>(collapsedSize) && isa<Value>(collapsedOffset) &&

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

From here to line 544 is the added special case.

@Max191 Max191 left a comment

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.

First round of comments. If we can do the fusion in GPUFuseAndHoistParallelLoops, then it would be good to avoid adding the new pass option.

yzhang93 added 2 commits July 21, 2025 22:17
Signed-off-by: yzhang93 <[email protected]>
Signed-off-by: yzhang93 <[email protected]>

@Max191 Max191 left a comment

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.

LGTM, just one nit!

Comment on lines +486 to +489
if (map.getNumInputs() != 1 || map.getNumResults() != 1) {
return rewriter.notifyMatchFailure(
sliceOp, "affine.apply must have one input and one result");
}

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 think having multiple inputs is okay, as long as there is a single result. isMultipleOf only checks the result, so the number of inputs shouldn't matter.

Signed-off-by: yzhang93 <[email protected]>
@yzhang93
yzhang93 enabled auto-merge (squash) July 24, 2025 20:01
@yzhang93
yzhang93 merged commit 35984f0 into iree-org:main Jul 24, 2025
AWoloszyn pushed a commit that referenced this pull request Dec 1, 2025
This PR adds a pattern to swap the `collapse_shape` with the
`extract_slice` op to enable more loop fusion opportunity. Note this
pattern is adapted from the upstream[
`BubbleUpCollapseShapeThroughExtractSlice`](https://github.com/llvm/llvm-project/blob/ffb453989b0e95d85b6cfa543b65fec23b65649d/mlir/lib/Dialect/Tensor/Transforms/ReshapePatterns.cpp#L322)
by allowing some special cases we've seen through the IGEMM path.

The special case is processed under `CASE #1` session with the
corresponding tests mainly focusing on the changes. Note that some
strict conditions are added to match the special case for convolutions
which maybe not general and robust enough to add to upstream. In
addition, we break the [upstream single
pattern](https://github.com/llvm/llvm-project/blob/ffb453989b0e95d85b6cfa543b65fec23b65649d/mlir/lib/Dialect/Tensor/Transforms/ReshapePatterns.cpp#L767)
into two separate ones to be able to apply them separately in different
passes.

---------

Signed-off-by: yzhang93 <[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