Skip to content

Commit

Permalink
[MLIR] Imporve location tracking for parseElementsLiteralType (#127992
Browse files Browse the repository at this point in the history
)

This commit improves line location tracking in case of error reporting
to the user in `parseElementsLiteralType`. There are two cases: the type
is already parsed [1] or not yet parsed [2]. With these changes we print
the error at the attribute's location in both cases to ensure
consistency.

Case 1)
```mlir
memref<i32> = dense<[3]>
              ^
```

Case 2)
```mlir
dense<[3]> : memref<i32>
^
```

Note that today for a simple:

```mlir
func.func @main() {
  %0 = arith.constant dense<[3]> : i32
  return
}
```

we print the error after the constant:

```
./bin/c.mlir:3:3: error: elements literal must be a shaped type
  return
  ^
```
  • Loading branch information
chelini authored Feb 21, 2025
1 parent ad0c7da commit 0181af2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
22 changes: 10 additions & 12 deletions mlir/lib/AsmParser/AttributeParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -951,14 +951,10 @@ Attribute Parser::parseDenseElementsAttr(Type attrType) {
return nullptr;
}

// If the type is specified `parseElementsLiteralType` will not parse a type.
// Use the attribute location as the location for error reporting in that
// case.
auto loc = attrType ? attribLoc : getToken().getLoc();
auto type = parseElementsLiteralType(attrType);
auto type = parseElementsLiteralType(attribLoc, attrType);
if (!type)
return nullptr;
return literalParser.getAttr(loc, type);
return literalParser.getAttr(attribLoc, type);
}

Attribute Parser::parseDenseResourceElementsAttr(Type attrType) {
Expand Down Expand Up @@ -999,7 +995,7 @@ Attribute Parser::parseDenseResourceElementsAttr(Type attrType) {
/// elements-literal-type ::= vector-type | ranked-tensor-type
///
/// This method also checks the type has static shape.
ShapedType Parser::parseElementsLiteralType(Type type) {
ShapedType Parser::parseElementsLiteralType(SMLoc loc, Type type) {
// If the user didn't provide a type, parse the colon type for the literal.
if (!type) {
if (parseToken(Token::colon, "expected ':'"))
Expand All @@ -1010,12 +1006,14 @@ ShapedType Parser::parseElementsLiteralType(Type type) {

auto sType = dyn_cast<ShapedType>(type);
if (!sType) {
emitError("elements literal must be a shaped type");
emitError(loc, "elements literal must be a shaped type");
return nullptr;
}

if (!sType.hasStaticShape())
return (emitError("elements literal type must have static shape"), nullptr);
if (!sType.hasStaticShape()) {
emitError(loc, "elements literal type must have static shape");
return nullptr;
}

return sType;
}
Expand All @@ -1032,7 +1030,7 @@ Attribute Parser::parseSparseElementsAttr(Type attrType) {
// of the type.
Type indiceEltType = builder.getIntegerType(64);
if (consumeIf(Token::greater)) {
ShapedType type = parseElementsLiteralType(attrType);
ShapedType type = parseElementsLiteralType(loc, attrType);
if (!type)
return nullptr;

Expand Down Expand Up @@ -1065,7 +1063,7 @@ Attribute Parser::parseSparseElementsAttr(Type attrType) {
if (parseToken(Token::greater, "expected '>'"))
return nullptr;

auto type = parseElementsLiteralType(attrType);
auto type = parseElementsLiteralType(loc, attrType);
if (!type)
return nullptr;

Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/AsmParser/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ class Parser {

/// Parse a dense elements attribute.
Attribute parseDenseElementsAttr(Type attrType);
ShapedType parseElementsLiteralType(Type type);
ShapedType parseElementsLiteralType(SMLoc loc, Type type);

/// Parse a dense resource elements attribute.
Attribute parseDenseResourceElementsAttr(Type attrType);
Expand Down
35 changes: 35 additions & 0 deletions mlir/test/IR/invalid-builtin-attributes.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -591,3 +591,38 @@ func.func @duplicate_dictionary_attr_key() {
#attr1 = distinct[0]<43 : i32>

// -----

// Make sure the error is not printed on the return.
func.func @print_error_on_correct_line() {
%0 = arith.constant
// expected-error@below {{elements literal must be a shaped type}}
dense<[3]> : i32
return
}

// -----

// Make sure the error is not printed on the return.
func.func @print_error_on_correct_line() {
%0 = arith.constant
// expected-error@below {{elements literal must be a shaped type}}
sparse<
[
[0, 1, 2, 3],
[1, 1, 2, 3],
[1, 2, 2, 3],
[1, 2, 3, 4]
],
[1, 1, 1, 1] > : i32
return
}

// -----

// Make sure the error is not printed on the return.
func.func @print_error_on_correct_line() {
%0 = arith.constant
// expected-error@below {{elements literal must be a shaped type}}
sparse <> : i32
return
}

0 comments on commit 0181af2

Please sign in to comment.