//===- AttributeParser.cpp - MLIR Attribute Parser Implementation ---------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This file implements the parser for the MLIR Types. // //===----------------------------------------------------------------------===// #include "Parser.h" #include "AsmParserImpl.h" #include "mlir/AsmParser/AsmParserState.h" #include "mlir/IR/AffineMap.h" #include "mlir/IR/BuiltinAttributes.h" #include "mlir/IR/BuiltinDialect.h" #include "mlir/IR/BuiltinTypes.h" #include "mlir/IR/DialectImplementation.h" #include "mlir/IR/DialectResourceBlobManager.h" #include "mlir/IR/IntegerSet.h" #include "llvm/ADT/StringExtras.h" #include "llvm/Support/Endian.h" #include <optional> usingnamespacemlir; usingnamespacemlir::detail; /// Parse an arbitrary attribute. /// /// attribute-value ::= `unit` /// | bool-literal /// | integer-literal (`:` (index-type | integer-type))? /// | float-literal (`:` float-type)? /// | string-literal (`:` type)? /// | type /// | `[` `:` (integer-type | float-type) tensor-literal `]` /// | `[` (attribute-value (`,` attribute-value)*)? `]` /// | `{` (attribute-entry (`,` attribute-entry)*)? `}` /// | symbol-ref-id (`::` symbol-ref-id)* /// | `dense` `<` tensor-literal `>` `:` /// (tensor-type | vector-type) /// | `sparse` `<` attribute-value `,` attribute-value `>` /// `:` (tensor-type | vector-type) /// | `strided` `<` `[` comma-separated-int-or-question `]` /// (`,` `offset` `:` integer-literal)? `>` /// | distinct-attribute /// | extended-attribute /// Attribute Parser::parseAttribute(Type type) { … } /// Parse an optional attribute with the provided type. OptionalParseResult Parser::parseOptionalAttribute(Attribute &attribute, Type type) { … } OptionalParseResult Parser::parseOptionalAttribute(ArrayAttr &attribute, Type type) { … } OptionalParseResult Parser::parseOptionalAttribute(StringAttr &attribute, Type type) { … } OptionalParseResult Parser::parseOptionalAttribute(SymbolRefAttr &result, Type type) { … } /// Attribute dictionary. /// /// attribute-dict ::= `{` `}` /// | `{` attribute-entry (`,` attribute-entry)* `}` /// attribute-entry ::= (bare-id | string-literal) `=` attribute-value /// ParseResult Parser::parseAttributeDict(NamedAttrList &attributes) { … } /// Parse a float attribute. Attribute Parser::parseFloatAttr(Type type, bool isNegative) { … } /// Construct an APint from a parsed value, a known attribute type and /// sign. static std::optional<APInt> buildAttributeAPInt(Type type, bool isNegative, StringRef spelling) { … } /// Parse a decimal or a hexadecimal literal, which can be either an integer /// or a float attribute. Attribute Parser::parseDecOrHexAttr(Type type, bool isNegative) { … } //===----------------------------------------------------------------------===// // TensorLiteralParser //===----------------------------------------------------------------------===// /// Parse elements values stored within a hex string. On success, the values are /// stored into 'result'. static ParseResult parseElementAttrHexValues(Parser &parser, Token tok, std::string &result) { … } namespace { /// This class implements a parser for TensorLiterals. A tensor literal is /// either a single element (e.g, 5) or a multi-dimensional list of elements /// (e.g., [[5, 5]]). class TensorLiteralParser { … }; } // namespace /// Parse the elements of a tensor literal. If 'allowHex' is true, the parser /// may also parse a tensor literal that is store as a hex string. ParseResult TensorLiteralParser::parse(bool allowHex) { … } /// Build a dense attribute instance with the parsed elements and the given /// shaped type. DenseElementsAttr TensorLiteralParser::getAttr(SMLoc loc, ShapedType type) { … } /// Build a Dense Integer attribute for the given type. ParseResult TensorLiteralParser::getIntAttrElements(SMLoc loc, Type eltTy, std::vector<APInt> &intValues) { … } /// Build a Dense Float attribute for the given type. ParseResult TensorLiteralParser::getFloatAttrElements(SMLoc loc, FloatType eltTy, std::vector<APFloat> &floatValues) { … } /// Build a Dense String attribute for the given type. DenseElementsAttr TensorLiteralParser::getStringAttr(SMLoc loc, ShapedType type, Type eltTy) { … } /// Build a Dense attribute with hex data for the given type. DenseElementsAttr TensorLiteralParser::getHexAttr(SMLoc loc, ShapedType type) { … } ParseResult TensorLiteralParser::parseElement() { … } /// Parse a list of either lists or elements, returning the dimensions of the /// parsed sub-tensors in dims. For example: /// parseList([1, 2, 3]) -> Success, [3] /// parseList([[1, 2], [3, 4]]) -> Success, [2, 2] /// parseList([[1, 2], 3]) -> Failure /// parseList([[1, [2, 3]], [4, [5]]]) -> Failure ParseResult TensorLiteralParser::parseList(SmallVectorImpl<int64_t> &dims) { … } //===----------------------------------------------------------------------===// // DenseArrayAttr Parser //===----------------------------------------------------------------------===// namespace { /// A generic dense array element parser. It parsers integer and floating point /// elements. class DenseArrayElementParser { … }; } // namespace void DenseArrayElementParser::append(const APInt &data) { … } ParseResult DenseArrayElementParser::parseIntegerElement(Parser &p) { … } ParseResult DenseArrayElementParser::parseFloatElement(Parser &p) { … } /// Parse a dense array attribute. Attribute Parser::parseDenseArrayAttr(Type attrType) { … } /// Parse a dense elements attribute. Attribute Parser::parseDenseElementsAttr(Type attrType) { … } Attribute Parser::parseDenseResourceElementsAttr(Type attrType) { … } /// Shaped type for elements attribute. /// /// elements-literal-type ::= vector-type | ranked-tensor-type /// /// This method also checks the type has static shape. ShapedType Parser::parseElementsLiteralType(Type type) { … } /// Parse a sparse elements attribute. Attribute Parser::parseSparseElementsAttr(Type attrType) { … } Attribute Parser::parseStridedLayoutAttr() { … } /// Parse a distinct attribute. /// /// distinct-attribute ::= `distinct` /// `[` integer-literal `]<` attribute-value `>` /// Attribute Parser::parseDistinctAttr(Type type) { … }