Skip to content

[MLIR] Imporve location tracking for parseElementsLiteralType #127992

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 1 commit into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
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
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
}