1 //===- Parser.cpp - MLIR 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 textual form. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "Parser.h" 14 #include "mlir/IR/AffineMap.h" 15 #include "mlir/IR/BuiltinOps.h" 16 #include "mlir/IR/Dialect.h" 17 #include "mlir/IR/Verifier.h" 18 #include "mlir/Parser.h" 19 #include "mlir/Parser/AsmParserState.h" 20 #include "llvm/ADT/DenseMap.h" 21 #include "llvm/ADT/StringSet.h" 22 #include "llvm/ADT/bit.h" 23 #include "llvm/Support/PrettyStackTrace.h" 24 #include "llvm/Support/SourceMgr.h" 25 #include <algorithm> 26 27 using namespace mlir; 28 using namespace mlir::detail; 29 using llvm::MemoryBuffer; 30 using llvm::SMLoc; 31 using llvm::SourceMgr; 32 33 //===----------------------------------------------------------------------===// 34 // Parser 35 //===----------------------------------------------------------------------===// 36 37 /// Parse a comma separated list of elements that must have at least one entry 38 /// in it. 39 ParseResult 40 Parser::parseCommaSeparatedList(function_ref<ParseResult()> parseElement) { 41 // Non-empty case starts with an element. 42 if (parseElement()) 43 return failure(); 44 45 // Otherwise we have a list of comma separated elements. 46 while (consumeIf(Token::comma)) { 47 if (parseElement()) 48 return failure(); 49 } 50 return success(); 51 } 52 53 /// Parse a comma-separated list of elements, terminated with an arbitrary 54 /// token. This allows empty lists if allowEmptyList is true. 55 /// 56 /// abstract-list ::= rightToken // if allowEmptyList == true 57 /// abstract-list ::= element (',' element)* rightToken 58 /// 59 ParseResult 60 Parser::parseCommaSeparatedListUntil(Token::Kind rightToken, 61 function_ref<ParseResult()> parseElement, 62 bool allowEmptyList) { 63 // Handle the empty case. 64 if (getToken().is(rightToken)) { 65 if (!allowEmptyList) 66 return emitError("expected list element"); 67 consumeToken(rightToken); 68 return success(); 69 } 70 71 if (parseCommaSeparatedList(parseElement) || 72 parseToken(rightToken, "expected ',' or '" + 73 Token::getTokenSpelling(rightToken) + "'")) 74 return failure(); 75 76 return success(); 77 } 78 79 InFlightDiagnostic Parser::emitError(SMLoc loc, const Twine &message) { 80 auto diag = mlir::emitError(getEncodedSourceLocation(loc), message); 81 82 // If we hit a parse error in response to a lexer error, then the lexer 83 // already reported the error. 84 if (getToken().is(Token::error)) 85 diag.abandon(); 86 return diag; 87 } 88 89 /// Consume the specified token if present and return success. On failure, 90 /// output a diagnostic and return failure. 91 ParseResult Parser::parseToken(Token::Kind expectedToken, 92 const Twine &message) { 93 if (consumeIf(expectedToken)) 94 return success(); 95 return emitError(message); 96 } 97 98 /// Parse an optional integer value from the stream. 99 OptionalParseResult Parser::parseOptionalInteger(APInt &result) { 100 Token curToken = getToken(); 101 if (curToken.isNot(Token::integer, Token::minus)) 102 return llvm::None; 103 104 bool negative = consumeIf(Token::minus); 105 Token curTok = getToken(); 106 if (parseToken(Token::integer, "expected integer value")) 107 return failure(); 108 109 StringRef spelling = curTok.getSpelling(); 110 bool isHex = spelling.size() > 1 && spelling[1] == 'x'; 111 if (spelling.getAsInteger(isHex ? 0 : 10, result)) 112 return emitError(curTok.getLoc(), "integer value too large"); 113 114 // Make sure we have a zero at the top so we return the right signedness. 115 if (result.isNegative()) 116 result = result.zext(result.getBitWidth() + 1); 117 118 // Process the negative sign if present. 119 if (negative) 120 result.negate(); 121 122 return success(); 123 } 124 125 /// Parse a floating point value from an integer literal token. 126 ParseResult Parser::parseFloatFromIntegerLiteral( 127 Optional<APFloat> &result, const Token &tok, bool isNegative, 128 const llvm::fltSemantics &semantics, size_t typeSizeInBits) { 129 llvm::SMLoc loc = tok.getLoc(); 130 StringRef spelling = tok.getSpelling(); 131 bool isHex = spelling.size() > 1 && spelling[1] == 'x'; 132 if (!isHex) { 133 return emitError(loc, "unexpected decimal integer literal for a " 134 "floating point value") 135 .attachNote() 136 << "add a trailing dot to make the literal a float"; 137 } 138 if (isNegative) { 139 return emitError(loc, "hexadecimal float literal should not have a " 140 "leading minus"); 141 } 142 143 Optional<uint64_t> value = tok.getUInt64IntegerValue(); 144 if (!value.hasValue()) 145 return emitError(loc, "hexadecimal float constant out of range for type"); 146 147 if (&semantics == &APFloat::IEEEdouble()) { 148 result = APFloat(semantics, APInt(typeSizeInBits, *value)); 149 return success(); 150 } 151 152 APInt apInt(typeSizeInBits, *value); 153 if (apInt != *value) 154 return emitError(loc, "hexadecimal float constant out of range for type"); 155 result = APFloat(semantics, apInt); 156 157 return success(); 158 } 159 160 //===----------------------------------------------------------------------===// 161 // OperationParser 162 //===----------------------------------------------------------------------===// 163 164 namespace { 165 /// This class provides support for parsing operations and regions of 166 /// operations. 167 class OperationParser : public Parser { 168 public: 169 OperationParser(ParserState &state, ModuleOp topLevelOp); 170 ~OperationParser(); 171 172 /// After parsing is finished, this function must be called to see if there 173 /// are any remaining issues. 174 ParseResult finalize(); 175 176 //===--------------------------------------------------------------------===// 177 // SSA Value Handling 178 //===--------------------------------------------------------------------===// 179 180 /// This represents a use of an SSA value in the program. The first two 181 /// entries in the tuple are the name and result number of a reference. The 182 /// third is the location of the reference, which is used in case this ends 183 /// up being a use of an undefined value. 184 struct SSAUseInfo { 185 StringRef name; // Value name, e.g. %42 or %abc 186 unsigned number; // Number, specified with #12 187 SMLoc loc; // Location of first definition or use. 188 }; 189 190 /// Push a new SSA name scope to the parser. 191 void pushSSANameScope(bool isIsolated); 192 193 /// Pop the last SSA name scope from the parser. 194 ParseResult popSSANameScope(); 195 196 /// Register a definition of a value with the symbol table. 197 ParseResult addDefinition(SSAUseInfo useInfo, Value value); 198 199 /// Parse an optional list of SSA uses into 'results'. 200 ParseResult parseOptionalSSAUseList(SmallVectorImpl<SSAUseInfo> &results); 201 202 /// Parse a single SSA use into 'result'. 203 ParseResult parseSSAUse(SSAUseInfo &result); 204 205 /// Given a reference to an SSA value and its type, return a reference. This 206 /// returns null on failure. 207 Value resolveSSAUse(SSAUseInfo useInfo, Type type); 208 209 ParseResult 210 parseSSADefOrUseAndType(function_ref<ParseResult(SSAUseInfo, Type)> action); 211 212 ParseResult parseOptionalSSAUseAndTypeList(SmallVectorImpl<Value> &results); 213 214 /// Return the location of the value identified by its name and number if it 215 /// has been already reference. 216 Optional<SMLoc> getReferenceLoc(StringRef name, unsigned number) { 217 auto &values = isolatedNameScopes.back().values; 218 if (!values.count(name) || number >= values[name].size()) 219 return {}; 220 if (values[name][number].value) 221 return values[name][number].loc; 222 return {}; 223 } 224 225 //===--------------------------------------------------------------------===// 226 // Operation Parsing 227 //===--------------------------------------------------------------------===// 228 229 /// Parse an operation instance. 230 ParseResult parseOperation(); 231 232 /// Parse a single operation successor. 233 ParseResult parseSuccessor(Block *&dest); 234 235 /// Parse a comma-separated list of operation successors in brackets. 236 ParseResult parseSuccessors(SmallVectorImpl<Block *> &destinations); 237 238 /// Parse an operation instance that is in the generic form. 239 Operation *parseGenericOperation(); 240 241 /// Parse an operation instance that is in the generic form and insert it at 242 /// the provided insertion point. 243 Operation *parseGenericOperation(Block *insertBlock, 244 Block::iterator insertPt); 245 246 /// This type is used to keep track of things that are either an Operation or 247 /// a BlockArgument. We cannot use Value for this, because not all Operations 248 /// have results. 249 using OpOrArgument = llvm::PointerUnion<Operation *, BlockArgument>; 250 251 /// Parse an optional trailing location and add it to the specifier Operation 252 /// or `OperandType` if present. 253 /// 254 /// trailing-location ::= (`loc` (`(` location `)` | attribute-alias))? 255 /// 256 ParseResult parseTrailingLocationSpecifier(OpOrArgument opOrArgument); 257 258 /// This is the structure of a result specifier in the assembly syntax, 259 /// including the name, number of results, and location. 260 using ResultRecord = std::tuple<StringRef, unsigned, SMLoc>; 261 262 /// Parse an operation instance that is in the op-defined custom form. 263 /// resultInfo specifies information about the "%name =" specifiers. 264 Operation *parseCustomOperation(ArrayRef<ResultRecord> resultIDs); 265 266 //===--------------------------------------------------------------------===// 267 // Region Parsing 268 //===--------------------------------------------------------------------===// 269 270 /// Parse a region into 'region' with the provided entry block arguments. 271 /// 'isIsolatedNameScope' indicates if the naming scope of this region is 272 /// isolated from those above. 273 ParseResult parseRegion(Region ®ion, 274 ArrayRef<std::pair<SSAUseInfo, Type>> entryArguments, 275 bool isIsolatedNameScope = false); 276 277 /// Parse a region body into 'region'. 278 ParseResult 279 parseRegionBody(Region ®ion, llvm::SMLoc startLoc, 280 ArrayRef<std::pair<SSAUseInfo, Type>> entryArguments, 281 bool isIsolatedNameScope); 282 283 //===--------------------------------------------------------------------===// 284 // Block Parsing 285 //===--------------------------------------------------------------------===// 286 287 /// Parse a new block into 'block'. 288 ParseResult parseBlock(Block *&block); 289 290 /// Parse a list of operations into 'block'. 291 ParseResult parseBlockBody(Block *block); 292 293 /// Parse a (possibly empty) list of block arguments. 294 ParseResult parseOptionalBlockArgList(Block *owner); 295 296 /// Get the block with the specified name, creating it if it doesn't 297 /// already exist. The location specified is the point of use, which allows 298 /// us to diagnose references to blocks that are not defined precisely. 299 Block *getBlockNamed(StringRef name, SMLoc loc); 300 301 /// Define the block with the specified name. Returns the Block* or nullptr in 302 /// the case of redefinition. 303 Block *defineBlockNamed(StringRef name, SMLoc loc, Block *existing); 304 305 private: 306 /// This class represents a definition of a Block. 307 struct BlockDefinition { 308 /// A pointer to the defined Block. 309 Block *block; 310 /// The location that the Block was defined at. 311 SMLoc loc; 312 }; 313 /// This class represents a definition of a Value. 314 struct ValueDefinition { 315 /// A pointer to the defined Value. 316 Value value; 317 /// The location that the Value was defined at. 318 SMLoc loc; 319 }; 320 321 /// Returns the info for a block at the current scope for the given name. 322 BlockDefinition &getBlockInfoByName(StringRef name) { 323 return blocksByName.back()[name]; 324 } 325 326 /// Insert a new forward reference to the given block. 327 void insertForwardRef(Block *block, SMLoc loc) { 328 forwardRef.back().try_emplace(block, loc); 329 } 330 331 /// Erase any forward reference to the given block. 332 bool eraseForwardRef(Block *block) { return forwardRef.back().erase(block); } 333 334 /// Record that a definition was added at the current scope. 335 void recordDefinition(StringRef def); 336 337 /// Get the value entry for the given SSA name. 338 SmallVectorImpl<ValueDefinition> &getSSAValueEntry(StringRef name); 339 340 /// Create a forward reference placeholder value with the given location and 341 /// result type. 342 Value createForwardRefPlaceholder(SMLoc loc, Type type); 343 344 /// Return true if this is a forward reference. 345 bool isForwardRefPlaceholder(Value value) { 346 return forwardRefPlaceholders.count(value); 347 } 348 349 /// This struct represents an isolated SSA name scope. This scope may contain 350 /// other nested non-isolated scopes. These scopes are used for operations 351 /// that are known to be isolated to allow for reusing names within their 352 /// regions, even if those names are used above. 353 struct IsolatedSSANameScope { 354 /// Record that a definition was added at the current scope. 355 void recordDefinition(StringRef def) { 356 definitionsPerScope.back().insert(def); 357 } 358 359 /// Push a nested name scope. 360 void pushSSANameScope() { definitionsPerScope.push_back({}); } 361 362 /// Pop a nested name scope. 363 void popSSANameScope() { 364 for (auto &def : definitionsPerScope.pop_back_val()) 365 values.erase(def.getKey()); 366 } 367 368 /// This keeps track of all of the SSA values we are tracking for each name 369 /// scope, indexed by their name. This has one entry per result number. 370 llvm::StringMap<SmallVector<ValueDefinition, 1>> values; 371 372 /// This keeps track of all of the values defined by a specific name scope. 373 SmallVector<llvm::StringSet<>, 2> definitionsPerScope; 374 }; 375 376 /// A list of isolated name scopes. 377 SmallVector<IsolatedSSANameScope, 2> isolatedNameScopes; 378 379 /// This keeps track of the block names as well as the location of the first 380 /// reference for each nested name scope. This is used to diagnose invalid 381 /// block references and memorize them. 382 SmallVector<DenseMap<StringRef, BlockDefinition>, 2> blocksByName; 383 SmallVector<DenseMap<Block *, SMLoc>, 2> forwardRef; 384 385 /// These are all of the placeholders we've made along with the location of 386 /// their first reference, to allow checking for use of undefined values. 387 DenseMap<Value, SMLoc> forwardRefPlaceholders; 388 389 /// A set of operations whose locations reference aliases that have yet to 390 /// be resolved. 391 SmallVector<std::pair<OpOrArgument, Token>, 8> 392 opsAndArgumentsWithDeferredLocs; 393 394 /// The builder used when creating parsed operation instances. 395 OpBuilder opBuilder; 396 397 /// The top level operation that holds all of the parsed operations. 398 Operation *topLevelOp; 399 }; 400 } // end anonymous namespace 401 402 OperationParser::OperationParser(ParserState &state, ModuleOp topLevelOp) 403 : Parser(state), opBuilder(topLevelOp.getRegion()), topLevelOp(topLevelOp) { 404 // The top level operation starts a new name scope. 405 pushSSANameScope(/*isIsolated=*/true); 406 407 // If we are populating the parser state, prepare it for parsing. 408 if (state.asmState) 409 state.asmState->initialize(topLevelOp); 410 } 411 412 OperationParser::~OperationParser() { 413 for (auto &fwd : forwardRefPlaceholders) { 414 // Drop all uses of undefined forward declared reference and destroy 415 // defining operation. 416 fwd.first.dropAllUses(); 417 fwd.first.getDefiningOp()->destroy(); 418 } 419 for (const auto &scope : forwardRef) { 420 for (const auto &fwd : scope) { 421 // Delete all blocks that were created as forward references but never 422 // included into a region. 423 fwd.first->dropAllUses(); 424 delete fwd.first; 425 } 426 } 427 } 428 429 /// After parsing is finished, this function must be called to see if there are 430 /// any remaining issues. 431 ParseResult OperationParser::finalize() { 432 // Check for any forward references that are left. If we find any, error 433 // out. 434 if (!forwardRefPlaceholders.empty()) { 435 SmallVector<const char *, 4> errors; 436 // Iteration over the map isn't deterministic, so sort by source location. 437 for (auto entry : forwardRefPlaceholders) 438 errors.push_back(entry.second.getPointer()); 439 llvm::array_pod_sort(errors.begin(), errors.end()); 440 441 for (auto entry : errors) { 442 auto loc = SMLoc::getFromPointer(entry); 443 emitError(loc, "use of undeclared SSA value name"); 444 } 445 return failure(); 446 } 447 448 // Resolve the locations of any deferred operations. 449 auto &attributeAliases = state.symbols.attributeAliasDefinitions; 450 for (std::pair<OpOrArgument, Token> &it : opsAndArgumentsWithDeferredLocs) { 451 llvm::SMLoc tokLoc = it.second.getLoc(); 452 StringRef identifier = it.second.getSpelling().drop_front(); 453 Attribute attr = attributeAliases.lookup(identifier); 454 if (!attr) 455 return emitError(tokLoc) << "operation location alias was never defined"; 456 457 LocationAttr locAttr = attr.dyn_cast<LocationAttr>(); 458 if (!locAttr) 459 return emitError(tokLoc) 460 << "expected location, but found '" << attr << "'"; 461 auto opOrArgument = it.first; 462 if (auto *op = opOrArgument.dyn_cast<Operation *>()) 463 op->setLoc(locAttr); 464 else 465 opOrArgument.get<BlockArgument>().setLoc(locAttr); 466 } 467 468 // Pop the top level name scope. 469 if (failed(popSSANameScope())) 470 return failure(); 471 472 // Verify that the parsed operations are valid. 473 if (failed(verify(topLevelOp))) 474 return failure(); 475 476 // If we are populating the parser state, finalize the top-level operation. 477 if (state.asmState) 478 state.asmState->finalize(topLevelOp); 479 return success(); 480 } 481 482 //===----------------------------------------------------------------------===// 483 // SSA Value Handling 484 //===----------------------------------------------------------------------===// 485 486 void OperationParser::pushSSANameScope(bool isIsolated) { 487 blocksByName.push_back(DenseMap<StringRef, BlockDefinition>()); 488 forwardRef.push_back(DenseMap<Block *, SMLoc>()); 489 490 // Push back a new name definition scope. 491 if (isIsolated) 492 isolatedNameScopes.push_back({}); 493 isolatedNameScopes.back().pushSSANameScope(); 494 } 495 496 ParseResult OperationParser::popSSANameScope() { 497 auto forwardRefInCurrentScope = forwardRef.pop_back_val(); 498 499 // Verify that all referenced blocks were defined. 500 if (!forwardRefInCurrentScope.empty()) { 501 SmallVector<std::pair<const char *, Block *>, 4> errors; 502 // Iteration over the map isn't deterministic, so sort by source location. 503 for (auto entry : forwardRefInCurrentScope) { 504 errors.push_back({entry.second.getPointer(), entry.first}); 505 // Add this block to the top-level region to allow for automatic cleanup. 506 topLevelOp->getRegion(0).push_back(entry.first); 507 } 508 llvm::array_pod_sort(errors.begin(), errors.end()); 509 510 for (auto entry : errors) { 511 auto loc = SMLoc::getFromPointer(entry.first); 512 emitError(loc, "reference to an undefined block"); 513 } 514 return failure(); 515 } 516 517 // Pop the next nested namescope. If there is only one internal namescope, 518 // just pop the isolated scope. 519 auto ¤tNameScope = isolatedNameScopes.back(); 520 if (currentNameScope.definitionsPerScope.size() == 1) 521 isolatedNameScopes.pop_back(); 522 else 523 currentNameScope.popSSANameScope(); 524 525 blocksByName.pop_back(); 526 return success(); 527 } 528 529 /// Register a definition of a value with the symbol table. 530 ParseResult OperationParser::addDefinition(SSAUseInfo useInfo, Value value) { 531 auto &entries = getSSAValueEntry(useInfo.name); 532 533 // Make sure there is a slot for this value. 534 if (entries.size() <= useInfo.number) 535 entries.resize(useInfo.number + 1); 536 537 // If we already have an entry for this, check to see if it was a definition 538 // or a forward reference. 539 if (auto existing = entries[useInfo.number].value) { 540 if (!isForwardRefPlaceholder(existing)) { 541 return emitError(useInfo.loc) 542 .append("redefinition of SSA value '", useInfo.name, "'") 543 .attachNote(getEncodedSourceLocation(entries[useInfo.number].loc)) 544 .append("previously defined here"); 545 } 546 547 if (existing.getType() != value.getType()) { 548 return emitError(useInfo.loc) 549 .append("definition of SSA value '", useInfo.name, "#", 550 useInfo.number, "' has type ", value.getType()) 551 .attachNote(getEncodedSourceLocation(entries[useInfo.number].loc)) 552 .append("previously used here with type ", existing.getType()); 553 } 554 555 // If it was a forward reference, update everything that used it to use 556 // the actual definition instead, delete the forward ref, and remove it 557 // from our set of forward references we track. 558 existing.replaceAllUsesWith(value); 559 existing.getDefiningOp()->destroy(); 560 forwardRefPlaceholders.erase(existing); 561 562 // If a definition of the value already exists, replace it in the assembly 563 // state. 564 if (state.asmState) 565 state.asmState->refineDefinition(existing, value); 566 } 567 568 /// Record this definition for the current scope. 569 entries[useInfo.number] = {value, useInfo.loc}; 570 recordDefinition(useInfo.name); 571 return success(); 572 } 573 574 /// Parse a (possibly empty) list of SSA operands. 575 /// 576 /// ssa-use-list ::= ssa-use (`,` ssa-use)* 577 /// ssa-use-list-opt ::= ssa-use-list? 578 /// 579 ParseResult 580 OperationParser::parseOptionalSSAUseList(SmallVectorImpl<SSAUseInfo> &results) { 581 if (getToken().isNot(Token::percent_identifier)) 582 return success(); 583 return parseCommaSeparatedList([&]() -> ParseResult { 584 SSAUseInfo result; 585 if (parseSSAUse(result)) 586 return failure(); 587 results.push_back(result); 588 return success(); 589 }); 590 } 591 592 /// Parse a SSA operand for an operation. 593 /// 594 /// ssa-use ::= ssa-id 595 /// 596 ParseResult OperationParser::parseSSAUse(SSAUseInfo &result) { 597 result.name = getTokenSpelling(); 598 result.number = 0; 599 result.loc = getToken().getLoc(); 600 if (parseToken(Token::percent_identifier, "expected SSA operand")) 601 return failure(); 602 603 // If we have an attribute ID, it is a result number. 604 if (getToken().is(Token::hash_identifier)) { 605 if (auto value = getToken().getHashIdentifierNumber()) 606 result.number = value.getValue(); 607 else 608 return emitError("invalid SSA value result number"); 609 consumeToken(Token::hash_identifier); 610 } 611 612 return success(); 613 } 614 615 /// Given an unbound reference to an SSA value and its type, return the value 616 /// it specifies. This returns null on failure. 617 Value OperationParser::resolveSSAUse(SSAUseInfo useInfo, Type type) { 618 auto &entries = getSSAValueEntry(useInfo.name); 619 620 // Functor used to record the use of the given value if the assembly state 621 // field is populated. 622 auto maybeRecordUse = [&](Value value) { 623 if (state.asmState) 624 state.asmState->addUses(value, useInfo.loc); 625 return value; 626 }; 627 628 // If we have already seen a value of this name, return it. 629 if (useInfo.number < entries.size() && entries[useInfo.number].value) { 630 Value result = entries[useInfo.number].value; 631 // Check that the type matches the other uses. 632 if (result.getType() == type) 633 return maybeRecordUse(result); 634 635 emitError(useInfo.loc, "use of value '") 636 .append(useInfo.name, 637 "' expects different type than prior uses: ", type, " vs ", 638 result.getType()) 639 .attachNote(getEncodedSourceLocation(entries[useInfo.number].loc)) 640 .append("prior use here"); 641 return nullptr; 642 } 643 644 // Make sure we have enough slots for this. 645 if (entries.size() <= useInfo.number) 646 entries.resize(useInfo.number + 1); 647 648 // If the value has already been defined and this is an overly large result 649 // number, diagnose that. 650 if (entries[0].value && !isForwardRefPlaceholder(entries[0].value)) 651 return (emitError(useInfo.loc, "reference to invalid result number"), 652 nullptr); 653 654 // Otherwise, this is a forward reference. Create a placeholder and remember 655 // that we did so. 656 Value result = createForwardRefPlaceholder(useInfo.loc, type); 657 entries[useInfo.number] = {result, useInfo.loc}; 658 return maybeRecordUse(result); 659 } 660 661 /// Parse an SSA use with an associated type. 662 /// 663 /// ssa-use-and-type ::= ssa-use `:` type 664 ParseResult OperationParser::parseSSADefOrUseAndType( 665 function_ref<ParseResult(SSAUseInfo, Type)> action) { 666 SSAUseInfo useInfo; 667 if (parseSSAUse(useInfo) || 668 parseToken(Token::colon, "expected ':' and type for SSA operand")) 669 return failure(); 670 671 auto type = parseType(); 672 if (!type) 673 return failure(); 674 675 return action(useInfo, type); 676 } 677 678 /// Parse a (possibly empty) list of SSA operands, followed by a colon, then 679 /// followed by a type list. 680 /// 681 /// ssa-use-and-type-list 682 /// ::= ssa-use-list ':' type-list-no-parens 683 /// 684 ParseResult OperationParser::parseOptionalSSAUseAndTypeList( 685 SmallVectorImpl<Value> &results) { 686 SmallVector<SSAUseInfo, 4> valueIDs; 687 if (parseOptionalSSAUseList(valueIDs)) 688 return failure(); 689 690 // If there were no operands, then there is no colon or type lists. 691 if (valueIDs.empty()) 692 return success(); 693 694 SmallVector<Type, 4> types; 695 if (parseToken(Token::colon, "expected ':' in operand list") || 696 parseTypeListNoParens(types)) 697 return failure(); 698 699 if (valueIDs.size() != types.size()) 700 return emitError("expected ") 701 << valueIDs.size() << " types to match operand list"; 702 703 results.reserve(valueIDs.size()); 704 for (unsigned i = 0, e = valueIDs.size(); i != e; ++i) { 705 if (auto value = resolveSSAUse(valueIDs[i], types[i])) 706 results.push_back(value); 707 else 708 return failure(); 709 } 710 711 return success(); 712 } 713 714 /// Record that a definition was added at the current scope. 715 void OperationParser::recordDefinition(StringRef def) { 716 isolatedNameScopes.back().recordDefinition(def); 717 } 718 719 /// Get the value entry for the given SSA name. 720 auto OperationParser::getSSAValueEntry(StringRef name) 721 -> SmallVectorImpl<ValueDefinition> & { 722 return isolatedNameScopes.back().values[name]; 723 } 724 725 /// Create and remember a new placeholder for a forward reference. 726 Value OperationParser::createForwardRefPlaceholder(SMLoc loc, Type type) { 727 // Forward references are always created as operations, because we just need 728 // something with a def/use chain. 729 // 730 // We create these placeholders as having an empty name, which we know 731 // cannot be created through normal user input, allowing us to distinguish 732 // them. 733 auto name = OperationName("builtin.unrealized_conversion_cast", getContext()); 734 auto *op = Operation::create( 735 getEncodedSourceLocation(loc), name, type, /*operands=*/{}, 736 /*attributes=*/llvm::None, /*successors=*/{}, /*numRegions=*/0); 737 forwardRefPlaceholders[op->getResult(0)] = loc; 738 return op->getResult(0); 739 } 740 741 //===----------------------------------------------------------------------===// 742 // Operation Parsing 743 //===----------------------------------------------------------------------===// 744 745 /// Parse an operation. 746 /// 747 /// operation ::= op-result-list? 748 /// (generic-operation | custom-operation) 749 /// trailing-location? 750 /// generic-operation ::= string-literal `(` ssa-use-list? `)` 751 /// successor-list? (`(` region-list `)`)? 752 /// attribute-dict? `:` function-type 753 /// custom-operation ::= bare-id custom-operation-format 754 /// op-result-list ::= op-result (`,` op-result)* `=` 755 /// op-result ::= ssa-id (`:` integer-literal) 756 /// 757 ParseResult OperationParser::parseOperation() { 758 auto loc = getToken().getLoc(); 759 SmallVector<ResultRecord, 1> resultIDs; 760 size_t numExpectedResults = 0; 761 if (getToken().is(Token::percent_identifier)) { 762 // Parse the group of result ids. 763 auto parseNextResult = [&]() -> ParseResult { 764 // Parse the next result id. 765 if (!getToken().is(Token::percent_identifier)) 766 return emitError("expected valid ssa identifier"); 767 768 Token nameTok = getToken(); 769 consumeToken(Token::percent_identifier); 770 771 // If the next token is a ':', we parse the expected result count. 772 size_t expectedSubResults = 1; 773 if (consumeIf(Token::colon)) { 774 // Check that the next token is an integer. 775 if (!getToken().is(Token::integer)) 776 return emitError("expected integer number of results"); 777 778 // Check that number of results is > 0. 779 auto val = getToken().getUInt64IntegerValue(); 780 if (!val.hasValue() || val.getValue() < 1) 781 return emitError("expected named operation to have atleast 1 result"); 782 consumeToken(Token::integer); 783 expectedSubResults = *val; 784 } 785 786 resultIDs.emplace_back(nameTok.getSpelling(), expectedSubResults, 787 nameTok.getLoc()); 788 numExpectedResults += expectedSubResults; 789 return success(); 790 }; 791 if (parseCommaSeparatedList(parseNextResult)) 792 return failure(); 793 794 if (parseToken(Token::equal, "expected '=' after SSA name")) 795 return failure(); 796 } 797 798 Operation *op; 799 Token nameTok = getToken(); 800 if (nameTok.is(Token::bare_identifier) || nameTok.isKeyword()) 801 op = parseCustomOperation(resultIDs); 802 else if (nameTok.is(Token::string)) 803 op = parseGenericOperation(); 804 else 805 return emitError("expected operation name in quotes"); 806 807 // If parsing of the basic operation failed, then this whole thing fails. 808 if (!op) 809 return failure(); 810 811 // If the operation had a name, register it. 812 if (!resultIDs.empty()) { 813 if (op->getNumResults() == 0) 814 return emitError(loc, "cannot name an operation with no results"); 815 if (numExpectedResults != op->getNumResults()) 816 return emitError(loc, "operation defines ") 817 << op->getNumResults() << " results but was provided " 818 << numExpectedResults << " to bind"; 819 820 // Add this operation to the assembly state if it was provided to populate. 821 if (state.asmState) { 822 unsigned resultIt = 0; 823 SmallVector<std::pair<unsigned, llvm::SMLoc>> asmResultGroups; 824 asmResultGroups.reserve(resultIDs.size()); 825 for (ResultRecord &record : resultIDs) { 826 asmResultGroups.emplace_back(resultIt, std::get<2>(record)); 827 resultIt += std::get<1>(record); 828 } 829 state.asmState->finalizeOperationDefinition( 830 op, nameTok.getLocRange(), /*endLoc=*/getToken().getLoc(), 831 asmResultGroups); 832 } 833 834 // Add definitions for each of the result groups. 835 unsigned opResI = 0; 836 for (ResultRecord &resIt : resultIDs) { 837 for (unsigned subRes : llvm::seq<unsigned>(0, std::get<1>(resIt))) { 838 if (addDefinition({std::get<0>(resIt), subRes, std::get<2>(resIt)}, 839 op->getResult(opResI++))) 840 return failure(); 841 } 842 } 843 844 // Add this operation to the assembly state if it was provided to populate. 845 } else if (state.asmState) { 846 state.asmState->finalizeOperationDefinition(op, nameTok.getLocRange(), 847 /*endLoc=*/getToken().getLoc()); 848 } 849 850 return success(); 851 } 852 853 /// Parse a single operation successor. 854 /// 855 /// successor ::= block-id 856 /// 857 ParseResult OperationParser::parseSuccessor(Block *&dest) { 858 // Verify branch is identifier and get the matching block. 859 if (!getToken().is(Token::caret_identifier)) 860 return emitError("expected block name"); 861 dest = getBlockNamed(getTokenSpelling(), getToken().getLoc()); 862 consumeToken(); 863 return success(); 864 } 865 866 /// Parse a comma-separated list of operation successors in brackets. 867 /// 868 /// successor-list ::= `[` successor (`,` successor )* `]` 869 /// 870 ParseResult 871 OperationParser::parseSuccessors(SmallVectorImpl<Block *> &destinations) { 872 if (parseToken(Token::l_square, "expected '['")) 873 return failure(); 874 875 auto parseElt = [this, &destinations] { 876 Block *dest; 877 ParseResult res = parseSuccessor(dest); 878 destinations.push_back(dest); 879 return res; 880 }; 881 return parseCommaSeparatedListUntil(Token::r_square, parseElt, 882 /*allowEmptyList=*/false); 883 } 884 885 namespace { 886 // RAII-style guard for cleaning up the regions in the operation state before 887 // deleting them. Within the parser, regions may get deleted if parsing failed, 888 // and other errors may be present, in particular undominated uses. This makes 889 // sure such uses are deleted. 890 struct CleanupOpStateRegions { 891 ~CleanupOpStateRegions() { 892 SmallVector<Region *, 4> regionsToClean; 893 regionsToClean.reserve(state.regions.size()); 894 for (auto ®ion : state.regions) 895 if (region) 896 for (auto &block : *region) 897 block.dropAllDefinedValueUses(); 898 } 899 OperationState &state; 900 }; 901 } // namespace 902 903 Operation *OperationParser::parseGenericOperation() { 904 // Get location information for the operation. 905 auto srcLocation = getEncodedSourceLocation(getToken().getLoc()); 906 907 std::string name = getToken().getStringValue(); 908 if (name.empty()) 909 return (emitError("empty operation name is invalid"), nullptr); 910 if (name.find('\0') != StringRef::npos) 911 return (emitError("null character not allowed in operation name"), nullptr); 912 913 consumeToken(Token::string); 914 915 OperationState result(srcLocation, name); 916 917 // Lazy load dialects in the context as needed. 918 if (!result.name.getAbstractOperation()) { 919 StringRef dialectName = StringRef(name).split('.').first; 920 if (!getContext()->getLoadedDialect(dialectName) && 921 getContext()->getOrLoadDialect(dialectName)) { 922 result.name = OperationName(name, getContext()); 923 } 924 } 925 926 // If we are populating the parser state, start a new operation definition. 927 if (state.asmState) 928 state.asmState->startOperationDefinition(result.name); 929 930 // Parse the operand list. 931 SmallVector<SSAUseInfo, 8> operandInfos; 932 if (parseToken(Token::l_paren, "expected '(' to start operand list") || 933 parseOptionalSSAUseList(operandInfos) || 934 parseToken(Token::r_paren, "expected ')' to end operand list")) { 935 return nullptr; 936 } 937 938 // Parse the successor list. 939 if (getToken().is(Token::l_square)) { 940 // Check if the operation is a known terminator. 941 const AbstractOperation *abstractOp = result.name.getAbstractOperation(); 942 if (abstractOp && !abstractOp->hasTrait<OpTrait::IsTerminator>()) 943 return emitError("successors in non-terminator"), nullptr; 944 945 SmallVector<Block *, 2> successors; 946 if (parseSuccessors(successors)) 947 return nullptr; 948 result.addSuccessors(successors); 949 } 950 951 // Parse the region list. 952 CleanupOpStateRegions guard{result}; 953 if (consumeIf(Token::l_paren)) { 954 do { 955 // Create temporary regions with the top level region as parent. 956 result.regions.emplace_back(new Region(topLevelOp)); 957 if (parseRegion(*result.regions.back(), /*entryArguments=*/{})) 958 return nullptr; 959 } while (consumeIf(Token::comma)); 960 if (parseToken(Token::r_paren, "expected ')' to end region list")) 961 return nullptr; 962 } 963 964 if (getToken().is(Token::l_brace)) { 965 if (parseAttributeDict(result.attributes)) 966 return nullptr; 967 } 968 969 if (parseToken(Token::colon, "expected ':' followed by operation type")) 970 return nullptr; 971 972 auto typeLoc = getToken().getLoc(); 973 auto type = parseType(); 974 if (!type) 975 return nullptr; 976 auto fnType = type.dyn_cast<FunctionType>(); 977 if (!fnType) 978 return (emitError(typeLoc, "expected function type"), nullptr); 979 980 result.addTypes(fnType.getResults()); 981 982 // Check that we have the right number of types for the operands. 983 auto operandTypes = fnType.getInputs(); 984 if (operandTypes.size() != operandInfos.size()) { 985 auto plural = "s"[operandInfos.size() == 1]; 986 return (emitError(typeLoc, "expected ") 987 << operandInfos.size() << " operand type" << plural 988 << " but had " << operandTypes.size(), 989 nullptr); 990 } 991 992 // Resolve all of the operands. 993 for (unsigned i = 0, e = operandInfos.size(); i != e; ++i) { 994 result.operands.push_back(resolveSSAUse(operandInfos[i], operandTypes[i])); 995 if (!result.operands.back()) 996 return nullptr; 997 } 998 999 // Create the operation and try to parse a location for it. 1000 Operation *op = opBuilder.createOperation(result); 1001 if (parseTrailingLocationSpecifier(op)) 1002 return nullptr; 1003 return op; 1004 } 1005 1006 Operation *OperationParser::parseGenericOperation(Block *insertBlock, 1007 Block::iterator insertPt) { 1008 Token nameToken = getToken(); 1009 1010 OpBuilder::InsertionGuard restoreInsertionPoint(opBuilder); 1011 opBuilder.setInsertionPoint(insertBlock, insertPt); 1012 Operation *op = parseGenericOperation(); 1013 if (!op) 1014 return nullptr; 1015 1016 // If we are populating the parser asm state, finalize this operation 1017 // definition. 1018 if (state.asmState) 1019 state.asmState->finalizeOperationDefinition(op, nameToken.getLocRange(), 1020 /*endLoc=*/getToken().getLoc()); 1021 return op; 1022 } 1023 1024 namespace { 1025 class CustomOpAsmParser : public OpAsmParser { 1026 public: 1027 CustomOpAsmParser( 1028 SMLoc nameLoc, ArrayRef<OperationParser::ResultRecord> resultIDs, 1029 function_ref<ParseResult(OpAsmParser &, OperationState &)> parseAssembly, 1030 bool isIsolatedFromAbove, StringRef opName, OperationParser &parser) 1031 : nameLoc(nameLoc), resultIDs(resultIDs), parseAssembly(parseAssembly), 1032 isIsolatedFromAbove(isIsolatedFromAbove), opName(opName), 1033 parser(parser) { 1034 (void)isIsolatedFromAbove; // Only used in assert, silence unused warning. 1035 } 1036 1037 /// Parse an instance of the operation described by 'opDefinition' into the 1038 /// provided operation state. 1039 ParseResult parseOperation(OperationState &opState) { 1040 if (parseAssembly(*this, opState)) 1041 return failure(); 1042 // Verify that the parsed attributes does not have duplicate attributes. 1043 // This can happen if an attribute set during parsing is also specified in 1044 // the attribute dictionary in the assembly, or the attribute is set 1045 // multiple during parsing. 1046 Optional<NamedAttribute> duplicate = opState.attributes.findDuplicate(); 1047 if (duplicate) 1048 return emitError(getNameLoc(), "attribute '") 1049 << duplicate->first 1050 << "' occurs more than once in the attribute list"; 1051 return success(); 1052 } 1053 1054 Operation *parseGenericOperation(Block *insertBlock, 1055 Block::iterator insertPt) final { 1056 return parser.parseGenericOperation(insertBlock, insertPt); 1057 } 1058 1059 //===--------------------------------------------------------------------===// 1060 // Utilities 1061 //===--------------------------------------------------------------------===// 1062 1063 /// Return if any errors were emitted during parsing. 1064 bool didEmitError() const { return emittedError; } 1065 1066 /// Emit a diagnostic at the specified location and return failure. 1067 InFlightDiagnostic emitError(llvm::SMLoc loc, const Twine &message) override { 1068 emittedError = true; 1069 return parser.emitError(loc, "custom op '" + opName + "' " + message); 1070 } 1071 1072 llvm::SMLoc getCurrentLocation() override { 1073 return parser.getToken().getLoc(); 1074 } 1075 1076 Builder &getBuilder() const override { return parser.builder; } 1077 1078 /// Return the name of the specified result in the specified syntax, as well 1079 /// as the subelement in the name. For example, in this operation: 1080 /// 1081 /// %x, %y:2, %z = foo.op 1082 /// 1083 /// getResultName(0) == {"x", 0 } 1084 /// getResultName(1) == {"y", 0 } 1085 /// getResultName(2) == {"y", 1 } 1086 /// getResultName(3) == {"z", 0 } 1087 std::pair<StringRef, unsigned> 1088 getResultName(unsigned resultNo) const override { 1089 // Scan for the resultID that contains this result number. 1090 for (unsigned nameID = 0, e = resultIDs.size(); nameID != e; ++nameID) { 1091 const auto &entry = resultIDs[nameID]; 1092 if (resultNo < std::get<1>(entry)) { 1093 // Don't pass on the leading %. 1094 StringRef name = std::get<0>(entry).drop_front(); 1095 return {name, resultNo}; 1096 } 1097 resultNo -= std::get<1>(entry); 1098 } 1099 1100 // Invalid result number. 1101 return {"", ~0U}; 1102 } 1103 1104 /// Return the number of declared SSA results. This returns 4 for the foo.op 1105 /// example in the comment for getResultName. 1106 size_t getNumResults() const override { 1107 size_t count = 0; 1108 for (auto &entry : resultIDs) 1109 count += std::get<1>(entry); 1110 return count; 1111 } 1112 1113 llvm::SMLoc getNameLoc() const override { return nameLoc; } 1114 1115 /// Re-encode the given source location as an MLIR location and return it. 1116 Location getEncodedSourceLoc(llvm::SMLoc loc) override { 1117 return parser.getEncodedSourceLocation(loc); 1118 } 1119 1120 //===--------------------------------------------------------------------===// 1121 // Token Parsing 1122 //===--------------------------------------------------------------------===// 1123 1124 /// Parse a `->` token. 1125 ParseResult parseArrow() override { 1126 return parser.parseToken(Token::arrow, "expected '->'"); 1127 } 1128 1129 /// Parses a `->` if present. 1130 ParseResult parseOptionalArrow() override { 1131 return success(parser.consumeIf(Token::arrow)); 1132 } 1133 1134 /// Parse a '{' token. 1135 ParseResult parseLBrace() override { 1136 return parser.parseToken(Token::l_brace, "expected '{'"); 1137 } 1138 1139 /// Parse a '{' token if present 1140 ParseResult parseOptionalLBrace() override { 1141 return success(parser.consumeIf(Token::l_brace)); 1142 } 1143 1144 /// Parse a `}` token. 1145 ParseResult parseRBrace() override { 1146 return parser.parseToken(Token::r_brace, "expected '}'"); 1147 } 1148 1149 /// Parse a `}` token if present 1150 ParseResult parseOptionalRBrace() override { 1151 return success(parser.consumeIf(Token::r_brace)); 1152 } 1153 1154 /// Parse a `:` token. 1155 ParseResult parseColon() override { 1156 return parser.parseToken(Token::colon, "expected ':'"); 1157 } 1158 1159 /// Parse a `:` token if present. 1160 ParseResult parseOptionalColon() override { 1161 return success(parser.consumeIf(Token::colon)); 1162 } 1163 1164 /// Parse a `,` token. 1165 ParseResult parseComma() override { 1166 return parser.parseToken(Token::comma, "expected ','"); 1167 } 1168 1169 /// Parse a `,` token if present. 1170 ParseResult parseOptionalComma() override { 1171 return success(parser.consumeIf(Token::comma)); 1172 } 1173 1174 /// Parses a `...` if present. 1175 ParseResult parseOptionalEllipsis() override { 1176 return success(parser.consumeIf(Token::ellipsis)); 1177 } 1178 1179 /// Parse a `=` token. 1180 ParseResult parseEqual() override { 1181 return parser.parseToken(Token::equal, "expected '='"); 1182 } 1183 1184 /// Parse a `=` token if present. 1185 ParseResult parseOptionalEqual() override { 1186 return success(parser.consumeIf(Token::equal)); 1187 } 1188 1189 /// Parse a '<' token. 1190 ParseResult parseLess() override { 1191 return parser.parseToken(Token::less, "expected '<'"); 1192 } 1193 1194 /// Parse a '<' token if present. 1195 ParseResult parseOptionalLess() override { 1196 return success(parser.consumeIf(Token::less)); 1197 } 1198 1199 /// Parse a '>' token. 1200 ParseResult parseGreater() override { 1201 return parser.parseToken(Token::greater, "expected '>'"); 1202 } 1203 1204 /// Parse a '>' token if present. 1205 ParseResult parseOptionalGreater() override { 1206 return success(parser.consumeIf(Token::greater)); 1207 } 1208 1209 /// Parse a `(` token. 1210 ParseResult parseLParen() override { 1211 return parser.parseToken(Token::l_paren, "expected '('"); 1212 } 1213 1214 /// Parses a '(' if present. 1215 ParseResult parseOptionalLParen() override { 1216 return success(parser.consumeIf(Token::l_paren)); 1217 } 1218 1219 /// Parse a `)` token. 1220 ParseResult parseRParen() override { 1221 return parser.parseToken(Token::r_paren, "expected ')'"); 1222 } 1223 1224 /// Parses a ')' if present. 1225 ParseResult parseOptionalRParen() override { 1226 return success(parser.consumeIf(Token::r_paren)); 1227 } 1228 1229 /// Parse a `[` token. 1230 ParseResult parseLSquare() override { 1231 return parser.parseToken(Token::l_square, "expected '['"); 1232 } 1233 1234 /// Parses a '[' if present. 1235 ParseResult parseOptionalLSquare() override { 1236 return success(parser.consumeIf(Token::l_square)); 1237 } 1238 1239 /// Parse a `]` token. 1240 ParseResult parseRSquare() override { 1241 return parser.parseToken(Token::r_square, "expected ']'"); 1242 } 1243 1244 /// Parses a ']' if present. 1245 ParseResult parseOptionalRSquare() override { 1246 return success(parser.consumeIf(Token::r_square)); 1247 } 1248 1249 /// Parses a '?' token. 1250 ParseResult parseQuestion() override { 1251 return parser.parseToken(Token::question, "expected '?'"); 1252 } 1253 1254 /// Parses a '?' token if present. 1255 ParseResult parseOptionalQuestion() override { 1256 return success(parser.consumeIf(Token::question)); 1257 } 1258 1259 /// Parses a '+' token. 1260 ParseResult parsePlus() override { 1261 return parser.parseToken(Token::plus, "expected '+'"); 1262 } 1263 1264 /// Parses a '+' token if present. 1265 ParseResult parseOptionalPlus() override { 1266 return success(parser.consumeIf(Token::plus)); 1267 } 1268 1269 /// Parses a '*' token. 1270 ParseResult parseStar() override { 1271 return parser.parseToken(Token::star, "expected '*'"); 1272 } 1273 1274 /// Parses a '*' token if present. 1275 ParseResult parseOptionalStar() override { 1276 return success(parser.consumeIf(Token::star)); 1277 } 1278 1279 /// Parse an optional integer value from the stream. 1280 OptionalParseResult parseOptionalInteger(APInt &result) override { 1281 return parser.parseOptionalInteger(result); 1282 } 1283 1284 //===--------------------------------------------------------------------===// 1285 // Attribute Parsing 1286 //===--------------------------------------------------------------------===// 1287 1288 /// Parse an arbitrary attribute of a given type and return it in result. 1289 ParseResult parseAttribute(Attribute &result, Type type) override { 1290 result = parser.parseAttribute(type); 1291 return success(static_cast<bool>(result)); 1292 } 1293 1294 /// Parse an optional attribute. 1295 template <typename AttrT> 1296 OptionalParseResult 1297 parseOptionalAttributeAndAddToList(AttrT &result, Type type, 1298 StringRef attrName, NamedAttrList &attrs) { 1299 OptionalParseResult parseResult = 1300 parser.parseOptionalAttribute(result, type); 1301 if (parseResult.hasValue() && succeeded(*parseResult)) 1302 attrs.push_back(parser.builder.getNamedAttr(attrName, result)); 1303 return parseResult; 1304 } 1305 OptionalParseResult parseOptionalAttribute(Attribute &result, Type type, 1306 StringRef attrName, 1307 NamedAttrList &attrs) override { 1308 return parseOptionalAttributeAndAddToList(result, type, attrName, attrs); 1309 } 1310 OptionalParseResult parseOptionalAttribute(ArrayAttr &result, Type type, 1311 StringRef attrName, 1312 NamedAttrList &attrs) override { 1313 return parseOptionalAttributeAndAddToList(result, type, attrName, attrs); 1314 } 1315 OptionalParseResult parseOptionalAttribute(StringAttr &result, Type type, 1316 StringRef attrName, 1317 NamedAttrList &attrs) override { 1318 return parseOptionalAttributeAndAddToList(result, type, attrName, attrs); 1319 } 1320 1321 /// Parse a named dictionary into 'result' if it is present. 1322 ParseResult parseOptionalAttrDict(NamedAttrList &result) override { 1323 if (parser.getToken().isNot(Token::l_brace)) 1324 return success(); 1325 return parser.parseAttributeDict(result); 1326 } 1327 1328 /// Parse a named dictionary into 'result' if the `attributes` keyword is 1329 /// present. 1330 ParseResult parseOptionalAttrDictWithKeyword(NamedAttrList &result) override { 1331 if (failed(parseOptionalKeyword("attributes"))) 1332 return success(); 1333 return parser.parseAttributeDict(result); 1334 } 1335 1336 /// Parse an affine map instance into 'map'. 1337 ParseResult parseAffineMap(AffineMap &map) override { 1338 return parser.parseAffineMapReference(map); 1339 } 1340 1341 /// Parse an integer set instance into 'set'. 1342 ParseResult printIntegerSet(IntegerSet &set) override { 1343 return parser.parseIntegerSetReference(set); 1344 } 1345 1346 //===--------------------------------------------------------------------===// 1347 // Identifier Parsing 1348 //===--------------------------------------------------------------------===// 1349 1350 /// Returns true if the current token corresponds to a keyword. 1351 bool isCurrentTokenAKeyword() const { 1352 return parser.getToken().is(Token::bare_identifier) || 1353 parser.getToken().isKeyword(); 1354 } 1355 1356 /// Parse the given keyword if present. 1357 ParseResult parseOptionalKeyword(StringRef keyword) override { 1358 // Check that the current token has the same spelling. 1359 if (!isCurrentTokenAKeyword() || parser.getTokenSpelling() != keyword) 1360 return failure(); 1361 parser.consumeToken(); 1362 return success(); 1363 } 1364 1365 /// Parse a keyword, if present, into 'keyword'. 1366 ParseResult parseOptionalKeyword(StringRef *keyword) override { 1367 // Check that the current token is a keyword. 1368 if (!isCurrentTokenAKeyword()) 1369 return failure(); 1370 1371 *keyword = parser.getTokenSpelling(); 1372 parser.consumeToken(); 1373 return success(); 1374 } 1375 1376 /// Parse a keyword if it is one of the 'allowedKeywords'. 1377 ParseResult 1378 parseOptionalKeyword(StringRef *keyword, 1379 ArrayRef<StringRef> allowedKeywords) override { 1380 // Check that the current token is a keyword. 1381 if (!isCurrentTokenAKeyword()) 1382 return failure(); 1383 1384 StringRef currentKeyword = parser.getTokenSpelling(); 1385 if (llvm::is_contained(allowedKeywords, currentKeyword)) { 1386 *keyword = currentKeyword; 1387 parser.consumeToken(); 1388 return success(); 1389 } 1390 1391 return failure(); 1392 } 1393 1394 /// Parse an optional @-identifier and store it (without the '@' symbol) in a 1395 /// string attribute named 'attrName'. 1396 ParseResult parseOptionalSymbolName(StringAttr &result, StringRef attrName, 1397 NamedAttrList &attrs) override { 1398 Token atToken = parser.getToken(); 1399 if (atToken.isNot(Token::at_identifier)) 1400 return failure(); 1401 1402 result = getBuilder().getStringAttr(atToken.getSymbolReference()); 1403 attrs.push_back(getBuilder().getNamedAttr(attrName, result)); 1404 parser.consumeToken(); 1405 1406 // If we are populating the assembly parser state, record this as a symbol 1407 // reference. 1408 if (parser.getState().asmState) { 1409 parser.getState().asmState->addUses( 1410 getBuilder().getSymbolRefAttr(result.getValue()), 1411 atToken.getLocRange()); 1412 } 1413 return success(); 1414 } 1415 1416 /// Parse a loc(...) specifier if present, filling in result if so. 1417 ParseResult 1418 parseOptionalLocationSpecifier(Optional<Location> &result) override { 1419 // If there is a 'loc' we parse a trailing location. 1420 if (!parser.consumeIf(Token::kw_loc)) 1421 return success(); 1422 LocationAttr directLoc; 1423 if (parser.parseToken(Token::l_paren, "expected '(' in location") || 1424 parser.parseLocationInstance(directLoc) || 1425 parser.parseToken(Token::r_paren, "expected ')' in location")) 1426 return failure(); 1427 1428 result = directLoc; 1429 return success(); 1430 } 1431 1432 //===--------------------------------------------------------------------===// 1433 // Operand Parsing 1434 //===--------------------------------------------------------------------===// 1435 1436 /// Parse a single operand. 1437 ParseResult parseOperand(OperandType &result) override { 1438 OperationParser::SSAUseInfo useInfo; 1439 if (parser.parseSSAUse(useInfo)) 1440 return failure(); 1441 1442 result = {useInfo.loc, useInfo.name, useInfo.number}; 1443 return success(); 1444 } 1445 1446 /// Parse a single operand if present. 1447 OptionalParseResult parseOptionalOperand(OperandType &result) override { 1448 if (parser.getToken().is(Token::percent_identifier)) 1449 return parseOperand(result); 1450 return llvm::None; 1451 } 1452 1453 /// Parse zero or more SSA comma-separated operand references with a specified 1454 /// surrounding delimiter, and an optional required operand count. 1455 ParseResult parseOperandList(SmallVectorImpl<OperandType> &result, 1456 int requiredOperandCount = -1, 1457 Delimiter delimiter = Delimiter::None) override { 1458 return parseOperandOrRegionArgList(result, /*isOperandList=*/true, 1459 requiredOperandCount, delimiter); 1460 } 1461 1462 /// Parse zero or more SSA comma-separated operand or region arguments with 1463 /// optional surrounding delimiter and required operand count. 1464 ParseResult 1465 parseOperandOrRegionArgList(SmallVectorImpl<OperandType> &result, 1466 bool isOperandList, int requiredOperandCount = -1, 1467 Delimiter delimiter = Delimiter::None) { 1468 auto startLoc = parser.getToken().getLoc(); 1469 1470 // Handle delimiters. 1471 switch (delimiter) { 1472 case Delimiter::None: 1473 // Don't check for the absence of a delimiter if the number of operands 1474 // is unknown (and hence the operand list could be empty). 1475 if (requiredOperandCount == -1) 1476 break; 1477 // Token already matches an identifier and so can't be a delimiter. 1478 if (parser.getToken().is(Token::percent_identifier)) 1479 break; 1480 // Test against known delimiters. 1481 if (parser.getToken().is(Token::l_paren) || 1482 parser.getToken().is(Token::l_square)) 1483 return emitError(startLoc, "unexpected delimiter"); 1484 return emitError(startLoc, "invalid operand"); 1485 case Delimiter::OptionalParen: 1486 if (parser.getToken().isNot(Token::l_paren)) 1487 return success(); 1488 LLVM_FALLTHROUGH; 1489 case Delimiter::Paren: 1490 if (parser.parseToken(Token::l_paren, "expected '(' in operand list")) 1491 return failure(); 1492 break; 1493 case Delimiter::OptionalSquare: 1494 if (parser.getToken().isNot(Token::l_square)) 1495 return success(); 1496 LLVM_FALLTHROUGH; 1497 case Delimiter::Square: 1498 if (parser.parseToken(Token::l_square, "expected '[' in operand list")) 1499 return failure(); 1500 break; 1501 } 1502 1503 // Check for zero operands. 1504 if (parser.getToken().is(Token::percent_identifier)) { 1505 do { 1506 OperandType operandOrArg; 1507 if (isOperandList ? parseOperand(operandOrArg) 1508 : parseRegionArgument(operandOrArg)) 1509 return failure(); 1510 result.push_back(operandOrArg); 1511 } while (parser.consumeIf(Token::comma)); 1512 } 1513 1514 // Handle delimiters. If we reach here, the optional delimiters were 1515 // present, so we need to parse their closing one. 1516 switch (delimiter) { 1517 case Delimiter::None: 1518 break; 1519 case Delimiter::OptionalParen: 1520 case Delimiter::Paren: 1521 if (parser.parseToken(Token::r_paren, "expected ')' in operand list")) 1522 return failure(); 1523 break; 1524 case Delimiter::OptionalSquare: 1525 case Delimiter::Square: 1526 if (parser.parseToken(Token::r_square, "expected ']' in operand list")) 1527 return failure(); 1528 break; 1529 } 1530 1531 if (requiredOperandCount != -1 && 1532 result.size() != static_cast<size_t>(requiredOperandCount)) 1533 return emitError(startLoc, "expected ") 1534 << requiredOperandCount << " operands"; 1535 return success(); 1536 } 1537 1538 /// Parse zero or more trailing SSA comma-separated trailing operand 1539 /// references with a specified surrounding delimiter, and an optional 1540 /// required operand count. A leading comma is expected before the operands. 1541 ParseResult parseTrailingOperandList(SmallVectorImpl<OperandType> &result, 1542 int requiredOperandCount, 1543 Delimiter delimiter) override { 1544 if (parser.getToken().is(Token::comma)) { 1545 parseComma(); 1546 return parseOperandList(result, requiredOperandCount, delimiter); 1547 } 1548 if (requiredOperandCount != -1) 1549 return emitError(parser.getToken().getLoc(), "expected ") 1550 << requiredOperandCount << " operands"; 1551 return success(); 1552 } 1553 1554 /// Resolve an operand to an SSA value, emitting an error on failure. 1555 ParseResult resolveOperand(const OperandType &operand, Type type, 1556 SmallVectorImpl<Value> &result) override { 1557 OperationParser::SSAUseInfo operandInfo = {operand.name, operand.number, 1558 operand.location}; 1559 if (auto value = parser.resolveSSAUse(operandInfo, type)) { 1560 result.push_back(value); 1561 return success(); 1562 } 1563 return failure(); 1564 } 1565 1566 /// Parse an AffineMap of SSA ids. 1567 ParseResult parseAffineMapOfSSAIds(SmallVectorImpl<OperandType> &operands, 1568 Attribute &mapAttr, StringRef attrName, 1569 NamedAttrList &attrs, 1570 Delimiter delimiter) override { 1571 SmallVector<OperandType, 2> dimOperands; 1572 SmallVector<OperandType, 1> symOperands; 1573 1574 auto parseElement = [&](bool isSymbol) -> ParseResult { 1575 OperandType operand; 1576 if (parseOperand(operand)) 1577 return failure(); 1578 if (isSymbol) 1579 symOperands.push_back(operand); 1580 else 1581 dimOperands.push_back(operand); 1582 return success(); 1583 }; 1584 1585 AffineMap map; 1586 if (parser.parseAffineMapOfSSAIds(map, parseElement, delimiter)) 1587 return failure(); 1588 // Add AffineMap attribute. 1589 if (map) { 1590 mapAttr = AffineMapAttr::get(map); 1591 attrs.push_back(parser.builder.getNamedAttr(attrName, mapAttr)); 1592 } 1593 1594 // Add dim operands before symbol operands in 'operands'. 1595 operands.assign(dimOperands.begin(), dimOperands.end()); 1596 operands.append(symOperands.begin(), symOperands.end()); 1597 return success(); 1598 } 1599 1600 /// Parse an AffineExpr of SSA ids. 1601 ParseResult 1602 parseAffineExprOfSSAIds(SmallVectorImpl<OperandType> &dimOperands, 1603 SmallVectorImpl<OperandType> &symbOperands, 1604 AffineExpr &expr) override { 1605 auto parseElement = [&](bool isSymbol) -> ParseResult { 1606 OperandType operand; 1607 if (parseOperand(operand)) 1608 return failure(); 1609 if (isSymbol) 1610 symbOperands.push_back(operand); 1611 else 1612 dimOperands.push_back(operand); 1613 return success(); 1614 }; 1615 1616 return parser.parseAffineExprOfSSAIds(expr, parseElement); 1617 } 1618 1619 //===--------------------------------------------------------------------===// 1620 // Region Parsing 1621 //===--------------------------------------------------------------------===// 1622 1623 /// Parse a region that takes `arguments` of `argTypes` types. This 1624 /// effectively defines the SSA values of `arguments` and assigns their type. 1625 ParseResult parseRegion(Region ®ion, ArrayRef<OperandType> arguments, 1626 ArrayRef<Type> argTypes, 1627 bool enableNameShadowing) override { 1628 assert(arguments.size() == argTypes.size() && 1629 "mismatching number of arguments and types"); 1630 1631 SmallVector<std::pair<OperationParser::SSAUseInfo, Type>, 2> 1632 regionArguments; 1633 for (auto pair : llvm::zip(arguments, argTypes)) { 1634 const OperandType &operand = std::get<0>(pair); 1635 Type type = std::get<1>(pair); 1636 OperationParser::SSAUseInfo operandInfo = {operand.name, operand.number, 1637 operand.location}; 1638 regionArguments.emplace_back(operandInfo, type); 1639 } 1640 1641 // Try to parse the region. 1642 (void)isIsolatedFromAbove; 1643 assert((!enableNameShadowing || isIsolatedFromAbove) && 1644 "name shadowing is only allowed on isolated regions"); 1645 if (parser.parseRegion(region, regionArguments, enableNameShadowing)) 1646 return failure(); 1647 return success(); 1648 } 1649 1650 /// Parses a region if present. 1651 OptionalParseResult parseOptionalRegion(Region ®ion, 1652 ArrayRef<OperandType> arguments, 1653 ArrayRef<Type> argTypes, 1654 bool enableNameShadowing) override { 1655 if (parser.getToken().isNot(Token::l_brace)) 1656 return llvm::None; 1657 return parseRegion(region, arguments, argTypes, enableNameShadowing); 1658 } 1659 1660 /// Parses a region if present. If the region is present, a new region is 1661 /// allocated and placed in `region`. If no region is present, `region` 1662 /// remains untouched. 1663 OptionalParseResult 1664 parseOptionalRegion(std::unique_ptr<Region> ®ion, 1665 ArrayRef<OperandType> arguments, ArrayRef<Type> argTypes, 1666 bool enableNameShadowing = false) override { 1667 if (parser.getToken().isNot(Token::l_brace)) 1668 return llvm::None; 1669 std::unique_ptr<Region> newRegion = std::make_unique<Region>(); 1670 if (parseRegion(*newRegion, arguments, argTypes, enableNameShadowing)) 1671 return failure(); 1672 1673 region = std::move(newRegion); 1674 return success(); 1675 } 1676 1677 /// Parse a region argument. The type of the argument will be resolved later 1678 /// by a call to `parseRegion`. 1679 ParseResult parseRegionArgument(OperandType &argument) override { 1680 return parseOperand(argument); 1681 } 1682 1683 /// Parse a region argument if present. 1684 ParseResult parseOptionalRegionArgument(OperandType &argument) override { 1685 if (parser.getToken().isNot(Token::percent_identifier)) 1686 return success(); 1687 return parseRegionArgument(argument); 1688 } 1689 1690 ParseResult 1691 parseRegionArgumentList(SmallVectorImpl<OperandType> &result, 1692 int requiredOperandCount = -1, 1693 Delimiter delimiter = Delimiter::None) override { 1694 return parseOperandOrRegionArgList(result, /*isOperandList=*/false, 1695 requiredOperandCount, delimiter); 1696 } 1697 1698 //===--------------------------------------------------------------------===// 1699 // Successor Parsing 1700 //===--------------------------------------------------------------------===// 1701 1702 /// Parse a single operation successor. 1703 ParseResult parseSuccessor(Block *&dest) override { 1704 return parser.parseSuccessor(dest); 1705 } 1706 1707 /// Parse an optional operation successor and its operand list. 1708 OptionalParseResult parseOptionalSuccessor(Block *&dest) override { 1709 if (parser.getToken().isNot(Token::caret_identifier)) 1710 return llvm::None; 1711 return parseSuccessor(dest); 1712 } 1713 1714 /// Parse a single operation successor and its operand list. 1715 ParseResult 1716 parseSuccessorAndUseList(Block *&dest, 1717 SmallVectorImpl<Value> &operands) override { 1718 if (parseSuccessor(dest)) 1719 return failure(); 1720 1721 // Handle optional arguments. 1722 if (succeeded(parseOptionalLParen()) && 1723 (parser.parseOptionalSSAUseAndTypeList(operands) || parseRParen())) { 1724 return failure(); 1725 } 1726 return success(); 1727 } 1728 1729 //===--------------------------------------------------------------------===// 1730 // Type Parsing 1731 //===--------------------------------------------------------------------===// 1732 1733 /// Parse a type. 1734 ParseResult parseType(Type &result) override { 1735 return failure(!(result = parser.parseType())); 1736 } 1737 1738 /// Parse an optional type. 1739 OptionalParseResult parseOptionalType(Type &result) override { 1740 return parser.parseOptionalType(result); 1741 } 1742 1743 /// Parse an arrow followed by a type list. 1744 ParseResult parseArrowTypeList(SmallVectorImpl<Type> &result) override { 1745 if (parseArrow() || parser.parseFunctionResultTypes(result)) 1746 return failure(); 1747 return success(); 1748 } 1749 1750 /// Parse an optional arrow followed by a type list. 1751 ParseResult 1752 parseOptionalArrowTypeList(SmallVectorImpl<Type> &result) override { 1753 if (!parser.consumeIf(Token::arrow)) 1754 return success(); 1755 return parser.parseFunctionResultTypes(result); 1756 } 1757 1758 /// Parse a colon followed by a type. 1759 ParseResult parseColonType(Type &result) override { 1760 return failure(parser.parseToken(Token::colon, "expected ':'") || 1761 !(result = parser.parseType())); 1762 } 1763 1764 /// Parse a colon followed by a type list, which must have at least one type. 1765 ParseResult parseColonTypeList(SmallVectorImpl<Type> &result) override { 1766 if (parser.parseToken(Token::colon, "expected ':'")) 1767 return failure(); 1768 return parser.parseTypeListNoParens(result); 1769 } 1770 1771 /// Parse an optional colon followed by a type list, which if present must 1772 /// have at least one type. 1773 ParseResult 1774 parseOptionalColonTypeList(SmallVectorImpl<Type> &result) override { 1775 if (!parser.consumeIf(Token::colon)) 1776 return success(); 1777 return parser.parseTypeListNoParens(result); 1778 } 1779 1780 /// Parse a list of assignments of the form 1781 /// (%x1 = %y1, %x2 = %y2, ...). 1782 OptionalParseResult 1783 parseOptionalAssignmentList(SmallVectorImpl<OperandType> &lhs, 1784 SmallVectorImpl<OperandType> &rhs) override { 1785 if (failed(parseOptionalLParen())) 1786 return llvm::None; 1787 1788 auto parseElt = [&]() -> ParseResult { 1789 OperandType regionArg, operand; 1790 if (parseRegionArgument(regionArg) || parseEqual() || 1791 parseOperand(operand)) 1792 return failure(); 1793 lhs.push_back(regionArg); 1794 rhs.push_back(operand); 1795 return success(); 1796 }; 1797 return parser.parseCommaSeparatedListUntil(Token::r_paren, parseElt); 1798 } 1799 1800 /// Parse a list of assignments of the form 1801 /// (%x1 = %y1 : type1, %x2 = %y2 : type2, ...). 1802 OptionalParseResult 1803 parseOptionalAssignmentListWithTypes(SmallVectorImpl<OperandType> &lhs, 1804 SmallVectorImpl<OperandType> &rhs, 1805 SmallVectorImpl<Type> &types) override { 1806 if (failed(parseOptionalLParen())) 1807 return llvm::None; 1808 1809 auto parseElt = [&]() -> ParseResult { 1810 OperandType regionArg, operand; 1811 Type type; 1812 if (parseRegionArgument(regionArg) || parseEqual() || 1813 parseOperand(operand) || parseColon() || parseType(type)) 1814 return failure(); 1815 lhs.push_back(regionArg); 1816 rhs.push_back(operand); 1817 types.push_back(type); 1818 return success(); 1819 }; 1820 return parser.parseCommaSeparatedListUntil(Token::r_paren, parseElt); 1821 } 1822 1823 private: 1824 /// The source location of the operation name. 1825 SMLoc nameLoc; 1826 1827 /// Information about the result name specifiers. 1828 ArrayRef<OperationParser::ResultRecord> resultIDs; 1829 1830 /// The abstract information of the operation. 1831 function_ref<ParseResult(OpAsmParser &, OperationState &)> parseAssembly; 1832 bool isIsolatedFromAbove; 1833 StringRef opName; 1834 1835 /// The main operation parser. 1836 OperationParser &parser; 1837 1838 /// A flag that indicates if any errors were emitted during parsing. 1839 bool emittedError = false; 1840 }; 1841 } // end anonymous namespace. 1842 1843 Operation * 1844 OperationParser::parseCustomOperation(ArrayRef<ResultRecord> resultIDs) { 1845 llvm::SMLoc opLoc = getToken().getLoc(); 1846 StringRef opName = getTokenSpelling(); 1847 auto *opDefinition = AbstractOperation::lookup(opName, getContext()); 1848 Dialect *dialect = nullptr; 1849 if (opDefinition) { 1850 dialect = &opDefinition->dialect; 1851 } else { 1852 if (opName.contains('.')) { 1853 // This op has a dialect, we try to check if we can register it in the 1854 // context on the fly. 1855 StringRef dialectName = opName.split('.').first; 1856 dialect = getContext()->getLoadedDialect(dialectName); 1857 if (!dialect && (dialect = getContext()->getOrLoadDialect(dialectName))) 1858 opDefinition = AbstractOperation::lookup(opName, getContext()); 1859 } else { 1860 // If the operation name has no namespace prefix we treat it as a builtin 1861 // or standard operation and prefix it with "builtin" or "std". 1862 // TODO: Remove the special casing here. 1863 opDefinition = AbstractOperation::lookup(Twine("builtin." + opName).str(), 1864 getContext()); 1865 if (!opDefinition && getContext()->getOrLoadDialect("std")) { 1866 opDefinition = AbstractOperation::lookup(Twine("std." + opName).str(), 1867 getContext()); 1868 } 1869 if (opDefinition) 1870 opName = opDefinition->name.strref(); 1871 } 1872 } 1873 1874 // This is the actual hook for the custom op parsing, usually implemented by 1875 // the op itself (`Op::parse()`). We retrieve it either from the 1876 // AbstractOperation or from the Dialect. 1877 function_ref<ParseResult(OpAsmParser &, OperationState &)> parseAssemblyFn; 1878 bool isIsolatedFromAbove = false; 1879 1880 if (opDefinition) { 1881 parseAssemblyFn = opDefinition->getParseAssemblyFn(); 1882 isIsolatedFromAbove = 1883 opDefinition->hasTrait<OpTrait::IsIsolatedFromAbove>(); 1884 } else { 1885 Optional<Dialect::ParseOpHook> dialectHook; 1886 if (dialect) 1887 dialectHook = dialect->getParseOperationHook(opName); 1888 if (!dialectHook.hasValue()) { 1889 emitError(opLoc) << "custom op '" << opName << "' is unknown"; 1890 return nullptr; 1891 } 1892 parseAssemblyFn = *dialectHook; 1893 } 1894 1895 consumeToken(); 1896 1897 // If the custom op parser crashes, produce some indication to help 1898 // debugging. 1899 std::string opNameStr = opName.str(); 1900 llvm::PrettyStackTraceFormat fmt("MLIR Parser: custom op parser '%s'", 1901 opNameStr.c_str()); 1902 1903 // Get location information for the operation. 1904 auto srcLocation = getEncodedSourceLocation(opLoc); 1905 OperationState opState(srcLocation, opName); 1906 1907 // If we are populating the parser state, start a new operation definition. 1908 if (state.asmState) 1909 state.asmState->startOperationDefinition(opState.name); 1910 1911 // Have the op implementation take a crack and parsing this. 1912 CleanupOpStateRegions guard{opState}; 1913 CustomOpAsmParser opAsmParser(opLoc, resultIDs, parseAssemblyFn, 1914 isIsolatedFromAbove, opName, *this); 1915 if (opAsmParser.parseOperation(opState)) 1916 return nullptr; 1917 1918 // If it emitted an error, we failed. 1919 if (opAsmParser.didEmitError()) 1920 return nullptr; 1921 1922 // Otherwise, create the operation and try to parse a location for it. 1923 Operation *op = opBuilder.createOperation(opState); 1924 if (parseTrailingLocationSpecifier(op)) 1925 return nullptr; 1926 return op; 1927 } 1928 1929 ParseResult 1930 OperationParser::parseTrailingLocationSpecifier(OpOrArgument opOrArgument) { 1931 // If there is a 'loc' we parse a trailing location. 1932 if (!consumeIf(Token::kw_loc)) 1933 return success(); 1934 if (parseToken(Token::l_paren, "expected '(' in location")) 1935 return failure(); 1936 Token tok = getToken(); 1937 1938 // Check to see if we are parsing a location alias. 1939 LocationAttr directLoc; 1940 if (tok.is(Token::hash_identifier)) { 1941 consumeToken(); 1942 1943 StringRef identifier = tok.getSpelling().drop_front(); 1944 if (identifier.contains('.')) { 1945 return emitError(tok.getLoc()) 1946 << "expected location, but found dialect attribute: '#" 1947 << identifier << "'"; 1948 } 1949 1950 // If this alias can be resolved, do it now. 1951 Attribute attr = state.symbols.attributeAliasDefinitions.lookup(identifier); 1952 if (attr) { 1953 if (!(directLoc = attr.dyn_cast<LocationAttr>())) 1954 return emitError(tok.getLoc()) 1955 << "expected location, but found '" << attr << "'"; 1956 } else { 1957 // Otherwise, remember this operation and resolve its location later. 1958 opsAndArgumentsWithDeferredLocs.emplace_back(opOrArgument, tok); 1959 } 1960 1961 // Otherwise, we parse the location directly. 1962 } else if (parseLocationInstance(directLoc)) { 1963 return failure(); 1964 } 1965 1966 if (parseToken(Token::r_paren, "expected ')' in location")) 1967 return failure(); 1968 1969 if (directLoc) { 1970 if (auto *op = opOrArgument.dyn_cast<Operation *>()) 1971 op->setLoc(directLoc); 1972 else 1973 opOrArgument.get<BlockArgument>().setLoc(directLoc); 1974 } 1975 return success(); 1976 } 1977 1978 //===----------------------------------------------------------------------===// 1979 // Region Parsing 1980 //===----------------------------------------------------------------------===// 1981 1982 ParseResult OperationParser::parseRegion( 1983 Region ®ion, 1984 ArrayRef<std::pair<OperationParser::SSAUseInfo, Type>> entryArguments, 1985 bool isIsolatedNameScope) { 1986 // Parse the '{'. 1987 Token lBraceTok = getToken(); 1988 if (parseToken(Token::l_brace, "expected '{' to begin a region")) 1989 return failure(); 1990 1991 // If we are populating the parser state, start a new region definition. 1992 if (state.asmState) 1993 state.asmState->startRegionDefinition(); 1994 1995 // Parse the region body. 1996 if ((!entryArguments.empty() || getToken().isNot(Token::r_brace)) && 1997 parseRegionBody(region, lBraceTok.getLoc(), entryArguments, 1998 isIsolatedNameScope)) { 1999 return failure(); 2000 } 2001 consumeToken(Token::r_brace); 2002 2003 // If we are populating the parser state, finalize this region. 2004 if (state.asmState) 2005 state.asmState->finalizeRegionDefinition(); 2006 2007 return success(); 2008 } 2009 2010 ParseResult OperationParser::parseRegionBody( 2011 Region ®ion, llvm::SMLoc startLoc, 2012 ArrayRef<std::pair<OperationParser::SSAUseInfo, Type>> entryArguments, 2013 bool isIsolatedNameScope) { 2014 auto currentPt = opBuilder.saveInsertionPoint(); 2015 2016 // Push a new named value scope. 2017 pushSSANameScope(isIsolatedNameScope); 2018 2019 // Parse the first block directly to allow for it to be unnamed. 2020 auto owning_block = std::make_unique<Block>(); 2021 Block *block = owning_block.get(); 2022 2023 // If this block is not defined in the source file, add a definition for it 2024 // now in the assembly state. Blocks with a name will be defined when the name 2025 // is parsed. 2026 if (state.asmState && getToken().isNot(Token::caret_identifier)) 2027 state.asmState->addDefinition(block, startLoc); 2028 2029 // Add arguments to the entry block. 2030 if (!entryArguments.empty()) { 2031 // If we had named arguments, then don't allow a block name. 2032 if (getToken().is(Token::caret_identifier)) 2033 return emitError("invalid block name in region with named arguments"); 2034 2035 for (auto &placeholderArgPair : entryArguments) { 2036 auto &argInfo = placeholderArgPair.first; 2037 2038 // Ensure that the argument was not already defined. 2039 if (auto defLoc = getReferenceLoc(argInfo.name, argInfo.number)) { 2040 return emitError(argInfo.loc, "region entry argument '" + argInfo.name + 2041 "' is already in use") 2042 .attachNote(getEncodedSourceLocation(*defLoc)) 2043 << "previously referenced here"; 2044 } 2045 auto loc = getEncodedSourceLocation(placeholderArgPair.first.loc); 2046 BlockArgument arg = block->addArgument(placeholderArgPair.second, loc); 2047 2048 // Add a definition of this arg to the assembly state if provided. 2049 if (state.asmState) 2050 state.asmState->addDefinition(arg, argInfo.loc); 2051 2052 // Record the definition for this argument. 2053 if (addDefinition(argInfo, arg)) 2054 return failure(); 2055 } 2056 } 2057 2058 if (parseBlock(block)) 2059 return failure(); 2060 2061 // Verify that no other arguments were parsed. 2062 if (!entryArguments.empty() && 2063 block->getNumArguments() > entryArguments.size()) { 2064 return emitError("entry block arguments were already defined"); 2065 } 2066 2067 // Parse the rest of the region. 2068 region.push_back(owning_block.release()); 2069 while (getToken().isNot(Token::r_brace)) { 2070 Block *newBlock = nullptr; 2071 if (parseBlock(newBlock)) 2072 return failure(); 2073 region.push_back(newBlock); 2074 } 2075 2076 // Pop the SSA value scope for this region. 2077 if (popSSANameScope()) 2078 return failure(); 2079 2080 // Reset the original insertion point. 2081 opBuilder.restoreInsertionPoint(currentPt); 2082 return success(); 2083 } 2084 2085 //===----------------------------------------------------------------------===// 2086 // Block Parsing 2087 //===----------------------------------------------------------------------===// 2088 2089 /// Block declaration. 2090 /// 2091 /// block ::= block-label? operation* 2092 /// block-label ::= block-id block-arg-list? `:` 2093 /// block-id ::= caret-id 2094 /// block-arg-list ::= `(` ssa-id-and-type-list? `)` 2095 /// 2096 ParseResult OperationParser::parseBlock(Block *&block) { 2097 // The first block of a region may already exist, if it does the caret 2098 // identifier is optional. 2099 if (block && getToken().isNot(Token::caret_identifier)) 2100 return parseBlockBody(block); 2101 2102 SMLoc nameLoc = getToken().getLoc(); 2103 auto name = getTokenSpelling(); 2104 if (parseToken(Token::caret_identifier, "expected block name")) 2105 return failure(); 2106 2107 block = defineBlockNamed(name, nameLoc, block); 2108 2109 // Fail if the block was already defined. 2110 if (!block) 2111 return emitError(nameLoc, "redefinition of block '") << name << "'"; 2112 2113 // If an argument list is present, parse it. 2114 if (consumeIf(Token::l_paren)) { 2115 if (parseOptionalBlockArgList(block) || 2116 parseToken(Token::r_paren, "expected ')' to end argument list")) 2117 return failure(); 2118 } 2119 2120 if (parseToken(Token::colon, "expected ':' after block name")) 2121 return failure(); 2122 2123 return parseBlockBody(block); 2124 } 2125 2126 ParseResult OperationParser::parseBlockBody(Block *block) { 2127 // Set the insertion point to the end of the block to parse. 2128 opBuilder.setInsertionPointToEnd(block); 2129 2130 // Parse the list of operations that make up the body of the block. 2131 while (getToken().isNot(Token::caret_identifier, Token::r_brace)) 2132 if (parseOperation()) 2133 return failure(); 2134 2135 return success(); 2136 } 2137 2138 /// Get the block with the specified name, creating it if it doesn't already 2139 /// exist. The location specified is the point of use, which allows 2140 /// us to diagnose references to blocks that are not defined precisely. 2141 Block *OperationParser::getBlockNamed(StringRef name, SMLoc loc) { 2142 BlockDefinition &blockDef = getBlockInfoByName(name); 2143 if (!blockDef.block) { 2144 blockDef = {new Block(), loc}; 2145 insertForwardRef(blockDef.block, blockDef.loc); 2146 } 2147 2148 // Populate the high level assembly state if necessary. 2149 if (state.asmState) 2150 state.asmState->addUses(blockDef.block, loc); 2151 2152 return blockDef.block; 2153 } 2154 2155 /// Define the block with the specified name. Returns the Block* or nullptr in 2156 /// the case of redefinition. 2157 Block *OperationParser::defineBlockNamed(StringRef name, SMLoc loc, 2158 Block *existing) { 2159 auto &blockAndLoc = getBlockInfoByName(name); 2160 blockAndLoc.loc = loc; 2161 2162 // If a block has yet to be set, this is a new definition. If the caller 2163 // provided a block, use it. Otherwise create a new one. 2164 if (!blockAndLoc.block) { 2165 blockAndLoc.block = existing ? existing : new Block(); 2166 2167 // Otherwise, the block has a forward declaration. Forward declarations are 2168 // removed once defined, so if we are defining a existing block and it is 2169 // not a forward declaration, then it is a redeclaration. 2170 } else if (!eraseForwardRef(blockAndLoc.block)) { 2171 return nullptr; 2172 } 2173 2174 // Populate the high level assembly state if necessary. 2175 if (state.asmState) 2176 state.asmState->addDefinition(blockAndLoc.block, loc); 2177 2178 return blockAndLoc.block; 2179 } 2180 2181 /// Parse a (possibly empty) list of SSA operands with types as block arguments. 2182 /// 2183 /// ssa-id-and-type-list ::= ssa-id-and-type (`,` ssa-id-and-type)* 2184 /// 2185 ParseResult OperationParser::parseOptionalBlockArgList(Block *owner) { 2186 if (getToken().is(Token::r_brace)) 2187 return success(); 2188 2189 // If the block already has arguments, then we're handling the entry block. 2190 // Parse and register the names for the arguments, but do not add them. 2191 bool definingExistingArgs = owner->getNumArguments() != 0; 2192 unsigned nextArgument = 0; 2193 2194 return parseCommaSeparatedList([&]() -> ParseResult { 2195 return parseSSADefOrUseAndType( 2196 [&](SSAUseInfo useInfo, Type type) -> ParseResult { 2197 BlockArgument arg; 2198 2199 // If we are defining existing arguments, ensure that the argument 2200 // has already been created with the right type. 2201 if (definingExistingArgs) { 2202 // Otherwise, ensure that this argument has already been created. 2203 if (nextArgument >= owner->getNumArguments()) 2204 return emitError("too many arguments specified in argument list"); 2205 2206 // Finally, make sure the existing argument has the correct type. 2207 arg = owner->getArgument(nextArgument++); 2208 if (arg.getType() != type) 2209 return emitError("argument and block argument type mismatch"); 2210 } else { 2211 auto loc = getEncodedSourceLocation(useInfo.loc); 2212 arg = owner->addArgument(type, loc); 2213 } 2214 2215 // If the argument has an explicit loc(...) specifier, parse and apply 2216 // it. 2217 if (parseTrailingLocationSpecifier(arg)) 2218 return failure(); 2219 2220 // Mark this block argument definition in the parser state if it was 2221 // provided. 2222 if (state.asmState) 2223 state.asmState->addDefinition(arg, useInfo.loc); 2224 2225 return addDefinition(useInfo, arg); 2226 }); 2227 }); 2228 } 2229 2230 //===----------------------------------------------------------------------===// 2231 // Top-level entity parsing. 2232 //===----------------------------------------------------------------------===// 2233 2234 namespace { 2235 /// This parser handles entities that are only valid at the top level of the 2236 /// file. 2237 class TopLevelOperationParser : public Parser { 2238 public: 2239 explicit TopLevelOperationParser(ParserState &state) : Parser(state) {} 2240 2241 /// Parse a set of operations into the end of the given Block. 2242 ParseResult parse(Block *topLevelBlock, Location parserLoc); 2243 2244 private: 2245 /// Parse an attribute alias declaration. 2246 ParseResult parseAttributeAliasDef(); 2247 2248 /// Parse an attribute alias declaration. 2249 ParseResult parseTypeAliasDef(); 2250 }; 2251 } // end anonymous namespace 2252 2253 /// Parses an attribute alias declaration. 2254 /// 2255 /// attribute-alias-def ::= '#' alias-name `=` attribute-value 2256 /// 2257 ParseResult TopLevelOperationParser::parseAttributeAliasDef() { 2258 assert(getToken().is(Token::hash_identifier)); 2259 StringRef aliasName = getTokenSpelling().drop_front(); 2260 2261 // Check for redefinitions. 2262 if (state.symbols.attributeAliasDefinitions.count(aliasName) > 0) 2263 return emitError("redefinition of attribute alias id '" + aliasName + "'"); 2264 2265 // Make sure this isn't invading the dialect attribute namespace. 2266 if (aliasName.contains('.')) 2267 return emitError("attribute names with a '.' are reserved for " 2268 "dialect-defined names"); 2269 2270 consumeToken(Token::hash_identifier); 2271 2272 // Parse the '='. 2273 if (parseToken(Token::equal, "expected '=' in attribute alias definition")) 2274 return failure(); 2275 2276 // Parse the attribute value. 2277 Attribute attr = parseAttribute(); 2278 if (!attr) 2279 return failure(); 2280 2281 state.symbols.attributeAliasDefinitions[aliasName] = attr; 2282 return success(); 2283 } 2284 2285 /// Parse a type alias declaration. 2286 /// 2287 /// type-alias-def ::= '!' alias-name `=` 'type' type 2288 /// 2289 ParseResult TopLevelOperationParser::parseTypeAliasDef() { 2290 assert(getToken().is(Token::exclamation_identifier)); 2291 StringRef aliasName = getTokenSpelling().drop_front(); 2292 2293 // Check for redefinitions. 2294 if (state.symbols.typeAliasDefinitions.count(aliasName) > 0) 2295 return emitError("redefinition of type alias id '" + aliasName + "'"); 2296 2297 // Make sure this isn't invading the dialect type namespace. 2298 if (aliasName.contains('.')) 2299 return emitError("type names with a '.' are reserved for " 2300 "dialect-defined names"); 2301 2302 consumeToken(Token::exclamation_identifier); 2303 2304 // Parse the '=' and 'type'. 2305 if (parseToken(Token::equal, "expected '=' in type alias definition") || 2306 parseToken(Token::kw_type, "expected 'type' in type alias definition")) 2307 return failure(); 2308 2309 // Parse the type. 2310 Type aliasedType = parseType(); 2311 if (!aliasedType) 2312 return failure(); 2313 2314 // Register this alias with the parser state. 2315 state.symbols.typeAliasDefinitions.try_emplace(aliasName, aliasedType); 2316 return success(); 2317 } 2318 2319 ParseResult TopLevelOperationParser::parse(Block *topLevelBlock, 2320 Location parserLoc) { 2321 // Create a top-level operation to contain the parsed state. 2322 OwningOpRef<ModuleOp> topLevelOp(ModuleOp::create(parserLoc)); 2323 OperationParser opParser(state, topLevelOp.get()); 2324 while (true) { 2325 switch (getToken().getKind()) { 2326 default: 2327 // Parse a top-level operation. 2328 if (opParser.parseOperation()) 2329 return failure(); 2330 break; 2331 2332 // If we got to the end of the file, then we're done. 2333 case Token::eof: { 2334 if (opParser.finalize()) 2335 return failure(); 2336 2337 // Splice the blocks of the parsed operation over to the provided 2338 // top-level block. 2339 auto &parsedOps = topLevelOp->getBody()->getOperations(); 2340 auto &destOps = topLevelBlock->getOperations(); 2341 destOps.splice(destOps.empty() ? destOps.end() : std::prev(destOps.end()), 2342 parsedOps, parsedOps.begin(), parsedOps.end()); 2343 return success(); 2344 } 2345 2346 // If we got an error token, then the lexer already emitted an error, just 2347 // stop. Someday we could introduce error recovery if there was demand 2348 // for it. 2349 case Token::error: 2350 return failure(); 2351 2352 // Parse an attribute alias. 2353 case Token::hash_identifier: 2354 if (parseAttributeAliasDef()) 2355 return failure(); 2356 break; 2357 2358 // Parse a type alias. 2359 case Token::exclamation_identifier: 2360 if (parseTypeAliasDef()) 2361 return failure(); 2362 break; 2363 } 2364 } 2365 } 2366 2367 //===----------------------------------------------------------------------===// 2368 2369 LogicalResult mlir::parseSourceFile(const llvm::SourceMgr &sourceMgr, 2370 Block *block, MLIRContext *context, 2371 LocationAttr *sourceFileLoc, 2372 AsmParserState *asmState) { 2373 const auto *sourceBuf = sourceMgr.getMemoryBuffer(sourceMgr.getMainFileID()); 2374 2375 Location parserLoc = FileLineColLoc::get( 2376 context, sourceBuf->getBufferIdentifier(), /*line=*/0, /*column=*/0); 2377 if (sourceFileLoc) 2378 *sourceFileLoc = parserLoc; 2379 2380 SymbolState aliasState; 2381 ParserState state(sourceMgr, context, aliasState, asmState); 2382 return TopLevelOperationParser(state).parse(block, parserLoc); 2383 } 2384 2385 LogicalResult mlir::parseSourceFile(llvm::StringRef filename, Block *block, 2386 MLIRContext *context, 2387 LocationAttr *sourceFileLoc) { 2388 llvm::SourceMgr sourceMgr; 2389 return parseSourceFile(filename, sourceMgr, block, context, sourceFileLoc); 2390 } 2391 2392 LogicalResult mlir::parseSourceFile(llvm::StringRef filename, 2393 llvm::SourceMgr &sourceMgr, Block *block, 2394 MLIRContext *context, 2395 LocationAttr *sourceFileLoc, 2396 AsmParserState *asmState) { 2397 if (sourceMgr.getNumBuffers() != 0) { 2398 // TODO: Extend to support multiple buffers. 2399 return emitError(mlir::UnknownLoc::get(context), 2400 "only main buffer parsed at the moment"); 2401 } 2402 auto file_or_err = llvm::MemoryBuffer::getFileOrSTDIN(filename); 2403 if (std::error_code error = file_or_err.getError()) 2404 return emitError(mlir::UnknownLoc::get(context), 2405 "could not open input file " + filename); 2406 2407 // Load the MLIR source file. 2408 sourceMgr.AddNewSourceBuffer(std::move(*file_or_err), llvm::SMLoc()); 2409 return parseSourceFile(sourceMgr, block, context, sourceFileLoc, asmState); 2410 } 2411 2412 LogicalResult mlir::parseSourceString(llvm::StringRef sourceStr, Block *block, 2413 MLIRContext *context, 2414 LocationAttr *sourceFileLoc) { 2415 auto memBuffer = MemoryBuffer::getMemBuffer(sourceStr); 2416 if (!memBuffer) 2417 return failure(); 2418 2419 SourceMgr sourceMgr; 2420 sourceMgr.AddNewSourceBuffer(std::move(memBuffer), SMLoc()); 2421 return parseSourceFile(sourceMgr, block, context, sourceFileLoc); 2422 } 2423