[Mlir-commits] [mlir] 44b22f6 - [MLIR] Expose optional attribute parsing functions

Andrew Young llvmlistbot at llvm.org
Mon Oct 18 11:45:54 PDT 2021


Author: Andrew Young
Date: 2021-10-18T11:45:43-07:00
New Revision: 44b22f6f40f15ff24545c5fb0d612b3d86591955

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

LOG: [MLIR] Expose optional attribute parsing functions

The functionality already exists in AsmParser to parse optional ArrayAttrs and
StringAttrs, but only if they are added to a NamedAttrList.  This moves the
code to parse an optional attribute and add it to an list into a common
template, and exposes the simpler functionality of just parsing the optional
attributes.

Reviewed By: rriddle

Differential Revision: https://reviews.llvm.org/D111918

Added: 
    

Modified: 
    mlir/include/mlir/IR/OpImplementation.h
    mlir/lib/Parser/AsmParserImpl.h

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/IR/OpImplementation.h b/mlir/include/mlir/IR/OpImplementation.h
index 2cbe08815baa3..db1f7a3c071d2 100644
--- a/mlir/include/mlir/IR/OpImplementation.h
+++ b/mlir/include/mlir/IR/OpImplementation.h
@@ -639,28 +639,6 @@ class AsmParser {
     return parseAttribute(result, Type(), attrName, attrs);
   }
 
-  /// Parse an optional attribute.
-  virtual OptionalParseResult parseOptionalAttribute(Attribute &result,
-                                                     Type type,
-                                                     StringRef attrName,
-                                                     NamedAttrList &attrs) = 0;
-  template <typename AttrT>
-  OptionalParseResult parseOptionalAttribute(AttrT &result, StringRef attrName,
-                                             NamedAttrList &attrs) {
-    return parseOptionalAttribute(result, Type(), attrName, attrs);
-  }
-
-  /// Specialized variants of `parseOptionalAttribute` that remove potential
-  /// ambiguities in syntax.
-  virtual OptionalParseResult parseOptionalAttribute(ArrayAttr &result,
-                                                     Type type,
-                                                     StringRef attrName,
-                                                     NamedAttrList &attrs) = 0;
-  virtual OptionalParseResult parseOptionalAttribute(StringAttr &result,
-                                                     Type type,
-                                                     StringRef attrName,
-                                                     NamedAttrList &attrs) = 0;
-
   /// Parse an arbitrary attribute of a given type and return it in result. This
   /// also adds the attribute to the specified attribute list with the specified
   /// name.
@@ -683,6 +661,40 @@ class AsmParser {
     return success();
   }
 
+  /// Parse an arbitrary optional attribute of a given type and return it in
+  /// result.
+  virtual OptionalParseResult parseOptionalAttribute(Attribute &result,
+                                                     Type type = {}) = 0;
+
+  /// Parse an optional array attribute and return it in result.
+  virtual OptionalParseResult parseOptionalAttribute(ArrayAttr &result,
+                                                     Type type = {}) = 0;
+
+  /// Parse an optional string attribute and return it in result.
+  virtual OptionalParseResult parseOptionalAttribute(StringAttr &result,
+                                                     Type type = {}) = 0;
+
+  /// Parse an optional attribute of a specific type and add it to the list with
+  /// the specified name.
+  template <typename AttrType>
+  OptionalParseResult parseOptionalAttribute(AttrType &result,
+                                             StringRef attrName,
+                                             NamedAttrList &attrs) {
+    return parseOptionalAttribute(result, Type(), attrName, attrs);
+  }
+
+  /// Parse an optional attribute of a specific type and add it to the list with
+  /// the specified name.
+  template <typename AttrType>
+  OptionalParseResult parseOptionalAttribute(AttrType &result, Type type,
+                                             StringRef attrName,
+                                             NamedAttrList &attrs) {
+    OptionalParseResult parseResult = parseOptionalAttribute(result, type);
+    if (parseResult.hasValue() && succeeded(*parseResult))
+      attrs.append(attrName, result);
+    return parseResult;
+  }
+
   /// Parse a named dictionary into 'result' if it is present.
   virtual ParseResult parseOptionalAttrDict(NamedAttrList &result) = 0;
 

diff  --git a/mlir/lib/Parser/AsmParserImpl.h b/mlir/lib/Parser/AsmParserImpl.h
index 2d35225717bba..70039c2736a42 100644
--- a/mlir/lib/Parser/AsmParserImpl.h
+++ b/mlir/lib/Parser/AsmParserImpl.h
@@ -343,31 +343,17 @@ class AsmParserImpl : public BaseT {
     return success(static_cast<bool>(result));
   }
 
-  /// Parse an optional attribute.
-  template <typename AttrT>
-  OptionalParseResult
-  parseOptionalAttributeAndAddToList(AttrT &result, Type type,
-                                     StringRef attrName, NamedAttrList &attrs) {
-    OptionalParseResult parseResult =
-        parser.parseOptionalAttribute(result, type);
-    if (parseResult.hasValue() && succeeded(*parseResult))
-      attrs.push_back(parser.builder.getNamedAttr(attrName, result));
-    return parseResult;
-  }
-  OptionalParseResult parseOptionalAttribute(Attribute &result, Type type,
-                                             StringRef attrName,
-                                             NamedAttrList &attrs) override {
-    return parseOptionalAttributeAndAddToList(result, type, attrName, attrs);
-  }
-  OptionalParseResult parseOptionalAttribute(ArrayAttr &result, Type type,
-                                             StringRef attrName,
-                                             NamedAttrList &attrs) override {
-    return parseOptionalAttributeAndAddToList(result, type, attrName, attrs);
-  }
-  OptionalParseResult parseOptionalAttribute(StringAttr &result, Type type,
-                                             StringRef attrName,
-                                             NamedAttrList &attrs) override {
-    return parseOptionalAttributeAndAddToList(result, type, attrName, attrs);
+  OptionalParseResult parseOptionalAttribute(Attribute &result,
+                                             Type type) override {
+    return parser.parseOptionalAttribute(result, type);
+  }
+  OptionalParseResult parseOptionalAttribute(ArrayAttr &result,
+                                             Type type) override {
+    return parser.parseOptionalAttribute(result, type);
+  }
+  OptionalParseResult parseOptionalAttribute(StringAttr &result,
+                                             Type type) override {
+    return parser.parseOptionalAttribute(result, type);
   }
 
   /// Parse a named dictionary into 'result' if it is present.


        


More information about the Mlir-commits mailing list