28 case Token::kw_memref:
29 case Token::kw_tensor:
30 case Token::kw_complex:
32 case Token::kw_vector:
34 case Token::kw_f8E5M2:
35 case Token::kw_f8E4M3FN:
44 case Token::exclamation_identifier:
75 elements.push_back(t);
87 elements.push_back(elt);
100 if (
parseToken(Token::l_paren,
"expected '('"))
121 if (
parseToken(Token::less,
"expected '<' in complex type"))
127 parseToken(Token::greater,
"expected '>' in complex type"))
129 if (!elementType.isa<
FloatType>() && !elementType.
isa<IntegerType>())
130 return emitError(elementTypeLoc,
"invalid element type for complex"),
133 return ComplexType::get(elementType);
141 assert(
getToken().is(Token::l_paren));
145 parseToken(Token::arrow,
"expected '->' in function type") ||
170 if (
parseToken(Token::less,
"expected '<' in memref type"))
196 return emitError(typeLoc,
"invalid memref element type"),
nullptr;
198 MemRefLayoutAttrInterface layout;
207 if (attr.
isa<MemRefLayoutAttrInterface>()) {
208 layout = attr.
cast<MemRefLayoutAttrInterface>();
209 }
else if (memorySpace) {
210 return emitError(
"multiple memory spaces specified in memref type");
217 return emitError(
"cannot have affine map for unranked memref type");
219 return emitError(
"expected memory space to be last in memref type");
227 if (
parseToken(Token::comma,
"expected ',' or '>' in memref type") ||
235 return getChecked<UnrankedMemRefType>(loc, elementType, memorySpace);
237 return getChecked<MemRefType>(loc, dimensions, elementType, layout,
262 case Token::kw_memref:
264 case Token::kw_tensor:
266 case Token::kw_complex:
268 case Token::kw_tuple:
270 case Token::kw_vector:
273 case Token::inttype: {
275 if (!width.has_value())
276 return (
emitError(
"invalid integer width"),
nullptr);
277 if (*width > IntegerType::kMaxWidth) {
279 << IntegerType::kMaxWidth <<
" bits";
283 IntegerType::SignednessSemantics signSemantics = IntegerType::Signless;
284 if (std::optional<bool> signedness =
getToken().getIntTypeSignedness())
288 return IntegerType::get(
getContext(), *width, signSemantics);
292 case Token::kw_f8E5M2:
295 case Token::kw_f8E4M3FN:
318 case Token::kw_index:
328 case Token::exclamation_identifier:
332 case Token::code_complete:
333 if (
getToken().isCodeCompletionFor(Token::exclamation_identifier))
347 if (
parseToken(Token::less,
"expected '<' in tensor type"))
375 if (
failed(v.verifyEncoding(dimensions, elementType,
376 [&] { return emitError(); })))
381 if (!elementType ||
parseToken(Token::greater,
"expected '>' in tensor type"))
384 return emitError(elementTypeLoc,
"invalid tensor element type"),
nullptr;
388 return emitError(
"cannot apply encoding to unranked tensor"),
nullptr;
389 return UnrankedTensorType::get(elementType);
391 return RankedTensorType::get(dimensions, elementType, encoding);
402 if (
parseToken(Token::less,
"expected '<' in tuple type"))
412 parseToken(Token::greater,
"expected '>' in tuple type"))
427 if (
parseToken(Token::less,
"expected '<' in vector type"))
431 unsigned numScalableDims;
434 if (any_of(dimensions, [](int64_t i) {
return i <= 0; }))
436 "vector types must have positive constant sizes"),
442 if (!elementType ||
parseToken(Token::greater,
"expected '>' in vector type"))
445 if (!VectorType::isValidElementType(elementType))
446 return emitError(typeLoc,
"vector elements must be int/index/float type"),
449 return VectorType::get(dimensions, elementType, numScalableDims);
460 unsigned &numScalableDims) {
463 while (
getToken().is(Token::integer)) {
467 dimensions.push_back(value);
474 while (
getToken().is(Token::integer)) {
478 dimensions.push_back(value);
492 "missing ']' closing set of scalable dimensions");
513 bool allowDynamic,
bool withTrailingX) {
518 return emitError(loc,
"expected static shape");
519 dimensions.push_back(ShapedType::kDynamic);
524 dimensions.push_back(value);
530 while (
getToken().isAny(Token::integer, Token::question)) {
537 if (
getToken().isAny(Token::integer, Token::question)) {
540 while (
getToken().is(Token::bare_identifier) &&
568 value = (int64_t)*dimension;
static Value max(ImplicitLocOpBuilder &builder, Value value, Value bound)
Attributes are known-constant values of operations.
U dyn_cast_or_null() const
bool isa() const
Casting utility functions.
static bool isValidElementType(Type type)
Return true if the specified element type is ok in a memref.
FloatType getFloat8E5M2Type()
FunctionType getFunctionType(TypeRange inputs, TypeRange results)
FloatType getFloat8E4M3FNType()
void resetPointer(const char *newPointer)
Change the position of the lexer cursor.
This class implements Optional functionality for ParseResult.
This class represents success/failure for parsing-like operations that find it important to chain tog...
static bool isValidElementType(Type type)
Return true if the specified element type is ok in a tensor.
static std::optional< uint64_t > getUInt64IntegerValue(StringRef spelling)
For an integer token, return its value as an uint64_t.
std::optional< unsigned > getIntTypeBitwidth() const
For an inttype token, return its bitwidth.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
ParseResult parseVectorDimensionList(SmallVectorImpl< int64_t > &dimensions, unsigned &numScalableDims)
Parse a dimension list in a vector type.
ParseResult parseXInDimensionList()
Parse an 'x' token in a dimension list, handling the case where the x is juxtaposed with an element t...
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.
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.
Type parseType()
Parse an arbitrary type.
ParseResult parseTypeListParens(SmallVectorImpl< Type > &elements)
Parse a parenthesized list of types.
Type parseMemRefType()
Parse a memref type.
Type parseNonFunctionType()
Parse a non function type.
Type parseExtendedType()
Parse an extended type.
Type parseTupleType()
Parse a tuple type.
InFlightDiagnostic emitError(const Twine &message={})
Emit an error and return failure.
ParserState & state
The Parser is subclassed and reinstantiated.
Attribute parseAttribute(Type type={})
Parse an arbitrary attribute with an optional type.
StringRef getTokenSpelling() const
void consumeToken()
Advance the current lexer onto the next token.
ParseResult parseIntegerInDimensionList(int64_t &value)
Type parseComplexType()
Parse a complex type.
ParseResult parseDimensionListRanked(SmallVectorImpl< int64_t > &dimensions, bool allowDynamic=true, bool withTrailingX=true)
Parse a dimension list of a tensor or memref type.
ParseResult parseFunctionResultTypes(SmallVectorImpl< Type > &elements)
Parse a function result type.
MLIRContext * getContext() const
InFlightDiagnostic emitWrongTokenError(const Twine &message={})
Emit an error about a "wrong token".
ParseResult parseCommaSeparatedList(Delimiter delimiter, function_ref< ParseResult()> parseElementFn, StringRef contextMessage=StringRef())
Parse a list of comma-separated items with an optional delimiter.
VectorType parseVectorType()
Parse a vector type.
Type parseFunctionType()
Parse a function type.
ParseResult parseTypeListNoParens(SmallVectorImpl< Type > &elements)
Parse a list of types without an enclosing parenthesis.
const Token & getToken() const
Return the current token the parser is inspecting.
Type parseTensorType()
Parse a tensor type.
bool consumeIf(Token::Kind kind)
If the current token has the specified kind, consume it and return true.
Detect if any of the given parameter types has a sub-element handler.
Include the generated interface declarations.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value.
This class represents an efficient way to signal success or failure.
Lexer lex
The lexer for the source file we're parsing.