1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
|
//===- TypeParser.cpp - MLIR Type Parser Implementation -------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements the parser for the MLIR Types.
//
//===----------------------------------------------------------------------===//
#include "Parser.h"
#include "mlir/IR/AffineMap.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/OpDefinition.h"
#include "mlir/IR/TensorEncoding.h"
#include <optional>
using namespace mlir;
using namespace mlir::detail;
/// Optionally parse a type.
OptionalParseResult Parser::parseOptionalType(Type &type) {
// There are many different starting tokens for a type, check them here.
switch (getToken().getKind()) {
case Token::l_paren:
case Token::kw_memref:
case Token::kw_tensor:
case Token::kw_complex:
case Token::kw_tuple:
case Token::kw_vector:
case Token::inttype:
case Token::kw_f8E5M2:
case Token::kw_f8E4M3FN:
case Token::kw_f8E5M2FNUZ:
case Token::kw_f8E4M3FNUZ:
case Token::kw_f8E4M3B11FNUZ:
case Token::kw_bf16:
case Token::kw_f16:
case Token::kw_tf32:
case Token::kw_f32:
case Token::kw_f64:
case Token::kw_f80:
case Token::kw_f128:
case Token::kw_index:
case Token::kw_none:
case Token::exclamation_identifier:
return failure(!(type = parseType()));
default:
return std::nullopt;
}
}
/// Parse an arbitrary type.
///
/// type ::= function-type
/// | non-function-type
///
Type Parser::parseType() {
if (getToken().is(Token::l_paren))
return parseFunctionType();
return parseNonFunctionType();
}
/// Parse a function result type.
///
/// function-result-type ::= type-list-parens
/// | non-function-type
///
ParseResult Parser::parseFunctionResultTypes(SmallVectorImpl<Type> &elements) {
if (getToken().is(Token::l_paren))
return parseTypeListParens(elements);
Type t = parseNonFunctionType();
if (!t)
return failure();
elements.push_back(t);
return success();
}
/// Parse a list of types without an enclosing parenthesis. The list must have
/// at least one member.
///
/// type-list-no-parens ::= type (`,` type)*
///
ParseResult Parser::parseTypeListNoParens(SmallVectorImpl<Type> &elements) {
auto parseElt = [&]() -> ParseResult {
auto elt = parseType();
elements.push_back(elt);
return elt ? success() : failure();
};
return parseCommaSeparatedList(parseElt);
}
/// Parse a parenthesized list of types.
///
/// type-list-parens ::= `(` `)`
/// | `(` type-list-no-parens `)`
///
ParseResult Parser::parseTypeListParens(SmallVectorImpl<Type> &elements) {
if (parseToken(Token::l_paren, "expected '('"))
return failure();
// Handle empty lists.
if (getToken().is(Token::r_paren))
return consumeToken(), success();
if (parseTypeListNoParens(elements) ||
parseToken(Token::r_paren, "expected ')'"))
return failure();
return success();
}
/// Parse a complex type.
///
/// complex-type ::= `complex` `<` type `>`
///
Type Parser::parseComplexType() {
consumeToken(Token::kw_complex);
// Parse the '<'.
if (parseToken(Token::less, "expected '<' in complex type"))
return nullptr;
SMLoc elementTypeLoc = getToken().getLoc();
auto elementType = parseType();
if (!elementType ||
parseToken(Token::greater, "expected '>' in complex type"))
return nullptr;
if (!isa<FloatType>(elementType) && !isa<IntegerType>(elementType))
return emitError(elementTypeLoc, "invalid element type for complex"),
nullptr;
return ComplexType::get(elementType);
}
/// Parse a function type.
///
/// function-type ::= type-list-parens `->` function-result-type
///
Type Parser::parseFunctionType() {
assert(getToken().is(Token::l_paren));
SmallVector<Type, 4> arguments, results;
if (parseTypeListParens(arguments) ||
parseToken(Token::arrow, "expected '->' in function type") ||
parseFunctionResultTypes(results))
return nullptr;
return builder.getFunctionType(arguments, results);
}
/// Parse a memref type.
///
/// memref-type ::= ranked-memref-type | unranked-memref-type
///
/// ranked-memref-type ::= `memref` `<` dimension-list-ranked type
/// (`,` layout-specification)? (`,` memory-space)? `>`
///
/// unranked-memref-type ::= `memref` `<*x` type (`,` memory-space)? `>`
///
/// stride-list ::= `[` (dimension (`,` dimension)*)? `]`
/// strided-layout ::= `offset:` dimension `,` `strides: ` stride-list
/// layout-specification ::= semi-affine-map | strided-layout | attribute
/// memory-space ::= integer-literal | attribute
///
Type Parser::parseMemRefType() {
SMLoc loc = getToken().getLoc();
consumeToken(Token::kw_memref);
if (parseToken(Token::less, "expected '<' in memref type"))
return nullptr;
bool isUnranked;
SmallVector<int64_t, 4> dimensions;
if (consumeIf(Token::star)) {
// This is an unranked memref type.
isUnranked = true;
if (parseXInDimensionList())
return nullptr;
} else {
isUnranked = false;
if (parseDimensionListRanked(dimensions))
return nullptr;
}
// Parse the element type.
auto typeLoc = getToken().getLoc();
auto elementType = parseType();
if (!elementType)
return nullptr;
// Check that memref is formed from allowed types.
if (!BaseMemRefType::isValidElementType(elementType))
return emitError(typeLoc, "invalid memref element type"), nullptr;
MemRefLayoutAttrInterface layout;
Attribute memorySpace;
auto parseElt = [&]() -> ParseResult {
// Either it is MemRefLayoutAttrInterface or memory space attribute.
Attribute attr = parseAttribute();
if (!attr)
return failure();
if (isa<MemRefLayoutAttrInterface>(attr)) {
layout = cast<MemRefLayoutAttrInterface>(attr);
} else if (memorySpace) {
return emitError("multiple memory spaces specified in memref type");
} else {
memorySpace = attr;
return success();
}
if (isUnranked)
return emitError("cannot have affine map for unranked memref type");
if (memorySpace)
return emitError("expected memory space to be last in memref type");
return success();
};
// Parse a list of mappings and address space if present.
if (!consumeIf(Token::greater)) {
// Parse comma separated list of affine maps, followed by memory space.
if (parseToken(Token::comma, "expected ',' or '>' in memref type") ||
parseCommaSeparatedListUntil(Token::greater, parseElt,
/*allowEmptyList=*/false)) {
return nullptr;
}
}
if (isUnranked)
return getChecked<UnrankedMemRefType>(loc, elementType, memorySpace);
return getChecked<MemRefType>(loc, dimensions, elementType, layout,
memorySpace);
}
/// Parse any type except the function type.
///
/// non-function-type ::= integer-type
/// | index-type
/// | float-type
/// | extended-type
/// | vector-type
/// | tensor-type
/// | memref-type
/// | complex-type
/// | tuple-type
/// | none-type
///
/// index-type ::= `index`
/// float-type ::= `f16` | `bf16` | `f32` | `f64` | `f80` | `f128`
/// none-type ::= `none`
///
Type Parser::parseNonFunctionType() {
switch (getToken().getKind()) {
default:
return (emitWrongTokenError("expected non-function type"), nullptr);
case Token::kw_memref:
return parseMemRefType();
case Token::kw_tensor:
return parseTensorType();
case Token::kw_complex:
return parseComplexType();
case Token::kw_tuple:
return parseTupleType();
case Token::kw_vector:
return parseVectorType();
// integer-type
case Token::inttype: {
auto width = getToken().getIntTypeBitwidth();
if (!width.has_value())
return (emitError("invalid integer width"), nullptr);
if (*width > IntegerType::kMaxWidth) {
emitError(getToken().getLoc(), "integer bitwidth is limited to ")
<< IntegerType::kMaxWidth << " bits";
return nullptr;
}
IntegerType::SignednessSemantics signSemantics = IntegerType::Signless;
if (std::optional<bool> signedness = getToken().getIntTypeSignedness())
signSemantics = *signedness ? IntegerType::Signed : IntegerType::Unsigned;
consumeToken(Token::inttype);
return IntegerType::get(getContext(), *width, signSemantics);
}
// float-type
case Token::kw_f8E5M2:
consumeToken(Token::kw_f8E5M2);
return builder.getFloat8E5M2Type();
case Token::kw_f8E4M3FN:
consumeToken(Token::kw_f8E4M3FN);
return builder.getFloat8E4M3FNType();
case Token::kw_f8E5M2FNUZ:
consumeToken(Token::kw_f8E5M2FNUZ);
return builder.getFloat8E5M2FNUZType();
case Token::kw_f8E4M3FNUZ:
consumeToken(Token::kw_f8E4M3FNUZ);
return builder.getFloat8E4M3FNUZType();
case Token::kw_f8E4M3B11FNUZ:
consumeToken(Token::kw_f8E4M3B11FNUZ);
return builder.getFloat8E4M3B11FNUZType();
case Token::kw_bf16:
consumeToken(Token::kw_bf16);
return builder.getBF16Type();
case Token::kw_f16:
consumeToken(Token::kw_f16);
return builder.getF16Type();
case Token::kw_tf32:
consumeToken(Token::kw_tf32);
return builder.getTF32Type();
case Token::kw_f32:
consumeToken(Token::kw_f32);
return builder.getF32Type();
case Token::kw_f64:
consumeToken(Token::kw_f64);
return builder.getF64Type();
case Token::kw_f80:
consumeToken(Token::kw_f80);
return builder.getF80Type();
case Token::kw_f128:
consumeToken(Token::kw_f128);
return builder.getF128Type();
// index-type
case Token::kw_index:
consumeToken(Token::kw_index);
return builder.getIndexType();
// none-type
case Token::kw_none:
consumeToken(Token::kw_none);
return builder.getNoneType();
// extended type
case Token::exclamation_identifier:
return parseExtendedType();
// Handle completion of a dialect type.
case Token::code_complete:
if (getToken().isCodeCompletionFor(Token::exclamation_identifier))
return parseExtendedType();
return codeCompleteType();
}
}
/// Parse a tensor type.
///
/// tensor-type ::= `tensor` `<` dimension-list type `>`
/// dimension-list ::= dimension-list-ranked | `*x`
///
Type Parser::parseTensorType() {
consumeToken(Token::kw_tensor);
if (parseToken(Token::less, "expected '<' in tensor type"))
return nullptr;
bool isUnranked;
SmallVector<int64_t, 4> dimensions;
if (consumeIf(Token::star)) {
// This is an unranked tensor type.
isUnranked = true;
if (parseXInDimensionList())
return nullptr;
} else {
isUnranked = false;
if (parseDimensionListRanked(dimensions))
return nullptr;
}
// Parse the element type.
auto elementTypeLoc = getToken().getLoc();
auto elementType = parseType();
// Parse an optional encoding attribute.
Attribute encoding;
if (consumeIf(Token::comma)) {
encoding = parseAttribute();
if (auto v = dyn_cast_or_null<VerifiableTensorEncoding>(encoding)) {
if (failed(v.verifyEncoding(dimensions, elementType,
[&] { return emitError(); })))
return nullptr;
}
}
if (!elementType || parseToken(Token::greater, "expected '>' in tensor type"))
return nullptr;
if (!TensorType::isValidElementType(elementType))
return emitError(elementTypeLoc, "invalid tensor element type"), nullptr;
if (isUnranked) {
if (encoding)
return emitError("cannot apply encoding to unranked tensor"), nullptr;
return UnrankedTensorType::get(elementType);
}
return RankedTensorType::get(dimensions, elementType, encoding);
}
/// Parse a tuple type.
///
/// tuple-type ::= `tuple` `<` (type (`,` type)*)? `>`
///
Type Parser::parseTupleType() {
consumeToken(Token::kw_tuple);
// Parse the '<'.
if (parseToken(Token::less, "expected '<' in tuple type"))
return nullptr;
// Check for an empty tuple by directly parsing '>'.
if (consumeIf(Token::greater))
return TupleType::get(getContext());
// Parse the element types and the '>'.
SmallVector<Type, 4> types;
if (parseTypeListNoParens(types) ||
parseToken(Token::greater, "expected '>' in tuple type"))
return nullptr;
return TupleType::get(getContext(), types);
}
/// Parse a vector type.
///
/// vector-type ::= `vector` `<` vector-dim-list vector-element-type `>`
/// vector-dim-list := (static-dim-list `x`)? (`[` static-dim-list `]` `x`)?
/// static-dim-list ::= decimal-literal (`x` decimal-literal)*
///
VectorType Parser::parseVectorType() {
consumeToken(Token::kw_vector);
if (parseToken(Token::less, "expected '<' in vector type"))
return nullptr;
SmallVector<int64_t, 4> dimensions;
SmallVector<bool, 4> scalableDims;
if (parseVectorDimensionList(dimensions, scalableDims))
return nullptr;
if (any_of(dimensions, [](int64_t i) { return i <= 0; }))
return emitError(getToken().getLoc(),
"vector types must have positive constant sizes"),
nullptr;
// Parse the element type.
auto typeLoc = getToken().getLoc();
auto elementType = parseType();
if (!elementType || parseToken(Token::greater, "expected '>' in vector type"))
return nullptr;
if (!VectorType::isValidElementType(elementType))
return emitError(typeLoc, "vector elements must be int/index/float type"),
nullptr;
return VectorType::get(dimensions, elementType, scalableDims);
}
/// Parse a dimension list in a vector type. This populates the dimension list.
/// For i-th dimension, `scalableDims[i]` contains either:
/// * `false` for a non-scalable dimension (e.g. `4`),
/// * `true` for a scalable dimension (e.g. `[4]`).
///
/// vector-dim-list := (static-dim-list `x`)?
/// static-dim-list ::= static-dim (`x` static-dim)*
/// static-dim ::= (decimal-literal | `[` decimal-literal `]`)
///
ParseResult
Parser::parseVectorDimensionList(SmallVectorImpl<int64_t> &dimensions,
SmallVectorImpl<bool> &scalableDims) {
// If there is a set of fixed-length dimensions, consume it
while (getToken().is(Token::integer) || getToken().is(Token::l_square)) {
int64_t value;
bool scalable = consumeIf(Token::l_square);
if (parseIntegerInDimensionList(value))
return failure();
dimensions.push_back(value);
if (scalable) {
if (!consumeIf(Token::r_square))
return emitWrongTokenError("missing ']' closing scalable dimension");
}
scalableDims.push_back(scalable);
// Make sure we have an 'x' or something like 'xbf32'.
if (parseXInDimensionList())
return failure();
}
return success();
}
/// Parse a dimension list of a tensor or memref type. This populates the
/// dimension list, using ShapedType::kDynamic for the `?` dimensions if
/// `allowDynamic` is set and errors out on `?` otherwise. Parsing the trailing
/// `x` is configurable.
///
/// dimension-list ::= eps | dimension (`x` dimension)*
/// dimension-list-with-trailing-x ::= (dimension `x`)*
/// dimension ::= `?` | decimal-literal
///
/// When `allowDynamic` is not set, this is used to parse:
///
/// static-dimension-list ::= eps | decimal-literal (`x` decimal-literal)*
/// static-dimension-list-with-trailing-x ::= (dimension `x`)*
ParseResult
Parser::parseDimensionListRanked(SmallVectorImpl<int64_t> &dimensions,
bool allowDynamic, bool withTrailingX) {
auto parseDim = [&]() -> LogicalResult {
auto loc = getToken().getLoc();
if (consumeIf(Token::question)) {
if (!allowDynamic)
return emitError(loc, "expected static shape");
dimensions.push_back(ShapedType::kDynamic);
} else {
int64_t value;
if (failed(parseIntegerInDimensionList(value)))
return failure();
dimensions.push_back(value);
}
return success();
};
if (withTrailingX) {
while (getToken().isAny(Token::integer, Token::question)) {
if (failed(parseDim()) || failed(parseXInDimensionList()))
return failure();
}
return success();
}
if (getToken().isAny(Token::integer, Token::question)) {
if (failed(parseDim()))
return failure();
while (getToken().is(Token::bare_identifier) &&
getTokenSpelling()[0] == 'x') {
if (failed(parseXInDimensionList()) || failed(parseDim()))
return failure();
}
}
return success();
}
ParseResult Parser::parseIntegerInDimensionList(int64_t &value) {
// Hexadecimal integer literals (starting with `0x`) are not allowed in
// aggregate type declarations. Therefore, `0xf32` should be processed as
// a sequence of separate elements `0`, `x`, `f32`.
if (getTokenSpelling().size() > 1 && getTokenSpelling()[1] == 'x') {
// We can get here only if the token is an integer literal. Hexadecimal
// integer literals can only start with `0x` (`1x` wouldn't lex as a
// literal, just `1` would, at which point we don't get into this
// branch).
assert(getTokenSpelling()[0] == '0' && "invalid integer literal");
value = 0;
state.lex.resetPointer(getTokenSpelling().data() + 1);
consumeToken();
} else {
// Make sure this integer value is in bound and valid.
std::optional<uint64_t> dimension = getToken().getUInt64IntegerValue();
if (!dimension ||
*dimension > (uint64_t)std::numeric_limits<int64_t>::max())
return emitError("invalid dimension");
value = (int64_t)*dimension;
consumeToken(Token::integer);
}
return success();
}
/// Parse an 'x' token in a dimension list, handling the case where the x is
/// juxtaposed with an element type, as in "xf32", leaving the "f32" as the next
/// token.
ParseResult Parser::parseXInDimensionList() {
if (getToken().isNot(Token::bare_identifier) || getTokenSpelling()[0] != 'x')
return emitWrongTokenError("expected 'x' in dimension list");
// If we had a prefix of 'x', lex the next token immediately after the 'x'.
if (getTokenSpelling().size() != 1)
state.lex.resetPointer(getTokenSpelling().data() + 1);
// Consume the 'x'.
consumeToken(Token::bare_identifier);
return success();
}
|