blob: 0d2497298813b9b8359a2cd383b43cab72150545 [file] [log] [blame]
Chris Lattnere79379a2018-06-22 10:39:19 -07001//===- Parser.cpp - MLIR Parser Implementation ----------------------------===//
2//
3// Copyright 2019 The MLIR Authors.
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16// =============================================================================
17//
18// This file implements the parser for the MLIR textual form.
19//
20//===----------------------------------------------------------------------===//
21
22#include "mlir/Parser.h"
23#include "Lexer.h"
MLIR Teamf85a6262018-06-27 11:03:08 -070024#include "mlir/IR/AffineMap.h"
Chris Lattnere79379a2018-06-22 10:39:19 -070025#include "mlir/IR/Module.h"
Chris Lattner4c95a502018-06-23 16:03:42 -070026#include "mlir/IR/CFGFunction.h"
Chris Lattnerf7e22732018-06-22 22:03:48 -070027#include "mlir/IR/Types.h"
Chris Lattnere79379a2018-06-22 10:39:19 -070028#include "llvm/Support/SourceMgr.h"
29using namespace mlir;
30using llvm::SourceMgr;
Chris Lattner4c95a502018-06-23 16:03:42 -070031using llvm::SMLoc;
Chris Lattnere79379a2018-06-22 10:39:19 -070032
33namespace {
Chris Lattner4c95a502018-06-23 16:03:42 -070034class CFGFunctionParserState;
35
Chris Lattnerf7e22732018-06-22 22:03:48 -070036/// Simple enum to make code read better in cases that would otherwise return a
37/// bool value. Failure is "true" in a boolean context.
Chris Lattnere79379a2018-06-22 10:39:19 -070038enum ParseResult {
39 ParseSuccess,
40 ParseFailure
41};
42
43/// Main parser implementation.
44class Parser {
45 public:
Jacques Pienaar9c411be2018-06-24 19:17:35 -070046 Parser(llvm::SourceMgr &sourceMgr, MLIRContext *context,
47 const SMDiagnosticHandlerTy &errorReporter)
48 : context(context),
49 lex(sourceMgr, errorReporter),
50 curToken(lex.lexToken()),
51 errorReporter(errorReporter) {
Chris Lattnere79379a2018-06-22 10:39:19 -070052 module.reset(new Module());
53 }
54
55 Module *parseModule();
56private:
57 // State.
Chris Lattnerf7e22732018-06-22 22:03:48 -070058 MLIRContext *const context;
59
60 // The lexer for the source file we're parsing.
Chris Lattnere79379a2018-06-22 10:39:19 -070061 Lexer lex;
62
63 // This is the next token that hasn't been consumed yet.
64 Token curToken;
65
Jacques Pienaar9c411be2018-06-24 19:17:35 -070066 // The diagnostic error reporter.
67 const SMDiagnosticHandlerTy &errorReporter;
68
Chris Lattnere79379a2018-06-22 10:39:19 -070069 // This is the result module we are parsing into.
70 std::unique_ptr<Module> module;
71
MLIR Teamf85a6262018-06-27 11:03:08 -070072 // A map from affine map identifier to AffineMap.
73 // TODO(andydavis) Remove use of unique_ptr when AffineMaps are bump pointer
74 // allocated.
75 llvm::StringMap<std::unique_ptr<AffineMap>> affineMaps;
76
Chris Lattnere79379a2018-06-22 10:39:19 -070077private:
78 // Helper methods.
79
80 /// Emit an error and return failure.
Chris Lattner4c95a502018-06-23 16:03:42 -070081 ParseResult emitError(const Twine &message) {
82 return emitError(curToken.getLoc(), message);
83 }
84 ParseResult emitError(SMLoc loc, const Twine &message);
Chris Lattnere79379a2018-06-22 10:39:19 -070085
86 /// Advance the current lexer onto the next token.
87 void consumeToken() {
88 assert(curToken.isNot(Token::eof, Token::error) &&
89 "shouldn't advance past EOF or errors");
90 curToken = lex.lexToken();
91 }
92
93 /// Advance the current lexer onto the next token, asserting what the expected
94 /// current token is. This is preferred to the above method because it leads
95 /// to more self-documenting code with better checking.
96 void consumeToken(Token::TokenKind kind) {
97 assert(curToken.is(kind) && "consumed an unexpected token");
98 consumeToken();
99 }
100
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700101 /// If the current token has the specified kind, consume it and return true.
102 /// If not, return false.
103 bool consumeIf(Token::TokenKind kind) {
104 if (curToken.isNot(kind))
105 return false;
106 consumeToken(kind);
107 return true;
108 }
109
110 ParseResult parseCommaSeparatedList(Token::TokenKind rightToken,
111 const std::function<ParseResult()> &parseElement,
112 bool allowEmptyList = true);
113
Chris Lattnerf7e22732018-06-22 22:03:48 -0700114 // We have two forms of parsing methods - those that return a non-null
115 // pointer on success, and those that return a ParseResult to indicate whether
116 // they returned a failure. The second class fills in by-reference arguments
117 // as the results of their action.
118
Chris Lattnere79379a2018-06-22 10:39:19 -0700119 // Type parsing.
Chris Lattnerf7e22732018-06-22 22:03:48 -0700120 PrimitiveType *parsePrimitiveType();
121 Type *parseElementType();
122 VectorType *parseVectorType();
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700123 ParseResult parseDimensionListRanked(SmallVectorImpl<int> &dimensions);
Chris Lattnerf7e22732018-06-22 22:03:48 -0700124 Type *parseTensorType();
125 Type *parseMemRefType();
126 Type *parseFunctionType();
127 Type *parseType();
128 ParseResult parseTypeList(SmallVectorImpl<Type*> &elements);
Chris Lattnere79379a2018-06-22 10:39:19 -0700129
MLIR Teamf85a6262018-06-27 11:03:08 -0700130 // Polyhedral structures
131 ParseResult parseAffineMapDef();
132
Chris Lattner4c95a502018-06-23 16:03:42 -0700133 // Functions.
Chris Lattnerf7e22732018-06-22 22:03:48 -0700134 ParseResult parseFunctionSignature(StringRef &name, FunctionType *&type);
Chris Lattnere79379a2018-06-22 10:39:19 -0700135 ParseResult parseExtFunc();
Chris Lattner4c95a502018-06-23 16:03:42 -0700136 ParseResult parseCFGFunc();
137 ParseResult parseBasicBlock(CFGFunctionParserState &functionState);
Chris Lattnerf6d80a02018-06-24 11:18:29 -0700138 TerminatorInst *parseTerminator(BasicBlock *currentBB,
139 CFGFunctionParserState &functionState);
140
Chris Lattnere79379a2018-06-22 10:39:19 -0700141};
142} // end anonymous namespace
143
144//===----------------------------------------------------------------------===//
145// Helper methods.
146//===----------------------------------------------------------------------===//
147
Chris Lattner4c95a502018-06-23 16:03:42 -0700148ParseResult Parser::emitError(SMLoc loc, const Twine &message) {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700149 // If we hit a parse error in response to a lexer error, then the lexer
Jacques Pienaar9c411be2018-06-24 19:17:35 -0700150 // already reported the error.
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700151 if (curToken.is(Token::error))
152 return ParseFailure;
153
Jacques Pienaar9c411be2018-06-24 19:17:35 -0700154 errorReporter(
155 lex.getSourceMgr().GetMessage(loc, SourceMgr::DK_Error, message));
Chris Lattnere79379a2018-06-22 10:39:19 -0700156 return ParseFailure;
157}
158
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700159/// Parse a comma-separated list of elements, terminated with an arbitrary
160/// token. This allows empty lists if allowEmptyList is true.
161///
162/// abstract-list ::= rightToken // if allowEmptyList == true
163/// abstract-list ::= element (',' element)* rightToken
164///
165ParseResult Parser::
166parseCommaSeparatedList(Token::TokenKind rightToken,
167 const std::function<ParseResult()> &parseElement,
168 bool allowEmptyList) {
169 // Handle the empty case.
170 if (curToken.is(rightToken)) {
171 if (!allowEmptyList)
172 return emitError("expected list element");
173 consumeToken(rightToken);
174 return ParseSuccess;
175 }
176
177 // Non-empty case starts with an element.
178 if (parseElement())
179 return ParseFailure;
180
181 // Otherwise we have a list of comma separated elements.
182 while (consumeIf(Token::comma)) {
183 if (parseElement())
184 return ParseFailure;
185 }
186
187 // Consume the end character.
188 if (!consumeIf(rightToken))
189 return emitError("expected ',' or ')'");
190
191 return ParseSuccess;
192}
Chris Lattnere79379a2018-06-22 10:39:19 -0700193
194//===----------------------------------------------------------------------===//
195// Type Parsing
196//===----------------------------------------------------------------------===//
197
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700198/// Parse the low-level fixed dtypes in the system.
199///
200/// primitive-type
201/// ::= `f16` | `bf16` | `f32` | `f64` // Floating point
202/// | `i1` | `i8` | `i16` | `i32` | `i64` // Sized integers
203/// | `int`
204///
Chris Lattnerf7e22732018-06-22 22:03:48 -0700205PrimitiveType *Parser::parsePrimitiveType() {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700206 switch (curToken.getKind()) {
Chris Lattnerf7e22732018-06-22 22:03:48 -0700207 default:
208 return (emitError("expected type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700209 case Token::kw_bf16:
210 consumeToken(Token::kw_bf16);
Chris Lattnerf7e22732018-06-22 22:03:48 -0700211 return Type::getBF16(context);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700212 case Token::kw_f16:
213 consumeToken(Token::kw_f16);
Chris Lattnerf7e22732018-06-22 22:03:48 -0700214 return Type::getF16(context);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700215 case Token::kw_f32:
216 consumeToken(Token::kw_f32);
Chris Lattnerf7e22732018-06-22 22:03:48 -0700217 return Type::getF32(context);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700218 case Token::kw_f64:
219 consumeToken(Token::kw_f64);
Chris Lattnerf7e22732018-06-22 22:03:48 -0700220 return Type::getF64(context);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700221 case Token::kw_i1:
222 consumeToken(Token::kw_i1);
Chris Lattnerf7e22732018-06-22 22:03:48 -0700223 return Type::getI1(context);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700224 case Token::kw_i8:
225 consumeToken(Token::kw_i8);
Chris Lattnerf7e22732018-06-22 22:03:48 -0700226 return Type::getI8(context);
227 case Token::kw_i16:
228 consumeToken(Token::kw_i16);
229 return Type::getI16(context);
230 case Token::kw_i32:
231 consumeToken(Token::kw_i32);
232 return Type::getI32(context);
233 case Token::kw_i64:
234 consumeToken(Token::kw_i64);
235 return Type::getI64(context);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700236 case Token::kw_int:
237 consumeToken(Token::kw_int);
Chris Lattnerf7e22732018-06-22 22:03:48 -0700238 return Type::getInt(context);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700239 }
240}
241
242/// Parse the element type of a tensor or memref type.
243///
244/// element-type ::= primitive-type | vector-type
245///
Chris Lattnerf7e22732018-06-22 22:03:48 -0700246Type *Parser::parseElementType() {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700247 if (curToken.is(Token::kw_vector))
248 return parseVectorType();
249
250 return parsePrimitiveType();
251}
252
253/// Parse a vector type.
254///
255/// vector-type ::= `vector` `<` const-dimension-list primitive-type `>`
256/// const-dimension-list ::= (integer-literal `x`)+
257///
Chris Lattnerf7e22732018-06-22 22:03:48 -0700258VectorType *Parser::parseVectorType() {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700259 consumeToken(Token::kw_vector);
260
261 if (!consumeIf(Token::less))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700262 return (emitError("expected '<' in vector type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700263
264 if (curToken.isNot(Token::integer))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700265 return (emitError("expected dimension size in vector type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700266
267 SmallVector<unsigned, 4> dimensions;
268 while (curToken.is(Token::integer)) {
269 // Make sure this integer value is in bound and valid.
270 auto dimension = curToken.getUnsignedIntegerValue();
271 if (!dimension.hasValue())
Chris Lattnerf7e22732018-06-22 22:03:48 -0700272 return (emitError("invalid dimension in vector type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700273 dimensions.push_back(dimension.getValue());
274
275 consumeToken(Token::integer);
276
277 // Make sure we have an 'x' or something like 'xbf32'.
278 if (curToken.isNot(Token::bare_identifier) ||
279 curToken.getSpelling()[0] != 'x')
Chris Lattnerf7e22732018-06-22 22:03:48 -0700280 return (emitError("expected 'x' in vector dimension list"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700281
282 // If we had a prefix of 'x', lex the next token immediately after the 'x'.
283 if (curToken.getSpelling().size() != 1)
284 lex.resetPointer(curToken.getSpelling().data()+1);
285
286 // Consume the 'x'.
287 consumeToken(Token::bare_identifier);
288 }
289
290 // Parse the element type.
Chris Lattnerf7e22732018-06-22 22:03:48 -0700291 auto *elementType = parsePrimitiveType();
292 if (!elementType)
293 return nullptr;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700294
295 if (!consumeIf(Token::greater))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700296 return (emitError("expected '>' in vector type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700297
Chris Lattnerf7e22732018-06-22 22:03:48 -0700298 return VectorType::get(dimensions, elementType);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700299}
300
301/// Parse a dimension list of a tensor or memref type. This populates the
302/// dimension list, returning -1 for the '?' dimensions.
303///
304/// dimension-list-ranked ::= (dimension `x`)*
305/// dimension ::= `?` | integer-literal
306///
307ParseResult Parser::parseDimensionListRanked(SmallVectorImpl<int> &dimensions) {
308 while (curToken.isAny(Token::integer, Token::question)) {
309 if (consumeIf(Token::question)) {
310 dimensions.push_back(-1);
311 } else {
312 // Make sure this integer value is in bound and valid.
313 auto dimension = curToken.getUnsignedIntegerValue();
314 if (!dimension.hasValue() || (int)dimension.getValue() < 0)
315 return emitError("invalid dimension");
316 dimensions.push_back((int)dimension.getValue());
317 consumeToken(Token::integer);
318 }
319
320 // Make sure we have an 'x' or something like 'xbf32'.
321 if (curToken.isNot(Token::bare_identifier) ||
322 curToken.getSpelling()[0] != 'x')
323 return emitError("expected 'x' in dimension list");
324
325 // If we had a prefix of 'x', lex the next token immediately after the 'x'.
326 if (curToken.getSpelling().size() != 1)
327 lex.resetPointer(curToken.getSpelling().data()+1);
328
329 // Consume the 'x'.
330 consumeToken(Token::bare_identifier);
331 }
332
333 return ParseSuccess;
334}
335
336/// Parse a tensor type.
337///
338/// tensor-type ::= `tensor` `<` dimension-list element-type `>`
339/// dimension-list ::= dimension-list-ranked | `??`
340///
Chris Lattnerf7e22732018-06-22 22:03:48 -0700341Type *Parser::parseTensorType() {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700342 consumeToken(Token::kw_tensor);
343
344 if (!consumeIf(Token::less))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700345 return (emitError("expected '<' in tensor type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700346
347 bool isUnranked;
348 SmallVector<int, 4> dimensions;
349
350 if (consumeIf(Token::questionquestion)) {
351 isUnranked = true;
352 } else {
353 isUnranked = false;
354 if (parseDimensionListRanked(dimensions))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700355 return nullptr;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700356 }
357
358 // Parse the element type.
Chris Lattnerf7e22732018-06-22 22:03:48 -0700359 auto elementType = parseElementType();
360 if (!elementType)
361 return nullptr;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700362
363 if (!consumeIf(Token::greater))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700364 return (emitError("expected '>' in tensor type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700365
MLIR Team355ec862018-06-23 18:09:09 -0700366 if (isUnranked)
367 return UnrankedTensorType::get(elementType);
368 return RankedTensorType::get(dimensions, elementType);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700369}
370
371/// Parse a memref type.
372///
373/// memref-type ::= `memref` `<` dimension-list-ranked element-type
374/// (`,` semi-affine-map-composition)? (`,` memory-space)? `>`
375///
376/// semi-affine-map-composition ::= (semi-affine-map `,` )* semi-affine-map
377/// memory-space ::= integer-literal /* | TODO: address-space-id */
378///
Chris Lattnerf7e22732018-06-22 22:03:48 -0700379Type *Parser::parseMemRefType() {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700380 consumeToken(Token::kw_memref);
381
382 if (!consumeIf(Token::less))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700383 return (emitError("expected '<' in memref type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700384
385 SmallVector<int, 4> dimensions;
386 if (parseDimensionListRanked(dimensions))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700387 return nullptr;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700388
389 // Parse the element type.
Chris Lattnerf7e22732018-06-22 22:03:48 -0700390 auto elementType = parseElementType();
391 if (!elementType)
392 return nullptr;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700393
394 // TODO: Parse semi-affine-map-composition.
395 // TODO: Parse memory-space.
396
397 if (!consumeIf(Token::greater))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700398 return (emitError("expected '>' in memref type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700399
Chris Lattnerf7e22732018-06-22 22:03:48 -0700400 // FIXME: Add an IR representation for memref types.
401 return Type::getI1(context);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700402}
403
404
405
406/// Parse a function type.
407///
408/// function-type ::= type-list-parens `->` type-list
409///
Chris Lattnerf7e22732018-06-22 22:03:48 -0700410Type *Parser::parseFunctionType() {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700411 assert(curToken.is(Token::l_paren));
412
Chris Lattnerf7e22732018-06-22 22:03:48 -0700413 SmallVector<Type*, 4> arguments;
414 if (parseTypeList(arguments))
415 return nullptr;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700416
417 if (!consumeIf(Token::arrow))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700418 return (emitError("expected '->' in function type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700419
Chris Lattnerf7e22732018-06-22 22:03:48 -0700420 SmallVector<Type*, 4> results;
421 if (parseTypeList(results))
422 return nullptr;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700423
Chris Lattnerf7e22732018-06-22 22:03:48 -0700424 return FunctionType::get(arguments, results, context);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700425}
426
427
428/// Parse an arbitrary type.
429///
430/// type ::= primitive-type
431/// | vector-type
432/// | tensor-type
433/// | memref-type
434/// | function-type
435/// element-type ::= primitive-type | vector-type
436///
Chris Lattnerf7e22732018-06-22 22:03:48 -0700437Type *Parser::parseType() {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700438 switch (curToken.getKind()) {
439 case Token::kw_memref: return parseMemRefType();
440 case Token::kw_tensor: return parseTensorType();
441 case Token::kw_vector: return parseVectorType();
442 case Token::l_paren: return parseFunctionType();
443 default:
444 return parsePrimitiveType();
445 }
446}
447
448/// Parse a "type list", which is a singular type, or a parenthesized list of
449/// types.
450///
451/// type-list ::= type-list-parens | type
452/// type-list-parens ::= `(` `)`
453/// | `(` type (`,` type)* `)`
454///
Chris Lattnerf7e22732018-06-22 22:03:48 -0700455ParseResult Parser::parseTypeList(SmallVectorImpl<Type*> &elements) {
456 auto parseElt = [&]() -> ParseResult {
457 auto elt = parseType();
458 elements.push_back(elt);
459 return elt ? ParseSuccess : ParseFailure;
460 };
461
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700462 // If there is no parens, then it must be a singular type.
463 if (!consumeIf(Token::l_paren))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700464 return parseElt();
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700465
Chris Lattnerf7e22732018-06-22 22:03:48 -0700466 if (parseCommaSeparatedList(Token::r_paren, parseElt))
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700467 return ParseFailure;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700468
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700469 return ParseSuccess;
470}
471
Chris Lattner4c95a502018-06-23 16:03:42 -0700472//===----------------------------------------------------------------------===//
MLIR Teamf85a6262018-06-27 11:03:08 -0700473// Polyhedral structures.
474//===----------------------------------------------------------------------===//
475
476/// Affine map declaration.
477///
478/// affine-map-def ::= affine-map-id `=` affine-map-inline
479/// affine-map-inline ::= dim-and-symbol-id-lists `->` multi-dim-affine-expr
480/// ( `size` `(` dim-size (`,` dim-size)* `)` )?
481/// dim-size ::= affine-expr | `min` `(` affine-expr ( `,` affine-expr)+ `)`
482///
483ParseResult Parser::parseAffineMapDef() {
484 assert(curToken.is(Token::affine_map_id));
485
486 StringRef affineMapId = curToken.getSpelling().drop_front();
487 // Check that 'affineMapId' is unique.
488 // TODO(andydavis) Add a unit test for this case.
489 if (affineMaps.count(affineMapId) > 0)
490 return emitError("encountered non-unique affine map id");
491
492 consumeToken(Token::affine_map_id);
493
494 // TODO(andydavis,bondhugula) Parse affine map definition.
495 affineMaps[affineMapId].reset(new AffineMap(1, 0));
496 return ParseSuccess;
497}
498
499//===----------------------------------------------------------------------===//
Chris Lattner4c95a502018-06-23 16:03:42 -0700500// Functions
501//===----------------------------------------------------------------------===//
Chris Lattnere79379a2018-06-22 10:39:19 -0700502
Chris Lattnere79379a2018-06-22 10:39:19 -0700503
504/// Parse a function signature, starting with a name and including the parameter
505/// list.
506///
507/// argument-list ::= type (`,` type)* | /*empty*/
508/// function-signature ::= function-id `(` argument-list `)` (`->` type-list)?
509///
Chris Lattnerf7e22732018-06-22 22:03:48 -0700510ParseResult Parser::parseFunctionSignature(StringRef &name,
511 FunctionType *&type) {
Chris Lattnere79379a2018-06-22 10:39:19 -0700512 if (curToken.isNot(Token::at_identifier))
513 return emitError("expected a function identifier like '@foo'");
514
515 name = curToken.getSpelling().drop_front();
516 consumeToken(Token::at_identifier);
517
518 if (curToken.isNot(Token::l_paren))
519 return emitError("expected '(' in function signature");
Chris Lattnere79379a2018-06-22 10:39:19 -0700520
Chris Lattnerf7e22732018-06-22 22:03:48 -0700521 SmallVector<Type*, 4> arguments;
522 if (parseTypeList(arguments))
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700523 return ParseFailure;
Chris Lattnere79379a2018-06-22 10:39:19 -0700524
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700525 // Parse the return type if present.
Chris Lattnerf7e22732018-06-22 22:03:48 -0700526 SmallVector<Type*, 4> results;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700527 if (consumeIf(Token::arrow)) {
Chris Lattnerf7e22732018-06-22 22:03:48 -0700528 if (parseTypeList(results))
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700529 return ParseFailure;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700530 }
Chris Lattnerf7e22732018-06-22 22:03:48 -0700531 type = FunctionType::get(arguments, results, context);
Chris Lattnere79379a2018-06-22 10:39:19 -0700532 return ParseSuccess;
533}
534
535
536/// External function declarations.
537///
538/// ext-func ::= `extfunc` function-signature
539///
540ParseResult Parser::parseExtFunc() {
541 consumeToken(Token::kw_extfunc);
542
543 StringRef name;
Chris Lattnerf7e22732018-06-22 22:03:48 -0700544 FunctionType *type = nullptr;
545 if (parseFunctionSignature(name, type))
Chris Lattnere79379a2018-06-22 10:39:19 -0700546 return ParseFailure;
547
548
549 // Okay, the external function definition was parsed correctly.
Chris Lattner4c95a502018-06-23 16:03:42 -0700550 module->functionList.push_back(new ExtFunction(name, type));
Chris Lattnere79379a2018-06-22 10:39:19 -0700551 return ParseSuccess;
552}
553
554
Chris Lattner4c95a502018-06-23 16:03:42 -0700555namespace {
556/// This class represents the transient parser state for the internals of a
557/// function as we are parsing it, e.g. the names for basic blocks. It handles
558/// forward references.
559class CFGFunctionParserState {
560public:
Chris Lattnerf6d80a02018-06-24 11:18:29 -0700561 CFGFunction *function;
562 llvm::StringMap<std::pair<BasicBlock*, SMLoc>> blocksByName;
563
Chris Lattner4c95a502018-06-23 16:03:42 -0700564 CFGFunctionParserState(CFGFunction *function) : function(function) {}
565
566 /// Get the basic block with the specified name, creating it if it doesn't
Chris Lattnerf6d80a02018-06-24 11:18:29 -0700567 /// already exist. The location specified is the point of use, which allows
568 /// us to diagnose references to blocks that are not defined precisely.
569 BasicBlock *getBlockNamed(StringRef name, SMLoc loc) {
570 auto &blockAndLoc = blocksByName[name];
571 if (!blockAndLoc.first) {
572 blockAndLoc.first = new BasicBlock(function);
573 blockAndLoc.second = loc;
Chris Lattner4c95a502018-06-23 16:03:42 -0700574 }
Chris Lattnerf6d80a02018-06-24 11:18:29 -0700575 return blockAndLoc.first;
Chris Lattner4c95a502018-06-23 16:03:42 -0700576 }
Chris Lattner4c95a502018-06-23 16:03:42 -0700577};
578} // end anonymous namespace
579
580
581/// CFG function declarations.
582///
583/// cfg-func ::= `cfgfunc` function-signature `{` basic-block+ `}`
584///
585ParseResult Parser::parseCFGFunc() {
586 consumeToken(Token::kw_cfgfunc);
587
588 StringRef name;
589 FunctionType *type = nullptr;
590 if (parseFunctionSignature(name, type))
591 return ParseFailure;
592
593 if (!consumeIf(Token::l_brace))
594 return emitError("expected '{' in CFG function");
595
596 // Okay, the CFG function signature was parsed correctly, create the function.
597 auto function = new CFGFunction(name, type);
598
599 // Make sure we have at least one block.
600 if (curToken.is(Token::r_brace))
601 return emitError("CFG functions must have at least one basic block");
602
603 CFGFunctionParserState functionState(function);
604
605 // Parse the list of blocks.
606 while (!consumeIf(Token::r_brace))
607 if (parseBasicBlock(functionState))
608 return ParseFailure;
609
Chris Lattnerf6d80a02018-06-24 11:18:29 -0700610 // Verify that all referenced blocks were defined. Iteration over a
611 // StringMap isn't determinstic, but this is good enough for our purposes.
612 for (auto &elt : functionState.blocksByName) {
613 auto *bb = elt.second.first;
614 if (!bb->getTerminator())
615 return emitError(elt.second.second,
616 "reference to an undefined basic block '" +
617 elt.first() + "'");
618 }
619
Chris Lattner4c95a502018-06-23 16:03:42 -0700620 module->functionList.push_back(function);
621 return ParseSuccess;
622}
623
624/// Basic block declaration.
625///
626/// basic-block ::= bb-label instruction* terminator-stmt
627/// bb-label ::= bb-id bb-arg-list? `:`
628/// bb-id ::= bare-id
629/// bb-arg-list ::= `(` ssa-id-and-type-list? `)`
630///
631ParseResult Parser::parseBasicBlock(CFGFunctionParserState &functionState) {
632 SMLoc nameLoc = curToken.getLoc();
633 auto name = curToken.getSpelling();
634 if (!consumeIf(Token::bare_identifier))
635 return emitError("expected basic block name");
Chris Lattnerf6d80a02018-06-24 11:18:29 -0700636
637 auto block = functionState.getBlockNamed(name, nameLoc);
Chris Lattner4c95a502018-06-23 16:03:42 -0700638
639 // If this block has already been parsed, then this is a redefinition with the
640 // same block name.
641 if (block->getTerminator())
Chris Lattnerf6d80a02018-06-24 11:18:29 -0700642 return emitError(nameLoc, "redefinition of block '" + name.str() + "'");
643
644 // References to blocks can occur in any order, but we need to reassemble the
645 // function in the order that occurs in the source file. Do this by moving
646 // each block to the end of the list as it is defined.
647 // FIXME: This is inefficient for large functions given that blockList is a
648 // vector. blockList will eventually be an ilist, which will make this fast.
649 auto &blockList = functionState.function->blockList;
650 if (blockList.back() != block) {
651 auto it = std::find(blockList.begin(), blockList.end(), block);
652 assert(it != blockList.end() && "Block has to be in the blockList");
653 std::swap(*it, blockList.back());
654 }
Chris Lattner4c95a502018-06-23 16:03:42 -0700655
656 // TODO: parse bb argument list.
657
658 if (!consumeIf(Token::colon))
659 return emitError("expected ':' after basic block name");
660
661
662 // TODO(clattner): Verify block hasn't already been parsed (this would be a
663 // redefinition of the same name) once we have a body implementation.
664
665 // TODO(clattner): Move block to the end of the list, once we have a proper
666 // block list representation in CFGFunction.
667
668 // TODO: parse instruction list.
669
670 // TODO: Generalize this once instruction list parsing is built out.
Chris Lattner4c95a502018-06-23 16:03:42 -0700671
Chris Lattnerf6d80a02018-06-24 11:18:29 -0700672 auto *termInst = parseTerminator(block, functionState);
673 if (!termInst)
674 return ParseFailure;
675 block->setTerminator(termInst);
Chris Lattner4c95a502018-06-23 16:03:42 -0700676
677 return ParseSuccess;
678}
679
680
Chris Lattnerf6d80a02018-06-24 11:18:29 -0700681/// Parse the terminator instruction for a basic block.
682///
683/// terminator-stmt ::= `br` bb-id branch-use-list?
684/// branch-use-list ::= `(` ssa-use-and-type-list? `)`
685/// terminator-stmt ::=
686/// `cond_br` ssa-use `,` bb-id branch-use-list? `,` bb-id branch-use-list?
687/// terminator-stmt ::= `return` ssa-use-and-type-list?
688///
689TerminatorInst *Parser::parseTerminator(BasicBlock *currentBB,
690 CFGFunctionParserState &functionState) {
691 switch (curToken.getKind()) {
692 default:
693 return (emitError("expected terminator at end of basic block"), nullptr);
694
695 case Token::kw_return:
696 consumeToken(Token::kw_return);
697 return new ReturnInst(currentBB);
698
699 case Token::kw_br: {
700 consumeToken(Token::kw_br);
701 auto destBB = functionState.getBlockNamed(curToken.getSpelling(),
702 curToken.getLoc());
703 if (!consumeIf(Token::bare_identifier))
704 return (emitError("expected basic block name"), nullptr);
705 return new BranchInst(destBB, currentBB);
706 }
707 }
708}
709
710
Chris Lattner4c95a502018-06-23 16:03:42 -0700711//===----------------------------------------------------------------------===//
712// Top-level entity parsing.
713//===----------------------------------------------------------------------===//
714
Chris Lattnere79379a2018-06-22 10:39:19 -0700715/// This is the top-level module parser.
716Module *Parser::parseModule() {
717 while (1) {
718 switch (curToken.getKind()) {
719 default:
720 emitError("expected a top level entity");
721 return nullptr;
722
723 // If we got to the end of the file, then we're done.
724 case Token::eof:
725 return module.release();
726
727 // If we got an error token, then the lexer already emitted an error, just
728 // stop. Someday we could introduce error recovery if there was demand for
729 // it.
730 case Token::error:
731 return nullptr;
732
733 case Token::kw_extfunc:
Chris Lattner4c95a502018-06-23 16:03:42 -0700734 if (parseExtFunc()) return nullptr;
Chris Lattnere79379a2018-06-22 10:39:19 -0700735 break;
736
Chris Lattner4c95a502018-06-23 16:03:42 -0700737 case Token::kw_cfgfunc:
738 if (parseCFGFunc()) return nullptr;
739 break;
MLIR Teamf85a6262018-06-27 11:03:08 -0700740 case Token::affine_map_id:
741 if (parseAffineMapDef()) return nullptr;
742 break;
Chris Lattner4c95a502018-06-23 16:03:42 -0700743
744 // TODO: mlfunc, affine entity declarations, etc.
Chris Lattnere79379a2018-06-22 10:39:19 -0700745 }
746 }
747}
748
749//===----------------------------------------------------------------------===//
750
751/// This parses the file specified by the indicated SourceMgr and returns an
752/// MLIR module if it was valid. If not, it emits diagnostics and returns null.
Jacques Pienaar9c411be2018-06-24 19:17:35 -0700753Module *mlir::parseSourceFile(llvm::SourceMgr &sourceMgr, MLIRContext *context,
754 const SMDiagnosticHandlerTy &errorReporter) {
755 return Parser(sourceMgr, context, errorReporter).parseModule();
Chris Lattnere79379a2018-06-22 10:39:19 -0700756}