[Mlir-commits] [mlir] a88167a - [MLIR] Improve error handling when parsing dense attributes (#128523)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Mon Feb 24 07:40:12 PST 2025


Author: lorenzo chelini
Date: 2025-02-24T16:40:08+01:00
New Revision: a88167a60d0b5529b2a5ab185680f25c3c983ec3

URL: https://github.com/llvm/llvm-project/commit/a88167a60d0b5529b2a5ab185680f25c3c983ec3
DIFF: https://github.com/llvm/llvm-project/commit/a88167a60d0b5529b2a5ab185680f25c3c983ec3.diff

LOG: [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.

Added: 
    

Modified: 
    mlir/lib/AsmParser/AttributeParser.cpp
    mlir/test/IR/invalid-builtin-attributes.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/lib/AsmParser/AttributeParser.cpp b/mlir/lib/AsmParser/AttributeParser.cpp
index 32c68e093a0b0..2013d3623711b 100644
--- a/mlir/lib/AsmParser/AttributeParser.cpp
+++ b/mlir/lib/AsmParser/AttributeParser.cpp
@@ -680,6 +680,11 @@ DenseElementsAttr TensorLiteralParser::getStringAttr(SMLoc loc, ShapedType type,
   stringRefValues.reserve(storage.size());
 
   for (auto val : storage) {
+    if (!val.second.is(Token::string)) {
+      p.emitError(loc) << "expected string token, got "
+                       << val.second.getSpelling();
+      return nullptr;
+    }
     stringValues.push_back(val.second.getStringValue());
     stringRefValues.emplace_back(stringValues.back());
   }

diff  --git a/mlir/test/IR/invalid-builtin-attributes.mlir b/mlir/test/IR/invalid-builtin-attributes.mlir
index 64201950b2306..10988be91d84a 100644
--- a/mlir/test/IR/invalid-builtin-attributes.mlir
+++ b/mlir/test/IR/invalid-builtin-attributes.mlir
@@ -626,3 +626,13 @@ func.func @print_error_on_correct_line() {
     sparse <> : i32
   return
 }
+
+// -----
+
+// Prevent assertions when parsing a dense attribute expected to be a string 
+// but encountering a 
diff erent type. 
+func.func @expect_to_parse_literal() {
+  // expected-error at below {{expected string token, got 23}}
+  %0 = arith.constant dense<[23]> : tensor<1x!unknown<>>
+  return
+}


        


More information about the Mlir-commits mailing list