[Mlir-commits] [mlir] a756f12 - [mlir][Linalg] Add folding of linalg.copy that are in fact identities.

Nicolas Vasilache llvmlistbot at llvm.org
Thu Mar 4 05:41:40 PST 2021


Author: Nicolas Vasilache
Date: 2021-03-04T13:37:26Z
New Revision: a756f12b4dc94a2bcd8cd5e57ed7d64f9b3d4670

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

LOG: [mlir][Linalg] Add folding of linalg.copy that are in fact identities.

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

Added: 
    

Modified: 
    mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp
    mlir/test/Dialect/Linalg/canonicalize.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp b/mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp
index 4a2999aeaa37..1f686b940812 100644
--- a/mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp
+++ b/mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp
@@ -2595,6 +2595,15 @@ struct RemoveIdentityLinalgOps : public RewritePattern {
 
   LogicalResult matchAndRewrite(Operation *op,
                                 PatternRewriter &rewriter) const override {
+    if (auto copyOp = dyn_cast<CopyOp>(op)) {
+      assert(copyOp.hasBufferSemantics());
+      if (copyOp.input() == copyOp.output() &&
+          copyOp.inputPermutation() == copyOp.outputPermutation()) {
+        rewriter.eraseOp(op);
+        return success();
+      }
+    }
+
     if (!isa<GenericOp, IndexedGenericOp>(op))
       return failure();
     LinalgOp genericOp = cast<LinalgOp>(op);

diff  --git a/mlir/test/Dialect/Linalg/canonicalize.mlir b/mlir/test/Dialect/Linalg/canonicalize.mlir
index f2f3a44169e8..1ab790ad84de 100644
--- a/mlir/test/Dialect/Linalg/canonicalize.mlir
+++ b/mlir/test/Dialect/Linalg/canonicalize.mlir
@@ -789,3 +789,15 @@ func @propogate_casts(%arg0 : tensor<?x?xf32>, %arg1 : f32, %arg2 : index,
 //       CHECK:   %[[INSERTED:.+]] = subtensor_insert %{{.+}} into %[[FILL]]
 //       CHECK:   %[[RESULT:.+]] = tensor.cast %[[INSERTED]]
 //       CHECK:   return %[[RESULT]]
+
+// -----
+
+// CHECK-LABEL: @self_copy
+func @self_copy(%arg0 : memref<2x3x?x4xf32>) {
+
+//   CHECK-NOT: linalg.copy
+  linalg.copy(%arg0, %arg0): memref<2x3x?x4xf32>, memref<2x3x?x4xf32>
+
+//   CHECK: return
+  return
+}


        


More information about the Mlir-commits mailing list