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