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