Skip to content

Commit 19fec00

Browse files
[MLIR] Add note for file-line numbers in tablegen errors for assembly formats
Error messages for the custom assembly format are difficult to understand because there are no line numbers. This happens because the assembly format is parsed as a standalone line, separate from it's parent file, with no useful ___location information. Fixing this properly probably requires quite a bit of invasive plumbing through the SourceMgr, similar to how included files are handled This proposal is a less invasive short term solution. When generating an error message we generate an additional note which at least properly describes the operation definition the error occured in, if not the actual line number of the assemblyFormat definition. A typical message is like: error: type of operand #0, named 'operand', is not buildable and a buildable type cannot be inferred $operand type($result) attr-dict ^ /src/llvm-project/mlir/test/mlir-tblgen/op-format-spec.td:296:1: note: in custom assembly format for this operation def ZCoverageInvalidC : TestFormat_Op<"variable_invalid_c", [{ ^ note: suggest adding a type constraint to the operation or adding a 'type($operand)' directive to the custom assembly format $operand type($result) attr-dict ^ Differential Revision: https://reviews.llvm.org/D77488
1 parent 75f60c6 commit 19fec00

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

mlir/test/mlir-tblgen/op-format-spec.td

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class TestFormat_Op<string name, string fmt, list<OpTrait> traits = []>
1919
//===----------------------------------------------------------------------===//
2020
// attr-dict
2121

22-
// CHECK: error: 'attr-dict' directive not found in custom assembly format
22+
// CHECK: error: 'attr-dict' directive not found
2323
def DirectiveAttrDictInvalidA : TestFormat_Op<"attrdict_invalid_a", [{
2424
}]>;
2525
// CHECK: error: 'attr-dict' directive has already been seen
@@ -300,7 +300,7 @@ def VariableInvalidK : TestFormat_Op<"variable_invalid_k", [{
300300
def ZCoverageInvalidA : TestFormat_Op<"variable_invalid_a", [{
301301
attr-dict
302302
}]>, Arguments<(ins AnyMemRef:$operand)>, Results<(outs AnyMemRef:$result)>;
303-
// CHECK: error: operand #0, named 'operand', not found in custom assembly format
303+
// CHECK: error: operand #0, named 'operand', not found
304304
// CHECK: note: suggest adding a '$operand' directive to the custom assembly format
305305
def ZCoverageInvalidB : TestFormat_Op<"variable_invalid_b", [{
306306
type($result) attr-dict
@@ -320,7 +320,7 @@ def ZCoverageInvalidD : TestFormat_Op<"variable_invalid_d", [{
320320
def ZCoverageInvalidE : TestFormat_Op<"variable_invalid_e", [{
321321
attr-dict
322322
}]>, Results<(outs Variadic<I64>:$result)>;
323-
// CHECK: error: successor #0, named 'successor', not found in custom assembly format
323+
// CHECK: error: successor #0, named 'successor', not found
324324
// CHECK: note: suggest adding a '$successor' directive to the custom assembly format
325325
def ZCoverageInvalidF : TestFormat_Op<"variable_invalid_f", [{
326326
attr-dict

mlir/tools/mlir-tblgen/OpFormatGen.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,7 @@ class Token {
10551055
/// This class implements a simple lexer for operation assembly format strings.
10561056
class FormatLexer {
10571057
public:
1058-
FormatLexer(llvm::SourceMgr &mgr);
1058+
FormatLexer(llvm::SourceMgr &mgr, Operator &op);
10591059

10601060
/// Lex the next token and return it.
10611061
Token lexToken();
@@ -1080,23 +1080,29 @@ class FormatLexer {
10801080
Token lexVariable(const char *tokStart);
10811081

10821082
llvm::SourceMgr &srcMgr;
1083+
Operator &op;
10831084
StringRef curBuffer;
10841085
const char *curPtr;
10851086
};
10861087
} // end anonymous namespace
10871088

1088-
FormatLexer::FormatLexer(llvm::SourceMgr &mgr) : srcMgr(mgr) {
1089+
FormatLexer::FormatLexer(llvm::SourceMgr &mgr, Operator &op)
1090+
: srcMgr(mgr), op(op) {
10891091
curBuffer = srcMgr.getMemoryBuffer(mgr.getMainFileID())->getBuffer();
10901092
curPtr = curBuffer.begin();
10911093
}
10921094

10931095
Token FormatLexer::emitError(llvm::SMLoc loc, const Twine &msg) {
10941096
srcMgr.PrintMessage(loc, llvm::SourceMgr::DK_Error, msg);
1097+
llvm::SrcMgr.PrintMessage(op.getLoc()[0], llvm::SourceMgr::DK_Note,
1098+
"in custom assembly format for this operation");
10951099
return formToken(Token::error, loc.getPointer());
10961100
}
10971101
Token FormatLexer::emitErrorAndNote(llvm::SMLoc loc, const Twine &msg,
10981102
const Twine &note) {
10991103
srcMgr.PrintMessage(loc, llvm::SourceMgr::DK_Error, msg);
1104+
llvm::SrcMgr.PrintMessage(op.getLoc()[0], llvm::SourceMgr::DK_Note,
1105+
"in custom assembly format for this operation");
11001106
srcMgr.PrintMessage(loc, llvm::SourceMgr::DK_Note, note);
11011107
return formToken(Token::error, loc.getPointer());
11021108
}
@@ -1233,7 +1239,7 @@ namespace {
12331239
class FormatParser {
12341240
public:
12351241
FormatParser(llvm::SourceMgr &mgr, OperationFormat &format, Operator &op)
1236-
: lexer(mgr), curToken(lexer.lexToken()), fmt(format), op(op),
1242+
: lexer(mgr, op), curToken(lexer.lexToken()), fmt(format), op(op),
12371243
seenOperandTypes(op.getNumOperands()),
12381244
seenResultTypes(op.getNumResults()) {}
12391245

@@ -1481,8 +1487,7 @@ LogicalResult FormatParser::verifyOperands(
14811487
if (!hasAllOperands && !seenOperands.count(&operand)) {
14821488
return emitErrorAndNote(loc,
14831489
"operand #" + Twine(i) + ", named '" +
1484-
operand.name +
1485-
"', not found in custom assembly format",
1490+
operand.name + "', not found",
14861491
"suggest adding a '$" + operand.name +
14871492
"' directive to the custom assembly format");
14881493
}
@@ -1572,8 +1577,7 @@ LogicalResult FormatParser::verifySuccessors(llvm::SMLoc loc) {
15721577
if (!seenSuccessors.count(&successor)) {
15731578
return emitErrorAndNote(loc,
15741579
"successor #" + Twine(i) + ", named '" +
1575-
successor.name +
1576-
"', not found in custom assembly format",
1580+
successor.name + "', not found",
15771581
"suggest adding a '$" + successor.name +
15781582
"' directive to the custom assembly format");
15791583
}

0 commit comments

Comments
 (0)