Skip to content

Commit a88167a

Browse files
authored
[MLIR] Improve error handling when parsing dense attributes (#128523)
Avoid triggering assertions when we expect to parse a string but encounter a different type. Instead, handle the mismatch gracefully by emitting a parser error.
1 parent 7262a1e commit a88167a

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

mlir/lib/AsmParser/AttributeParser.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,11 @@ DenseElementsAttr TensorLiteralParser::getStringAttr(SMLoc loc, ShapedType type,
680680
stringRefValues.reserve(storage.size());
681681

682682
for (auto val : storage) {
683+
if (!val.second.is(Token::string)) {
684+
p.emitError(loc) << "expected string token, got "
685+
<< val.second.getSpelling();
686+
return nullptr;
687+
}
683688
stringValues.push_back(val.second.getStringValue());
684689
stringRefValues.emplace_back(stringValues.back());
685690
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,3 +626,13 @@ func.func @print_error_on_correct_line() {
626626
sparse <> : i32
627627
return
628628
}
629+
630+
// -----
631+
632+
// Prevent assertions when parsing a dense attribute expected to be a string
633+
// but encountering a different type.
634+
func.func @expect_to_parse_literal() {
635+
// expected-error@below {{expected string token, got 23}}
636+
%0 = arith.constant dense<[23]> : tensor<1x!unknown<>>
637+
return
638+
}

0 commit comments

Comments
 (0)