Skip to content

[mlir][Transforms] More detailed error message when new IR cannot be legalized #152297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

matthias-springer
Copy link
Member

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.

@llvmbot llvmbot added mlir:core MLIR Core Infrastructure mlir labels Aug 6, 2025
@matthias-springer matthias-springer requested review from zero9178 and j2kun and removed request for zero9178 August 6, 2025 12:05
@llvmbot
Copy link
Member

llvmbot commented Aug 6, 2025

@llvm/pr-subscribers-mlir

Author: Matthias Springer (matthias-springer)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/152297.diff

1 Files Affected:

  • (modified) mlir/lib/Transforms/Utils/DialectConversion.cpp (+33-2)
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)

@llvmbot
Copy link
Member

llvmbot commented Aug 6, 2025

@llvm/pr-subscribers-mlir-core

Author: Matthias Springer (matthias-springer)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/152297.diff

1 Files Affected:

  • (modified) mlir/lib/Transforms/Utils/DialectConversion.cpp (+33-2)
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)

Copy link
Contributor

@j2kun j2kun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with one change

Copy link

github-actions bot commented Aug 7, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@matthias-springer matthias-springer force-pushed the users/matthias-springer/failed_leg_new_error branch from 8f7faa2 to 73dfec2 Compare August 7, 2025 07:04
@matthias-springer matthias-springer merged commit a3e0685 into main Aug 7, 2025
9 checks passed
@matthias-springer matthias-springer deleted the users/matthias-springer/failed_leg_new_error branch August 7, 2025 07:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mlir:core MLIR Core Infrastructure mlir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants