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

llvmlistbot at llvm.org llvmlistbot at llvm.org
Mon Feb 24 07:23:18 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir

Author: lorenzo chelini (chelini)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/128523.diff


2 Files Affected:

- (modified) mlir/lib/AsmParser/AttributeParser.cpp (+5) 
- (modified) mlir/test/IR/invalid-builtin-attributes.mlir (+10) 


``````````diff
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..b0a67451a598a 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 different type. 
+func.func @parse_literal() {
+  // expected-error at below {{expected string token, got 23}}
+  %0 = arith.constant dense<[23]> : tensor<1x!unknown<>>
+  return
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/128523


More information about the Mlir-commits mailing list