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