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 1 commit
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
35 changes: 33 additions & 2 deletions mlir/lib/Transforms/Utils/DialectConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down