blob: 7633a0929dac01a72bd02e649d5c6febf37864f0 [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"
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -070024#include "mlir/IR/AffineExpr.h"
MLIR Teamf85a6262018-06-27 11:03:08 -070025#include "mlir/IR/AffineMap.h"
Chris Lattner7121b802018-07-04 20:45:39 -070026#include "mlir/IR/Attributes.h"
Chris Lattner158e0a3e2018-07-08 20:51:38 -070027#include "mlir/IR/Builders.h"
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -070028#include "mlir/IR/MLFunction.h"
Chris Lattner21e67f62018-07-06 10:46:19 -070029#include "mlir/IR/Module.h"
30#include "mlir/IR/OperationSet.h"
Chris Lattnerf7e22732018-06-22 22:03:48 -070031#include "mlir/IR/Types.h"
Chris Lattnere79379a2018-06-22 10:39:19 -070032#include "llvm/Support/SourceMgr.h"
33using namespace mlir;
34using llvm::SourceMgr;
Chris Lattner4c95a502018-06-23 16:03:42 -070035using llvm::SMLoc;
Chris Lattnere79379a2018-06-22 10:39:19 -070036
37namespace {
Chris Lattner4c95a502018-06-23 16:03:42 -070038class CFGFunctionParserState;
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -070039class AffineMapParserState;
Chris Lattner4c95a502018-06-23 16:03:42 -070040
Chris Lattnerf7e22732018-06-22 22:03:48 -070041/// Simple enum to make code read better in cases that would otherwise return a
42/// bool value. Failure is "true" in a boolean context.
Chris Lattnere79379a2018-06-22 10:39:19 -070043enum ParseResult {
44 ParseSuccess,
45 ParseFailure
46};
47
Uday Bondhugula015cbb12018-07-03 20:16:08 -070048/// Lower precedence ops (all at the same precedence level). LNoOp is false in
49/// the boolean sense.
50enum AffineLowPrecOp {
51 /// Null value.
52 LNoOp,
53 Add,
54 Sub
55};
56
57/// Higher precedence ops - all at the same precedence level. HNoOp is false in
58/// the boolean sense.
59enum AffineHighPrecOp {
60 /// Null value.
61 HNoOp,
62 Mul,
63 FloorDiv,
64 CeilDiv,
65 Mod
66};
67
Chris Lattnere79379a2018-06-22 10:39:19 -070068/// Main parser implementation.
69class Parser {
Chris Lattnered65a732018-06-28 20:45:33 -070070public:
Jacques Pienaar9c411be2018-06-24 19:17:35 -070071 Parser(llvm::SourceMgr &sourceMgr, MLIRContext *context,
Jacques Pienaar7b829702018-07-03 13:24:09 -070072 SMDiagnosticHandlerTy errorReporter)
Chris Lattner158e0a3e2018-07-08 20:51:38 -070073 : builder(context), lex(sourceMgr, errorReporter),
Jacques Pienaar7b829702018-07-03 13:24:09 -070074 curToken(lex.lexToken()), errorReporter(std::move(errorReporter)) {
Chris Lattner158e0a3e2018-07-08 20:51:38 -070075 module.reset(new Module(context));
Chris Lattnere79379a2018-06-22 10:39:19 -070076 }
77
78 Module *parseModule();
79private:
80 // State.
Chris Lattner158e0a3e2018-07-08 20:51:38 -070081 Builder builder;
Chris Lattnerf7e22732018-06-22 22:03:48 -070082
83 // The lexer for the source file we're parsing.
Chris Lattnere79379a2018-06-22 10:39:19 -070084 Lexer lex;
85
86 // This is the next token that hasn't been consumed yet.
87 Token curToken;
88
Jacques Pienaar9c411be2018-06-24 19:17:35 -070089 // The diagnostic error reporter.
Jacques Pienaar7b829702018-07-03 13:24:09 -070090 SMDiagnosticHandlerTy errorReporter;
Jacques Pienaar9c411be2018-06-24 19:17:35 -070091
Chris Lattnere79379a2018-06-22 10:39:19 -070092 // This is the result module we are parsing into.
93 std::unique_ptr<Module> module;
94
MLIR Teamf85a6262018-06-27 11:03:08 -070095 // A map from affine map identifier to AffineMap.
Chris Lattner7121b802018-07-04 20:45:39 -070096 llvm::StringMap<AffineMap*> affineMapDefinitions;
MLIR Teamf85a6262018-06-27 11:03:08 -070097
Chris Lattnere79379a2018-06-22 10:39:19 -070098private:
99 // Helper methods.
100
101 /// Emit an error and return failure.
Chris Lattner4c95a502018-06-23 16:03:42 -0700102 ParseResult emitError(const Twine &message) {
103 return emitError(curToken.getLoc(), message);
104 }
105 ParseResult emitError(SMLoc loc, const Twine &message);
Chris Lattnere79379a2018-06-22 10:39:19 -0700106
107 /// Advance the current lexer onto the next token.
108 void consumeToken() {
109 assert(curToken.isNot(Token::eof, Token::error) &&
110 "shouldn't advance past EOF or errors");
111 curToken = lex.lexToken();
112 }
113
114 /// Advance the current lexer onto the next token, asserting what the expected
115 /// current token is. This is preferred to the above method because it leads
116 /// to more self-documenting code with better checking.
Chris Lattner8da0c282018-06-29 11:15:56 -0700117 void consumeToken(Token::Kind kind) {
Chris Lattnere79379a2018-06-22 10:39:19 -0700118 assert(curToken.is(kind) && "consumed an unexpected token");
119 consumeToken();
120 }
121
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700122 /// If the current token has the specified kind, consume it and return true.
123 /// If not, return false.
Chris Lattner8da0c282018-06-29 11:15:56 -0700124 bool consumeIf(Token::Kind kind) {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700125 if (curToken.isNot(kind))
126 return false;
127 consumeToken(kind);
128 return true;
129 }
130
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700131 // Binary affine op parsing
132 AffineLowPrecOp consumeIfLowPrecOp();
133 AffineHighPrecOp consumeIfHighPrecOp();
134
Chris Lattner8da0c282018-06-29 11:15:56 -0700135 ParseResult parseCommaSeparatedList(Token::Kind rightToken,
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700136 const std::function<ParseResult()> &parseElement,
137 bool allowEmptyList = true);
138
Chris Lattnerf7e22732018-06-22 22:03:48 -0700139 // We have two forms of parsing methods - those that return a non-null
140 // pointer on success, and those that return a ParseResult to indicate whether
141 // they returned a failure. The second class fills in by-reference arguments
142 // as the results of their action.
143
Chris Lattnere79379a2018-06-22 10:39:19 -0700144 // Type parsing.
Chris Lattnerf958bbe2018-06-29 22:08:05 -0700145 Type *parsePrimitiveType();
Chris Lattnerf7e22732018-06-22 22:03:48 -0700146 Type *parseElementType();
147 VectorType *parseVectorType();
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700148 ParseResult parseDimensionListRanked(SmallVectorImpl<int> &dimensions);
Chris Lattnerf7e22732018-06-22 22:03:48 -0700149 Type *parseTensorType();
150 Type *parseMemRefType();
151 Type *parseFunctionType();
152 Type *parseType();
153 ParseResult parseTypeList(SmallVectorImpl<Type*> &elements);
Chris Lattnere79379a2018-06-22 10:39:19 -0700154
Chris Lattner7121b802018-07-04 20:45:39 -0700155 // Attribute parsing.
156 Attribute *parseAttribute();
157 ParseResult parseAttributeDict(SmallVectorImpl<NamedAttribute> &attributes);
158
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700159 // Parsing identifiers' lists for polyhedral structures.
160 ParseResult parseDimIdList(AffineMapParserState &state);
161 ParseResult parseSymbolIdList(AffineMapParserState &state);
162 ParseResult parseDimOrSymbolId(AffineMapParserState &state, bool dim);
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700163
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700164 // Polyhedral structures.
MLIR Teamf85a6262018-06-27 11:03:08 -0700165 ParseResult parseAffineMapDef();
Chris Lattner7121b802018-07-04 20:45:39 -0700166 AffineMap *parseAffineMapInline(StringRef mapId);
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700167 AffineExpr *parseAffineExpr(const AffineMapParserState &state);
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700168 AffineExpr *parseParentheticalExpr(const AffineMapParserState &state);
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700169 AffineExpr *parseNegateExpression(AffineExpr *lhs,
170 const AffineMapParserState &state);
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700171 AffineExpr *parseIntegerExpr(const AffineMapParserState &state);
172 AffineExpr *parseBareIdExpr(const AffineMapParserState &state);
173
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700174 AffineExpr *getBinaryAffineOpExpr(AffineHighPrecOp op, AffineExpr *lhs,
175 AffineExpr *rhs);
176 AffineExpr *getBinaryAffineOpExpr(AffineLowPrecOp op, AffineExpr *lhs,
177 AffineExpr *rhs);
178 AffineExpr *parseAffineOperandExpr(AffineExpr *lhs,
179 const AffineMapParserState &state);
180 AffineExpr *parseAffineLowPrecOpExpr(AffineExpr *llhs, AffineLowPrecOp llhsOp,
181 const AffineMapParserState &state);
182 AffineExpr *parseAffineHighPrecOpExpr(AffineExpr *llhs,
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700183 AffineHighPrecOp llhsOp,
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700184 const AffineMapParserState &state);
MLIR Teamf85a6262018-06-27 11:03:08 -0700185
Chris Lattner78276e32018-07-07 15:48:26 -0700186 // SSA
187 ParseResult parseSSAUse();
188 ParseResult parseOptionalSSAUseList(Token::Kind endToken);
189 ParseResult parseSSAUseAndType();
190 ParseResult parseOptionalSSAUseAndTypeList(Token::Kind endToken);
191
Chris Lattner4c95a502018-06-23 16:03:42 -0700192 // Functions.
Chris Lattnerf7e22732018-06-22 22:03:48 -0700193 ParseResult parseFunctionSignature(StringRef &name, FunctionType *&type);
Chris Lattnere79379a2018-06-22 10:39:19 -0700194 ParseResult parseExtFunc();
Chris Lattner4c95a502018-06-23 16:03:42 -0700195 ParseResult parseCFGFunc();
196 ParseResult parseBasicBlock(CFGFunctionParserState &functionState);
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -0700197 Statement *parseStatement(ParentType parent);
Chris Lattnerf6d80a02018-06-24 11:18:29 -0700198
Chris Lattner3a467cc2018-07-01 20:28:00 -0700199 OperationInst *parseCFGOperation(CFGFunctionParserState &functionState);
200 TerminatorInst *parseTerminator(CFGFunctionParserState &functionState);
Chris Lattnered65a732018-06-28 20:45:33 -0700201
Chris Lattner78276e32018-07-07 15:48:26 -0700202 ParseResult parseMLFunc();
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -0700203 ForStmt *parseForStmt(ParentType parent);
204 IfStmt *parseIfStmt(ParentType parent);
205 ParseResult parseNestedStatements(NodeStmt *parent);
Chris Lattnere79379a2018-06-22 10:39:19 -0700206};
207} // end anonymous namespace
208
209//===----------------------------------------------------------------------===//
210// Helper methods.
211//===----------------------------------------------------------------------===//
212
Chris Lattner4c95a502018-06-23 16:03:42 -0700213ParseResult Parser::emitError(SMLoc loc, const Twine &message) {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700214 // If we hit a parse error in response to a lexer error, then the lexer
Jacques Pienaar9c411be2018-06-24 19:17:35 -0700215 // already reported the error.
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700216 if (curToken.is(Token::error))
217 return ParseFailure;
218
Jacques Pienaar9c411be2018-06-24 19:17:35 -0700219 errorReporter(
220 lex.getSourceMgr().GetMessage(loc, SourceMgr::DK_Error, message));
Chris Lattnere79379a2018-06-22 10:39:19 -0700221 return ParseFailure;
222}
223
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700224/// Parse a comma-separated list of elements, terminated with an arbitrary
225/// token. This allows empty lists if allowEmptyList is true.
226///
227/// abstract-list ::= rightToken // if allowEmptyList == true
228/// abstract-list ::= element (',' element)* rightToken
229///
230ParseResult Parser::
Chris Lattner8da0c282018-06-29 11:15:56 -0700231parseCommaSeparatedList(Token::Kind rightToken,
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700232 const std::function<ParseResult()> &parseElement,
233 bool allowEmptyList) {
234 // Handle the empty case.
235 if (curToken.is(rightToken)) {
236 if (!allowEmptyList)
237 return emitError("expected list element");
238 consumeToken(rightToken);
239 return ParseSuccess;
240 }
241
242 // Non-empty case starts with an element.
243 if (parseElement())
244 return ParseFailure;
245
246 // Otherwise we have a list of comma separated elements.
247 while (consumeIf(Token::comma)) {
248 if (parseElement())
249 return ParseFailure;
250 }
251
252 // Consume the end character.
253 if (!consumeIf(rightToken))
Chris Lattner8da0c282018-06-29 11:15:56 -0700254 return emitError("expected ',' or '" + Token::getTokenSpelling(rightToken) +
255 "'");
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700256
257 return ParseSuccess;
258}
Chris Lattnere79379a2018-06-22 10:39:19 -0700259
260//===----------------------------------------------------------------------===//
261// Type Parsing
262//===----------------------------------------------------------------------===//
263
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700264/// Parse the low-level fixed dtypes in the system.
265///
Chris Lattnerf958bbe2018-06-29 22:08:05 -0700266/// primitive-type ::= `f16` | `bf16` | `f32` | `f64`
267/// primitive-type ::= integer-type
268/// primitive-type ::= `affineint`
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700269///
Chris Lattnerf958bbe2018-06-29 22:08:05 -0700270Type *Parser::parsePrimitiveType() {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700271 switch (curToken.getKind()) {
Chris Lattnerf7e22732018-06-22 22:03:48 -0700272 default:
273 return (emitError("expected type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700274 case Token::kw_bf16:
275 consumeToken(Token::kw_bf16);
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700276 return builder.getBF16Type();
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700277 case Token::kw_f16:
278 consumeToken(Token::kw_f16);
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700279 return builder.getF16Type();
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700280 case Token::kw_f32:
281 consumeToken(Token::kw_f32);
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700282 return builder.getF32Type();
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700283 case Token::kw_f64:
284 consumeToken(Token::kw_f64);
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700285 return builder.getF64Type();
Chris Lattnerf958bbe2018-06-29 22:08:05 -0700286 case Token::kw_affineint:
287 consumeToken(Token::kw_affineint);
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700288 return builder.getAffineIntType();
Chris Lattnerf958bbe2018-06-29 22:08:05 -0700289 case Token::inttype: {
290 auto width = curToken.getIntTypeBitwidth();
291 if (!width.hasValue())
292 return (emitError("invalid integer width"), nullptr);
293 consumeToken(Token::inttype);
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700294 return builder.getIntegerType(width.getValue());
Chris Lattnerf958bbe2018-06-29 22:08:05 -0700295 }
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700296 }
297}
298
299/// Parse the element type of a tensor or memref type.
300///
301/// element-type ::= primitive-type | vector-type
302///
Chris Lattnerf7e22732018-06-22 22:03:48 -0700303Type *Parser::parseElementType() {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700304 if (curToken.is(Token::kw_vector))
305 return parseVectorType();
306
307 return parsePrimitiveType();
308}
309
310/// Parse a vector type.
311///
312/// vector-type ::= `vector` `<` const-dimension-list primitive-type `>`
313/// const-dimension-list ::= (integer-literal `x`)+
314///
Chris Lattnerf7e22732018-06-22 22:03:48 -0700315VectorType *Parser::parseVectorType() {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700316 consumeToken(Token::kw_vector);
317
318 if (!consumeIf(Token::less))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700319 return (emitError("expected '<' in vector type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700320
321 if (curToken.isNot(Token::integer))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700322 return (emitError("expected dimension size in vector type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700323
324 SmallVector<unsigned, 4> dimensions;
325 while (curToken.is(Token::integer)) {
326 // Make sure this integer value is in bound and valid.
327 auto dimension = curToken.getUnsignedIntegerValue();
328 if (!dimension.hasValue())
Chris Lattnerf7e22732018-06-22 22:03:48 -0700329 return (emitError("invalid dimension in vector type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700330 dimensions.push_back(dimension.getValue());
331
332 consumeToken(Token::integer);
333
334 // Make sure we have an 'x' or something like 'xbf32'.
335 if (curToken.isNot(Token::bare_identifier) ||
336 curToken.getSpelling()[0] != 'x')
Chris Lattnerf7e22732018-06-22 22:03:48 -0700337 return (emitError("expected 'x' in vector dimension list"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700338
339 // If we had a prefix of 'x', lex the next token immediately after the 'x'.
340 if (curToken.getSpelling().size() != 1)
341 lex.resetPointer(curToken.getSpelling().data()+1);
342
343 // Consume the 'x'.
344 consumeToken(Token::bare_identifier);
345 }
346
347 // Parse the element type.
Chris Lattnerf7e22732018-06-22 22:03:48 -0700348 auto *elementType = parsePrimitiveType();
349 if (!elementType)
350 return nullptr;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700351
352 if (!consumeIf(Token::greater))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700353 return (emitError("expected '>' in vector type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700354
Chris Lattnerf7e22732018-06-22 22:03:48 -0700355 return VectorType::get(dimensions, elementType);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700356}
357
358/// Parse a dimension list of a tensor or memref type. This populates the
359/// dimension list, returning -1 for the '?' dimensions.
360///
361/// dimension-list-ranked ::= (dimension `x`)*
362/// dimension ::= `?` | integer-literal
363///
364ParseResult Parser::parseDimensionListRanked(SmallVectorImpl<int> &dimensions) {
365 while (curToken.isAny(Token::integer, Token::question)) {
366 if (consumeIf(Token::question)) {
367 dimensions.push_back(-1);
368 } else {
369 // Make sure this integer value is in bound and valid.
370 auto dimension = curToken.getUnsignedIntegerValue();
371 if (!dimension.hasValue() || (int)dimension.getValue() < 0)
372 return emitError("invalid dimension");
373 dimensions.push_back((int)dimension.getValue());
374 consumeToken(Token::integer);
375 }
376
377 // Make sure we have an 'x' or something like 'xbf32'.
378 if (curToken.isNot(Token::bare_identifier) ||
379 curToken.getSpelling()[0] != 'x')
380 return emitError("expected 'x' in dimension list");
381
382 // If we had a prefix of 'x', lex the next token immediately after the 'x'.
383 if (curToken.getSpelling().size() != 1)
384 lex.resetPointer(curToken.getSpelling().data()+1);
385
386 // Consume the 'x'.
387 consumeToken(Token::bare_identifier);
388 }
389
390 return ParseSuccess;
391}
392
393/// Parse a tensor type.
394///
395/// tensor-type ::= `tensor` `<` dimension-list element-type `>`
396/// dimension-list ::= dimension-list-ranked | `??`
397///
Chris Lattnerf7e22732018-06-22 22:03:48 -0700398Type *Parser::parseTensorType() {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700399 consumeToken(Token::kw_tensor);
400
401 if (!consumeIf(Token::less))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700402 return (emitError("expected '<' in tensor type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700403
404 bool isUnranked;
405 SmallVector<int, 4> dimensions;
406
407 if (consumeIf(Token::questionquestion)) {
408 isUnranked = true;
409 } else {
410 isUnranked = false;
411 if (parseDimensionListRanked(dimensions))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700412 return nullptr;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700413 }
414
415 // Parse the element type.
Chris Lattnerf7e22732018-06-22 22:03:48 -0700416 auto elementType = parseElementType();
417 if (!elementType)
418 return nullptr;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700419
420 if (!consumeIf(Token::greater))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700421 return (emitError("expected '>' in tensor type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700422
MLIR Team355ec862018-06-23 18:09:09 -0700423 if (isUnranked)
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700424 return builder.getTensorType(elementType);
425 return builder.getTensorType(dimensions, elementType);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700426}
427
428/// Parse a memref type.
429///
430/// memref-type ::= `memref` `<` dimension-list-ranked element-type
431/// (`,` semi-affine-map-composition)? (`,` memory-space)? `>`
432///
433/// semi-affine-map-composition ::= (semi-affine-map `,` )* semi-affine-map
434/// memory-space ::= integer-literal /* | TODO: address-space-id */
435///
Chris Lattnerf7e22732018-06-22 22:03:48 -0700436Type *Parser::parseMemRefType() {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700437 consumeToken(Token::kw_memref);
438
439 if (!consumeIf(Token::less))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700440 return (emitError("expected '<' in memref type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700441
442 SmallVector<int, 4> dimensions;
443 if (parseDimensionListRanked(dimensions))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700444 return nullptr;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700445
446 // Parse the element type.
Chris Lattnerf7e22732018-06-22 22:03:48 -0700447 auto elementType = parseElementType();
448 if (!elementType)
449 return nullptr;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700450
451 // TODO: Parse semi-affine-map-composition.
452 // TODO: Parse memory-space.
453
454 if (!consumeIf(Token::greater))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700455 return (emitError("expected '>' in memref type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700456
Chris Lattnerf7e22732018-06-22 22:03:48 -0700457 // FIXME: Add an IR representation for memref types.
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700458 return builder.getIntegerType(1);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700459}
460
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700461/// Parse a function type.
462///
463/// function-type ::= type-list-parens `->` type-list
464///
Chris Lattnerf7e22732018-06-22 22:03:48 -0700465Type *Parser::parseFunctionType() {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700466 assert(curToken.is(Token::l_paren));
467
Chris Lattnerf7e22732018-06-22 22:03:48 -0700468 SmallVector<Type*, 4> arguments;
469 if (parseTypeList(arguments))
470 return nullptr;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700471
472 if (!consumeIf(Token::arrow))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700473 return (emitError("expected '->' in function type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700474
Chris Lattnerf7e22732018-06-22 22:03:48 -0700475 SmallVector<Type*, 4> results;
476 if (parseTypeList(results))
477 return nullptr;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700478
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700479 return builder.getFunctionType(arguments, results);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700480}
481
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700482/// Parse an arbitrary type.
483///
484/// type ::= primitive-type
485/// | vector-type
486/// | tensor-type
487/// | memref-type
488/// | function-type
489/// element-type ::= primitive-type | vector-type
490///
Chris Lattnerf7e22732018-06-22 22:03:48 -0700491Type *Parser::parseType() {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700492 switch (curToken.getKind()) {
493 case Token::kw_memref: return parseMemRefType();
494 case Token::kw_tensor: return parseTensorType();
495 case Token::kw_vector: return parseVectorType();
496 case Token::l_paren: return parseFunctionType();
497 default:
498 return parsePrimitiveType();
499 }
500}
501
502/// Parse a "type list", which is a singular type, or a parenthesized list of
503/// types.
504///
505/// type-list ::= type-list-parens | type
506/// type-list-parens ::= `(` `)`
507/// | `(` type (`,` type)* `)`
508///
Chris Lattnerf7e22732018-06-22 22:03:48 -0700509ParseResult Parser::parseTypeList(SmallVectorImpl<Type*> &elements) {
510 auto parseElt = [&]() -> ParseResult {
511 auto elt = parseType();
512 elements.push_back(elt);
513 return elt ? ParseSuccess : ParseFailure;
514 };
515
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700516 // If there is no parens, then it must be a singular type.
517 if (!consumeIf(Token::l_paren))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700518 return parseElt();
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700519
Chris Lattnerf7e22732018-06-22 22:03:48 -0700520 if (parseCommaSeparatedList(Token::r_paren, parseElt))
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700521 return ParseFailure;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700522
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700523 return ParseSuccess;
524}
525
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700526namespace {
527/// This class represents the transient parser state while parsing an affine
528/// expression.
529class AffineMapParserState {
530 public:
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700531 explicit AffineMapParserState() {}
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700532
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700533 void addDim(StringRef sRef) { dims.insert({sRef, dims.size()}); }
534 void addSymbol(StringRef sRef) { symbols.insert({sRef, symbols.size()}); }
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700535
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700536 unsigned getNumDims() const { return dims.size(); }
537 unsigned getNumSymbols() const { return symbols.size(); }
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700538
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700539 // TODO(bondhugula): could just use an vector/ArrayRef and scan the numbers.
540 const llvm::StringMap<unsigned> &getDims() const { return dims; }
541 const llvm::StringMap<unsigned> &getSymbols() const { return symbols; }
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700542
543 private:
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700544 llvm::StringMap<unsigned> dims;
545 llvm::StringMap<unsigned> symbols;
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700546};
547} // end anonymous namespace
548
Chris Lattner4c95a502018-06-23 16:03:42 -0700549//===----------------------------------------------------------------------===//
Chris Lattner7121b802018-07-04 20:45:39 -0700550// Attribute parsing.
551//===----------------------------------------------------------------------===//
552
553
554/// Attribute parsing.
555///
556/// attribute-value ::= bool-literal
557/// | integer-literal
558/// | float-literal
559/// | string-literal
560/// | `[` (attribute-value (`,` attribute-value)*)? `]`
561///
562Attribute *Parser::parseAttribute() {
563 switch (curToken.getKind()) {
564 case Token::kw_true:
565 consumeToken(Token::kw_true);
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700566 return BoolAttr::get(true, builder.getContext());
Chris Lattner7121b802018-07-04 20:45:39 -0700567 case Token::kw_false:
568 consumeToken(Token::kw_false);
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700569 return BoolAttr::get(false, builder.getContext());
Chris Lattner7121b802018-07-04 20:45:39 -0700570
571 case Token::integer: {
572 auto val = curToken.getUInt64IntegerValue();
573 if (!val.hasValue() || (int64_t)val.getValue() < 0)
574 return (emitError("integer too large for attribute"), nullptr);
575 consumeToken(Token::integer);
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700576 return IntegerAttr::get((int64_t)val.getValue(), builder.getContext());
Chris Lattner7121b802018-07-04 20:45:39 -0700577 }
578
579 case Token::minus: {
580 consumeToken(Token::minus);
581 if (curToken.is(Token::integer)) {
582 auto val = curToken.getUInt64IntegerValue();
583 if (!val.hasValue() || (int64_t)-val.getValue() >= 0)
584 return (emitError("integer too large for attribute"), nullptr);
585 consumeToken(Token::integer);
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700586 return IntegerAttr::get((int64_t)-val.getValue(), builder.getContext());
Chris Lattner7121b802018-07-04 20:45:39 -0700587 }
588
589 return (emitError("expected constant integer or floating point value"),
590 nullptr);
591 }
592
593 case Token::string: {
594 auto val = curToken.getStringValue();
595 consumeToken(Token::string);
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700596 return StringAttr::get(val, builder.getContext());
Chris Lattner7121b802018-07-04 20:45:39 -0700597 }
598
599 case Token::l_bracket: {
600 consumeToken(Token::l_bracket);
601 SmallVector<Attribute*, 4> elements;
602
603 auto parseElt = [&]() -> ParseResult {
604 elements.push_back(parseAttribute());
605 return elements.back() ? ParseSuccess : ParseFailure;
606 };
607
608 if (parseCommaSeparatedList(Token::r_bracket, parseElt))
609 return nullptr;
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700610 return ArrayAttr::get(elements, builder.getContext());
Chris Lattner7121b802018-07-04 20:45:39 -0700611 }
612 default:
613 // TODO: Handle floating point.
614 return (emitError("expected constant attribute value"), nullptr);
615 }
616}
617
Chris Lattner7121b802018-07-04 20:45:39 -0700618/// Attribute dictionary.
619///
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700620/// attribute-dict ::= `{` `}`
621/// | `{` attribute-entry (`,` attribute-entry)* `}`
622/// attribute-entry ::= bare-id `:` attribute-value
Chris Lattner7121b802018-07-04 20:45:39 -0700623///
624ParseResult Parser::parseAttributeDict(
625 SmallVectorImpl<NamedAttribute> &attributes) {
626 consumeToken(Token::l_brace);
627
628 auto parseElt = [&]() -> ParseResult {
629 // We allow keywords as attribute names.
630 if (curToken.isNot(Token::bare_identifier, Token::inttype) &&
631 !curToken.isKeyword())
632 return emitError("expected attribute name");
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700633 auto nameId = Identifier::get(curToken.getSpelling(), builder.getContext());
Chris Lattner7121b802018-07-04 20:45:39 -0700634 consumeToken();
635
636 if (!consumeIf(Token::colon))
637 return emitError("expected ':' in attribute list");
638
639 auto attr = parseAttribute();
640 if (!attr) return ParseFailure;
641
642 attributes.push_back({nameId, attr});
643 return ParseSuccess;
644 };
645
646 if (parseCommaSeparatedList(Token::r_brace, parseElt))
647 return ParseFailure;
648
649 return ParseSuccess;
650}
651
652//===----------------------------------------------------------------------===//
MLIR Teamf85a6262018-06-27 11:03:08 -0700653// Polyhedral structures.
654//===----------------------------------------------------------------------===//
655
656/// Affine map declaration.
657///
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700658/// affine-map-def ::= affine-map-id `=` affine-map-inline
MLIR Teamf85a6262018-06-27 11:03:08 -0700659///
660ParseResult Parser::parseAffineMapDef() {
Chris Lattner78276e32018-07-07 15:48:26 -0700661 assert(curToken.is(Token::hash_identifier));
MLIR Teamf85a6262018-06-27 11:03:08 -0700662
663 StringRef affineMapId = curToken.getSpelling().drop_front();
Chris Lattner7121b802018-07-04 20:45:39 -0700664
665 // Check for redefinitions.
666 auto *&entry = affineMapDefinitions[affineMapId];
667 if (entry)
668 return emitError("redefinition of affine map id '" + affineMapId + "'");
669
Chris Lattner78276e32018-07-07 15:48:26 -0700670 consumeToken(Token::hash_identifier);
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700671
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700672 // Parse the '='
673 if (!consumeIf(Token::equal))
674 return emitError("expected '=' in affine map outlined definition");
MLIR Teamf85a6262018-06-27 11:03:08 -0700675
Chris Lattner7121b802018-07-04 20:45:39 -0700676 entry = parseAffineMapInline(affineMapId);
677 if (!entry)
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700678 return ParseFailure;
MLIR Teamf85a6262018-06-27 11:03:08 -0700679
Chris Lattner7121b802018-07-04 20:45:39 -0700680 module->affineMapList.push_back(entry);
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700681 return ParseSuccess;
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700682}
683
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700684/// Create an affine binary high precedence op expression (mul's, div's, mod)
685AffineExpr *Parser::getBinaryAffineOpExpr(AffineHighPrecOp op, AffineExpr *lhs,
686 AffineExpr *rhs) {
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700687 switch (op) {
688 case Mul:
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700689 if (!lhs->isSymbolic() && !rhs->isSymbolic()) {
690 emitError("non-affine expression: at least one of the multiply "
691 "operands has to be either a constant or symbolic");
692 return nullptr;
693 }
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700694 return AffineMulExpr::get(lhs, rhs, builder.getContext());
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700695 case FloorDiv:
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700696 if (!rhs->isSymbolic()) {
697 emitError("non-affine expression: right operand of floordiv "
698 "has to be either a constant or symbolic");
699 return nullptr;
700 }
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700701 return AffineFloorDivExpr::get(lhs, rhs, builder.getContext());
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700702 case CeilDiv:
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700703 if (!rhs->isSymbolic()) {
704 emitError("non-affine expression: right operand of ceildiv "
705 "has to be either a constant or symbolic");
706 return nullptr;
707 }
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700708 return AffineCeilDivExpr::get(lhs, rhs, builder.getContext());
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700709 case Mod:
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700710 if (!rhs->isSymbolic()) {
711 emitError("non-affine expression: right operand of mod "
712 "has to be either a constant or symbolic");
713 return nullptr;
714 }
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700715 return AffineModExpr::get(lhs, rhs, builder.getContext());
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700716 case HNoOp:
717 llvm_unreachable("can't create affine expression for null high prec op");
718 return nullptr;
719 }
720}
721
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700722/// Create an affine binary low precedence op expression (add, sub).
723AffineExpr *Parser::getBinaryAffineOpExpr(AffineLowPrecOp op, AffineExpr *lhs,
724 AffineExpr *rhs) {
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700725 switch (op) {
726 case AffineLowPrecOp::Add:
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700727 return AffineAddExpr::get(lhs, rhs, builder.getContext());
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700728 case AffineLowPrecOp::Sub:
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700729 return AffineSubExpr::get(lhs, rhs, builder.getContext());
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700730 case AffineLowPrecOp::LNoOp:
731 llvm_unreachable("can't create affine expression for null low prec op");
732 return nullptr;
733 }
734}
735
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700736/// Consume this token if it is a lower precedence affine op (there are only two
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700737/// precedence levels).
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700738AffineLowPrecOp Parser::consumeIfLowPrecOp() {
739 switch (curToken.getKind()) {
740 case Token::plus:
741 consumeToken(Token::plus);
742 return AffineLowPrecOp::Add;
743 case Token::minus:
744 consumeToken(Token::minus);
745 return AffineLowPrecOp::Sub;
746 default:
747 return AffineLowPrecOp::LNoOp;
748 }
749}
750
751/// Consume this token if it is a higher precedence affine op (there are only
752/// two precedence levels)
753AffineHighPrecOp Parser::consumeIfHighPrecOp() {
754 switch (curToken.getKind()) {
755 case Token::star:
756 consumeToken(Token::star);
757 return Mul;
758 case Token::kw_floordiv:
759 consumeToken(Token::kw_floordiv);
760 return FloorDiv;
761 case Token::kw_ceildiv:
762 consumeToken(Token::kw_ceildiv);
763 return CeilDiv;
764 case Token::kw_mod:
765 consumeToken(Token::kw_mod);
766 return Mod;
767 default:
768 return HNoOp;
769 }
770}
771
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700772/// Parse a high precedence op expression list: mul, div, and mod are high
773/// precedence binary ops, i.e., parse a
774/// expr_1 op_1 expr_2 op_2 ... expr_n
775/// where op_1, op_2 are all a AffineHighPrecOp (mul, div, mod).
776/// All affine binary ops are left associative.
777/// Given llhs, returns (llhs llhsOp lhs) op rhs, or (lhs op rhs) if llhs is
778/// null. If no rhs can be found, returns (llhs llhsOp lhs) or lhs if llhs is
779/// null.
780AffineExpr *
781Parser::parseAffineHighPrecOpExpr(AffineExpr *llhs, AffineHighPrecOp llhsOp,
782 const AffineMapParserState &state) {
783 AffineExpr *lhs = parseAffineOperandExpr(llhs, state);
784 if (!lhs)
785 return nullptr;
786
787 AffineHighPrecOp op = HNoOp;
788 // Found an LHS. Parse the remaining expression.
789 if ((op = consumeIfHighPrecOp())) {
790 if (llhs) {
791 AffineExpr *expr = getBinaryAffineOpExpr(llhsOp, llhs, lhs);
792 if (!expr)
793 return nullptr;
794 return parseAffineHighPrecOpExpr(expr, op, state);
795 }
796 // No LLHS, get RHS
797 return parseAffineHighPrecOpExpr(lhs, op, state);
798 }
799
800 // This is the last operand in this expression.
801 if (llhs)
802 return getBinaryAffineOpExpr(llhsOp, llhs, lhs);
803
804 // No llhs, 'lhs' itself is the expression.
805 return lhs;
806}
807
808/// Parse an affine expression inside parentheses.
809///
810/// affine-expr ::= `(` affine-expr `)`
811AffineExpr *Parser::parseParentheticalExpr(const AffineMapParserState &state) {
812 if (!consumeIf(Token::l_paren))
813 return (emitError("expected '('"), nullptr);
814 if (curToken.is(Token::r_paren))
815 return (emitError("no expression inside parentheses"), nullptr);
816 auto *expr = parseAffineExpr(state);
817 if (!expr)
818 // Error would have been emitted by parseAffineExpr.
819 return nullptr;
820 if (!consumeIf(Token::r_paren))
821 return (emitError("expected ')'"), nullptr);
822 return expr;
823}
824
825/// Parse the negation expression.
826///
827/// affine-expr ::= `-` affine-expr
828AffineExpr *Parser::parseNegateExpression(AffineExpr *lhs,
829 const AffineMapParserState &state) {
830 if (!consumeIf(Token::minus))
831 return (emitError("expected '-'"), nullptr);
832
833 AffineExpr *operand = parseAffineOperandExpr(lhs, state);
834 // Since negation has the highest precedence of all ops (including high
835 // precedence ops) but lower than parentheses, we are only going to use
836 // parseAffineOperandExpr instead of parseAffineExpr here.
837 if (!operand)
838 // Extra error message although parseAffineOperandExpr would have
839 // complained. Leads to a better diagnostic.
840 return (emitError("missing operand of negation"), nullptr);
841 AffineConstantExpr *minusOne =
842 AffineConstantExpr::get(-1, builder.getContext());
843 return AffineMulExpr::get(minusOne, operand, builder.getContext());
844}
845
846/// Parse a bare id that may appear in an affine expression.
847///
848/// affine-expr ::= bare-id
849AffineExpr *Parser::parseBareIdExpr(const AffineMapParserState &state) {
850 if (curToken.isNot(Token::bare_identifier))
851 return (emitError("expected bare identifier"), nullptr);
852
853 StringRef sRef = curToken.getSpelling();
854 const auto &dims = state.getDims();
855 const auto &symbols = state.getSymbols();
856 if (dims.count(sRef)) {
857 consumeToken(Token::bare_identifier);
858 return AffineDimExpr::get(dims.lookup(sRef), builder.getContext());
859 }
860 if (symbols.count(sRef)) {
861 consumeToken(Token::bare_identifier);
862 return AffineSymbolExpr::get(symbols.lookup(sRef), builder.getContext());
863 }
864 return (emitError("identifier is neither dimensional nor symbolic"), nullptr);
865}
866
867/// Parse a positive integral constant appearing in an affine expression.
868///
869/// affine-expr ::= integer-literal
870AffineExpr *Parser::parseIntegerExpr(const AffineMapParserState &state) {
871 // No need to handle negative numbers separately here. They are naturally
872 // handled via the unary negation operator, although (FIXME) MININT_64 still
873 // not correctly handled.
874 if (curToken.isNot(Token::integer))
875 return (emitError("expected integer"), nullptr);
876
877 auto val = curToken.getUInt64IntegerValue();
878 if (!val.hasValue() || (int64_t)val.getValue() < 0) {
879 return (emitError("constant too large for affineint"), nullptr);
880 }
881 consumeToken(Token::integer);
882 return AffineConstantExpr::get((int64_t)val.getValue(), builder.getContext());
883}
884
885/// Parses an expression that can be a valid operand of an affine expression.
886/// lhs: if non-null, an affine expression that is the lhs of a binary operator,
887/// the rhs of which is being parsed. This is used to determine whether an error
888/// should be emitted for a missing right operand.
889// Eg: for an expression without parentheses (like i + j + k + l), each
890// of the four identifiers is an operand. For i + j*k + l, j*k is not an
891// operand expression, it's an op expression and will be parsed via
892// parseAffineHighPrecOpExpression(). However, for i + (j*k) + -l, (j*k) and -l
893// are valid operands that will be parsed by this function.
894AffineExpr *Parser::parseAffineOperandExpr(AffineExpr *lhs,
895 const AffineMapParserState &state) {
896 switch (curToken.getKind()) {
897 case Token::bare_identifier:
898 return parseBareIdExpr(state);
899 case Token::integer:
900 return parseIntegerExpr(state);
901 case Token::l_paren:
902 return parseParentheticalExpr(state);
903 case Token::minus:
904 return parseNegateExpression(lhs, state);
905 default:
906 if (lhs)
907 emitError("missing right operand of binary op");
908 else
909 emitError("expected affine expression");
910 return nullptr;
911 }
912}
913
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700914/// Parse affine expressions that are bare-id's, integer constants,
915/// parenthetical affine expressions, and affine op expressions that are a
916/// composition of those.
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700917///
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700918/// All binary op's associate from left to right.
919///
920/// {add, sub} have lower precedence than {mul, div, and mod}.
921///
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700922/// Add, sub'are themselves at the same precedence level, mul, div, and mod are
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700923/// at the same higher precedence level.
924///
925/// llhs: the affine expression appearing on the left of the one being parsed.
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700926/// This function will return ((llhs llhsOp lhs) op rhs) if llhs is non null,
927/// and lhs op rhs otherwise; if there is no rhs, llhs llhsOp lhs is returned if
928/// llhs is non-null; otherwise lhs is returned. This is to deal with left
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700929/// associativity.
930///
931/// Eg: when the expression is e1 + e2*e3 + e4, with e1 as llhs, this function
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700932/// will return the affine expr equivalent of (e1 + (e2*e3)) + e4, where (e2*e3)
933/// will be parsed using parseAffineHighPrecOpExpr().
934AffineExpr *
935Parser::parseAffineLowPrecOpExpr(AffineExpr *llhs, AffineLowPrecOp llhsOp,
936 const AffineMapParserState &state) {
937 AffineExpr *lhs = parseAffineOperandExpr(llhs, state);
938 if (!lhs)
939 return nullptr;
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700940
941 // Found an LHS. Deal with the ops.
942 AffineLowPrecOp lOp;
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700943 AffineHighPrecOp hOp;
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700944 if ((lOp = consumeIfLowPrecOp())) {
945 if (llhs) {
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700946 AffineExpr *sum = getBinaryAffineOpExpr(llhsOp, llhs, lhs);
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700947 return parseAffineLowPrecOpExpr(sum, lOp, state);
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700948 }
949 // No LLHS, get RHS and form the expression.
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700950 return parseAffineLowPrecOpExpr(lhs, lOp, state);
951 }
952 if ((hOp = consumeIfHighPrecOp())) {
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700953 // We have a higher precedence op here. Get the rhs operand for the llhs
954 // through parseAffineHighPrecOpExpr.
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700955 AffineExpr *highRes = parseAffineHighPrecOpExpr(lhs, hOp, state);
956 if (!highRes)
957 return nullptr;
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700958 // If llhs is null, the product forms the first operand of the yet to be
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700959 // found expression. If non-null, the op to associate with llhs is llhsOp.
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700960 AffineExpr *expr =
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700961 llhs ? getBinaryAffineOpExpr(llhsOp, llhs, highRes) : highRes;
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700962 // Recurse for subsequent low prec op's after the affine high prec op
963 // expression.
964 AffineLowPrecOp nextOp;
965 if ((nextOp = consumeIfLowPrecOp()))
966 return parseAffineLowPrecOpExpr(expr, nextOp, state);
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700967 return expr;
968 }
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700969 // Last operand in the expression list.
970 if (llhs)
971 return getBinaryAffineOpExpr(llhsOp, llhs, lhs);
972 // No llhs, 'lhs' itself is the expression.
973 return lhs;
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700974}
975
976/// Parse an affine expression.
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700977/// affine-expr ::= `(` affine-expr `)`
978/// | `-` affine-expr
979/// | affine-expr `+` affine-expr
980/// | affine-expr `-` affine-expr
981/// | affine-expr `*` affine-expr
982/// | affine-expr `floordiv` affine-expr
983/// | affine-expr `ceildiv` affine-expr
984/// | affine-expr `mod` affine-expr
985/// | bare-id
986/// | integer-literal
987///
988/// Additional conditions are checked depending on the production. For eg., one
989/// of the operands for `*` has to be either constant/symbolic; the second
990/// operand for floordiv, ceildiv, and mod has to be a positive integer.
991/// Use 'state' to check if valid identifiers appear in the expressoins.
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700992AffineExpr *Parser::parseAffineExpr(const AffineMapParserState &state) {
993 switch (curToken.getKind()) {
994 case Token::l_paren:
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700995 case Token::bare_identifier:
996 case Token::minus:
997 case Token::integer:
998 return parseAffineLowPrecOpExpr(nullptr, AffineLowPrecOp::LNoOp, state);
999
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001000 case Token::kw_ceildiv:
1001 case Token::kw_floordiv:
Uday Bondhugula3934d4d2018-07-09 09:00:25 -07001002 case Token::kw_mod:
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001003 case Token::plus:
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001004 case Token::star:
1005 emitError("left operand of binary op missing");
1006 return nullptr;
1007
1008 default:
Uday Bondhugula3934d4d2018-07-09 09:00:25 -07001009 emitError("expected affine expression");
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001010 return nullptr;
1011 }
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001012}
1013
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001014/// Parse a dim or symbol from the lists appearing before the actual expressions
1015/// of the affine map. Update state to store the dimensional/symbolic
1016/// identifier. 'dim': whether it's the dim list or symbol list that is being
1017/// parsed.
1018ParseResult Parser::parseDimOrSymbolId(AffineMapParserState &state, bool dim) {
1019 if (curToken.isNot(Token::bare_identifier))
1020 return emitError("expected bare identifier");
1021 auto sRef = curToken.getSpelling();
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001022 consumeToken(Token::bare_identifier);
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001023 if (state.getDims().count(sRef) == 1)
1024 return emitError("dimensional identifier name reused");
1025 if (state.getSymbols().count(sRef) == 1)
1026 return emitError("symbolic identifier name reused");
1027 if (dim)
1028 state.addDim(sRef);
1029 else
1030 state.addSymbol(sRef);
1031 return ParseSuccess;
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001032}
1033
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001034/// Parse the list of symbolic identifiers to an affine map.
1035ParseResult Parser::parseSymbolIdList(AffineMapParserState &state) {
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001036 if (!consumeIf(Token::l_bracket)) return emitError("expected '['");
1037
1038 auto parseElt = [&]() -> ParseResult {
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001039 return parseDimOrSymbolId(state, false);
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001040 };
1041 return parseCommaSeparatedList(Token::r_bracket, parseElt);
1042}
1043
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001044/// Parse the list of dimensional identifiers to an affine map.
1045ParseResult Parser::parseDimIdList(AffineMapParserState &state) {
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001046 if (!consumeIf(Token::l_paren))
1047 return emitError("expected '(' at start of dimensional identifiers list");
1048
1049 auto parseElt = [&]() -> ParseResult {
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001050 return parseDimOrSymbolId(state, true);
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001051 };
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001052 return parseCommaSeparatedList(Token::r_paren, parseElt);
1053}
1054
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001055/// Parse an affine map definition.
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001056///
Uday Bondhugula3934d4d2018-07-09 09:00:25 -07001057/// affine-map-inline ::= dim-and-symbol-id-lists `->` multi-dim-affine-expr
1058/// (`size` `(` dim-size (`,` dim-size)* `)`)?
1059/// dim-size ::= affine-expr | `min` `(` affine-expr ( `,` affine-expr)+ `)`
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001060///
Uday Bondhugula3934d4d2018-07-09 09:00:25 -07001061/// multi-dim-affine-expr ::= `(` affine-expr (`,` affine-expr)* `)
1062// TODO(bondhugula): parse range size information.
Chris Lattner7121b802018-07-04 20:45:39 -07001063AffineMap *Parser::parseAffineMapInline(StringRef mapId) {
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001064 AffineMapParserState state;
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001065
1066 // List of dimensional identifiers.
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001067 if (parseDimIdList(state))
Chris Lattner7121b802018-07-04 20:45:39 -07001068 return nullptr;
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001069
1070 // Symbols are optional.
1071 if (curToken.is(Token::l_bracket)) {
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001072 if (parseSymbolIdList(state))
Chris Lattner7121b802018-07-04 20:45:39 -07001073 return nullptr;
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001074 }
1075 if (!consumeIf(Token::arrow)) {
Chris Lattner7121b802018-07-04 20:45:39 -07001076 return (emitError("expected '->' or '['"), nullptr);
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001077 }
1078 if (!consumeIf(Token::l_paren)) {
1079 emitError("expected '(' at start of affine map range");
Chris Lattner7121b802018-07-04 20:45:39 -07001080 return nullptr;
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001081 }
1082
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001083 SmallVector<AffineExpr *, 4> exprs;
1084 auto parseElt = [&]() -> ParseResult {
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001085 auto *elt = parseAffineExpr(state);
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001086 ParseResult res = elt ? ParseSuccess : ParseFailure;
1087 exprs.push_back(elt);
1088 return res;
1089 };
1090
1091 // Parse a multi-dimensional affine expression (a comma-separated list of 1-d
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001092 // affine expressions); the list cannot be empty.
1093 // Grammar: multi-dim-affine-expr ::= `(` affine-expr (`,` affine-expr)* `)
1094 if (parseCommaSeparatedList(Token::r_paren, parseElt, false))
Chris Lattner7121b802018-07-04 20:45:39 -07001095 return nullptr;
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001096
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001097 // Parsed a valid affine map.
Chris Lattner7121b802018-07-04 20:45:39 -07001098 return AffineMap::get(state.getNumDims(), state.getNumSymbols(), exprs,
Chris Lattner158e0a3e2018-07-08 20:51:38 -07001099 builder.getContext());
MLIR Teamf85a6262018-06-27 11:03:08 -07001100}
1101
1102//===----------------------------------------------------------------------===//
Chris Lattner78276e32018-07-07 15:48:26 -07001103// SSA
Chris Lattner4c95a502018-06-23 16:03:42 -07001104//===----------------------------------------------------------------------===//
Chris Lattnere79379a2018-06-22 10:39:19 -07001105
Chris Lattner78276e32018-07-07 15:48:26 -07001106/// Parse a SSA operand for an instruction or statement.
1107///
1108/// ssa-use ::= ssa-id | ssa-constant
1109///
1110ParseResult Parser::parseSSAUse() {
1111 if (curToken.is(Token::percent_identifier)) {
1112 StringRef name = curToken.getSpelling().drop_front();
1113 consumeToken(Token::percent_identifier);
1114 // TODO: Return this use.
1115 (void)name;
1116 return ParseSuccess;
1117 }
1118
1119 // TODO: Parse SSA constants.
1120
1121 return emitError("expected SSA operand");
1122}
1123
1124/// Parse a (possibly empty) list of SSA operands.
1125///
1126/// ssa-use-list ::= ssa-use (`,` ssa-use)*
1127/// ssa-use-list-opt ::= ssa-use-list?
1128///
1129ParseResult Parser::parseOptionalSSAUseList(Token::Kind endToken) {
1130 // TODO: Build and return this.
1131 return parseCommaSeparatedList(
1132 endToken, [&]() -> ParseResult { return parseSSAUse(); });
1133}
1134
1135/// Parse an SSA use with an associated type.
1136///
1137/// ssa-use-and-type ::= ssa-use `:` type
1138ParseResult Parser::parseSSAUseAndType() {
1139 if (parseSSAUse())
1140 return ParseFailure;
1141
1142 if (!consumeIf(Token::colon))
1143 return emitError("expected ':' and type for SSA operand");
1144
1145 if (!parseType())
1146 return ParseFailure;
1147
1148 return ParseSuccess;
1149}
1150
1151/// Parse a (possibly empty) list of SSA operands with types.
1152///
1153/// ssa-use-and-type-list ::= ssa-use-and-type (`,` ssa-use-and-type)*
1154///
1155ParseResult Parser::parseOptionalSSAUseAndTypeList(Token::Kind endToken) {
1156 // TODO: Build and return this.
1157 return parseCommaSeparatedList(
1158 endToken, [&]() -> ParseResult { return parseSSAUseAndType(); });
1159}
1160
1161//===----------------------------------------------------------------------===//
1162// Functions
1163//===----------------------------------------------------------------------===//
Chris Lattnere79379a2018-06-22 10:39:19 -07001164
1165/// Parse a function signature, starting with a name and including the parameter
1166/// list.
1167///
1168/// argument-list ::= type (`,` type)* | /*empty*/
1169/// function-signature ::= function-id `(` argument-list `)` (`->` type-list)?
1170///
Chris Lattnerf7e22732018-06-22 22:03:48 -07001171ParseResult Parser::parseFunctionSignature(StringRef &name,
1172 FunctionType *&type) {
Chris Lattnere79379a2018-06-22 10:39:19 -07001173 if (curToken.isNot(Token::at_identifier))
1174 return emitError("expected a function identifier like '@foo'");
1175
1176 name = curToken.getSpelling().drop_front();
1177 consumeToken(Token::at_identifier);
1178
1179 if (curToken.isNot(Token::l_paren))
1180 return emitError("expected '(' in function signature");
Chris Lattnere79379a2018-06-22 10:39:19 -07001181
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001182 SmallVector<Type*, 4> arguments;
1183 if (parseTypeList(arguments))
Chris Lattnerbb8fafc2018-06-22 15:52:02 -07001184 return ParseFailure;
Chris Lattnere79379a2018-06-22 10:39:19 -07001185
Chris Lattnerbb8fafc2018-06-22 15:52:02 -07001186 // Parse the return type if present.
Chris Lattnerf7e22732018-06-22 22:03:48 -07001187 SmallVector<Type*, 4> results;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -07001188 if (consumeIf(Token::arrow)) {
Chris Lattnerf7e22732018-06-22 22:03:48 -07001189 if (parseTypeList(results))
Chris Lattnerbb8fafc2018-06-22 15:52:02 -07001190 return ParseFailure;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -07001191 }
Chris Lattner158e0a3e2018-07-08 20:51:38 -07001192 type = builder.getFunctionType(arguments, results);
Chris Lattnere79379a2018-06-22 10:39:19 -07001193 return ParseSuccess;
1194}
1195
Chris Lattnere79379a2018-06-22 10:39:19 -07001196/// External function declarations.
1197///
1198/// ext-func ::= `extfunc` function-signature
1199///
1200ParseResult Parser::parseExtFunc() {
1201 consumeToken(Token::kw_extfunc);
1202
1203 StringRef name;
Chris Lattnerf7e22732018-06-22 22:03:48 -07001204 FunctionType *type = nullptr;
1205 if (parseFunctionSignature(name, type))
Chris Lattnere79379a2018-06-22 10:39:19 -07001206 return ParseFailure;
1207
Chris Lattnere79379a2018-06-22 10:39:19 -07001208 // Okay, the external function definition was parsed correctly.
Chris Lattner4c95a502018-06-23 16:03:42 -07001209 module->functionList.push_back(new ExtFunction(name, type));
Chris Lattnere79379a2018-06-22 10:39:19 -07001210 return ParseSuccess;
1211}
1212
1213
Chris Lattner4c95a502018-06-23 16:03:42 -07001214namespace {
1215/// This class represents the transient parser state for the internals of a
1216/// function as we are parsing it, e.g. the names for basic blocks. It handles
1217/// forward references.
1218class CFGFunctionParserState {
Chris Lattner158e0a3e2018-07-08 20:51:38 -07001219public:
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001220 CFGFunction *function;
1221 llvm::StringMap<std::pair<BasicBlock*, SMLoc>> blocksByName;
Chris Lattner158e0a3e2018-07-08 20:51:38 -07001222 CFGFuncBuilder builder;
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001223
Chris Lattner158e0a3e2018-07-08 20:51:38 -07001224 CFGFunctionParserState(CFGFunction *function)
1225 : function(function), builder(function) {}
Chris Lattner4c95a502018-06-23 16:03:42 -07001226
1227 /// Get the basic block with the specified name, creating it if it doesn't
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001228 /// already exist. The location specified is the point of use, which allows
1229 /// us to diagnose references to blocks that are not defined precisely.
1230 BasicBlock *getBlockNamed(StringRef name, SMLoc loc) {
1231 auto &blockAndLoc = blocksByName[name];
1232 if (!blockAndLoc.first) {
Chris Lattner3a467cc2018-07-01 20:28:00 -07001233 blockAndLoc.first = new BasicBlock();
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001234 blockAndLoc.second = loc;
Chris Lattner4c95a502018-06-23 16:03:42 -07001235 }
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001236 return blockAndLoc.first;
Chris Lattner4c95a502018-06-23 16:03:42 -07001237 }
Chris Lattner4c95a502018-06-23 16:03:42 -07001238};
1239} // end anonymous namespace
1240
1241
1242/// CFG function declarations.
1243///
1244/// cfg-func ::= `cfgfunc` function-signature `{` basic-block+ `}`
1245///
1246ParseResult Parser::parseCFGFunc() {
1247 consumeToken(Token::kw_cfgfunc);
1248
1249 StringRef name;
1250 FunctionType *type = nullptr;
1251 if (parseFunctionSignature(name, type))
1252 return ParseFailure;
1253
1254 if (!consumeIf(Token::l_brace))
1255 return emitError("expected '{' in CFG function");
1256
1257 // Okay, the CFG function signature was parsed correctly, create the function.
1258 auto function = new CFGFunction(name, type);
1259
1260 // Make sure we have at least one block.
1261 if (curToken.is(Token::r_brace))
1262 return emitError("CFG functions must have at least one basic block");
1263
1264 CFGFunctionParserState functionState(function);
1265
1266 // Parse the list of blocks.
1267 while (!consumeIf(Token::r_brace))
1268 if (parseBasicBlock(functionState))
1269 return ParseFailure;
1270
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001271 // Verify that all referenced blocks were defined. Iteration over a
1272 // StringMap isn't determinstic, but this is good enough for our purposes.
1273 for (auto &elt : functionState.blocksByName) {
1274 auto *bb = elt.second.first;
Chris Lattner3a467cc2018-07-01 20:28:00 -07001275 if (!bb->getFunction())
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001276 return emitError(elt.second.second,
1277 "reference to an undefined basic block '" +
1278 elt.first() + "'");
1279 }
1280
Chris Lattner4c95a502018-06-23 16:03:42 -07001281 module->functionList.push_back(function);
1282 return ParseSuccess;
1283}
1284
1285/// Basic block declaration.
1286///
1287/// basic-block ::= bb-label instruction* terminator-stmt
1288/// bb-label ::= bb-id bb-arg-list? `:`
1289/// bb-id ::= bare-id
1290/// bb-arg-list ::= `(` ssa-id-and-type-list? `)`
1291///
1292ParseResult Parser::parseBasicBlock(CFGFunctionParserState &functionState) {
1293 SMLoc nameLoc = curToken.getLoc();
1294 auto name = curToken.getSpelling();
1295 if (!consumeIf(Token::bare_identifier))
1296 return emitError("expected basic block name");
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001297
1298 auto block = functionState.getBlockNamed(name, nameLoc);
Chris Lattner4c95a502018-06-23 16:03:42 -07001299
1300 // If this block has already been parsed, then this is a redefinition with the
1301 // same block name.
Chris Lattner3a467cc2018-07-01 20:28:00 -07001302 if (block->getFunction())
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001303 return emitError(nameLoc, "redefinition of block '" + name.str() + "'");
1304
Chris Lattner3a467cc2018-07-01 20:28:00 -07001305 // Add the block to the function.
1306 functionState.function->push_back(block);
Chris Lattner4c95a502018-06-23 16:03:42 -07001307
Chris Lattner78276e32018-07-07 15:48:26 -07001308 // If an argument list is present, parse it.
1309 if (consumeIf(Token::l_paren)) {
1310 if (parseOptionalSSAUseAndTypeList(Token::r_paren))
1311 return ParseFailure;
1312
1313 // TODO: attach it.
1314 }
Chris Lattner4c95a502018-06-23 16:03:42 -07001315
1316 if (!consumeIf(Token::colon))
1317 return emitError("expected ':' after basic block name");
1318
Chris Lattner158e0a3e2018-07-08 20:51:38 -07001319 // Set the insertion point to the block we want to insert new operations into.
1320 functionState.builder.setInsertionPoint(block);
1321
Chris Lattnered65a732018-06-28 20:45:33 -07001322 // Parse the list of operations that make up the body of the block.
1323 while (curToken.isNot(Token::kw_return, Token::kw_br)) {
Chris Lattner21e67f62018-07-06 10:46:19 -07001324 auto loc = curToken.getLoc();
Chris Lattner3a467cc2018-07-01 20:28:00 -07001325 auto *inst = parseCFGOperation(functionState);
1326 if (!inst)
Chris Lattnered65a732018-06-28 20:45:33 -07001327 return ParseFailure;
Chris Lattner3a467cc2018-07-01 20:28:00 -07001328
Chris Lattner21e67f62018-07-06 10:46:19 -07001329 // We just parsed an operation. If it is a recognized one, verify that it
1330 // is structurally as we expect. If not, produce an error with a reasonable
1331 // source location.
Chris Lattner158e0a3e2018-07-08 20:51:38 -07001332 if (auto *opInfo = inst->getAbstractOperation(builder.getContext()))
Chris Lattner21e67f62018-07-06 10:46:19 -07001333 if (auto error = opInfo->verifyInvariants(inst))
1334 return emitError(loc, error);
Chris Lattnered65a732018-06-28 20:45:33 -07001335 }
Chris Lattner4c95a502018-06-23 16:03:42 -07001336
Chris Lattner3a467cc2018-07-01 20:28:00 -07001337 auto *term = parseTerminator(functionState);
1338 if (!term)
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001339 return ParseFailure;
Chris Lattner4c95a502018-06-23 16:03:42 -07001340
1341 return ParseSuccess;
1342}
1343
1344
Chris Lattnered65a732018-06-28 20:45:33 -07001345/// Parse the CFG operation.
1346///
1347/// TODO(clattner): This is a change from the MLIR spec as written, it is an
1348/// experiment that will eliminate "builtin" instructions as a thing.
1349///
1350/// cfg-operation ::=
1351/// (ssa-id `=`)? string '(' ssa-use-list? ')' attribute-dict?
1352/// `:` function-type
1353///
Chris Lattner3a467cc2018-07-01 20:28:00 -07001354OperationInst *Parser::
1355parseCFGOperation(CFGFunctionParserState &functionState) {
Chris Lattnered65a732018-06-28 20:45:33 -07001356
Chris Lattner78276e32018-07-07 15:48:26 -07001357 StringRef resultID;
1358 if (curToken.is(Token::percent_identifier)) {
1359 resultID = curToken.getSpelling().drop_front();
1360 consumeToken();
1361 if (!consumeIf(Token::equal))
1362 return (emitError("expected '=' after SSA name"), nullptr);
1363 }
Chris Lattnered65a732018-06-28 20:45:33 -07001364
1365 if (curToken.isNot(Token::string))
Chris Lattner3a467cc2018-07-01 20:28:00 -07001366 return (emitError("expected operation name in quotes"), nullptr);
Chris Lattnered65a732018-06-28 20:45:33 -07001367
1368 auto name = curToken.getStringValue();
1369 if (name.empty())
Chris Lattner3a467cc2018-07-01 20:28:00 -07001370 return (emitError("empty operation name is invalid"), nullptr);
Chris Lattnered65a732018-06-28 20:45:33 -07001371
1372 consumeToken(Token::string);
1373
1374 if (!consumeIf(Token::l_paren))
Chris Lattner7121b802018-07-04 20:45:39 -07001375 return (emitError("expected '(' to start operand list"), nullptr);
Chris Lattnered65a732018-06-28 20:45:33 -07001376
Chris Lattner78276e32018-07-07 15:48:26 -07001377 // Parse the operand list.
1378 parseOptionalSSAUseList(Token::r_paren);
Chris Lattner7121b802018-07-04 20:45:39 -07001379
1380 SmallVector<NamedAttribute, 4> attributes;
1381 if (curToken.is(Token::l_brace)) {
1382 if (parseAttributeDict(attributes))
1383 return nullptr;
1384 }
Chris Lattnered65a732018-06-28 20:45:33 -07001385
Chris Lattner78276e32018-07-07 15:48:26 -07001386 // TODO: Don't drop result name and operand names on the floor.
Chris Lattner158e0a3e2018-07-08 20:51:38 -07001387 auto nameId = Identifier::get(name, builder.getContext());
1388 return functionState.builder.createOperation(nameId, attributes);
Chris Lattnered65a732018-06-28 20:45:33 -07001389}
1390
1391
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001392/// Parse the terminator instruction for a basic block.
1393///
1394/// terminator-stmt ::= `br` bb-id branch-use-list?
1395/// branch-use-list ::= `(` ssa-use-and-type-list? `)`
1396/// terminator-stmt ::=
1397/// `cond_br` ssa-use `,` bb-id branch-use-list? `,` bb-id branch-use-list?
1398/// terminator-stmt ::= `return` ssa-use-and-type-list?
1399///
Chris Lattner3a467cc2018-07-01 20:28:00 -07001400TerminatorInst *Parser::parseTerminator(CFGFunctionParserState &functionState) {
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001401 switch (curToken.getKind()) {
1402 default:
Chris Lattner3a467cc2018-07-01 20:28:00 -07001403 return (emitError("expected terminator at end of basic block"), nullptr);
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001404
1405 case Token::kw_return:
1406 consumeToken(Token::kw_return);
Chris Lattner158e0a3e2018-07-08 20:51:38 -07001407 return functionState.builder.createReturnInst();
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001408
1409 case Token::kw_br: {
1410 consumeToken(Token::kw_br);
1411 auto destBB = functionState.getBlockNamed(curToken.getSpelling(),
1412 curToken.getLoc());
1413 if (!consumeIf(Token::bare_identifier))
Chris Lattner3a467cc2018-07-01 20:28:00 -07001414 return (emitError("expected basic block name"), nullptr);
Chris Lattner158e0a3e2018-07-08 20:51:38 -07001415 return functionState.builder.createBranchInst(destBB);
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001416 }
Chris Lattner78276e32018-07-07 15:48:26 -07001417 // TODO: cond_br.
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001418 }
1419}
1420
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -07001421/// ML function declarations.
1422///
1423/// ml-func ::= `mlfunc` ml-func-signature `{` ml-stmt* ml-return-stmt `}`
1424///
1425ParseResult Parser::parseMLFunc() {
1426 consumeToken(Token::kw_mlfunc);
1427
1428 StringRef name;
1429 FunctionType *type = nullptr;
1430
1431 // FIXME: Parse ML function signature (args + types)
1432 // by passing pointer to SmallVector<identifier> into parseFunctionSignature
1433 if (parseFunctionSignature(name, type))
1434 return ParseFailure;
1435
1436 if (!consumeIf(Token::l_brace))
1437 return emitError("expected '{' in ML function");
1438
1439 // Okay, the ML function signature was parsed correctly, create the function.
1440 auto function = new MLFunction(name, type);
1441
1442 // Make sure we have at least one statement.
1443 if (curToken.is(Token::r_brace))
1444 return emitError("ML function must end with return statement");
1445
1446 // Parse the list of instructions.
1447 while (!consumeIf(Token::kw_return)) {
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001448 auto *stmt = parseStatement(function);
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -07001449 if (!stmt)
1450 return ParseFailure;
1451 function->stmtList.push_back(stmt);
1452 }
1453
1454 // TODO: parse return statement operands
1455 if (!consumeIf(Token::r_brace))
1456 emitError("expected '}' in ML function");
1457
1458 module->functionList.push_back(function);
1459
1460 return ParseSuccess;
1461}
1462
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001463/// Statement.
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -07001464///
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001465/// ml-stmt ::= instruction | ml-for-stmt | ml-if-stmt
1466/// TODO: fix terminology in MLSpec document. ML functions
1467/// contain operation statements, not instructions.
1468///
1469Statement * Parser::parseStatement(ParentType parent) {
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -07001470 switch (curToken.getKind()) {
1471 default:
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001472 //TODO: parse OperationStmt
1473 return (emitError("expected statement"), nullptr);
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -07001474
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001475 case Token::kw_for:
1476 return parseForStmt(parent);
1477
1478 case Token::kw_if:
1479 return parseIfStmt(parent);
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -07001480 }
1481}
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001482
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001483/// For statement.
1484///
1485/// ml-for-stmt ::= `for` ssa-id `=` lower-bound `to` upper-bound
1486/// (`step` integer-literal)? `{` ml-stmt* `}`
1487///
1488ForStmt * Parser::parseForStmt(ParentType parent) {
1489 consumeToken(Token::kw_for);
1490
1491 //TODO: parse loop header
1492 ForStmt *stmt = new ForStmt(parent);
1493 if (parseNestedStatements(stmt)) {
1494 delete stmt;
1495 return nullptr;
1496 }
1497 return stmt;
1498}
1499
1500/// If statement.
1501///
1502/// ml-if-head ::= `if` ml-if-cond `{` ml-stmt* `}`
1503/// | ml-if-head `else` `if` ml-if-cond `{` ml-stmt* `}`
1504/// ml-if-stmt ::= ml-if-head
1505/// | ml-if-head `else` `{` ml-stmt* `}`
1506///
1507IfStmt * Parser::parseIfStmt(PointerUnion<MLFunction *, NodeStmt *> parent) {
1508 consumeToken(Token::kw_if);
1509
1510 //TODO: parse condition
1511 IfStmt *stmt = new IfStmt(parent);
1512 if (parseNestedStatements(stmt)) {
1513 delete stmt;
1514 return nullptr;
1515 }
1516
1517 int clauseNum = 0;
1518 while (consumeIf(Token::kw_else)) {
1519 if (consumeIf(Token::kw_if)) {
1520 //TODO: parse condition
1521 }
1522 ElseClause * clause = new ElseClause(stmt, clauseNum);
1523 ++clauseNum;
1524 if (parseNestedStatements(clause)) {
1525 delete clause;
1526 return nullptr;
1527 }
1528 }
1529
1530 return stmt;
1531}
1532
1533///
1534/// Parse `{` ml-stmt* `}`
1535///
1536ParseResult Parser::parseNestedStatements(NodeStmt *parent) {
1537 if (!consumeIf(Token::l_brace))
1538 return emitError("expected '{' before statement list");
1539
1540 if (consumeIf(Token::r_brace)) {
1541 // TODO: parse OperationStmt
1542 return ParseSuccess;
1543 }
1544
1545 while (!consumeIf(Token::r_brace)) {
1546 auto *stmt = parseStatement(parent);
1547 if (!stmt)
1548 return ParseFailure;
1549 parent->children.push_back(stmt);
1550 }
1551
1552 return ParseSuccess;
1553}
1554
Chris Lattner4c95a502018-06-23 16:03:42 -07001555//===----------------------------------------------------------------------===//
1556// Top-level entity parsing.
1557//===----------------------------------------------------------------------===//
1558
Chris Lattnere79379a2018-06-22 10:39:19 -07001559/// This is the top-level module parser.
1560Module *Parser::parseModule() {
1561 while (1) {
1562 switch (curToken.getKind()) {
1563 default:
1564 emitError("expected a top level entity");
1565 return nullptr;
1566
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001567 // If we got to the end of the file, then we're done.
Chris Lattnere79379a2018-06-22 10:39:19 -07001568 case Token::eof:
1569 return module.release();
1570
1571 // If we got an error token, then the lexer already emitted an error, just
1572 // stop. Someday we could introduce error recovery if there was demand for
1573 // it.
1574 case Token::error:
1575 return nullptr;
1576
1577 case Token::kw_extfunc:
Chris Lattner4c95a502018-06-23 16:03:42 -07001578 if (parseExtFunc()) return nullptr;
Chris Lattnere79379a2018-06-22 10:39:19 -07001579 break;
1580
Chris Lattner4c95a502018-06-23 16:03:42 -07001581 case Token::kw_cfgfunc:
1582 if (parseCFGFunc()) return nullptr;
1583 break;
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001584
Chris Lattner78276e32018-07-07 15:48:26 -07001585 case Token::hash_identifier:
MLIR Teamf85a6262018-06-27 11:03:08 -07001586 if (parseAffineMapDef()) return nullptr;
1587 break;
Chris Lattner4c95a502018-06-23 16:03:42 -07001588
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -07001589 case Token::kw_mlfunc:
1590 if (parseMLFunc()) return nullptr;
1591 break;
1592
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001593 // TODO: affine entity declarations, etc.
Chris Lattnere79379a2018-06-22 10:39:19 -07001594 }
1595 }
1596}
1597
1598//===----------------------------------------------------------------------===//
1599
Jacques Pienaar7b829702018-07-03 13:24:09 -07001600void mlir::defaultErrorReporter(const llvm::SMDiagnostic &error) {
1601 const auto &sourceMgr = *error.getSourceMgr();
1602 sourceMgr.PrintMessage(error.getLoc(), error.getKind(), error.getMessage());
1603}
1604
Chris Lattnere79379a2018-06-22 10:39:19 -07001605/// This parses the file specified by the indicated SourceMgr and returns an
1606/// MLIR module if it was valid. If not, it emits diagnostics and returns null.
Jacques Pienaar9c411be2018-06-24 19:17:35 -07001607Module *mlir::parseSourceFile(llvm::SourceMgr &sourceMgr, MLIRContext *context,
Jacques Pienaar7b829702018-07-03 13:24:09 -07001608 SMDiagnosticHandlerTy errorReporter) {
Chris Lattner21e67f62018-07-06 10:46:19 -07001609 auto *result =
1610 Parser(sourceMgr, context,
1611 errorReporter ? std::move(errorReporter) : defaultErrorReporter)
1612 .parseModule();
1613
1614 // Make sure the parse module has no other structural problems detected by the
1615 // verifier.
1616 if (result)
1617 result->verify();
1618 return result;
Chris Lattnere79379a2018-06-22 10:39:19 -07001619}