[Mlir-commits] [mlir] e58e401 - [mlir][memref] Fold self copies

Matthias Springer llvmlistbot at llvm.org
Fri Jan 14 05:44:40 PST 2022


Author: Matthias Springer
Date: 2022-01-14T22:44:22+09:00
New Revision: e58e401b798845ebaf131528d286b39dadfda914

URL: https://github.com/llvm/llvm-project/commit/e58e401b798845ebaf131528d286b39dadfda914
DIFF: https://github.com/llvm/llvm-project/commit/e58e401b798845ebaf131528d286b39dadfda914.diff

LOG: [mlir][memref] Fold self copies

Fold `memref.copy %x, %x`.

Differential Revision: https://reviews.llvm.org/D117224

Added: 
    

Modified: 
    mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
    mlir/test/Dialect/MemRef/canonicalize.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp b/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
index 11e8de603062e..5d3ec36983d1f 100644
--- a/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
+++ b/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
@@ -486,11 +486,25 @@ struct FoldCopyOfCast : public OpRewritePattern<CopyOp> {
     return success(modified);
   }
 };
+
+/// Fold memref.copy(%x, %x).
+struct FoldSelfCopy : public OpRewritePattern<CopyOp> {
+  using OpRewritePattern<CopyOp>::OpRewritePattern;
+
+  LogicalResult matchAndRewrite(CopyOp copyOp,
+                                PatternRewriter &rewriter) const override {
+    if (copyOp.source() != copyOp.target())
+      return failure();
+
+    rewriter.eraseOp(copyOp);
+    return success();
+  }
+};
 } // namespace
 
 void CopyOp::getCanonicalizationPatterns(RewritePatternSet &results,
                                          MLIRContext *context) {
-  results.add<FoldCopyOfCast>(context);
+  results.add<FoldCopyOfCast, FoldSelfCopy>(context);
 }
 
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/test/Dialect/MemRef/canonicalize.mlir b/mlir/test/Dialect/MemRef/canonicalize.mlir
index 092f527a15af3..bd7a8dd830a80 100644
--- a/mlir/test/Dialect/MemRef/canonicalize.mlir
+++ b/mlir/test/Dialect/MemRef/canonicalize.mlir
@@ -525,3 +525,13 @@ func @copy_of_cast(%m1: memref<?xf32>, %m2: memref<*xf32>) {
 //  CHECK-SAME:     %[[m1:.*]]: memref<?xf32>, %[[m2:.*]]: memref<*xf32>
 //       CHECK:   %[[casted2:.*]] = memref.cast %[[m2]]
 //       CHECK:   memref.copy %[[m1]], %[[casted2]]
+
+// -----
+
+func @self_copy(%m1: memref<?xf32>) {
+  memref.copy %m1, %m1 : memref<?xf32> to memref<?xf32>
+  return
+}
+
+// CHECK-LABEL: func @self_copy
+//  CHECK-NEXT:   return


        


More information about the Mlir-commits mailing list