blob: 678b5fc2bbfb07d18ee50ac662996398a4cd3896 [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 Lattner6119d382018-07-20 18:41:34 -070033#include "llvm/ADT/DenseMap.h"
Chris Lattnere79379a2018-06-22 10:39:19 -070034#include "llvm/Support/SourceMgr.h"
35using namespace mlir;
36using llvm::SourceMgr;
Chris Lattner4c95a502018-06-23 16:03:42 -070037using llvm::SMLoc;
Chris Lattnere79379a2018-06-22 10:39:19 -070038
Chris Lattnerf7e22732018-06-22 22:03:48 -070039/// Simple enum to make code read better in cases that would otherwise return a
40/// bool value. Failure is "true" in a boolean context.
Chris Lattnere79379a2018-06-22 10:39:19 -070041enum ParseResult {
42 ParseSuccess,
43 ParseFailure
44};
45
Chris Lattner48af7d12018-07-09 19:05:38 -070046namespace {
47class Parser;
48
49/// This class refers to all of the state maintained globally by the parser,
50/// such as the current lexer position etc. The Parser base class provides
51/// methods to access this.
52class ParserState {
Chris Lattnered65a732018-06-28 20:45:33 -070053public:
Chris Lattner2e595eb2018-07-10 10:08:27 -070054 ParserState(llvm::SourceMgr &sourceMgr, Module *module,
Chris Lattner48af7d12018-07-09 19:05:38 -070055 SMDiagnosticHandlerTy errorReporter)
Chris Lattner2e595eb2018-07-10 10:08:27 -070056 : context(module->getContext()), module(module),
57 lex(sourceMgr, errorReporter), curToken(lex.lexToken()),
Jacques Pienaard4c784e2018-07-11 00:07:36 -070058 errorReporter(errorReporter) {}
Chris Lattner2e595eb2018-07-10 10:08:27 -070059
60 // A map from affine map identifier to AffineMap.
61 llvm::StringMap<AffineMap *> affineMapDefinitions;
Chris Lattnere79379a2018-06-22 10:39:19 -070062
Chris Lattnere79379a2018-06-22 10:39:19 -070063private:
Chris Lattner48af7d12018-07-09 19:05:38 -070064 ParserState(const ParserState &) = delete;
65 void operator=(const ParserState &) = delete;
66
67 friend class Parser;
68
69 // The context we're parsing into.
Chris Lattner2e595eb2018-07-10 10:08:27 -070070 MLIRContext *const context;
71
72 // This is the module we are parsing into.
73 Module *const module;
Chris Lattnerf7e22732018-06-22 22:03:48 -070074
75 // The lexer for the source file we're parsing.
Chris Lattnere79379a2018-06-22 10:39:19 -070076 Lexer lex;
77
78 // This is the next token that hasn't been consumed yet.
79 Token curToken;
80
Jacques Pienaar9c411be2018-06-24 19:17:35 -070081 // The diagnostic error reporter.
Chris Lattner2e595eb2018-07-10 10:08:27 -070082 SMDiagnosticHandlerTy const errorReporter;
Chris Lattner48af7d12018-07-09 19:05:38 -070083};
84} // end anonymous namespace
MLIR Teamf85a6262018-06-27 11:03:08 -070085
Chris Lattner48af7d12018-07-09 19:05:38 -070086namespace {
87
Chris Lattner7f9cc272018-07-19 08:35:28 -070088typedef std::function<Operation *(Identifier, ArrayRef<SSAValue *>,
89 ArrayRef<Type *>, ArrayRef<NamedAttribute>)>
Tatiana Shpeisman565b9642018-07-16 11:47:09 -070090 CreateOperationFunction;
91
Chris Lattner48af7d12018-07-09 19:05:38 -070092/// This class implement support for parsing global entities like types and
93/// shared entities like SSA names. It is intended to be subclassed by
94/// specialized subparsers that include state, e.g. when a local symbol table.
95class Parser {
96public:
Chris Lattner2e595eb2018-07-10 10:08:27 -070097 Builder builder;
Chris Lattner48af7d12018-07-09 19:05:38 -070098
Chris Lattner2e595eb2018-07-10 10:08:27 -070099 Parser(ParserState &state) : builder(state.context), state(state) {}
100
101 // Helper methods to get stuff from the parser-global state.
102 ParserState &getState() const { return state; }
Chris Lattner48af7d12018-07-09 19:05:38 -0700103 MLIRContext *getContext() const { return state.context; }
Chris Lattner2e595eb2018-07-10 10:08:27 -0700104 Module *getModule() { return state.module; }
Chris Lattner48af7d12018-07-09 19:05:38 -0700105
106 /// Return the current token the parser is inspecting.
107 const Token &getToken() const { return state.curToken; }
108 StringRef getTokenSpelling() const { return state.curToken.getSpelling(); }
Chris Lattnere79379a2018-06-22 10:39:19 -0700109
110 /// Emit an error and return failure.
Chris Lattner4c95a502018-06-23 16:03:42 -0700111 ParseResult emitError(const Twine &message) {
Chris Lattner48af7d12018-07-09 19:05:38 -0700112 return emitError(state.curToken.getLoc(), message);
Chris Lattner4c95a502018-06-23 16:03:42 -0700113 }
114 ParseResult emitError(SMLoc loc, const Twine &message);
Chris Lattnere79379a2018-06-22 10:39:19 -0700115
116 /// Advance the current lexer onto the next token.
117 void consumeToken() {
Chris Lattner48af7d12018-07-09 19:05:38 -0700118 assert(state.curToken.isNot(Token::eof, Token::error) &&
Chris Lattnere79379a2018-06-22 10:39:19 -0700119 "shouldn't advance past EOF or errors");
Chris Lattner48af7d12018-07-09 19:05:38 -0700120 state.curToken = state.lex.lexToken();
Chris Lattnere79379a2018-06-22 10:39:19 -0700121 }
122
123 /// Advance the current lexer onto the next token, asserting what the expected
124 /// current token is. This is preferred to the above method because it leads
125 /// to more self-documenting code with better checking.
Chris Lattner8da0c282018-06-29 11:15:56 -0700126 void consumeToken(Token::Kind kind) {
Chris Lattner48af7d12018-07-09 19:05:38 -0700127 assert(state.curToken.is(kind) && "consumed an unexpected token");
Chris Lattnere79379a2018-06-22 10:39:19 -0700128 consumeToken();
129 }
130
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700131 /// If the current token has the specified kind, consume it and return true.
132 /// If not, return false.
Chris Lattner8da0c282018-06-29 11:15:56 -0700133 bool consumeIf(Token::Kind kind) {
Chris Lattner48af7d12018-07-09 19:05:38 -0700134 if (state.curToken.isNot(kind))
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700135 return false;
136 consumeToken(kind);
137 return true;
138 }
139
Chris Lattner40746442018-07-21 14:32:09 -0700140 /// Parse a comma-separated list of elements up until the specified end token.
141 ParseResult
142 parseCommaSeparatedListUntil(Token::Kind rightToken,
143 const std::function<ParseResult()> &parseElement,
144 bool allowEmptyList = true);
145
146 /// Parse a comma separated list of elements that must have at least one entry
147 /// in it.
148 ParseResult
149 parseCommaSeparatedList(const std::function<ParseResult()> &parseElement);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700150
Chris Lattnerf7e22732018-06-22 22:03:48 -0700151 // We have two forms of parsing methods - those that return a non-null
152 // pointer on success, and those that return a ParseResult to indicate whether
153 // they returned a failure. The second class fills in by-reference arguments
154 // as the results of their action.
155
Chris Lattnere79379a2018-06-22 10:39:19 -0700156 // Type parsing.
Chris Lattnerf958bbe2018-06-29 22:08:05 -0700157 Type *parsePrimitiveType();
Chris Lattnerf7e22732018-06-22 22:03:48 -0700158 Type *parseElementType();
159 VectorType *parseVectorType();
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700160 ParseResult parseDimensionListRanked(SmallVectorImpl<int> &dimensions);
Chris Lattnerf7e22732018-06-22 22:03:48 -0700161 Type *parseTensorType();
162 Type *parseMemRefType();
163 Type *parseFunctionType();
164 Type *parseType();
Chris Lattner1604e472018-07-23 08:42:19 -0700165 ParseResult parseTypeListNoParens(SmallVectorImpl<Type *> &elements);
Chris Lattnerf7e22732018-06-22 22:03:48 -0700166 ParseResult parseTypeList(SmallVectorImpl<Type*> &elements);
Chris Lattnere79379a2018-06-22 10:39:19 -0700167
Chris Lattner7121b802018-07-04 20:45:39 -0700168 // Attribute parsing.
169 Attribute *parseAttribute();
170 ParseResult parseAttributeDict(SmallVectorImpl<NamedAttribute> &attributes);
171
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700172 // Polyhedral structures.
Chris Lattner2e595eb2018-07-10 10:08:27 -0700173 AffineMap *parseAffineMapInline();
MLIR Team718c82f2018-07-16 09:45:22 -0700174 AffineMap *parseAffineMapReference();
MLIR Teamf85a6262018-06-27 11:03:08 -0700175
Chris Lattner48af7d12018-07-09 19:05:38 -0700176private:
177 // The Parser is subclassed and reinstantiated. Do not add additional
178 // non-trivial state here, add it to the ParserState class.
179 ParserState &state;
Chris Lattnere79379a2018-06-22 10:39:19 -0700180};
181} // end anonymous namespace
182
183//===----------------------------------------------------------------------===//
184// Helper methods.
185//===----------------------------------------------------------------------===//
186
Chris Lattner4c95a502018-06-23 16:03:42 -0700187ParseResult Parser::emitError(SMLoc loc, const Twine &message) {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700188 // If we hit a parse error in response to a lexer error, then the lexer
Jacques Pienaar9c411be2018-06-24 19:17:35 -0700189 // already reported the error.
Chris Lattner48af7d12018-07-09 19:05:38 -0700190 if (getToken().is(Token::error))
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700191 return ParseFailure;
192
Chris Lattner48af7d12018-07-09 19:05:38 -0700193 auto &sourceMgr = state.lex.getSourceMgr();
194 state.errorReporter(sourceMgr.GetMessage(loc, SourceMgr::DK_Error, message));
Chris Lattnere79379a2018-06-22 10:39:19 -0700195 return ParseFailure;
196}
197
Chris Lattner40746442018-07-21 14:32:09 -0700198/// Parse a comma separated list of elements that must have at least one entry
199/// in it.
200ParseResult Parser::parseCommaSeparatedList(
201 const std::function<ParseResult()> &parseElement) {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700202 // Non-empty case starts with an element.
203 if (parseElement())
204 return ParseFailure;
205
206 // Otherwise we have a list of comma separated elements.
207 while (consumeIf(Token::comma)) {
208 if (parseElement())
209 return ParseFailure;
210 }
Chris Lattner40746442018-07-21 14:32:09 -0700211 return ParseSuccess;
212}
213
214/// Parse a comma-separated list of elements, terminated with an arbitrary
215/// token. This allows empty lists if allowEmptyList is true.
216///
217/// abstract-list ::= rightToken // if allowEmptyList == true
218/// abstract-list ::= element (',' element)* rightToken
219///
220ParseResult Parser::parseCommaSeparatedListUntil(
221 Token::Kind rightToken, const std::function<ParseResult()> &parseElement,
222 bool allowEmptyList) {
223 // Handle the empty case.
224 if (getToken().is(rightToken)) {
225 if (!allowEmptyList)
226 return emitError("expected list element");
227 consumeToken(rightToken);
228 return ParseSuccess;
229 }
230
231 if (parseCommaSeparatedList(parseElement))
232 return ParseFailure;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700233
234 // Consume the end character.
235 if (!consumeIf(rightToken))
Chris Lattner8da0c282018-06-29 11:15:56 -0700236 return emitError("expected ',' or '" + Token::getTokenSpelling(rightToken) +
237 "'");
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700238
239 return ParseSuccess;
240}
Chris Lattnere79379a2018-06-22 10:39:19 -0700241
242//===----------------------------------------------------------------------===//
243// Type Parsing
244//===----------------------------------------------------------------------===//
245
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700246/// Parse the low-level fixed dtypes in the system.
247///
Chris Lattnerf958bbe2018-06-29 22:08:05 -0700248/// primitive-type ::= `f16` | `bf16` | `f32` | `f64`
249/// primitive-type ::= integer-type
250/// primitive-type ::= `affineint`
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700251///
Chris Lattnerf958bbe2018-06-29 22:08:05 -0700252Type *Parser::parsePrimitiveType() {
Chris Lattner48af7d12018-07-09 19:05:38 -0700253 switch (getToken().getKind()) {
Chris Lattnerf7e22732018-06-22 22:03:48 -0700254 default:
255 return (emitError("expected type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700256 case Token::kw_bf16:
257 consumeToken(Token::kw_bf16);
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700258 return builder.getBF16Type();
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700259 case Token::kw_f16:
260 consumeToken(Token::kw_f16);
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700261 return builder.getF16Type();
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700262 case Token::kw_f32:
263 consumeToken(Token::kw_f32);
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700264 return builder.getF32Type();
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700265 case Token::kw_f64:
266 consumeToken(Token::kw_f64);
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700267 return builder.getF64Type();
Chris Lattnerf958bbe2018-06-29 22:08:05 -0700268 case Token::kw_affineint:
269 consumeToken(Token::kw_affineint);
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700270 return builder.getAffineIntType();
Chris Lattnerf958bbe2018-06-29 22:08:05 -0700271 case Token::inttype: {
Chris Lattner48af7d12018-07-09 19:05:38 -0700272 auto width = getToken().getIntTypeBitwidth();
Chris Lattnerf958bbe2018-06-29 22:08:05 -0700273 if (!width.hasValue())
274 return (emitError("invalid integer width"), nullptr);
275 consumeToken(Token::inttype);
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700276 return builder.getIntegerType(width.getValue());
Chris Lattnerf958bbe2018-06-29 22:08:05 -0700277 }
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700278 }
279}
280
281/// Parse the element type of a tensor or memref type.
282///
283/// element-type ::= primitive-type | vector-type
284///
Chris Lattnerf7e22732018-06-22 22:03:48 -0700285Type *Parser::parseElementType() {
Chris Lattner48af7d12018-07-09 19:05:38 -0700286 if (getToken().is(Token::kw_vector))
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700287 return parseVectorType();
288
289 return parsePrimitiveType();
290}
291
292/// Parse a vector type.
293///
294/// vector-type ::= `vector` `<` const-dimension-list primitive-type `>`
295/// const-dimension-list ::= (integer-literal `x`)+
296///
Chris Lattnerf7e22732018-06-22 22:03:48 -0700297VectorType *Parser::parseVectorType() {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700298 consumeToken(Token::kw_vector);
299
300 if (!consumeIf(Token::less))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700301 return (emitError("expected '<' in vector type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700302
Chris Lattner48af7d12018-07-09 19:05:38 -0700303 if (getToken().isNot(Token::integer))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700304 return (emitError("expected dimension size in vector type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700305
306 SmallVector<unsigned, 4> dimensions;
Chris Lattner48af7d12018-07-09 19:05:38 -0700307 while (getToken().is(Token::integer)) {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700308 // Make sure this integer value is in bound and valid.
Chris Lattner48af7d12018-07-09 19:05:38 -0700309 auto dimension = getToken().getUnsignedIntegerValue();
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700310 if (!dimension.hasValue())
Chris Lattnerf7e22732018-06-22 22:03:48 -0700311 return (emitError("invalid dimension in vector type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700312 dimensions.push_back(dimension.getValue());
313
314 consumeToken(Token::integer);
315
316 // Make sure we have an 'x' or something like 'xbf32'.
Chris Lattner48af7d12018-07-09 19:05:38 -0700317 if (getToken().isNot(Token::bare_identifier) ||
318 getTokenSpelling()[0] != 'x')
Chris Lattnerf7e22732018-06-22 22:03:48 -0700319 return (emitError("expected 'x' in vector dimension list"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700320
321 // If we had a prefix of 'x', lex the next token immediately after the 'x'.
Chris Lattner48af7d12018-07-09 19:05:38 -0700322 if (getTokenSpelling().size() != 1)
323 state.lex.resetPointer(getTokenSpelling().data() + 1);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700324
325 // Consume the 'x'.
326 consumeToken(Token::bare_identifier);
327 }
328
329 // Parse the element type.
Chris Lattnerf7e22732018-06-22 22:03:48 -0700330 auto *elementType = parsePrimitiveType();
331 if (!elementType)
332 return nullptr;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700333
334 if (!consumeIf(Token::greater))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700335 return (emitError("expected '>' in vector type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700336
Chris Lattnerf7e22732018-06-22 22:03:48 -0700337 return VectorType::get(dimensions, elementType);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700338}
339
340/// Parse a dimension list of a tensor or memref type. This populates the
341/// dimension list, returning -1 for the '?' dimensions.
342///
343/// dimension-list-ranked ::= (dimension `x`)*
344/// dimension ::= `?` | integer-literal
345///
346ParseResult Parser::parseDimensionListRanked(SmallVectorImpl<int> &dimensions) {
Chris Lattner48af7d12018-07-09 19:05:38 -0700347 while (getToken().isAny(Token::integer, Token::question)) {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700348 if (consumeIf(Token::question)) {
349 dimensions.push_back(-1);
350 } else {
351 // Make sure this integer value is in bound and valid.
Chris Lattner48af7d12018-07-09 19:05:38 -0700352 auto dimension = getToken().getUnsignedIntegerValue();
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700353 if (!dimension.hasValue() || (int)dimension.getValue() < 0)
354 return emitError("invalid dimension");
355 dimensions.push_back((int)dimension.getValue());
356 consumeToken(Token::integer);
357 }
358
359 // Make sure we have an 'x' or something like 'xbf32'.
Chris Lattner48af7d12018-07-09 19:05:38 -0700360 if (getToken().isNot(Token::bare_identifier) ||
361 getTokenSpelling()[0] != 'x')
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700362 return emitError("expected 'x' in dimension list");
363
364 // If we had a prefix of 'x', lex the next token immediately after the 'x'.
Chris Lattner48af7d12018-07-09 19:05:38 -0700365 if (getTokenSpelling().size() != 1)
366 state.lex.resetPointer(getTokenSpelling().data() + 1);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700367
368 // Consume the 'x'.
369 consumeToken(Token::bare_identifier);
370 }
371
372 return ParseSuccess;
373}
374
375/// Parse a tensor type.
376///
377/// tensor-type ::= `tensor` `<` dimension-list element-type `>`
378/// dimension-list ::= dimension-list-ranked | `??`
379///
Chris Lattnerf7e22732018-06-22 22:03:48 -0700380Type *Parser::parseTensorType() {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700381 consumeToken(Token::kw_tensor);
382
383 if (!consumeIf(Token::less))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700384 return (emitError("expected '<' in tensor type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700385
386 bool isUnranked;
387 SmallVector<int, 4> dimensions;
388
389 if (consumeIf(Token::questionquestion)) {
390 isUnranked = true;
391 } else {
392 isUnranked = false;
393 if (parseDimensionListRanked(dimensions))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700394 return nullptr;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700395 }
396
397 // Parse the element type.
Chris Lattnerf7e22732018-06-22 22:03:48 -0700398 auto elementType = parseElementType();
399 if (!elementType)
400 return nullptr;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700401
402 if (!consumeIf(Token::greater))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700403 return (emitError("expected '>' in tensor type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700404
MLIR Team355ec862018-06-23 18:09:09 -0700405 if (isUnranked)
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700406 return builder.getTensorType(elementType);
407 return builder.getTensorType(dimensions, elementType);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700408}
409
410/// Parse a memref type.
411///
412/// memref-type ::= `memref` `<` dimension-list-ranked element-type
413/// (`,` semi-affine-map-composition)? (`,` memory-space)? `>`
414///
415/// semi-affine-map-composition ::= (semi-affine-map `,` )* semi-affine-map
416/// memory-space ::= integer-literal /* | TODO: address-space-id */
417///
Chris Lattnerf7e22732018-06-22 22:03:48 -0700418Type *Parser::parseMemRefType() {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700419 consumeToken(Token::kw_memref);
420
421 if (!consumeIf(Token::less))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700422 return (emitError("expected '<' in memref type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700423
424 SmallVector<int, 4> dimensions;
425 if (parseDimensionListRanked(dimensions))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700426 return nullptr;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700427
428 // Parse the element type.
Chris Lattnerf7e22732018-06-22 22:03:48 -0700429 auto elementType = parseElementType();
430 if (!elementType)
431 return nullptr;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700432
MLIR Team718c82f2018-07-16 09:45:22 -0700433 if (!consumeIf(Token::comma))
434 return (emitError("expected ',' in memref type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700435
MLIR Team718c82f2018-07-16 09:45:22 -0700436 // Parse semi-affine-map-composition.
437 SmallVector<AffineMap*, 2> affineMapComposition;
438 unsigned memorySpace;
439 bool parsedMemorySpace = false;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700440
MLIR Team718c82f2018-07-16 09:45:22 -0700441 auto parseElt = [&]() -> ParseResult {
442 if (getToken().is(Token::integer)) {
443 // Parse memory space.
444 if (parsedMemorySpace)
445 return emitError("multiple memory spaces specified in memref type");
446 auto v = getToken().getUnsignedIntegerValue();
447 if (!v.hasValue())
448 return emitError("invalid memory space in memref type");
449 memorySpace = v.getValue();
450 consumeToken(Token::integer);
451 parsedMemorySpace = true;
452 } else {
453 // Parse affine map.
454 if (parsedMemorySpace)
455 return emitError("affine map after memory space in memref type");
456 auto* affineMap = parseAffineMapReference();
457 if (affineMap == nullptr)
458 return ParseFailure;
459 affineMapComposition.push_back(affineMap);
460 }
461 return ParseSuccess;
462 };
463
464 // Parse comma separated list of affine maps, followed by memory space.
Chris Lattner40746442018-07-21 14:32:09 -0700465 if (parseCommaSeparatedListUntil(Token::greater, parseElt,
466 /*allowEmptyList=*/false)) {
MLIR Team718c82f2018-07-16 09:45:22 -0700467 return nullptr;
468 }
469 // Check that MemRef type specifies at least one affine map in composition.
470 if (affineMapComposition.empty())
471 return (emitError("expected semi-affine-map in memref type"), nullptr);
472 if (!parsedMemorySpace)
473 return (emitError("expected memory space in memref type"), nullptr);
474
475 return MemRefType::get(dimensions, elementType, affineMapComposition,
476 memorySpace);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700477}
478
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700479/// Parse a function type.
480///
481/// function-type ::= type-list-parens `->` type-list
482///
Chris Lattnerf7e22732018-06-22 22:03:48 -0700483Type *Parser::parseFunctionType() {
Chris Lattner48af7d12018-07-09 19:05:38 -0700484 assert(getToken().is(Token::l_paren));
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700485
Chris Lattnerf7e22732018-06-22 22:03:48 -0700486 SmallVector<Type*, 4> arguments;
487 if (parseTypeList(arguments))
488 return nullptr;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700489
490 if (!consumeIf(Token::arrow))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700491 return (emitError("expected '->' in function type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700492
Chris Lattnerf7e22732018-06-22 22:03:48 -0700493 SmallVector<Type*, 4> results;
494 if (parseTypeList(results))
495 return nullptr;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700496
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700497 return builder.getFunctionType(arguments, results);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700498}
499
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700500/// Parse an arbitrary type.
501///
502/// type ::= primitive-type
503/// | vector-type
504/// | tensor-type
505/// | memref-type
506/// | function-type
507/// element-type ::= primitive-type | vector-type
508///
Chris Lattnerf7e22732018-06-22 22:03:48 -0700509Type *Parser::parseType() {
Chris Lattner48af7d12018-07-09 19:05:38 -0700510 switch (getToken().getKind()) {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700511 case Token::kw_memref: return parseMemRefType();
512 case Token::kw_tensor: return parseTensorType();
513 case Token::kw_vector: return parseVectorType();
514 case Token::l_paren: return parseFunctionType();
515 default:
516 return parsePrimitiveType();
517 }
518}
519
Chris Lattner1604e472018-07-23 08:42:19 -0700520/// Parse a list of types without an enclosing parenthesis. The list must have
521/// at least one member.
522///
523/// type-list-no-parens ::= type (`,` type)*
524///
525ParseResult Parser::parseTypeListNoParens(SmallVectorImpl<Type *> &elements) {
526 auto parseElt = [&]() -> ParseResult {
527 auto elt = parseType();
528 elements.push_back(elt);
529 return elt ? ParseSuccess : ParseFailure;
530 };
531
532 return parseCommaSeparatedList(parseElt);
533}
534
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700535/// Parse a "type list", which is a singular type, or a parenthesized list of
536/// types.
537///
538/// type-list ::= type-list-parens | type
539/// type-list-parens ::= `(` `)`
Chris Lattner1604e472018-07-23 08:42:19 -0700540/// | `(` type-list-no-parens `)`
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700541///
Chris Lattnerf7e22732018-06-22 22:03:48 -0700542ParseResult Parser::parseTypeList(SmallVectorImpl<Type*> &elements) {
543 auto parseElt = [&]() -> ParseResult {
544 auto elt = parseType();
545 elements.push_back(elt);
546 return elt ? ParseSuccess : ParseFailure;
547 };
548
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700549 // If there is no parens, then it must be a singular type.
550 if (!consumeIf(Token::l_paren))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700551 return parseElt();
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700552
Chris Lattner40746442018-07-21 14:32:09 -0700553 if (parseCommaSeparatedListUntil(Token::r_paren, parseElt))
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700554 return ParseFailure;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700555
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700556 return ParseSuccess;
557}
558
Chris Lattner4c95a502018-06-23 16:03:42 -0700559//===----------------------------------------------------------------------===//
Chris Lattner7121b802018-07-04 20:45:39 -0700560// Attribute parsing.
561//===----------------------------------------------------------------------===//
562
563
564/// Attribute parsing.
565///
566/// attribute-value ::= bool-literal
567/// | integer-literal
568/// | float-literal
569/// | string-literal
570/// | `[` (attribute-value (`,` attribute-value)*)? `]`
571///
572Attribute *Parser::parseAttribute() {
Chris Lattner48af7d12018-07-09 19:05:38 -0700573 switch (getToken().getKind()) {
Chris Lattner7121b802018-07-04 20:45:39 -0700574 case Token::kw_true:
575 consumeToken(Token::kw_true);
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700576 return builder.getBoolAttr(true);
Chris Lattner7121b802018-07-04 20:45:39 -0700577 case Token::kw_false:
578 consumeToken(Token::kw_false);
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700579 return builder.getBoolAttr(false);
Chris Lattner7121b802018-07-04 20:45:39 -0700580
581 case Token::integer: {
Chris Lattner48af7d12018-07-09 19:05:38 -0700582 auto val = getToken().getUInt64IntegerValue();
Chris Lattner7121b802018-07-04 20:45:39 -0700583 if (!val.hasValue() || (int64_t)val.getValue() < 0)
584 return (emitError("integer too large for attribute"), nullptr);
585 consumeToken(Token::integer);
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700586 return builder.getIntegerAttr((int64_t)val.getValue());
Chris Lattner7121b802018-07-04 20:45:39 -0700587 }
588
589 case Token::minus: {
590 consumeToken(Token::minus);
Chris Lattner48af7d12018-07-09 19:05:38 -0700591 if (getToken().is(Token::integer)) {
592 auto val = getToken().getUInt64IntegerValue();
Chris Lattner7121b802018-07-04 20:45:39 -0700593 if (!val.hasValue() || (int64_t)-val.getValue() >= 0)
594 return (emitError("integer too large for attribute"), nullptr);
595 consumeToken(Token::integer);
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700596 return builder.getIntegerAttr((int64_t)-val.getValue());
Chris Lattner7121b802018-07-04 20:45:39 -0700597 }
598
599 return (emitError("expected constant integer or floating point value"),
600 nullptr);
601 }
602
603 case Token::string: {
Chris Lattner48af7d12018-07-09 19:05:38 -0700604 auto val = getToken().getStringValue();
Chris Lattner7121b802018-07-04 20:45:39 -0700605 consumeToken(Token::string);
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700606 return builder.getStringAttr(val);
Chris Lattner7121b802018-07-04 20:45:39 -0700607 }
608
609 case Token::l_bracket: {
610 consumeToken(Token::l_bracket);
611 SmallVector<Attribute*, 4> elements;
612
613 auto parseElt = [&]() -> ParseResult {
614 elements.push_back(parseAttribute());
615 return elements.back() ? ParseSuccess : ParseFailure;
616 };
617
Chris Lattner40746442018-07-21 14:32:09 -0700618 if (parseCommaSeparatedListUntil(Token::r_bracket, parseElt))
Chris Lattner7121b802018-07-04 20:45:39 -0700619 return nullptr;
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700620 return builder.getArrayAttr(elements);
Chris Lattner7121b802018-07-04 20:45:39 -0700621 }
622 default:
MLIR Teamb61885d2018-07-18 16:29:21 -0700623 // Try to parse affine map reference.
624 auto* affineMap = parseAffineMapReference();
625 if (affineMap != nullptr)
626 return builder.getAffineMapAttr(affineMap);
627
Chris Lattner7121b802018-07-04 20:45:39 -0700628 // TODO: Handle floating point.
629 return (emitError("expected constant attribute value"), nullptr);
630 }
631}
632
Chris Lattner7121b802018-07-04 20:45:39 -0700633/// Attribute dictionary.
634///
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700635/// attribute-dict ::= `{` `}`
636/// | `{` attribute-entry (`,` attribute-entry)* `}`
637/// attribute-entry ::= bare-id `:` attribute-value
Chris Lattner7121b802018-07-04 20:45:39 -0700638///
639ParseResult Parser::parseAttributeDict(
640 SmallVectorImpl<NamedAttribute> &attributes) {
641 consumeToken(Token::l_brace);
642
643 auto parseElt = [&]() -> ParseResult {
644 // We allow keywords as attribute names.
Chris Lattner48af7d12018-07-09 19:05:38 -0700645 if (getToken().isNot(Token::bare_identifier, Token::inttype) &&
646 !getToken().isKeyword())
Chris Lattner7121b802018-07-04 20:45:39 -0700647 return emitError("expected attribute name");
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700648 auto nameId = builder.getIdentifier(getTokenSpelling());
Chris Lattner7121b802018-07-04 20:45:39 -0700649 consumeToken();
650
651 if (!consumeIf(Token::colon))
652 return emitError("expected ':' in attribute list");
653
654 auto attr = parseAttribute();
655 if (!attr) return ParseFailure;
656
657 attributes.push_back({nameId, attr});
658 return ParseSuccess;
659 };
660
Chris Lattner40746442018-07-21 14:32:09 -0700661 if (parseCommaSeparatedListUntil(Token::r_brace, parseElt))
Chris Lattner7121b802018-07-04 20:45:39 -0700662 return ParseFailure;
663
664 return ParseSuccess;
665}
666
667//===----------------------------------------------------------------------===//
MLIR Teamf85a6262018-06-27 11:03:08 -0700668// Polyhedral structures.
669//===----------------------------------------------------------------------===//
670
Chris Lattner2e595eb2018-07-10 10:08:27 -0700671/// Lower precedence ops (all at the same precedence level). LNoOp is false in
672/// the boolean sense.
673enum AffineLowPrecOp {
674 /// Null value.
675 LNoOp,
676 Add,
677 Sub
678};
MLIR Teamf85a6262018-06-27 11:03:08 -0700679
Chris Lattner2e595eb2018-07-10 10:08:27 -0700680/// Higher precedence ops - all at the same precedence level. HNoOp is false in
681/// the boolean sense.
682enum AffineHighPrecOp {
683 /// Null value.
684 HNoOp,
685 Mul,
686 FloorDiv,
687 CeilDiv,
688 Mod
689};
Chris Lattner7121b802018-07-04 20:45:39 -0700690
Chris Lattner2e595eb2018-07-10 10:08:27 -0700691namespace {
692/// This is a specialized parser for AffineMap's, maintaining the state
693/// transient to their bodies.
694class AffineMapParser : public Parser {
695public:
696 explicit AffineMapParser(ParserState &state) : Parser(state) {}
Chris Lattner7121b802018-07-04 20:45:39 -0700697
Chris Lattner2e595eb2018-07-10 10:08:27 -0700698 AffineMap *parseAffineMapInline();
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700699
Chris Lattner2e595eb2018-07-10 10:08:27 -0700700private:
701 unsigned getNumDims() const { return dims.size(); }
702 unsigned getNumSymbols() const { return symbols.size(); }
MLIR Teamf85a6262018-06-27 11:03:08 -0700703
Uday Bondhugula0115dbb2018-07-11 21:31:07 -0700704 /// Returns true if the only identifiers the parser accepts in affine
705 /// expressions are symbolic identifiers.
706 bool isPureSymbolic() const { return pureSymbolic; }
707 void setSymbolicParsing(bool val) { pureSymbolic = val; }
708
Chris Lattner2e595eb2018-07-10 10:08:27 -0700709 // Binary affine op parsing.
710 AffineLowPrecOp consumeIfLowPrecOp();
711 AffineHighPrecOp consumeIfHighPrecOp();
MLIR Teamf85a6262018-06-27 11:03:08 -0700712
Chris Lattner2e595eb2018-07-10 10:08:27 -0700713 // Identifier lists for polyhedral structures.
714 ParseResult parseDimIdList();
715 ParseResult parseSymbolIdList();
716 ParseResult parseDimOrSymbolId(bool isDim);
717
718 AffineExpr *parseAffineExpr();
719 AffineExpr *parseParentheticalExpr();
720 AffineExpr *parseNegateExpression(AffineExpr *lhs);
721 AffineExpr *parseIntegerExpr();
722 AffineExpr *parseBareIdExpr();
723
724 AffineExpr *getBinaryAffineOpExpr(AffineHighPrecOp op, AffineExpr *lhs,
Uday Bondhugula851b8fd2018-07-20 14:57:21 -0700725 AffineExpr *rhs, SMLoc opLoc);
Chris Lattner2e595eb2018-07-10 10:08:27 -0700726 AffineExpr *getBinaryAffineOpExpr(AffineLowPrecOp op, AffineExpr *lhs,
727 AffineExpr *rhs);
728 AffineExpr *parseAffineOperandExpr(AffineExpr *lhs);
729 AffineExpr *parseAffineLowPrecOpExpr(AffineExpr *llhs,
730 AffineLowPrecOp llhsOp);
731 AffineExpr *parseAffineHighPrecOpExpr(AffineExpr *llhs,
Uday Bondhugula851b8fd2018-07-20 14:57:21 -0700732 AffineHighPrecOp llhsOp,
733 SMLoc llhsOpLoc);
Chris Lattner2e595eb2018-07-10 10:08:27 -0700734
735private:
736 // TODO(bondhugula): could just use an vector/ArrayRef and scan the numbers.
737 llvm::StringMap<unsigned> dims;
738 llvm::StringMap<unsigned> symbols;
Uday Bondhugula0115dbb2018-07-11 21:31:07 -0700739 /// True if the parser should allow only symbolic identifiers in affine
740 /// expressions.
741 bool pureSymbolic = false;
Chris Lattner2e595eb2018-07-10 10:08:27 -0700742};
743} // end anonymous namespace
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700744
Uday Bondhugula851b8fd2018-07-20 14:57:21 -0700745/// Create an affine binary high precedence op expression (mul's, div's, mod).
746/// opLoc is the location of the op token to be used to report errors
747/// for non-conforming expressions.
Chris Lattner2e595eb2018-07-10 10:08:27 -0700748AffineExpr *AffineMapParser::getBinaryAffineOpExpr(AffineHighPrecOp op,
749 AffineExpr *lhs,
Chris Lattner40746442018-07-21 14:32:09 -0700750 AffineExpr *rhs,
751 SMLoc opLoc) {
Uday Bondhugula0115dbb2018-07-11 21:31:07 -0700752 // TODO: make the error location info accurate.
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700753 switch (op) {
754 case Mul:
Uday Bondhugulacbe4cca2018-07-19 13:07:16 -0700755 if (!lhs->isSymbolicOrConstant() && !rhs->isSymbolicOrConstant()) {
Uday Bondhugula851b8fd2018-07-20 14:57:21 -0700756 emitError(opLoc, "non-affine expression: at least one of the multiply "
757 "operands has to be either a constant or symbolic");
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700758 return nullptr;
759 }
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700760 return builder.getMulExpr(lhs, rhs);
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700761 case FloorDiv:
Uday Bondhugulacbe4cca2018-07-19 13:07:16 -0700762 if (!rhs->isSymbolicOrConstant()) {
Uday Bondhugula851b8fd2018-07-20 14:57:21 -0700763 emitError(opLoc, "non-affine expression: right operand of floordiv "
764 "has to be either a constant or symbolic");
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700765 return nullptr;
766 }
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700767 return builder.getFloorDivExpr(lhs, rhs);
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700768 case CeilDiv:
Uday Bondhugulacbe4cca2018-07-19 13:07:16 -0700769 if (!rhs->isSymbolicOrConstant()) {
Uday Bondhugula851b8fd2018-07-20 14:57:21 -0700770 emitError(opLoc, "non-affine expression: right operand of ceildiv "
771 "has to be either a constant or symbolic");
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700772 return nullptr;
773 }
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700774 return builder.getCeilDivExpr(lhs, rhs);
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700775 case Mod:
Uday Bondhugulacbe4cca2018-07-19 13:07:16 -0700776 if (!rhs->isSymbolicOrConstant()) {
Uday Bondhugula851b8fd2018-07-20 14:57:21 -0700777 emitError(opLoc, "non-affine expression: right operand of mod "
778 "has to be either a constant or symbolic");
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700779 return nullptr;
780 }
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700781 return builder.getModExpr(lhs, rhs);
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700782 case HNoOp:
783 llvm_unreachable("can't create affine expression for null high prec op");
784 return nullptr;
785 }
786}
787
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700788/// Create an affine binary low precedence op expression (add, sub).
Chris Lattner2e595eb2018-07-10 10:08:27 -0700789AffineExpr *AffineMapParser::getBinaryAffineOpExpr(AffineLowPrecOp op,
790 AffineExpr *lhs,
791 AffineExpr *rhs) {
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700792 switch (op) {
793 case AffineLowPrecOp::Add:
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700794 return builder.getAddExpr(lhs, rhs);
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700795 case AffineLowPrecOp::Sub:
Uday Bondhugulac1faf662018-07-19 14:08:50 -0700796 return builder.getAddExpr(
797 lhs, builder.getMulExpr(rhs, builder.getConstantExpr(-1)));
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700798 case AffineLowPrecOp::LNoOp:
799 llvm_unreachable("can't create affine expression for null low prec op");
800 return nullptr;
801 }
802}
803
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700804/// Consume this token if it is a lower precedence affine op (there are only two
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700805/// precedence levels).
Chris Lattner2e595eb2018-07-10 10:08:27 -0700806AffineLowPrecOp AffineMapParser::consumeIfLowPrecOp() {
Chris Lattner48af7d12018-07-09 19:05:38 -0700807 switch (getToken().getKind()) {
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700808 case Token::plus:
809 consumeToken(Token::plus);
810 return AffineLowPrecOp::Add;
811 case Token::minus:
812 consumeToken(Token::minus);
813 return AffineLowPrecOp::Sub;
814 default:
815 return AffineLowPrecOp::LNoOp;
816 }
817}
818
819/// Consume this token if it is a higher precedence affine op (there are only
820/// two precedence levels)
Chris Lattner2e595eb2018-07-10 10:08:27 -0700821AffineHighPrecOp AffineMapParser::consumeIfHighPrecOp() {
Chris Lattner48af7d12018-07-09 19:05:38 -0700822 switch (getToken().getKind()) {
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700823 case Token::star:
824 consumeToken(Token::star);
825 return Mul;
826 case Token::kw_floordiv:
827 consumeToken(Token::kw_floordiv);
828 return FloorDiv;
829 case Token::kw_ceildiv:
830 consumeToken(Token::kw_ceildiv);
831 return CeilDiv;
832 case Token::kw_mod:
833 consumeToken(Token::kw_mod);
834 return Mod;
835 default:
836 return HNoOp;
837 }
838}
839
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700840/// Parse a high precedence op expression list: mul, div, and mod are high
841/// precedence binary ops, i.e., parse a
842/// expr_1 op_1 expr_2 op_2 ... expr_n
843/// where op_1, op_2 are all a AffineHighPrecOp (mul, div, mod).
844/// All affine binary ops are left associative.
845/// Given llhs, returns (llhs llhsOp lhs) op rhs, or (lhs op rhs) if llhs is
846/// null. If no rhs can be found, returns (llhs llhsOp lhs) or lhs if llhs is
Uday Bondhugula851b8fd2018-07-20 14:57:21 -0700847/// null. llhsOpLoc is the location of the llhsOp token that will be used to
848/// report an error for non-conforming expressions.
849AffineExpr *AffineMapParser::parseAffineHighPrecOpExpr(AffineExpr *llhs,
850 AffineHighPrecOp llhsOp,
851 SMLoc llhsOpLoc) {
Chris Lattner2e595eb2018-07-10 10:08:27 -0700852 AffineExpr *lhs = parseAffineOperandExpr(llhs);
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700853 if (!lhs)
854 return nullptr;
855
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700856 // Found an LHS. Parse the remaining expression.
Uday Bondhugula851b8fd2018-07-20 14:57:21 -0700857 auto opLoc = getToken().getLoc();
Chris Lattner2e595eb2018-07-10 10:08:27 -0700858 if (AffineHighPrecOp op = consumeIfHighPrecOp()) {
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700859 if (llhs) {
Uday Bondhugula851b8fd2018-07-20 14:57:21 -0700860 AffineExpr *expr = getBinaryAffineOpExpr(llhsOp, llhs, lhs, opLoc);
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700861 if (!expr)
862 return nullptr;
Uday Bondhugula851b8fd2018-07-20 14:57:21 -0700863 return parseAffineHighPrecOpExpr(expr, op, opLoc);
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700864 }
865 // No LLHS, get RHS
Uday Bondhugula851b8fd2018-07-20 14:57:21 -0700866 return parseAffineHighPrecOpExpr(lhs, op, opLoc);
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700867 }
868
869 // This is the last operand in this expression.
870 if (llhs)
Uday Bondhugula851b8fd2018-07-20 14:57:21 -0700871 return getBinaryAffineOpExpr(llhsOp, llhs, lhs, llhsOpLoc);
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700872
873 // No llhs, 'lhs' itself is the expression.
874 return lhs;
875}
876
877/// Parse an affine expression inside parentheses.
878///
879/// affine-expr ::= `(` affine-expr `)`
Chris Lattner2e595eb2018-07-10 10:08:27 -0700880AffineExpr *AffineMapParser::parseParentheticalExpr() {
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700881 if (!consumeIf(Token::l_paren))
882 return (emitError("expected '('"), nullptr);
Chris Lattner48af7d12018-07-09 19:05:38 -0700883 if (getToken().is(Token::r_paren))
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700884 return (emitError("no expression inside parentheses"), nullptr);
Chris Lattner2e595eb2018-07-10 10:08:27 -0700885 auto *expr = parseAffineExpr();
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700886 if (!expr)
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700887 return nullptr;
888 if (!consumeIf(Token::r_paren))
889 return (emitError("expected ')'"), nullptr);
890 return expr;
891}
892
893/// Parse the negation expression.
894///
895/// affine-expr ::= `-` affine-expr
Chris Lattner2e595eb2018-07-10 10:08:27 -0700896AffineExpr *AffineMapParser::parseNegateExpression(AffineExpr *lhs) {
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700897 if (!consumeIf(Token::minus))
898 return (emitError("expected '-'"), nullptr);
899
Chris Lattner2e595eb2018-07-10 10:08:27 -0700900 AffineExpr *operand = parseAffineOperandExpr(lhs);
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700901 // Since negation has the highest precedence of all ops (including high
902 // precedence ops) but lower than parentheses, we are only going to use
903 // parseAffineOperandExpr instead of parseAffineExpr here.
904 if (!operand)
905 // Extra error message although parseAffineOperandExpr would have
906 // complained. Leads to a better diagnostic.
907 return (emitError("missing operand of negation"), nullptr);
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700908 auto *minusOne = builder.getConstantExpr(-1);
909 return builder.getMulExpr(minusOne, operand);
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700910}
911
912/// Parse a bare id that may appear in an affine expression.
913///
914/// affine-expr ::= bare-id
Chris Lattner2e595eb2018-07-10 10:08:27 -0700915AffineExpr *AffineMapParser::parseBareIdExpr() {
Chris Lattner48af7d12018-07-09 19:05:38 -0700916 if (getToken().isNot(Token::bare_identifier))
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700917 return (emitError("expected bare identifier"), nullptr);
918
Chris Lattner48af7d12018-07-09 19:05:38 -0700919 StringRef sRef = getTokenSpelling();
Uday Bondhugula0115dbb2018-07-11 21:31:07 -0700920 // dims, symbols are all pairwise distinct.
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700921 if (dims.count(sRef)) {
Uday Bondhugula0115dbb2018-07-11 21:31:07 -0700922 if (isPureSymbolic())
923 return (emitError("identifier used is not a symbolic identifier"),
924 nullptr);
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700925 consumeToken(Token::bare_identifier);
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700926 return builder.getDimExpr(dims.lookup(sRef));
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700927 }
Uday Bondhugula0115dbb2018-07-11 21:31:07 -0700928
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700929 if (symbols.count(sRef)) {
930 consumeToken(Token::bare_identifier);
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700931 return builder.getSymbolExpr(symbols.lookup(sRef));
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700932 }
Uday Bondhugula0115dbb2018-07-11 21:31:07 -0700933
934 return (emitError("use of undeclared identifier"), nullptr);
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700935}
936
937/// Parse a positive integral constant appearing in an affine expression.
938///
939/// affine-expr ::= integer-literal
Chris Lattner2e595eb2018-07-10 10:08:27 -0700940AffineExpr *AffineMapParser::parseIntegerExpr() {
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700941 // No need to handle negative numbers separately here. They are naturally
942 // handled via the unary negation operator, although (FIXME) MININT_64 still
943 // not correctly handled.
Chris Lattner48af7d12018-07-09 19:05:38 -0700944 if (getToken().isNot(Token::integer))
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700945 return (emitError("expected integer"), nullptr);
946
Chris Lattner48af7d12018-07-09 19:05:38 -0700947 auto val = getToken().getUInt64IntegerValue();
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700948 if (!val.hasValue() || (int64_t)val.getValue() < 0) {
949 return (emitError("constant too large for affineint"), nullptr);
950 }
951 consumeToken(Token::integer);
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700952 return builder.getConstantExpr((int64_t)val.getValue());
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700953}
954
955/// Parses an expression that can be a valid operand of an affine expression.
Uday Bondhugula76345202018-07-09 13:47:52 -0700956/// lhs: if non-null, lhs is an affine expression that is the lhs of a binary
957/// operator, the rhs of which is being parsed. This is used to determine
958/// whether an error should be emitted for a missing right operand.
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700959// Eg: for an expression without parentheses (like i + j + k + l), each
960// of the four identifiers is an operand. For i + j*k + l, j*k is not an
961// operand expression, it's an op expression and will be parsed via
962// parseAffineHighPrecOpExpression(). However, for i + (j*k) + -l, (j*k) and -l
963// are valid operands that will be parsed by this function.
Chris Lattner2e595eb2018-07-10 10:08:27 -0700964AffineExpr *AffineMapParser::parseAffineOperandExpr(AffineExpr *lhs) {
Chris Lattner48af7d12018-07-09 19:05:38 -0700965 switch (getToken().getKind()) {
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700966 case Token::bare_identifier:
Chris Lattner2e595eb2018-07-10 10:08:27 -0700967 return parseBareIdExpr();
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700968 case Token::integer:
Chris Lattner2e595eb2018-07-10 10:08:27 -0700969 return parseIntegerExpr();
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700970 case Token::l_paren:
Chris Lattner2e595eb2018-07-10 10:08:27 -0700971 return parseParentheticalExpr();
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700972 case Token::minus:
Chris Lattner2e595eb2018-07-10 10:08:27 -0700973 return parseNegateExpression(lhs);
Uday Bondhugula76345202018-07-09 13:47:52 -0700974 case Token::kw_ceildiv:
975 case Token::kw_floordiv:
976 case Token::kw_mod:
977 case Token::plus:
978 case Token::star:
979 if (lhs)
980 emitError("missing right operand of binary operator");
981 else
982 emitError("missing left operand of binary operator");
983 return nullptr;
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700984 default:
985 if (lhs)
Uday Bondhugula76345202018-07-09 13:47:52 -0700986 emitError("missing right operand of binary operator");
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700987 else
988 emitError("expected affine expression");
989 return nullptr;
990 }
991}
992
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700993/// Parse affine expressions that are bare-id's, integer constants,
994/// parenthetical affine expressions, and affine op expressions that are a
995/// composition of those.
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700996///
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700997/// All binary op's associate from left to right.
998///
999/// {add, sub} have lower precedence than {mul, div, and mod}.
1000///
Uday Bondhugula76345202018-07-09 13:47:52 -07001001/// Add, sub'are themselves at the same precedence level. Mul, floordiv,
1002/// ceildiv, and mod are at the same higher precedence level. Negation has
1003/// higher precedence than any binary op.
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001004///
1005/// llhs: the affine expression appearing on the left of the one being parsed.
Uday Bondhugula3934d4d2018-07-09 09:00:25 -07001006/// This function will return ((llhs llhsOp lhs) op rhs) if llhs is non null,
1007/// and lhs op rhs otherwise; if there is no rhs, llhs llhsOp lhs is returned if
1008/// llhs is non-null; otherwise lhs is returned. This is to deal with left
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001009/// associativity.
1010///
1011/// Eg: when the expression is e1 + e2*e3 + e4, with e1 as llhs, this function
Uday Bondhugula3934d4d2018-07-09 09:00:25 -07001012/// will return the affine expr equivalent of (e1 + (e2*e3)) + e4, where (e2*e3)
1013/// will be parsed using parseAffineHighPrecOpExpr().
Chris Lattner2e595eb2018-07-10 10:08:27 -07001014AffineExpr *AffineMapParser::parseAffineLowPrecOpExpr(AffineExpr *llhs,
1015 AffineLowPrecOp llhsOp) {
Uday Bondhugula76345202018-07-09 13:47:52 -07001016 AffineExpr *lhs;
Chris Lattner2e595eb2018-07-10 10:08:27 -07001017 if (!(lhs = parseAffineOperandExpr(llhs)))
Uday Bondhugula3934d4d2018-07-09 09:00:25 -07001018 return nullptr;
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001019
1020 // Found an LHS. Deal with the ops.
Chris Lattner2e595eb2018-07-10 10:08:27 -07001021 if (AffineLowPrecOp lOp = consumeIfLowPrecOp()) {
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001022 if (llhs) {
Chris Lattner158e0a3e2018-07-08 20:51:38 -07001023 AffineExpr *sum = getBinaryAffineOpExpr(llhsOp, llhs, lhs);
Chris Lattner2e595eb2018-07-10 10:08:27 -07001024 return parseAffineLowPrecOpExpr(sum, lOp);
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001025 }
1026 // No LLHS, get RHS and form the expression.
Chris Lattner2e595eb2018-07-10 10:08:27 -07001027 return parseAffineLowPrecOpExpr(lhs, lOp);
Uday Bondhugula3934d4d2018-07-09 09:00:25 -07001028 }
Uday Bondhugula851b8fd2018-07-20 14:57:21 -07001029 auto opLoc = getToken().getLoc();
Chris Lattner2e595eb2018-07-10 10:08:27 -07001030 if (AffineHighPrecOp hOp = consumeIfHighPrecOp()) {
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001031 // We have a higher precedence op here. Get the rhs operand for the llhs
1032 // through parseAffineHighPrecOpExpr.
Uday Bondhugula851b8fd2018-07-20 14:57:21 -07001033 AffineExpr *highRes = parseAffineHighPrecOpExpr(lhs, hOp, opLoc);
Uday Bondhugula3934d4d2018-07-09 09:00:25 -07001034 if (!highRes)
1035 return nullptr;
Chris Lattner2e595eb2018-07-10 10:08:27 -07001036
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001037 // If llhs is null, the product forms the first operand of the yet to be
Uday Bondhugula3934d4d2018-07-09 09:00:25 -07001038 // found expression. If non-null, the op to associate with llhs is llhsOp.
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001039 AffineExpr *expr =
Chris Lattner158e0a3e2018-07-08 20:51:38 -07001040 llhs ? getBinaryAffineOpExpr(llhsOp, llhs, highRes) : highRes;
Chris Lattner2e595eb2018-07-10 10:08:27 -07001041
Uday Bondhugula3934d4d2018-07-09 09:00:25 -07001042 // Recurse for subsequent low prec op's after the affine high prec op
1043 // expression.
Chris Lattner2e595eb2018-07-10 10:08:27 -07001044 if (AffineLowPrecOp nextOp = consumeIfLowPrecOp())
1045 return parseAffineLowPrecOpExpr(expr, nextOp);
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001046 return expr;
1047 }
Uday Bondhugula3934d4d2018-07-09 09:00:25 -07001048 // Last operand in the expression list.
1049 if (llhs)
1050 return getBinaryAffineOpExpr(llhsOp, llhs, lhs);
1051 // No llhs, 'lhs' itself is the expression.
1052 return lhs;
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001053}
1054
1055/// Parse an affine expression.
Uday Bondhugula3934d4d2018-07-09 09:00:25 -07001056/// affine-expr ::= `(` affine-expr `)`
1057/// | `-` affine-expr
1058/// | affine-expr `+` affine-expr
1059/// | affine-expr `-` affine-expr
1060/// | affine-expr `*` affine-expr
1061/// | affine-expr `floordiv` affine-expr
1062/// | affine-expr `ceildiv` affine-expr
1063/// | affine-expr `mod` affine-expr
1064/// | bare-id
1065/// | integer-literal
1066///
1067/// Additional conditions are checked depending on the production. For eg., one
1068/// of the operands for `*` has to be either constant/symbolic; the second
1069/// operand for floordiv, ceildiv, and mod has to be a positive integer.
Chris Lattner2e595eb2018-07-10 10:08:27 -07001070AffineExpr *AffineMapParser::parseAffineExpr() {
1071 return parseAffineLowPrecOpExpr(nullptr, AffineLowPrecOp::LNoOp);
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001072}
1073
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001074/// Parse a dim or symbol from the lists appearing before the actual expressions
Chris Lattner2e595eb2018-07-10 10:08:27 -07001075/// of the affine map. Update our state to store the dimensional/symbolic
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001076/// identifier. 'dim': whether it's the dim list or symbol list that is being
1077/// parsed.
Chris Lattner2e595eb2018-07-10 10:08:27 -07001078ParseResult AffineMapParser::parseDimOrSymbolId(bool isDim) {
Chris Lattner48af7d12018-07-09 19:05:38 -07001079 if (getToken().isNot(Token::bare_identifier))
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001080 return emitError("expected bare identifier");
Chris Lattner48af7d12018-07-09 19:05:38 -07001081 auto sRef = getTokenSpelling();
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001082 consumeToken(Token::bare_identifier);
Chris Lattner2e595eb2018-07-10 10:08:27 -07001083 if (dims.count(sRef))
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001084 return emitError("dimensional identifier name reused");
Chris Lattner2e595eb2018-07-10 10:08:27 -07001085 if (symbols.count(sRef))
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001086 return emitError("symbolic identifier name reused");
Chris Lattner2e595eb2018-07-10 10:08:27 -07001087 if (isDim)
1088 dims.insert({sRef, dims.size()});
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001089 else
Chris Lattner2e595eb2018-07-10 10:08:27 -07001090 symbols.insert({sRef, symbols.size()});
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001091 return ParseSuccess;
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001092}
1093
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001094/// Parse the list of symbolic identifiers to an affine map.
Chris Lattner2e595eb2018-07-10 10:08:27 -07001095ParseResult AffineMapParser::parseSymbolIdList() {
1096 if (!consumeIf(Token::l_bracket))
1097 return emitError("expected '['");
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001098
Chris Lattner2e595eb2018-07-10 10:08:27 -07001099 auto parseElt = [&]() -> ParseResult { return parseDimOrSymbolId(false); };
Chris Lattner40746442018-07-21 14:32:09 -07001100 return parseCommaSeparatedListUntil(Token::r_bracket, parseElt);
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001101}
1102
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001103/// Parse the list of dimensional identifiers to an affine map.
Chris Lattner2e595eb2018-07-10 10:08:27 -07001104ParseResult AffineMapParser::parseDimIdList() {
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001105 if (!consumeIf(Token::l_paren))
1106 return emitError("expected '(' at start of dimensional identifiers list");
1107
Chris Lattner2e595eb2018-07-10 10:08:27 -07001108 auto parseElt = [&]() -> ParseResult { return parseDimOrSymbolId(true); };
Chris Lattner40746442018-07-21 14:32:09 -07001109 return parseCommaSeparatedListUntil(Token::r_paren, parseElt);
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001110}
1111
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001112/// Parse an affine map definition.
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001113///
Uday Bondhugula3934d4d2018-07-09 09:00:25 -07001114/// affine-map-inline ::= dim-and-symbol-id-lists `->` multi-dim-affine-expr
1115/// (`size` `(` dim-size (`,` dim-size)* `)`)?
1116/// dim-size ::= affine-expr | `min` `(` affine-expr ( `,` affine-expr)+ `)`
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001117///
Uday Bondhugula3934d4d2018-07-09 09:00:25 -07001118/// multi-dim-affine-expr ::= `(` affine-expr (`,` affine-expr)* `)
Chris Lattner2e595eb2018-07-10 10:08:27 -07001119AffineMap *AffineMapParser::parseAffineMapInline() {
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001120 // List of dimensional identifiers.
Chris Lattner2e595eb2018-07-10 10:08:27 -07001121 if (parseDimIdList())
Chris Lattner7121b802018-07-04 20:45:39 -07001122 return nullptr;
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001123
1124 // Symbols are optional.
Chris Lattner48af7d12018-07-09 19:05:38 -07001125 if (getToken().is(Token::l_bracket)) {
Chris Lattner2e595eb2018-07-10 10:08:27 -07001126 if (parseSymbolIdList())
Chris Lattner7121b802018-07-04 20:45:39 -07001127 return nullptr;
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001128 }
1129 if (!consumeIf(Token::arrow)) {
Chris Lattner7121b802018-07-04 20:45:39 -07001130 return (emitError("expected '->' or '['"), nullptr);
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001131 }
1132 if (!consumeIf(Token::l_paren)) {
1133 emitError("expected '(' at start of affine map range");
Chris Lattner7121b802018-07-04 20:45:39 -07001134 return nullptr;
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001135 }
1136
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001137 SmallVector<AffineExpr *, 4> exprs;
1138 auto parseElt = [&]() -> ParseResult {
Chris Lattner2e595eb2018-07-10 10:08:27 -07001139 auto *elt = parseAffineExpr();
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001140 ParseResult res = elt ? ParseSuccess : ParseFailure;
1141 exprs.push_back(elt);
1142 return res;
1143 };
1144
1145 // Parse a multi-dimensional affine expression (a comma-separated list of 1-d
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001146 // affine expressions); the list cannot be empty.
1147 // Grammar: multi-dim-affine-expr ::= `(` affine-expr (`,` affine-expr)* `)
Chris Lattner40746442018-07-21 14:32:09 -07001148 if (parseCommaSeparatedListUntil(Token::r_paren, parseElt, false))
Chris Lattner7121b802018-07-04 20:45:39 -07001149 return nullptr;
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001150
Uday Bondhugula0115dbb2018-07-11 21:31:07 -07001151 // Parse optional range sizes.
Uday Bondhugula1e500b42018-07-12 18:04:04 -07001152 // range-sizes ::= (`size` `(` dim-size (`,` dim-size)* `)`)?
1153 // dim-size ::= affine-expr | `min` `(` affine-expr (`,` affine-expr)+ `)`
1154 // TODO(bondhugula): support for min of several affine expressions.
Uday Bondhugula0115dbb2018-07-11 21:31:07 -07001155 // TODO: check if sizes are non-negative whenever they are constant.
1156 SmallVector<AffineExpr *, 4> rangeSizes;
1157 if (consumeIf(Token::kw_size)) {
1158 // Location of the l_paren token (if it exists) for error reporting later.
1159 auto loc = getToken().getLoc();
1160 if (!consumeIf(Token::l_paren))
1161 return (emitError("expected '(' at start of affine map range"), nullptr);
1162
1163 auto parseRangeSize = [&]() -> ParseResult {
1164 auto *elt = parseAffineExpr();
1165 ParseResult res = elt ? ParseSuccess : ParseFailure;
1166 rangeSizes.push_back(elt);
1167 return res;
1168 };
1169
1170 setSymbolicParsing(true);
Chris Lattner40746442018-07-21 14:32:09 -07001171 if (parseCommaSeparatedListUntil(Token::r_paren, parseRangeSize, false))
Uday Bondhugula0115dbb2018-07-11 21:31:07 -07001172 return nullptr;
1173 if (exprs.size() > rangeSizes.size())
1174 return (emitError(loc, "fewer range sizes than range expressions"),
1175 nullptr);
1176 if (exprs.size() < rangeSizes.size())
1177 return (emitError(loc, "more range sizes than range expressions"),
1178 nullptr);
1179 }
1180
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001181 // Parsed a valid affine map.
Uday Bondhugula0115dbb2018-07-11 21:31:07 -07001182 return builder.getAffineMap(dims.size(), symbols.size(), exprs, rangeSizes);
MLIR Teamf85a6262018-06-27 11:03:08 -07001183}
1184
Chris Lattner2e595eb2018-07-10 10:08:27 -07001185AffineMap *Parser::parseAffineMapInline() {
1186 return AffineMapParser(state).parseAffineMapInline();
1187}
1188
MLIR Team718c82f2018-07-16 09:45:22 -07001189AffineMap *Parser::parseAffineMapReference() {
1190 if (getToken().is(Token::hash_identifier)) {
1191 // Parse affine map identifier and verify that it exists.
1192 StringRef affineMapId = getTokenSpelling().drop_front();
1193 if (getState().affineMapDefinitions.count(affineMapId) == 0)
1194 return (emitError("undefined affine map id '" + affineMapId + "'"),
1195 nullptr);
1196 consumeToken(Token::hash_identifier);
1197 return getState().affineMapDefinitions[affineMapId];
1198 }
1199 // Try to parse inline affine map.
1200 return parseAffineMapInline();
1201}
1202
MLIR Teamf85a6262018-06-27 11:03:08 -07001203//===----------------------------------------------------------------------===//
Chris Lattner7f9cc272018-07-19 08:35:28 -07001204// FunctionParser
Chris Lattner4c95a502018-06-23 16:03:42 -07001205//===----------------------------------------------------------------------===//
Chris Lattnere79379a2018-06-22 10:39:19 -07001206
Chris Lattner7f9cc272018-07-19 08:35:28 -07001207namespace {
1208/// This class contains parser state that is common across CFG and ML functions,
1209/// notably for dealing with operations and SSA values.
1210class FunctionParser : public Parser {
1211public:
1212 FunctionParser(ParserState &state) : Parser(state) {}
1213
Chris Lattner6119d382018-07-20 18:41:34 -07001214 /// After the function is finished parsing, this function checks to see if
1215 /// there are any remaining issues.
Chris Lattner40746442018-07-21 14:32:09 -07001216 ParseResult finalizeFunction(Function *func, SMLoc loc);
Chris Lattner6119d382018-07-20 18:41:34 -07001217
1218 /// This represents a use of an SSA value in the program. The first two
1219 /// entries in the tuple are the name and result number of a reference. The
1220 /// third is the location of the reference, which is used in case this ends up
1221 /// being a use of an undefined value.
1222 struct SSAUseInfo {
1223 StringRef name; // Value name, e.g. %42 or %abc
1224 unsigned number; // Number, specified with #12
1225 SMLoc loc; // Location of first definition or use.
1226 };
Chris Lattner7f9cc272018-07-19 08:35:28 -07001227
1228 /// Given a reference to an SSA value and its type, return a reference. This
1229 /// returns null on failure.
1230 SSAValue *resolveSSAUse(SSAUseInfo useInfo, Type *type);
1231
1232 /// Register a definition of a value with the symbol table.
1233 ParseResult addDefinition(SSAUseInfo useInfo, SSAValue *value);
1234
1235 // SSA parsing productions.
1236 ParseResult parseSSAUse(SSAUseInfo &result);
Chris Lattner40746442018-07-21 14:32:09 -07001237 ParseResult parseOptionalSSAUseList(SmallVectorImpl<SSAUseInfo> &results);
James Molloy61a656c2018-07-22 15:45:24 -07001238
1239 template <typename ResultType>
1240 ResultType parseSSADefOrUseAndType(
1241 const std::function<ResultType(SSAUseInfo, Type *)> &action);
1242
1243 SSAValue *parseSSAUseAndType() {
1244 return parseSSADefOrUseAndType<SSAValue *>(
1245 [&](SSAUseInfo useInfo, Type *type) -> SSAValue * {
1246 return resolveSSAUse(useInfo, type);
1247 });
1248 }
Chris Lattner40746442018-07-21 14:32:09 -07001249
1250 template <typename ValueTy>
Chris Lattner7f9cc272018-07-19 08:35:28 -07001251 ParseResult
Chris Lattner40746442018-07-21 14:32:09 -07001252 parseOptionalSSAUseAndTypeList(SmallVectorImpl<ValueTy *> &results);
Chris Lattner7f9cc272018-07-19 08:35:28 -07001253
1254 // Operations
1255 ParseResult parseOperation(const CreateOperationFunction &createOpFunc);
1256
1257private:
1258 /// This keeps track of all of the SSA values we are tracking, indexed by
Chris Lattner6119d382018-07-20 18:41:34 -07001259 /// their name. This has one entry per result number.
1260 llvm::StringMap<SmallVector<std::pair<SSAValue *, SMLoc>, 1>> values;
1261
1262 /// These are all of the placeholders we've made along with the location of
1263 /// their first reference, to allow checking for use of undefined values.
1264 DenseMap<SSAValue *, SMLoc> forwardReferencePlaceholders;
1265
1266 SSAValue *createForwardReferencePlaceholder(SMLoc loc, Type *type);
1267
1268 /// Return true if this is a forward reference.
1269 bool isForwardReferencePlaceholder(SSAValue *value) {
1270 return forwardReferencePlaceholders.count(value);
1271 }
Chris Lattner7f9cc272018-07-19 08:35:28 -07001272};
1273} // end anonymous namespace
1274
Chris Lattner6119d382018-07-20 18:41:34 -07001275/// Create and remember a new placeholder for a forward reference.
1276SSAValue *FunctionParser::createForwardReferencePlaceholder(SMLoc loc,
1277 Type *type) {
1278 // Forward references are always created as instructions, even in ML
1279 // functions, because we just need something with a def/use chain.
1280 //
1281 // We create these placeholders as having an empty name, which we know cannot
1282 // be created through normal user input, allowing us to distinguish them.
1283 auto name = Identifier::get("placeholder", getContext());
1284 auto *inst = OperationInst::create(name, /*operands*/ {}, type, /*attrs*/ {},
1285 getContext());
1286 forwardReferencePlaceholders[inst->getResult(0)] = loc;
1287 return inst->getResult(0);
1288}
1289
Chris Lattner7f9cc272018-07-19 08:35:28 -07001290/// Given an unbound reference to an SSA value and its type, return a the value
1291/// it specifies. This returns null on failure.
1292SSAValue *FunctionParser::resolveSSAUse(SSAUseInfo useInfo, Type *type) {
Chris Lattner6119d382018-07-20 18:41:34 -07001293 auto &entries = values[useInfo.name];
1294
Chris Lattner7f9cc272018-07-19 08:35:28 -07001295 // If we have already seen a value of this name, return it.
Chris Lattner6119d382018-07-20 18:41:34 -07001296 if (useInfo.number < entries.size() && entries[useInfo.number].first) {
1297 auto *result = entries[useInfo.number].first;
Chris Lattner7f9cc272018-07-19 08:35:28 -07001298 // Check that the type matches the other uses.
Chris Lattner7f9cc272018-07-19 08:35:28 -07001299 if (result->getType() == type)
1300 return result;
1301
Chris Lattner6119d382018-07-20 18:41:34 -07001302 emitError(useInfo.loc, "use of value '" + useInfo.name.str() +
1303 "' expects different type than prior uses");
1304 emitError(entries[useInfo.number].second, "prior use here");
Chris Lattner7f9cc272018-07-19 08:35:28 -07001305 return nullptr;
1306 }
1307
Chris Lattner6119d382018-07-20 18:41:34 -07001308 // Make sure we have enough slots for this.
1309 if (entries.size() <= useInfo.number)
1310 entries.resize(useInfo.number + 1);
1311
1312 // If the value has already been defined and this is an overly large result
1313 // number, diagnose that.
1314 if (entries[0].first && !isForwardReferencePlaceholder(entries[0].first))
1315 return (emitError(useInfo.loc, "reference to invalid result number"),
1316 nullptr);
1317
1318 // Otherwise, this is a forward reference. Create a placeholder and remember
1319 // that we did so.
1320 auto *result = createForwardReferencePlaceholder(useInfo.loc, type);
1321 entries[useInfo.number].first = result;
1322 entries[useInfo.number].second = useInfo.loc;
1323 return result;
Chris Lattner7f9cc272018-07-19 08:35:28 -07001324}
1325
1326/// Register a definition of a value with the symbol table.
1327ParseResult FunctionParser::addDefinition(SSAUseInfo useInfo, SSAValue *value) {
Chris Lattner6119d382018-07-20 18:41:34 -07001328 auto &entries = values[useInfo.name];
Chris Lattner7f9cc272018-07-19 08:35:28 -07001329
Chris Lattner6119d382018-07-20 18:41:34 -07001330 // Make sure there is a slot for this value.
1331 if (entries.size() <= useInfo.number)
1332 entries.resize(useInfo.number + 1);
Chris Lattner7f9cc272018-07-19 08:35:28 -07001333
Chris Lattner6119d382018-07-20 18:41:34 -07001334 // If we already have an entry for this, check to see if it was a definition
1335 // or a forward reference.
1336 if (auto *existing = entries[useInfo.number].first) {
1337 if (!isForwardReferencePlaceholder(existing)) {
1338 emitError(useInfo.loc,
1339 "redefinition of SSA value '" + useInfo.name + "'");
1340 return emitError(entries[useInfo.number].second,
1341 "previously defined here");
1342 }
1343
1344 // If it was a forward reference, update everything that used it to use the
1345 // actual definition instead, delete the forward ref, and remove it from our
1346 // set of forward references we track.
1347 existing->replaceAllUsesWith(value);
1348 existing->getDefiningInst()->destroy();
1349 forwardReferencePlaceholders.erase(existing);
1350 }
1351
1352 entries[useInfo.number].first = value;
1353 entries[useInfo.number].second = useInfo.loc;
1354 return ParseSuccess;
1355}
1356
1357/// After the function is finished parsing, this function checks to see if
1358/// there are any remaining issues.
Chris Lattner40746442018-07-21 14:32:09 -07001359ParseResult FunctionParser::finalizeFunction(Function *func, SMLoc loc) {
Chris Lattner6119d382018-07-20 18:41:34 -07001360 // Check for any forward references that are left. If we find any, error out.
1361 if (!forwardReferencePlaceholders.empty()) {
1362 SmallVector<std::pair<const char *, SSAValue *>, 4> errors;
1363 // Iteration over the map isn't determinstic, so sort by source location.
1364 for (auto entry : forwardReferencePlaceholders)
1365 errors.push_back({entry.second.getPointer(), entry.first});
1366 llvm::array_pod_sort(errors.begin(), errors.end());
1367
1368 for (auto entry : errors)
1369 emitError(SMLoc::getFromPointer(entry.first),
1370 "use of undeclared SSA value name");
1371 return ParseFailure;
1372 }
1373
Chris Lattner40746442018-07-21 14:32:09 -07001374 // Run the verifier on this function. If an error is detected, report it.
1375 std::string errorString;
1376 if (func->verify(&errorString))
1377 return emitError(loc, errorString);
1378
Chris Lattner6119d382018-07-20 18:41:34 -07001379 return ParseSuccess;
Chris Lattner7f9cc272018-07-19 08:35:28 -07001380}
1381
Chris Lattner78276e32018-07-07 15:48:26 -07001382/// Parse a SSA operand for an instruction or statement.
1383///
James Molloy61a656c2018-07-22 15:45:24 -07001384/// ssa-use ::= ssa-id
Chris Lattner78276e32018-07-07 15:48:26 -07001385///
Chris Lattner7f9cc272018-07-19 08:35:28 -07001386ParseResult FunctionParser::parseSSAUse(SSAUseInfo &result) {
Chris Lattner6119d382018-07-20 18:41:34 -07001387 result.name = getTokenSpelling();
1388 result.number = 0;
1389 result.loc = getToken().getLoc();
Chris Lattner7f9cc272018-07-19 08:35:28 -07001390 if (!consumeIf(Token::percent_identifier))
1391 return emitError("expected SSA operand");
Chris Lattner6119d382018-07-20 18:41:34 -07001392
1393 // If we have an affine map ID, it is a result number.
1394 if (getToken().is(Token::hash_identifier)) {
1395 if (auto value = getToken().getHashIdentifierNumber())
1396 result.number = value.getValue();
1397 else
1398 return emitError("invalid SSA value result number");
1399 consumeToken(Token::hash_identifier);
1400 }
1401
Chris Lattner7f9cc272018-07-19 08:35:28 -07001402 return ParseSuccess;
Chris Lattner78276e32018-07-07 15:48:26 -07001403}
1404
1405/// Parse a (possibly empty) list of SSA operands.
1406///
1407/// ssa-use-list ::= ssa-use (`,` ssa-use)*
1408/// ssa-use-list-opt ::= ssa-use-list?
1409///
Chris Lattner7f9cc272018-07-19 08:35:28 -07001410ParseResult
Chris Lattner40746442018-07-21 14:32:09 -07001411FunctionParser::parseOptionalSSAUseList(SmallVectorImpl<SSAUseInfo> &results) {
1412 if (!getToken().is(Token::percent_identifier))
1413 return ParseSuccess;
1414 return parseCommaSeparatedList([&]() -> ParseResult {
Chris Lattner7f9cc272018-07-19 08:35:28 -07001415 SSAUseInfo result;
1416 if (parseSSAUse(result))
1417 return ParseFailure;
1418 results.push_back(result);
1419 return ParseSuccess;
1420 });
Chris Lattner78276e32018-07-07 15:48:26 -07001421}
1422
1423/// Parse an SSA use with an associated type.
1424///
1425/// ssa-use-and-type ::= ssa-use `:` type
James Molloy61a656c2018-07-22 15:45:24 -07001426template <typename ResultType>
1427ResultType FunctionParser::parseSSADefOrUseAndType(
1428 const std::function<ResultType(SSAUseInfo, Type *)> &action) {
Chris Lattner7f9cc272018-07-19 08:35:28 -07001429 SSAUseInfo useInfo;
1430 if (parseSSAUse(useInfo))
1431 return nullptr;
Chris Lattner78276e32018-07-07 15:48:26 -07001432
1433 if (!consumeIf(Token::colon))
Chris Lattner7f9cc272018-07-19 08:35:28 -07001434 return (emitError("expected ':' and type for SSA operand"), nullptr);
Chris Lattner78276e32018-07-07 15:48:26 -07001435
Chris Lattner7f9cc272018-07-19 08:35:28 -07001436 auto *type = parseType();
1437 if (!type)
1438 return nullptr;
Chris Lattner78276e32018-07-07 15:48:26 -07001439
James Molloy61a656c2018-07-22 15:45:24 -07001440 return action(useInfo, type);
Chris Lattner78276e32018-07-07 15:48:26 -07001441}
1442
1443/// Parse a (possibly empty) list of SSA operands with types.
1444///
1445/// ssa-use-and-type-list ::= ssa-use-and-type (`,` ssa-use-and-type)*
1446///
Chris Lattner40746442018-07-21 14:32:09 -07001447template <typename ValueTy>
Chris Lattner7f9cc272018-07-19 08:35:28 -07001448ParseResult FunctionParser::parseOptionalSSAUseAndTypeList(
Chris Lattner40746442018-07-21 14:32:09 -07001449 SmallVectorImpl<ValueTy *> &results) {
1450 if (getToken().isNot(Token::percent_identifier))
1451 return ParseSuccess;
1452
1453 return parseCommaSeparatedList([&]() -> ParseResult {
Chris Lattner7f9cc272018-07-19 08:35:28 -07001454 if (auto *value = parseSSAUseAndType()) {
Chris Lattner40746442018-07-21 14:32:09 -07001455 results.push_back(cast<ValueTy>(value));
Chris Lattner7f9cc272018-07-19 08:35:28 -07001456 return ParseSuccess;
1457 }
1458 return ParseFailure;
1459 });
Chris Lattner78276e32018-07-07 15:48:26 -07001460}
1461
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001462/// Parse the CFG or MLFunc operation.
1463///
1464/// TODO(clattner): This is a change from the MLIR spec as written, it is an
1465/// experiment that will eliminate "builtin" instructions as a thing.
1466///
1467/// operation ::=
1468/// (ssa-id `=`)? string '(' ssa-use-list? ')' attribute-dict?
1469/// `:` function-type
1470///
1471ParseResult
Chris Lattner7f9cc272018-07-19 08:35:28 -07001472FunctionParser::parseOperation(const CreateOperationFunction &createOpFunc) {
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001473 auto loc = getToken().getLoc();
1474
1475 StringRef resultID;
1476 if (getToken().is(Token::percent_identifier)) {
Chris Lattner7f9cc272018-07-19 08:35:28 -07001477 resultID = getTokenSpelling();
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001478 consumeToken(Token::percent_identifier);
1479 if (!consumeIf(Token::equal))
1480 return emitError("expected '=' after SSA name");
1481 }
1482
1483 if (getToken().isNot(Token::string))
1484 return emitError("expected operation name in quotes");
1485
1486 auto name = getToken().getStringValue();
1487 if (name.empty())
1488 return emitError("empty operation name is invalid");
1489
1490 consumeToken(Token::string);
1491
1492 if (!consumeIf(Token::l_paren))
1493 return emitError("expected '(' to start operand list");
1494
1495 // Parse the operand list.
Chris Lattner7f9cc272018-07-19 08:35:28 -07001496 SmallVector<SSAUseInfo, 8> operandInfos;
Chris Lattner40746442018-07-21 14:32:09 -07001497 if (parseOptionalSSAUseList(operandInfos))
1498 return ParseFailure;
1499
1500 if (!consumeIf(Token::r_paren))
1501 return emitError("expected ')' to end operand list");
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001502
1503 SmallVector<NamedAttribute, 4> attributes;
1504 if (getToken().is(Token::l_brace)) {
1505 if (parseAttributeDict(attributes))
1506 return ParseFailure;
1507 }
1508
Chris Lattner3b2ef762018-07-18 15:31:25 -07001509 if (!consumeIf(Token::colon))
1510 return emitError("expected ':' followed by instruction type");
1511
1512 auto typeLoc = getToken().getLoc();
1513 auto type = parseType();
1514 if (!type)
1515 return ParseFailure;
1516 auto fnType = dyn_cast<FunctionType>(type);
1517 if (!fnType)
1518 return emitError(typeLoc, "expected function type");
1519
Chris Lattner7f9cc272018-07-19 08:35:28 -07001520 // Check that we have the right number of types for the operands.
1521 auto operandTypes = fnType->getInputs();
1522 if (operandTypes.size() != operandInfos.size()) {
1523 auto plural = "s"[operandInfos.size() == 1];
1524 return emitError(typeLoc, "expected " + llvm::utostr(operandInfos.size()) +
Chris Lattnerf8cce872018-07-20 09:28:54 -07001525 " operand type" + plural + " but had " +
Chris Lattner7f9cc272018-07-19 08:35:28 -07001526 llvm::utostr(operandTypes.size()));
1527 }
1528
1529 // Resolve all of the operands.
1530 SmallVector<SSAValue *, 8> operands;
1531 for (unsigned i = 0, e = operandInfos.size(); i != e; ++i) {
1532 operands.push_back(resolveSSAUse(operandInfos[i], operandTypes[i]));
1533 if (!operands.back())
1534 return ParseFailure;
1535 }
1536
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001537 auto nameId = builder.getIdentifier(name);
Chris Lattner7f9cc272018-07-19 08:35:28 -07001538 auto op = createOpFunc(nameId, operands, fnType->getResults(), attributes);
1539 if (!op)
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001540 return ParseFailure;
1541
1542 // We just parsed an operation. If it is a recognized one, verify that it
1543 // is structurally as we expect. If not, produce an error with a reasonable
1544 // source location.
Chris Lattner7f9cc272018-07-19 08:35:28 -07001545 if (auto *opInfo = op->getAbstractOperation(builder.getContext())) {
1546 if (auto error = opInfo->verifyInvariants(op))
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001547 return emitError(loc, error);
1548 }
1549
Chris Lattner7f9cc272018-07-19 08:35:28 -07001550 // If the instruction had a name, register it.
1551 if (!resultID.empty()) {
1552 // FIXME: Add result infra to handle Stmt results as well to make this
1553 // generic.
1554 if (auto *inst = dyn_cast<OperationInst>(op)) {
Chris Lattnerf8cce872018-07-20 09:28:54 -07001555 if (inst->getNumResults() == 0)
Chris Lattner7f9cc272018-07-19 08:35:28 -07001556 return emitError(loc, "cannot name an operation with no results");
1557
Chris Lattner6119d382018-07-20 18:41:34 -07001558 for (unsigned i = 0, e = inst->getNumResults(); i != e; ++i)
1559 addDefinition({resultID, i, loc}, inst->getResult(i));
Chris Lattner7f9cc272018-07-19 08:35:28 -07001560 }
1561 }
1562
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001563 return ParseSuccess;
1564}
Chris Lattnere79379a2018-06-22 10:39:19 -07001565
Chris Lattner48af7d12018-07-09 19:05:38 -07001566//===----------------------------------------------------------------------===//
1567// CFG Functions
1568//===----------------------------------------------------------------------===//
Chris Lattnere79379a2018-06-22 10:39:19 -07001569
Chris Lattner4c95a502018-06-23 16:03:42 -07001570namespace {
Chris Lattner48af7d12018-07-09 19:05:38 -07001571/// This is a specialized parser for CFGFunction's, maintaining the state
1572/// transient to their bodies.
Chris Lattner7f9cc272018-07-19 08:35:28 -07001573class CFGFunctionParser : public FunctionParser {
Chris Lattner158e0a3e2018-07-08 20:51:38 -07001574public:
Chris Lattner2e595eb2018-07-10 10:08:27 -07001575 CFGFunctionParser(ParserState &state, CFGFunction *function)
Chris Lattner7f9cc272018-07-19 08:35:28 -07001576 : FunctionParser(state), function(function), builder(function) {}
Chris Lattner2e595eb2018-07-10 10:08:27 -07001577
1578 ParseResult parseFunctionBody();
1579
1580private:
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001581 CFGFunction *function;
1582 llvm::StringMap<std::pair<BasicBlock*, SMLoc>> blocksByName;
Chris Lattner48af7d12018-07-09 19:05:38 -07001583
1584 /// This builder intentionally shadows the builder in the base class, with a
1585 /// more specific builder type.
Chris Lattner158e0a3e2018-07-08 20:51:38 -07001586 CFGFuncBuilder builder;
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001587
Chris Lattner4c95a502018-06-23 16:03:42 -07001588 /// Get the basic block with the specified name, creating it if it doesn't
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001589 /// already exist. The location specified is the point of use, which allows
1590 /// us to diagnose references to blocks that are not defined precisely.
1591 BasicBlock *getBlockNamed(StringRef name, SMLoc loc) {
1592 auto &blockAndLoc = blocksByName[name];
1593 if (!blockAndLoc.first) {
Chris Lattner3a467cc2018-07-01 20:28:00 -07001594 blockAndLoc.first = new BasicBlock();
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001595 blockAndLoc.second = loc;
Chris Lattner4c95a502018-06-23 16:03:42 -07001596 }
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001597 return blockAndLoc.first;
Chris Lattner4c95a502018-06-23 16:03:42 -07001598 }
Chris Lattner48af7d12018-07-09 19:05:38 -07001599
James Molloy61a656c2018-07-22 15:45:24 -07001600 ParseResult
1601 parseOptionalBasicBlockArgList(SmallVectorImpl<BBArgument *> &results,
1602 BasicBlock *owner);
1603
Chris Lattner48af7d12018-07-09 19:05:38 -07001604 ParseResult parseBasicBlock();
1605 OperationInst *parseCFGOperation();
1606 TerminatorInst *parseTerminator();
Chris Lattner4c95a502018-06-23 16:03:42 -07001607};
1608} // end anonymous namespace
1609
James Molloy61a656c2018-07-22 15:45:24 -07001610/// Parse a (possibly empty) list of SSA operands with types as basic block
1611/// arguments. Unlike parseOptionalSsaUseAndTypeList the SSA IDs are treated as
1612/// defs, not uses.
1613///
1614/// ssa-id-and-type-list ::= ssa-id-and-type (`,` ssa-id-and-type)*
1615///
1616ParseResult CFGFunctionParser::parseOptionalBasicBlockArgList(
1617 SmallVectorImpl<BBArgument *> &results, BasicBlock *owner) {
1618 if (getToken().is(Token::r_brace))
1619 return ParseSuccess;
1620
1621 return parseCommaSeparatedList([&]() -> ParseResult {
1622 auto type = parseSSADefOrUseAndType<Type *>(
1623 [&](SSAUseInfo useInfo, Type *type) -> Type * {
1624 BBArgument *arg = owner->addArgument(type);
1625 if (addDefinition(useInfo, arg) == ParseFailure)
1626 return nullptr;
1627 return type;
1628 });
1629 return type ? ParseSuccess : ParseFailure;
1630 });
1631}
1632
Chris Lattner48af7d12018-07-09 19:05:38 -07001633ParseResult CFGFunctionParser::parseFunctionBody() {
Chris Lattner40746442018-07-21 14:32:09 -07001634 auto braceLoc = getToken().getLoc();
Chris Lattner48af7d12018-07-09 19:05:38 -07001635 if (!consumeIf(Token::l_brace))
1636 return emitError("expected '{' in CFG function");
1637
1638 // Make sure we have at least one block.
1639 if (getToken().is(Token::r_brace))
1640 return emitError("CFG functions must have at least one basic block");
Chris Lattner4c95a502018-06-23 16:03:42 -07001641
1642 // Parse the list of blocks.
1643 while (!consumeIf(Token::r_brace))
Chris Lattner48af7d12018-07-09 19:05:38 -07001644 if (parseBasicBlock())
Chris Lattner4c95a502018-06-23 16:03:42 -07001645 return ParseFailure;
1646
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001647 // Verify that all referenced blocks were defined. Iteration over a
1648 // StringMap isn't determinstic, but this is good enough for our purposes.
Chris Lattner48af7d12018-07-09 19:05:38 -07001649 for (auto &elt : blocksByName) {
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001650 auto *bb = elt.second.first;
Chris Lattner3a467cc2018-07-01 20:28:00 -07001651 if (!bb->getFunction())
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001652 return emitError(elt.second.second,
1653 "reference to an undefined basic block '" +
1654 elt.first() + "'");
1655 }
1656
Chris Lattner48af7d12018-07-09 19:05:38 -07001657 getModule()->functionList.push_back(function);
Chris Lattner6119d382018-07-20 18:41:34 -07001658
Chris Lattner40746442018-07-21 14:32:09 -07001659 return finalizeFunction(function, braceLoc);
Chris Lattner4c95a502018-06-23 16:03:42 -07001660}
1661
1662/// Basic block declaration.
1663///
1664/// basic-block ::= bb-label instruction* terminator-stmt
1665/// bb-label ::= bb-id bb-arg-list? `:`
1666/// bb-id ::= bare-id
1667/// bb-arg-list ::= `(` ssa-id-and-type-list? `)`
1668///
Chris Lattner48af7d12018-07-09 19:05:38 -07001669ParseResult CFGFunctionParser::parseBasicBlock() {
1670 SMLoc nameLoc = getToken().getLoc();
1671 auto name = getTokenSpelling();
Chris Lattner4c95a502018-06-23 16:03:42 -07001672 if (!consumeIf(Token::bare_identifier))
1673 return emitError("expected basic block name");
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001674
Chris Lattner48af7d12018-07-09 19:05:38 -07001675 auto *block = getBlockNamed(name, nameLoc);
Chris Lattner4c95a502018-06-23 16:03:42 -07001676
1677 // If this block has already been parsed, then this is a redefinition with the
1678 // same block name.
Chris Lattner3a467cc2018-07-01 20:28:00 -07001679 if (block->getFunction())
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001680 return emitError(nameLoc, "redefinition of block '" + name.str() + "'");
1681
Chris Lattner78276e32018-07-07 15:48:26 -07001682 // If an argument list is present, parse it.
1683 if (consumeIf(Token::l_paren)) {
James Molloy61a656c2018-07-22 15:45:24 -07001684 SmallVector<BBArgument *, 8> bbArgs;
1685 if (parseOptionalBasicBlockArgList(bbArgs, block))
Chris Lattner78276e32018-07-07 15:48:26 -07001686 return ParseFailure;
Chris Lattner40746442018-07-21 14:32:09 -07001687 if (!consumeIf(Token::r_paren))
1688 return emitError("expected ')' to end argument list");
Chris Lattner78276e32018-07-07 15:48:26 -07001689 }
Chris Lattner4c95a502018-06-23 16:03:42 -07001690
James Molloy61a656c2018-07-22 15:45:24 -07001691 // Add the block to the function.
1692 function->push_back(block);
1693
Chris Lattner4c95a502018-06-23 16:03:42 -07001694 if (!consumeIf(Token::colon))
1695 return emitError("expected ':' after basic block name");
1696
Chris Lattner158e0a3e2018-07-08 20:51:38 -07001697 // Set the insertion point to the block we want to insert new operations into.
Chris Lattner48af7d12018-07-09 19:05:38 -07001698 builder.setInsertionPoint(block);
Chris Lattner158e0a3e2018-07-08 20:51:38 -07001699
Chris Lattner7f9cc272018-07-19 08:35:28 -07001700 auto createOpFunc = [&](Identifier name, ArrayRef<SSAValue *> operands,
1701 ArrayRef<Type *> resultTypes,
1702 ArrayRef<NamedAttribute> attrs) -> Operation * {
1703 SmallVector<CFGValue *, 8> cfgOperands;
1704 cfgOperands.reserve(operands.size());
1705 for (auto *op : operands)
1706 cfgOperands.push_back(cast<CFGValue>(op));
1707 return builder.createOperation(name, cfgOperands, resultTypes, attrs);
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001708 };
1709
Chris Lattnered65a732018-06-28 20:45:33 -07001710 // Parse the list of operations that make up the body of the block.
Chris Lattner48af7d12018-07-09 19:05:38 -07001711 while (getToken().isNot(Token::kw_return, Token::kw_br)) {
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001712 if (parseOperation(createOpFunc))
Chris Lattnered65a732018-06-28 20:45:33 -07001713 return ParseFailure;
1714 }
Chris Lattner4c95a502018-06-23 16:03:42 -07001715
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001716 if (!parseTerminator())
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001717 return ParseFailure;
Chris Lattner4c95a502018-06-23 16:03:42 -07001718
1719 return ParseSuccess;
1720}
1721
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001722/// Parse the terminator instruction for a basic block.
1723///
1724/// terminator-stmt ::= `br` bb-id branch-use-list?
Chris Lattner1604e472018-07-23 08:42:19 -07001725/// branch-use-list ::= `(` ssa-use-list `)` ':' type-list-no-parens
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001726/// terminator-stmt ::=
1727/// `cond_br` ssa-use `,` bb-id branch-use-list? `,` bb-id branch-use-list?
1728/// terminator-stmt ::= `return` ssa-use-and-type-list?
1729///
Chris Lattner48af7d12018-07-09 19:05:38 -07001730TerminatorInst *CFGFunctionParser::parseTerminator() {
1731 switch (getToken().getKind()) {
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001732 default:
Chris Lattner3a467cc2018-07-01 20:28:00 -07001733 return (emitError("expected terminator at end of basic block"), nullptr);
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001734
Chris Lattner40746442018-07-21 14:32:09 -07001735 case Token::kw_return: {
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001736 consumeToken(Token::kw_return);
Chris Lattner40746442018-07-21 14:32:09 -07001737 SmallVector<CFGValue *, 8> results;
1738 if (parseOptionalSSAUseAndTypeList(results))
1739 return nullptr;
1740
1741 return builder.createReturnInst(results);
1742 }
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001743
1744 case Token::kw_br: {
1745 consumeToken(Token::kw_br);
Chris Lattner48af7d12018-07-09 19:05:38 -07001746 auto destBB = getBlockNamed(getTokenSpelling(), getToken().getLoc());
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001747 if (!consumeIf(Token::bare_identifier))
Chris Lattner3a467cc2018-07-01 20:28:00 -07001748 return (emitError("expected basic block name"), nullptr);
Chris Lattner1604e472018-07-23 08:42:19 -07001749 auto branch = builder.createBranchInst(destBB);
1750
1751 // Parse the use list.
1752 if (!consumeIf(Token::l_paren))
1753 return branch;
1754
1755 SmallVector<SSAUseInfo, 4> valueIDs;
1756 if (parseOptionalSSAUseList(valueIDs))
1757 return nullptr;
1758 if (!consumeIf(Token::r_paren))
1759 return (emitError("expected ')' in branch argument list"), nullptr);
1760 if (!consumeIf(Token::colon))
1761 return (emitError("expected ':' in branch argument list"), nullptr);
1762
1763 auto typeLoc = getToken().getLoc();
1764 SmallVector<Type *, 4> types;
1765 if (parseTypeListNoParens(types))
1766 return nullptr;
1767
1768 if (types.size() != valueIDs.size())
1769 return (emitError(typeLoc, "expected " + Twine(valueIDs.size()) +
1770 " types to match operand list"),
1771 nullptr);
1772
1773 SmallVector<CFGValue *, 4> values;
1774 values.reserve(valueIDs.size());
1775 for (unsigned i = 0, e = valueIDs.size(); i != e; ++i) {
1776 if (auto *value = resolveSSAUse(valueIDs[i], types[i]))
1777 values.push_back(cast<CFGValue>(value));
1778 else
1779 return nullptr;
1780 }
1781 branch->addOperands(values);
1782 return branch;
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001783 }
Chris Lattner78276e32018-07-07 15:48:26 -07001784 // TODO: cond_br.
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001785 }
1786}
1787
Chris Lattner48af7d12018-07-09 19:05:38 -07001788//===----------------------------------------------------------------------===//
1789// ML Functions
1790//===----------------------------------------------------------------------===//
1791
1792namespace {
1793/// Refined parser for MLFunction bodies.
Chris Lattner7f9cc272018-07-19 08:35:28 -07001794class MLFunctionParser : public FunctionParser {
Chris Lattner48af7d12018-07-09 19:05:38 -07001795public:
Chris Lattner48af7d12018-07-09 19:05:38 -07001796 MLFunctionParser(ParserState &state, MLFunction *function)
Chris Lattner7f9cc272018-07-19 08:35:28 -07001797 : FunctionParser(state), function(function), builder(function) {}
Chris Lattner48af7d12018-07-09 19:05:38 -07001798
1799 ParseResult parseFunctionBody();
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -07001800
1801private:
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001802 MLFunction *function;
1803
1804 /// This builder intentionally shadows the builder in the base class, with a
1805 /// more specific builder type.
1806 MLFuncBuilder builder;
1807
1808 ParseResult parseForStmt();
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001809 AffineConstantExpr *parseIntConstant();
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001810 ParseResult parseIfStmt();
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -07001811 ParseResult parseElseClause(IfClause *elseClause);
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001812 ParseResult parseStatements(StmtBlock *block);
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -07001813 ParseResult parseStmtBlock(StmtBlock *block);
Chris Lattner48af7d12018-07-09 19:05:38 -07001814};
1815} // end anonymous namespace
1816
Chris Lattner48af7d12018-07-09 19:05:38 -07001817ParseResult MLFunctionParser::parseFunctionBody() {
Chris Lattner40746442018-07-21 14:32:09 -07001818 auto braceLoc = getToken().getLoc();
Chris Lattner48af7d12018-07-09 19:05:38 -07001819 if (!consumeIf(Token::l_brace))
1820 return emitError("expected '{' in ML function");
1821
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001822 // Parse statements in this function
1823 if (parseStatements(function))
1824 return ParseFailure;
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -07001825
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001826 if (!consumeIf(Token::kw_return))
1827 emitError("ML function must end with return statement");
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001828
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001829 // TODO: store return operands in the IR.
1830 SmallVector<SSAUseInfo, 4> dummyUseInfo;
Chris Lattner40746442018-07-21 14:32:09 -07001831 if (parseOptionalSSAUseList(dummyUseInfo))
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001832 return ParseFailure;
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -07001833
Chris Lattner40746442018-07-21 14:32:09 -07001834 if (!consumeIf(Token::r_brace))
1835 return emitError("expected '}' to end mlfunc");
1836
Chris Lattner48af7d12018-07-09 19:05:38 -07001837 getModule()->functionList.push_back(function);
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -07001838
Chris Lattner40746442018-07-21 14:32:09 -07001839 return finalizeFunction(function, braceLoc);
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -07001840}
1841
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001842/// For statement.
1843///
Chris Lattner48af7d12018-07-09 19:05:38 -07001844/// ml-for-stmt ::= `for` ssa-id `=` lower-bound `to` upper-bound
1845/// (`step` integer-literal)? `{` ml-stmt* `}`
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001846///
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001847ParseResult MLFunctionParser::parseForStmt() {
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001848 consumeToken(Token::kw_for);
1849
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001850 // Parse induction variable
1851 if (getToken().isNot(Token::percent_identifier))
1852 return emitError("expected SSA identifier for the loop variable");
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001853
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001854 // TODO: create SSA value definition from name
1855 StringRef name = getTokenSpelling().drop_front();
1856 (void)name;
1857
1858 consumeToken(Token::percent_identifier);
1859
1860 if (!consumeIf(Token::equal))
1861 return emitError("expected =");
1862
1863 // Parse loop bounds
1864 AffineConstantExpr *lowerBound = parseIntConstant();
1865 if (!lowerBound)
1866 return ParseFailure;
1867
1868 if (!consumeIf(Token::kw_to))
1869 return emitError("expected 'to' between bounds");
1870
1871 AffineConstantExpr *upperBound = parseIntConstant();
1872 if (!upperBound)
1873 return ParseFailure;
1874
1875 // Parse step
1876 AffineConstantExpr *step = nullptr;
1877 if (consumeIf(Token::kw_step)) {
1878 step = parseIntConstant();
1879 if (!step)
1880 return ParseFailure;
1881 }
1882
1883 // Create for statement.
1884 ForStmt *stmt = builder.createFor(lowerBound, upperBound, step);
1885
1886 // If parsing of the for statement body fails,
1887 // MLIR contains for statement with those nested statements that have been
1888 // successfully parsed.
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001889 if (parseStmtBlock(static_cast<StmtBlock *>(stmt)))
1890 return ParseFailure;
1891
1892 return ParseSuccess;
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001893}
1894
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001895// This method is temporary workaround to parse simple loop bounds and
1896// step.
1897// TODO: remove this method once it's no longer used.
1898AffineConstantExpr *MLFunctionParser::parseIntConstant() {
1899 if (getToken().isNot(Token::integer))
1900 return (emitError("expected non-negative integer for now"), nullptr);
1901
1902 auto val = getToken().getUInt64IntegerValue();
1903 if (!val.hasValue() || (int64_t)val.getValue() < 0) {
1904 return (emitError("constant too large for affineint"), nullptr);
1905 }
1906 consumeToken(Token::integer);
1907 return builder.getConstantExpr((int64_t)val.getValue());
1908}
1909
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001910/// If statement.
1911///
Chris Lattner48af7d12018-07-09 19:05:38 -07001912/// ml-if-head ::= `if` ml-if-cond `{` ml-stmt* `}`
1913/// | ml-if-head `else` `if` ml-if-cond `{` ml-stmt* `}`
1914/// ml-if-stmt ::= ml-if-head
1915/// | ml-if-head `else` `{` ml-stmt* `}`
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001916///
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001917ParseResult MLFunctionParser::parseIfStmt() {
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001918 consumeToken(Token::kw_if);
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -07001919 if (!consumeIf(Token::l_paren))
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001920 return emitError("expected (");
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001921
1922 //TODO: parse condition
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -07001923
1924 if (!consumeIf(Token::r_paren))
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001925 return emitError("expected ')'");
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -07001926
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001927 IfStmt *ifStmt = builder.createIf();
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -07001928 IfClause *thenClause = ifStmt->getThenClause();
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001929
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07001930 // When parsing of an if statement body fails, the IR contains
1931 // the if statement with the portion of the body that has been
1932 // successfully parsed.
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001933 if (parseStmtBlock(thenClause))
1934 return ParseFailure;
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001935
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -07001936 if (consumeIf(Token::kw_else)) {
1937 IfClause *elseClause = ifStmt->createElseClause();
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001938 if (parseElseClause(elseClause))
1939 return ParseFailure;
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001940 }
1941
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001942 return ParseSuccess;
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -07001943}
1944
1945ParseResult MLFunctionParser::parseElseClause(IfClause *elseClause) {
1946 if (getToken().is(Token::kw_if)) {
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001947 builder.setInsertionPoint(elseClause);
1948 return parseIfStmt();
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -07001949 }
1950
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001951 return parseStmtBlock(elseClause);
1952}
1953
1954///
1955/// Parse a list of statements ending with `return` or `}`
1956///
1957ParseResult MLFunctionParser::parseStatements(StmtBlock *block) {
Chris Lattner7f9cc272018-07-19 08:35:28 -07001958 auto createOpFunc = [&](Identifier name, ArrayRef<SSAValue *> operands,
1959 ArrayRef<Type *> resultTypes,
1960 ArrayRef<NamedAttribute> attrs) -> Operation * {
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001961 return builder.createOperation(name, attrs);
1962 };
1963
1964 builder.setInsertionPoint(block);
1965
1966 while (getToken().isNot(Token::kw_return, Token::r_brace)) {
1967 switch (getToken().getKind()) {
1968 default:
1969 if (parseOperation(createOpFunc))
1970 return ParseFailure;
1971 break;
1972 case Token::kw_for:
1973 if (parseForStmt())
1974 return ParseFailure;
1975 break;
1976 case Token::kw_if:
1977 if (parseIfStmt())
1978 return ParseFailure;
1979 break;
1980 } // end switch
1981 }
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -07001982
1983 return ParseSuccess;
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001984}
1985
1986///
1987/// Parse `{` ml-stmt* `}`
1988///
Tatiana Shpeisman1bcfe982018-07-13 13:03:13 -07001989ParseResult MLFunctionParser::parseStmtBlock(StmtBlock *block) {
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001990 if (!consumeIf(Token::l_brace))
1991 return emitError("expected '{' before statement list");
1992
Tatiana Shpeisman565b9642018-07-16 11:47:09 -07001993 if (parseStatements(block))
1994 return ParseFailure;
1995
1996 if (!consumeIf(Token::r_brace))
1997 return emitError("expected '}' at the end of the statement block");
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001998
1999 return ParseSuccess;
2000}
2001
Chris Lattner4c95a502018-06-23 16:03:42 -07002002//===----------------------------------------------------------------------===//
2003// Top-level entity parsing.
2004//===----------------------------------------------------------------------===//
2005
Chris Lattner2e595eb2018-07-10 10:08:27 -07002006namespace {
2007/// This parser handles entities that are only valid at the top level of the
2008/// file.
2009class ModuleParser : public Parser {
2010public:
2011 explicit ModuleParser(ParserState &state) : Parser(state) {}
2012
2013 ParseResult parseModule();
2014
2015private:
2016 ParseResult parseAffineMapDef();
2017
2018 // Functions.
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07002019 ParseResult parseMLArgumentList(SmallVectorImpl<Type *> &argTypes,
2020 SmallVectorImpl<StringRef> &argNames);
2021 ParseResult parseFunctionSignature(StringRef &name, FunctionType *&type,
2022 SmallVectorImpl<StringRef> *argNames);
Chris Lattner2e595eb2018-07-10 10:08:27 -07002023 ParseResult parseExtFunc();
2024 ParseResult parseCFGFunc();
2025 ParseResult parseMLFunc();
2026};
2027} // end anonymous namespace
2028
2029/// Affine map declaration.
2030///
2031/// affine-map-def ::= affine-map-id `=` affine-map-inline
2032///
2033ParseResult ModuleParser::parseAffineMapDef() {
2034 assert(getToken().is(Token::hash_identifier));
2035
2036 StringRef affineMapId = getTokenSpelling().drop_front();
2037
2038 // Check for redefinitions.
2039 auto *&entry = getState().affineMapDefinitions[affineMapId];
2040 if (entry)
2041 return emitError("redefinition of affine map id '" + affineMapId + "'");
2042
2043 consumeToken(Token::hash_identifier);
2044
2045 // Parse the '='
2046 if (!consumeIf(Token::equal))
2047 return emitError("expected '=' in affine map outlined definition");
2048
2049 entry = parseAffineMapInline();
2050 if (!entry)
2051 return ParseFailure;
2052
Chris Lattner2e595eb2018-07-10 10:08:27 -07002053 return ParseSuccess;
2054}
2055
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07002056/// Parse a (possibly empty) list of MLFunction arguments with types.
2057///
2058/// ml-argument ::= ssa-id `:` type
2059/// ml-argument-list ::= ml-argument (`,` ml-argument)* | /*empty*/
2060///
2061ParseResult
2062ModuleParser::parseMLArgumentList(SmallVectorImpl<Type *> &argTypes,
2063 SmallVectorImpl<StringRef> &argNames) {
2064 auto parseElt = [&]() -> ParseResult {
2065 // Parse argument name
2066 if (getToken().isNot(Token::percent_identifier))
2067 return emitError("expected SSA identifier");
2068
2069 StringRef name = getTokenSpelling().drop_front();
2070 consumeToken(Token::percent_identifier);
2071 argNames.push_back(name);
2072
2073 if (!consumeIf(Token::colon))
2074 return emitError("expected ':'");
2075
2076 // Parse argument type
2077 auto elt = parseType();
2078 if (!elt)
2079 return ParseFailure;
2080 argTypes.push_back(elt);
2081
2082 return ParseSuccess;
2083 };
2084
2085 if (!consumeIf(Token::l_paren))
2086 llvm_unreachable("expected '('");
2087
Chris Lattner40746442018-07-21 14:32:09 -07002088 return parseCommaSeparatedListUntil(Token::r_paren, parseElt);
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07002089}
2090
Chris Lattner2e595eb2018-07-10 10:08:27 -07002091/// Parse a function signature, starting with a name and including the parameter
2092/// list.
2093///
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07002094/// argument-list ::= type (`,` type)* | /*empty*/ | ml-argument-list
Chris Lattner2e595eb2018-07-10 10:08:27 -07002095/// function-signature ::= function-id `(` argument-list `)` (`->` type-list)?
2096///
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07002097ParseResult
2098ModuleParser::parseFunctionSignature(StringRef &name, FunctionType *&type,
2099 SmallVectorImpl<StringRef> *argNames) {
Chris Lattner2e595eb2018-07-10 10:08:27 -07002100 if (getToken().isNot(Token::at_identifier))
2101 return emitError("expected a function identifier like '@foo'");
2102
2103 name = getTokenSpelling().drop_front();
2104 consumeToken(Token::at_identifier);
2105
2106 if (getToken().isNot(Token::l_paren))
2107 return emitError("expected '(' in function signature");
2108
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07002109 SmallVector<Type *, 4> argTypes;
2110 ParseResult parseResult;
2111
2112 if (argNames)
2113 parseResult = parseMLArgumentList(argTypes, *argNames);
2114 else
2115 parseResult = parseTypeList(argTypes);
2116
2117 if (parseResult)
Chris Lattner2e595eb2018-07-10 10:08:27 -07002118 return ParseFailure;
2119
2120 // Parse the return type if present.
2121 SmallVector<Type *, 4> results;
2122 if (consumeIf(Token::arrow)) {
2123 if (parseTypeList(results))
2124 return ParseFailure;
2125 }
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07002126 type = builder.getFunctionType(argTypes, results);
Chris Lattner2e595eb2018-07-10 10:08:27 -07002127 return ParseSuccess;
2128}
2129
2130/// External function declarations.
2131///
2132/// ext-func ::= `extfunc` function-signature
2133///
2134ParseResult ModuleParser::parseExtFunc() {
2135 consumeToken(Token::kw_extfunc);
2136
2137 StringRef name;
2138 FunctionType *type = nullptr;
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07002139 if (parseFunctionSignature(name, type, /*arguments*/ nullptr))
Chris Lattner2e595eb2018-07-10 10:08:27 -07002140 return ParseFailure;
2141
2142 // Okay, the external function definition was parsed correctly.
2143 getModule()->functionList.push_back(new ExtFunction(name, type));
2144 return ParseSuccess;
2145}
2146
2147/// CFG function declarations.
2148///
2149/// cfg-func ::= `cfgfunc` function-signature `{` basic-block+ `}`
2150///
2151ParseResult ModuleParser::parseCFGFunc() {
2152 consumeToken(Token::kw_cfgfunc);
2153
2154 StringRef name;
2155 FunctionType *type = nullptr;
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07002156 if (parseFunctionSignature(name, type, /*arguments*/ nullptr))
Chris Lattner2e595eb2018-07-10 10:08:27 -07002157 return ParseFailure;
2158
2159 // Okay, the CFG function signature was parsed correctly, create the function.
2160 auto function = new CFGFunction(name, type);
2161
2162 return CFGFunctionParser(getState(), function).parseFunctionBody();
2163}
2164
2165/// ML function declarations.
2166///
2167/// ml-func ::= `mlfunc` ml-func-signature `{` ml-stmt* ml-return-stmt `}`
2168///
2169ParseResult ModuleParser::parseMLFunc() {
2170 consumeToken(Token::kw_mlfunc);
2171
2172 StringRef name;
2173 FunctionType *type = nullptr;
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07002174 SmallVector<StringRef, 4> argNames;
Chris Lattner2e595eb2018-07-10 10:08:27 -07002175 // FIXME: Parse ML function signature (args + types)
2176 // by passing pointer to SmallVector<identifier> into parseFunctionSignature
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -07002177
2178 if (parseFunctionSignature(name, type, &argNames))
Chris Lattner2e595eb2018-07-10 10:08:27 -07002179 return ParseFailure;
2180
2181 // Okay, the ML function signature was parsed correctly, create the function.
2182 auto function = new MLFunction(name, type);
2183
2184 return MLFunctionParser(getState(), function).parseFunctionBody();
2185}
2186
Chris Lattnere79379a2018-06-22 10:39:19 -07002187/// This is the top-level module parser.
Chris Lattner2e595eb2018-07-10 10:08:27 -07002188ParseResult ModuleParser::parseModule() {
Chris Lattnere79379a2018-06-22 10:39:19 -07002189 while (1) {
Chris Lattner48af7d12018-07-09 19:05:38 -07002190 switch (getToken().getKind()) {
Chris Lattnere79379a2018-06-22 10:39:19 -07002191 default:
2192 emitError("expected a top level entity");
Chris Lattner2e595eb2018-07-10 10:08:27 -07002193 return ParseFailure;
Chris Lattnere79379a2018-06-22 10:39:19 -07002194
Uday Bondhugula015cbb12018-07-03 20:16:08 -07002195 // If we got to the end of the file, then we're done.
Chris Lattnere79379a2018-06-22 10:39:19 -07002196 case Token::eof:
Chris Lattner2e595eb2018-07-10 10:08:27 -07002197 return ParseSuccess;
Chris Lattnere79379a2018-06-22 10:39:19 -07002198
2199 // If we got an error token, then the lexer already emitted an error, just
2200 // stop. Someday we could introduce error recovery if there was demand for
2201 // it.
2202 case Token::error:
Chris Lattner2e595eb2018-07-10 10:08:27 -07002203 return ParseFailure;
2204
2205 case Token::hash_identifier:
2206 if (parseAffineMapDef())
2207 return ParseFailure;
2208 break;
Chris Lattnere79379a2018-06-22 10:39:19 -07002209
2210 case Token::kw_extfunc:
Chris Lattner2e595eb2018-07-10 10:08:27 -07002211 if (parseExtFunc())
2212 return ParseFailure;
Chris Lattnere79379a2018-06-22 10:39:19 -07002213 break;
2214
Chris Lattner4c95a502018-06-23 16:03:42 -07002215 case Token::kw_cfgfunc:
Chris Lattner2e595eb2018-07-10 10:08:27 -07002216 if (parseCFGFunc())
2217 return ParseFailure;
MLIR Teamf85a6262018-06-27 11:03:08 -07002218 break;
Chris Lattner4c95a502018-06-23 16:03:42 -07002219
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -07002220 case Token::kw_mlfunc:
Chris Lattner2e595eb2018-07-10 10:08:27 -07002221 if (parseMLFunc())
2222 return ParseFailure;
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -07002223 break;
2224
Uday Bondhugula015cbb12018-07-03 20:16:08 -07002225 // TODO: affine entity declarations, etc.
Chris Lattnere79379a2018-06-22 10:39:19 -07002226 }
2227 }
2228}
2229
2230//===----------------------------------------------------------------------===//
2231
Jacques Pienaar7b829702018-07-03 13:24:09 -07002232void mlir::defaultErrorReporter(const llvm::SMDiagnostic &error) {
2233 const auto &sourceMgr = *error.getSourceMgr();
2234 sourceMgr.PrintMessage(error.getLoc(), error.getKind(), error.getMessage());
2235}
2236
Chris Lattnere79379a2018-06-22 10:39:19 -07002237/// This parses the file specified by the indicated SourceMgr and returns an
2238/// MLIR module if it was valid. If not, it emits diagnostics and returns null.
Jacques Pienaar9c411be2018-06-24 19:17:35 -07002239Module *mlir::parseSourceFile(llvm::SourceMgr &sourceMgr, MLIRContext *context,
Jacques Pienaar7b829702018-07-03 13:24:09 -07002240 SMDiagnosticHandlerTy errorReporter) {
Chris Lattner2e595eb2018-07-10 10:08:27 -07002241 // This is the result module we are parsing into.
2242 std::unique_ptr<Module> module(new Module(context));
2243
2244 ParserState state(sourceMgr, module.get(),
Jacques Pienaar0bffd862018-07-11 13:26:23 -07002245 errorReporter ? errorReporter : defaultErrorReporter);
Chris Lattner2e595eb2018-07-10 10:08:27 -07002246 if (ModuleParser(state).parseModule())
2247 return nullptr;
Chris Lattner21e67f62018-07-06 10:46:19 -07002248
2249 // Make sure the parse module has no other structural problems detected by the
2250 // verifier.
Chris Lattner2e595eb2018-07-10 10:08:27 -07002251 module->verify();
2252 return module.release();
Chris Lattnere79379a2018-06-22 10:39:19 -07002253}