[Mlir-commits] [mlir] [mlir][memref] Fold CopyOp if source and target are practically the same (PR #171801)

Kunwar Grover llvmlistbot at llvm.org
Thu Dec 11 03:17:49 PST 2025


================
@@ -812,9 +812,22 @@ namespace {
 struct FoldSelfCopy : public OpRewritePattern<CopyOp> {
   using OpRewritePattern<CopyOp>::OpRewritePattern;
 
+  // Given a value, traverse through CastOp, CollapseShapeOp and ExpandShapeOp
+  // to get the root value. The root and the original value have the same data.
+  // Thus operations like SubViewOp are not considered here.
+  static Value getRoot(Value v) {
+    while (auto *definingOp = v.getDefiningOp()) {
+      if (!isa<CastOp, CollapseShapeOp, ExpandShapeOp>(definingOp))
+        return v;
+      v = definingOp->getOperand(0);
+    }
+    return v;
+  }
----------------
Groverkss wrote:

If there is a chain that gets the same value, then the entire chain should fold into that value, we shouldn't be looking at the chain like this.

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


More information about the Mlir-commits mailing list