blob: 7b7ee89f28c6b0b194995b111b67846c39fce8ed [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()) +
1367 " type" + plural +
1368 " in operand list but had " +
1369 llvm::utostr(operandTypes.size()));
1370 }
1371
1372 // Resolve all of the operands.
1373 SmallVector<SSAValue *, 8> operands;
1374 for (unsigned i = 0, e = operandInfos.size(); i != e; ++i) {
1375 operands.push_back(resolveSSAUse(operandInfos[i], operandTypes[i]));
1376 if (!operands.back())
1377 return ParseFailure;
1378 }
1379
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001380 auto nameId = builder.getIdentifier(name);
Chris Lattner7f9cc272018-07-19 08:35:28 -07001381 auto op = createOpFunc(nameId, operands, fnType->getResults(), attributes);
1382 if (!op)
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001383 return ParseFailure;
1384
1385 // We just parsed an operation. If it is a recognized one, verify that it
1386 // is structurally as we expect. If not, produce an error with a reasonable
1387 // source location.
Chris Lattner7f9cc272018-07-19 08:35:28 -07001388 if (auto *opInfo = op->getAbstractOperation(builder.getContext())) {
1389 if (auto error = opInfo->verifyInvariants(op))
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001390 return emitError(loc, error);
1391 }
1392
Chris Lattner7f9cc272018-07-19 08:35:28 -07001393 // If the instruction had a name, register it.
1394 if (!resultID.empty()) {
1395 // FIXME: Add result infra to handle Stmt results as well to make this
1396 // generic.
1397 if (auto *inst = dyn_cast<OperationInst>(op)) {
1398 if (inst->getResults().empty())
1399 return emitError(loc, "cannot name an operation with no results");
1400
1401 // TODO: This should be getResult(0)
1402 addDefinition({resultID, loc}, &inst->getResults()[0]);
1403 }
1404 }
1405
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001406 return ParseSuccess;
1407}
Chris Lattnere79379a2018-06-22 10:39:19 -07001408
Chris Lattner48af7d12018-07-09 19:05:38 -07001409//===----------------------------------------------------------------------===//
1410// CFG Functions
1411//===----------------------------------------------------------------------===//
Chris Lattnere79379a2018-06-22 10:39:19 -07001412
Chris Lattner4c95a502018-06-23 16:03:42 -07001413namespace {
Chris Lattner48af7d12018-07-09 19:05:38 -07001414/// This is a specialized parser for CFGFunction's, maintaining the state
1415/// transient to their bodies.
Chris Lattner7f9cc272018-07-19 08:35:28 -07001416class CFGFunctionParser : public FunctionParser {
Chris Lattner158e0a3e2018-07-08 20:51:38 -07001417public:
Chris Lattner2e595eb2018-07-10 10:08:27 -07001418 CFGFunctionParser(ParserState &state, CFGFunction *function)
Chris Lattner7f9cc272018-07-19 08:35:28 -07001419 : FunctionParser(state), function(function), builder(function) {}
Chris Lattner2e595eb2018-07-10 10:08:27 -07001420
1421 ParseResult parseFunctionBody();
1422
1423private:
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001424 CFGFunction *function;
1425 llvm::StringMap<std::pair<BasicBlock*, SMLoc>> blocksByName;
Chris Lattner48af7d12018-07-09 19:05:38 -07001426
1427 /// This builder intentionally shadows the builder in the base class, with a
1428 /// more specific builder type.
Chris Lattner158e0a3e2018-07-08 20:51:38 -07001429 CFGFuncBuilder builder;
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001430
Chris Lattner4c95a502018-06-23 16:03:42 -07001431 /// Get the basic block with the specified name, creating it if it doesn't
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001432 /// already exist. The location specified is the point of use, which allows
1433 /// us to diagnose references to blocks that are not defined precisely.
1434 BasicBlock *getBlockNamed(StringRef name, SMLoc loc) {
1435 auto &blockAndLoc = blocksByName[name];
1436 if (!blockAndLoc.first) {
Chris Lattner3a467cc2018-07-01 20:28:00 -07001437 blockAndLoc.first = new BasicBlock();
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001438 blockAndLoc.second = loc;
Chris Lattner4c95a502018-06-23 16:03:42 -07001439 }
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001440 return blockAndLoc.first;
Chris Lattner4c95a502018-06-23 16:03:42 -07001441 }
Chris Lattner48af7d12018-07-09 19:05:38 -07001442
Chris Lattner48af7d12018-07-09 19:05:38 -07001443 ParseResult parseBasicBlock();
1444 OperationInst *parseCFGOperation();
1445 TerminatorInst *parseTerminator();
Chris Lattner4c95a502018-06-23 16:03:42 -07001446};
1447} // end anonymous namespace
1448
Chris Lattner48af7d12018-07-09 19:05:38 -07001449ParseResult CFGFunctionParser::parseFunctionBody() {
1450 if (!consumeIf(Token::l_brace))
1451 return emitError("expected '{' in CFG function");
1452
1453 // Make sure we have at least one block.
1454 if (getToken().is(Token::r_brace))
1455 return emitError("CFG functions must have at least one basic block");
Chris Lattner4c95a502018-06-23 16:03:42 -07001456
1457 // Parse the list of blocks.
1458 while (!consumeIf(Token::r_brace))
Chris Lattner48af7d12018-07-09 19:05:38 -07001459 if (parseBasicBlock())
Chris Lattner4c95a502018-06-23 16:03:42 -07001460 return ParseFailure;
1461
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001462 // Verify that all referenced blocks were defined. Iteration over a
1463 // StringMap isn't determinstic, but this is good enough for our purposes.
Chris Lattner48af7d12018-07-09 19:05:38 -07001464 for (auto &elt : blocksByName) {
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001465 auto *bb = elt.second.first;
Chris Lattner3a467cc2018-07-01 20:28:00 -07001466 if (!bb->getFunction())
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001467 return emitError(elt.second.second,
1468 "reference to an undefined basic block '" +
1469 elt.first() + "'");
1470 }
1471
Chris Lattner48af7d12018-07-09 19:05:38 -07001472 getModule()->functionList.push_back(function);
Chris Lattner4c95a502018-06-23 16:03:42 -07001473 return ParseSuccess;
1474}
1475
1476/// Basic block declaration.
1477///
1478/// basic-block ::= bb-label instruction* terminator-stmt
1479/// bb-label ::= bb-id bb-arg-list? `:`
1480/// bb-id ::= bare-id
1481/// bb-arg-list ::= `(` ssa-id-and-type-list? `)`
1482///
Chris Lattner48af7d12018-07-09 19:05:38 -07001483ParseResult CFGFunctionParser::parseBasicBlock() {
1484 SMLoc nameLoc = getToken().getLoc();
1485 auto name = getTokenSpelling();
Chris Lattner4c95a502018-06-23 16:03:42 -07001486 if (!consumeIf(Token::bare_identifier))
1487 return emitError("expected basic block name");
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001488
Chris Lattner48af7d12018-07-09 19:05:38 -07001489 auto *block = getBlockNamed(name, nameLoc);
Chris Lattner4c95a502018-06-23 16:03:42 -07001490
1491 // If this block has already been parsed, then this is a redefinition with the
1492 // same block name.
Chris Lattner3a467cc2018-07-01 20:28:00 -07001493 if (block->getFunction())
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001494 return emitError(nameLoc, "redefinition of block '" + name.str() + "'");
1495
Chris Lattner3a467cc2018-07-01 20:28:00 -07001496 // Add the block to the function.
Chris Lattner48af7d12018-07-09 19:05:38 -07001497 function->push_back(block);
Chris Lattner4c95a502018-06-23 16:03:42 -07001498
Chris Lattner78276e32018-07-07 15:48:26 -07001499 // If an argument list is present, parse it.
1500 if (consumeIf(Token::l_paren)) {
Chris Lattner7f9cc272018-07-19 08:35:28 -07001501 SmallVector<SSAValue *, 8> bbArgs;
1502 if (parseOptionalSSAUseAndTypeList(Token::r_paren, bbArgs))
Chris Lattner78276e32018-07-07 15:48:26 -07001503 return ParseFailure;
1504
1505 // TODO: attach it.
1506 }
Chris Lattner4c95a502018-06-23 16:03:42 -07001507
1508 if (!consumeIf(Token::colon))
1509 return emitError("expected ':' after basic block name");
1510
Chris Lattner158e0a3e2018-07-08 20:51:38 -07001511 // Set the insertion point to the block we want to insert new operations into.
Chris Lattner48af7d12018-07-09 19:05:38 -07001512 builder.setInsertionPoint(block);
Chris Lattner158e0a3e2018-07-08 20:51:38 -07001513
Chris Lattner7f9cc272018-07-19 08:35:28 -07001514 auto createOpFunc = [&](Identifier name, ArrayRef<SSAValue *> operands,
1515 ArrayRef<Type *> resultTypes,
1516 ArrayRef<NamedAttribute> attrs) -> Operation * {
1517 SmallVector<CFGValue *, 8> cfgOperands;
1518 cfgOperands.reserve(operands.size());
1519 for (auto *op : operands)
1520 cfgOperands.push_back(cast<CFGValue>(op));
1521 return builder.createOperation(name, cfgOperands, resultTypes, attrs);
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001522 };
1523
Chris Lattnered65a732018-06-28 20:45:33 -07001524 // Parse the list of operations that make up the body of the block.
Chris Lattner48af7d12018-07-09 19:05:38 -07001525 while (getToken().isNot(Token::kw_return, Token::kw_br)) {
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001526 if (parseOperation(createOpFunc))
Chris Lattnered65a732018-06-28 20:45:33 -07001527 return ParseFailure;
1528 }
Chris Lattner4c95a502018-06-23 16:03:42 -07001529
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001530 if (!parseTerminator())
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001531 return ParseFailure;
Chris Lattner4c95a502018-06-23 16:03:42 -07001532
1533 return ParseSuccess;
1534}
1535
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001536/// Parse the terminator instruction for a basic block.
1537///
1538/// terminator-stmt ::= `br` bb-id branch-use-list?
1539/// branch-use-list ::= `(` ssa-use-and-type-list? `)`
1540/// terminator-stmt ::=
1541/// `cond_br` ssa-use `,` bb-id branch-use-list? `,` bb-id branch-use-list?
1542/// terminator-stmt ::= `return` ssa-use-and-type-list?
1543///
Chris Lattner48af7d12018-07-09 19:05:38 -07001544TerminatorInst *CFGFunctionParser::parseTerminator() {
1545 switch (getToken().getKind()) {
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001546 default:
Chris Lattner3a467cc2018-07-01 20:28:00 -07001547 return (emitError("expected terminator at end of basic block"), nullptr);
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001548
1549 case Token::kw_return:
1550 consumeToken(Token::kw_return);
Chris Lattner48af7d12018-07-09 19:05:38 -07001551 return builder.createReturnInst();
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001552
1553 case Token::kw_br: {
1554 consumeToken(Token::kw_br);
Chris Lattner48af7d12018-07-09 19:05:38 -07001555 auto destBB = getBlockNamed(getTokenSpelling(), getToken().getLoc());
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001556 if (!consumeIf(Token::bare_identifier))
Chris Lattner3a467cc2018-07-01 20:28:00 -07001557 return (emitError("expected basic block name"), nullptr);
Chris Lattner48af7d12018-07-09 19:05:38 -07001558 return builder.createBranchInst(destBB);
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001559 }
Chris Lattner78276e32018-07-07 15:48:26 -07001560 // TODO: cond_br.
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001561 }
1562}
1563
Chris Lattner48af7d12018-07-09 19:05:38 -07001564//===----------------------------------------------------------------------===//
1565// ML Functions
1566//===----------------------------------------------------------------------===//
1567
1568namespace {
1569/// Refined parser for MLFunction bodies.
Chris Lattner7f9cc272018-07-19 08:35:28 -07001570class MLFunctionParser : public FunctionParser {
Chris Lattner48af7d12018-07-09 19:05:38 -07001571public:
Chris Lattner48af7d12018-07-09 19:05:38 -07001572 MLFunctionParser(ParserState &state, MLFunction *function)
Chris Lattner7f9cc272018-07-19 08:35:28 -07001573 : FunctionParser(state), function(function), builder(function) {}
Chris Lattner48af7d12018-07-09 19:05:38 -07001574
1575 ParseResult parseFunctionBody();
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -07001576
1577private:
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001578 MLFunction *function;
1579
1580 /// This builder intentionally shadows the builder in the base class, with a
1581 /// more specific builder type.
1582 MLFuncBuilder builder;
1583
1584 ParseResult parseForStmt();
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001585 AffineConstantExpr *parseIntConstant();
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001586 ParseResult parseIfStmt();
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -07001587 ParseResult parseElseClause(IfClause *elseClause);
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001588 ParseResult parseStatements(StmtBlock *block);
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -07001589 ParseResult parseStmtBlock(StmtBlock *block);
Chris Lattner48af7d12018-07-09 19:05:38 -07001590};
1591} // end anonymous namespace
1592
Chris Lattner48af7d12018-07-09 19:05:38 -07001593ParseResult MLFunctionParser::parseFunctionBody() {
1594 if (!consumeIf(Token::l_brace))
1595 return emitError("expected '{' in ML function");
1596
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001597 // Parse statements in this function
1598 if (parseStatements(function))
1599 return ParseFailure;
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -07001600
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001601 if (!consumeIf(Token::kw_return))
1602 emitError("ML function must end with return statement");
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001603
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001604 // TODO: store return operands in the IR.
1605 SmallVector<SSAUseInfo, 4> dummyUseInfo;
1606 if (parseOptionalSSAUseList(Token::r_brace, dummyUseInfo))
1607 return ParseFailure;
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -07001608
Chris Lattner48af7d12018-07-09 19:05:38 -07001609 getModule()->functionList.push_back(function);
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -07001610
1611 return ParseSuccess;
1612}
1613
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001614/// For statement.
1615///
Chris Lattner48af7d12018-07-09 19:05:38 -07001616/// ml-for-stmt ::= `for` ssa-id `=` lower-bound `to` upper-bound
1617/// (`step` integer-literal)? `{` ml-stmt* `}`
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001618///
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001619ParseResult MLFunctionParser::parseForStmt() {
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001620 consumeToken(Token::kw_for);
1621
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001622 // Parse induction variable
1623 if (getToken().isNot(Token::percent_identifier))
1624 return emitError("expected SSA identifier for the loop variable");
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001625
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001626 // TODO: create SSA value definition from name
1627 StringRef name = getTokenSpelling().drop_front();
1628 (void)name;
1629
1630 consumeToken(Token::percent_identifier);
1631
1632 if (!consumeIf(Token::equal))
1633 return emitError("expected =");
1634
1635 // Parse loop bounds
1636 AffineConstantExpr *lowerBound = parseIntConstant();
1637 if (!lowerBound)
1638 return ParseFailure;
1639
1640 if (!consumeIf(Token::kw_to))
1641 return emitError("expected 'to' between bounds");
1642
1643 AffineConstantExpr *upperBound = parseIntConstant();
1644 if (!upperBound)
1645 return ParseFailure;
1646
1647 // Parse step
1648 AffineConstantExpr *step = nullptr;
1649 if (consumeIf(Token::kw_step)) {
1650 step = parseIntConstant();
1651 if (!step)
1652 return ParseFailure;
1653 }
1654
1655 // Create for statement.
1656 ForStmt *stmt = builder.createFor(lowerBound, upperBound, step);
1657
1658 // If parsing of the for statement body fails,
1659 // MLIR contains for statement with those nested statements that have been
1660 // successfully parsed.
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001661 if (parseStmtBlock(static_cast<StmtBlock *>(stmt)))
1662 return ParseFailure;
1663
1664 return ParseSuccess;
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001665}
1666
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001667// This method is temporary workaround to parse simple loop bounds and
1668// step.
1669// TODO: remove this method once it's no longer used.
1670AffineConstantExpr *MLFunctionParser::parseIntConstant() {
1671 if (getToken().isNot(Token::integer))
1672 return (emitError("expected non-negative integer for now"), nullptr);
1673
1674 auto val = getToken().getUInt64IntegerValue();
1675 if (!val.hasValue() || (int64_t)val.getValue() < 0) {
1676 return (emitError("constant too large for affineint"), nullptr);
1677 }
1678 consumeToken(Token::integer);
1679 return builder.getConstantExpr((int64_t)val.getValue());
1680}
1681
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001682/// If statement.
1683///
Chris Lattner48af7d12018-07-09 19:05:38 -07001684/// ml-if-head ::= `if` ml-if-cond `{` ml-stmt* `}`
1685/// | ml-if-head `else` `if` ml-if-cond `{` ml-stmt* `}`
1686/// ml-if-stmt ::= ml-if-head
1687/// | ml-if-head `else` `{` ml-stmt* `}`
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001688///
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001689ParseResult MLFunctionParser::parseIfStmt() {
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001690 consumeToken(Token::kw_if);
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -07001691 if (!consumeIf(Token::l_paren))
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001692 return emitError("expected (");
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001693
1694 //TODO: parse condition
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -07001695
1696 if (!consumeIf(Token::r_paren))
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001697 return emitError("expected ')'");
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -07001698
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001699 IfStmt *ifStmt = builder.createIf();
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -07001700 IfClause *thenClause = ifStmt->getThenClause();
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001701
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001702 // When parsing of an if statement body fails, the IR contains
1703 // the if statement with the portion of the body that has been
1704 // successfully parsed.
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001705 if (parseStmtBlock(thenClause))
1706 return ParseFailure;
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001707
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -07001708 if (consumeIf(Token::kw_else)) {
1709 IfClause *elseClause = ifStmt->createElseClause();
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001710 if (parseElseClause(elseClause))
1711 return ParseFailure;
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001712 }
1713
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001714 return ParseSuccess;
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -07001715}
1716
1717ParseResult MLFunctionParser::parseElseClause(IfClause *elseClause) {
1718 if (getToken().is(Token::kw_if)) {
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001719 builder.setInsertionPoint(elseClause);
1720 return parseIfStmt();
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -07001721 }
1722
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001723 return parseStmtBlock(elseClause);
1724}
1725
1726///
1727/// Parse a list of statements ending with `return` or `}`
1728///
1729ParseResult MLFunctionParser::parseStatements(StmtBlock *block) {
Chris Lattner7f9cc272018-07-19 08:35:28 -07001730 auto createOpFunc = [&](Identifier name, ArrayRef<SSAValue *> operands,
1731 ArrayRef<Type *> resultTypes,
1732 ArrayRef<NamedAttribute> attrs) -> Operation * {
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001733 return builder.createOperation(name, attrs);
1734 };
1735
1736 builder.setInsertionPoint(block);
1737
1738 while (getToken().isNot(Token::kw_return, Token::r_brace)) {
1739 switch (getToken().getKind()) {
1740 default:
1741 if (parseOperation(createOpFunc))
1742 return ParseFailure;
1743 break;
1744 case Token::kw_for:
1745 if (parseForStmt())
1746 return ParseFailure;
1747 break;
1748 case Token::kw_if:
1749 if (parseIfStmt())
1750 return ParseFailure;
1751 break;
1752 } // end switch
1753 }
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -07001754
1755 return ParseSuccess;
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001756}
1757
1758///
1759/// Parse `{` ml-stmt* `}`
1760///
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -07001761ParseResult MLFunctionParser::parseStmtBlock(StmtBlock *block) {
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001762 if (!consumeIf(Token::l_brace))
1763 return emitError("expected '{' before statement list");
1764
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001765 if (parseStatements(block))
1766 return ParseFailure;
1767
1768 if (!consumeIf(Token::r_brace))
1769 return emitError("expected '}' at the end of the statement block");
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001770
1771 return ParseSuccess;
1772}
1773
Chris Lattner4c95a502018-06-23 16:03:42 -07001774//===----------------------------------------------------------------------===//
1775// Top-level entity parsing.
1776//===----------------------------------------------------------------------===//
1777
Chris Lattner2e595eb2018-07-10 10:08:27 -07001778namespace {
1779/// This parser handles entities that are only valid at the top level of the
1780/// file.
1781class ModuleParser : public Parser {
1782public:
1783 explicit ModuleParser(ParserState &state) : Parser(state) {}
1784
1785 ParseResult parseModule();
1786
1787private:
1788 ParseResult parseAffineMapDef();
1789
1790 // Functions.
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001791 ParseResult parseMLArgumentList(SmallVectorImpl<Type *> &argTypes,
1792 SmallVectorImpl<StringRef> &argNames);
1793 ParseResult parseFunctionSignature(StringRef &name, FunctionType *&type,
1794 SmallVectorImpl<StringRef> *argNames);
Chris Lattner2e595eb2018-07-10 10:08:27 -07001795 ParseResult parseExtFunc();
1796 ParseResult parseCFGFunc();
1797 ParseResult parseMLFunc();
1798};
1799} // end anonymous namespace
1800
1801/// Affine map declaration.
1802///
1803/// affine-map-def ::= affine-map-id `=` affine-map-inline
1804///
1805ParseResult ModuleParser::parseAffineMapDef() {
1806 assert(getToken().is(Token::hash_identifier));
1807
1808 StringRef affineMapId = getTokenSpelling().drop_front();
1809
1810 // Check for redefinitions.
1811 auto *&entry = getState().affineMapDefinitions[affineMapId];
1812 if (entry)
1813 return emitError("redefinition of affine map id '" + affineMapId + "'");
1814
1815 consumeToken(Token::hash_identifier);
1816
1817 // Parse the '='
1818 if (!consumeIf(Token::equal))
1819 return emitError("expected '=' in affine map outlined definition");
1820
1821 entry = parseAffineMapInline();
1822 if (!entry)
1823 return ParseFailure;
1824
Chris Lattner2e595eb2018-07-10 10:08:27 -07001825 return ParseSuccess;
1826}
1827
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001828/// Parse a (possibly empty) list of MLFunction arguments with types.
1829///
1830/// ml-argument ::= ssa-id `:` type
1831/// ml-argument-list ::= ml-argument (`,` ml-argument)* | /*empty*/
1832///
1833ParseResult
1834ModuleParser::parseMLArgumentList(SmallVectorImpl<Type *> &argTypes,
1835 SmallVectorImpl<StringRef> &argNames) {
1836 auto parseElt = [&]() -> ParseResult {
1837 // Parse argument name
1838 if (getToken().isNot(Token::percent_identifier))
1839 return emitError("expected SSA identifier");
1840
1841 StringRef name = getTokenSpelling().drop_front();
1842 consumeToken(Token::percent_identifier);
1843 argNames.push_back(name);
1844
1845 if (!consumeIf(Token::colon))
1846 return emitError("expected ':'");
1847
1848 // Parse argument type
1849 auto elt = parseType();
1850 if (!elt)
1851 return ParseFailure;
1852 argTypes.push_back(elt);
1853
1854 return ParseSuccess;
1855 };
1856
1857 if (!consumeIf(Token::l_paren))
1858 llvm_unreachable("expected '('");
1859
1860 return parseCommaSeparatedList(Token::r_paren, parseElt);
1861}
1862
Chris Lattner2e595eb2018-07-10 10:08:27 -07001863/// Parse a function signature, starting with a name and including the parameter
1864/// list.
1865///
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001866/// argument-list ::= type (`,` type)* | /*empty*/ | ml-argument-list
Chris Lattner2e595eb2018-07-10 10:08:27 -07001867/// function-signature ::= function-id `(` argument-list `)` (`->` type-list)?
1868///
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001869ParseResult
1870ModuleParser::parseFunctionSignature(StringRef &name, FunctionType *&type,
1871 SmallVectorImpl<StringRef> *argNames) {
Chris Lattner2e595eb2018-07-10 10:08:27 -07001872 if (getToken().isNot(Token::at_identifier))
1873 return emitError("expected a function identifier like '@foo'");
1874
1875 name = getTokenSpelling().drop_front();
1876 consumeToken(Token::at_identifier);
1877
1878 if (getToken().isNot(Token::l_paren))
1879 return emitError("expected '(' in function signature");
1880
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001881 SmallVector<Type *, 4> argTypes;
1882 ParseResult parseResult;
1883
1884 if (argNames)
1885 parseResult = parseMLArgumentList(argTypes, *argNames);
1886 else
1887 parseResult = parseTypeList(argTypes);
1888
1889 if (parseResult)
Chris Lattner2e595eb2018-07-10 10:08:27 -07001890 return ParseFailure;
1891
1892 // Parse the return type if present.
1893 SmallVector<Type *, 4> results;
1894 if (consumeIf(Token::arrow)) {
1895 if (parseTypeList(results))
1896 return ParseFailure;
1897 }
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001898 type = builder.getFunctionType(argTypes, results);
Chris Lattner2e595eb2018-07-10 10:08:27 -07001899 return ParseSuccess;
1900}
1901
1902/// External function declarations.
1903///
1904/// ext-func ::= `extfunc` function-signature
1905///
1906ParseResult ModuleParser::parseExtFunc() {
1907 consumeToken(Token::kw_extfunc);
1908
1909 StringRef name;
1910 FunctionType *type = nullptr;
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001911 if (parseFunctionSignature(name, type, /*arguments*/ nullptr))
Chris Lattner2e595eb2018-07-10 10:08:27 -07001912 return ParseFailure;
1913
1914 // Okay, the external function definition was parsed correctly.
1915 getModule()->functionList.push_back(new ExtFunction(name, type));
1916 return ParseSuccess;
1917}
1918
1919/// CFG function declarations.
1920///
1921/// cfg-func ::= `cfgfunc` function-signature `{` basic-block+ `}`
1922///
1923ParseResult ModuleParser::parseCFGFunc() {
1924 consumeToken(Token::kw_cfgfunc);
1925
1926 StringRef name;
1927 FunctionType *type = nullptr;
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001928 if (parseFunctionSignature(name, type, /*arguments*/ nullptr))
Chris Lattner2e595eb2018-07-10 10:08:27 -07001929 return ParseFailure;
1930
1931 // Okay, the CFG function signature was parsed correctly, create the function.
1932 auto function = new CFGFunction(name, type);
1933
1934 return CFGFunctionParser(getState(), function).parseFunctionBody();
1935}
1936
1937/// ML function declarations.
1938///
1939/// ml-func ::= `mlfunc` ml-func-signature `{` ml-stmt* ml-return-stmt `}`
1940///
1941ParseResult ModuleParser::parseMLFunc() {
1942 consumeToken(Token::kw_mlfunc);
1943
1944 StringRef name;
1945 FunctionType *type = nullptr;
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001946 SmallVector<StringRef, 4> argNames;
Chris Lattner2e595eb2018-07-10 10:08:27 -07001947 // FIXME: Parse ML function signature (args + types)
1948 // by passing pointer to SmallVector<identifier> into parseFunctionSignature
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001949
1950 if (parseFunctionSignature(name, type, &argNames))
Chris Lattner2e595eb2018-07-10 10:08:27 -07001951 return ParseFailure;
1952
1953 // Okay, the ML function signature was parsed correctly, create the function.
1954 auto function = new MLFunction(name, type);
1955
1956 return MLFunctionParser(getState(), function).parseFunctionBody();
1957}
1958
Chris Lattnere79379a2018-06-22 10:39:19 -07001959/// This is the top-level module parser.
Chris Lattner2e595eb2018-07-10 10:08:27 -07001960ParseResult ModuleParser::parseModule() {
Chris Lattnere79379a2018-06-22 10:39:19 -07001961 while (1) {
Chris Lattner48af7d12018-07-09 19:05:38 -07001962 switch (getToken().getKind()) {
Chris Lattnere79379a2018-06-22 10:39:19 -07001963 default:
1964 emitError("expected a top level entity");
Chris Lattner2e595eb2018-07-10 10:08:27 -07001965 return ParseFailure;
Chris Lattnere79379a2018-06-22 10:39:19 -07001966
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001967 // If we got to the end of the file, then we're done.
Chris Lattnere79379a2018-06-22 10:39:19 -07001968 case Token::eof:
Chris Lattner2e595eb2018-07-10 10:08:27 -07001969 return ParseSuccess;
Chris Lattnere79379a2018-06-22 10:39:19 -07001970
1971 // If we got an error token, then the lexer already emitted an error, just
1972 // stop. Someday we could introduce error recovery if there was demand for
1973 // it.
1974 case Token::error:
Chris Lattner2e595eb2018-07-10 10:08:27 -07001975 return ParseFailure;
1976
1977 case Token::hash_identifier:
1978 if (parseAffineMapDef())
1979 return ParseFailure;
1980 break;
Chris Lattnere79379a2018-06-22 10:39:19 -07001981
1982 case Token::kw_extfunc:
Chris Lattner2e595eb2018-07-10 10:08:27 -07001983 if (parseExtFunc())
1984 return ParseFailure;
Chris Lattnere79379a2018-06-22 10:39:19 -07001985 break;
1986
Chris Lattner4c95a502018-06-23 16:03:42 -07001987 case Token::kw_cfgfunc:
Chris Lattner2e595eb2018-07-10 10:08:27 -07001988 if (parseCFGFunc())
1989 return ParseFailure;
MLIR Teamf85a6262018-06-27 11:03:08 -07001990 break;
Chris Lattner4c95a502018-06-23 16:03:42 -07001991
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -07001992 case Token::kw_mlfunc:
Chris Lattner2e595eb2018-07-10 10:08:27 -07001993 if (parseMLFunc())
1994 return ParseFailure;
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -07001995 break;
1996
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001997 // TODO: affine entity declarations, etc.
Chris Lattnere79379a2018-06-22 10:39:19 -07001998 }
1999 }
2000}
2001
2002//===----------------------------------------------------------------------===//
2003
Jacques Pienaar7b829702018-07-03 13:24:09 -07002004void mlir::defaultErrorReporter(const llvm::SMDiagnostic &error) {
2005 const auto &sourceMgr = *error.getSourceMgr();
2006 sourceMgr.PrintMessage(error.getLoc(), error.getKind(), error.getMessage());
2007}
2008
Chris Lattnere79379a2018-06-22 10:39:19 -07002009/// This parses the file specified by the indicated SourceMgr and returns an
2010/// MLIR module if it was valid. If not, it emits diagnostics and returns null.
Jacques Pienaar9c411be2018-06-24 19:17:35 -07002011Module *mlir::parseSourceFile(llvm::SourceMgr &sourceMgr, MLIRContext *context,
Jacques Pienaar7b829702018-07-03 13:24:09 -07002012 SMDiagnosticHandlerTy errorReporter) {
Chris Lattner2e595eb2018-07-10 10:08:27 -07002013 // This is the result module we are parsing into.
2014 std::unique_ptr<Module> module(new Module(context));
2015
2016 ParserState state(sourceMgr, module.get(),
Jacques Pienaar0bffd862018-07-11 13:26:23 -07002017 errorReporter ? errorReporter : defaultErrorReporter);
Chris Lattner2e595eb2018-07-10 10:08:27 -07002018 if (ModuleParser(state).parseModule())
2019 return nullptr;
Chris Lattner21e67f62018-07-06 10:46:19 -07002020
2021 // Make sure the parse module has no other structural problems detected by the
2022 // verifier.
Chris Lattner2e595eb2018-07-10 10:08:27 -07002023 module->verify();
2024 return module.release();
Chris Lattnere79379a2018-06-22 10:39:19 -07002025}