Commit 6b476e24 authored by River Riddle's avatar River Riddle
Browse files

[mlir] Add support for parsing optional Attribute values.

This adds a `parseOptionalAttribute` method to the OpAsmParser that allows for parsing optional attributes, in a similar fashion to how optional types are parsed. This also enables the use of attribute values as the first element of an assembly format optional group.

Differential Revision: https://reviews.llvm.org/D83712
parent aef60af3
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -713,7 +713,8 @@ of the assembly format can be marked as `optional` based on the presence of this
information. An optional group is defined by wrapping a set of elements within
`()` followed by a `?` and has the following requirements:

*   The first element of the group must either be a literal or an operand.
*   The first element of the group must either be a literal, attribute, or an
    operand.
    -   This is because the first element must be optionally parsable.
*   Exactly one argument variable within the group must be marked as the anchor
    of the group.
+11 −0
Original line number Diff line number Diff line
@@ -384,6 +384,17 @@ public:
                                     StringRef attrName,
                                     NamedAttrList &attrs) = 0;

  /// Parse an optional attribute.
  virtual OptionalParseResult parseOptionalAttribute(Attribute &result,
                                                     Type type,
                                                     StringRef attrName,
                                                     NamedAttrList &attrs) = 0;
  OptionalParseResult parseOptionalAttribute(Attribute &result,
                                             StringRef attrName,
                                             NamedAttrList &attrs) {
    return parseOptionalAttribute(result, Type(), attrName, attrs);
  }

  /// Parse an attribute of a specific kind and type.
  template <typename AttrType>
  ParseResult parseAttribute(AttrType &result, Type type, StringRef attrName,
+34 −0
Original line number Diff line number Diff line
@@ -187,6 +187,40 @@ Attribute Parser::parseAttribute(Type type) {
  }
}

/// Parse an optional attribute with the provided type.
OptionalParseResult Parser::parseOptionalAttribute(Attribute &attribute,
                                                   Type type) {
  switch (getToken().getKind()) {
  case Token::at_identifier:
  case Token::floatliteral:
  case Token::integer:
  case Token::hash_identifier:
  case Token::kw_affine_map:
  case Token::kw_affine_set:
  case Token::kw_dense:
  case Token::kw_false:
  case Token::kw_loc:
  case Token::kw_opaque:
  case Token::kw_sparse:
  case Token::kw_true:
  case Token::kw_unit:
  case Token::l_brace:
  case Token::l_square:
  case Token::minus:
  case Token::string:
    attribute = parseAttribute(type);
    return success(attribute != nullptr);

  default:
    // Parse an optional type attribute.
    Type type;
    OptionalParseResult result = parseOptionalType(type);
    if (result.hasValue() && succeeded(*result))
      attribute = TypeAttr::get(type);
    return result;
  }
}

/// Attribute dictionary.
///
///   attribute-dict ::= `{` `}`
+11 −0
Original line number Diff line number Diff line
@@ -1011,6 +1011,17 @@ public:
    return success();
  }

  /// Parse an optional attribute.
  OptionalParseResult parseOptionalAttribute(Attribute &result, Type type,
                                             StringRef attrName,
                                             NamedAttrList &attrs) override {
    OptionalParseResult parseResult =
        parser.parseOptionalAttribute(result, type);
    if (parseResult.hasValue() && succeeded(*parseResult))
      attrs.push_back(parser.builder.getNamedAttr(attrName, result));
    return parseResult;
  }

  /// Parse a named dictionary into 'result' if it is present.
  ParseResult parseOptionalAttrDict(NamedAttrList &result) override {
    if (parser.getToken().isNot(Token::l_brace))
+4 −0
Original line number Diff line number Diff line
@@ -184,6 +184,10 @@ public:
  /// Parse an arbitrary attribute with an optional type.
  Attribute parseAttribute(Type type = {});

  /// Parse an optional attribute with the provided type.
  OptionalParseResult parseOptionalAttribute(Attribute &attribute,
                                             Type type = {});

  /// Parse an attribute dictionary.
  ParseResult parseAttributeDict(NamedAttrList &attributes);

Loading