This is an archive of the discontinued LLVM Phabricator instance.

[mlir][tensor] Fold consecutive insert_slice/extract_slice ops
ClosedPublic

Authored by antiagainst on Sep 14 2022, 8:57 AM.

Details

Summary

Consecutive tensor.insert_slice/tensor.extract_slice can be
created for the case like tiling convolution and then downsizing
2-D convolutions into 1-D ones. It hinder further transformations.
So adding these patterns to clean it up.

Given that bufferization is sensitive and have requirements over
the IR structure (see https://reviews.llvm.org/D132666),
these patterns are put in Transforms/ with separate entry points
for explicit collection.

Diff Detail

Event Timeline

antiagainst created this revision.Sep 14 2022, 8:57 AM
antiagainst requested review of this revision.Sep 14 2022, 8:57 AM

Generally looks good but let's please standardize to e.g. InsertExtractSlicePatterns.cpp to match some of the sanitization on the vector side, e.g.

mlir/lib/Dialect/Vector/Transforms/VectorTransferSplitRewritePatterns.cpp
mlir/lib/Dialect/Vector/Transforms/VectorTransferPermutationMapRewritePatterns.cpp
mlir/lib/Dialect/Vector/Transforms/VectorInsertExtractStridedSliceRewritePatterns.cpp

Also "fold" is overloaded and generally means no new IR is created, finding a better name would be good.

mlir/lib/Dialect/Tensor/Transforms/CMakeLists.txt
5

Please suffix with Patterns.cpp

Address comments

Generally looks good but let's please standardize to e.g. InsertExtractSlicePatterns.cpp to match some of the sanitization on the vector side, e.g.

mlir/lib/Dialect/Vector/Transforms/VectorTransferSplitRewritePatterns.cpp
mlir/lib/Dialect/Vector/Transforms/VectorTransferPermutationMapRewritePatterns.cpp
mlir/lib/Dialect/Vector/Transforms/VectorInsertExtractStridedSliceRewritePatterns.cpp

Also "fold" is overloaded and generally means no new IR is created, finding a better name would be good.

@nicolasvasilache: done! I've also uploaded https://reviews.llvm.org/D134202 to make the rest consistent.

mravishankar requested changes to this revision.Sep 19 2022, 1:35 PM

IREE has https://github.com/iree-org/iree/blob/4a003840ceb95a3a4c103ee584170d49ec039aa4/compiler/src/iree/compiler/Dialect/Flow/IR/FlowOps.h#L41 which works on the OffsetSizeAndStrideOpInterface and is general enough to work with rank-reducing slices and strides. You can probably move that method into MLIR (IREE can be udpated to use things from here).

This revision now requires changes to proceed.Sep 19 2022, 1:35 PM

IREE has https://github.com/iree-org/iree/blob/4a003840ceb95a3a4c103ee584170d49ec039aa4/compiler/src/iree/compiler/Dialect/Flow/IR/FlowOps.h#L41 which works on the OffsetSizeAndStrideOpInterface and is general enough to work with rank-reducing slices and strides. You can probably move that method into MLIR (IREE can be udpated to use things from here).

Nice! Thanks for the pointers! I uploaded a subsequent patch to take the IREE code and extend the pattern to be more general: https://reviews.llvm.org/D134294

Merge the two patches? Looked at the other one, it looks fine to me (but will wait for someone else to review that code since I wrote that one :P )

Merge the two patches? Looked at the other one, it looks fine to me (but will wait for someone else to review that code since I wrote that one :P )

Let's keep them separate. So I can land the second one with you as a coauthor. ;-P

ThomasRaoux accepted this revision.Sep 20 2022, 3:17 PM

LGTM added couple minor comments.

mlir/lib/Dialect/Tensor/Transforms/MergeConsecutiveInsertExtractSlicePatterns.cpp
60

nit: can be: prevOp.getType()

107

that seems like a strong assert. Are you sure we cannot have a valid IR with zero offsets? The offset could be a Value that has to be dynamically equal to zero.

antiagainst marked 2 inline comments as done.

Address comments

mlir/lib/Dialect/Tensor/Transforms/MergeConsecutiveInsertExtractSlicePatterns.cpp
107

Good point. Dropped this.

mravishankar accepted this revision.Sep 20 2022, 4:50 PM
This revision is now accepted and ready to land.Sep 20 2022, 4:50 PM
This revision was landed with ongoing or failed builds.Sep 20 2022, 5:00 PM
This revision was automatically updated to reflect the committed changes.