Skip to content

[mlir][memref] canonicalization for erasing copying subview to identical subview #125852

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -824,8 +824,22 @@ struct FoldSelfCopy : public OpRewritePattern<CopyOp> {

LogicalResult matchAndRewrite(CopyOp copyOp,
PatternRewriter &rewriter) const override {
if (copyOp.getSource() != copyOp.getTarget())
return failure();
if (copyOp.getSource() != copyOp.getTarget()) {
// We can still fold if source and target are similar SubViews.
auto source = copyOp.getSource().getDefiningOp<SubViewOp>();
auto target = copyOp.getTarget().getDefiningOp<SubViewOp>();
if (!source || !target)
return failure();
if (source.getSource() != target.getSource() ||
source.getOffsets() != target.getOffsets() ||
source.getStaticOffsets() != target.getStaticOffsets() ||
source.getStrides() != target.getStrides() ||
source.getStaticStrides() != target.getStaticStrides()) {
// By copy semantics, sizes of source and target must be the same
// -> no need to check sizes.
return failure();
}
}

rewriter.eraseOp(copyOp);
return success();
Expand Down
30 changes: 30 additions & 0 deletions mlir/test/Dialect/MemRef/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,36 @@ func.func @self_copy(%m1: memref<?xf32>) {

// -----

func.func @self_copy_subview(%arg0: memref<?xf32>, %arg1: memref<?xf32>, %s: index) {
%c3 = arith.constant 3: index
%0 = memref.subview %arg0[3] [4] [2] : memref<?xf32> to memref<4xf32, strided<[2], offset: 3>>
%1 = memref.subview %arg0[%c3] [4] [2] : memref<?xf32> to memref<4xf32, strided<[2], offset: ?>>
%2 = memref.subview %arg0[%c3] [4] [%s] : memref<?xf32> to memref<4xf32, strided<[?], offset: ?>>
%3 = memref.subview %arg0[3] [4] [%s] : memref<?xf32> to memref<4xf32, strided<[?], offset: 3>>
%4 = memref.subview %arg1[3] [4] [%s] : memref<?xf32> to memref<4xf32, strided<[?], offset: 3>>
// erase (source and destination subviews render the same)
memref.copy %0, %1 : memref<4xf32, strided<[2], offset: 3>> to memref<4xf32, strided<[2], offset: ?>>
// keep (strides differ)
memref.copy %2, %1 : memref<4xf32, strided<[?], offset: ?>> to memref<4xf32, strided<[2], offset: ?>>
// erase (source and destination subviews render the same)
memref.copy %2, %3 : memref<4xf32, strided<[?], offset: ?>> to memref<4xf32, strided<[?], offset: 3>>
// keep (source and destination differ)
memref.copy %3, %4 : memref<4xf32, strided<[?], offset: 3>> to memref<4xf32, strided<[?], offset: 3>>
return
}

// CHECK-LABEL: func.func @self_copy_subview(
// CHECK-SAME: [[varg0:%.*]]: memref<?xf32>, [[varg1:%.*]]: memref<?xf32>, [[varg2:%.*]]: index) {
// CHECK: [[vsubview:%.*]] = memref.subview [[varg0]][3] [4] [2]
// CHECK: [[vsubview_0:%.*]] = memref.subview [[varg0]][3] [4] [[[varg2]]]
// CHECK: [[vsubview_1:%.*]] = memref.subview [[varg0]][3] [4] [[[varg2]]]
// CHECK: [[vsubview_2:%.*]] = memref.subview [[varg1]][3] [4] [[[varg2]]]
// CHECK-NEXT: memref.copy [[vsubview_0]], [[vsubview]]
// CHECK-NEXT: memref.copy [[vsubview_1]], [[vsubview_2]]
// CHECK-NEXT: return

// -----

// CHECK-LABEL: func @empty_copy
// CHECK-NEXT: return
func.func @empty_copy(%m1: memref<0x10xf32>, %m2: memref<?x10xf32>) {
Expand Down