Skip to content

Commit 0181af2

Browse files
authored
[MLIR] Imporve location tracking for parseElementsLiteralType (#127992)
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 ^ ```
1 parent ad0c7da commit 0181af2

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
lines changed

mlir/lib/AsmParser/AttributeParser.cpp

+10-12
Original file line numberDiff line numberDiff line change
@@ -951,14 +951,10 @@ Attribute Parser::parseDenseElementsAttr(Type attrType) {
951951
return nullptr;
952952
}
953953

954-
// If the type is specified `parseElementsLiteralType` will not parse a type.
955-
// Use the attribute location as the location for error reporting in that
956-
// case.
957-
auto loc = attrType ? attribLoc : getToken().getLoc();
958-
auto type = parseElementsLiteralType(attrType);
954+
auto type = parseElementsLiteralType(attribLoc, attrType);
959955
if (!type)
960956
return nullptr;
961-
return literalParser.getAttr(loc, type);
957+
return literalParser.getAttr(attribLoc, type);
962958
}
963959

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

10111007
auto sType = dyn_cast<ShapedType>(type);
10121008
if (!sType) {
1013-
emitError("elements literal must be a shaped type");
1009+
emitError(loc, "elements literal must be a shaped type");
10141010
return nullptr;
10151011
}
10161012

1017-
if (!sType.hasStaticShape())
1018-
return (emitError("elements literal type must have static shape"), nullptr);
1013+
if (!sType.hasStaticShape()) {
1014+
emitError(loc, "elements literal type must have static shape");
1015+
return nullptr;
1016+
}
10191017

10201018
return sType;
10211019
}
@@ -1032,7 +1030,7 @@ Attribute Parser::parseSparseElementsAttr(Type attrType) {
10321030
// of the type.
10331031
Type indiceEltType = builder.getIntegerType(64);
10341032
if (consumeIf(Token::greater)) {
1035-
ShapedType type = parseElementsLiteralType(attrType);
1033+
ShapedType type = parseElementsLiteralType(loc, attrType);
10361034
if (!type)
10371035
return nullptr;
10381036

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

1068-
auto type = parseElementsLiteralType(attrType);
1066+
auto type = parseElementsLiteralType(loc, attrType);
10691067
if (!type)
10701068
return nullptr;
10711069

mlir/lib/AsmParser/Parser.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ class Parser {
288288

289289
/// Parse a dense elements attribute.
290290
Attribute parseDenseElementsAttr(Type attrType);
291-
ShapedType parseElementsLiteralType(Type type);
291+
ShapedType parseElementsLiteralType(SMLoc loc, Type type);
292292

293293
/// Parse a dense resource elements attribute.
294294
Attribute parseDenseResourceElementsAttr(Type attrType);

mlir/test/IR/invalid-builtin-attributes.mlir

+35
Original file line numberDiff line numberDiff line change
@@ -591,3 +591,38 @@ func.func @duplicate_dictionary_attr_key() {
591591
#attr1 = distinct[0]<43 : i32>
592592

593593
// -----
594+
595+
// Make sure the error is not printed on the return.
596+
func.func @print_error_on_correct_line() {
597+
%0 = arith.constant
598+
// expected-error@below {{elements literal must be a shaped type}}
599+
dense<[3]> : i32
600+
return
601+
}
602+
603+
// -----
604+
605+
// Make sure the error is not printed on the return.
606+
func.func @print_error_on_correct_line() {
607+
%0 = arith.constant
608+
// expected-error@below {{elements literal must be a shaped type}}
609+
sparse<
610+
[
611+
[0, 1, 2, 3],
612+
[1, 1, 2, 3],
613+
[1, 2, 2, 3],
614+
[1, 2, 3, 4]
615+
],
616+
[1, 1, 1, 1] > : i32
617+
return
618+
}
619+
620+
// -----
621+
622+
// Make sure the error is not printed on the return.
623+
func.func @print_error_on_correct_line() {
624+
%0 = arith.constant
625+
// expected-error@below {{elements literal must be a shaped type}}
626+
sparse <> : i32
627+
return
628+
}

0 commit comments

Comments
 (0)