| 1 | //===- AttributeParser.cpp - MLIR Attribute Parser Implementation ---------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the parser for the MLIR Types. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "Parser.h" |
| 14 | |
| 15 | #include "mlir/AsmParser/AsmParserState.h" |
| 16 | #include "mlir/IR/AffineMap.h" |
| 17 | #include "mlir/IR/BuiltinAttributes.h" |
| 18 | #include "mlir/IR/BuiltinDialect.h" |
| 19 | #include "mlir/IR/BuiltinTypeInterfaces.h" |
| 20 | #include "mlir/IR/BuiltinTypes.h" |
| 21 | #include "mlir/IR/DialectResourceBlobManager.h" |
| 22 | #include "mlir/IR/IntegerSet.h" |
| 23 | #include <optional> |
| 24 | |
| 25 | using namespace mlir; |
| 26 | using namespace mlir::detail; |
| 27 | |
| 28 | /// Parse an arbitrary attribute. |
| 29 | /// |
| 30 | /// attribute-value ::= `unit` |
| 31 | /// | bool-literal |
| 32 | /// | integer-literal (`:` (index-type | integer-type))? |
| 33 | /// | float-literal (`:` float-type)? |
| 34 | /// | string-literal (`:` type)? |
| 35 | /// | type |
| 36 | /// | `[` `:` (integer-type | float-type) tensor-literal `]` |
| 37 | /// | `[` (attribute-value (`,` attribute-value)*)? `]` |
| 38 | /// | `{` (attribute-entry (`,` attribute-entry)*)? `}` |
| 39 | /// | symbol-ref-id (`::` symbol-ref-id)* |
| 40 | /// | `dense` `<` tensor-literal `>` `:` |
| 41 | /// (tensor-type | vector-type) |
| 42 | /// | `sparse` `<` attribute-value `,` attribute-value `>` |
| 43 | /// `:` (tensor-type | vector-type) |
| 44 | /// | `strided` `<` `[` comma-separated-int-or-question `]` |
| 45 | /// (`,` `offset` `:` integer-literal)? `>` |
| 46 | /// | distinct-attribute |
| 47 | /// | extended-attribute |
| 48 | /// |
| 49 | Attribute Parser::parseAttribute(Type type) { |
| 50 | switch (getToken().getKind()) { |
| 51 | // Parse an AffineMap or IntegerSet attribute. |
| 52 | case Token::kw_affine_map: { |
| 53 | consumeToken(kind: Token::kw_affine_map); |
| 54 | |
| 55 | AffineMap map; |
| 56 | if (parseToken(expectedToken: Token::less, message: "expected '<' in affine map" ) || |
| 57 | parseAffineMapReference(map) || |
| 58 | parseToken(expectedToken: Token::greater, message: "expected '>' in affine map" )) |
| 59 | return Attribute(); |
| 60 | return AffineMapAttr::get(value: map); |
| 61 | } |
| 62 | case Token::kw_affine_set: { |
| 63 | consumeToken(kind: Token::kw_affine_set); |
| 64 | |
| 65 | IntegerSet set; |
| 66 | if (parseToken(expectedToken: Token::less, message: "expected '<' in integer set" ) || |
| 67 | parseIntegerSetReference(set) || |
| 68 | parseToken(expectedToken: Token::greater, message: "expected '>' in integer set" )) |
| 69 | return Attribute(); |
| 70 | return IntegerSetAttr::get(value: set); |
| 71 | } |
| 72 | |
| 73 | // Parse an array attribute. |
| 74 | case Token::l_square: { |
| 75 | consumeToken(kind: Token::l_square); |
| 76 | SmallVector<Attribute, 4> elements; |
| 77 | auto parseElt = [&]() -> ParseResult { |
| 78 | elements.push_back(Elt: parseAttribute()); |
| 79 | return elements.back() ? success() : failure(); |
| 80 | }; |
| 81 | |
| 82 | if (parseCommaSeparatedListUntil(rightToken: Token::r_square, parseElement: parseElt)) |
| 83 | return nullptr; |
| 84 | return builder.getArrayAttr(value: elements); |
| 85 | } |
| 86 | |
| 87 | // Parse a boolean attribute. |
| 88 | case Token::kw_false: |
| 89 | consumeToken(kind: Token::kw_false); |
| 90 | return builder.getBoolAttr(value: false); |
| 91 | case Token::kw_true: |
| 92 | consumeToken(kind: Token::kw_true); |
| 93 | return builder.getBoolAttr(value: true); |
| 94 | |
| 95 | // Parse a dense elements attribute. |
| 96 | case Token::kw_dense: |
| 97 | return parseDenseElementsAttr(attrType: type); |
| 98 | |
| 99 | // Parse a dense resource elements attribute. |
| 100 | case Token::kw_dense_resource: |
| 101 | return parseDenseResourceElementsAttr(attrType: type); |
| 102 | |
| 103 | // Parse a dense array attribute. |
| 104 | case Token::kw_array: |
| 105 | return parseDenseArrayAttr(type); |
| 106 | |
| 107 | // Parse a dictionary attribute. |
| 108 | case Token::l_brace: { |
| 109 | NamedAttrList elements; |
| 110 | if (parseAttributeDict(attributes&: elements)) |
| 111 | return nullptr; |
| 112 | return elements.getDictionary(context: getContext()); |
| 113 | } |
| 114 | |
| 115 | // Parse an extended attribute, i.e. alias or dialect attribute. |
| 116 | case Token::hash_identifier: |
| 117 | return parseExtendedAttr(type); |
| 118 | |
| 119 | // Parse floating point and integer attributes. |
| 120 | case Token::floatliteral: |
| 121 | return parseFloatAttr(type, /*isNegative=*/false); |
| 122 | case Token::integer: |
| 123 | return parseDecOrHexAttr(type, /*isNegative=*/false); |
| 124 | case Token::minus: { |
| 125 | consumeToken(kind: Token::minus); |
| 126 | if (getToken().is(k: Token::integer)) |
| 127 | return parseDecOrHexAttr(type, /*isNegative=*/true); |
| 128 | if (getToken().is(k: Token::floatliteral)) |
| 129 | return parseFloatAttr(type, /*isNegative=*/true); |
| 130 | |
| 131 | return (emitWrongTokenError( |
| 132 | message: "expected constant integer or floating point value" ), |
| 133 | nullptr); |
| 134 | } |
| 135 | |
| 136 | // Parse a location attribute. |
| 137 | case Token::kw_loc: { |
| 138 | consumeToken(kind: Token::kw_loc); |
| 139 | |
| 140 | LocationAttr locAttr; |
| 141 | if (parseToken(expectedToken: Token::l_paren, message: "expected '(' in inline location" ) || |
| 142 | parseLocationInstance(loc&: locAttr) || |
| 143 | parseToken(expectedToken: Token::r_paren, message: "expected ')' in inline location" )) |
| 144 | return Attribute(); |
| 145 | return locAttr; |
| 146 | } |
| 147 | |
| 148 | // Parse a sparse elements attribute. |
| 149 | case Token::kw_sparse: |
| 150 | return parseSparseElementsAttr(attrType: type); |
| 151 | |
| 152 | // Parse a strided layout attribute. |
| 153 | case Token::kw_strided: |
| 154 | return parseStridedLayoutAttr(); |
| 155 | |
| 156 | // Parse a distinct attribute. |
| 157 | case Token::kw_distinct: |
| 158 | return parseDistinctAttr(type); |
| 159 | |
| 160 | // Parse a string attribute. |
| 161 | case Token::string: { |
| 162 | auto val = getToken().getStringValue(); |
| 163 | consumeToken(kind: Token::string); |
| 164 | // Parse the optional trailing colon type if one wasn't explicitly provided. |
| 165 | if (!type && consumeIf(kind: Token::colon) && !(type = parseType())) |
| 166 | return Attribute(); |
| 167 | |
| 168 | return type ? StringAttr::get(bytes: val, type) |
| 169 | : StringAttr::get(context: getContext(), bytes: val); |
| 170 | } |
| 171 | |
| 172 | // Parse a symbol reference attribute. |
| 173 | case Token::at_identifier: { |
| 174 | // When populating the parser state, this is a list of locations for all of |
| 175 | // the nested references. |
| 176 | SmallVector<SMRange> referenceLocations; |
| 177 | if (state.asmState) |
| 178 | referenceLocations.push_back(Elt: getToken().getLocRange()); |
| 179 | |
| 180 | // Parse the top-level reference. |
| 181 | std::string nameStr = getToken().getSymbolReference(); |
| 182 | consumeToken(kind: Token::at_identifier); |
| 183 | |
| 184 | // Parse any nested references. |
| 185 | std::vector<FlatSymbolRefAttr> nestedRefs; |
| 186 | while (getToken().is(k: Token::colon)) { |
| 187 | // Check for the '::' prefix. |
| 188 | const char *curPointer = getToken().getLoc().getPointer(); |
| 189 | consumeToken(kind: Token::colon); |
| 190 | if (!consumeIf(kind: Token::colon)) { |
| 191 | if (getToken().isNot(k1: Token::eof, k2: Token::error)) { |
| 192 | state.lex.resetPointer(newPointer: curPointer); |
| 193 | consumeToken(); |
| 194 | } |
| 195 | break; |
| 196 | } |
| 197 | // Parse the reference itself. |
| 198 | auto curLoc = getToken().getLoc(); |
| 199 | if (getToken().isNot(k: Token::at_identifier)) { |
| 200 | emitError(loc: curLoc, message: "expected nested symbol reference identifier" ); |
| 201 | return Attribute(); |
| 202 | } |
| 203 | |
| 204 | // If we are populating the assembly state, add the location for this |
| 205 | // reference. |
| 206 | if (state.asmState) |
| 207 | referenceLocations.push_back(Elt: getToken().getLocRange()); |
| 208 | |
| 209 | std::string nameStr = getToken().getSymbolReference(); |
| 210 | consumeToken(kind: Token::at_identifier); |
| 211 | nestedRefs.push_back(x: SymbolRefAttr::get(ctx: getContext(), value: nameStr)); |
| 212 | } |
| 213 | SymbolRefAttr symbolRefAttr = |
| 214 | SymbolRefAttr::get(ctx: getContext(), value: nameStr, nestedRefs); |
| 215 | |
| 216 | // If we are populating the assembly state, record this symbol reference. |
| 217 | if (state.asmState) |
| 218 | state.asmState->addUses(refAttr: symbolRefAttr, refLocations: referenceLocations); |
| 219 | return symbolRefAttr; |
| 220 | } |
| 221 | |
| 222 | // Parse a 'unit' attribute. |
| 223 | case Token::kw_unit: |
| 224 | consumeToken(kind: Token::kw_unit); |
| 225 | return builder.getUnitAttr(); |
| 226 | |
| 227 | // Handle completion of an attribute. |
| 228 | case Token::code_complete: |
| 229 | if (getToken().isCodeCompletionFor(kind: Token::hash_identifier)) |
| 230 | return parseExtendedAttr(type); |
| 231 | return codeCompleteAttribute(); |
| 232 | |
| 233 | default: |
| 234 | // Parse a type attribute. We parse `Optional` here to allow for providing a |
| 235 | // better error message. |
| 236 | Type type; |
| 237 | OptionalParseResult result = parseOptionalType(type); |
| 238 | if (!result.has_value()) |
| 239 | return emitWrongTokenError(message: "expected attribute value" ), Attribute(); |
| 240 | return failed(Result: *result) ? Attribute() : TypeAttr::get(type); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | /// Parse an optional attribute with the provided type. |
| 245 | OptionalParseResult Parser::parseOptionalAttribute(Attribute &attribute, |
| 246 | Type type) { |
| 247 | switch (getToken().getKind()) { |
| 248 | case Token::at_identifier: |
| 249 | case Token::floatliteral: |
| 250 | case Token::integer: |
| 251 | case Token::hash_identifier: |
| 252 | case Token::kw_affine_map: |
| 253 | case Token::kw_affine_set: |
| 254 | case Token::kw_dense: |
| 255 | case Token::kw_dense_resource: |
| 256 | case Token::kw_false: |
| 257 | case Token::kw_loc: |
| 258 | case Token::kw_sparse: |
| 259 | case Token::kw_true: |
| 260 | case Token::kw_unit: |
| 261 | case Token::l_brace: |
| 262 | case Token::l_square: |
| 263 | case Token::minus: |
| 264 | case Token::string: |
| 265 | attribute = parseAttribute(type); |
| 266 | return success(IsSuccess: attribute != nullptr); |
| 267 | |
| 268 | default: |
| 269 | // Parse an optional type attribute. |
| 270 | Type type; |
| 271 | OptionalParseResult result = parseOptionalType(type); |
| 272 | if (result.has_value() && succeeded(Result: *result)) |
| 273 | attribute = TypeAttr::get(type); |
| 274 | return result; |
| 275 | } |
| 276 | } |
| 277 | OptionalParseResult Parser::parseOptionalAttribute(ArrayAttr &attribute, |
| 278 | Type type) { |
| 279 | return parseOptionalAttributeWithToken(kind: Token::l_square, attr&: attribute, type); |
| 280 | } |
| 281 | OptionalParseResult Parser::parseOptionalAttribute(StringAttr &attribute, |
| 282 | Type type) { |
| 283 | return parseOptionalAttributeWithToken(kind: Token::string, attr&: attribute, type); |
| 284 | } |
| 285 | OptionalParseResult Parser::parseOptionalAttribute(SymbolRefAttr &result, |
| 286 | Type type) { |
| 287 | return parseOptionalAttributeWithToken(kind: Token::at_identifier, attr&: result, type); |
| 288 | } |
| 289 | |
| 290 | /// Attribute dictionary. |
| 291 | /// |
| 292 | /// attribute-dict ::= `{` `}` |
| 293 | /// | `{` attribute-entry (`,` attribute-entry)* `}` |
| 294 | /// attribute-entry ::= (bare-id | string-literal) `=` attribute-value |
| 295 | /// |
| 296 | ParseResult Parser::parseAttributeDict(NamedAttrList &attributes) { |
| 297 | llvm::SmallDenseSet<StringAttr> seenKeys; |
| 298 | auto parseElt = [&]() -> ParseResult { |
| 299 | // The name of an attribute can either be a bare identifier, or a string. |
| 300 | std::optional<StringAttr> nameId; |
| 301 | if (getToken().is(k: Token::string)) |
| 302 | nameId = builder.getStringAttr(bytes: getToken().getStringValue()); |
| 303 | else if (getToken().isAny(k1: Token::bare_identifier, k2: Token::inttype) || |
| 304 | getToken().isKeyword()) |
| 305 | nameId = builder.getStringAttr(bytes: getTokenSpelling()); |
| 306 | else |
| 307 | return emitWrongTokenError(message: "expected attribute name" ); |
| 308 | |
| 309 | if (nameId->empty()) |
| 310 | return emitError(message: "expected valid attribute name" ); |
| 311 | |
| 312 | if (!seenKeys.insert(V: *nameId).second) |
| 313 | return emitError(message: "duplicate key '" ) |
| 314 | << nameId->getValue() << "' in dictionary attribute" ; |
| 315 | consumeToken(); |
| 316 | |
| 317 | // Lazy load a dialect in the context if there is a possible namespace. |
| 318 | auto splitName = nameId->strref().split(Separator: '.'); |
| 319 | if (!splitName.second.empty()) |
| 320 | getContext()->getOrLoadDialect(name: splitName.first); |
| 321 | |
| 322 | // Try to parse the '=' for the attribute value. |
| 323 | if (!consumeIf(kind: Token::equal)) { |
| 324 | // If there is no '=', we treat this as a unit attribute. |
| 325 | attributes.push_back(newAttribute: {*nameId, builder.getUnitAttr()}); |
| 326 | return success(); |
| 327 | } |
| 328 | |
| 329 | auto attr = parseAttribute(); |
| 330 | if (!attr) |
| 331 | return failure(); |
| 332 | attributes.push_back(newAttribute: {*nameId, attr}); |
| 333 | return success(); |
| 334 | }; |
| 335 | |
| 336 | return parseCommaSeparatedList(delimiter: Delimiter::Braces, parseElementFn: parseElt, |
| 337 | contextMessage: " in attribute dictionary" ); |
| 338 | } |
| 339 | |
| 340 | /// Parse a float attribute. |
| 341 | Attribute Parser::parseFloatAttr(Type type, bool isNegative) { |
| 342 | // Defer parsing the literal until the type, and with it the float semantics, |
| 343 | // is known. |
| 344 | Token literalTok = getToken(); |
| 345 | consumeToken(kind: Token::floatliteral); |
| 346 | if (!type) { |
| 347 | // Default to F64 when no type is specified. |
| 348 | if (!consumeIf(kind: Token::colon)) |
| 349 | type = builder.getF64Type(); |
| 350 | else if (!(type = parseType())) |
| 351 | return nullptr; |
| 352 | } |
| 353 | if (!isa<FloatType>(Val: type)) |
| 354 | return (emitError(message: "floating point value not valid for specified type" ), |
| 355 | nullptr); |
| 356 | // Note: parseFloatFromLiteral rejects a negative literal for a type with no |
| 357 | // signed representation, such as f8E8M0FNU. |
| 358 | std::optional<APFloat> result; |
| 359 | if (failed(Result: parseFloatFromLiteral(result, tok: literalTok, isNegative, |
| 360 | semantics: cast<FloatType>(Val&: type).getFloatSemantics()))) |
| 361 | return nullptr; |
| 362 | return FloatAttr::get(type, value: *result); |
| 363 | } |
| 364 | |
| 365 | /// Construct an APint from a parsed value, a known attribute type and |
| 366 | /// sign. |
| 367 | static std::optional<APInt> buildAttributeAPInt(Type type, bool isNegative, |
| 368 | StringRef spelling) { |
| 369 | // Parse the integer value into an APInt that is big enough to hold the value. |
| 370 | APInt result; |
| 371 | bool isHex = spelling.size() > 1 && spelling[1] == 'x'; |
| 372 | if (spelling.getAsInteger(Radix: isHex ? 0 : 10, Result&: result)) |
| 373 | return std::nullopt; |
| 374 | |
| 375 | // Extend or truncate the bitwidth to the right size. |
| 376 | unsigned width = type.isIndex() ? IndexType::kInternalStorageBitWidth |
| 377 | : type.getIntOrFloatBitWidth(); |
| 378 | |
| 379 | if (width > result.getBitWidth()) { |
| 380 | result = result.zext(width); |
| 381 | } else if (width < result.getBitWidth()) { |
| 382 | // The parser can return an unnecessarily wide result with leading zeros. |
| 383 | // This isn't a problem, but truncating off bits is bad. |
| 384 | if (result.countl_zero() < result.getBitWidth() - width) |
| 385 | return std::nullopt; |
| 386 | |
| 387 | result = result.trunc(width); |
| 388 | } |
| 389 | |
| 390 | if (width == 0) { |
| 391 | // 0 bit integers cannot be negative and manipulation of their sign bit will |
| 392 | // assert, so short-cut validation here. |
| 393 | if (isNegative) |
| 394 | return std::nullopt; |
| 395 | } else if (isNegative) { |
| 396 | // The value is negative, we have an overflow if the sign bit is not set |
| 397 | // in the negated apInt. |
| 398 | result.negate(); |
| 399 | if (!result.isSignBitSet()) |
| 400 | return std::nullopt; |
| 401 | } else if ((type.isSignedInteger() || type.isIndex()) && |
| 402 | result.isSignBitSet()) { |
| 403 | // The value is a positive signed integer or index, |
| 404 | // we have an overflow if the sign bit is set. |
| 405 | return std::nullopt; |
| 406 | } |
| 407 | |
| 408 | return result; |
| 409 | } |
| 410 | |
| 411 | /// Parse a decimal or a hexadecimal literal, which can be either an integer |
| 412 | /// or a float attribute. |
| 413 | Attribute Parser::parseDecOrHexAttr(Type type, bool isNegative) { |
| 414 | Token tok = getToken(); |
| 415 | StringRef spelling = tok.getSpelling(); |
| 416 | SMLoc loc = tok.getLoc(); |
| 417 | |
| 418 | consumeToken(kind: Token::integer); |
| 419 | if (!type) { |
| 420 | // Default to i64 if not type is specified. |
| 421 | if (!consumeIf(kind: Token::colon)) |
| 422 | type = builder.getIntegerType(width: 64); |
| 423 | else if (!(type = parseType())) |
| 424 | return nullptr; |
| 425 | } |
| 426 | |
| 427 | if (auto floatType = dyn_cast<FloatType>(Val&: type)) { |
| 428 | std::optional<APFloat> result; |
| 429 | if (failed(Result: parseFloatFromIntegerLiteral(result, tok, isNegative, |
| 430 | semantics: floatType.getFloatSemantics()))) |
| 431 | return Attribute(); |
| 432 | return FloatAttr::get(type: floatType, value: *result); |
| 433 | } |
| 434 | |
| 435 | if (!isa<IntegerType, IndexType>(Val: type)) |
| 436 | return emitError(loc, message: "integer literal not valid for specified type" ), |
| 437 | nullptr; |
| 438 | |
| 439 | if (isNegative && type.isUnsignedInteger()) { |
| 440 | emitError(loc, |
| 441 | message: "negative integer literal not valid for unsigned integer type" ); |
| 442 | return nullptr; |
| 443 | } |
| 444 | |
| 445 | std::optional<APInt> apInt = buildAttributeAPInt(type, isNegative, spelling); |
| 446 | if (!apInt) |
| 447 | return emitError(loc, message: "integer constant out of range for attribute" ), |
| 448 | nullptr; |
| 449 | return builder.getIntegerAttr(type, value: *apInt); |
| 450 | } |
| 451 | |
| 452 | //===----------------------------------------------------------------------===// |
| 453 | // TensorLiteralParser |
| 454 | //===----------------------------------------------------------------------===// |
| 455 | |
| 456 | /// Parse elements values stored within a hex string. On success, the values are |
| 457 | /// stored into 'result'. |
| 458 | static ParseResult parseElementAttrHexValues(Parser &parser, Token tok, |
| 459 | std::string &result) { |
| 460 | if (std::optional<std::string> value = tok.getHexStringValue()) { |
| 461 | result = std::move(*value); |
| 462 | return success(); |
| 463 | } |
| 464 | return parser.emitError( |
| 465 | loc: tok.getLoc(), message: "expected string containing hex digits starting with `0x`" ); |
| 466 | } |
| 467 | |
| 468 | namespace { |
| 469 | /// This class implements a parser for TensorLiterals. A tensor literal is |
| 470 | /// either a single element (e.g, 5) or a multi-dimensional list of elements |
| 471 | /// (e.g., [[5, 5]]). |
| 472 | class TensorLiteralParser { |
| 473 | public: |
| 474 | TensorLiteralParser(Parser &p) : p(p) {} |
| 475 | |
| 476 | /// Parse the elements of a tensor literal. If 'allowHex' is true, the parser |
| 477 | /// may also parse a tensor literal that is store as a hex string. |
| 478 | ParseResult parse(bool allowHex); |
| 479 | |
| 480 | /// Build a dense attribute instance with the parsed elements and the given |
| 481 | /// shaped type. |
| 482 | DenseElementsAttr getAttr(SMLoc loc, ShapedType type); |
| 483 | |
| 484 | ArrayRef<int64_t> getShape() const { return shape; } |
| 485 | |
| 486 | private: |
| 487 | /// Get the parsed elements for an integer attribute. |
| 488 | ParseResult getIntAttrElements(SMLoc loc, Type eltTy, |
| 489 | std::vector<APInt> &intValues); |
| 490 | |
| 491 | /// Get the parsed elements for a float attribute. |
| 492 | ParseResult getFloatAttrElements(SMLoc loc, FloatType eltTy, |
| 493 | std::vector<APFloat> &floatValues); |
| 494 | |
| 495 | /// Build a Dense String attribute for the given type. |
| 496 | DenseElementsAttr getStringAttr(SMLoc loc, ShapedType type, Type eltTy); |
| 497 | |
| 498 | /// Build a Dense attribute with hex data for the given type. |
| 499 | DenseElementsAttr getHexAttr(SMLoc loc, ShapedType type); |
| 500 | |
| 501 | /// Parse a single element, returning failure if it isn't a valid element |
| 502 | /// literal. For example: |
| 503 | /// parseElement(1) -> Success, 1 |
| 504 | /// parseElement([1]) -> Failure |
| 505 | ParseResult parseElement(); |
| 506 | |
| 507 | /// Parse a list of either lists or elements, returning the dimensions of the |
| 508 | /// parsed sub-tensors in dims. For example: |
| 509 | /// parseList([1, 2, 3]) -> Success, [3] |
| 510 | /// parseList([[1, 2], [3, 4]]) -> Success, [2, 2] |
| 511 | /// parseList([[1, 2], 3]) -> Failure |
| 512 | /// parseList([[1, [2, 3]], [4, [5]]]) -> Failure |
| 513 | ParseResult parseList(SmallVectorImpl<int64_t> &dims); |
| 514 | |
| 515 | /// Parse a literal that was printed as a hex string. |
| 516 | ParseResult parseHexElements(); |
| 517 | |
| 518 | Parser &p; |
| 519 | |
| 520 | /// The shape inferred from the parsed elements. |
| 521 | SmallVector<int64_t, 4> shape; |
| 522 | |
| 523 | /// Storage used when parsing elements, this is a pair of <is_negated, token>. |
| 524 | std::vector<std::pair<bool, Token>> storage; |
| 525 | |
| 526 | /// Storage used when parsing elements that were stored as hex values. |
| 527 | std::optional<Token> hexStorage; |
| 528 | }; |
| 529 | } // namespace |
| 530 | |
| 531 | /// Parse the elements of a tensor literal. If 'allowHex' is true, the parser |
| 532 | /// may also parse a tensor literal that is store as a hex string. |
| 533 | ParseResult TensorLiteralParser::parse(bool allowHex) { |
| 534 | // If hex is allowed, check for a string literal. |
| 535 | if (allowHex && p.getToken().is(k: Token::string)) { |
| 536 | hexStorage = p.getToken(); |
| 537 | p.consumeToken(kind: Token::string); |
| 538 | return success(); |
| 539 | } |
| 540 | // Otherwise, parse a list or an individual element. |
| 541 | if (p.getToken().is(k: Token::l_square)) |
| 542 | return parseList(dims&: shape); |
| 543 | return parseElement(); |
| 544 | } |
| 545 | |
| 546 | /// Build a dense attribute instance with the parsed elements and the given |
| 547 | /// shaped type. |
| 548 | DenseElementsAttr TensorLiteralParser::getAttr(SMLoc loc, ShapedType type) { |
| 549 | Type eltType = type.getElementType(); |
| 550 | |
| 551 | // Check to see if we parse the literal from a hex string. |
| 552 | if (hexStorage && |
| 553 | (eltType.isIntOrIndexOrFloat() || isa<ComplexType, MXI8Type>(Val: eltType))) |
| 554 | return getHexAttr(loc, type); |
| 555 | |
| 556 | // Check that the parsed storage size has the same number of elements to the |
| 557 | // type, or is a known splat. |
| 558 | if (!shape.empty() && getShape() != type.getShape()) { |
| 559 | p.emitError(loc) << "inferred shape of elements literal ([" << getShape() |
| 560 | << "]) does not match type ([" << type.getShape() << "])" ; |
| 561 | return nullptr; |
| 562 | } |
| 563 | |
| 564 | // Handle the case where no elements were parsed. |
| 565 | if (!hexStorage && storage.empty() && type.getNumElements()) { |
| 566 | p.emitError(loc) << "parsed zero elements, but type (" << type |
| 567 | << ") expected at least 1" ; |
| 568 | return nullptr; |
| 569 | } |
| 570 | |
| 571 | // Handle complex types in the specific element type cases below. |
| 572 | bool isComplex = false; |
| 573 | if (ComplexType complexTy = dyn_cast<ComplexType>(Val&: eltType)) { |
| 574 | eltType = complexTy.getElementType(); |
| 575 | isComplex = true; |
| 576 | // Complex types have N*2 elements or complex splat. |
| 577 | // Empty shape may mean a splat or empty literal, only validate splats. |
| 578 | bool isSplat = shape.empty() && type.getNumElements() != 0; |
| 579 | if (isSplat && storage.size() != 2) { |
| 580 | p.emitError(loc) << "parsed " << storage.size() << " elements, but type (" |
| 581 | << complexTy << ") expected 2 elements" ; |
| 582 | return nullptr; |
| 583 | } |
| 584 | if (!shape.empty() && |
| 585 | storage.size() != static_cast<size_t>(type.getNumElements()) * 2) { |
| 586 | p.emitError(loc) << "parsed " << storage.size() << " elements, but type (" |
| 587 | << type << ") expected " << type.getNumElements() * 2 |
| 588 | << " elements" ; |
| 589 | return nullptr; |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | // Handle integer and index types. |
| 594 | if (eltType.isIntOrIndex()) { |
| 595 | std::vector<APInt> intValues; |
| 596 | if (failed(Result: getIntAttrElements(loc, eltTy: eltType, intValues))) |
| 597 | return nullptr; |
| 598 | if (isComplex) { |
| 599 | // If this is a complex, treat the parsed values as complex values. |
| 600 | auto complexData = llvm::ArrayRef( |
| 601 | reinterpret_cast<mlir::Complex<APInt> *>(intValues.data()), |
| 602 | intValues.size() / 2); |
| 603 | return DenseElementsAttr::get(type, values: complexData); |
| 604 | } |
| 605 | return DenseElementsAttr::get(type, values: intValues); |
| 606 | } |
| 607 | // MXI8 stores encoded values as signless i8 attributes. |
| 608 | if (isa<MXI8Type>(Val: eltType)) { |
| 609 | std::vector<APInt> intValues; |
| 610 | Type storageType = IntegerType::get(context: p.getContext(), width: 8); |
| 611 | if (failed(Result: getIntAttrElements(loc, eltTy: storageType, intValues))) |
| 612 | return nullptr; |
| 613 | SmallVector<Attribute> attrs; |
| 614 | attrs.reserve(N: intValues.size()); |
| 615 | for (const APInt &value : intValues) |
| 616 | attrs.push_back(Elt: IntegerAttr::get(type: storageType, value)); |
| 617 | return DenseElementsAttr::get(type, values: attrs); |
| 618 | } |
| 619 | // Handle floating point types. |
| 620 | if (FloatType floatTy = dyn_cast<FloatType>(Val&: eltType)) { |
| 621 | std::vector<APFloat> floatValues; |
| 622 | if (failed(Result: getFloatAttrElements(loc, eltTy: floatTy, floatValues))) |
| 623 | return nullptr; |
| 624 | if (isComplex) { |
| 625 | // If this is a complex, treat the parsed values as complex values. |
| 626 | auto complexData = llvm::ArrayRef( |
| 627 | reinterpret_cast<mlir::Complex<APFloat> *>(floatValues.data()), |
| 628 | floatValues.size() / 2); |
| 629 | return DenseElementsAttr::get(type, values: complexData); |
| 630 | } |
| 631 | return DenseElementsAttr::get(type, values: floatValues); |
| 632 | } |
| 633 | |
| 634 | // Other types are assumed to be string representations. |
| 635 | return getStringAttr(loc, type, eltTy: type.getElementType()); |
| 636 | } |
| 637 | |
| 638 | /// Build a Dense Integer attribute for the given type. |
| 639 | ParseResult |
| 640 | TensorLiteralParser::getIntAttrElements(SMLoc loc, Type eltTy, |
| 641 | std::vector<APInt> &intValues) { |
| 642 | intValues.reserve(n: storage.size()); |
| 643 | bool isUintType = eltTy.isUnsignedInteger(); |
| 644 | for (const auto &signAndToken : storage) { |
| 645 | bool isNegative = signAndToken.first; |
| 646 | const Token &token = signAndToken.second; |
| 647 | auto tokenLoc = token.getLoc(); |
| 648 | |
| 649 | if (isNegative && isUintType) { |
| 650 | return p.emitError(loc: tokenLoc) |
| 651 | << "expected unsigned integer elements, but parsed negative value" ; |
| 652 | } |
| 653 | |
| 654 | // Check to see if floating point values were parsed. |
| 655 | if (token.is(k: Token::floatliteral)) { |
| 656 | return p.emitError(loc: tokenLoc) |
| 657 | << "expected integer elements, but parsed floating-point" ; |
| 658 | } |
| 659 | |
| 660 | if (token.is(k: Token::string)) { |
| 661 | return p.emitError(loc: tokenLoc) |
| 662 | << "expected integer elements, but parsed string" ; |
| 663 | } |
| 664 | assert(token.isAny(Token::integer, Token::kw_true, Token::kw_false) && |
| 665 | "unexpected token type" ); |
| 666 | if (token.isAny(k1: Token::kw_true, k2: Token::kw_false)) { |
| 667 | if (!eltTy.isInteger(width: 1)) { |
| 668 | return p.emitError(loc: tokenLoc) |
| 669 | << "expected i1 type for 'true' or 'false' values" ; |
| 670 | } |
| 671 | APInt apInt(1, token.is(k: Token::kw_true), /*isSigned=*/false); |
| 672 | intValues.push_back(x: apInt); |
| 673 | continue; |
| 674 | } |
| 675 | |
| 676 | // Create APInt values for each element with the correct bitwidth. |
| 677 | std::optional<APInt> apInt = |
| 678 | buildAttributeAPInt(type: eltTy, isNegative, spelling: token.getSpelling()); |
| 679 | if (!apInt) |
| 680 | return p.emitError(loc: tokenLoc, message: "integer constant out of range for type" ); |
| 681 | intValues.push_back(x: *apInt); |
| 682 | } |
| 683 | return success(); |
| 684 | } |
| 685 | |
| 686 | /// Build a Dense Float attribute for the given type. |
| 687 | ParseResult |
| 688 | TensorLiteralParser::getFloatAttrElements(SMLoc loc, FloatType eltTy, |
| 689 | std::vector<APFloat> &floatValues) { |
| 690 | floatValues.reserve(n: storage.size()); |
| 691 | for (const auto &signAndToken : storage) { |
| 692 | bool isNegative = signAndToken.first; |
| 693 | const Token &token = signAndToken.second; |
| 694 | std::optional<APFloat> result; |
| 695 | if (failed(Result: p.parseFloatFromLiteral(result, tok: token, isNegative, |
| 696 | semantics: eltTy.getFloatSemantics()))) |
| 697 | return failure(); |
| 698 | floatValues.push_back(x: *result); |
| 699 | } |
| 700 | return success(); |
| 701 | } |
| 702 | |
| 703 | /// Build a Dense String attribute for the given type. |
| 704 | DenseElementsAttr TensorLiteralParser::getStringAttr(SMLoc loc, ShapedType type, |
| 705 | Type eltTy) { |
| 706 | if (hexStorage.has_value()) { |
| 707 | auto stringValue = hexStorage->getStringValue(); |
| 708 | return DenseStringElementsAttr::get(type, values: {stringValue}); |
| 709 | } |
| 710 | |
| 711 | std::vector<std::string> stringValues; |
| 712 | std::vector<StringRef> stringRefValues; |
| 713 | stringValues.reserve(n: storage.size()); |
| 714 | stringRefValues.reserve(n: storage.size()); |
| 715 | |
| 716 | for (auto val : storage) { |
| 717 | if (!val.second.is(k: Token::string)) { |
| 718 | p.emitError(loc) << "expected string token, got " |
| 719 | << val.second.getSpelling(); |
| 720 | return nullptr; |
| 721 | } |
| 722 | stringValues.push_back(x: val.second.getStringValue()); |
| 723 | stringRefValues.emplace_back(args&: stringValues.back()); |
| 724 | } |
| 725 | |
| 726 | return DenseStringElementsAttr::get(type, values: stringRefValues); |
| 727 | } |
| 728 | |
| 729 | /// Build a Dense attribute with hex data for the given type. |
| 730 | DenseElementsAttr TensorLiteralParser::getHexAttr(SMLoc loc, ShapedType type) { |
| 731 | Type elementType = type.getElementType(); |
| 732 | if (!elementType.isIntOrIndexOrFloat() && |
| 733 | !isa<ComplexType, MXI8Type>(Val: elementType)) { |
| 734 | p.emitError(loc) << "expected floating-point, integer, complex, or mxi8 " |
| 735 | "element type, got " |
| 736 | << elementType; |
| 737 | return nullptr; |
| 738 | } |
| 739 | |
| 740 | std::string data; |
| 741 | if (parseElementAttrHexValues(parser&: p, tok: *hexStorage, result&: data)) |
| 742 | return nullptr; |
| 743 | |
| 744 | ArrayRef<char> rawData(data); |
| 745 | if (!DenseElementsAttr::isValidRawBuffer(type, rawBuffer: rawData)) { |
| 746 | p.emitError(loc) << "elements hex data size is invalid for provided type: " |
| 747 | << type; |
| 748 | return nullptr; |
| 749 | } |
| 750 | |
| 751 | // MXI8 elements occupy one byte, so no endian conversion is needed. |
| 752 | if (isa<MXI8Type>(Val: elementType)) |
| 753 | return DenseElementsAttr::getFromRawBuffer(type, rawBuffer: rawData); |
| 754 | |
| 755 | if (llvm::endianness::native == llvm::endianness::big) { |
| 756 | // Convert endianess in big-endian(BE) machines. `rawData` is |
| 757 | // little-endian(LE) because HEX in raw data of dense element attribute |
| 758 | // is always LE format. It is converted into BE here to be used in BE |
| 759 | // machines. |
| 760 | SmallVector<char, 64> outDataVec(rawData.size()); |
| 761 | MutableArrayRef<char> convRawData(outDataVec); |
| 762 | DenseTypedElementsAttr::convertEndianOfArrayRefForBEmachine( |
| 763 | inRawData: rawData, outRawData: convRawData, type); |
| 764 | return DenseElementsAttr::getFromRawBuffer(type, rawBuffer: convRawData); |
| 765 | } |
| 766 | |
| 767 | return DenseElementsAttr::getFromRawBuffer(type, rawBuffer: rawData); |
| 768 | } |
| 769 | |
| 770 | ParseResult TensorLiteralParser::parseElement() { |
| 771 | switch (p.getToken().getKind()) { |
| 772 | // Parse a boolean element. |
| 773 | case Token::kw_true: |
| 774 | case Token::kw_false: |
| 775 | case Token::floatliteral: |
| 776 | case Token::integer: |
| 777 | storage.emplace_back(/*isNegative=*/args: false, args: p.getToken()); |
| 778 | p.consumeToken(); |
| 779 | break; |
| 780 | |
| 781 | // Parse a signed integer or a negative floating-point element. |
| 782 | case Token::minus: |
| 783 | p.consumeToken(kind: Token::minus); |
| 784 | if (!p.getToken().isAny(k1: Token::floatliteral, k2: Token::integer)) |
| 785 | return p.emitError(message: "expected integer or floating point literal" ); |
| 786 | storage.emplace_back(/*isNegative=*/args: true, args: p.getToken()); |
| 787 | p.consumeToken(); |
| 788 | break; |
| 789 | |
| 790 | case Token::string: |
| 791 | storage.emplace_back(/*isNegative=*/args: false, args: p.getToken()); |
| 792 | p.consumeToken(); |
| 793 | break; |
| 794 | |
| 795 | // Parse a complex element of the form '(' element ',' element ')'. |
| 796 | case Token::l_paren: |
| 797 | p.consumeToken(kind: Token::l_paren); |
| 798 | if (parseElement() || |
| 799 | p.parseToken(expectedToken: Token::comma, message: "expected ',' between complex elements" ) || |
| 800 | parseElement() || |
| 801 | p.parseToken(expectedToken: Token::r_paren, message: "expected ')' after complex elements" )) |
| 802 | return failure(); |
| 803 | break; |
| 804 | |
| 805 | default: |
| 806 | return p.emitError(message: "expected element literal of primitive type" ); |
| 807 | } |
| 808 | |
| 809 | return success(); |
| 810 | } |
| 811 | |
| 812 | /// Parse a list of either lists or elements, returning the dimensions of the |
| 813 | /// parsed sub-tensors in dims. For example: |
| 814 | /// parseList([1, 2, 3]) -> Success, [3] |
| 815 | /// parseList([[1, 2], [3, 4]]) -> Success, [2, 2] |
| 816 | /// parseList([[1, 2], 3]) -> Failure |
| 817 | /// parseList([[1, [2, 3]], [4, [5]]]) -> Failure |
| 818 | ParseResult TensorLiteralParser::parseList(SmallVectorImpl<int64_t> &dims) { |
| 819 | auto checkDims = [&](const SmallVectorImpl<int64_t> &prevDims, |
| 820 | const SmallVectorImpl<int64_t> &newDims) -> ParseResult { |
| 821 | if (prevDims == newDims) |
| 822 | return success(); |
| 823 | return p.emitError(message: "tensor literal is invalid; ranks are not consistent " |
| 824 | "between elements" ); |
| 825 | }; |
| 826 | |
| 827 | bool first = true; |
| 828 | SmallVector<int64_t, 4> newDims; |
| 829 | unsigned size = 0; |
| 830 | auto parseOneElement = [&]() -> ParseResult { |
| 831 | SmallVector<int64_t, 4> thisDims; |
| 832 | if (p.getToken().getKind() == Token::l_square) { |
| 833 | if (parseList(dims&: thisDims)) |
| 834 | return failure(); |
| 835 | } else if (parseElement()) { |
| 836 | return failure(); |
| 837 | } |
| 838 | ++size; |
| 839 | if (!first) |
| 840 | return checkDims(newDims, thisDims); |
| 841 | newDims = thisDims; |
| 842 | first = false; |
| 843 | return success(); |
| 844 | }; |
| 845 | if (p.parseCommaSeparatedList(delimiter: Parser::Delimiter::Square, parseElementFn: parseOneElement)) |
| 846 | return failure(); |
| 847 | |
| 848 | // Return the sublists' dimensions with 'size' prepended. |
| 849 | dims.clear(); |
| 850 | dims.push_back(Elt: size); |
| 851 | dims.append(in_start: newDims.begin(), in_end: newDims.end()); |
| 852 | return success(); |
| 853 | } |
| 854 | |
| 855 | //===----------------------------------------------------------------------===// |
| 856 | // DenseArrayAttr Parser |
| 857 | //===----------------------------------------------------------------------===// |
| 858 | |
| 859 | namespace { |
| 860 | /// A generic dense array element parser. It parsers integer and floating point |
| 861 | /// elements. |
| 862 | class DenseArrayElementParser { |
| 863 | public: |
| 864 | explicit DenseArrayElementParser(Type type) : type(type) {} |
| 865 | |
| 866 | /// Parse an integer element. |
| 867 | ParseResult parseIntegerElement(Parser &p); |
| 868 | |
| 869 | /// Parse a floating point element. |
| 870 | ParseResult parseFloatElement(Parser &p); |
| 871 | |
| 872 | /// Convert the current contents to a dense array. |
| 873 | DenseArrayAttr getAttr() { return DenseArrayAttr::get(elementType: type, size, rawData); } |
| 874 | |
| 875 | private: |
| 876 | /// Append the raw data of an APInt to the result. |
| 877 | void append(const APInt &data); |
| 878 | |
| 879 | /// The array element type. |
| 880 | Type type; |
| 881 | /// The resultant byte array representing the contents of the array. |
| 882 | std::vector<char> rawData; |
| 883 | /// The number of elements in the array. |
| 884 | int64_t size = 0; |
| 885 | }; |
| 886 | } // namespace |
| 887 | |
| 888 | void DenseArrayElementParser::append(const APInt &data) { |
| 889 | if (data.getBitWidth()) { |
| 890 | assert(data.getBitWidth() % 8 == 0); |
| 891 | unsigned byteSize = data.getBitWidth() / 8; |
| 892 | size_t offset = rawData.size(); |
| 893 | rawData.insert(position: rawData.end(), n: byteSize, x: 0); |
| 894 | llvm::StoreIntToMemory( |
| 895 | IntVal: data, Dst: reinterpret_cast<uint8_t *>(rawData.data() + offset), StoreBytes: byteSize); |
| 896 | } |
| 897 | ++size; |
| 898 | } |
| 899 | |
| 900 | ParseResult DenseArrayElementParser::parseIntegerElement(Parser &p) { |
| 901 | bool isNegative = p.consumeIf(kind: Token::minus); |
| 902 | |
| 903 | // Parse an integer literal as an APInt. |
| 904 | std::optional<APInt> value; |
| 905 | StringRef spelling = p.getToken().getSpelling(); |
| 906 | if (p.getToken().isAny(k1: Token::kw_true, k2: Token::kw_false)) { |
| 907 | if (!type.isInteger(width: 1)) |
| 908 | return p.emitError(message: "expected i1 type for 'true' or 'false' values" ); |
| 909 | value = APInt(/*numBits=*/8, p.getToken().is(k: Token::kw_true), |
| 910 | !type.isUnsignedInteger()); |
| 911 | p.consumeToken(); |
| 912 | } else if (p.consumeIf(kind: Token::integer)) { |
| 913 | if (type.isInteger(width: 1)) |
| 914 | return p.emitError(message: "expected 'true' or 'false' values for i1 type" ); |
| 915 | value = buildAttributeAPInt(type, isNegative, spelling); |
| 916 | if (!value) |
| 917 | return p.emitError(message: "integer constant out of range" ); |
| 918 | } else { |
| 919 | return p.emitError(message: "expected integer literal" ); |
| 920 | } |
| 921 | append(data: *value); |
| 922 | return success(); |
| 923 | } |
| 924 | |
| 925 | ParseResult DenseArrayElementParser::parseFloatElement(Parser &p) { |
| 926 | bool isNegative = p.consumeIf(kind: Token::minus); |
| 927 | Token token = p.getToken(); |
| 928 | std::optional<APFloat> fromIntLit; |
| 929 | if (failed( |
| 930 | Result: p.parseFloatFromLiteral(result&: fromIntLit, tok: token, isNegative, |
| 931 | semantics: cast<FloatType>(Val&: type).getFloatSemantics()))) |
| 932 | return failure(); |
| 933 | p.consumeToken(); |
| 934 | append(data: fromIntLit->bitcastToAPInt()); |
| 935 | return success(); |
| 936 | } |
| 937 | |
| 938 | /// Parse a dense array attribute. |
| 939 | Attribute Parser::parseDenseArrayAttr(Type attrType) { |
| 940 | consumeToken(kind: Token::kw_array); |
| 941 | if (parseToken(expectedToken: Token::less, message: "expected '<' after 'array'" )) |
| 942 | return {}; |
| 943 | |
| 944 | SMLoc typeLoc = getToken().getLoc(); |
| 945 | Type eltType = parseType(); |
| 946 | if (!eltType) { |
| 947 | emitError(loc: typeLoc, message: "expected an integer or floating point type" ); |
| 948 | return {}; |
| 949 | } |
| 950 | |
| 951 | // Only bool or integer and floating point elements divisible by bytes are |
| 952 | // supported. |
| 953 | if (!eltType.isIntOrFloat()) { |
| 954 | emitError(loc: typeLoc, message: "expected integer or float type, got: " ) << eltType; |
| 955 | return {}; |
| 956 | } |
| 957 | if (!eltType.isInteger(width: 1) && eltType.getIntOrFloatBitWidth() % 8 != 0) { |
| 958 | emitError(loc: typeLoc, message: "element type bitwidth must be a multiple of 8" ); |
| 959 | return {}; |
| 960 | } |
| 961 | |
| 962 | // Check for empty list. |
| 963 | if (consumeIf(kind: Token::greater)) |
| 964 | return DenseArrayAttr::get(elementType: eltType, size: 0, rawData: {}); |
| 965 | |
| 966 | if (parseToken(expectedToken: Token::colon, message: "expected ':' after dense array type" )) |
| 967 | return {}; |
| 968 | |
| 969 | DenseArrayElementParser eltParser(eltType); |
| 970 | if (isa<IntegerType>(Val: eltType)) { |
| 971 | if (parseCommaSeparatedList( |
| 972 | parseElementFn: [&] { return eltParser.parseIntegerElement(p&: *this); })) |
| 973 | return {}; |
| 974 | } else { |
| 975 | if (parseCommaSeparatedList( |
| 976 | parseElementFn: [&] { return eltParser.parseFloatElement(p&: *this); })) |
| 977 | return {}; |
| 978 | } |
| 979 | if (parseToken(expectedToken: Token::greater, message: "expected '>' to close an array attribute" )) |
| 980 | return {}; |
| 981 | return eltParser.getAttr(); |
| 982 | } |
| 983 | |
| 984 | /// Try to parse a dense elements attribute with the type-first syntax. |
| 985 | /// Syntax: dense<TYPE : [ATTR, ATTR, ...]> |
| 986 | /// This syntax is used for types other than int, float, index and complex. |
| 987 | /// |
| 988 | /// Returns: |
| 989 | /// - "null" attribute if this is not the type-first syntax. |
| 990 | /// - "failure" in case of a parse error. |
| 991 | /// - A valid Attribute otherwise. |
| 992 | static FailureOr<Attribute> parseDenseElementsAttrTyped(Parser &p, SMLoc loc) { |
| 993 | // Skip l_paren because "parseType" would try to parse it as a tuple/function |
| 994 | // type, but '(' starts a complex literal like in the literal-first syntax. |
| 995 | if (p.getToken().is(k: Token::l_paren)) |
| 996 | return Attribute(); |
| 997 | |
| 998 | // Parse type and valdiate that it's a shaped type. |
| 999 | auto typeLoc = p.getToken().getLoc(); |
| 1000 | Type type; |
| 1001 | OptionalParseResult typeResult = p.parseOptionalType(type); |
| 1002 | if (!typeResult.has_value()) |
| 1003 | return Attribute(); // Not type-first syntax. |
| 1004 | if (failed(Result: *typeResult)) |
| 1005 | return failure(); // Type parse error. |
| 1006 | |
| 1007 | auto shapedType = dyn_cast<ShapedType>(Val&: type); |
| 1008 | if (!shapedType) { |
| 1009 | p.emitError(loc: typeLoc, message: "expected a shaped type for dense elements" ); |
| 1010 | return failure(); |
| 1011 | } |
| 1012 | if (!shapedType.hasStaticShape()) { |
| 1013 | p.emitError(loc: typeLoc, message: "dense elements type must have static shape" ); |
| 1014 | return failure(); |
| 1015 | } |
| 1016 | |
| 1017 | // Check that the element type implements DenseElementTypeInterface. |
| 1018 | auto denseEltType = dyn_cast<DenseElementType>(Val: shapedType.getElementType()); |
| 1019 | if (!denseEltType) { |
| 1020 | p.emitError(loc: typeLoc, |
| 1021 | message: "element type must implement DenseElementTypeInterface " |
| 1022 | "for type-first dense syntax" ); |
| 1023 | return failure(); |
| 1024 | } |
| 1025 | |
| 1026 | // Parse colon. |
| 1027 | if (p.parseToken(expectedToken: Token::colon, message: "expected ':' after type in dense attribute" )) |
| 1028 | return failure(); |
| 1029 | |
| 1030 | // Parse the element attributes and convert to raw bytes. |
| 1031 | SmallVector<char> rawData; |
| 1032 | |
| 1033 | // Helper to parse a single element. |
| 1034 | auto parseSingleElement = [&]() -> ParseResult { |
| 1035 | Attribute elemAttr = p.parseAttribute(); |
| 1036 | if (!elemAttr) |
| 1037 | return failure(); |
| 1038 | if (failed(Result: denseEltType.convertFromAttribute(attr: elemAttr, result&: rawData))) { |
| 1039 | p.emitError(message: "incompatible attribute for element type" ); |
| 1040 | return failure(); |
| 1041 | } |
| 1042 | return success(); |
| 1043 | }; |
| 1044 | |
| 1045 | // Recursively parse elements matching the expected shape. |
| 1046 | std::function<ParseResult(ArrayRef<int64_t>)> parseElements; |
| 1047 | parseElements = [&](ArrayRef<int64_t> remainingShape) -> ParseResult { |
| 1048 | // Leaf: parse a single element. |
| 1049 | if (remainingShape.empty()) |
| 1050 | return parseSingleElement(); |
| 1051 | |
| 1052 | // Non-leaf: expect a list with the correct number of elements. |
| 1053 | int64_t expectedCount = remainingShape.front(); |
| 1054 | ArrayRef<int64_t> innerShape = remainingShape.drop_front(); |
| 1055 | int64_t actualCount = 0; |
| 1056 | |
| 1057 | auto parseOne = [&]() -> ParseResult { |
| 1058 | if (parseElements(innerShape)) |
| 1059 | return failure(); |
| 1060 | ++actualCount; |
| 1061 | return success(); |
| 1062 | }; |
| 1063 | |
| 1064 | if (p.parseCommaSeparatedList(delimiter: Parser::Delimiter::Square, parseElementFn: parseOne)) |
| 1065 | return failure(); |
| 1066 | |
| 1067 | if (actualCount != expectedCount) { |
| 1068 | p.emitError() << "expected " << expectedCount |
| 1069 | << " elements in dimension, got " << actualCount; |
| 1070 | return failure(); |
| 1071 | } |
| 1072 | return success(); |
| 1073 | }; |
| 1074 | |
| 1075 | // Parse elements. |
| 1076 | if (!p.getToken().is(k: Token::l_square)) { |
| 1077 | // Single element - parse as splat. |
| 1078 | if (parseSingleElement()) |
| 1079 | return failure(); |
| 1080 | } else if (shapedType.getShape().empty()) { |
| 1081 | // Scalar type shouldn't have a list. |
| 1082 | p.emitError(loc, message: "expected single element for scalar type, got list" ); |
| 1083 | return failure(); |
| 1084 | } else { |
| 1085 | // Parse structured literal matching the shape. |
| 1086 | if (parseElements(shapedType.getShape())) |
| 1087 | return failure(); |
| 1088 | } |
| 1089 | |
| 1090 | if (p.parseToken(expectedToken: Token::greater, message: "expected '>' to close dense attribute" )) |
| 1091 | return failure(); |
| 1092 | |
| 1093 | // Create the attribute from raw buffer. |
| 1094 | return DenseElementsAttr::getFromRawBuffer(type: shapedType, rawBuffer: rawData); |
| 1095 | } |
| 1096 | |
| 1097 | /// Parse a dense elements attribute. |
| 1098 | Attribute Parser::parseDenseElementsAttr(Type attrType) { |
| 1099 | auto attribLoc = getToken().getLoc(); |
| 1100 | consumeToken(kind: Token::kw_dense); |
| 1101 | if (parseToken(expectedToken: Token::less, message: "expected '<' after 'dense'" )) |
| 1102 | return nullptr; |
| 1103 | |
| 1104 | // Try to parse the type-first syntax: dense<TYPE : [ATTR, ...]> |
| 1105 | FailureOr<Attribute> typedResult = |
| 1106 | parseDenseElementsAttrTyped(p&: *this, loc: attribLoc); |
| 1107 | if (failed(Result: typedResult)) |
| 1108 | return nullptr; |
| 1109 | if (*typedResult) |
| 1110 | return *typedResult; |
| 1111 | |
| 1112 | // Try to parse the literal-first syntax, which is the default format for |
| 1113 | // int, float, index and complex element types. |
| 1114 | TensorLiteralParser literalParser(*this); |
| 1115 | if (!consumeIf(kind: Token::greater)) { |
| 1116 | if (literalParser.parse(/*allowHex=*/true) || |
| 1117 | parseToken(expectedToken: Token::greater, message: "expected '>'" )) |
| 1118 | return nullptr; |
| 1119 | } |
| 1120 | |
| 1121 | auto type = parseElementsLiteralType(loc: attribLoc, type: attrType); |
| 1122 | if (!type) |
| 1123 | return nullptr; |
| 1124 | return literalParser.getAttr(loc: attribLoc, type); |
| 1125 | } |
| 1126 | |
| 1127 | Attribute Parser::parseDenseResourceElementsAttr(Type attrType) { |
| 1128 | auto loc = getToken().getLoc(); |
| 1129 | consumeToken(kind: Token::kw_dense_resource); |
| 1130 | if (parseToken(expectedToken: Token::less, message: "expected '<' after 'dense_resource'" )) |
| 1131 | return nullptr; |
| 1132 | |
| 1133 | // Parse the resource handle. |
| 1134 | FailureOr<AsmDialectResourceHandle> rawHandle = |
| 1135 | parseResourceHandle(dialect: getContext()->getLoadedDialect<BuiltinDialect>()); |
| 1136 | if (failed(Result: rawHandle) || parseToken(expectedToken: Token::greater, message: "expected '>'" )) |
| 1137 | return nullptr; |
| 1138 | |
| 1139 | auto *handle = dyn_cast<DenseResourceElementsHandle>(Val: &*rawHandle); |
| 1140 | if (!handle) |
| 1141 | return emitError(loc, message: "invalid `dense_resource` handle type" ), nullptr; |
| 1142 | |
| 1143 | // Parse the type of the attribute if the user didn't provide one. |
| 1144 | SMLoc typeLoc = loc; |
| 1145 | if (!attrType) { |
| 1146 | typeLoc = getToken().getLoc(); |
| 1147 | if (parseToken(expectedToken: Token::colon, message: "expected ':'" ) || !(attrType = parseType())) |
| 1148 | return nullptr; |
| 1149 | } |
| 1150 | |
| 1151 | ShapedType shapedType = dyn_cast<ShapedType>(Val&: attrType); |
| 1152 | if (!shapedType) { |
| 1153 | emitError(loc: typeLoc, message: "`dense_resource` expected a shaped type" ); |
| 1154 | return nullptr; |
| 1155 | } |
| 1156 | |
| 1157 | return DenseResourceElementsAttr::get(type: shapedType, handle: *handle); |
| 1158 | } |
| 1159 | |
| 1160 | /// Shaped type for elements attribute. |
| 1161 | /// |
| 1162 | /// elements-literal-type ::= vector-type | ranked-tensor-type |
| 1163 | /// |
| 1164 | /// This method also checks the type has static shape. |
| 1165 | ShapedType Parser::parseElementsLiteralType(SMLoc loc, Type type) { |
| 1166 | // If the user didn't provide a type, parse the colon type for the literal. |
| 1167 | if (!type) { |
| 1168 | if (parseToken(expectedToken: Token::colon, message: "expected ':'" )) |
| 1169 | return nullptr; |
| 1170 | if (!(type = parseType())) |
| 1171 | return nullptr; |
| 1172 | } |
| 1173 | |
| 1174 | auto sType = dyn_cast<ShapedType>(Val&: type); |
| 1175 | if (!sType) { |
| 1176 | emitError(loc, message: "elements literal must be a shaped type" ); |
| 1177 | return nullptr; |
| 1178 | } |
| 1179 | |
| 1180 | if (!sType.hasStaticShape()) { |
| 1181 | emitError(loc, message: "elements literal type must have static shape" ); |
| 1182 | return nullptr; |
| 1183 | } |
| 1184 | |
| 1185 | return sType; |
| 1186 | } |
| 1187 | |
| 1188 | /// Parse a sparse elements attribute. |
| 1189 | Attribute Parser::parseSparseElementsAttr(Type attrType) { |
| 1190 | SMLoc loc = getToken().getLoc(); |
| 1191 | consumeToken(kind: Token::kw_sparse); |
| 1192 | if (parseToken(expectedToken: Token::less, message: "Expected '<' after 'sparse'" )) |
| 1193 | return nullptr; |
| 1194 | |
| 1195 | // Check for the case where all elements are sparse. The indices are |
| 1196 | // represented by a 2-dimensional shape where the second dimension is the rank |
| 1197 | // of the type. |
| 1198 | Type indiceEltType = builder.getIntegerType(width: 64); |
| 1199 | if (consumeIf(kind: Token::greater)) { |
| 1200 | ShapedType type = parseElementsLiteralType(loc, type: attrType); |
| 1201 | if (!type) |
| 1202 | return nullptr; |
| 1203 | |
| 1204 | // Construct the sparse elements attr using zero element indice/value |
| 1205 | // attributes. |
| 1206 | ShapedType indicesType = |
| 1207 | RankedTensorType::get(shape: {0, type.getRank()}, elementType: indiceEltType); |
| 1208 | ShapedType valuesType = RankedTensorType::get(shape: {0}, elementType: type.getElementType()); |
| 1209 | return getChecked<SparseElementsAttr>( |
| 1210 | loc, params&: type, params: DenseElementsAttr::get(type: indicesType, values: ArrayRef<Attribute>()), |
| 1211 | params: DenseElementsAttr::get(type: valuesType, values: ArrayRef<Attribute>())); |
| 1212 | } |
| 1213 | |
| 1214 | /// Parse the indices. We don't allow hex values here as we may need to use |
| 1215 | /// the inferred shape. |
| 1216 | auto indicesLoc = getToken().getLoc(); |
| 1217 | TensorLiteralParser indiceParser(*this); |
| 1218 | if (indiceParser.parse(/*allowHex=*/false)) |
| 1219 | return nullptr; |
| 1220 | |
| 1221 | if (parseToken(expectedToken: Token::comma, message: "expected ','" )) |
| 1222 | return nullptr; |
| 1223 | |
| 1224 | /// Parse the values. |
| 1225 | auto valuesLoc = getToken().getLoc(); |
| 1226 | TensorLiteralParser valuesParser(*this); |
| 1227 | if (valuesParser.parse(/*allowHex=*/true)) |
| 1228 | return nullptr; |
| 1229 | |
| 1230 | if (parseToken(expectedToken: Token::greater, message: "expected '>'" )) |
| 1231 | return nullptr; |
| 1232 | |
| 1233 | auto type = parseElementsLiteralType(loc, type: attrType); |
| 1234 | if (!type) |
| 1235 | return nullptr; |
| 1236 | |
| 1237 | // If the indices are a splat, i.e. the literal parser parsed an element and |
| 1238 | // not a list, we set the shape explicitly. The indices are represented by a |
| 1239 | // 2-dimensional shape where the second dimension is the rank of the type. |
| 1240 | // Given that the parsed indices is a splat, we know that we only have one |
| 1241 | // indice and thus one for the first dimension. |
| 1242 | ShapedType indicesType; |
| 1243 | if (indiceParser.getShape().empty()) { |
| 1244 | indicesType = RankedTensorType::get(shape: {1, type.getRank()}, elementType: indiceEltType); |
| 1245 | } else { |
| 1246 | // Otherwise, set the shape to the one parsed by the literal parser. |
| 1247 | indicesType = RankedTensorType::get(shape: indiceParser.getShape(), elementType: indiceEltType); |
| 1248 | } |
| 1249 | auto indices = indiceParser.getAttr(loc: indicesLoc, type: indicesType); |
| 1250 | if (!indices) |
| 1251 | return nullptr; |
| 1252 | |
| 1253 | // If the values are a splat, set the shape explicitly based on the number of |
| 1254 | // indices. The number of indices is encoded in the first dimension of the |
| 1255 | // indice shape type. |
| 1256 | auto valuesEltType = type.getElementType(); |
| 1257 | ShapedType valuesType = |
| 1258 | valuesParser.getShape().empty() |
| 1259 | ? RankedTensorType::get(shape: {indicesType.getDimSize(idx: 0)}, elementType: valuesEltType) |
| 1260 | : RankedTensorType::get(shape: valuesParser.getShape(), elementType: valuesEltType); |
| 1261 | auto values = valuesParser.getAttr(loc: valuesLoc, type: valuesType); |
| 1262 | if (!values) |
| 1263 | return nullptr; |
| 1264 | |
| 1265 | // Build the sparse elements attribute by the indices and values. |
| 1266 | return getChecked<SparseElementsAttr>(loc, params&: type, params&: indices, params&: values); |
| 1267 | } |
| 1268 | |
| 1269 | Attribute Parser::parseStridedLayoutAttr() { |
| 1270 | // Callback for error emissing at the keyword token location. |
| 1271 | llvm::SMLoc loc = getToken().getLoc(); |
| 1272 | auto errorEmitter = [&] { return emitError(loc); }; |
| 1273 | |
| 1274 | consumeToken(kind: Token::kw_strided); |
| 1275 | if (failed(Result: parseToken(expectedToken: Token::less, message: "expected '<' after 'strided'" )) || |
| 1276 | failed(Result: parseToken(expectedToken: Token::l_square, message: "expected '['" ))) |
| 1277 | return nullptr; |
| 1278 | |
| 1279 | // Parses either an integer token or a question mark token. Reports an error |
| 1280 | // and returns std::nullopt if the current token is neither. The integer token |
| 1281 | // must fit into int64_t limits. |
| 1282 | auto parseStrideOrOffset = [&]() -> std::optional<int64_t> { |
| 1283 | if (consumeIf(kind: Token::question)) |
| 1284 | return ShapedType::kDynamic; |
| 1285 | |
| 1286 | SMLoc loc = getToken().getLoc(); |
| 1287 | auto emitWrongTokenError = [&] { |
| 1288 | emitError(loc, message: "expected a 64-bit signed integer or '?'" ); |
| 1289 | return std::nullopt; |
| 1290 | }; |
| 1291 | |
| 1292 | bool negative = consumeIf(kind: Token::minus); |
| 1293 | |
| 1294 | if (getToken().is(k: Token::integer)) { |
| 1295 | std::optional<uint64_t> value = getToken().getUInt64IntegerValue(); |
| 1296 | if (!value || |
| 1297 | *value > static_cast<uint64_t>(std::numeric_limits<int64_t>::max())) |
| 1298 | return emitWrongTokenError(); |
| 1299 | consumeToken(); |
| 1300 | auto result = static_cast<int64_t>(*value); |
| 1301 | if (negative) |
| 1302 | result = -result; |
| 1303 | |
| 1304 | return result; |
| 1305 | } |
| 1306 | |
| 1307 | return emitWrongTokenError(); |
| 1308 | }; |
| 1309 | |
| 1310 | // Parse strides. |
| 1311 | SmallVector<int64_t> strides; |
| 1312 | if (!getToken().is(k: Token::r_square)) { |
| 1313 | do { |
| 1314 | std::optional<int64_t> stride = parseStrideOrOffset(); |
| 1315 | if (!stride) |
| 1316 | return nullptr; |
| 1317 | strides.push_back(Elt: *stride); |
| 1318 | } while (consumeIf(kind: Token::comma)); |
| 1319 | } |
| 1320 | |
| 1321 | if (failed(Result: parseToken(expectedToken: Token::r_square, message: "expected ']'" ))) |
| 1322 | return nullptr; |
| 1323 | |
| 1324 | // Fast path in absence of offset. |
| 1325 | if (consumeIf(kind: Token::greater)) { |
| 1326 | if (failed(Result: StridedLayoutAttr::verify(emitError: errorEmitter, |
| 1327 | /*offset=*/0, strides))) |
| 1328 | return nullptr; |
| 1329 | return StridedLayoutAttr::get(context: getContext(), /*offset=*/0, strides); |
| 1330 | } |
| 1331 | |
| 1332 | if (failed(Result: parseToken(expectedToken: Token::comma, message: "expected ','" )) || |
| 1333 | failed(Result: parseToken(expectedToken: Token::kw_offset, message: "expected 'offset' after comma" )) || |
| 1334 | failed(Result: parseToken(expectedToken: Token::colon, message: "expected ':' after 'offset'" ))) |
| 1335 | return nullptr; |
| 1336 | |
| 1337 | std::optional<int64_t> offset = parseStrideOrOffset(); |
| 1338 | if (!offset || failed(Result: parseToken(expectedToken: Token::greater, message: "expected '>'" ))) |
| 1339 | return nullptr; |
| 1340 | |
| 1341 | if (failed(Result: StridedLayoutAttr::verify(emitError: errorEmitter, offset: *offset, strides))) |
| 1342 | return nullptr; |
| 1343 | return StridedLayoutAttr::get(context: getContext(), offset: *offset, strides); |
| 1344 | // return getChecked<StridedLayoutAttr>(loc,getContext(), *offset, strides); |
| 1345 | } |
| 1346 | |
| 1347 | /// Parse a distinct attribute. |
| 1348 | /// |
| 1349 | /// distinct-attribute ::= `distinct` |
| 1350 | /// `[` integer-literal `]<` attribute-value `>` |
| 1351 | /// |
| 1352 | Attribute Parser::parseDistinctAttr(Type type) { |
| 1353 | SMLoc loc = getToken().getLoc(); |
| 1354 | consumeToken(kind: Token::kw_distinct); |
| 1355 | if (parseToken(expectedToken: Token::l_square, message: "expected '[' after 'distinct'" )) |
| 1356 | return {}; |
| 1357 | |
| 1358 | // Parse the distinct integer identifier. |
| 1359 | Token token = getToken(); |
| 1360 | if (parseToken(expectedToken: Token::integer, message: "expected distinct ID" )) |
| 1361 | return {}; |
| 1362 | std::optional<uint64_t> value = token.getUInt64IntegerValue(); |
| 1363 | if (!value) { |
| 1364 | emitError(message: "expected an unsigned 64-bit integer" ); |
| 1365 | return {}; |
| 1366 | } |
| 1367 | |
| 1368 | // Parse the referenced attribute. |
| 1369 | if (parseToken(expectedToken: Token::r_square, message: "expected ']' to close distinct ID" ) || |
| 1370 | parseToken(expectedToken: Token::less, message: "expected '<' after distinct ID" )) |
| 1371 | return {}; |
| 1372 | |
| 1373 | Attribute referencedAttr; |
| 1374 | if (getToken().is(k: Token::greater)) { |
| 1375 | consumeToken(); |
| 1376 | referencedAttr = builder.getUnitAttr(); |
| 1377 | } else { |
| 1378 | referencedAttr = parseAttribute(type); |
| 1379 | if (!referencedAttr) { |
| 1380 | emitError(message: "expected attribute" ); |
| 1381 | return {}; |
| 1382 | } |
| 1383 | |
| 1384 | if (parseToken(expectedToken: Token::greater, message: "expected '>' to close distinct attribute" )) |
| 1385 | return {}; |
| 1386 | } |
| 1387 | |
| 1388 | // Add the distinct attribute to the parser state, if it has not been parsed |
| 1389 | // before. Otherwise, check if the parsed reference attribute matches the one |
| 1390 | // found in the parser state. |
| 1391 | DenseMap<uint64_t, DistinctAttr> &distinctAttrs = |
| 1392 | state.symbols.distinctAttributes; |
| 1393 | auto it = distinctAttrs.find(Val: *value); |
| 1394 | if (it == distinctAttrs.end()) { |
| 1395 | DistinctAttr distinctAttr = DistinctAttr::create(referencedAttr); |
| 1396 | it = distinctAttrs.try_emplace(Key: *value, Args&: distinctAttr).first; |
| 1397 | } else if (it->getSecond().getReferencedAttr() != referencedAttr) { |
| 1398 | emitError(loc, message: "referenced attribute does not match previous definition: " ) |
| 1399 | << it->getSecond().getReferencedAttr(); |
| 1400 | return {}; |
| 1401 | } |
| 1402 | |
| 1403 | return it->getSecond(); |
| 1404 | } |
| 1405 | |