[Mlir-commits] [mlir] [MLIR] Handle materializeConstant failure in GreedyPatternRewriteDriver (PR #77258)

Matthias Springer llvmlistbot at llvm.org
Mon Jan 8 05:43:47 PST 2024


================
@@ -462,18 +463,38 @@ bool GreedyPatternRewriteDriver::processWorklist() {
           // Materialize Attributes as SSA values.
           Operation *constOp = op->getDialect()->materializeConstant(
               *this, ofr.get<Attribute>(), resultType, op->getLoc());
+
+          if (!constOp) {
+            // If materialization fails, cleanup any operations generated for
+            // the previous results.
+            llvm::SmallDenseSet<Operation *> replacementOps;
+            std::transform(replacements.begin(), replacements.end(),
+                           replacementOps.begin(), [](Value replacement) {
+                             return replacement.getDefiningOp();
+                           });
+            for (Operation *op : replacementOps)
+              eraseOp(op);
----------------
matthias-springer wrote:

Maybe assert that materialized constant does not have any uses, to protect against "smart" materializers that try to reuse an existing op.

(We should fail here anyway, but then we have a nicer error message.)


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


More information about the Mlir-commits mailing list