[mlir][Transforms] More detailed error message when new IR cannot be legalized (#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.

---------

Co-authored-by: Jeremy Kun <jkun@google.com>
This commit is contained in:
Matthias Springer 2025-08-07 09:14:24 +02:00 committed by GitHub
parent 1110e2ff9f
commit a3e0685529
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2356,6 +2356,31 @@ 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) {
auto newOpNames = llvm::map_range(
newOps, [](Operation *op) { return op->getName().getStringRef(); });
auto modifiedOpNames = llvm::map_range(
modifiedOps, [](Operation *op) { return op->getName().getStringRef(); });
StringRef detachedBlockStr = "(detached block)";
auto insertedBlockNames = 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: {" +
llvm::join(newOpNames, ", ") + "}, " + "modified ops: {" +
llvm::join(modifiedOpNames, ", ") + "}, " + "inserted block into ops: {" +
llvm::join(insertedBlockNames, ", ") + "}");
}
LogicalResult
OperationLegalizer::legalizeWithPattern(Operation *op,
ConversionPatternRewriter &rewriter) {
@ -2444,8 +2469,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)