blob: 407c7a386350eba6fec9c5205a76bddc413ce8b9 [file] [log] [blame]
Chris Lattnere79379a2018-06-22 10:39:19 -07001//===- Parser.cpp - MLIR Parser Implementation ----------------------------===//
2//
3// Copyright 2019 The MLIR Authors.
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16// =============================================================================
17//
18// This file implements the parser for the MLIR textual form.
19//
20//===----------------------------------------------------------------------===//
21
22#include "mlir/Parser.h"
23#include "Lexer.h"
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -070024#include "mlir/IR/AffineExpr.h"
MLIR Teamf85a6262018-06-27 11:03:08 -070025#include "mlir/IR/AffineMap.h"
Chris Lattner7121b802018-07-04 20:45:39 -070026#include "mlir/IR/Attributes.h"
Chris Lattner158e0a3e2018-07-08 20:51:38 -070027#include "mlir/IR/Builders.h"
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -070028#include "mlir/IR/MLFunction.h"
Chris Lattner21e67f62018-07-06 10:46:19 -070029#include "mlir/IR/Module.h"
30#include "mlir/IR/OperationSet.h"
Chris Lattnerf7e22732018-06-22 22:03:48 -070031#include "mlir/IR/Types.h"
Chris Lattnere79379a2018-06-22 10:39:19 -070032#include "llvm/Support/SourceMgr.h"
33using namespace mlir;
34using llvm::SourceMgr;
Chris Lattner4c95a502018-06-23 16:03:42 -070035using llvm::SMLoc;
Chris Lattnere79379a2018-06-22 10:39:19 -070036
Chris Lattnerf7e22732018-06-22 22:03:48 -070037/// Simple enum to make code read better in cases that would otherwise return a
38/// bool value. Failure is "true" in a boolean context.
Chris Lattnere79379a2018-06-22 10:39:19 -070039enum ParseResult {
40 ParseSuccess,
41 ParseFailure
42};
43
Chris Lattner48af7d12018-07-09 19:05:38 -070044namespace {
45class Parser;
46
47/// This class refers to all of the state maintained globally by the parser,
48/// such as the current lexer position etc. The Parser base class provides
49/// methods to access this.
50class ParserState {
Chris Lattnered65a732018-06-28 20:45:33 -070051public:
Chris Lattner2e595eb2018-07-10 10:08:27 -070052 ParserState(llvm::SourceMgr &sourceMgr, Module *module,
Chris Lattner48af7d12018-07-09 19:05:38 -070053 SMDiagnosticHandlerTy errorReporter)
Chris Lattner2e595eb2018-07-10 10:08:27 -070054 : context(module->getContext()), module(module),
55 lex(sourceMgr, errorReporter), curToken(lex.lexToken()),
56 errorReporter(std::move(errorReporter)) {}
57
58 // A map from affine map identifier to AffineMap.
59 llvm::StringMap<AffineMap *> affineMapDefinitions;
Chris Lattnere79379a2018-06-22 10:39:19 -070060
Chris Lattnere79379a2018-06-22 10:39:19 -070061private:
Chris Lattner48af7d12018-07-09 19:05:38 -070062 ParserState(const ParserState &) = delete;
63 void operator=(const ParserState &) = delete;
64
65 friend class Parser;
66
67 // The context we're parsing into.
Chris Lattner2e595eb2018-07-10 10:08:27 -070068 MLIRContext *const context;
69
70 // This is the module we are parsing into.
71 Module *const module;
Chris Lattnerf7e22732018-06-22 22:03:48 -070072
73 // The lexer for the source file we're parsing.
Chris Lattnere79379a2018-06-22 10:39:19 -070074 Lexer lex;
75
76 // This is the next token that hasn't been consumed yet.
77 Token curToken;
78
Jacques Pienaar9c411be2018-06-24 19:17:35 -070079 // The diagnostic error reporter.
Chris Lattner2e595eb2018-07-10 10:08:27 -070080 SMDiagnosticHandlerTy const errorReporter;
Chris Lattner48af7d12018-07-09 19:05:38 -070081};
82} // end anonymous namespace
MLIR Teamf85a6262018-06-27 11:03:08 -070083
Chris Lattner48af7d12018-07-09 19:05:38 -070084namespace {
85
86/// This class implement support for parsing global entities like types and
87/// shared entities like SSA names. It is intended to be subclassed by
88/// specialized subparsers that include state, e.g. when a local symbol table.
89class Parser {
90public:
Chris Lattner2e595eb2018-07-10 10:08:27 -070091 Builder builder;
Chris Lattner48af7d12018-07-09 19:05:38 -070092
Chris Lattner2e595eb2018-07-10 10:08:27 -070093 Parser(ParserState &state) : builder(state.context), state(state) {}
94
95 // Helper methods to get stuff from the parser-global state.
96 ParserState &getState() const { return state; }
Chris Lattner48af7d12018-07-09 19:05:38 -070097 MLIRContext *getContext() const { return state.context; }
Chris Lattner2e595eb2018-07-10 10:08:27 -070098 Module *getModule() { return state.module; }
Chris Lattner48af7d12018-07-09 19:05:38 -070099
100 /// Return the current token the parser is inspecting.
101 const Token &getToken() const { return state.curToken; }
102 StringRef getTokenSpelling() const { return state.curToken.getSpelling(); }
Chris Lattnere79379a2018-06-22 10:39:19 -0700103
104 /// Emit an error and return failure.
Chris Lattner4c95a502018-06-23 16:03:42 -0700105 ParseResult emitError(const Twine &message) {
Chris Lattner48af7d12018-07-09 19:05:38 -0700106 return emitError(state.curToken.getLoc(), message);
Chris Lattner4c95a502018-06-23 16:03:42 -0700107 }
108 ParseResult emitError(SMLoc loc, const Twine &message);
Chris Lattnere79379a2018-06-22 10:39:19 -0700109
110 /// Advance the current lexer onto the next token.
111 void consumeToken() {
Chris Lattner48af7d12018-07-09 19:05:38 -0700112 assert(state.curToken.isNot(Token::eof, Token::error) &&
Chris Lattnere79379a2018-06-22 10:39:19 -0700113 "shouldn't advance past EOF or errors");
Chris Lattner48af7d12018-07-09 19:05:38 -0700114 state.curToken = state.lex.lexToken();
Chris Lattnere79379a2018-06-22 10:39:19 -0700115 }
116
117 /// Advance the current lexer onto the next token, asserting what the expected
118 /// current token is. This is preferred to the above method because it leads
119 /// to more self-documenting code with better checking.
Chris Lattner8da0c282018-06-29 11:15:56 -0700120 void consumeToken(Token::Kind kind) {
Chris Lattner48af7d12018-07-09 19:05:38 -0700121 assert(state.curToken.is(kind) && "consumed an unexpected token");
Chris Lattnere79379a2018-06-22 10:39:19 -0700122 consumeToken();
123 }
124
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700125 /// If the current token has the specified kind, consume it and return true.
126 /// If not, return false.
Chris Lattner8da0c282018-06-29 11:15:56 -0700127 bool consumeIf(Token::Kind kind) {
Chris Lattner48af7d12018-07-09 19:05:38 -0700128 if (state.curToken.isNot(kind))
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700129 return false;
130 consumeToken(kind);
131 return true;
132 }
133
Chris Lattner8da0c282018-06-29 11:15:56 -0700134 ParseResult parseCommaSeparatedList(Token::Kind rightToken,
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700135 const std::function<ParseResult()> &parseElement,
136 bool allowEmptyList = true);
137
Chris Lattnerf7e22732018-06-22 22:03:48 -0700138 // We have two forms of parsing methods - those that return a non-null
139 // pointer on success, and those that return a ParseResult to indicate whether
140 // they returned a failure. The second class fills in by-reference arguments
141 // as the results of their action.
142
Chris Lattnere79379a2018-06-22 10:39:19 -0700143 // Type parsing.
Chris Lattnerf958bbe2018-06-29 22:08:05 -0700144 Type *parsePrimitiveType();
Chris Lattnerf7e22732018-06-22 22:03:48 -0700145 Type *parseElementType();
146 VectorType *parseVectorType();
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700147 ParseResult parseDimensionListRanked(SmallVectorImpl<int> &dimensions);
Chris Lattnerf7e22732018-06-22 22:03:48 -0700148 Type *parseTensorType();
149 Type *parseMemRefType();
150 Type *parseFunctionType();
151 Type *parseType();
152 ParseResult parseTypeList(SmallVectorImpl<Type*> &elements);
Chris Lattnere79379a2018-06-22 10:39:19 -0700153
Chris Lattner7121b802018-07-04 20:45:39 -0700154 // Attribute parsing.
155 Attribute *parseAttribute();
156 ParseResult parseAttributeDict(SmallVectorImpl<NamedAttribute> &attributes);
157
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700158 // Polyhedral structures.
Chris Lattner2e595eb2018-07-10 10:08:27 -0700159 AffineMap *parseAffineMapInline();
MLIR Teamf85a6262018-06-27 11:03:08 -0700160
Chris Lattner78276e32018-07-07 15:48:26 -0700161 // SSA
162 ParseResult parseSSAUse();
163 ParseResult parseOptionalSSAUseList(Token::Kind endToken);
164 ParseResult parseSSAUseAndType();
165 ParseResult parseOptionalSSAUseAndTypeList(Token::Kind endToken);
166
Chris Lattner48af7d12018-07-09 19:05:38 -0700167private:
168 // The Parser is subclassed and reinstantiated. Do not add additional
169 // non-trivial state here, add it to the ParserState class.
170 ParserState &state;
Chris Lattnere79379a2018-06-22 10:39:19 -0700171};
172} // end anonymous namespace
173
174//===----------------------------------------------------------------------===//
175// Helper methods.
176//===----------------------------------------------------------------------===//
177
Chris Lattner4c95a502018-06-23 16:03:42 -0700178ParseResult Parser::emitError(SMLoc loc, const Twine &message) {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700179 // If we hit a parse error in response to a lexer error, then the lexer
Jacques Pienaar9c411be2018-06-24 19:17:35 -0700180 // already reported the error.
Chris Lattner48af7d12018-07-09 19:05:38 -0700181 if (getToken().is(Token::error))
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700182 return ParseFailure;
183
Chris Lattner48af7d12018-07-09 19:05:38 -0700184 auto &sourceMgr = state.lex.getSourceMgr();
185 state.errorReporter(sourceMgr.GetMessage(loc, SourceMgr::DK_Error, message));
Chris Lattnere79379a2018-06-22 10:39:19 -0700186 return ParseFailure;
187}
188
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700189/// Parse a comma-separated list of elements, terminated with an arbitrary
190/// token. This allows empty lists if allowEmptyList is true.
191///
192/// abstract-list ::= rightToken // if allowEmptyList == true
193/// abstract-list ::= element (',' element)* rightToken
194///
195ParseResult Parser::
Chris Lattner8da0c282018-06-29 11:15:56 -0700196parseCommaSeparatedList(Token::Kind rightToken,
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700197 const std::function<ParseResult()> &parseElement,
198 bool allowEmptyList) {
199 // Handle the empty case.
Chris Lattner48af7d12018-07-09 19:05:38 -0700200 if (getToken().is(rightToken)) {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700201 if (!allowEmptyList)
202 return emitError("expected list element");
203 consumeToken(rightToken);
204 return ParseSuccess;
205 }
206
207 // Non-empty case starts with an element.
208 if (parseElement())
209 return ParseFailure;
210
211 // Otherwise we have a list of comma separated elements.
212 while (consumeIf(Token::comma)) {
213 if (parseElement())
214 return ParseFailure;
215 }
216
217 // Consume the end character.
218 if (!consumeIf(rightToken))
Chris Lattner8da0c282018-06-29 11:15:56 -0700219 return emitError("expected ',' or '" + Token::getTokenSpelling(rightToken) +
220 "'");
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700221
222 return ParseSuccess;
223}
Chris Lattnere79379a2018-06-22 10:39:19 -0700224
225//===----------------------------------------------------------------------===//
226// Type Parsing
227//===----------------------------------------------------------------------===//
228
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700229/// Parse the low-level fixed dtypes in the system.
230///
Chris Lattnerf958bbe2018-06-29 22:08:05 -0700231/// primitive-type ::= `f16` | `bf16` | `f32` | `f64`
232/// primitive-type ::= integer-type
233/// primitive-type ::= `affineint`
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700234///
Chris Lattnerf958bbe2018-06-29 22:08:05 -0700235Type *Parser::parsePrimitiveType() {
Chris Lattner48af7d12018-07-09 19:05:38 -0700236 switch (getToken().getKind()) {
Chris Lattnerf7e22732018-06-22 22:03:48 -0700237 default:
238 return (emitError("expected type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700239 case Token::kw_bf16:
240 consumeToken(Token::kw_bf16);
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700241 return builder.getBF16Type();
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700242 case Token::kw_f16:
243 consumeToken(Token::kw_f16);
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700244 return builder.getF16Type();
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700245 case Token::kw_f32:
246 consumeToken(Token::kw_f32);
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700247 return builder.getF32Type();
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700248 case Token::kw_f64:
249 consumeToken(Token::kw_f64);
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700250 return builder.getF64Type();
Chris Lattnerf958bbe2018-06-29 22:08:05 -0700251 case Token::kw_affineint:
252 consumeToken(Token::kw_affineint);
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700253 return builder.getAffineIntType();
Chris Lattnerf958bbe2018-06-29 22:08:05 -0700254 case Token::inttype: {
Chris Lattner48af7d12018-07-09 19:05:38 -0700255 auto width = getToken().getIntTypeBitwidth();
Chris Lattnerf958bbe2018-06-29 22:08:05 -0700256 if (!width.hasValue())
257 return (emitError("invalid integer width"), nullptr);
258 consumeToken(Token::inttype);
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700259 return builder.getIntegerType(width.getValue());
Chris Lattnerf958bbe2018-06-29 22:08:05 -0700260 }
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700261 }
262}
263
264/// Parse the element type of a tensor or memref type.
265///
266/// element-type ::= primitive-type | vector-type
267///
Chris Lattnerf7e22732018-06-22 22:03:48 -0700268Type *Parser::parseElementType() {
Chris Lattner48af7d12018-07-09 19:05:38 -0700269 if (getToken().is(Token::kw_vector))
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700270 return parseVectorType();
271
272 return parsePrimitiveType();
273}
274
275/// Parse a vector type.
276///
277/// vector-type ::= `vector` `<` const-dimension-list primitive-type `>`
278/// const-dimension-list ::= (integer-literal `x`)+
279///
Chris Lattnerf7e22732018-06-22 22:03:48 -0700280VectorType *Parser::parseVectorType() {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700281 consumeToken(Token::kw_vector);
282
283 if (!consumeIf(Token::less))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700284 return (emitError("expected '<' in vector type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700285
Chris Lattner48af7d12018-07-09 19:05:38 -0700286 if (getToken().isNot(Token::integer))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700287 return (emitError("expected dimension size in vector type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700288
289 SmallVector<unsigned, 4> dimensions;
Chris Lattner48af7d12018-07-09 19:05:38 -0700290 while (getToken().is(Token::integer)) {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700291 // Make sure this integer value is in bound and valid.
Chris Lattner48af7d12018-07-09 19:05:38 -0700292 auto dimension = getToken().getUnsignedIntegerValue();
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700293 if (!dimension.hasValue())
Chris Lattnerf7e22732018-06-22 22:03:48 -0700294 return (emitError("invalid dimension in vector type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700295 dimensions.push_back(dimension.getValue());
296
297 consumeToken(Token::integer);
298
299 // Make sure we have an 'x' or something like 'xbf32'.
Chris Lattner48af7d12018-07-09 19:05:38 -0700300 if (getToken().isNot(Token::bare_identifier) ||
301 getTokenSpelling()[0] != 'x')
Chris Lattnerf7e22732018-06-22 22:03:48 -0700302 return (emitError("expected 'x' in vector dimension list"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700303
304 // If we had a prefix of 'x', lex the next token immediately after the 'x'.
Chris Lattner48af7d12018-07-09 19:05:38 -0700305 if (getTokenSpelling().size() != 1)
306 state.lex.resetPointer(getTokenSpelling().data() + 1);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700307
308 // Consume the 'x'.
309 consumeToken(Token::bare_identifier);
310 }
311
312 // Parse the element type.
Chris Lattnerf7e22732018-06-22 22:03:48 -0700313 auto *elementType = parsePrimitiveType();
314 if (!elementType)
315 return nullptr;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700316
317 if (!consumeIf(Token::greater))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700318 return (emitError("expected '>' in vector type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700319
Chris Lattnerf7e22732018-06-22 22:03:48 -0700320 return VectorType::get(dimensions, elementType);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700321}
322
323/// Parse a dimension list of a tensor or memref type. This populates the
324/// dimension list, returning -1 for the '?' dimensions.
325///
326/// dimension-list-ranked ::= (dimension `x`)*
327/// dimension ::= `?` | integer-literal
328///
329ParseResult Parser::parseDimensionListRanked(SmallVectorImpl<int> &dimensions) {
Chris Lattner48af7d12018-07-09 19:05:38 -0700330 while (getToken().isAny(Token::integer, Token::question)) {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700331 if (consumeIf(Token::question)) {
332 dimensions.push_back(-1);
333 } else {
334 // Make sure this integer value is in bound and valid.
Chris Lattner48af7d12018-07-09 19:05:38 -0700335 auto dimension = getToken().getUnsignedIntegerValue();
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700336 if (!dimension.hasValue() || (int)dimension.getValue() < 0)
337 return emitError("invalid dimension");
338 dimensions.push_back((int)dimension.getValue());
339 consumeToken(Token::integer);
340 }
341
342 // Make sure we have an 'x' or something like 'xbf32'.
Chris Lattner48af7d12018-07-09 19:05:38 -0700343 if (getToken().isNot(Token::bare_identifier) ||
344 getTokenSpelling()[0] != 'x')
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700345 return emitError("expected 'x' in dimension list");
346
347 // If we had a prefix of 'x', lex the next token immediately after the 'x'.
Chris Lattner48af7d12018-07-09 19:05:38 -0700348 if (getTokenSpelling().size() != 1)
349 state.lex.resetPointer(getTokenSpelling().data() + 1);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700350
351 // Consume the 'x'.
352 consumeToken(Token::bare_identifier);
353 }
354
355 return ParseSuccess;
356}
357
358/// Parse a tensor type.
359///
360/// tensor-type ::= `tensor` `<` dimension-list element-type `>`
361/// dimension-list ::= dimension-list-ranked | `??`
362///
Chris Lattnerf7e22732018-06-22 22:03:48 -0700363Type *Parser::parseTensorType() {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700364 consumeToken(Token::kw_tensor);
365
366 if (!consumeIf(Token::less))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700367 return (emitError("expected '<' in tensor type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700368
369 bool isUnranked;
370 SmallVector<int, 4> dimensions;
371
372 if (consumeIf(Token::questionquestion)) {
373 isUnranked = true;
374 } else {
375 isUnranked = false;
376 if (parseDimensionListRanked(dimensions))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700377 return nullptr;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700378 }
379
380 // Parse the element type.
Chris Lattnerf7e22732018-06-22 22:03:48 -0700381 auto elementType = parseElementType();
382 if (!elementType)
383 return nullptr;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700384
385 if (!consumeIf(Token::greater))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700386 return (emitError("expected '>' in tensor type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700387
MLIR Team355ec862018-06-23 18:09:09 -0700388 if (isUnranked)
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700389 return builder.getTensorType(elementType);
390 return builder.getTensorType(dimensions, elementType);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700391}
392
393/// Parse a memref type.
394///
395/// memref-type ::= `memref` `<` dimension-list-ranked element-type
396/// (`,` semi-affine-map-composition)? (`,` memory-space)? `>`
397///
398/// semi-affine-map-composition ::= (semi-affine-map `,` )* semi-affine-map
399/// memory-space ::= integer-literal /* | TODO: address-space-id */
400///
Chris Lattnerf7e22732018-06-22 22:03:48 -0700401Type *Parser::parseMemRefType() {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700402 consumeToken(Token::kw_memref);
403
404 if (!consumeIf(Token::less))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700405 return (emitError("expected '<' in memref type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700406
407 SmallVector<int, 4> dimensions;
408 if (parseDimensionListRanked(dimensions))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700409 return nullptr;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700410
411 // Parse the element type.
Chris Lattnerf7e22732018-06-22 22:03:48 -0700412 auto elementType = parseElementType();
413 if (!elementType)
414 return nullptr;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700415
416 // TODO: Parse semi-affine-map-composition.
417 // TODO: Parse memory-space.
418
419 if (!consumeIf(Token::greater))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700420 return (emitError("expected '>' in memref type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700421
Chris Lattnerf7e22732018-06-22 22:03:48 -0700422 // FIXME: Add an IR representation for memref types.
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700423 return builder.getIntegerType(1);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700424}
425
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700426/// Parse a function type.
427///
428/// function-type ::= type-list-parens `->` type-list
429///
Chris Lattnerf7e22732018-06-22 22:03:48 -0700430Type *Parser::parseFunctionType() {
Chris Lattner48af7d12018-07-09 19:05:38 -0700431 assert(getToken().is(Token::l_paren));
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700432
Chris Lattnerf7e22732018-06-22 22:03:48 -0700433 SmallVector<Type*, 4> arguments;
434 if (parseTypeList(arguments))
435 return nullptr;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700436
437 if (!consumeIf(Token::arrow))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700438 return (emitError("expected '->' in function type"), nullptr);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700439
Chris Lattnerf7e22732018-06-22 22:03:48 -0700440 SmallVector<Type*, 4> results;
441 if (parseTypeList(results))
442 return nullptr;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700443
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700444 return builder.getFunctionType(arguments, results);
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700445}
446
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700447/// Parse an arbitrary type.
448///
449/// type ::= primitive-type
450/// | vector-type
451/// | tensor-type
452/// | memref-type
453/// | function-type
454/// element-type ::= primitive-type | vector-type
455///
Chris Lattnerf7e22732018-06-22 22:03:48 -0700456Type *Parser::parseType() {
Chris Lattner48af7d12018-07-09 19:05:38 -0700457 switch (getToken().getKind()) {
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700458 case Token::kw_memref: return parseMemRefType();
459 case Token::kw_tensor: return parseTensorType();
460 case Token::kw_vector: return parseVectorType();
461 case Token::l_paren: return parseFunctionType();
462 default:
463 return parsePrimitiveType();
464 }
465}
466
467/// Parse a "type list", which is a singular type, or a parenthesized list of
468/// types.
469///
470/// type-list ::= type-list-parens | type
471/// type-list-parens ::= `(` `)`
472/// | `(` type (`,` type)* `)`
473///
Chris Lattnerf7e22732018-06-22 22:03:48 -0700474ParseResult Parser::parseTypeList(SmallVectorImpl<Type*> &elements) {
475 auto parseElt = [&]() -> ParseResult {
476 auto elt = parseType();
477 elements.push_back(elt);
478 return elt ? ParseSuccess : ParseFailure;
479 };
480
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700481 // If there is no parens, then it must be a singular type.
482 if (!consumeIf(Token::l_paren))
Chris Lattnerf7e22732018-06-22 22:03:48 -0700483 return parseElt();
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700484
Chris Lattnerf7e22732018-06-22 22:03:48 -0700485 if (parseCommaSeparatedList(Token::r_paren, parseElt))
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700486 return ParseFailure;
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700487
Chris Lattnerbb8fafc2018-06-22 15:52:02 -0700488 return ParseSuccess;
489}
490
Chris Lattner4c95a502018-06-23 16:03:42 -0700491//===----------------------------------------------------------------------===//
Chris Lattner7121b802018-07-04 20:45:39 -0700492// Attribute parsing.
493//===----------------------------------------------------------------------===//
494
495
496/// Attribute parsing.
497///
498/// attribute-value ::= bool-literal
499/// | integer-literal
500/// | float-literal
501/// | string-literal
502/// | `[` (attribute-value (`,` attribute-value)*)? `]`
503///
504Attribute *Parser::parseAttribute() {
Chris Lattner48af7d12018-07-09 19:05:38 -0700505 switch (getToken().getKind()) {
Chris Lattner7121b802018-07-04 20:45:39 -0700506 case Token::kw_true:
507 consumeToken(Token::kw_true);
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700508 return BoolAttr::get(true, builder.getContext());
Chris Lattner7121b802018-07-04 20:45:39 -0700509 case Token::kw_false:
510 consumeToken(Token::kw_false);
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700511 return BoolAttr::get(false, builder.getContext());
Chris Lattner7121b802018-07-04 20:45:39 -0700512
513 case Token::integer: {
Chris Lattner48af7d12018-07-09 19:05:38 -0700514 auto val = getToken().getUInt64IntegerValue();
Chris Lattner7121b802018-07-04 20:45:39 -0700515 if (!val.hasValue() || (int64_t)val.getValue() < 0)
516 return (emitError("integer too large for attribute"), nullptr);
517 consumeToken(Token::integer);
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700518 return IntegerAttr::get((int64_t)val.getValue(), builder.getContext());
Chris Lattner7121b802018-07-04 20:45:39 -0700519 }
520
521 case Token::minus: {
522 consumeToken(Token::minus);
Chris Lattner48af7d12018-07-09 19:05:38 -0700523 if (getToken().is(Token::integer)) {
524 auto val = getToken().getUInt64IntegerValue();
Chris Lattner7121b802018-07-04 20:45:39 -0700525 if (!val.hasValue() || (int64_t)-val.getValue() >= 0)
526 return (emitError("integer too large for attribute"), nullptr);
527 consumeToken(Token::integer);
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700528 return IntegerAttr::get((int64_t)-val.getValue(), builder.getContext());
Chris Lattner7121b802018-07-04 20:45:39 -0700529 }
530
531 return (emitError("expected constant integer or floating point value"),
532 nullptr);
533 }
534
535 case Token::string: {
Chris Lattner48af7d12018-07-09 19:05:38 -0700536 auto val = getToken().getStringValue();
Chris Lattner7121b802018-07-04 20:45:39 -0700537 consumeToken(Token::string);
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700538 return StringAttr::get(val, builder.getContext());
Chris Lattner7121b802018-07-04 20:45:39 -0700539 }
540
541 case Token::l_bracket: {
542 consumeToken(Token::l_bracket);
543 SmallVector<Attribute*, 4> elements;
544
545 auto parseElt = [&]() -> ParseResult {
546 elements.push_back(parseAttribute());
547 return elements.back() ? ParseSuccess : ParseFailure;
548 };
549
550 if (parseCommaSeparatedList(Token::r_bracket, parseElt))
551 return nullptr;
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700552 return ArrayAttr::get(elements, builder.getContext());
Chris Lattner7121b802018-07-04 20:45:39 -0700553 }
554 default:
555 // TODO: Handle floating point.
556 return (emitError("expected constant attribute value"), nullptr);
557 }
558}
559
Chris Lattner7121b802018-07-04 20:45:39 -0700560/// Attribute dictionary.
561///
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700562/// attribute-dict ::= `{` `}`
563/// | `{` attribute-entry (`,` attribute-entry)* `}`
564/// attribute-entry ::= bare-id `:` attribute-value
Chris Lattner7121b802018-07-04 20:45:39 -0700565///
566ParseResult Parser::parseAttributeDict(
567 SmallVectorImpl<NamedAttribute> &attributes) {
568 consumeToken(Token::l_brace);
569
570 auto parseElt = [&]() -> ParseResult {
571 // We allow keywords as attribute names.
Chris Lattner48af7d12018-07-09 19:05:38 -0700572 if (getToken().isNot(Token::bare_identifier, Token::inttype) &&
573 !getToken().isKeyword())
Chris Lattner7121b802018-07-04 20:45:39 -0700574 return emitError("expected attribute name");
Chris Lattner48af7d12018-07-09 19:05:38 -0700575 auto nameId = Identifier::get(getTokenSpelling(), builder.getContext());
Chris Lattner7121b802018-07-04 20:45:39 -0700576 consumeToken();
577
578 if (!consumeIf(Token::colon))
579 return emitError("expected ':' in attribute list");
580
581 auto attr = parseAttribute();
582 if (!attr) return ParseFailure;
583
584 attributes.push_back({nameId, attr});
585 return ParseSuccess;
586 };
587
588 if (parseCommaSeparatedList(Token::r_brace, parseElt))
589 return ParseFailure;
590
591 return ParseSuccess;
592}
593
594//===----------------------------------------------------------------------===//
MLIR Teamf85a6262018-06-27 11:03:08 -0700595// Polyhedral structures.
596//===----------------------------------------------------------------------===//
597
Chris Lattner2e595eb2018-07-10 10:08:27 -0700598/// Lower precedence ops (all at the same precedence level). LNoOp is false in
599/// the boolean sense.
600enum AffineLowPrecOp {
601 /// Null value.
602 LNoOp,
603 Add,
604 Sub
605};
MLIR Teamf85a6262018-06-27 11:03:08 -0700606
Chris Lattner2e595eb2018-07-10 10:08:27 -0700607/// Higher precedence ops - all at the same precedence level. HNoOp is false in
608/// the boolean sense.
609enum AffineHighPrecOp {
610 /// Null value.
611 HNoOp,
612 Mul,
613 FloorDiv,
614 CeilDiv,
615 Mod
616};
Chris Lattner7121b802018-07-04 20:45:39 -0700617
Chris Lattner2e595eb2018-07-10 10:08:27 -0700618namespace {
619/// This is a specialized parser for AffineMap's, maintaining the state
620/// transient to their bodies.
621class AffineMapParser : public Parser {
622public:
623 explicit AffineMapParser(ParserState &state) : Parser(state) {}
Chris Lattner7121b802018-07-04 20:45:39 -0700624
Chris Lattner2e595eb2018-07-10 10:08:27 -0700625 AffineMap *parseAffineMapInline();
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700626
Chris Lattner2e595eb2018-07-10 10:08:27 -0700627private:
628 unsigned getNumDims() const { return dims.size(); }
629 unsigned getNumSymbols() const { return symbols.size(); }
MLIR Teamf85a6262018-06-27 11:03:08 -0700630
Chris Lattner2e595eb2018-07-10 10:08:27 -0700631 // Binary affine op parsing.
632 AffineLowPrecOp consumeIfLowPrecOp();
633 AffineHighPrecOp consumeIfHighPrecOp();
MLIR Teamf85a6262018-06-27 11:03:08 -0700634
Chris Lattner2e595eb2018-07-10 10:08:27 -0700635 // Identifier lists for polyhedral structures.
636 ParseResult parseDimIdList();
637 ParseResult parseSymbolIdList();
638 ParseResult parseDimOrSymbolId(bool isDim);
639
640 AffineExpr *parseAffineExpr();
641 AffineExpr *parseParentheticalExpr();
642 AffineExpr *parseNegateExpression(AffineExpr *lhs);
643 AffineExpr *parseIntegerExpr();
644 AffineExpr *parseBareIdExpr();
645
646 AffineExpr *getBinaryAffineOpExpr(AffineHighPrecOp op, AffineExpr *lhs,
647 AffineExpr *rhs);
648 AffineExpr *getBinaryAffineOpExpr(AffineLowPrecOp op, AffineExpr *lhs,
649 AffineExpr *rhs);
650 AffineExpr *parseAffineOperandExpr(AffineExpr *lhs);
651 AffineExpr *parseAffineLowPrecOpExpr(AffineExpr *llhs,
652 AffineLowPrecOp llhsOp);
653 AffineExpr *parseAffineHighPrecOpExpr(AffineExpr *llhs,
654 AffineHighPrecOp llhsOp);
655
656private:
657 // TODO(bondhugula): could just use an vector/ArrayRef and scan the numbers.
658 llvm::StringMap<unsigned> dims;
659 llvm::StringMap<unsigned> symbols;
660};
661} // end anonymous namespace
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700662
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700663/// Create an affine binary high precedence op expression (mul's, div's, mod)
Chris Lattner2e595eb2018-07-10 10:08:27 -0700664AffineExpr *AffineMapParser::getBinaryAffineOpExpr(AffineHighPrecOp op,
665 AffineExpr *lhs,
666 AffineExpr *rhs) {
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700667 switch (op) {
668 case Mul:
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700669 if (!lhs->isSymbolic() && !rhs->isSymbolic()) {
670 emitError("non-affine expression: at least one of the multiply "
671 "operands has to be either a constant or symbolic");
672 return nullptr;
673 }
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700674 return AffineMulExpr::get(lhs, rhs, builder.getContext());
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700675 case FloorDiv:
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700676 if (!rhs->isSymbolic()) {
677 emitError("non-affine expression: right operand of floordiv "
678 "has to be either a constant or symbolic");
679 return nullptr;
680 }
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700681 return AffineFloorDivExpr::get(lhs, rhs, builder.getContext());
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700682 case CeilDiv:
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700683 if (!rhs->isSymbolic()) {
684 emitError("non-affine expression: right operand of ceildiv "
685 "has to be either a constant or symbolic");
686 return nullptr;
687 }
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700688 return AffineCeilDivExpr::get(lhs, rhs, builder.getContext());
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700689 case Mod:
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700690 if (!rhs->isSymbolic()) {
691 emitError("non-affine expression: right operand of mod "
692 "has to be either a constant or symbolic");
693 return nullptr;
694 }
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700695 return AffineModExpr::get(lhs, rhs, builder.getContext());
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700696 case HNoOp:
697 llvm_unreachable("can't create affine expression for null high prec op");
698 return nullptr;
699 }
700}
701
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700702/// Create an affine binary low precedence op expression (add, sub).
Chris Lattner2e595eb2018-07-10 10:08:27 -0700703AffineExpr *AffineMapParser::getBinaryAffineOpExpr(AffineLowPrecOp op,
704 AffineExpr *lhs,
705 AffineExpr *rhs) {
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700706 switch (op) {
707 case AffineLowPrecOp::Add:
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700708 return AffineAddExpr::get(lhs, rhs, builder.getContext());
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700709 case AffineLowPrecOp::Sub:
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700710 return AffineSubExpr::get(lhs, rhs, builder.getContext());
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700711 case AffineLowPrecOp::LNoOp:
712 llvm_unreachable("can't create affine expression for null low prec op");
713 return nullptr;
714 }
715}
716
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700717/// Consume this token if it is a lower precedence affine op (there are only two
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700718/// precedence levels).
Chris Lattner2e595eb2018-07-10 10:08:27 -0700719AffineLowPrecOp AffineMapParser::consumeIfLowPrecOp() {
Chris Lattner48af7d12018-07-09 19:05:38 -0700720 switch (getToken().getKind()) {
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700721 case Token::plus:
722 consumeToken(Token::plus);
723 return AffineLowPrecOp::Add;
724 case Token::minus:
725 consumeToken(Token::minus);
726 return AffineLowPrecOp::Sub;
727 default:
728 return AffineLowPrecOp::LNoOp;
729 }
730}
731
732/// Consume this token if it is a higher precedence affine op (there are only
733/// two precedence levels)
Chris Lattner2e595eb2018-07-10 10:08:27 -0700734AffineHighPrecOp AffineMapParser::consumeIfHighPrecOp() {
Chris Lattner48af7d12018-07-09 19:05:38 -0700735 switch (getToken().getKind()) {
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700736 case Token::star:
737 consumeToken(Token::star);
738 return Mul;
739 case Token::kw_floordiv:
740 consumeToken(Token::kw_floordiv);
741 return FloorDiv;
742 case Token::kw_ceildiv:
743 consumeToken(Token::kw_ceildiv);
744 return CeilDiv;
745 case Token::kw_mod:
746 consumeToken(Token::kw_mod);
747 return Mod;
748 default:
749 return HNoOp;
750 }
751}
752
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700753/// Parse a high precedence op expression list: mul, div, and mod are high
754/// precedence binary ops, i.e., parse a
755/// expr_1 op_1 expr_2 op_2 ... expr_n
756/// where op_1, op_2 are all a AffineHighPrecOp (mul, div, mod).
757/// All affine binary ops are left associative.
758/// Given llhs, returns (llhs llhsOp lhs) op rhs, or (lhs op rhs) if llhs is
759/// null. If no rhs can be found, returns (llhs llhsOp lhs) or lhs if llhs is
760/// null.
761AffineExpr *
Chris Lattner2e595eb2018-07-10 10:08:27 -0700762AffineMapParser::parseAffineHighPrecOpExpr(AffineExpr *llhs,
763 AffineHighPrecOp llhsOp) {
764 AffineExpr *lhs = parseAffineOperandExpr(llhs);
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700765 if (!lhs)
766 return nullptr;
767
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700768 // Found an LHS. Parse the remaining expression.
Chris Lattner2e595eb2018-07-10 10:08:27 -0700769 if (AffineHighPrecOp op = consumeIfHighPrecOp()) {
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700770 if (llhs) {
771 AffineExpr *expr = getBinaryAffineOpExpr(llhsOp, llhs, lhs);
772 if (!expr)
773 return nullptr;
Chris Lattner2e595eb2018-07-10 10:08:27 -0700774 return parseAffineHighPrecOpExpr(expr, op);
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700775 }
776 // No LLHS, get RHS
Chris Lattner2e595eb2018-07-10 10:08:27 -0700777 return parseAffineHighPrecOpExpr(lhs, op);
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700778 }
779
780 // This is the last operand in this expression.
781 if (llhs)
782 return getBinaryAffineOpExpr(llhsOp, llhs, lhs);
783
784 // No llhs, 'lhs' itself is the expression.
785 return lhs;
786}
787
788/// Parse an affine expression inside parentheses.
789///
790/// affine-expr ::= `(` affine-expr `)`
Chris Lattner2e595eb2018-07-10 10:08:27 -0700791AffineExpr *AffineMapParser::parseParentheticalExpr() {
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700792 if (!consumeIf(Token::l_paren))
793 return (emitError("expected '('"), nullptr);
Chris Lattner48af7d12018-07-09 19:05:38 -0700794 if (getToken().is(Token::r_paren))
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700795 return (emitError("no expression inside parentheses"), nullptr);
Chris Lattner2e595eb2018-07-10 10:08:27 -0700796 auto *expr = parseAffineExpr();
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700797 if (!expr)
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700798 return nullptr;
799 if (!consumeIf(Token::r_paren))
800 return (emitError("expected ')'"), nullptr);
801 return expr;
802}
803
804/// Parse the negation expression.
805///
806/// affine-expr ::= `-` affine-expr
Chris Lattner2e595eb2018-07-10 10:08:27 -0700807AffineExpr *AffineMapParser::parseNegateExpression(AffineExpr *lhs) {
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700808 if (!consumeIf(Token::minus))
809 return (emitError("expected '-'"), nullptr);
810
Chris Lattner2e595eb2018-07-10 10:08:27 -0700811 AffineExpr *operand = parseAffineOperandExpr(lhs);
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700812 // Since negation has the highest precedence of all ops (including high
813 // precedence ops) but lower than parentheses, we are only going to use
814 // parseAffineOperandExpr instead of parseAffineExpr here.
815 if (!operand)
816 // Extra error message although parseAffineOperandExpr would have
817 // complained. Leads to a better diagnostic.
818 return (emitError("missing operand of negation"), nullptr);
Chris Lattner2e595eb2018-07-10 10:08:27 -0700819 auto *minusOne = AffineConstantExpr::get(-1, builder.getContext());
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700820 return AffineMulExpr::get(minusOne, operand, builder.getContext());
821}
822
823/// Parse a bare id that may appear in an affine expression.
824///
825/// affine-expr ::= bare-id
Chris Lattner2e595eb2018-07-10 10:08:27 -0700826AffineExpr *AffineMapParser::parseBareIdExpr() {
Chris Lattner48af7d12018-07-09 19:05:38 -0700827 if (getToken().isNot(Token::bare_identifier))
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700828 return (emitError("expected bare identifier"), nullptr);
829
Chris Lattner48af7d12018-07-09 19:05:38 -0700830 StringRef sRef = getTokenSpelling();
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700831 if (dims.count(sRef)) {
832 consumeToken(Token::bare_identifier);
833 return AffineDimExpr::get(dims.lookup(sRef), builder.getContext());
834 }
835 if (symbols.count(sRef)) {
836 consumeToken(Token::bare_identifier);
837 return AffineSymbolExpr::get(symbols.lookup(sRef), builder.getContext());
838 }
839 return (emitError("identifier is neither dimensional nor symbolic"), nullptr);
840}
841
842/// Parse a positive integral constant appearing in an affine expression.
843///
844/// affine-expr ::= integer-literal
Chris Lattner2e595eb2018-07-10 10:08:27 -0700845AffineExpr *AffineMapParser::parseIntegerExpr() {
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700846 // No need to handle negative numbers separately here. They are naturally
847 // handled via the unary negation operator, although (FIXME) MININT_64 still
848 // not correctly handled.
Chris Lattner48af7d12018-07-09 19:05:38 -0700849 if (getToken().isNot(Token::integer))
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700850 return (emitError("expected integer"), nullptr);
851
Chris Lattner48af7d12018-07-09 19:05:38 -0700852 auto val = getToken().getUInt64IntegerValue();
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700853 if (!val.hasValue() || (int64_t)val.getValue() < 0) {
854 return (emitError("constant too large for affineint"), nullptr);
855 }
856 consumeToken(Token::integer);
857 return AffineConstantExpr::get((int64_t)val.getValue(), builder.getContext());
858}
859
860/// Parses an expression that can be a valid operand of an affine expression.
Uday Bondhugula76345202018-07-09 13:47:52 -0700861/// lhs: if non-null, lhs is an affine expression that is the lhs of a binary
862/// operator, the rhs of which is being parsed. This is used to determine
863/// whether an error should be emitted for a missing right operand.
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700864// Eg: for an expression without parentheses (like i + j + k + l), each
865// of the four identifiers is an operand. For i + j*k + l, j*k is not an
866// operand expression, it's an op expression and will be parsed via
867// parseAffineHighPrecOpExpression(). However, for i + (j*k) + -l, (j*k) and -l
868// are valid operands that will be parsed by this function.
Chris Lattner2e595eb2018-07-10 10:08:27 -0700869AffineExpr *AffineMapParser::parseAffineOperandExpr(AffineExpr *lhs) {
Chris Lattner48af7d12018-07-09 19:05:38 -0700870 switch (getToken().getKind()) {
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700871 case Token::bare_identifier:
Chris Lattner2e595eb2018-07-10 10:08:27 -0700872 return parseBareIdExpr();
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700873 case Token::integer:
Chris Lattner2e595eb2018-07-10 10:08:27 -0700874 return parseIntegerExpr();
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700875 case Token::l_paren:
Chris Lattner2e595eb2018-07-10 10:08:27 -0700876 return parseParentheticalExpr();
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700877 case Token::minus:
Chris Lattner2e595eb2018-07-10 10:08:27 -0700878 return parseNegateExpression(lhs);
Uday Bondhugula76345202018-07-09 13:47:52 -0700879 case Token::kw_ceildiv:
880 case Token::kw_floordiv:
881 case Token::kw_mod:
882 case Token::plus:
883 case Token::star:
884 if (lhs)
885 emitError("missing right operand of binary operator");
886 else
887 emitError("missing left operand of binary operator");
888 return nullptr;
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700889 default:
890 if (lhs)
Uday Bondhugula76345202018-07-09 13:47:52 -0700891 emitError("missing right operand of binary operator");
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700892 else
893 emitError("expected affine expression");
894 return nullptr;
895 }
896}
897
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700898/// Parse affine expressions that are bare-id's, integer constants,
899/// parenthetical affine expressions, and affine op expressions that are a
900/// composition of those.
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700901///
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700902/// All binary op's associate from left to right.
903///
904/// {add, sub} have lower precedence than {mul, div, and mod}.
905///
Uday Bondhugula76345202018-07-09 13:47:52 -0700906/// Add, sub'are themselves at the same precedence level. Mul, floordiv,
907/// ceildiv, and mod are at the same higher precedence level. Negation has
908/// higher precedence than any binary op.
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700909///
910/// llhs: the affine expression appearing on the left of the one being parsed.
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700911/// This function will return ((llhs llhsOp lhs) op rhs) if llhs is non null,
912/// and lhs op rhs otherwise; if there is no rhs, llhs llhsOp lhs is returned if
913/// llhs is non-null; otherwise lhs is returned. This is to deal with left
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700914/// associativity.
915///
916/// Eg: when the expression is e1 + e2*e3 + e4, with e1 as llhs, this function
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700917/// will return the affine expr equivalent of (e1 + (e2*e3)) + e4, where (e2*e3)
918/// will be parsed using parseAffineHighPrecOpExpr().
Chris Lattner2e595eb2018-07-10 10:08:27 -0700919AffineExpr *AffineMapParser::parseAffineLowPrecOpExpr(AffineExpr *llhs,
920 AffineLowPrecOp llhsOp) {
Uday Bondhugula76345202018-07-09 13:47:52 -0700921 AffineExpr *lhs;
Chris Lattner2e595eb2018-07-10 10:08:27 -0700922 if (!(lhs = parseAffineOperandExpr(llhs)))
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700923 return nullptr;
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700924
925 // Found an LHS. Deal with the ops.
Chris Lattner2e595eb2018-07-10 10:08:27 -0700926 if (AffineLowPrecOp lOp = consumeIfLowPrecOp()) {
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700927 if (llhs) {
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700928 AffineExpr *sum = getBinaryAffineOpExpr(llhsOp, llhs, lhs);
Chris Lattner2e595eb2018-07-10 10:08:27 -0700929 return parseAffineLowPrecOpExpr(sum, lOp);
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700930 }
931 // No LLHS, get RHS and form the expression.
Chris Lattner2e595eb2018-07-10 10:08:27 -0700932 return parseAffineLowPrecOpExpr(lhs, lOp);
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700933 }
Chris Lattner2e595eb2018-07-10 10:08:27 -0700934 if (AffineHighPrecOp hOp = consumeIfHighPrecOp()) {
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700935 // We have a higher precedence op here. Get the rhs operand for the llhs
936 // through parseAffineHighPrecOpExpr.
Chris Lattner2e595eb2018-07-10 10:08:27 -0700937 AffineExpr *highRes = parseAffineHighPrecOpExpr(lhs, hOp);
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700938 if (!highRes)
939 return nullptr;
Chris Lattner2e595eb2018-07-10 10:08:27 -0700940
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700941 // If llhs is null, the product forms the first operand of the yet to be
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700942 // found expression. If non-null, the op to associate with llhs is llhsOp.
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700943 AffineExpr *expr =
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700944 llhs ? getBinaryAffineOpExpr(llhsOp, llhs, highRes) : highRes;
Chris Lattner2e595eb2018-07-10 10:08:27 -0700945
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700946 // Recurse for subsequent low prec op's after the affine high prec op
947 // expression.
Chris Lattner2e595eb2018-07-10 10:08:27 -0700948 if (AffineLowPrecOp nextOp = consumeIfLowPrecOp())
949 return parseAffineLowPrecOpExpr(expr, nextOp);
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700950 return expr;
951 }
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700952 // Last operand in the expression list.
953 if (llhs)
954 return getBinaryAffineOpExpr(llhsOp, llhs, lhs);
955 // No llhs, 'lhs' itself is the expression.
956 return lhs;
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700957}
958
959/// Parse an affine expression.
Uday Bondhugula3934d4d2018-07-09 09:00:25 -0700960/// affine-expr ::= `(` affine-expr `)`
961/// | `-` affine-expr
962/// | affine-expr `+` affine-expr
963/// | affine-expr `-` affine-expr
964/// | affine-expr `*` affine-expr
965/// | affine-expr `floordiv` affine-expr
966/// | affine-expr `ceildiv` affine-expr
967/// | affine-expr `mod` affine-expr
968/// | bare-id
969/// | integer-literal
970///
971/// Additional conditions are checked depending on the production. For eg., one
972/// of the operands for `*` has to be either constant/symbolic; the second
973/// operand for floordiv, ceildiv, and mod has to be a positive integer.
Chris Lattner2e595eb2018-07-10 10:08:27 -0700974AffineExpr *AffineMapParser::parseAffineExpr() {
975 return parseAffineLowPrecOpExpr(nullptr, AffineLowPrecOp::LNoOp);
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700976}
977
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700978/// Parse a dim or symbol from the lists appearing before the actual expressions
Chris Lattner2e595eb2018-07-10 10:08:27 -0700979/// of the affine map. Update our state to store the dimensional/symbolic
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700980/// identifier. 'dim': whether it's the dim list or symbol list that is being
981/// parsed.
Chris Lattner2e595eb2018-07-10 10:08:27 -0700982ParseResult AffineMapParser::parseDimOrSymbolId(bool isDim) {
Chris Lattner48af7d12018-07-09 19:05:38 -0700983 if (getToken().isNot(Token::bare_identifier))
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700984 return emitError("expected bare identifier");
Chris Lattner48af7d12018-07-09 19:05:38 -0700985 auto sRef = getTokenSpelling();
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700986 consumeToken(Token::bare_identifier);
Chris Lattner2e595eb2018-07-10 10:08:27 -0700987 if (dims.count(sRef))
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700988 return emitError("dimensional identifier name reused");
Chris Lattner2e595eb2018-07-10 10:08:27 -0700989 if (symbols.count(sRef))
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700990 return emitError("symbolic identifier name reused");
Chris Lattner2e595eb2018-07-10 10:08:27 -0700991 if (isDim)
992 dims.insert({sRef, dims.size()});
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700993 else
Chris Lattner2e595eb2018-07-10 10:08:27 -0700994 symbols.insert({sRef, symbols.size()});
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700995 return ParseSuccess;
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700996}
997
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700998/// Parse the list of symbolic identifiers to an affine map.
Chris Lattner2e595eb2018-07-10 10:08:27 -0700999ParseResult AffineMapParser::parseSymbolIdList() {
1000 if (!consumeIf(Token::l_bracket))
1001 return emitError("expected '['");
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001002
Chris Lattner2e595eb2018-07-10 10:08:27 -07001003 auto parseElt = [&]() -> ParseResult { return parseDimOrSymbolId(false); };
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001004 return parseCommaSeparatedList(Token::r_bracket, parseElt);
1005}
1006
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001007/// Parse the list of dimensional identifiers to an affine map.
Chris Lattner2e595eb2018-07-10 10:08:27 -07001008ParseResult AffineMapParser::parseDimIdList() {
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001009 if (!consumeIf(Token::l_paren))
1010 return emitError("expected '(' at start of dimensional identifiers list");
1011
Chris Lattner2e595eb2018-07-10 10:08:27 -07001012 auto parseElt = [&]() -> ParseResult { return parseDimOrSymbolId(true); };
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001013 return parseCommaSeparatedList(Token::r_paren, parseElt);
1014}
1015
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001016/// Parse an affine map definition.
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001017///
Uday Bondhugula3934d4d2018-07-09 09:00:25 -07001018/// affine-map-inline ::= dim-and-symbol-id-lists `->` multi-dim-affine-expr
1019/// (`size` `(` dim-size (`,` dim-size)* `)`)?
1020/// dim-size ::= affine-expr | `min` `(` affine-expr ( `,` affine-expr)+ `)`
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001021///
Uday Bondhugula3934d4d2018-07-09 09:00:25 -07001022/// multi-dim-affine-expr ::= `(` affine-expr (`,` affine-expr)* `)
1023// TODO(bondhugula): parse range size information.
Chris Lattner2e595eb2018-07-10 10:08:27 -07001024AffineMap *AffineMapParser::parseAffineMapInline() {
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001025 // List of dimensional identifiers.
Chris Lattner2e595eb2018-07-10 10:08:27 -07001026 if (parseDimIdList())
Chris Lattner7121b802018-07-04 20:45:39 -07001027 return nullptr;
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001028
1029 // Symbols are optional.
Chris Lattner48af7d12018-07-09 19:05:38 -07001030 if (getToken().is(Token::l_bracket)) {
Chris Lattner2e595eb2018-07-10 10:08:27 -07001031 if (parseSymbolIdList())
Chris Lattner7121b802018-07-04 20:45:39 -07001032 return nullptr;
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001033 }
1034 if (!consumeIf(Token::arrow)) {
Chris Lattner7121b802018-07-04 20:45:39 -07001035 return (emitError("expected '->' or '['"), nullptr);
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001036 }
1037 if (!consumeIf(Token::l_paren)) {
1038 emitError("expected '(' at start of affine map range");
Chris Lattner7121b802018-07-04 20:45:39 -07001039 return nullptr;
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001040 }
1041
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001042 SmallVector<AffineExpr *, 4> exprs;
1043 auto parseElt = [&]() -> ParseResult {
Chris Lattner2e595eb2018-07-10 10:08:27 -07001044 auto *elt = parseAffineExpr();
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001045 ParseResult res = elt ? ParseSuccess : ParseFailure;
1046 exprs.push_back(elt);
1047 return res;
1048 };
1049
1050 // Parse a multi-dimensional affine expression (a comma-separated list of 1-d
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001051 // affine expressions); the list cannot be empty.
1052 // Grammar: multi-dim-affine-expr ::= `(` affine-expr (`,` affine-expr)* `)
1053 if (parseCommaSeparatedList(Token::r_paren, parseElt, false))
Chris Lattner7121b802018-07-04 20:45:39 -07001054 return nullptr;
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -07001055
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001056 // Parsed a valid affine map.
Chris Lattner2e595eb2018-07-10 10:08:27 -07001057 return AffineMap::get(dims.size(), symbols.size(), exprs,
Chris Lattner158e0a3e2018-07-08 20:51:38 -07001058 builder.getContext());
MLIR Teamf85a6262018-06-27 11:03:08 -07001059}
1060
Chris Lattner2e595eb2018-07-10 10:08:27 -07001061AffineMap *Parser::parseAffineMapInline() {
1062 return AffineMapParser(state).parseAffineMapInline();
1063}
1064
MLIR Teamf85a6262018-06-27 11:03:08 -07001065//===----------------------------------------------------------------------===//
Chris Lattner78276e32018-07-07 15:48:26 -07001066// SSA
Chris Lattner4c95a502018-06-23 16:03:42 -07001067//===----------------------------------------------------------------------===//
Chris Lattnere79379a2018-06-22 10:39:19 -07001068
Chris Lattner78276e32018-07-07 15:48:26 -07001069/// Parse a SSA operand for an instruction or statement.
1070///
1071/// ssa-use ::= ssa-id | ssa-constant
1072///
1073ParseResult Parser::parseSSAUse() {
Chris Lattner48af7d12018-07-09 19:05:38 -07001074 if (getToken().is(Token::percent_identifier)) {
1075 StringRef name = getTokenSpelling().drop_front();
Chris Lattner78276e32018-07-07 15:48:26 -07001076 consumeToken(Token::percent_identifier);
1077 // TODO: Return this use.
1078 (void)name;
1079 return ParseSuccess;
1080 }
1081
1082 // TODO: Parse SSA constants.
1083
1084 return emitError("expected SSA operand");
1085}
1086
1087/// Parse a (possibly empty) list of SSA operands.
1088///
1089/// ssa-use-list ::= ssa-use (`,` ssa-use)*
1090/// ssa-use-list-opt ::= ssa-use-list?
1091///
1092ParseResult Parser::parseOptionalSSAUseList(Token::Kind endToken) {
1093 // TODO: Build and return this.
1094 return parseCommaSeparatedList(
1095 endToken, [&]() -> ParseResult { return parseSSAUse(); });
1096}
1097
1098/// Parse an SSA use with an associated type.
1099///
1100/// ssa-use-and-type ::= ssa-use `:` type
1101ParseResult Parser::parseSSAUseAndType() {
1102 if (parseSSAUse())
1103 return ParseFailure;
1104
1105 if (!consumeIf(Token::colon))
1106 return emitError("expected ':' and type for SSA operand");
1107
1108 if (!parseType())
1109 return ParseFailure;
1110
1111 return ParseSuccess;
1112}
1113
1114/// Parse a (possibly empty) list of SSA operands with types.
1115///
1116/// ssa-use-and-type-list ::= ssa-use-and-type (`,` ssa-use-and-type)*
1117///
1118ParseResult Parser::parseOptionalSSAUseAndTypeList(Token::Kind endToken) {
1119 // TODO: Build and return this.
1120 return parseCommaSeparatedList(
1121 endToken, [&]() -> ParseResult { return parseSSAUseAndType(); });
1122}
1123
Chris Lattnere79379a2018-06-22 10:39:19 -07001124
Chris Lattner48af7d12018-07-09 19:05:38 -07001125//===----------------------------------------------------------------------===//
1126// CFG Functions
1127//===----------------------------------------------------------------------===//
Chris Lattnere79379a2018-06-22 10:39:19 -07001128
Chris Lattner4c95a502018-06-23 16:03:42 -07001129namespace {
Chris Lattner48af7d12018-07-09 19:05:38 -07001130/// This is a specialized parser for CFGFunction's, maintaining the state
1131/// transient to their bodies.
1132class CFGFunctionParser : public Parser {
Chris Lattner158e0a3e2018-07-08 20:51:38 -07001133public:
Chris Lattner2e595eb2018-07-10 10:08:27 -07001134 CFGFunctionParser(ParserState &state, CFGFunction *function)
1135 : Parser(state), function(function), builder(function) {}
1136
1137 ParseResult parseFunctionBody();
1138
1139private:
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001140 CFGFunction *function;
1141 llvm::StringMap<std::pair<BasicBlock*, SMLoc>> blocksByName;
Chris Lattner48af7d12018-07-09 19:05:38 -07001142
1143 /// This builder intentionally shadows the builder in the base class, with a
1144 /// more specific builder type.
Chris Lattner158e0a3e2018-07-08 20:51:38 -07001145 CFGFuncBuilder builder;
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001146
Chris Lattner4c95a502018-06-23 16:03:42 -07001147 /// Get the basic block with the specified name, creating it if it doesn't
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001148 /// already exist. The location specified is the point of use, which allows
1149 /// us to diagnose references to blocks that are not defined precisely.
1150 BasicBlock *getBlockNamed(StringRef name, SMLoc loc) {
1151 auto &blockAndLoc = blocksByName[name];
1152 if (!blockAndLoc.first) {
Chris Lattner3a467cc2018-07-01 20:28:00 -07001153 blockAndLoc.first = new BasicBlock();
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001154 blockAndLoc.second = loc;
Chris Lattner4c95a502018-06-23 16:03:42 -07001155 }
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001156 return blockAndLoc.first;
Chris Lattner4c95a502018-06-23 16:03:42 -07001157 }
Chris Lattner48af7d12018-07-09 19:05:38 -07001158
Chris Lattner48af7d12018-07-09 19:05:38 -07001159 ParseResult parseBasicBlock();
1160 OperationInst *parseCFGOperation();
1161 TerminatorInst *parseTerminator();
Chris Lattner4c95a502018-06-23 16:03:42 -07001162};
1163} // end anonymous namespace
1164
Chris Lattner48af7d12018-07-09 19:05:38 -07001165ParseResult CFGFunctionParser::parseFunctionBody() {
1166 if (!consumeIf(Token::l_brace))
1167 return emitError("expected '{' in CFG function");
1168
1169 // Make sure we have at least one block.
1170 if (getToken().is(Token::r_brace))
1171 return emitError("CFG functions must have at least one basic block");
Chris Lattner4c95a502018-06-23 16:03:42 -07001172
1173 // Parse the list of blocks.
1174 while (!consumeIf(Token::r_brace))
Chris Lattner48af7d12018-07-09 19:05:38 -07001175 if (parseBasicBlock())
Chris Lattner4c95a502018-06-23 16:03:42 -07001176 return ParseFailure;
1177
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001178 // Verify that all referenced blocks were defined. Iteration over a
1179 // StringMap isn't determinstic, but this is good enough for our purposes.
Chris Lattner48af7d12018-07-09 19:05:38 -07001180 for (auto &elt : blocksByName) {
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001181 auto *bb = elt.second.first;
Chris Lattner3a467cc2018-07-01 20:28:00 -07001182 if (!bb->getFunction())
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001183 return emitError(elt.second.second,
1184 "reference to an undefined basic block '" +
1185 elt.first() + "'");
1186 }
1187
Chris Lattner48af7d12018-07-09 19:05:38 -07001188 getModule()->functionList.push_back(function);
Chris Lattner4c95a502018-06-23 16:03:42 -07001189 return ParseSuccess;
1190}
1191
1192/// Basic block declaration.
1193///
1194/// basic-block ::= bb-label instruction* terminator-stmt
1195/// bb-label ::= bb-id bb-arg-list? `:`
1196/// bb-id ::= bare-id
1197/// bb-arg-list ::= `(` ssa-id-and-type-list? `)`
1198///
Chris Lattner48af7d12018-07-09 19:05:38 -07001199ParseResult CFGFunctionParser::parseBasicBlock() {
1200 SMLoc nameLoc = getToken().getLoc();
1201 auto name = getTokenSpelling();
Chris Lattner4c95a502018-06-23 16:03:42 -07001202 if (!consumeIf(Token::bare_identifier))
1203 return emitError("expected basic block name");
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001204
Chris Lattner48af7d12018-07-09 19:05:38 -07001205 auto *block = getBlockNamed(name, nameLoc);
Chris Lattner4c95a502018-06-23 16:03:42 -07001206
1207 // If this block has already been parsed, then this is a redefinition with the
1208 // same block name.
Chris Lattner3a467cc2018-07-01 20:28:00 -07001209 if (block->getFunction())
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001210 return emitError(nameLoc, "redefinition of block '" + name.str() + "'");
1211
Chris Lattner3a467cc2018-07-01 20:28:00 -07001212 // Add the block to the function.
Chris Lattner48af7d12018-07-09 19:05:38 -07001213 function->push_back(block);
Chris Lattner4c95a502018-06-23 16:03:42 -07001214
Chris Lattner78276e32018-07-07 15:48:26 -07001215 // If an argument list is present, parse it.
1216 if (consumeIf(Token::l_paren)) {
1217 if (parseOptionalSSAUseAndTypeList(Token::r_paren))
1218 return ParseFailure;
1219
1220 // TODO: attach it.
1221 }
Chris Lattner4c95a502018-06-23 16:03:42 -07001222
1223 if (!consumeIf(Token::colon))
1224 return emitError("expected ':' after basic block name");
1225
Chris Lattner158e0a3e2018-07-08 20:51:38 -07001226 // Set the insertion point to the block we want to insert new operations into.
Chris Lattner48af7d12018-07-09 19:05:38 -07001227 builder.setInsertionPoint(block);
Chris Lattner158e0a3e2018-07-08 20:51:38 -07001228
Chris Lattnered65a732018-06-28 20:45:33 -07001229 // Parse the list of operations that make up the body of the block.
Chris Lattner48af7d12018-07-09 19:05:38 -07001230 while (getToken().isNot(Token::kw_return, Token::kw_br)) {
1231 auto loc = getToken().getLoc();
1232 auto *inst = parseCFGOperation();
Chris Lattner3a467cc2018-07-01 20:28:00 -07001233 if (!inst)
Chris Lattnered65a732018-06-28 20:45:33 -07001234 return ParseFailure;
Chris Lattner3a467cc2018-07-01 20:28:00 -07001235
Chris Lattner21e67f62018-07-06 10:46:19 -07001236 // We just parsed an operation. If it is a recognized one, verify that it
1237 // is structurally as we expect. If not, produce an error with a reasonable
1238 // source location.
Chris Lattner158e0a3e2018-07-08 20:51:38 -07001239 if (auto *opInfo = inst->getAbstractOperation(builder.getContext()))
Chris Lattner21e67f62018-07-06 10:46:19 -07001240 if (auto error = opInfo->verifyInvariants(inst))
1241 return emitError(loc, error);
Chris Lattnered65a732018-06-28 20:45:33 -07001242 }
Chris Lattner4c95a502018-06-23 16:03:42 -07001243
Chris Lattner48af7d12018-07-09 19:05:38 -07001244 auto *term = parseTerminator();
Chris Lattner3a467cc2018-07-01 20:28:00 -07001245 if (!term)
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001246 return ParseFailure;
Chris Lattner4c95a502018-06-23 16:03:42 -07001247
1248 return ParseSuccess;
1249}
1250
Chris Lattnered65a732018-06-28 20:45:33 -07001251/// Parse the CFG operation.
1252///
1253/// TODO(clattner): This is a change from the MLIR spec as written, it is an
1254/// experiment that will eliminate "builtin" instructions as a thing.
1255///
1256/// cfg-operation ::=
1257/// (ssa-id `=`)? string '(' ssa-use-list? ')' attribute-dict?
1258/// `:` function-type
1259///
Chris Lattner48af7d12018-07-09 19:05:38 -07001260OperationInst *CFGFunctionParser::parseCFGOperation() {
Chris Lattner78276e32018-07-07 15:48:26 -07001261 StringRef resultID;
Chris Lattner48af7d12018-07-09 19:05:38 -07001262 if (getToken().is(Token::percent_identifier)) {
1263 resultID = getTokenSpelling().drop_front();
Chris Lattner78276e32018-07-07 15:48:26 -07001264 consumeToken();
1265 if (!consumeIf(Token::equal))
1266 return (emitError("expected '=' after SSA name"), nullptr);
1267 }
Chris Lattnered65a732018-06-28 20:45:33 -07001268
Chris Lattner48af7d12018-07-09 19:05:38 -07001269 if (getToken().isNot(Token::string))
Chris Lattner3a467cc2018-07-01 20:28:00 -07001270 return (emitError("expected operation name in quotes"), nullptr);
Chris Lattnered65a732018-06-28 20:45:33 -07001271
Chris Lattner48af7d12018-07-09 19:05:38 -07001272 auto name = getToken().getStringValue();
Chris Lattnered65a732018-06-28 20:45:33 -07001273 if (name.empty())
Chris Lattner3a467cc2018-07-01 20:28:00 -07001274 return (emitError("empty operation name is invalid"), nullptr);
Chris Lattnered65a732018-06-28 20:45:33 -07001275
1276 consumeToken(Token::string);
1277
1278 if (!consumeIf(Token::l_paren))
Chris Lattner7121b802018-07-04 20:45:39 -07001279 return (emitError("expected '(' to start operand list"), nullptr);
Chris Lattnered65a732018-06-28 20:45:33 -07001280
Chris Lattner78276e32018-07-07 15:48:26 -07001281 // Parse the operand list.
1282 parseOptionalSSAUseList(Token::r_paren);
Chris Lattner7121b802018-07-04 20:45:39 -07001283
1284 SmallVector<NamedAttribute, 4> attributes;
Chris Lattner48af7d12018-07-09 19:05:38 -07001285 if (getToken().is(Token::l_brace)) {
Chris Lattner7121b802018-07-04 20:45:39 -07001286 if (parseAttributeDict(attributes))
1287 return nullptr;
1288 }
Chris Lattnered65a732018-06-28 20:45:33 -07001289
Chris Lattner78276e32018-07-07 15:48:26 -07001290 // TODO: Don't drop result name and operand names on the floor.
Chris Lattner158e0a3e2018-07-08 20:51:38 -07001291 auto nameId = Identifier::get(name, builder.getContext());
Chris Lattner48af7d12018-07-09 19:05:38 -07001292 return builder.createOperation(nameId, attributes);
Chris Lattnered65a732018-06-28 20:45:33 -07001293}
1294
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001295/// Parse the terminator instruction for a basic block.
1296///
1297/// terminator-stmt ::= `br` bb-id branch-use-list?
1298/// branch-use-list ::= `(` ssa-use-and-type-list? `)`
1299/// terminator-stmt ::=
1300/// `cond_br` ssa-use `,` bb-id branch-use-list? `,` bb-id branch-use-list?
1301/// terminator-stmt ::= `return` ssa-use-and-type-list?
1302///
Chris Lattner48af7d12018-07-09 19:05:38 -07001303TerminatorInst *CFGFunctionParser::parseTerminator() {
1304 switch (getToken().getKind()) {
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001305 default:
Chris Lattner3a467cc2018-07-01 20:28:00 -07001306 return (emitError("expected terminator at end of basic block"), nullptr);
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001307
1308 case Token::kw_return:
1309 consumeToken(Token::kw_return);
Chris Lattner48af7d12018-07-09 19:05:38 -07001310 return builder.createReturnInst();
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001311
1312 case Token::kw_br: {
1313 consumeToken(Token::kw_br);
Chris Lattner48af7d12018-07-09 19:05:38 -07001314 auto destBB = getBlockNamed(getTokenSpelling(), getToken().getLoc());
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001315 if (!consumeIf(Token::bare_identifier))
Chris Lattner3a467cc2018-07-01 20:28:00 -07001316 return (emitError("expected basic block name"), nullptr);
Chris Lattner48af7d12018-07-09 19:05:38 -07001317 return builder.createBranchInst(destBB);
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001318 }
Chris Lattner78276e32018-07-07 15:48:26 -07001319 // TODO: cond_br.
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001320 }
1321}
1322
Chris Lattner48af7d12018-07-09 19:05:38 -07001323//===----------------------------------------------------------------------===//
1324// ML Functions
1325//===----------------------------------------------------------------------===//
1326
1327namespace {
1328/// Refined parser for MLFunction bodies.
1329class MLFunctionParser : public Parser {
1330public:
1331 MLFunction *function;
1332
1333 /// This builder intentionally shadows the builder in the base class, with a
1334 /// more specific builder type.
1335 // TODO: MLFuncBuilder builder;
1336
1337 MLFunctionParser(ParserState &state, MLFunction *function)
1338 : Parser(state), function(function) {}
1339
1340 ParseResult parseFunctionBody();
1341 Statement *parseStatement(ParentType parent);
1342 ForStmt *parseForStmt(ParentType parent);
1343 IfStmt *parseIfStmt(ParentType parent);
1344 ParseResult parseNestedStatements(NodeStmt *parent);
1345};
1346} // end anonymous namespace
1347
Chris Lattner48af7d12018-07-09 19:05:38 -07001348ParseResult MLFunctionParser::parseFunctionBody() {
1349 if (!consumeIf(Token::l_brace))
1350 return emitError("expected '{' in ML function");
1351
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -07001352 // Make sure we have at least one statement.
Chris Lattner48af7d12018-07-09 19:05:38 -07001353 if (getToken().is(Token::r_brace))
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -07001354 return emitError("ML function must end with return statement");
1355
1356 // Parse the list of instructions.
1357 while (!consumeIf(Token::kw_return)) {
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001358 auto *stmt = parseStatement(function);
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -07001359 if (!stmt)
1360 return ParseFailure;
1361 function->stmtList.push_back(stmt);
1362 }
1363
1364 // TODO: parse return statement operands
1365 if (!consumeIf(Token::r_brace))
1366 emitError("expected '}' in ML function");
1367
Chris Lattner48af7d12018-07-09 19:05:38 -07001368 getModule()->functionList.push_back(function);
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -07001369
1370 return ParseSuccess;
1371}
1372
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001373/// Statement.
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -07001374///
Chris Lattner48af7d12018-07-09 19:05:38 -07001375/// ml-stmt ::= instruction | ml-for-stmt | ml-if-stmt
1376///
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001377/// TODO: fix terminology in MLSpec document. ML functions
1378/// contain operation statements, not instructions.
1379///
Chris Lattner48af7d12018-07-09 19:05:38 -07001380Statement *MLFunctionParser::parseStatement(ParentType parent) {
1381 switch (getToken().getKind()) {
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -07001382 default:
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001383 //TODO: parse OperationStmt
1384 return (emitError("expected statement"), nullptr);
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -07001385
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001386 case Token::kw_for:
1387 return parseForStmt(parent);
1388
1389 case Token::kw_if:
1390 return parseIfStmt(parent);
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -07001391 }
1392}
Chris Lattnerf6d80a02018-06-24 11:18:29 -07001393
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001394/// For statement.
1395///
Chris Lattner48af7d12018-07-09 19:05:38 -07001396/// ml-for-stmt ::= `for` ssa-id `=` lower-bound `to` upper-bound
1397/// (`step` integer-literal)? `{` ml-stmt* `}`
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001398///
Chris Lattner48af7d12018-07-09 19:05:38 -07001399ForStmt *MLFunctionParser::parseForStmt(ParentType parent) {
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001400 consumeToken(Token::kw_for);
1401
1402 //TODO: parse loop header
1403 ForStmt *stmt = new ForStmt(parent);
1404 if (parseNestedStatements(stmt)) {
1405 delete stmt;
1406 return nullptr;
1407 }
1408 return stmt;
1409}
1410
1411/// If statement.
1412///
Chris Lattner48af7d12018-07-09 19:05:38 -07001413/// ml-if-head ::= `if` ml-if-cond `{` ml-stmt* `}`
1414/// | ml-if-head `else` `if` ml-if-cond `{` ml-stmt* `}`
1415/// ml-if-stmt ::= ml-if-head
1416/// | ml-if-head `else` `{` ml-stmt* `}`
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001417///
Chris Lattner48af7d12018-07-09 19:05:38 -07001418IfStmt *
1419MLFunctionParser::parseIfStmt(PointerUnion<MLFunction *, NodeStmt *> parent) {
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001420 consumeToken(Token::kw_if);
1421
1422 //TODO: parse condition
1423 IfStmt *stmt = new IfStmt(parent);
1424 if (parseNestedStatements(stmt)) {
1425 delete stmt;
1426 return nullptr;
1427 }
1428
1429 int clauseNum = 0;
1430 while (consumeIf(Token::kw_else)) {
1431 if (consumeIf(Token::kw_if)) {
1432 //TODO: parse condition
1433 }
1434 ElseClause * clause = new ElseClause(stmt, clauseNum);
1435 ++clauseNum;
1436 if (parseNestedStatements(clause)) {
1437 delete clause;
1438 return nullptr;
1439 }
1440 }
1441
1442 return stmt;
1443}
1444
1445///
1446/// Parse `{` ml-stmt* `}`
1447///
Chris Lattner48af7d12018-07-09 19:05:38 -07001448ParseResult MLFunctionParser::parseNestedStatements(NodeStmt *parent) {
Tatiana Shpeismanbf079c92018-07-03 17:51:28 -07001449 if (!consumeIf(Token::l_brace))
1450 return emitError("expected '{' before statement list");
1451
1452 if (consumeIf(Token::r_brace)) {
1453 // TODO: parse OperationStmt
1454 return ParseSuccess;
1455 }
1456
1457 while (!consumeIf(Token::r_brace)) {
1458 auto *stmt = parseStatement(parent);
1459 if (!stmt)
1460 return ParseFailure;
1461 parent->children.push_back(stmt);
1462 }
1463
1464 return ParseSuccess;
1465}
1466
Chris Lattner4c95a502018-06-23 16:03:42 -07001467//===----------------------------------------------------------------------===//
1468// Top-level entity parsing.
1469//===----------------------------------------------------------------------===//
1470
Chris Lattner2e595eb2018-07-10 10:08:27 -07001471namespace {
1472/// This parser handles entities that are only valid at the top level of the
1473/// file.
1474class ModuleParser : public Parser {
1475public:
1476 explicit ModuleParser(ParserState &state) : Parser(state) {}
1477
1478 ParseResult parseModule();
1479
1480private:
1481 ParseResult parseAffineMapDef();
1482
1483 // Functions.
1484 ParseResult parseFunctionSignature(StringRef &name, FunctionType *&type);
1485 ParseResult parseExtFunc();
1486 ParseResult parseCFGFunc();
1487 ParseResult parseMLFunc();
1488};
1489} // end anonymous namespace
1490
1491/// Affine map declaration.
1492///
1493/// affine-map-def ::= affine-map-id `=` affine-map-inline
1494///
1495ParseResult ModuleParser::parseAffineMapDef() {
1496 assert(getToken().is(Token::hash_identifier));
1497
1498 StringRef affineMapId = getTokenSpelling().drop_front();
1499
1500 // Check for redefinitions.
1501 auto *&entry = getState().affineMapDefinitions[affineMapId];
1502 if (entry)
1503 return emitError("redefinition of affine map id '" + affineMapId + "'");
1504
1505 consumeToken(Token::hash_identifier);
1506
1507 // Parse the '='
1508 if (!consumeIf(Token::equal))
1509 return emitError("expected '=' in affine map outlined definition");
1510
1511 entry = parseAffineMapInline();
1512 if (!entry)
1513 return ParseFailure;
1514
1515 getModule()->affineMapList.push_back(entry);
1516 return ParseSuccess;
1517}
1518
1519/// Parse a function signature, starting with a name and including the parameter
1520/// list.
1521///
1522/// argument-list ::= type (`,` type)* | /*empty*/
1523/// function-signature ::= function-id `(` argument-list `)` (`->` type-list)?
1524///
1525ParseResult ModuleParser::parseFunctionSignature(StringRef &name,
1526 FunctionType *&type) {
1527 if (getToken().isNot(Token::at_identifier))
1528 return emitError("expected a function identifier like '@foo'");
1529
1530 name = getTokenSpelling().drop_front();
1531 consumeToken(Token::at_identifier);
1532
1533 if (getToken().isNot(Token::l_paren))
1534 return emitError("expected '(' in function signature");
1535
1536 SmallVector<Type *, 4> arguments;
1537 if (parseTypeList(arguments))
1538 return ParseFailure;
1539
1540 // Parse the return type if present.
1541 SmallVector<Type *, 4> results;
1542 if (consumeIf(Token::arrow)) {
1543 if (parseTypeList(results))
1544 return ParseFailure;
1545 }
1546 type = builder.getFunctionType(arguments, results);
1547 return ParseSuccess;
1548}
1549
1550/// External function declarations.
1551///
1552/// ext-func ::= `extfunc` function-signature
1553///
1554ParseResult ModuleParser::parseExtFunc() {
1555 consumeToken(Token::kw_extfunc);
1556
1557 StringRef name;
1558 FunctionType *type = nullptr;
1559 if (parseFunctionSignature(name, type))
1560 return ParseFailure;
1561
1562 // Okay, the external function definition was parsed correctly.
1563 getModule()->functionList.push_back(new ExtFunction(name, type));
1564 return ParseSuccess;
1565}
1566
1567/// CFG function declarations.
1568///
1569/// cfg-func ::= `cfgfunc` function-signature `{` basic-block+ `}`
1570///
1571ParseResult ModuleParser::parseCFGFunc() {
1572 consumeToken(Token::kw_cfgfunc);
1573
1574 StringRef name;
1575 FunctionType *type = nullptr;
1576 if (parseFunctionSignature(name, type))
1577 return ParseFailure;
1578
1579 // Okay, the CFG function signature was parsed correctly, create the function.
1580 auto function = new CFGFunction(name, type);
1581
1582 return CFGFunctionParser(getState(), function).parseFunctionBody();
1583}
1584
1585/// ML function declarations.
1586///
1587/// ml-func ::= `mlfunc` ml-func-signature `{` ml-stmt* ml-return-stmt `}`
1588///
1589ParseResult ModuleParser::parseMLFunc() {
1590 consumeToken(Token::kw_mlfunc);
1591
1592 StringRef name;
1593 FunctionType *type = nullptr;
1594
1595 // FIXME: Parse ML function signature (args + types)
1596 // by passing pointer to SmallVector<identifier> into parseFunctionSignature
1597 if (parseFunctionSignature(name, type))
1598 return ParseFailure;
1599
1600 // Okay, the ML function signature was parsed correctly, create the function.
1601 auto function = new MLFunction(name, type);
1602
1603 return MLFunctionParser(getState(), function).parseFunctionBody();
1604}
1605
Chris Lattnere79379a2018-06-22 10:39:19 -07001606/// This is the top-level module parser.
Chris Lattner2e595eb2018-07-10 10:08:27 -07001607ParseResult ModuleParser::parseModule() {
Chris Lattnere79379a2018-06-22 10:39:19 -07001608 while (1) {
Chris Lattner48af7d12018-07-09 19:05:38 -07001609 switch (getToken().getKind()) {
Chris Lattnere79379a2018-06-22 10:39:19 -07001610 default:
1611 emitError("expected a top level entity");
Chris Lattner2e595eb2018-07-10 10:08:27 -07001612 return ParseFailure;
Chris Lattnere79379a2018-06-22 10:39:19 -07001613
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001614 // If we got to the end of the file, then we're done.
Chris Lattnere79379a2018-06-22 10:39:19 -07001615 case Token::eof:
Chris Lattner2e595eb2018-07-10 10:08:27 -07001616 return ParseSuccess;
Chris Lattnere79379a2018-06-22 10:39:19 -07001617
1618 // If we got an error token, then the lexer already emitted an error, just
1619 // stop. Someday we could introduce error recovery if there was demand for
1620 // it.
1621 case Token::error:
Chris Lattner2e595eb2018-07-10 10:08:27 -07001622 return ParseFailure;
1623
1624 case Token::hash_identifier:
1625 if (parseAffineMapDef())
1626 return ParseFailure;
1627 break;
Chris Lattnere79379a2018-06-22 10:39:19 -07001628
1629 case Token::kw_extfunc:
Chris Lattner2e595eb2018-07-10 10:08:27 -07001630 if (parseExtFunc())
1631 return ParseFailure;
Chris Lattnere79379a2018-06-22 10:39:19 -07001632 break;
1633
Chris Lattner4c95a502018-06-23 16:03:42 -07001634 case Token::kw_cfgfunc:
Chris Lattner2e595eb2018-07-10 10:08:27 -07001635 if (parseCFGFunc())
1636 return ParseFailure;
MLIR Teamf85a6262018-06-27 11:03:08 -07001637 break;
Chris Lattner4c95a502018-06-23 16:03:42 -07001638
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -07001639 case Token::kw_mlfunc:
Chris Lattner2e595eb2018-07-10 10:08:27 -07001640 if (parseMLFunc())
1641 return ParseFailure;
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -07001642 break;
1643
Uday Bondhugula015cbb12018-07-03 20:16:08 -07001644 // TODO: affine entity declarations, etc.
Chris Lattnere79379a2018-06-22 10:39:19 -07001645 }
1646 }
1647}
1648
1649//===----------------------------------------------------------------------===//
1650
Jacques Pienaar7b829702018-07-03 13:24:09 -07001651void mlir::defaultErrorReporter(const llvm::SMDiagnostic &error) {
1652 const auto &sourceMgr = *error.getSourceMgr();
1653 sourceMgr.PrintMessage(error.getLoc(), error.getKind(), error.getMessage());
1654}
1655
Chris Lattnere79379a2018-06-22 10:39:19 -07001656/// This parses the file specified by the indicated SourceMgr and returns an
1657/// MLIR module if it was valid. If not, it emits diagnostics and returns null.
Jacques Pienaar9c411be2018-06-24 19:17:35 -07001658Module *mlir::parseSourceFile(llvm::SourceMgr &sourceMgr, MLIRContext *context,
Jacques Pienaar7b829702018-07-03 13:24:09 -07001659 SMDiagnosticHandlerTy errorReporter) {
Chris Lattner2e595eb2018-07-10 10:08:27 -07001660 // This is the result module we are parsing into.
1661 std::unique_ptr<Module> module(new Module(context));
1662
1663 ParserState state(sourceMgr, module.get(),
Chris Lattner48af7d12018-07-09 19:05:38 -07001664 errorReporter ? std::move(errorReporter)
1665 : defaultErrorReporter);
Chris Lattner2e595eb2018-07-10 10:08:27 -07001666 if (ModuleParser(state).parseModule())
1667 return nullptr;
Chris Lattner21e67f62018-07-06 10:46:19 -07001668
1669 // Make sure the parse module has no other structural problems detected by the
1670 // verifier.
Chris Lattner2e595eb2018-07-10 10:08:27 -07001671 module->verify();
1672 return module.release();
Chris Lattnere79379a2018-06-22 10:39:19 -07001673}