[PATCH] D76415: [mlir] Fix unsafe create operation in GreedyPatternRewriter

Mahesh Ravishankar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 19 09:11:53 PDT 2020


mravishankar updated this revision to Diff 251402.
mravishankar added a comment.

Addressing comments


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D76415/new/

https://reviews.llvm.org/D76415

Files:
  mlir/include/mlir/Transforms/FoldUtils.h


Index: mlir/include/mlir/Transforms/FoldUtils.h
===================================================================
--- mlir/include/mlir/Transforms/FoldUtils.h
+++ mlir/include/mlir/Transforms/FoldUtils.h
@@ -75,11 +75,20 @@
   template <typename OpTy, typename... Args>
   void create(OpBuilder &builder, SmallVectorImpl<Value> &results,
               Location location, Args &&... args) {
-    Operation *op = builder.create<OpTy>(location, std::forward<Args>(args)...);
-    if (failed(tryToFold(op, results)))
+    // The op needs to be inserted only if the fold (below) fails, or the number
+    // of results of the op is zero (which is treated as an in-place
+    // fold). Using create methods of the builder will insert the op, so not
+    // using it here.
+    OperationState state(location, OpTy::getOperationName());
+    OpTy::build(&builder, state, std::forward<Args>(args)...);
+    Operation *op = Operation::create(state);
+
+    if (failed(tryToFold(op, results)) || op->getNumResults() == 0) {
+      builder.insert(op);
       results.assign(op->result_begin(), op->result_end());
-    else if (op->getNumResults() != 0)
-      op->erase();
+      return;
+    }
+    op->destroy();
   }
 
   /// Overload to create or fold a single result operation.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D76415.251402.patch
Type: text/x-patch
Size: 1274 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200319/fc76dfff/attachment.bin>


More information about the llvm-commits mailing list