[PATCH] D72379: Canonicalize static alloc followed by memref_cast and std.view

Ahmed S. Taei via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 7 17:50:39 PST 2020


asaadaldien created this revision.
asaadaldien added a reviewer: nicolasvasilache.
Herald added subscribers: llvm-commits, lucyrfox, mgester, arpith-jacob, antiagainst, shauheen, burmako, jpienaar, rriddle, mehdi_amini.
Herald added a project: LLVM.

Rewrite alloc, memref_cast, std.view into allo, std.view by droping memref_cast.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D72379

Files:
  mlir/lib/Dialect/StandardOps/Ops.cpp
  mlir/test/Transforms/canonicalize.mlir


Index: mlir/test/Transforms/canonicalize.mlir
===================================================================
--- mlir/test/Transforms/canonicalize.mlir
+++ mlir/test/Transforms/canonicalize.mlir
@@ -735,6 +735,11 @@
     : memref<2048xi8> to memref<?x4xf32, #TEST_VIEW_MAP2>
   load %5[%c0, %c0] : memref<?x4xf32, #TEST_VIEW_MAP2>
 
+  // Test: folding static alloc and memref_cast into a view.
+  // CHECK: std.view %0[][%c15, %c7] : memref<2048xi8> to memref<?x?xf32>
+  %6 = memref_cast %0 : memref<2048xi8> to memref<?xi8>
+  %7 = view %6[%c15][%c7] : memref<?xi8> to memref<?x?xf32>
+  load %7[%c0, %c0] : memref<?x?xf32>
   return
 }
 
Index: mlir/lib/Dialect/StandardOps/Ops.cpp
===================================================================
--- mlir/lib/Dialect/StandardOps/Ops.cpp
+++ mlir/lib/Dialect/StandardOps/Ops.cpp
@@ -2527,11 +2527,36 @@
   }
 };
 
+struct ViewOpMemrefCastFolder : public OpRewritePattern<ViewOp> {
+  using OpRewritePattern<ViewOp>::OpRewritePattern;
+
+  PatternMatchResult matchAndRewrite(ViewOp viewOp,
+                                     PatternRewriter &rewriter) const override {
+    Value memrefOperand = viewOp.getOperand(0);
+    MemRefCastOp memrefCastOp =
+        dyn_cast_or_null<MemRefCastOp>(memrefOperand.getDefiningOp());
+    if (!memrefCastOp) {
+      return matchFailure();
+    }
+    Value allocOperand = memrefCastOp.getOperand();
+    AllocOp allocOp = dyn_cast_or_null<AllocOp>(allocOperand.getDefiningOp());
+    if (!allocOp)
+      return matchFailure();
+
+    auto newOperands = {viewOp.getOperands()[1], viewOp.getOperands()[2]};
+
+    // Replace view op and remove memrefcast values.
+    rewriter.replaceOpWithNewOp<ViewOp>(memrefOperand, viewOp, viewOp.getType(),
+                                        allocOperand, newOperands);
+    return matchSuccess();
+  }
+};
+
 } // end anonymous namespace
 
 void ViewOp::getCanonicalizationPatterns(OwningRewritePatternList &results,
                                          MLIRContext *context) {
-  results.insert<ViewOpShapeFolder>(context);
+  results.insert<ViewOpShapeFolder, ViewOpMemrefCastFolder>(context);
 }
 
 //===----------------------------------------------------------------------===//


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D72379.236727.patch
Type: text/x-patch
Size: 2236 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200108/0300f0d8/attachment.bin>


More information about the llvm-commits mailing list