[Mlir-commits] [mlir] [mlir][Transforms] More detailed error message when new IR cannot be legalized (PR #152297)

Matthias Springer llvmlistbot at llvm.org
Wed Aug 6 05:05:09 PDT 2025


https://github.com/matthias-springer created https://github.com/llvm/llvm-project/pull/152297

Print a more detailed error message when new/modified IR could not be legalized with `allowPatternRollback = false`. This is useful to understand why a pattern is incompatible with the new One-Shot Dialect Conversion driver.


>From c53c18e526ab12b84720c85eea3a84ed3a5f38aa Mon Sep 17 00:00:00 2001
From: Matthias Springer <me at m-sp.org>
Date: Wed, 6 Aug 2025 12:03:28 +0000
Subject: [PATCH] [mlir][Transforms] More detailed error message when new IR
 cannot be legalized

---
 .../Transforms/Utils/DialectConversion.cpp    | 35 +++++++++++++++++--
 1 file changed, 33 insertions(+), 2 deletions(-)

diff --git a/mlir/lib/Transforms/Utils/DialectConversion.cpp b/mlir/lib/Transforms/Utils/DialectConversion.cpp
index f23c6197accd5..463394c19b51c 100644
--- a/mlir/lib/Transforms/Utils/DialectConversion.cpp
+++ b/mlir/lib/Transforms/Utils/DialectConversion.cpp
@@ -2302,6 +2302,37 @@ OperationLegalizer::legalizeWithFold(Operation *op,
   return success();
 }
 
+/// Report a fatal error indicating that newly produced or modified IR could
+/// not be legalized.
+static void
+reportNewIrLegalizationFatalError(const Pattern &pattern,
+                                  const SetVector<Operation *> &newOps,
+                                  const SetVector<Operation *> &modifiedOps,
+                                  const SetVector<Block *> &insertedBlocks) {
+  StringRef detachedBlockStr = "(detached block)";
+  std::string newOpNames = llvm::join(
+      llvm::map_range(
+          newOps, [](Operation *op) { return op->getName().getStringRef(); }),
+      ", ");
+  std::string modifiedOpNames = llvm::join(
+      llvm::map_range(
+          newOps, [](Operation *op) { return op->getName().getStringRef(); }),
+      ", ");
+  std::string insertedBlockNames = llvm::join(
+      llvm::map_range(insertedBlocks,
+                      [&](Block *block) {
+                        if (block->getParentOp())
+                          return block->getParentOp()->getName().getStringRef();
+                        return detachedBlockStr;
+                      }),
+      ", ");
+  llvm::report_fatal_error(
+      "pattern '" + pattern.getDebugName() +
+      "' produced IR that could not be legalized. " + "new ops: {" +
+      newOpNames + "}, " + "modified ops: {" + modifiedOpNames + "}, " +
+      "inserted block into ops: {" + insertedBlockNames + "}");
+}
+
 LogicalResult
 OperationLegalizer::legalizeWithPattern(Operation *op,
                                         ConversionPatternRewriter &rewriter) {
@@ -2389,8 +2420,8 @@ OperationLegalizer::legalizeWithPattern(Operation *op,
     appliedPatterns.erase(&pattern);
     if (failed(result)) {
       if (!rewriterImpl.config.allowPatternRollback)
-        llvm::report_fatal_error("pattern '" + pattern.getDebugName() +
-                                 "' produced IR that could not be legalized");
+        reportNewIrLegalizationFatalError(pattern, newOps, modifiedOps,
+                                          insertedBlocks);
       rewriterImpl.resetState(curState, pattern.getDebugName());
     }
     if (config.listener)



More information about the Mlir-commits mailing list