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