blob: b8bae40627f1c9464834a03db2a373a87930799a [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"
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -070031#include "mlir/IR/Statements.h"
Chris Lattnerf7e22732018-06-22 22:03:48 -070032#include "mlir/IR/Types.h"
Chris Lattnere79379a2018-06-22 10:39:19 -070033#include "llvm/Support/SourceMgr.h"
34using namespace mlir;
35using llvm::SourceMgr;
Chris Lattner4c95a502018-06-23 16:03:42 -070036using llvm::SMLoc;
Chris Lattnere79379a2018-06-22 10:39:19 -070037
Chris Lattnerf7e22732018-06-22 22:03:48 -070038/// Simple enum to make code read better in cases that would otherwise return a
39/// bool value. Failure is "true" in a boolean context.
Chris Lattnere79379a2018-06-22 10:39:19 -070040enum ParseResult {
41 ParseSuccess,
42 ParseFailure
43};
44
Chris Lattner48af7d12018-07-09 19:05:38 -070045namespace {
46class Parser;
47
48/// This class refers to all of the state maintained globally by the parser,
49/// such as the current lexer position etc. The Parser base class provides
50/// methods to access this.
51class ParserState {
Chris Lattnered65a732018-06-28 20:45:33 -070052public:
Chris Lattner2e595eb2018-07-10 10:08:27 -070053 ParserState(llvm::SourceMgr &sourceMgr, Module *module,
Chris Lattner48af7d12018-07-09 19:05:38 -070054 SMDiagnosticHandlerTy errorReporter)
Chris Lattner2e595eb2018-07-10 10:08:27 -070055 : context(module->getContext()), module(module),
56 lex(sourceMgr, errorReporter), curToken(lex.lexToken()),
Jacques Pienaard4c784e2018-07-11 00:07:36 -070057 errorReporter(errorReporter) {}
Chris Lattner2e595eb2018-07-10 10:08:27 -070058
59 // A map from affine map identifier to AffineMap.
60 llvm::StringMap<AffineMap *> affineMapDefinitions;
Chris Lattnere79379a2018-06-22 10:39:19 -070061
Chris Lattnere79379a2018-06-22 10:39:19 -070062private:
Chris Lattner48af7d12018-07-09 19:05:38 -070063 ParserState(const ParserState &) = delete;
64 void operator=(const ParserState &) = delete;
65
66 friend class Parser;
67
68 // The context we're parsing into.
Chris Lattner2e595eb2018-07-10 10:08:27 -070069 MLIRContext *const context;
70
71 // This is the module we are parsing into.
72 Module *const module;
Chris Lattnerf7e22732018-06-22 22:03:48 -070073
74 // The lexer for the source file we're parsing.
Chris Lattnere79379a2018-06-22 10:39:19 -070075 Lexer lex;
76
77 // This is the next token that hasn't been consumed yet.
78 Token curToken;
79
Jacques Pienaar9c411be2018-06-24 19:17:35 -070080 // The diagnostic error reporter.
Chris Lattner2e595eb2018-07-10 10:08:27 -070081 SMDiagnosticHandlerTy const errorReporter;
Chris Lattner48af7d12018-07-09 19:05:38 -070082};
83} // end anonymous namespace
MLIR Teamf85a6262018-06-27 11:03:08 -070084
Chris Lattner48af7d12018-07-09 19:05:38 -070085namespace {
86
Chris Lattner7f9cc272018-07-19 08:35:28 -070087typedef std::function<Operation *(Identifier, ArrayRef<SSAValue *>,
88 ArrayRef<Type *>, ArrayRef<NamedAttribute>)>
Tatiana Shpeisman565b9642018-07-16 11:47:09 -070089 CreateOperationFunction;
90
Chris Lattner48af7d12018-07-09 19:05:38 -070091/// This class implement support for parsing global entities like types and
92/// shared entities like SSA names. It is intended to be subclassed by
93/// specialized subparsers that include state, e.g. when a local symbol table.
94class Parser {
95public:
Chris Lattner2e595eb2018-07-10 10:08:27 -070096 Builder builder;
Chris Lattner48af7d12018-07-09 19:05:38 -070097
Chris Lattner2e595eb2018-07-10 10:08:27 -070098 Parser(ParserState &state) : builder(state.context), state(state) {}
99
100 // Helper methods to get stuff from the parser-global state.
101 ParserState &getState() const { return state; }
Chris Lattner48af7d12018-07-09 19:05:38 -0700102 MLIRContext *getContext() const { return state.context; }
Chris Lattner2e595eb2018-07-10 10:08:27 -0700103 Module *getModule() { return state.module; }
Chris Lattner48af7d12018-07-09 19:05:38 -0700104
105 /// Return the current token the parser is inspecting.
106 const Token &getToken() const { return state.curToken; }
107 StringRef getTokenSpelling() const { return state.curToken.getSpelling(); }
Chris Lattnere79379a2018-06-22 10:39:19 -0700108
109 /// Emit an error and return failure.
Chris Lattner4c95a502018-06-23 16:03:42 -0700110 ParseResult emitError(const Twine &message) {
Chris Lattner48af7d12018-07-09 19:05:38 -0700111 return emitError(state.curToken.getLoc(), message);
Chris Lattner4c95a502018-06-23 16:03:42 -0700112 }
113 ParseResult emitError(SMLoc loc, const Twine &message);
Chris Lattnere79379a2018-06-22 10:39:19 -0700114
115 /// Advance the current lexer onto the next token.
116 void consumeToken() {
Chris Lattner48af7d12018-07-09 19:05:38 -0700117 assert(state.curToken.isNot(Token::eof, Token::error) &&
Chris Lattnere79379a2018-06-22 10:39:19 -0700118 "shouldn't advance past EOF or errors");
Chris Lattner48af7d12018-07-09 19:05:38 -0700119 state.curToken = state.lex.lexToken();
Chris Lattnere79379a2018-06-22 10:39:19 -0700120 }
121
122 /// Advance the current lexer onto the next token, asserting what the expected
123 /// current token is. This is preferred to the above method because it leads
124 /// to more self-documenting code with better checking.
Chris Lattner8da0c282018-06-29 11:15:56 -0700125 void consumeToken(Token::Kind kind) {
Chris Lattner48af7d12018-07-09 19:05:38 -0700126 assert(state.curToken.is(kind) && "consumed an unexpected token");
Chris Lattnere79379a2018-06-22 10:39:19 -0700127 consumeToken();
128 }
129
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700130 /// If the current token has the specified kind, consume it and return true.
131 /// If not, return false.
Chris Lattner8da0c282018-06-29 11:15:56 -0700132 bool consumeIf(Token::Kind kind) {
Chris Lattner48af7d12018-07-09 19:05:38 -0700133 if (state.curToken.isNot(kind))
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700134 return false;
135 consumeToken(kind);
136 return true;
137 }
138
MLIR Team718c82f2018-07-16 09:45:22 -0700139 ParseResult parseCommaSeparatedList(
140 Token::Kind rightToken,
141 const std::function<ParseResult()> &parseElement,
142 bool allowEmptyList = true);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700143
Chris Lattnerf7e22732018-06-22 22:03:48 -0700144 // We have two forms of parsing methods - those that return a non-null
145 // pointer on success, and those that return a ParseResult to indicate whether
146 // they returned a failure. The second class fills in by-reference arguments
147 // as the results of their action.
148
Chris Lattnere79379a2018-06-22 10:39:19 -0700149 // Type parsing.
Chris Lattnerf958bbe2018-06-29 22:08:05 -0700150 Type *parsePrimitiveType();
Chris Lattnerf7e22732018-06-22 22:03:48 -0700151 Type *parseElementType();
152 VectorType *parseVectorType();
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700153 ParseResult parseDimensionListRanked(SmallVectorImpl<int> &dimensions);
Chris Lattnerf7e22732018-06-22 22:03:48 -0700154 Type *parseTensorType();
155 Type *parseMemRefType();
156 Type *parseFunctionType();
157 Type *parseType();
158 ParseResult parseTypeList(SmallVectorImpl<Type*> &elements);
Chris Lattnere79379a2018-06-22 10:39:19 -0700159
Chris Lattner7121b802018-07-04 20:45:39 -0700160 // Attribute parsing.
161 Attribute *parseAttribute();
162 ParseResult parseAttributeDict(SmallVectorImpl<NamedAttribute> &attributes);
163
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700164 // Polyhedral structures.
Chris Lattner2e595eb2018-07-10 10:08:27 -0700165 AffineMap *parseAffineMapInline();
MLIR Team718c82f2018-07-16 09:45:22 -0700166 AffineMap *parseAffineMapReference();
MLIR Teamf85a6262018-06-27 11:03:08 -0700167
Chris Lattner48af7d12018-07-09 19:05:38 -0700168private:
169 // The Parser is subclassed and reinstantiated. Do not add additional
170 // non-trivial state here, add it to the ParserState class.
171 ParserState &state;
Chris Lattnere79379a2018-06-22 10:39:19 -0700172};
173} // end anonymous namespace
174
175//===----------------------------------------------------------------------===//
176// Helper methods.
177//===----------------------------------------------------------------------===//
178
Chris Lattner4c95a502018-06-23 16:03:42 -0700179ParseResult Parser::emitError(SMLoc loc, const Twine &message) {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700180 // If we hit a parse error in response to a lexer error, then the lexer
Jacques Pienaar9c411be2018-06-24 19:17:35 -0700181 // already reported the error.
Chris Lattner48af7d12018-07-09 19:05:38 -0700182 if (getToken().is(Token::error))
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700183 return ParseFailure;
184
Chris Lattner48af7d12018-07-09 19:05:38 -0700185 auto &sourceMgr = state.lex.getSourceMgr();
186 state.errorReporter(sourceMgr.GetMessage(loc, SourceMgr::DK_Error, message));
Chris Lattnere79379a2018-06-22 10:39:19 -0700187 return ParseFailure;
188}
189
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700190/// Parse a comma-separated list of elements, terminated with an arbitrary
191/// token. This allows empty lists if allowEmptyList is true.
192///
193/// abstract-list ::= rightToken // if allowEmptyList == true
194/// abstract-list ::= element (',' element)* rightToken
195///
196ParseResult Parser::
Chris Lattner8da0c282018-06-29 11:15:56 -0700197parseCommaSeparatedList(Token::Kind rightToken,
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700198 const std::function<ParseResult()> &parseElement,
199 bool allowEmptyList) {
200 // Handle the empty case.
Chris Lattner48af7d12018-07-09 19:05:38 -0700201 if (getToken().is(rightToken)) {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700202 if (!allowEmptyList)
203 return emitError("expected list element");
204 consumeToken(rightToken);
205 return ParseSuccess;
206 }
207
208 // Non-empty case starts with an element.
209 if (parseElement())
210 return ParseFailure;
211
212 // Otherwise we have a list of comma separated elements.
213 while (consumeIf(Token::comma)) {
214 if (parseElement())
215 return ParseFailure;
216 }
217
218 // Consume the end character.
219 if (!consumeIf(rightToken))
Chris Lattner8da0c282018-06-29 11:15:56 -0700220 return emitError("expected ',' or '" + Token::getTokenSpelling(rightToken) +
221 "'");
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700222
223 return ParseSuccess;
224}
Chris Lattnere79379a2018-06-22 10:39:19 -0700225
226//===----------------------------------------------------------------------===//
227// Type Parsing
228//===----------------------------------------------------------------------===//
229
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700230/// Parse the low-level fixed dtypes in the system.
231///
Chris Lattnerf958bbe2018-06-29 22:08:05 -0700232/// primitive-type ::= `f16` | `bf16` | `f32` | `f64`
233/// primitive-type ::= integer-type
234/// primitive-type ::= `affineint`
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700235///
Chris Lattnerf958bbe2018-06-29 22:08:05 -0700236Type *Parser::parsePrimitiveType() {
Chris Lattner48af7d12018-07-09 19:05:38 -0700237 switch (getToken().getKind()) {
Chris Lattnerf7e22732018-06-22 22:03:48 -0700238 default:
239 return (emitError("expected type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700240 case Token::kw_bf16:
241 consumeToken(Token::kw_bf16);
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700242 return builder.getBF16Type();
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700243 case Token::kw_f16:
244 consumeToken(Token::kw_f16);
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700245 return builder.getF16Type();
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700246 case Token::kw_f32:
247 consumeToken(Token::kw_f32);
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700248 return builder.getF32Type();
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700249 case Token::kw_f64:
250 consumeToken(Token::kw_f64);
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700251 return builder.getF64Type();
Chris Lattnerf958bbe2018-06-29 22:08:05 -0700252 case Token::kw_affineint:
253 consumeToken(Token::kw_affineint);
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700254 return builder.getAffineIntType();
Chris Lattnerf958bbe2018-06-29 22:08:05 -0700255 case Token::inttype: {
Chris Lattner48af7d12018-07-09 19:05:38 -0700256 auto width = getToken().getIntTypeBitwidth();
Chris Lattnerf958bbe2018-06-29 22:08:05 -0700257 if (!width.hasValue())
258 return (emitError("invalid integer width"), nullptr);
259 consumeToken(Token::inttype);
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700260 return builder.getIntegerType(width.getValue());
Chris Lattnerf958bbe2018-06-29 22:08:05 -0700261 }
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700262 }
263}
264
265/// Parse the element type of a tensor or memref type.
266///
267/// element-type ::= primitive-type | vector-type
268///
Chris Lattnerf7e22732018-06-22 22:03:48 -0700269Type *Parser::parseElementType() {
Chris Lattner48af7d12018-07-09 19:05:38 -0700270 if (getToken().is(Token::kw_vector))
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700271 return parseVectorType();
272
273 return parsePrimitiveType();
274}
275
276/// Parse a vector type.
277///
278/// vector-type ::= `vector` `<` const-dimension-list primitive-type `>`
279/// const-dimension-list ::= (integer-literal `x`)+
280///
Chris Lattnerf7e22732018-06-22 22:03:48 -0700281VectorType *Parser::parseVectorType() {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700282 consumeToken(Token::kw_vector);
283
284 if (!consumeIf(Token::less))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700285 return (emitError("expected '<' in vector type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700286
Chris Lattner48af7d12018-07-09 19:05:38 -0700287 if (getToken().isNot(Token::integer))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700288 return (emitError("expected dimension size in vector type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700289
290 SmallVector<unsigned, 4> dimensions;
Chris Lattner48af7d12018-07-09 19:05:38 -0700291 while (getToken().is(Token::integer)) {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700292 // Make sure this integer value is in bound and valid.
Chris Lattner48af7d12018-07-09 19:05:38 -0700293 auto dimension = getToken().getUnsignedIntegerValue();
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700294 if (!dimension.hasValue())
Chris Lattnerf7e22732018-06-22 22:03:48 -0700295 return (emitError("invalid dimension in vector type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700296 dimensions.push_back(dimension.getValue());
297
298 consumeToken(Token::integer);
299
300 // Make sure we have an 'x' or something like 'xbf32'.
Chris Lattner48af7d12018-07-09 19:05:38 -0700301 if (getToken().isNot(Token::bare_identifier) ||
302 getTokenSpelling()[0] != 'x')
Chris Lattnerf7e22732018-06-22 22:03:48 -0700303 return (emitError("expected 'x' in vector dimension list"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700304
305 // If we had a prefix of 'x', lex the next token immediately after the 'x'.
Chris Lattner48af7d12018-07-09 19:05:38 -0700306 if (getTokenSpelling().size() != 1)
307 state.lex.resetPointer(getTokenSpelling().data() + 1);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700308
309 // Consume the 'x'.
310 consumeToken(Token::bare_identifier);
311 }
312
313 // Parse the element type.
Chris Lattnerf7e22732018-06-22 22:03:48 -0700314 auto *elementType = parsePrimitiveType();
315 if (!elementType)
316 return nullptr;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700317
318 if (!consumeIf(Token::greater))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700319 return (emitError("expected '>' in vector type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700320
Chris Lattnerf7e22732018-06-22 22:03:48 -0700321 return VectorType::get(dimensions, elementType);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700322}
323
324/// Parse a dimension list of a tensor or memref type. This populates the
325/// dimension list, returning -1 for the '?' dimensions.
326///
327/// dimension-list-ranked ::= (dimension `x`)*
328/// dimension ::= `?` | integer-literal
329///
330ParseResult Parser::parseDimensionListRanked(SmallVectorImpl<int> &dimensions) {
Chris Lattner48af7d12018-07-09 19:05:38 -0700331 while (getToken().isAny(Token::integer, Token::question)) {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700332 if (consumeIf(Token::question)) {
333 dimensions.push_back(-1);
334 } else {
335 // Make sure this integer value is in bound and valid.
Chris Lattner48af7d12018-07-09 19:05:38 -0700336 auto dimension = getToken().getUnsignedIntegerValue();
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700337 if (!dimension.hasValue() || (int)dimension.getValue() < 0)
338 return emitError("invalid dimension");
339 dimensions.push_back((int)dimension.getValue());
340 consumeToken(Token::integer);
341 }
342
343 // Make sure we have an 'x' or something like 'xbf32'.
Chris Lattner48af7d12018-07-09 19:05:38 -0700344 if (getToken().isNot(Token::bare_identifier) ||
345 getTokenSpelling()[0] != 'x')
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700346 return emitError("expected 'x' in dimension list");
347
348 // If we had a prefix of 'x', lex the next token immediately after the 'x'.
Chris Lattner48af7d12018-07-09 19:05:38 -0700349 if (getTokenSpelling().size() != 1)
350 state.lex.resetPointer(getTokenSpelling().data() + 1);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700351
352 // Consume the 'x'.
353 consumeToken(Token::bare_identifier);
354 }
355
356 return ParseSuccess;
357}
358
359/// Parse a tensor type.
360///
361/// tensor-type ::= `tensor` `<` dimension-list element-type `>`
362/// dimension-list ::= dimension-list-ranked | `??`
363///
Chris Lattnerf7e22732018-06-22 22:03:48 -0700364Type *Parser::parseTensorType() {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700365 consumeToken(Token::kw_tensor);
366
367 if (!consumeIf(Token::less))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700368 return (emitError("expected '<' in tensor type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700369
370 bool isUnranked;
371 SmallVector<int, 4> dimensions;
372
373 if (consumeIf(Token::questionquestion)) {
374 isUnranked = true;
375 } else {
376 isUnranked = false;
377 if (parseDimensionListRanked(dimensions))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700378 return nullptr;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700379 }
380
381 // Parse the element type.
Chris Lattnerf7e22732018-06-22 22:03:48 -0700382 auto elementType = parseElementType();
383 if (!elementType)
384 return nullptr;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700385
386 if (!consumeIf(Token::greater))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700387 return (emitError("expected '>' in tensor type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700388
MLIR Team355ec862018-06-23 18:09:09 -0700389 if (isUnranked)
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700390 return builder.getTensorType(elementType);
391 return builder.getTensorType(dimensions, elementType);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700392}
393
394/// Parse a memref type.
395///
396/// memref-type ::= `memref` `<` dimension-list-ranked element-type
397/// (`,` semi-affine-map-composition)? (`,` memory-space)? `>`
398///
399/// semi-affine-map-composition ::= (semi-affine-map `,` )* semi-affine-map
400/// memory-space ::= integer-literal /* | TODO: address-space-id */
401///
Chris Lattnerf7e22732018-06-22 22:03:48 -0700402Type *Parser::parseMemRefType() {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700403 consumeToken(Token::kw_memref);
404
405 if (!consumeIf(Token::less))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700406 return (emitError("expected '<' in memref type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700407
408 SmallVector<int, 4> dimensions;
409 if (parseDimensionListRanked(dimensions))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700410 return nullptr;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700411
412 // Parse the element type.
Chris Lattnerf7e22732018-06-22 22:03:48 -0700413 auto elementType = parseElementType();
414 if (!elementType)
415 return nullptr;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700416
MLIR Team718c82f2018-07-16 09:45:22 -0700417 if (!consumeIf(Token::comma))
418 return (emitError("expected ',' in memref type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700419
MLIR Team718c82f2018-07-16 09:45:22 -0700420 // Parse semi-affine-map-composition.
421 SmallVector<AffineMap*, 2> affineMapComposition;
422 unsigned memorySpace;
423 bool parsedMemorySpace = false;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700424
MLIR Team718c82f2018-07-16 09:45:22 -0700425 auto parseElt = [&]() -> ParseResult {
426 if (getToken().is(Token::integer)) {
427 // Parse memory space.
428 if (parsedMemorySpace)
429 return emitError("multiple memory spaces specified in memref type");
430 auto v = getToken().getUnsignedIntegerValue();
431 if (!v.hasValue())
432 return emitError("invalid memory space in memref type");
433 memorySpace = v.getValue();
434 consumeToken(Token::integer);
435 parsedMemorySpace = true;
436 } else {
437 // Parse affine map.
438 if (parsedMemorySpace)
439 return emitError("affine map after memory space in memref type");
440 auto* affineMap = parseAffineMapReference();
441 if (affineMap == nullptr)
442 return ParseFailure;
443 affineMapComposition.push_back(affineMap);
444 }
445 return ParseSuccess;
446 };
447
448 // Parse comma separated list of affine maps, followed by memory space.
449 if (parseCommaSeparatedList(Token::greater, parseElt,
450 /*allowEmptyList=*/false)) {
451 return nullptr;
452 }
453 // Check that MemRef type specifies at least one affine map in composition.
454 if (affineMapComposition.empty())
455 return (emitError("expected semi-affine-map in memref type"), nullptr);
456 if (!parsedMemorySpace)
457 return (emitError("expected memory space in memref type"), nullptr);
458
459 return MemRefType::get(dimensions, elementType, affineMapComposition,
460 memorySpace);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700461}
462
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700463/// Parse a function type.
464///
465/// function-type ::= type-list-parens `->` type-list
466///
Chris Lattnerf7e22732018-06-22 22:03:48 -0700467Type *Parser::parseFunctionType() {
Chris Lattner48af7d12018-07-09 19:05:38 -0700468 assert(getToken().is(Token::l_paren));
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700469
Chris Lattnerf7e22732018-06-22 22:03:48 -0700470 SmallVector<Type*, 4> arguments;
471 if (parseTypeList(arguments))
472 return nullptr;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700473
474 if (!consumeIf(Token::arrow))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700475 return (emitError("expected '->' in function type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700476
Chris Lattnerf7e22732018-06-22 22:03:48 -0700477 SmallVector<Type*, 4> results;
478 if (parseTypeList(results))
479 return nullptr;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700480
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700481 return builder.getFunctionType(arguments, results);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700482}
483
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700484/// Parse an arbitrary type.
485///
486/// type ::= primitive-type
487/// | vector-type
488/// | tensor-type
489/// | memref-type
490/// | function-type
491/// element-type ::= primitive-type | vector-type
492///
Chris Lattnerf7e22732018-06-22 22:03:48 -0700493Type *Parser::parseType() {
Chris Lattner48af7d12018-07-09 19:05:38 -0700494 switch (getToken().getKind()) {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700495 case Token::kw_memref: return parseMemRefType();
496 case Token::kw_tensor: return parseTensorType();
497 case Token::kw_vector: return parseVectorType();
498 case Token::l_paren: return parseFunctionType();
499 default:
500 return parsePrimitiveType();
501 }
502}
503
504/// Parse a "type list", which is a singular type, or a parenthesized list of
505/// types.
506///
507/// type-list ::= type-list-parens | type
508/// type-list-parens ::= `(` `)`
509/// | `(` type (`,` type)* `)`
510///
Chris Lattnerf7e22732018-06-22 22:03:48 -0700511ParseResult Parser::parseTypeList(SmallVectorImpl<Type*> &elements) {
512 auto parseElt = [&]() -> ParseResult {
513 auto elt = parseType();
514 elements.push_back(elt);
515 return elt ? ParseSuccess : ParseFailure;
516 };
517
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700518 // If there is no parens, then it must be a singular type.
519 if (!consumeIf(Token::l_paren))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700520 return parseElt();
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700521
Chris Lattnerf7e22732018-06-22 22:03:48 -0700522 if (parseCommaSeparatedList(Token::r_paren, parseElt))
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700523 return ParseFailure;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700524
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700525 return ParseSuccess;
526}
527
Chris Lattner4c95a502018-06-23 16:03:42 -0700528//===----------------------------------------------------------------------===//
Chris Lattner7121b802018-07-04 20:45:39 -0700529// Attribute parsing.
530//===----------------------------------------------------------------------===//
531
532
533/// Attribute parsing.
534///
535/// attribute-value ::= bool-literal
536/// | integer-literal
537/// | float-literal
538/// | string-literal
539/// | `[` (attribute-value (`,` attribute-value)*)? `]`
540///
541Attribute *Parser::parseAttribute() {
Chris Lattner48af7d12018-07-09 19:05:38 -0700542 switch (getToken().getKind()) {
Chris Lattner7121b802018-07-04 20:45:39 -0700543 case Token::kw_true:
544 consumeToken(Token::kw_true);
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700545 return builder.getBoolAttr(true);
Chris Lattner7121b802018-07-04 20:45:39 -0700546 case Token::kw_false:
547 consumeToken(Token::kw_false);
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700548 return builder.getBoolAttr(false);
Chris Lattner7121b802018-07-04 20:45:39 -0700549
550 case Token::integer: {
Chris Lattner48af7d12018-07-09 19:05:38 -0700551 auto val = getToken().getUInt64IntegerValue();
Chris Lattner7121b802018-07-04 20:45:39 -0700552 if (!val.hasValue() || (int64_t)val.getValue() < 0)
553 return (emitError("integer too large for attribute"), nullptr);
554 consumeToken(Token::integer);
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700555 return builder.getIntegerAttr((int64_t)val.getValue());
Chris Lattner7121b802018-07-04 20:45:39 -0700556 }
557
558 case Token::minus: {
559 consumeToken(Token::minus);
Chris Lattner48af7d12018-07-09 19:05:38 -0700560 if (getToken().is(Token::integer)) {
561 auto val = getToken().getUInt64IntegerValue();
Chris Lattner7121b802018-07-04 20:45:39 -0700562 if (!val.hasValue() || (int64_t)-val.getValue() >= 0)
563 return (emitError("integer too large for attribute"), nullptr);
564 consumeToken(Token::integer);
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700565 return builder.getIntegerAttr((int64_t)-val.getValue());
Chris Lattner7121b802018-07-04 20:45:39 -0700566 }
567
568 return (emitError("expected constant integer or floating point value"),
569 nullptr);
570 }
571
572 case Token::string: {
Chris Lattner48af7d12018-07-09 19:05:38 -0700573 auto val = getToken().getStringValue();
Chris Lattner7121b802018-07-04 20:45:39 -0700574 consumeToken(Token::string);
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700575 return builder.getStringAttr(val);
Chris Lattner7121b802018-07-04 20:45:39 -0700576 }
577
578 case Token::l_bracket: {
579 consumeToken(Token::l_bracket);
580 SmallVector<Attribute*, 4> elements;
581
582 auto parseElt = [&]() -> ParseResult {
583 elements.push_back(parseAttribute());
584 return elements.back() ? ParseSuccess : ParseFailure;
585 };
586
587 if (parseCommaSeparatedList(Token::r_bracket, parseElt))
588 return nullptr;
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700589 return builder.getArrayAttr(elements);
Chris Lattner7121b802018-07-04 20:45:39 -0700590 }
591 default:
MLIR Teamb61885d2018-07-18 16:29:21 -0700592 // Try to parse affine map reference.
593 auto* affineMap = parseAffineMapReference();
594 if (affineMap != nullptr)
595 return builder.getAffineMapAttr(affineMap);
596
Chris Lattner7121b802018-07-04 20:45:39 -0700597 // TODO: Handle floating point.
598 return (emitError("expected constant attribute value"), nullptr);
599 }
600}
601
Chris Lattner7121b802018-07-04 20:45:39 -0700602/// Attribute dictionary.
603///
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700604/// attribute-dict ::= `{` `}`
605/// | `{` attribute-entry (`,` attribute-entry)* `}`
606/// attribute-entry ::= bare-id `:` attribute-value
Chris Lattner7121b802018-07-04 20:45:39 -0700607///
608ParseResult Parser::parseAttributeDict(
609 SmallVectorImpl<NamedAttribute> &attributes) {
610 consumeToken(Token::l_brace);
611
612 auto parseElt = [&]() -> ParseResult {
613 // We allow keywords as attribute names.
Chris Lattner48af7d12018-07-09 19:05:38 -0700614 if (getToken().isNot(Token::bare_identifier, Token::inttype) &&
615 !getToken().isKeyword())
Chris Lattner7121b802018-07-04 20:45:39 -0700616 return emitError("expected attribute name");
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700617 auto nameId = builder.getIdentifier(getTokenSpelling());
Chris Lattner7121b802018-07-04 20:45:39 -0700618 consumeToken();
619
620 if (!consumeIf(Token::colon))
621 return emitError("expected ':' in attribute list");
622
623 auto attr = parseAttribute();
624 if (!attr) return ParseFailure;
625
626 attributes.push_back({nameId, attr});
627 return ParseSuccess;
628 };
629
630 if (parseCommaSeparatedList(Token::r_brace, parseElt))
631 return ParseFailure;
632
633 return ParseSuccess;
634}
635
636//===----------------------------------------------------------------------===//
MLIR Teamf85a6262018-06-27 11:03:08 -0700637// Polyhedral structures.
638//===----------------------------------------------------------------------===//
639
Chris Lattner2e595eb2018-07-10 10:08:27 -0700640/// Lower precedence ops (all at the same precedence level). LNoOp is false in
641/// the boolean sense.
642enum AffineLowPrecOp {
643 /// Null value.
644 LNoOp,
645 Add,
646 Sub
647};
MLIR Teamf85a6262018-06-27 11:03:08 -0700648
Chris Lattner2e595eb2018-07-10 10:08:27 -0700649/// Higher precedence ops - all at the same precedence level. HNoOp is false in
650/// the boolean sense.
651enum AffineHighPrecOp {
652 /// Null value.
653 HNoOp,
654 Mul,
655 FloorDiv,
656 CeilDiv,
657 Mod
658};
Chris Lattner7121b802018-07-04 20:45:39 -0700659
Chris Lattner2e595eb2018-07-10 10:08:27 -0700660namespace {
661/// This is a specialized parser for AffineMap's, maintaining the state
662/// transient to their bodies.
663class AffineMapParser : public Parser {
664public:
665 explicit AffineMapParser(ParserState &state) : Parser(state) {}
Chris Lattner7121b802018-07-04 20:45:39 -0700666
Chris Lattner2e595eb2018-07-10 10:08:27 -0700667 AffineMap *parseAffineMapInline();
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700668
Chris Lattner2e595eb2018-07-10 10:08:27 -0700669private:
670 unsigned getNumDims() const { return dims.size(); }
671 unsigned getNumSymbols() const { return symbols.size(); }
MLIR Teamf85a6262018-06-27 11:03:08 -0700672
Uday Bondhugula0115dbb2018-07-11 21:31:07 -0700673 /// Returns true if the only identifiers the parser accepts in affine
674 /// expressions are symbolic identifiers.
675 bool isPureSymbolic() const { return pureSymbolic; }
676 void setSymbolicParsing(bool val) { pureSymbolic = val; }
677
Chris Lattner2e595eb2018-07-10 10:08:27 -0700678 // Binary affine op parsing.
679 AffineLowPrecOp consumeIfLowPrecOp();
680 AffineHighPrecOp consumeIfHighPrecOp();
MLIR Teamf85a6262018-06-27 11:03:08 -0700681
Chris Lattner2e595eb2018-07-10 10:08:27 -0700682 // Identifier lists for polyhedral structures.
683 ParseResult parseDimIdList();
684 ParseResult parseSymbolIdList();
685 ParseResult parseDimOrSymbolId(bool isDim);
686
687 AffineExpr *parseAffineExpr();
688 AffineExpr *parseParentheticalExpr();
689 AffineExpr *parseNegateExpression(AffineExpr *lhs);
690 AffineExpr *parseIntegerExpr();
691 AffineExpr *parseBareIdExpr();
692
693 AffineExpr *getBinaryAffineOpExpr(AffineHighPrecOp op, AffineExpr *lhs,
694 AffineExpr *rhs);
695 AffineExpr *getBinaryAffineOpExpr(AffineLowPrecOp op, AffineExpr *lhs,
696 AffineExpr *rhs);
697 AffineExpr *parseAffineOperandExpr(AffineExpr *lhs);
698 AffineExpr *parseAffineLowPrecOpExpr(AffineExpr *llhs,
699 AffineLowPrecOp llhsOp);
700 AffineExpr *parseAffineHighPrecOpExpr(AffineExpr *llhs,
701 AffineHighPrecOp llhsOp);
702
703private:
704 // TODO(bondhugula): could just use an vector/ArrayRef and scan the numbers.
705 llvm::StringMap<unsigned> dims;
706 llvm::StringMap<unsigned> symbols;
Uday Bondhugula0115dbb2018-07-11 21:31:07 -0700707 /// True if the parser should allow only symbolic identifiers in affine
708 /// expressions.
709 bool pureSymbolic = false;
Chris Lattner2e595eb2018-07-10 10:08:27 -0700710};
711} // end anonymous namespace
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700712
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700713/// Create an affine binary high precedence op expression (mul's, div's, mod)
Chris Lattner2e595eb2018-07-10 10:08:27 -0700714AffineExpr *AffineMapParser::getBinaryAffineOpExpr(AffineHighPrecOp op,
715 AffineExpr *lhs,
716 AffineExpr *rhs) {
Uday Bondhugula0115dbb2018-07-11 21:31:07 -0700717 // TODO: make the error location info accurate.
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700718 switch (op) {
719 case Mul:
Uday Bondhugulacbe4cca2018-07-19 13:07:16 -0700720 if (!lhs->isSymbolicOrConstant() && !rhs->isSymbolicOrConstant()) {
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700721 emitError("non-affine expression: at least one of the multiply "
722 "operands has to be either a constant or symbolic");
723 return nullptr;
724 }
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700725 return builder.getMulExpr(lhs, rhs);
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700726 case FloorDiv:
Uday Bondhugulacbe4cca2018-07-19 13:07:16 -0700727 if (!rhs->isSymbolicOrConstant()) {
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700728 emitError("non-affine expression: right operand of floordiv "
729 "has to be either a constant or symbolic");
730 return nullptr;
731 }
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700732 return builder.getFloorDivExpr(lhs, rhs);
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700733 case CeilDiv:
Uday Bondhugulacbe4cca2018-07-19 13:07:16 -0700734 if (!rhs->isSymbolicOrConstant()) {
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700735 emitError("non-affine expression: right operand of ceildiv "
736 "has to be either a constant or symbolic");
737 return nullptr;
738 }
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700739 return builder.getCeilDivExpr(lhs, rhs);
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700740 case Mod:
Uday Bondhugulacbe4cca2018-07-19 13:07:16 -0700741 if (!rhs->isSymbolicOrConstant()) {
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700742 emitError("non-affine expression: right operand of mod "
743 "has to be either a constant or symbolic");
744 return nullptr;
745 }
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700746 return builder.getModExpr(lhs, rhs);
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700747 case HNoOp:
748 llvm_unreachable("can't create affine expression for null high prec op");
749 return nullptr;
750 }
751}
752
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700753/// Create an affine binary low precedence op expression (add, sub).
Chris Lattner2e595eb2018-07-10 10:08:27 -0700754AffineExpr *AffineMapParser::getBinaryAffineOpExpr(AffineLowPrecOp op,
755 AffineExpr *lhs,
756 AffineExpr *rhs) {
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700757 switch (op) {
758 case AffineLowPrecOp::Add:
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700759 return builder.getAddExpr(lhs, rhs);
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700760 case AffineLowPrecOp::Sub:
Uday Bondhugulac1faf662018-07-19 14:08:50 -0700761 return builder.getAddExpr(
762 lhs, builder.getMulExpr(rhs, builder.getConstantExpr(-1)));
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700763 case AffineLowPrecOp::LNoOp:
764 llvm_unreachable("can't create affine expression for null low prec op");
765 return nullptr;
766 }
767}
768
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700769/// Consume this token if it is a lower precedence affine op (there are only two
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700770/// precedence levels).
Chris Lattner2e595eb2018-07-10 10:08:27 -0700771AffineLowPrecOp AffineMapParser::consumeIfLowPrecOp() {
Chris Lattner48af7d12018-07-09 19:05:38 -0700772 switch (getToken().getKind()) {
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700773 case Token::plus:
774 consumeToken(Token::plus);
775 return AffineLowPrecOp::Add;
776 case Token::minus:
777 consumeToken(Token::minus);
778 return AffineLowPrecOp::Sub;
779 default:
780 return AffineLowPrecOp::LNoOp;
781 }
782}
783
784/// Consume this token if it is a higher precedence affine op (there are only
785/// two precedence levels)
Chris Lattner2e595eb2018-07-10 10:08:27 -0700786AffineHighPrecOp AffineMapParser::consumeIfHighPrecOp() {
Chris Lattner48af7d12018-07-09 19:05:38 -0700787 switch (getToken().getKind()) {
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700788 case Token::star:
789 consumeToken(Token::star);
790 return Mul;
791 case Token::kw_floordiv:
792 consumeToken(Token::kw_floordiv);
793 return FloorDiv;
794 case Token::kw_ceildiv:
795 consumeToken(Token::kw_ceildiv);
796 return CeilDiv;
797 case Token::kw_mod:
798 consumeToken(Token::kw_mod);
799 return Mod;
800 default:
801 return HNoOp;
802 }
803}
804
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700805/// Parse a high precedence op expression list: mul, div, and mod are high
806/// precedence binary ops, i.e., parse a
807/// expr_1 op_1 expr_2 op_2 ... expr_n
808/// where op_1, op_2 are all a AffineHighPrecOp (mul, div, mod).
809/// All affine binary ops are left associative.
810/// Given llhs, returns (llhs llhsOp lhs) op rhs, or (lhs op rhs) if llhs is
811/// null. If no rhs can be found, returns (llhs llhsOp lhs) or lhs if llhs is
812/// null.
813AffineExpr *
Chris Lattner2e595eb2018-07-10 10:08:27 -0700814AffineMapParser::parseAffineHighPrecOpExpr(AffineExpr *llhs,
815 AffineHighPrecOp llhsOp) {
816 AffineExpr *lhs = parseAffineOperandExpr(llhs);
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700817 if (!lhs)
818 return nullptr;
819
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700820 // Found an LHS. Parse the remaining expression.
Chris Lattner2e595eb2018-07-10 10:08:27 -0700821 if (AffineHighPrecOp op = consumeIfHighPrecOp()) {
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700822 if (llhs) {
823 AffineExpr *expr = getBinaryAffineOpExpr(llhsOp, llhs, lhs);
824 if (!expr)
825 return nullptr;
Chris Lattner2e595eb2018-07-10 10:08:27 -0700826 return parseAffineHighPrecOpExpr(expr, op);
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700827 }
828 // No LLHS, get RHS
Chris Lattner2e595eb2018-07-10 10:08:27 -0700829 return parseAffineHighPrecOpExpr(lhs, op);
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700830 }
831
832 // This is the last operand in this expression.
833 if (llhs)
834 return getBinaryAffineOpExpr(llhsOp, llhs, lhs);
835
836 // No llhs, 'lhs' itself is the expression.
837 return lhs;
838}
839
840/// Parse an affine expression inside parentheses.
841///
842/// affine-expr ::= `(` affine-expr `)`
Chris Lattner2e595eb2018-07-10 10:08:27 -0700843AffineExpr *AffineMapParser::parseParentheticalExpr() {
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700844 if (!consumeIf(Token::l_paren))
845 return (emitError("expected '('"), nullptr);
Chris Lattner48af7d12018-07-09 19:05:38 -0700846 if (getToken().is(Token::r_paren))
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700847 return (emitError("no expression inside parentheses"), nullptr);
Chris Lattner2e595eb2018-07-10 10:08:27 -0700848 auto *expr = parseAffineExpr();
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700849 if (!expr)
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700850 return nullptr;
851 if (!consumeIf(Token::r_paren))
852 return (emitError("expected ')'"), nullptr);
853 return expr;
854}
855
856/// Parse the negation expression.
857///
858/// affine-expr ::= `-` affine-expr
Chris Lattner2e595eb2018-07-10 10:08:27 -0700859AffineExpr *AffineMapParser::parseNegateExpression(AffineExpr *lhs) {
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700860 if (!consumeIf(Token::minus))
861 return (emitError("expected '-'"), nullptr);
862
Chris Lattner2e595eb2018-07-10 10:08:27 -0700863 AffineExpr *operand = parseAffineOperandExpr(lhs);
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700864 // Since negation has the highest precedence of all ops (including high
865 // precedence ops) but lower than parentheses, we are only going to use
866 // parseAffineOperandExpr instead of parseAffineExpr here.
867 if (!operand)
868 // Extra error message although parseAffineOperandExpr would have
869 // complained. Leads to a better diagnostic.
870 return (emitError("missing operand of negation"), nullptr);
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700871 auto *minusOne = builder.getConstantExpr(-1);
872 return builder.getMulExpr(minusOne, operand);
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700873}
874
875/// Parse a bare id that may appear in an affine expression.
876///
877/// affine-expr ::= bare-id
Chris Lattner2e595eb2018-07-10 10:08:27 -0700878AffineExpr *AffineMapParser::parseBareIdExpr() {
Chris Lattner48af7d12018-07-09 19:05:38 -0700879 if (getToken().isNot(Token::bare_identifier))
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700880 return (emitError("expected bare identifier"), nullptr);
881
Chris Lattner48af7d12018-07-09 19:05:38 -0700882 StringRef sRef = getTokenSpelling();
Uday Bondhugula0115dbb2018-07-11 21:31:07 -0700883 // dims, symbols are all pairwise distinct.
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700884 if (dims.count(sRef)) {
Uday Bondhugula0115dbb2018-07-11 21:31:07 -0700885 if (isPureSymbolic())
886 return (emitError("identifier used is not a symbolic identifier"),
887 nullptr);
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700888 consumeToken(Token::bare_identifier);
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700889 return builder.getDimExpr(dims.lookup(sRef));
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700890 }
Uday Bondhugula0115dbb2018-07-11 21:31:07 -0700891
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700892 if (symbols.count(sRef)) {
893 consumeToken(Token::bare_identifier);
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700894 return builder.getSymbolExpr(symbols.lookup(sRef));
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700895 }
Uday Bondhugula0115dbb2018-07-11 21:31:07 -0700896
897 return (emitError("use of undeclared identifier"), nullptr);
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700898}
899
900/// Parse a positive integral constant appearing in an affine expression.
901///
902/// affine-expr ::= integer-literal
Chris Lattner2e595eb2018-07-10 10:08:27 -0700903AffineExpr *AffineMapParser::parseIntegerExpr() {
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700904 // No need to handle negative numbers separately here. They are naturally
905 // handled via the unary negation operator, although (FIXME) MININT_64 still
906 // not correctly handled.
Chris Lattner48af7d12018-07-09 19:05:38 -0700907 if (getToken().isNot(Token::integer))
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700908 return (emitError("expected integer"), nullptr);
909
Chris Lattner48af7d12018-07-09 19:05:38 -0700910 auto val = getToken().getUInt64IntegerValue();
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700911 if (!val.hasValue() || (int64_t)val.getValue() < 0) {
912 return (emitError("constant too large for affineint"), nullptr);
913 }
914 consumeToken(Token::integer);
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700915 return builder.getConstantExpr((int64_t)val.getValue());
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700916}
917
918/// Parses an expression that can be a valid operand of an affine expression.
Uday Bondhugula76345202018-07-09 13:47:52 -0700919/// lhs: if non-null, lhs is an affine expression that is the lhs of a binary
920/// operator, the rhs of which is being parsed. This is used to determine
921/// whether an error should be emitted for a missing right operand.
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700922// Eg: for an expression without parentheses (like i + j + k + l), each
923// of the four identifiers is an operand. For i + j*k + l, j*k is not an
924// operand expression, it's an op expression and will be parsed via
925// parseAffineHighPrecOpExpression(). However, for i + (j*k) + -l, (j*k) and -l
926// are valid operands that will be parsed by this function.
Chris Lattner2e595eb2018-07-10 10:08:27 -0700927AffineExpr *AffineMapParser::parseAffineOperandExpr(AffineExpr *lhs) {
Chris Lattner48af7d12018-07-09 19:05:38 -0700928 switch (getToken().getKind()) {
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700929 case Token::bare_identifier:
Chris Lattner2e595eb2018-07-10 10:08:27 -0700930 return parseBareIdExpr();
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700931 case Token::integer:
Chris Lattner2e595eb2018-07-10 10:08:27 -0700932 return parseIntegerExpr();
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700933 case Token::l_paren:
Chris Lattner2e595eb2018-07-10 10:08:27 -0700934 return parseParentheticalExpr();
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700935 case Token::minus:
Chris Lattner2e595eb2018-07-10 10:08:27 -0700936 return parseNegateExpression(lhs);
Uday Bondhugula76345202018-07-09 13:47:52 -0700937 case Token::kw_ceildiv:
938 case Token::kw_floordiv:
939 case Token::kw_mod:
940 case Token::plus:
941 case Token::star:
942 if (lhs)
943 emitError("missing right operand of binary operator");
944 else
945 emitError("missing left operand of binary operator");
946 return nullptr;
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700947 default:
948 if (lhs)
Uday Bondhugula76345202018-07-09 13:47:52 -0700949 emitError("missing right operand of binary operator");
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700950 else
951 emitError("expected affine expression");
952 return nullptr;
953 }
954}
955
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700956/// Parse affine expressions that are bare-id's, integer constants,
957/// parenthetical affine expressions, and affine op expressions that are a
958/// composition of those.
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700959///
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700960/// All binary op's associate from left to right.
961///
962/// {add, sub} have lower precedence than {mul, div, and mod}.
963///
Uday Bondhugula76345202018-07-09 13:47:52 -0700964/// Add, sub'are themselves at the same precedence level. Mul, floordiv,
965/// ceildiv, and mod are at the same higher precedence level. Negation has
966/// higher precedence than any binary op.
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700967///
968/// llhs: the affine expression appearing on the left of the one being parsed.
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700969/// This function will return ((llhs llhsOp lhs) op rhs) if llhs is non null,
970/// and lhs op rhs otherwise; if there is no rhs, llhs llhsOp lhs is returned if
971/// llhs is non-null; otherwise lhs is returned. This is to deal with left
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700972/// associativity.
973///
974/// Eg: when the expression is e1 + e2*e3 + e4, with e1 as llhs, this function
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700975/// will return the affine expr equivalent of (e1 + (e2*e3)) + e4, where (e2*e3)
976/// will be parsed using parseAffineHighPrecOpExpr().
Chris Lattner2e595eb2018-07-10 10:08:27 -0700977AffineExpr *AffineMapParser::parseAffineLowPrecOpExpr(AffineExpr *llhs,
978 AffineLowPrecOp llhsOp) {
Uday Bondhugula76345202018-07-09 13:47:52 -0700979 AffineExpr *lhs;
Chris Lattner2e595eb2018-07-10 10:08:27 -0700980 if (!(lhs = parseAffineOperandExpr(llhs)))
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700981 return nullptr;
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700982
983 // Found an LHS. Deal with the ops.
Chris Lattner2e595eb2018-07-10 10:08:27 -0700984 if (AffineLowPrecOp lOp = consumeIfLowPrecOp()) {
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700985 if (llhs) {
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700986 AffineExpr *sum = getBinaryAffineOpExpr(llhsOp, llhs, lhs);
Chris Lattner2e595eb2018-07-10 10:08:27 -0700987 return parseAffineLowPrecOpExpr(sum, lOp);
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700988 }
989 // No LLHS, get RHS and form the expression.
Chris Lattner2e595eb2018-07-10 10:08:27 -0700990 return parseAffineLowPrecOpExpr(lhs, lOp);
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700991 }
Chris Lattner2e595eb2018-07-10 10:08:27 -0700992 if (AffineHighPrecOp hOp = consumeIfHighPrecOp()) {
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700993 // We have a higher precedence op here. Get the rhs operand for the llhs
994 // through parseAffineHighPrecOpExpr.
Chris Lattner2e595eb2018-07-10 10:08:27 -0700995 AffineExpr *highRes = parseAffineHighPrecOpExpr(lhs, hOp);
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700996 if (!highRes)
997 return nullptr;
Chris Lattner2e595eb2018-07-10 10:08:27 -0700998
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700999 // If llhs is null, the product forms the first operand of the yet to be
Uday Bondhugula3934d4d2018-07-09 09:00:25 -07001000 // found expression. If non-null, the op to associate with llhs is llhsOp.
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001001 AffineExpr *expr =
Chris Lattner158e0a3e2018-07-08 20:51:38 -07001002 llhs ? getBinaryAffineOpExpr(llhsOp, llhs, highRes) : highRes;
Chris Lattner2e595eb2018-07-10 10:08:27 -07001003
Uday Bondhugula3934d4d2018-07-09 09:00:25 -07001004 // Recurse for subsequent low prec op's after the affine high prec op
1005 // expression.
Chris Lattner2e595eb2018-07-10 10:08:27 -07001006 if (AffineLowPrecOp nextOp = consumeIfLowPrecOp())
1007 return parseAffineLowPrecOpExpr(expr, nextOp);
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001008 return expr;
1009 }
Uday Bondhugula3934d4d2018-07-09 09:00:25 -07001010 // Last operand in the expression list.
1011 if (llhs)
1012 return getBinaryAffineOpExpr(llhsOp, llhs, lhs);
1013 // No llhs, 'lhs' itself is the expression.
1014 return lhs;
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001015}
1016
1017/// Parse an affine expression.
Uday Bondhugula3934d4d2018-07-09 09:00:25 -07001018/// affine-expr ::= `(` affine-expr `)`
1019/// | `-` affine-expr
1020/// | affine-expr `+` affine-expr
1021/// | affine-expr `-` affine-expr
1022/// | affine-expr `*` affine-expr
1023/// | affine-expr `floordiv` affine-expr
1024/// | affine-expr `ceildiv` affine-expr
1025/// | affine-expr `mod` affine-expr
1026/// | bare-id
1027/// | integer-literal
1028///
1029/// Additional conditions are checked depending on the production. For eg., one
1030/// of the operands for `*` has to be either constant/symbolic; the second
1031/// operand for floordiv, ceildiv, and mod has to be a positive integer.
Chris Lattner2e595eb2018-07-10 10:08:27 -07001032AffineExpr *AffineMapParser::parseAffineExpr() {
1033 return parseAffineLowPrecOpExpr(nullptr, AffineLowPrecOp::LNoOp);
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001034}
1035
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001036/// Parse a dim or symbol from the lists appearing before the actual expressions
Chris Lattner2e595eb2018-07-10 10:08:27 -07001037/// of the affine map. Update our state to store the dimensional/symbolic
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001038/// identifier. 'dim': whether it's the dim list or symbol list that is being
1039/// parsed.
Chris Lattner2e595eb2018-07-10 10:08:27 -07001040ParseResult AffineMapParser::parseDimOrSymbolId(bool isDim) {
Chris Lattner48af7d12018-07-09 19:05:38 -07001041 if (getToken().isNot(Token::bare_identifier))
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001042 return emitError("expected bare identifier");
Chris Lattner48af7d12018-07-09 19:05:38 -07001043 auto sRef = getTokenSpelling();
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001044 consumeToken(Token::bare_identifier);
Chris Lattner2e595eb2018-07-10 10:08:27 -07001045 if (dims.count(sRef))
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001046 return emitError("dimensional identifier name reused");
Chris Lattner2e595eb2018-07-10 10:08:27 -07001047 if (symbols.count(sRef))
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001048 return emitError("symbolic identifier name reused");
Chris Lattner2e595eb2018-07-10 10:08:27 -07001049 if (isDim)
1050 dims.insert({sRef, dims.size()});
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001051 else
Chris Lattner2e595eb2018-07-10 10:08:27 -07001052 symbols.insert({sRef, symbols.size()});
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001053 return ParseSuccess;
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001054}
1055
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001056/// Parse the list of symbolic identifiers to an affine map.
Chris Lattner2e595eb2018-07-10 10:08:27 -07001057ParseResult AffineMapParser::parseSymbolIdList() {
1058 if (!consumeIf(Token::l_bracket))
1059 return emitError("expected '['");
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001060
Chris Lattner2e595eb2018-07-10 10:08:27 -07001061 auto parseElt = [&]() -> ParseResult { return parseDimOrSymbolId(false); };
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001062 return parseCommaSeparatedList(Token::r_bracket, parseElt);
1063}
1064
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001065/// Parse the list of dimensional identifiers to an affine map.
Chris Lattner2e595eb2018-07-10 10:08:27 -07001066ParseResult AffineMapParser::parseDimIdList() {
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001067 if (!consumeIf(Token::l_paren))
1068 return emitError("expected '(' at start of dimensional identifiers list");
1069
Chris Lattner2e595eb2018-07-10 10:08:27 -07001070 auto parseElt = [&]() -> ParseResult { return parseDimOrSymbolId(true); };
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001071 return parseCommaSeparatedList(Token::r_paren, parseElt);
1072}
1073
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001074/// Parse an affine map definition.
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001075///
Uday Bondhugula3934d4d2018-07-09 09:00:25 -07001076/// affine-map-inline ::= dim-and-symbol-id-lists `->` multi-dim-affine-expr
1077/// (`size` `(` dim-size (`,` dim-size)* `)`)?
1078/// dim-size ::= affine-expr | `min` `(` affine-expr ( `,` affine-expr)+ `)`
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001079///
Uday Bondhugula3934d4d2018-07-09 09:00:25 -07001080/// multi-dim-affine-expr ::= `(` affine-expr (`,` affine-expr)* `)
Chris Lattner2e595eb2018-07-10 10:08:27 -07001081AffineMap *AffineMapParser::parseAffineMapInline() {
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001082 // List of dimensional identifiers.
Chris Lattner2e595eb2018-07-10 10:08:27 -07001083 if (parseDimIdList())
Chris Lattner7121b802018-07-04 20:45:39 -07001084 return nullptr;
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001085
1086 // Symbols are optional.
Chris Lattner48af7d12018-07-09 19:05:38 -07001087 if (getToken().is(Token::l_bracket)) {
Chris Lattner2e595eb2018-07-10 10:08:27 -07001088 if (parseSymbolIdList())
Chris Lattner7121b802018-07-04 20:45:39 -07001089 return nullptr;
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001090 }
1091 if (!consumeIf(Token::arrow)) {
Chris Lattner7121b802018-07-04 20:45:39 -07001092 return (emitError("expected '->' or '['"), nullptr);
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001093 }
1094 if (!consumeIf(Token::l_paren)) {
1095 emitError("expected '(' at start of affine map range");
Chris Lattner7121b802018-07-04 20:45:39 -07001096 return nullptr;
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001097 }
1098
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001099 SmallVector<AffineExpr *, 4> exprs;
1100 auto parseElt = [&]() -> ParseResult {
Chris Lattner2e595eb2018-07-10 10:08:27 -07001101 auto *elt = parseAffineExpr();
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001102 ParseResult res = elt ? ParseSuccess : ParseFailure;
1103 exprs.push_back(elt);
1104 return res;
1105 };
1106
1107 // Parse a multi-dimensional affine expression (a comma-separated list of 1-d
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001108 // affine expressions); the list cannot be empty.
1109 // Grammar: multi-dim-affine-expr ::= `(` affine-expr (`,` affine-expr)* `)
1110 if (parseCommaSeparatedList(Token::r_paren, parseElt, false))
Chris Lattner7121b802018-07-04 20:45:39 -07001111 return nullptr;
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001112
Uday Bondhugula0115dbb2018-07-11 21:31:07 -07001113 // Parse optional range sizes.
Uday Bondhugula1e500b42018-07-12 18:04:04 -07001114 // range-sizes ::= (`size` `(` dim-size (`,` dim-size)* `)`)?
1115 // dim-size ::= affine-expr | `min` `(` affine-expr (`,` affine-expr)+ `)`
1116 // TODO(bondhugula): support for min of several affine expressions.
Uday Bondhugula0115dbb2018-07-11 21:31:07 -07001117 // TODO: check if sizes are non-negative whenever they are constant.
1118 SmallVector<AffineExpr *, 4> rangeSizes;
1119 if (consumeIf(Token::kw_size)) {
1120 // Location of the l_paren token (if it exists) for error reporting later.
1121 auto loc = getToken().getLoc();
1122 if (!consumeIf(Token::l_paren))
1123 return (emitError("expected '(' at start of affine map range"), nullptr);
1124
1125 auto parseRangeSize = [&]() -> ParseResult {
1126 auto *elt = parseAffineExpr();
1127 ParseResult res = elt ? ParseSuccess : ParseFailure;
1128 rangeSizes.push_back(elt);
1129 return res;
1130 };
1131
1132 setSymbolicParsing(true);
1133 if (parseCommaSeparatedList(Token::r_paren, parseRangeSize, false))
1134 return nullptr;
1135 if (exprs.size() > rangeSizes.size())
1136 return (emitError(loc, "fewer range sizes than range expressions"),
1137 nullptr);
1138 if (exprs.size() < rangeSizes.size())
1139 return (emitError(loc, "more range sizes than range expressions"),
1140 nullptr);
1141 }
1142
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001143 // Parsed a valid affine map.
Uday Bondhugula0115dbb2018-07-11 21:31:07 -07001144 return builder.getAffineMap(dims.size(), symbols.size(), exprs, rangeSizes);
MLIR Teamf85a6262018-06-27 11:03:08 -07001145}
1146
Chris Lattner2e595eb2018-07-10 10:08:27 -07001147AffineMap *Parser::parseAffineMapInline() {
1148 return AffineMapParser(state).parseAffineMapInline();
1149}
1150
MLIR Team718c82f2018-07-16 09:45:22 -07001151AffineMap *Parser::parseAffineMapReference() {
1152 if (getToken().is(Token::hash_identifier)) {
1153 // Parse affine map identifier and verify that it exists.
1154 StringRef affineMapId = getTokenSpelling().drop_front();
1155 if (getState().affineMapDefinitions.count(affineMapId) == 0)
1156 return (emitError("undefined affine map id '" + affineMapId + "'"),
1157 nullptr);
1158 consumeToken(Token::hash_identifier);
1159 return getState().affineMapDefinitions[affineMapId];
1160 }
1161 // Try to parse inline affine map.
1162 return parseAffineMapInline();
1163}
1164
MLIR Teamf85a6262018-06-27 11:03:08 -07001165//===----------------------------------------------------------------------===//
Chris Lattner7f9cc272018-07-19 08:35:28 -07001166// FunctionParser
Chris Lattner4c95a502018-06-23 16:03:42 -07001167//===----------------------------------------------------------------------===//
Chris Lattnere79379a2018-06-22 10:39:19 -07001168
Chris Lattner7f9cc272018-07-19 08:35:28 -07001169namespace {
1170/// This class contains parser state that is common across CFG and ML functions,
1171/// notably for dealing with operations and SSA values.
1172class FunctionParser : public Parser {
1173public:
1174 FunctionParser(ParserState &state) : Parser(state) {}
1175
1176 /// This represents a use of an SSA value in the program. This tracks
1177 /// location information in case this ends up being a use of an undefined
1178 /// value.
1179 typedef std::pair<StringRef, SMLoc> SSAUseInfo;
1180
1181 /// Given a reference to an SSA value and its type, return a reference. This
1182 /// returns null on failure.
1183 SSAValue *resolveSSAUse(SSAUseInfo useInfo, Type *type);
1184
1185 /// Register a definition of a value with the symbol table.
1186 ParseResult addDefinition(SSAUseInfo useInfo, SSAValue *value);
1187
1188 // SSA parsing productions.
1189 ParseResult parseSSAUse(SSAUseInfo &result);
1190 ParseResult parseOptionalSSAUseList(Token::Kind endToken,
1191 SmallVectorImpl<SSAUseInfo> &results);
1192 SSAValue *parseSSAUseAndType();
1193 ParseResult
1194 parseOptionalSSAUseAndTypeList(Token::Kind endToken,
1195 SmallVectorImpl<SSAValue *> &results);
1196
1197 // Operations
1198 ParseResult parseOperation(const CreateOperationFunction &createOpFunc);
1199
1200private:
1201 /// This keeps track of all of the SSA values we are tracking, indexed by
1202 /// their name (either an identifier or a number).
1203 llvm::StringMap<std::pair<SSAValue *, SMLoc>> values;
1204};
1205} // end anonymous namespace
1206
1207/// Given an unbound reference to an SSA value and its type, return a the value
1208/// it specifies. This returns null on failure.
1209SSAValue *FunctionParser::resolveSSAUse(SSAUseInfo useInfo, Type *type) {
1210 // If we have already seen a value of this name, return it.
1211 auto it = values.find(useInfo.first);
1212 if (it != values.end()) {
1213 // Check that the type matches the other uses.
1214 auto result = it->second.first;
1215 if (result->getType() == type)
1216 return result;
1217
1218 emitError(useInfo.second, "use of value '" + useInfo.first.str() +
1219 "' expects different type than prior uses");
1220 emitError(it->second.second, "prior use here");
1221 return nullptr;
1222 }
1223
1224 // Otherwise we have a forward reference.
1225 // TODO: Handle forward references.
1226 emitError(useInfo.second, "undeclared or forward reference");
1227 return nullptr;
1228}
1229
1230/// Register a definition of a value with the symbol table.
1231ParseResult FunctionParser::addDefinition(SSAUseInfo useInfo, SSAValue *value) {
1232
1233 // If this is the first definition of this thing, then we are trivially done.
1234 auto insertInfo = values.insert({useInfo.first, {value, useInfo.second}});
1235 if (insertInfo.second)
1236 return ParseSuccess;
1237
1238 // If we already had a value, replace it with the new one and remove the
1239 // placeholder, only if it was a forward ref.
1240 // TODO: Handle forward references.
1241 emitError(useInfo.second, "redefinition of SSA value " + useInfo.first.str());
1242 return ParseFailure;
1243}
1244
Chris Lattner78276e32018-07-07 15:48:26 -07001245/// Parse a SSA operand for an instruction or statement.
1246///
1247/// ssa-use ::= ssa-id | ssa-constant
Chris Lattner7f9cc272018-07-19 08:35:28 -07001248/// TODO: SSA Constants.
Chris Lattner78276e32018-07-07 15:48:26 -07001249///
Chris Lattner7f9cc272018-07-19 08:35:28 -07001250ParseResult FunctionParser::parseSSAUse(SSAUseInfo &result) {
1251 result.first = getTokenSpelling();
1252 result.second = getToken().getLoc();
1253 if (!consumeIf(Token::percent_identifier))
1254 return emitError("expected SSA operand");
1255 return ParseSuccess;
Chris Lattner78276e32018-07-07 15:48:26 -07001256}
1257
1258/// Parse a (possibly empty) list of SSA operands.
1259///
1260/// ssa-use-list ::= ssa-use (`,` ssa-use)*
1261/// ssa-use-list-opt ::= ssa-use-list?
1262///
Chris Lattner7f9cc272018-07-19 08:35:28 -07001263ParseResult
1264FunctionParser::parseOptionalSSAUseList(Token::Kind endToken,
1265 SmallVectorImpl<SSAUseInfo> &results) {
1266 return parseCommaSeparatedList(endToken, [&]() -> ParseResult {
1267 SSAUseInfo result;
1268 if (parseSSAUse(result))
1269 return ParseFailure;
1270 results.push_back(result);
1271 return ParseSuccess;
1272 });
Chris Lattner78276e32018-07-07 15:48:26 -07001273}
1274
1275/// Parse an SSA use with an associated type.
1276///
1277/// ssa-use-and-type ::= ssa-use `:` type
Chris Lattner7f9cc272018-07-19 08:35:28 -07001278SSAValue *FunctionParser::parseSSAUseAndType() {
1279 SSAUseInfo useInfo;
1280 if (parseSSAUse(useInfo))
1281 return nullptr;
Chris Lattner78276e32018-07-07 15:48:26 -07001282
1283 if (!consumeIf(Token::colon))
Chris Lattner7f9cc272018-07-19 08:35:28 -07001284 return (emitError("expected ':' and type for SSA operand"), nullptr);
Chris Lattner78276e32018-07-07 15:48:26 -07001285
Chris Lattner7f9cc272018-07-19 08:35:28 -07001286 auto *type = parseType();
1287 if (!type)
1288 return nullptr;
Chris Lattner78276e32018-07-07 15:48:26 -07001289
Chris Lattner7f9cc272018-07-19 08:35:28 -07001290 return resolveSSAUse(useInfo, type);
Chris Lattner78276e32018-07-07 15:48:26 -07001291}
1292
1293/// Parse a (possibly empty) list of SSA operands with types.
1294///
1295/// ssa-use-and-type-list ::= ssa-use-and-type (`,` ssa-use-and-type)*
1296///
Chris Lattner7f9cc272018-07-19 08:35:28 -07001297ParseResult FunctionParser::parseOptionalSSAUseAndTypeList(
1298 Token::Kind endToken, SmallVectorImpl<SSAValue *> &results) {
1299 return parseCommaSeparatedList(endToken, [&]() -> ParseResult {
1300 if (auto *value = parseSSAUseAndType()) {
1301 results.push_back(value);
1302 return ParseSuccess;
1303 }
1304 return ParseFailure;
1305 });
Chris Lattner78276e32018-07-07 15:48:26 -07001306}
1307
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001308/// Parse the CFG or MLFunc operation.
1309///
1310/// TODO(clattner): This is a change from the MLIR spec as written, it is an
1311/// experiment that will eliminate "builtin" instructions as a thing.
1312///
1313/// operation ::=
1314/// (ssa-id `=`)? string '(' ssa-use-list? ')' attribute-dict?
1315/// `:` function-type
1316///
1317ParseResult
Chris Lattner7f9cc272018-07-19 08:35:28 -07001318FunctionParser::parseOperation(const CreateOperationFunction &createOpFunc) {
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001319 auto loc = getToken().getLoc();
1320
1321 StringRef resultID;
1322 if (getToken().is(Token::percent_identifier)) {
Chris Lattner7f9cc272018-07-19 08:35:28 -07001323 resultID = getTokenSpelling();
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001324 consumeToken(Token::percent_identifier);
1325 if (!consumeIf(Token::equal))
1326 return emitError("expected '=' after SSA name");
1327 }
1328
1329 if (getToken().isNot(Token::string))
1330 return emitError("expected operation name in quotes");
1331
1332 auto name = getToken().getStringValue();
1333 if (name.empty())
1334 return emitError("empty operation name is invalid");
1335
1336 consumeToken(Token::string);
1337
1338 if (!consumeIf(Token::l_paren))
1339 return emitError("expected '(' to start operand list");
1340
1341 // Parse the operand list.
Chris Lattner7f9cc272018-07-19 08:35:28 -07001342 SmallVector<SSAUseInfo, 8> operandInfos;
1343 parseOptionalSSAUseList(Token::r_paren, operandInfos);
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001344
1345 SmallVector<NamedAttribute, 4> attributes;
1346 if (getToken().is(Token::l_brace)) {
1347 if (parseAttributeDict(attributes))
1348 return ParseFailure;
1349 }
1350
Chris Lattner3b2ef762018-07-18 15:31:25 -07001351 if (!consumeIf(Token::colon))
1352 return emitError("expected ':' followed by instruction type");
1353
1354 auto typeLoc = getToken().getLoc();
1355 auto type = parseType();
1356 if (!type)
1357 return ParseFailure;
1358 auto fnType = dyn_cast<FunctionType>(type);
1359 if (!fnType)
1360 return emitError(typeLoc, "expected function type");
1361
Chris Lattner7f9cc272018-07-19 08:35:28 -07001362 // Check that we have the right number of types for the operands.
1363 auto operandTypes = fnType->getInputs();
1364 if (operandTypes.size() != operandInfos.size()) {
1365 auto plural = "s"[operandInfos.size() == 1];
1366 return emitError(typeLoc, "expected " + llvm::utostr(operandInfos.size()) +
Chris Lattnerf8cce872018-07-20 09:28:54 -07001367 " operand type" + plural + " but had " +
Chris Lattner7f9cc272018-07-19 08:35:28 -07001368 llvm::utostr(operandTypes.size()));
1369 }
1370
1371 // Resolve all of the operands.
1372 SmallVector<SSAValue *, 8> operands;
1373 for (unsigned i = 0, e = operandInfos.size(); i != e; ++i) {
1374 operands.push_back(resolveSSAUse(operandInfos[i], operandTypes[i]));
1375 if (!operands.back())
1376 return ParseFailure;
1377 }
1378
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001379 auto nameId = builder.getIdentifier(name);
Chris Lattner7f9cc272018-07-19 08:35:28 -07001380 auto op = createOpFunc(nameId, operands, fnType->getResults(), attributes);
1381 if (!op)
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001382 return ParseFailure;
1383
1384 // We just parsed an operation. If it is a recognized one, verify that it
1385 // is structurally as we expect. If not, produce an error with a reasonable
1386 // source location.
Chris Lattner7f9cc272018-07-19 08:35:28 -07001387 if (auto *opInfo = op->getAbstractOperation(builder.getContext())) {
1388 if (auto error = opInfo->verifyInvariants(op))
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001389 return emitError(loc, error);
1390 }
1391
Chris Lattner7f9cc272018-07-19 08:35:28 -07001392 // If the instruction had a name, register it.
1393 if (!resultID.empty()) {
1394 // FIXME: Add result infra to handle Stmt results as well to make this
1395 // generic.
1396 if (auto *inst = dyn_cast<OperationInst>(op)) {
Chris Lattnerf8cce872018-07-20 09:28:54 -07001397 if (inst->getNumResults() == 0)
Chris Lattner7f9cc272018-07-19 08:35:28 -07001398 return emitError(loc, "cannot name an operation with no results");
1399
Chris Lattnerf8cce872018-07-20 09:28:54 -07001400 addDefinition({resultID, loc}, inst->getResult(0));
Chris Lattner7f9cc272018-07-19 08:35:28 -07001401 }
1402 }
1403
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001404 return ParseSuccess;
1405}
Chris Lattnere79379a2018-06-22 10:39:19 -07001406
Chris Lattner48af7d12018-07-09 19:05:38 -07001407//===----------------------------------------------------------------------===//
1408// CFG Functions
1409//===----------------------------------------------------------------------===//
Chris Lattnere79379a2018-06-22 10:39:19 -07001410
Chris Lattner4c95a502018-06-23 16:03:42 -07001411namespace {
Chris Lattner48af7d12018-07-09 19:05:38 -07001412/// This is a specialized parser for CFGFunction's, maintaining the state
1413/// transient to their bodies.
Chris Lattner7f9cc272018-07-19 08:35:28 -07001414class CFGFunctionParser : public FunctionParser {
Chris Lattner158e0a3e2018-07-08 20:51:38 -07001415public:
Chris Lattner2e595eb2018-07-10 10:08:27 -07001416 CFGFunctionParser(ParserState &state, CFGFunction *function)
Chris Lattner7f9cc272018-07-19 08:35:28 -07001417 : FunctionParser(state), function(function), builder(function) {}
Chris Lattner2e595eb2018-07-10 10:08:27 -07001418
1419 ParseResult parseFunctionBody();
1420
1421private:
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001422 CFGFunction *function;
1423 llvm::StringMap<std::pair<BasicBlock*, SMLoc>> blocksByName;
Chris Lattner48af7d12018-07-09 19:05:38 -07001424
1425 /// This builder intentionally shadows the builder in the base class, with a
1426 /// more specific builder type.
Chris Lattner158e0a3e2018-07-08 20:51:38 -07001427 CFGFuncBuilder builder;
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001428
Chris Lattner4c95a502018-06-23 16:03:42 -07001429 /// Get the basic block with the specified name, creating it if it doesn't
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001430 /// already exist. The location specified is the point of use, which allows
1431 /// us to diagnose references to blocks that are not defined precisely.
1432 BasicBlock *getBlockNamed(StringRef name, SMLoc loc) {
1433 auto &blockAndLoc = blocksByName[name];
1434 if (!blockAndLoc.first) {
Chris Lattner3a467cc2018-07-01 20:28:00 -07001435 blockAndLoc.first = new BasicBlock();
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001436 blockAndLoc.second = loc;
Chris Lattner4c95a502018-06-23 16:03:42 -07001437 }
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001438 return blockAndLoc.first;
Chris Lattner4c95a502018-06-23 16:03:42 -07001439 }
Chris Lattner48af7d12018-07-09 19:05:38 -07001440
Chris Lattner48af7d12018-07-09 19:05:38 -07001441 ParseResult parseBasicBlock();
1442 OperationInst *parseCFGOperation();
1443 TerminatorInst *parseTerminator();
Chris Lattner4c95a502018-06-23 16:03:42 -07001444};
1445} // end anonymous namespace
1446
Chris Lattner48af7d12018-07-09 19:05:38 -07001447ParseResult CFGFunctionParser::parseFunctionBody() {
1448 if (!consumeIf(Token::l_brace))
1449 return emitError("expected '{' in CFG function");
1450
1451 // Make sure we have at least one block.
1452 if (getToken().is(Token::r_brace))
1453 return emitError("CFG functions must have at least one basic block");
Chris Lattner4c95a502018-06-23 16:03:42 -07001454
1455 // Parse the list of blocks.
1456 while (!consumeIf(Token::r_brace))
Chris Lattner48af7d12018-07-09 19:05:38 -07001457 if (parseBasicBlock())
Chris Lattner4c95a502018-06-23 16:03:42 -07001458 return ParseFailure;
1459
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001460 // Verify that all referenced blocks were defined. Iteration over a
1461 // StringMap isn't determinstic, but this is good enough for our purposes.
Chris Lattner48af7d12018-07-09 19:05:38 -07001462 for (auto &elt : blocksByName) {
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001463 auto *bb = elt.second.first;
Chris Lattner3a467cc2018-07-01 20:28:00 -07001464 if (!bb->getFunction())
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001465 return emitError(elt.second.second,
1466 "reference to an undefined basic block '" +
1467 elt.first() + "'");
1468 }
1469
Chris Lattner48af7d12018-07-09 19:05:38 -07001470 getModule()->functionList.push_back(function);
Chris Lattner4c95a502018-06-23 16:03:42 -07001471 return ParseSuccess;
1472}
1473
1474/// Basic block declaration.
1475///
1476/// basic-block ::= bb-label instruction* terminator-stmt
1477/// bb-label ::= bb-id bb-arg-list? `:`
1478/// bb-id ::= bare-id
1479/// bb-arg-list ::= `(` ssa-id-and-type-list? `)`
1480///
Chris Lattner48af7d12018-07-09 19:05:38 -07001481ParseResult CFGFunctionParser::parseBasicBlock() {
1482 SMLoc nameLoc = getToken().getLoc();
1483 auto name = getTokenSpelling();
Chris Lattner4c95a502018-06-23 16:03:42 -07001484 if (!consumeIf(Token::bare_identifier))
1485 return emitError("expected basic block name");
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001486
Chris Lattner48af7d12018-07-09 19:05:38 -07001487 auto *block = getBlockNamed(name, nameLoc);
Chris Lattner4c95a502018-06-23 16:03:42 -07001488
1489 // If this block has already been parsed, then this is a redefinition with the
1490 // same block name.
Chris Lattner3a467cc2018-07-01 20:28:00 -07001491 if (block->getFunction())
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001492 return emitError(nameLoc, "redefinition of block '" + name.str() + "'");
1493
Chris Lattner3a467cc2018-07-01 20:28:00 -07001494 // Add the block to the function.
Chris Lattner48af7d12018-07-09 19:05:38 -07001495 function->push_back(block);
Chris Lattner4c95a502018-06-23 16:03:42 -07001496
Chris Lattner78276e32018-07-07 15:48:26 -07001497 // If an argument list is present, parse it.
1498 if (consumeIf(Token::l_paren)) {
Chris Lattner7f9cc272018-07-19 08:35:28 -07001499 SmallVector<SSAValue *, 8> bbArgs;
1500 if (parseOptionalSSAUseAndTypeList(Token::r_paren, bbArgs))
Chris Lattner78276e32018-07-07 15:48:26 -07001501 return ParseFailure;
1502
1503 // TODO: attach it.
1504 }
Chris Lattner4c95a502018-06-23 16:03:42 -07001505
1506 if (!consumeIf(Token::colon))
1507 return emitError("expected ':' after basic block name");
1508
Chris Lattner158e0a3e2018-07-08 20:51:38 -07001509 // Set the insertion point to the block we want to insert new operations into.
Chris Lattner48af7d12018-07-09 19:05:38 -07001510 builder.setInsertionPoint(block);
Chris Lattner158e0a3e2018-07-08 20:51:38 -07001511
Chris Lattner7f9cc272018-07-19 08:35:28 -07001512 auto createOpFunc = [&](Identifier name, ArrayRef<SSAValue *> operands,
1513 ArrayRef<Type *> resultTypes,
1514 ArrayRef<NamedAttribute> attrs) -> Operation * {
1515 SmallVector<CFGValue *, 8> cfgOperands;
1516 cfgOperands.reserve(operands.size());
1517 for (auto *op : operands)
1518 cfgOperands.push_back(cast<CFGValue>(op));
1519 return builder.createOperation(name, cfgOperands, resultTypes, attrs);
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001520 };
1521
Chris Lattnered65a732018-06-28 20:45:33 -07001522 // Parse the list of operations that make up the body of the block.
Chris Lattner48af7d12018-07-09 19:05:38 -07001523 while (getToken().isNot(Token::kw_return, Token::kw_br)) {
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001524 if (parseOperation(createOpFunc))
Chris Lattnered65a732018-06-28 20:45:33 -07001525 return ParseFailure;
1526 }
Chris Lattner4c95a502018-06-23 16:03:42 -07001527
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001528 if (!parseTerminator())
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001529 return ParseFailure;
Chris Lattner4c95a502018-06-23 16:03:42 -07001530
1531 return ParseSuccess;
1532}
1533
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001534/// Parse the terminator instruction for a basic block.
1535///
1536/// terminator-stmt ::= `br` bb-id branch-use-list?
1537/// branch-use-list ::= `(` ssa-use-and-type-list? `)`
1538/// terminator-stmt ::=
1539/// `cond_br` ssa-use `,` bb-id branch-use-list? `,` bb-id branch-use-list?
1540/// terminator-stmt ::= `return` ssa-use-and-type-list?
1541///
Chris Lattner48af7d12018-07-09 19:05:38 -07001542TerminatorInst *CFGFunctionParser::parseTerminator() {
1543 switch (getToken().getKind()) {
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001544 default:
Chris Lattner3a467cc2018-07-01 20:28:00 -07001545 return (emitError("expected terminator at end of basic block"), nullptr);
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001546
1547 case Token::kw_return:
1548 consumeToken(Token::kw_return);
Chris Lattner48af7d12018-07-09 19:05:38 -07001549 return builder.createReturnInst();
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001550
1551 case Token::kw_br: {
1552 consumeToken(Token::kw_br);
Chris Lattner48af7d12018-07-09 19:05:38 -07001553 auto destBB = getBlockNamed(getTokenSpelling(), getToken().getLoc());
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001554 if (!consumeIf(Token::bare_identifier))
Chris Lattner3a467cc2018-07-01 20:28:00 -07001555 return (emitError("expected basic block name"), nullptr);
Chris Lattner48af7d12018-07-09 19:05:38 -07001556 return builder.createBranchInst(destBB);
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001557 }
Chris Lattner78276e32018-07-07 15:48:26 -07001558 // TODO: cond_br.
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001559 }
1560}
1561
Chris Lattner48af7d12018-07-09 19:05:38 -07001562//===----------------------------------------------------------------------===//
1563// ML Functions
1564//===----------------------------------------------------------------------===//
1565
1566namespace {
1567/// Refined parser for MLFunction bodies.
Chris Lattner7f9cc272018-07-19 08:35:28 -07001568class MLFunctionParser : public FunctionParser {
Chris Lattner48af7d12018-07-09 19:05:38 -07001569public:
Chris Lattner48af7d12018-07-09 19:05:38 -07001570 MLFunctionParser(ParserState &state, MLFunction *function)
Chris Lattner7f9cc272018-07-19 08:35:28 -07001571 : FunctionParser(state), function(function), builder(function) {}
Chris Lattner48af7d12018-07-09 19:05:38 -07001572
1573 ParseResult parseFunctionBody();
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -07001574
1575private:
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001576 MLFunction *function;
1577
1578 /// This builder intentionally shadows the builder in the base class, with a
1579 /// more specific builder type.
1580 MLFuncBuilder builder;
1581
1582 ParseResult parseForStmt();
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001583 AffineConstantExpr *parseIntConstant();
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001584 ParseResult parseIfStmt();
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -07001585 ParseResult parseElseClause(IfClause *elseClause);
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001586 ParseResult parseStatements(StmtBlock *block);
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -07001587 ParseResult parseStmtBlock(StmtBlock *block);
Chris Lattner48af7d12018-07-09 19:05:38 -07001588};
1589} // end anonymous namespace
1590
Chris Lattner48af7d12018-07-09 19:05:38 -07001591ParseResult MLFunctionParser::parseFunctionBody() {
1592 if (!consumeIf(Token::l_brace))
1593 return emitError("expected '{' in ML function");
1594
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001595 // Parse statements in this function
1596 if (parseStatements(function))
1597 return ParseFailure;
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -07001598
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001599 if (!consumeIf(Token::kw_return))
1600 emitError("ML function must end with return statement");
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001601
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001602 // TODO: store return operands in the IR.
1603 SmallVector<SSAUseInfo, 4> dummyUseInfo;
1604 if (parseOptionalSSAUseList(Token::r_brace, dummyUseInfo))
1605 return ParseFailure;
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -07001606
Chris Lattner48af7d12018-07-09 19:05:38 -07001607 getModule()->functionList.push_back(function);
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -07001608
1609 return ParseSuccess;
1610}
1611
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001612/// For statement.
1613///
Chris Lattner48af7d12018-07-09 19:05:38 -07001614/// ml-for-stmt ::= `for` ssa-id `=` lower-bound `to` upper-bound
1615/// (`step` integer-literal)? `{` ml-stmt* `}`
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001616///
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001617ParseResult MLFunctionParser::parseForStmt() {
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001618 consumeToken(Token::kw_for);
1619
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001620 // Parse induction variable
1621 if (getToken().isNot(Token::percent_identifier))
1622 return emitError("expected SSA identifier for the loop variable");
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001623
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001624 // TODO: create SSA value definition from name
1625 StringRef name = getTokenSpelling().drop_front();
1626 (void)name;
1627
1628 consumeToken(Token::percent_identifier);
1629
1630 if (!consumeIf(Token::equal))
1631 return emitError("expected =");
1632
1633 // Parse loop bounds
1634 AffineConstantExpr *lowerBound = parseIntConstant();
1635 if (!lowerBound)
1636 return ParseFailure;
1637
1638 if (!consumeIf(Token::kw_to))
1639 return emitError("expected 'to' between bounds");
1640
1641 AffineConstantExpr *upperBound = parseIntConstant();
1642 if (!upperBound)
1643 return ParseFailure;
1644
1645 // Parse step
1646 AffineConstantExpr *step = nullptr;
1647 if (consumeIf(Token::kw_step)) {
1648 step = parseIntConstant();
1649 if (!step)
1650 return ParseFailure;
1651 }
1652
1653 // Create for statement.
1654 ForStmt *stmt = builder.createFor(lowerBound, upperBound, step);
1655
1656 // If parsing of the for statement body fails,
1657 // MLIR contains for statement with those nested statements that have been
1658 // successfully parsed.
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001659 if (parseStmtBlock(static_cast<StmtBlock *>(stmt)))
1660 return ParseFailure;
1661
1662 return ParseSuccess;
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001663}
1664
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001665// This method is temporary workaround to parse simple loop bounds and
1666// step.
1667// TODO: remove this method once it's no longer used.
1668AffineConstantExpr *MLFunctionParser::parseIntConstant() {
1669 if (getToken().isNot(Token::integer))
1670 return (emitError("expected non-negative integer for now"), nullptr);
1671
1672 auto val = getToken().getUInt64IntegerValue();
1673 if (!val.hasValue() || (int64_t)val.getValue() < 0) {
1674 return (emitError("constant too large for affineint"), nullptr);
1675 }
1676 consumeToken(Token::integer);
1677 return builder.getConstantExpr((int64_t)val.getValue());
1678}
1679
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001680/// If statement.
1681///
Chris Lattner48af7d12018-07-09 19:05:38 -07001682/// ml-if-head ::= `if` ml-if-cond `{` ml-stmt* `}`
1683/// | ml-if-head `else` `if` ml-if-cond `{` ml-stmt* `}`
1684/// ml-if-stmt ::= ml-if-head
1685/// | ml-if-head `else` `{` ml-stmt* `}`
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001686///
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001687ParseResult MLFunctionParser::parseIfStmt() {
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001688 consumeToken(Token::kw_if);
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -07001689 if (!consumeIf(Token::l_paren))
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001690 return emitError("expected (");
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001691
1692 //TODO: parse condition
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -07001693
1694 if (!consumeIf(Token::r_paren))
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001695 return emitError("expected ')'");
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -07001696
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001697 IfStmt *ifStmt = builder.createIf();
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -07001698 IfClause *thenClause = ifStmt->getThenClause();
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001699
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001700 // When parsing of an if statement body fails, the IR contains
1701 // the if statement with the portion of the body that has been
1702 // successfully parsed.
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001703 if (parseStmtBlock(thenClause))
1704 return ParseFailure;
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001705
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -07001706 if (consumeIf(Token::kw_else)) {
1707 IfClause *elseClause = ifStmt->createElseClause();
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001708 if (parseElseClause(elseClause))
1709 return ParseFailure;
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001710 }
1711
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001712 return ParseSuccess;
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -07001713}
1714
1715ParseResult MLFunctionParser::parseElseClause(IfClause *elseClause) {
1716 if (getToken().is(Token::kw_if)) {
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001717 builder.setInsertionPoint(elseClause);
1718 return parseIfStmt();
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -07001719 }
1720
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001721 return parseStmtBlock(elseClause);
1722}
1723
1724///
1725/// Parse a list of statements ending with `return` or `}`
1726///
1727ParseResult MLFunctionParser::parseStatements(StmtBlock *block) {
Chris Lattner7f9cc272018-07-19 08:35:28 -07001728 auto createOpFunc = [&](Identifier name, ArrayRef<SSAValue *> operands,
1729 ArrayRef<Type *> resultTypes,
1730 ArrayRef<NamedAttribute> attrs) -> Operation * {
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001731 return builder.createOperation(name, attrs);
1732 };
1733
1734 builder.setInsertionPoint(block);
1735
1736 while (getToken().isNot(Token::kw_return, Token::r_brace)) {
1737 switch (getToken().getKind()) {
1738 default:
1739 if (parseOperation(createOpFunc))
1740 return ParseFailure;
1741 break;
1742 case Token::kw_for:
1743 if (parseForStmt())
1744 return ParseFailure;
1745 break;
1746 case Token::kw_if:
1747 if (parseIfStmt())
1748 return ParseFailure;
1749 break;
1750 } // end switch
1751 }
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -07001752
1753 return ParseSuccess;
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001754}
1755
1756///
1757/// Parse `{` ml-stmt* `}`
1758///
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -07001759ParseResult MLFunctionParser::parseStmtBlock(StmtBlock *block) {
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001760 if (!consumeIf(Token::l_brace))
1761 return emitError("expected '{' before statement list");
1762
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001763 if (parseStatements(block))
1764 return ParseFailure;
1765
1766 if (!consumeIf(Token::r_brace))
1767 return emitError("expected '}' at the end of the statement block");
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001768
1769 return ParseSuccess;
1770}
1771
Chris Lattner4c95a502018-06-23 16:03:42 -07001772//===----------------------------------------------------------------------===//
1773// Top-level entity parsing.
1774//===----------------------------------------------------------------------===//
1775
Chris Lattner2e595eb2018-07-10 10:08:27 -07001776namespace {
1777/// This parser handles entities that are only valid at the top level of the
1778/// file.
1779class ModuleParser : public Parser {
1780public:
1781 explicit ModuleParser(ParserState &state) : Parser(state) {}
1782
1783 ParseResult parseModule();
1784
1785private:
1786 ParseResult parseAffineMapDef();
1787
1788 // Functions.
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001789 ParseResult parseMLArgumentList(SmallVectorImpl<Type *> &argTypes,
1790 SmallVectorImpl<StringRef> &argNames);
1791 ParseResult parseFunctionSignature(StringRef &name, FunctionType *&type,
1792 SmallVectorImpl<StringRef> *argNames);
Chris Lattner2e595eb2018-07-10 10:08:27 -07001793 ParseResult parseExtFunc();
1794 ParseResult parseCFGFunc();
1795 ParseResult parseMLFunc();
1796};
1797} // end anonymous namespace
1798
1799/// Affine map declaration.
1800///
1801/// affine-map-def ::= affine-map-id `=` affine-map-inline
1802///
1803ParseResult ModuleParser::parseAffineMapDef() {
1804 assert(getToken().is(Token::hash_identifier));
1805
1806 StringRef affineMapId = getTokenSpelling().drop_front();
1807
1808 // Check for redefinitions.
1809 auto *&entry = getState().affineMapDefinitions[affineMapId];
1810 if (entry)
1811 return emitError("redefinition of affine map id '" + affineMapId + "'");
1812
1813 consumeToken(Token::hash_identifier);
1814
1815 // Parse the '='
1816 if (!consumeIf(Token::equal))
1817 return emitError("expected '=' in affine map outlined definition");
1818
1819 entry = parseAffineMapInline();
1820 if (!entry)
1821 return ParseFailure;
1822
Chris Lattner2e595eb2018-07-10 10:08:27 -07001823 return ParseSuccess;
1824}
1825
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001826/// Parse a (possibly empty) list of MLFunction arguments with types.
1827///
1828/// ml-argument ::= ssa-id `:` type
1829/// ml-argument-list ::= ml-argument (`,` ml-argument)* | /*empty*/
1830///
1831ParseResult
1832ModuleParser::parseMLArgumentList(SmallVectorImpl<Type *> &argTypes,
1833 SmallVectorImpl<StringRef> &argNames) {
1834 auto parseElt = [&]() -> ParseResult {
1835 // Parse argument name
1836 if (getToken().isNot(Token::percent_identifier))
1837 return emitError("expected SSA identifier");
1838
1839 StringRef name = getTokenSpelling().drop_front();
1840 consumeToken(Token::percent_identifier);
1841 argNames.push_back(name);
1842
1843 if (!consumeIf(Token::colon))
1844 return emitError("expected ':'");
1845
1846 // Parse argument type
1847 auto elt = parseType();
1848 if (!elt)
1849 return ParseFailure;
1850 argTypes.push_back(elt);
1851
1852 return ParseSuccess;
1853 };
1854
1855 if (!consumeIf(Token::l_paren))
1856 llvm_unreachable("expected '('");
1857
1858 return parseCommaSeparatedList(Token::r_paren, parseElt);
1859}
1860
Chris Lattner2e595eb2018-07-10 10:08:27 -07001861/// Parse a function signature, starting with a name and including the parameter
1862/// list.
1863///
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001864/// argument-list ::= type (`,` type)* | /*empty*/ | ml-argument-list
Chris Lattner2e595eb2018-07-10 10:08:27 -07001865/// function-signature ::= function-id `(` argument-list `)` (`->` type-list)?
1866///
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001867ParseResult
1868ModuleParser::parseFunctionSignature(StringRef &name, FunctionType *&type,
1869 SmallVectorImpl<StringRef> *argNames) {
Chris Lattner2e595eb2018-07-10 10:08:27 -07001870 if (getToken().isNot(Token::at_identifier))
1871 return emitError("expected a function identifier like '@foo'");
1872
1873 name = getTokenSpelling().drop_front();
1874 consumeToken(Token::at_identifier);
1875
1876 if (getToken().isNot(Token::l_paren))
1877 return emitError("expected '(' in function signature");
1878
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001879 SmallVector<Type *, 4> argTypes;
1880 ParseResult parseResult;
1881
1882 if (argNames)
1883 parseResult = parseMLArgumentList(argTypes, *argNames);
1884 else
1885 parseResult = parseTypeList(argTypes);
1886
1887 if (parseResult)
Chris Lattner2e595eb2018-07-10 10:08:27 -07001888 return ParseFailure;
1889
1890 // Parse the return type if present.
1891 SmallVector<Type *, 4> results;
1892 if (consumeIf(Token::arrow)) {
1893 if (parseTypeList(results))
1894 return ParseFailure;
1895 }
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001896 type = builder.getFunctionType(argTypes, results);
Chris Lattner2e595eb2018-07-10 10:08:27 -07001897 return ParseSuccess;
1898}
1899
1900/// External function declarations.
1901///
1902/// ext-func ::= `extfunc` function-signature
1903///
1904ParseResult ModuleParser::parseExtFunc() {
1905 consumeToken(Token::kw_extfunc);
1906
1907 StringRef name;
1908 FunctionType *type = nullptr;
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001909 if (parseFunctionSignature(name, type, /*arguments*/ nullptr))
Chris Lattner2e595eb2018-07-10 10:08:27 -07001910 return ParseFailure;
1911
1912 // Okay, the external function definition was parsed correctly.
1913 getModule()->functionList.push_back(new ExtFunction(name, type));
1914 return ParseSuccess;
1915}
1916
1917/// CFG function declarations.
1918///
1919/// cfg-func ::= `cfgfunc` function-signature `{` basic-block+ `}`
1920///
1921ParseResult ModuleParser::parseCFGFunc() {
1922 consumeToken(Token::kw_cfgfunc);
1923
1924 StringRef name;
1925 FunctionType *type = nullptr;
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001926 if (parseFunctionSignature(name, type, /*arguments*/ nullptr))
Chris Lattner2e595eb2018-07-10 10:08:27 -07001927 return ParseFailure;
1928
1929 // Okay, the CFG function signature was parsed correctly, create the function.
1930 auto function = new CFGFunction(name, type);
1931
1932 return CFGFunctionParser(getState(), function).parseFunctionBody();
1933}
1934
1935/// ML function declarations.
1936///
1937/// ml-func ::= `mlfunc` ml-func-signature `{` ml-stmt* ml-return-stmt `}`
1938///
1939ParseResult ModuleParser::parseMLFunc() {
1940 consumeToken(Token::kw_mlfunc);
1941
1942 StringRef name;
1943 FunctionType *type = nullptr;
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001944 SmallVector<StringRef, 4> argNames;
Chris Lattner2e595eb2018-07-10 10:08:27 -07001945 // FIXME: Parse ML function signature (args + types)
1946 // by passing pointer to SmallVector<identifier> into parseFunctionSignature
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001947
1948 if (parseFunctionSignature(name, type, &argNames))
Chris Lattner2e595eb2018-07-10 10:08:27 -07001949 return ParseFailure;
1950
1951 // Okay, the ML function signature was parsed correctly, create the function.
1952 auto function = new MLFunction(name, type);
1953
1954 return MLFunctionParser(getState(), function).parseFunctionBody();
1955}
1956
Chris Lattnere79379a2018-06-22 10:39:19 -07001957/// This is the top-level module parser.
Chris Lattner2e595eb2018-07-10 10:08:27 -07001958ParseResult ModuleParser::parseModule() {
Chris Lattnere79379a2018-06-22 10:39:19 -07001959 while (1) {
Chris Lattner48af7d12018-07-09 19:05:38 -07001960 switch (getToken().getKind()) {
Chris Lattnere79379a2018-06-22 10:39:19 -07001961 default:
1962 emitError("expected a top level entity");
Chris Lattner2e595eb2018-07-10 10:08:27 -07001963 return ParseFailure;
Chris Lattnere79379a2018-06-22 10:39:19 -07001964
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001965 // If we got to the end of the file, then we're done.
Chris Lattnere79379a2018-06-22 10:39:19 -07001966 case Token::eof:
Chris Lattner2e595eb2018-07-10 10:08:27 -07001967 return ParseSuccess;
Chris Lattnere79379a2018-06-22 10:39:19 -07001968
1969 // If we got an error token, then the lexer already emitted an error, just
1970 // stop. Someday we could introduce error recovery if there was demand for
1971 // it.
1972 case Token::error:
Chris Lattner2e595eb2018-07-10 10:08:27 -07001973 return ParseFailure;
1974
1975 case Token::hash_identifier:
1976 if (parseAffineMapDef())
1977 return ParseFailure;
1978 break;
Chris Lattnere79379a2018-06-22 10:39:19 -07001979
1980 case Token::kw_extfunc:
Chris Lattner2e595eb2018-07-10 10:08:27 -07001981 if (parseExtFunc())
1982 return ParseFailure;
Chris Lattnere79379a2018-06-22 10:39:19 -07001983 break;
1984
Chris Lattner4c95a502018-06-23 16:03:42 -07001985 case Token::kw_cfgfunc:
Chris Lattner2e595eb2018-07-10 10:08:27 -07001986 if (parseCFGFunc())
1987 return ParseFailure;
MLIR Teamf85a6262018-06-27 11:03:08 -07001988 break;
Chris Lattner4c95a502018-06-23 16:03:42 -07001989
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -07001990 case Token::kw_mlfunc:
Chris Lattner2e595eb2018-07-10 10:08:27 -07001991 if (parseMLFunc())
1992 return ParseFailure;
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -07001993 break;
1994
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001995 // TODO: affine entity declarations, etc.
Chris Lattnere79379a2018-06-22 10:39:19 -07001996 }
1997 }
1998}
1999
2000//===----------------------------------------------------------------------===//
2001
Jacques Pienaar7b829702018-07-03 13:24:09 -07002002void mlir::defaultErrorReporter(const llvm::SMDiagnostic &error) {
2003 const auto &sourceMgr = *error.getSourceMgr();
2004 sourceMgr.PrintMessage(error.getLoc(), error.getKind(), error.getMessage());
2005}
2006
Chris Lattnere79379a2018-06-22 10:39:19 -07002007/// This parses the file specified by the indicated SourceMgr and returns an
2008/// MLIR module if it was valid. If not, it emits diagnostics and returns null.
Jacques Pienaar9c411be2018-06-24 19:17:35 -07002009Module *mlir::parseSourceFile(llvm::SourceMgr &sourceMgr, MLIRContext *context,
Jacques Pienaar7b829702018-07-03 13:24:09 -07002010 SMDiagnosticHandlerTy errorReporter) {
Chris Lattner2e595eb2018-07-10 10:08:27 -07002011 // This is the result module we are parsing into.
2012 std::unique_ptr<Module> module(new Module(context));
2013
2014 ParserState state(sourceMgr, module.get(),
Jacques Pienaar0bffd862018-07-11 13:26:23 -07002015 errorReporter ? errorReporter : defaultErrorReporter);
Chris Lattner2e595eb2018-07-10 10:08:27 -07002016 if (ModuleParser(state).parseModule())
2017 return nullptr;
Chris Lattner21e67f62018-07-06 10:46:19 -07002018
2019 // Make sure the parse module has no other structural problems detected by the
2020 // verifier.
Chris Lattner2e595eb2018-07-10 10:08:27 -07002021 module->verify();
2022 return module.release();
Chris Lattnere79379a2018-06-22 10:39:19 -07002023}