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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions mlir/lib/Transforms/Utils/DialectConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2302,6 +2302,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) {
Expand Down Expand Up @@ -2389,8 +2414,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)
Expand Down