Change elements literal parsing to not rely on shaped type being a vector or...
authorGeoffrey Martin-Noble <gcmn@google.com>
Wed, 29 May 2019 20:46:41 +0000 (13:46 -0700)
committerMehdi Amini <joker.eph@gmail.com>
Sun, 2 Jun 2019 03:08:22 +0000 (20:08 -0700)
    This is in preparation for making MemRef a ShapedType. In general, a shaped type should be anything with shape, rank, and element type properties, so use sites shouldn't assume more than that.

    I also pulled the trailing comma parsing out the parseElementsLiteralType (new name) method. It seems weird to have the method parse the type + a trailing comma, even if all call sites currently need that. It's surprising behavior without looking at the implementation.

--

PiperOrigin-RevId: 250558363

mlir/lib/Parser/Parser.cpp
mlir/test/IR/invalid.mlir

index 801ec40..553aa92 100644 (file)
@@ -189,7 +189,7 @@ public:
                                                   IntegerSet &set);
   DenseElementsAttr parseDenseElementsAttr(ShapedType type);
   DenseElementsAttr parseDenseElementsAttrAsTensor(Type eltType);
-  ShapedType parseShapedType();
+  ShapedType parseElementsLiteralType();
 
   // Location Parsing.
 
@@ -1203,10 +1203,13 @@ Attribute Parser::parseAttribute(Type type) {
     if (parseToken(Token::comma, "expected ','"))
       return nullptr;
 
-    auto type = parseShapedType();
+    auto type = parseElementsLiteralType();
     if (!type)
       return nullptr;
 
+    if (parseToken(Token::comma, "expected ',' after elements literal type"))
+      return nullptr;
+
     if (getToken().getKind() != Token::string)
       return (emitError("opaque string should start with '0x'"), nullptr);
     auto val = getToken().getStringValue();
@@ -1227,9 +1230,11 @@ Attribute Parser::parseAttribute(Type type) {
     if (parseToken(Token::less, "expected '<' after 'splat'"))
       return nullptr;
 
-    auto type = parseShapedType();
+    auto type = parseElementsLiteralType();
     if (!type)
       return nullptr;
+    if (parseToken(Token::comma, "expected ',' after elements literal type"))
+      return nullptr;
     switch (getToken().getKind()) {
     case Token::floatliteral:
     case Token::integer:
@@ -1253,10 +1258,13 @@ Attribute Parser::parseAttribute(Type type) {
     if (parseToken(Token::less, "expected '<' after 'dense'"))
       return nullptr;
 
-    auto type = parseShapedType();
+    auto type = parseElementsLiteralType();
     if (!type)
       return nullptr;
 
+    if (parseToken(Token::comma, "expected ',' after elements literal type"))
+      return nullptr;
+
     auto attr = parseDenseElementsAttr(type);
     if (!attr)
       return nullptr;
@@ -1271,10 +1279,13 @@ Attribute Parser::parseAttribute(Type type) {
     if (parseToken(Token::less, "Expected '<' after 'sparse'"))
       return nullptr;
 
-    auto type = parseShapedType();
+    auto type = parseElementsLiteralType();
     if (!type)
       return nullptr;
 
+    if (parseToken(Token::comma, "expected ',' after elements literal type"))
+      return nullptr;
+
     switch (getToken().getKind()) {
     case Token::l_square: {
       /// Parse indices
@@ -1381,26 +1392,25 @@ DenseElementsAttr Parser::parseDenseElementsAttr(ShapedType type) {
 
 /// Shaped type for elements attribute.
 ///
-///   shaped-type ::= vector-type | tensor-type
+///   elements-literal-type ::= vector-type | ranked-tensor-type
 ///
 /// This method also checks the type has static shape.
-ShapedType Parser::parseShapedType() {
-  auto elementType = parseType();
-  if (!elementType)
+ShapedType Parser::parseElementsLiteralType() {
+  auto type = parseType();
+  if (!type)
     return nullptr;
 
-  auto type = elementType.dyn_cast<ShapedType>();
-  if (!type) {
-    return (emitError("elements literal must be a shaped type"), nullptr);
+  if (!type.isa<RankedTensorType>() && !type.isa<VectorType>()) {
+    return (
+        emitError("elements literal must be a ranked tensor or vector type"),
+        nullptr);
   }
 
-  if (parseToken(Token::comma, "expected ','"))
-    return nullptr;
+  auto sType = type.cast<ShapedType>();
+  if (!sType.hasStaticShape())
+    return (emitError("elements literal type must have static shape"), nullptr);
 
-  if (!type.hasStaticShape()) {
-    return (emitError("shaped literal must have static shape"), nullptr);
-  }
-  return type;
+  return sType;
 }
 
 /// Debug Location.
index bcdeab1..1e7fc3d 100644 (file)
@@ -591,14 +591,14 @@ func@n(){^b(
 
 func @elementsattr_non_tensor_type() -> () {
 ^bb0:
-  "foo"(){bar: dense<i32, [4]>} : () -> () // expected-error {{elements literal must be a shaped type}}
+  "foo"(){bar: dense<i32, [4]>} : () -> () // expected-error {{elements literal must be a ranked tensor or vector type}}
 }
 
 // -----
 
 func @elementsattr_non_ranked() -> () {
 ^bb0:
-  "foo"(){bar: dense<tensor<?xi32>, [4]>} : () -> () // expected-error {{shaped literal must have static shape}}
+  "foo"(){bar: dense<tensor<?xi32>, [4]>} : () -> () // expected-error {{elements literal type must have static shape}}
 }
 
 // -----