[Mlir-commits] [mlir] [mlir][memref] canonicalization for erasing copying subview to identical subview (PR #125852)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Thu Feb 6 18:12:22 PST 2025


================
@@ -824,8 +824,36 @@ 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()) {
+      // If the source and target are SubViews and they are identical, we can
+      // fold.
+      auto source = copyOp.getSource().getDefiningOp<SubViewOp>();
+      auto target = copyOp.getTarget().getDefiningOp<SubViewOp>();
+      if (!source || !target || source.getSource() != target.getSource() ||
+          llvm::any_of(llvm::zip(source.getOffsets(), target.getOffsets()),
+                       [](std::tuple<Value, Value> offsetPair) {
+                         return std::get<0>(offsetPair) !=
+                                std::get<1>(offsetPair);
+                       }) ||
+          llvm::any_of(
+              llvm::zip(source.getStaticOffsets(), target.getStaticOffsets()),
+              [](std::tuple<int64_t, int64_t> offsetPair) {
+                return std::get<0>(offsetPair) != std::get<1>(offsetPair);
+              }) ||
+          // sizes must be the same anyway
+          llvm::any_of(llvm::zip(source.getStrides(), target.getStrides()),
+                       [](std::tuple<Value, Value> stridePair) {
+                         return std::get<0>(stridePair) !=
+                                std::get<1>(stridePair);
+                       }) ||
+          llvm::any_of(
+              llvm::zip(source.getStaticStrides(), target.getStaticStrides()),
----------------
MaheshRavishankar wrote:

Are you not testing the sizes as well since by semantics of copy they are the same size?

https://github.com/llvm/llvm-project/pull/125852


More information about the Mlir-commits mailing list