2018-06-23 01:39:19 +08:00
|
|
|
//===- Parser.cpp - MLIR Parser Implementation ----------------------------===//
|
|
|
|
//
|
|
|
|
// Copyright 2019 The MLIR Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
// =============================================================================
|
|
|
|
//
|
|
|
|
// This file implements the parser for the MLIR textual form.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "mlir/Parser.h"
|
|
|
|
#include "Lexer.h"
|
2018-06-30 09:09:29 +08:00
|
|
|
#include "mlir/IR/AffineExpr.h"
|
2018-06-28 02:03:08 +08:00
|
|
|
#include "mlir/IR/AffineMap.h"
|
2018-07-05 11:45:39 +08:00
|
|
|
#include "mlir/IR/Attributes.h"
|
2018-07-09 11:51:38 +08:00
|
|
|
#include "mlir/IR/Builders.h"
|
2018-06-29 08:02:32 +08:00
|
|
|
#include "mlir/IR/MLFunction.h"
|
2018-07-07 01:46:19 +08:00
|
|
|
#include "mlir/IR/Module.h"
|
|
|
|
#include "mlir/IR/OperationSet.h"
|
2018-06-23 13:03:48 +08:00
|
|
|
#include "mlir/IR/Types.h"
|
2018-06-23 01:39:19 +08:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
|
|
|
using namespace mlir;
|
|
|
|
using llvm::SourceMgr;
|
2018-06-24 07:03:42 +08:00
|
|
|
using llvm::SMLoc;
|
2018-06-23 01:39:19 +08:00
|
|
|
|
2018-06-23 13:03:48 +08:00
|
|
|
/// Simple enum to make code read better in cases that would otherwise return a
|
|
|
|
/// bool value. Failure is "true" in a boolean context.
|
2018-06-23 01:39:19 +08:00
|
|
|
enum ParseResult {
|
|
|
|
ParseSuccess,
|
|
|
|
ParseFailure
|
|
|
|
};
|
|
|
|
|
2018-07-10 10:05:38 +08:00
|
|
|
namespace {
|
|
|
|
class Parser;
|
|
|
|
|
|
|
|
/// This class refers to all of the state maintained globally by the parser,
|
|
|
|
/// such as the current lexer position etc. The Parser base class provides
|
|
|
|
/// methods to access this.
|
|
|
|
class ParserState {
|
2018-06-29 11:45:33 +08:00
|
|
|
public:
|
2018-07-11 01:08:27 +08:00
|
|
|
ParserState(llvm::SourceMgr &sourceMgr, Module *module,
|
2018-07-10 10:05:38 +08:00
|
|
|
SMDiagnosticHandlerTy errorReporter)
|
2018-07-11 01:08:27 +08:00
|
|
|
: context(module->getContext()), module(module),
|
|
|
|
lex(sourceMgr, errorReporter), curToken(lex.lexToken()),
|
2018-07-11 15:07:36 +08:00
|
|
|
errorReporter(errorReporter) {}
|
2018-07-11 01:08:27 +08:00
|
|
|
|
|
|
|
// A map from affine map identifier to AffineMap.
|
|
|
|
llvm::StringMap<AffineMap *> affineMapDefinitions;
|
2018-06-23 01:39:19 +08:00
|
|
|
|
|
|
|
private:
|
2018-07-10 10:05:38 +08:00
|
|
|
ParserState(const ParserState &) = delete;
|
|
|
|
void operator=(const ParserState &) = delete;
|
|
|
|
|
|
|
|
friend class Parser;
|
|
|
|
|
|
|
|
// The context we're parsing into.
|
2018-07-11 01:08:27 +08:00
|
|
|
MLIRContext *const context;
|
|
|
|
|
|
|
|
// This is the module we are parsing into.
|
|
|
|
Module *const module;
|
2018-06-23 13:03:48 +08:00
|
|
|
|
|
|
|
// The lexer for the source file we're parsing.
|
2018-06-23 01:39:19 +08:00
|
|
|
Lexer lex;
|
|
|
|
|
|
|
|
// This is the next token that hasn't been consumed yet.
|
|
|
|
Token curToken;
|
|
|
|
|
2018-06-25 10:17:35 +08:00
|
|
|
// The diagnostic error reporter.
|
2018-07-11 01:08:27 +08:00
|
|
|
SMDiagnosticHandlerTy const errorReporter;
|
2018-07-10 10:05:38 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// This class implement support for parsing global entities like types and
|
|
|
|
/// shared entities like SSA names. It is intended to be subclassed by
|
|
|
|
/// specialized subparsers that include state, e.g. when a local symbol table.
|
|
|
|
class Parser {
|
|
|
|
public:
|
2018-07-11 01:08:27 +08:00
|
|
|
Builder builder;
|
|
|
|
|
|
|
|
Parser(ParserState &state) : builder(state.context), state(state) {}
|
2018-06-28 02:03:08 +08:00
|
|
|
|
2018-07-11 01:08:27 +08:00
|
|
|
// Helper methods to get stuff from the parser-global state.
|
|
|
|
ParserState &getState() const { return state; }
|
2018-07-10 10:05:38 +08:00
|
|
|
MLIRContext *getContext() const { return state.context; }
|
2018-07-11 01:08:27 +08:00
|
|
|
Module *getModule() { return state.module; }
|
2018-07-10 10:05:38 +08:00
|
|
|
|
|
|
|
/// Return the current token the parser is inspecting.
|
|
|
|
const Token &getToken() const { return state.curToken; }
|
|
|
|
StringRef getTokenSpelling() const { return state.curToken.getSpelling(); }
|
2018-06-23 01:39:19 +08:00
|
|
|
|
|
|
|
/// Emit an error and return failure.
|
2018-06-24 07:03:42 +08:00
|
|
|
ParseResult emitError(const Twine &message) {
|
2018-07-10 10:05:38 +08:00
|
|
|
return emitError(state.curToken.getLoc(), message);
|
2018-06-24 07:03:42 +08:00
|
|
|
}
|
|
|
|
ParseResult emitError(SMLoc loc, const Twine &message);
|
2018-06-23 01:39:19 +08:00
|
|
|
|
|
|
|
/// Advance the current lexer onto the next token.
|
|
|
|
void consumeToken() {
|
2018-07-10 10:05:38 +08:00
|
|
|
assert(state.curToken.isNot(Token::eof, Token::error) &&
|
2018-06-23 01:39:19 +08:00
|
|
|
"shouldn't advance past EOF or errors");
|
2018-07-10 10:05:38 +08:00
|
|
|
state.curToken = state.lex.lexToken();
|
2018-06-23 01:39:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Advance the current lexer onto the next token, asserting what the expected
|
|
|
|
/// current token is. This is preferred to the above method because it leads
|
|
|
|
/// to more self-documenting code with better checking.
|
2018-06-30 02:15:56 +08:00
|
|
|
void consumeToken(Token::Kind kind) {
|
2018-07-10 10:05:38 +08:00
|
|
|
assert(state.curToken.is(kind) && "consumed an unexpected token");
|
2018-06-23 01:39:19 +08:00
|
|
|
consumeToken();
|
|
|
|
}
|
|
|
|
|
2018-06-23 06:52:02 +08:00
|
|
|
/// If the current token has the specified kind, consume it and return true.
|
|
|
|
/// If not, return false.
|
2018-06-30 02:15:56 +08:00
|
|
|
bool consumeIf(Token::Kind kind) {
|
2018-07-10 10:05:38 +08:00
|
|
|
if (state.curToken.isNot(kind))
|
2018-06-23 06:52:02 +08:00
|
|
|
return false;
|
|
|
|
consumeToken(kind);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-30 02:15:56 +08:00
|
|
|
ParseResult parseCommaSeparatedList(Token::Kind rightToken,
|
2018-06-23 06:52:02 +08:00
|
|
|
const std::function<ParseResult()> &parseElement,
|
|
|
|
bool allowEmptyList = true);
|
|
|
|
|
2018-06-23 13:03:48 +08:00
|
|
|
// We have two forms of parsing methods - those that return a non-null
|
|
|
|
// pointer on success, and those that return a ParseResult to indicate whether
|
|
|
|
// they returned a failure. The second class fills in by-reference arguments
|
|
|
|
// as the results of their action.
|
|
|
|
|
2018-06-23 01:39:19 +08:00
|
|
|
// Type parsing.
|
2018-06-30 13:08:05 +08:00
|
|
|
Type *parsePrimitiveType();
|
2018-06-23 13:03:48 +08:00
|
|
|
Type *parseElementType();
|
|
|
|
VectorType *parseVectorType();
|
2018-06-23 06:52:02 +08:00
|
|
|
ParseResult parseDimensionListRanked(SmallVectorImpl<int> &dimensions);
|
2018-06-23 13:03:48 +08:00
|
|
|
Type *parseTensorType();
|
|
|
|
Type *parseMemRefType();
|
|
|
|
Type *parseFunctionType();
|
|
|
|
Type *parseType();
|
|
|
|
ParseResult parseTypeList(SmallVectorImpl<Type*> &elements);
|
2018-06-23 01:39:19 +08:00
|
|
|
|
2018-07-05 11:45:39 +08:00
|
|
|
// Attribute parsing.
|
|
|
|
Attribute *parseAttribute();
|
|
|
|
ParseResult parseAttributeDict(SmallVectorImpl<NamedAttribute> &attributes);
|
|
|
|
|
2018-07-04 11:16:08 +08:00
|
|
|
// Polyhedral structures.
|
2018-07-11 01:08:27 +08:00
|
|
|
AffineMap *parseAffineMapInline();
|
2018-06-28 02:03:08 +08:00
|
|
|
|
2018-07-08 06:48:26 +08:00
|
|
|
// SSA
|
|
|
|
ParseResult parseSSAUse();
|
|
|
|
ParseResult parseOptionalSSAUseList(Token::Kind endToken);
|
|
|
|
ParseResult parseSSAUseAndType();
|
|
|
|
ParseResult parseOptionalSSAUseAndTypeList(Token::Kind endToken);
|
|
|
|
|
2018-07-10 10:05:38 +08:00
|
|
|
private:
|
|
|
|
// The Parser is subclassed and reinstantiated. Do not add additional
|
|
|
|
// non-trivial state here, add it to the ParserState class.
|
|
|
|
ParserState &state;
|
2018-06-23 01:39:19 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Helper methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-06-24 07:03:42 +08:00
|
|
|
ParseResult Parser::emitError(SMLoc loc, const Twine &message) {
|
2018-06-23 06:52:02 +08:00
|
|
|
// If we hit a parse error in response to a lexer error, then the lexer
|
2018-06-25 10:17:35 +08:00
|
|
|
// already reported the error.
|
2018-07-10 10:05:38 +08:00
|
|
|
if (getToken().is(Token::error))
|
2018-06-23 06:52:02 +08:00
|
|
|
return ParseFailure;
|
|
|
|
|
2018-07-10 10:05:38 +08:00
|
|
|
auto &sourceMgr = state.lex.getSourceMgr();
|
|
|
|
state.errorReporter(sourceMgr.GetMessage(loc, SourceMgr::DK_Error, message));
|
2018-06-23 01:39:19 +08:00
|
|
|
return ParseFailure;
|
|
|
|
}
|
|
|
|
|
2018-06-23 06:52:02 +08:00
|
|
|
/// Parse a comma-separated list of elements, terminated with an arbitrary
|
|
|
|
/// token. This allows empty lists if allowEmptyList is true.
|
|
|
|
///
|
|
|
|
/// abstract-list ::= rightToken // if allowEmptyList == true
|
|
|
|
/// abstract-list ::= element (',' element)* rightToken
|
|
|
|
///
|
|
|
|
ParseResult Parser::
|
2018-06-30 02:15:56 +08:00
|
|
|
parseCommaSeparatedList(Token::Kind rightToken,
|
2018-06-23 06:52:02 +08:00
|
|
|
const std::function<ParseResult()> &parseElement,
|
|
|
|
bool allowEmptyList) {
|
|
|
|
// Handle the empty case.
|
2018-07-10 10:05:38 +08:00
|
|
|
if (getToken().is(rightToken)) {
|
2018-06-23 06:52:02 +08:00
|
|
|
if (!allowEmptyList)
|
|
|
|
return emitError("expected list element");
|
|
|
|
consumeToken(rightToken);
|
|
|
|
return ParseSuccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Non-empty case starts with an element.
|
|
|
|
if (parseElement())
|
|
|
|
return ParseFailure;
|
|
|
|
|
|
|
|
// Otherwise we have a list of comma separated elements.
|
|
|
|
while (consumeIf(Token::comma)) {
|
|
|
|
if (parseElement())
|
|
|
|
return ParseFailure;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Consume the end character.
|
|
|
|
if (!consumeIf(rightToken))
|
2018-06-30 02:15:56 +08:00
|
|
|
return emitError("expected ',' or '" + Token::getTokenSpelling(rightToken) +
|
|
|
|
"'");
|
2018-06-23 06:52:02 +08:00
|
|
|
|
|
|
|
return ParseSuccess;
|
|
|
|
}
|
2018-06-23 01:39:19 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Type Parsing
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-06-23 06:52:02 +08:00
|
|
|
/// Parse the low-level fixed dtypes in the system.
|
|
|
|
///
|
2018-06-30 13:08:05 +08:00
|
|
|
/// primitive-type ::= `f16` | `bf16` | `f32` | `f64`
|
|
|
|
/// primitive-type ::= integer-type
|
|
|
|
/// primitive-type ::= `affineint`
|
2018-06-23 06:52:02 +08:00
|
|
|
///
|
2018-06-30 13:08:05 +08:00
|
|
|
Type *Parser::parsePrimitiveType() {
|
2018-07-10 10:05:38 +08:00
|
|
|
switch (getToken().getKind()) {
|
2018-06-23 13:03:48 +08:00
|
|
|
default:
|
|
|
|
return (emitError("expected type"), nullptr);
|
2018-06-23 06:52:02 +08:00
|
|
|
case Token::kw_bf16:
|
|
|
|
consumeToken(Token::kw_bf16);
|
2018-07-09 11:51:38 +08:00
|
|
|
return builder.getBF16Type();
|
2018-06-23 06:52:02 +08:00
|
|
|
case Token::kw_f16:
|
|
|
|
consumeToken(Token::kw_f16);
|
2018-07-09 11:51:38 +08:00
|
|
|
return builder.getF16Type();
|
2018-06-23 06:52:02 +08:00
|
|
|
case Token::kw_f32:
|
|
|
|
consumeToken(Token::kw_f32);
|
2018-07-09 11:51:38 +08:00
|
|
|
return builder.getF32Type();
|
2018-06-23 06:52:02 +08:00
|
|
|
case Token::kw_f64:
|
|
|
|
consumeToken(Token::kw_f64);
|
2018-07-09 11:51:38 +08:00
|
|
|
return builder.getF64Type();
|
2018-06-30 13:08:05 +08:00
|
|
|
case Token::kw_affineint:
|
|
|
|
consumeToken(Token::kw_affineint);
|
2018-07-09 11:51:38 +08:00
|
|
|
return builder.getAffineIntType();
|
2018-06-30 13:08:05 +08:00
|
|
|
case Token::inttype: {
|
2018-07-10 10:05:38 +08:00
|
|
|
auto width = getToken().getIntTypeBitwidth();
|
2018-06-30 13:08:05 +08:00
|
|
|
if (!width.hasValue())
|
|
|
|
return (emitError("invalid integer width"), nullptr);
|
|
|
|
consumeToken(Token::inttype);
|
2018-07-09 11:51:38 +08:00
|
|
|
return builder.getIntegerType(width.getValue());
|
2018-06-30 13:08:05 +08:00
|
|
|
}
|
2018-06-23 06:52:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse the element type of a tensor or memref type.
|
|
|
|
///
|
|
|
|
/// element-type ::= primitive-type | vector-type
|
|
|
|
///
|
2018-06-23 13:03:48 +08:00
|
|
|
Type *Parser::parseElementType() {
|
2018-07-10 10:05:38 +08:00
|
|
|
if (getToken().is(Token::kw_vector))
|
2018-06-23 06:52:02 +08:00
|
|
|
return parseVectorType();
|
|
|
|
|
|
|
|
return parsePrimitiveType();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse a vector type.
|
|
|
|
///
|
|
|
|
/// vector-type ::= `vector` `<` const-dimension-list primitive-type `>`
|
|
|
|
/// const-dimension-list ::= (integer-literal `x`)+
|
|
|
|
///
|
2018-06-23 13:03:48 +08:00
|
|
|
VectorType *Parser::parseVectorType() {
|
2018-06-23 06:52:02 +08:00
|
|
|
consumeToken(Token::kw_vector);
|
|
|
|
|
|
|
|
if (!consumeIf(Token::less))
|
2018-06-23 13:03:48 +08:00
|
|
|
return (emitError("expected '<' in vector type"), nullptr);
|
2018-06-23 06:52:02 +08:00
|
|
|
|
2018-07-10 10:05:38 +08:00
|
|
|
if (getToken().isNot(Token::integer))
|
2018-06-23 13:03:48 +08:00
|
|
|
return (emitError("expected dimension size in vector type"), nullptr);
|
2018-06-23 06:52:02 +08:00
|
|
|
|
|
|
|
SmallVector<unsigned, 4> dimensions;
|
2018-07-10 10:05:38 +08:00
|
|
|
while (getToken().is(Token::integer)) {
|
2018-06-23 06:52:02 +08:00
|
|
|
// Make sure this integer value is in bound and valid.
|
2018-07-10 10:05:38 +08:00
|
|
|
auto dimension = getToken().getUnsignedIntegerValue();
|
2018-06-23 06:52:02 +08:00
|
|
|
if (!dimension.hasValue())
|
2018-06-23 13:03:48 +08:00
|
|
|
return (emitError("invalid dimension in vector type"), nullptr);
|
2018-06-23 06:52:02 +08:00
|
|
|
dimensions.push_back(dimension.getValue());
|
|
|
|
|
|
|
|
consumeToken(Token::integer);
|
|
|
|
|
|
|
|
// Make sure we have an 'x' or something like 'xbf32'.
|
2018-07-10 10:05:38 +08:00
|
|
|
if (getToken().isNot(Token::bare_identifier) ||
|
|
|
|
getTokenSpelling()[0] != 'x')
|
2018-06-23 13:03:48 +08:00
|
|
|
return (emitError("expected 'x' in vector dimension list"), nullptr);
|
2018-06-23 06:52:02 +08:00
|
|
|
|
|
|
|
// If we had a prefix of 'x', lex the next token immediately after the 'x'.
|
2018-07-10 10:05:38 +08:00
|
|
|
if (getTokenSpelling().size() != 1)
|
|
|
|
state.lex.resetPointer(getTokenSpelling().data() + 1);
|
2018-06-23 06:52:02 +08:00
|
|
|
|
|
|
|
// Consume the 'x'.
|
|
|
|
consumeToken(Token::bare_identifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the element type.
|
2018-06-23 13:03:48 +08:00
|
|
|
auto *elementType = parsePrimitiveType();
|
|
|
|
if (!elementType)
|
|
|
|
return nullptr;
|
2018-06-23 06:52:02 +08:00
|
|
|
|
|
|
|
if (!consumeIf(Token::greater))
|
2018-06-23 13:03:48 +08:00
|
|
|
return (emitError("expected '>' in vector type"), nullptr);
|
2018-06-23 06:52:02 +08:00
|
|
|
|
2018-06-23 13:03:48 +08:00
|
|
|
return VectorType::get(dimensions, elementType);
|
2018-06-23 06:52:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse a dimension list of a tensor or memref type. This populates the
|
|
|
|
/// dimension list, returning -1 for the '?' dimensions.
|
|
|
|
///
|
|
|
|
/// dimension-list-ranked ::= (dimension `x`)*
|
|
|
|
/// dimension ::= `?` | integer-literal
|
|
|
|
///
|
|
|
|
ParseResult Parser::parseDimensionListRanked(SmallVectorImpl<int> &dimensions) {
|
2018-07-10 10:05:38 +08:00
|
|
|
while (getToken().isAny(Token::integer, Token::question)) {
|
2018-06-23 06:52:02 +08:00
|
|
|
if (consumeIf(Token::question)) {
|
|
|
|
dimensions.push_back(-1);
|
|
|
|
} else {
|
|
|
|
// Make sure this integer value is in bound and valid.
|
2018-07-10 10:05:38 +08:00
|
|
|
auto dimension = getToken().getUnsignedIntegerValue();
|
2018-06-23 06:52:02 +08:00
|
|
|
if (!dimension.hasValue() || (int)dimension.getValue() < 0)
|
|
|
|
return emitError("invalid dimension");
|
|
|
|
dimensions.push_back((int)dimension.getValue());
|
|
|
|
consumeToken(Token::integer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we have an 'x' or something like 'xbf32'.
|
2018-07-10 10:05:38 +08:00
|
|
|
if (getToken().isNot(Token::bare_identifier) ||
|
|
|
|
getTokenSpelling()[0] != 'x')
|
2018-06-23 06:52:02 +08:00
|
|
|
return emitError("expected 'x' in dimension list");
|
|
|
|
|
|
|
|
// If we had a prefix of 'x', lex the next token immediately after the 'x'.
|
2018-07-10 10:05:38 +08:00
|
|
|
if (getTokenSpelling().size() != 1)
|
|
|
|
state.lex.resetPointer(getTokenSpelling().data() + 1);
|
2018-06-23 06:52:02 +08:00
|
|
|
|
|
|
|
// Consume the 'x'.
|
|
|
|
consumeToken(Token::bare_identifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ParseSuccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse a tensor type.
|
|
|
|
///
|
|
|
|
/// tensor-type ::= `tensor` `<` dimension-list element-type `>`
|
|
|
|
/// dimension-list ::= dimension-list-ranked | `??`
|
|
|
|
///
|
2018-06-23 13:03:48 +08:00
|
|
|
Type *Parser::parseTensorType() {
|
2018-06-23 06:52:02 +08:00
|
|
|
consumeToken(Token::kw_tensor);
|
|
|
|
|
|
|
|
if (!consumeIf(Token::less))
|
2018-06-23 13:03:48 +08:00
|
|
|
return (emitError("expected '<' in tensor type"), nullptr);
|
2018-06-23 06:52:02 +08:00
|
|
|
|
|
|
|
bool isUnranked;
|
|
|
|
SmallVector<int, 4> dimensions;
|
|
|
|
|
|
|
|
if (consumeIf(Token::questionquestion)) {
|
|
|
|
isUnranked = true;
|
|
|
|
} else {
|
|
|
|
isUnranked = false;
|
|
|
|
if (parseDimensionListRanked(dimensions))
|
2018-06-23 13:03:48 +08:00
|
|
|
return nullptr;
|
2018-06-23 06:52:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the element type.
|
2018-06-23 13:03:48 +08:00
|
|
|
auto elementType = parseElementType();
|
|
|
|
if (!elementType)
|
|
|
|
return nullptr;
|
2018-06-23 06:52:02 +08:00
|
|
|
|
|
|
|
if (!consumeIf(Token::greater))
|
2018-06-23 13:03:48 +08:00
|
|
|
return (emitError("expected '>' in tensor type"), nullptr);
|
2018-06-23 06:52:02 +08:00
|
|
|
|
2018-06-24 09:09:09 +08:00
|
|
|
if (isUnranked)
|
2018-07-09 11:51:38 +08:00
|
|
|
return builder.getTensorType(elementType);
|
|
|
|
return builder.getTensorType(dimensions, elementType);
|
2018-06-23 06:52:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse a memref type.
|
|
|
|
///
|
|
|
|
/// memref-type ::= `memref` `<` dimension-list-ranked element-type
|
|
|
|
/// (`,` semi-affine-map-composition)? (`,` memory-space)? `>`
|
|
|
|
///
|
|
|
|
/// semi-affine-map-composition ::= (semi-affine-map `,` )* semi-affine-map
|
|
|
|
/// memory-space ::= integer-literal /* | TODO: address-space-id */
|
|
|
|
///
|
2018-06-23 13:03:48 +08:00
|
|
|
Type *Parser::parseMemRefType() {
|
2018-06-23 06:52:02 +08:00
|
|
|
consumeToken(Token::kw_memref);
|
|
|
|
|
|
|
|
if (!consumeIf(Token::less))
|
2018-06-23 13:03:48 +08:00
|
|
|
return (emitError("expected '<' in memref type"), nullptr);
|
2018-06-23 06:52:02 +08:00
|
|
|
|
|
|
|
SmallVector<int, 4> dimensions;
|
|
|
|
if (parseDimensionListRanked(dimensions))
|
2018-06-23 13:03:48 +08:00
|
|
|
return nullptr;
|
2018-06-23 06:52:02 +08:00
|
|
|
|
|
|
|
// Parse the element type.
|
2018-06-23 13:03:48 +08:00
|
|
|
auto elementType = parseElementType();
|
|
|
|
if (!elementType)
|
|
|
|
return nullptr;
|
2018-06-23 06:52:02 +08:00
|
|
|
|
|
|
|
// TODO: Parse semi-affine-map-composition.
|
|
|
|
// TODO: Parse memory-space.
|
|
|
|
|
|
|
|
if (!consumeIf(Token::greater))
|
2018-06-23 13:03:48 +08:00
|
|
|
return (emitError("expected '>' in memref type"), nullptr);
|
2018-06-23 06:52:02 +08:00
|
|
|
|
2018-06-23 13:03:48 +08:00
|
|
|
// FIXME: Add an IR representation for memref types.
|
2018-07-09 11:51:38 +08:00
|
|
|
return builder.getIntegerType(1);
|
2018-06-23 06:52:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse a function type.
|
|
|
|
///
|
|
|
|
/// function-type ::= type-list-parens `->` type-list
|
|
|
|
///
|
2018-06-23 13:03:48 +08:00
|
|
|
Type *Parser::parseFunctionType() {
|
2018-07-10 10:05:38 +08:00
|
|
|
assert(getToken().is(Token::l_paren));
|
2018-06-23 06:52:02 +08:00
|
|
|
|
2018-06-23 13:03:48 +08:00
|
|
|
SmallVector<Type*, 4> arguments;
|
|
|
|
if (parseTypeList(arguments))
|
|
|
|
return nullptr;
|
2018-06-23 06:52:02 +08:00
|
|
|
|
|
|
|
if (!consumeIf(Token::arrow))
|
2018-06-23 13:03:48 +08:00
|
|
|
return (emitError("expected '->' in function type"), nullptr);
|
2018-06-23 06:52:02 +08:00
|
|
|
|
2018-06-23 13:03:48 +08:00
|
|
|
SmallVector<Type*, 4> results;
|
|
|
|
if (parseTypeList(results))
|
|
|
|
return nullptr;
|
2018-06-23 06:52:02 +08:00
|
|
|
|
2018-07-09 11:51:38 +08:00
|
|
|
return builder.getFunctionType(arguments, results);
|
2018-06-23 06:52:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse an arbitrary type.
|
|
|
|
///
|
|
|
|
/// type ::= primitive-type
|
|
|
|
/// | vector-type
|
|
|
|
/// | tensor-type
|
|
|
|
/// | memref-type
|
|
|
|
/// | function-type
|
|
|
|
/// element-type ::= primitive-type | vector-type
|
|
|
|
///
|
2018-06-23 13:03:48 +08:00
|
|
|
Type *Parser::parseType() {
|
2018-07-10 10:05:38 +08:00
|
|
|
switch (getToken().getKind()) {
|
2018-06-23 06:52:02 +08:00
|
|
|
case Token::kw_memref: return parseMemRefType();
|
|
|
|
case Token::kw_tensor: return parseTensorType();
|
|
|
|
case Token::kw_vector: return parseVectorType();
|
|
|
|
case Token::l_paren: return parseFunctionType();
|
|
|
|
default:
|
|
|
|
return parsePrimitiveType();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse a "type list", which is a singular type, or a parenthesized list of
|
|
|
|
/// types.
|
|
|
|
///
|
|
|
|
/// type-list ::= type-list-parens | type
|
|
|
|
/// type-list-parens ::= `(` `)`
|
|
|
|
/// | `(` type (`,` type)* `)`
|
|
|
|
///
|
2018-06-23 13:03:48 +08:00
|
|
|
ParseResult Parser::parseTypeList(SmallVectorImpl<Type*> &elements) {
|
|
|
|
auto parseElt = [&]() -> ParseResult {
|
|
|
|
auto elt = parseType();
|
|
|
|
elements.push_back(elt);
|
|
|
|
return elt ? ParseSuccess : ParseFailure;
|
|
|
|
};
|
|
|
|
|
2018-06-23 06:52:02 +08:00
|
|
|
// If there is no parens, then it must be a singular type.
|
|
|
|
if (!consumeIf(Token::l_paren))
|
2018-06-23 13:03:48 +08:00
|
|
|
return parseElt();
|
2018-06-23 06:52:02 +08:00
|
|
|
|
2018-06-23 13:03:48 +08:00
|
|
|
if (parseCommaSeparatedList(Token::r_paren, parseElt))
|
2018-06-23 06:52:02 +08:00
|
|
|
return ParseFailure;
|
|
|
|
|
|
|
|
return ParseSuccess;
|
|
|
|
}
|
|
|
|
|
2018-07-05 11:45:39 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Attribute parsing.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
|
|
/// Attribute parsing.
|
|
|
|
///
|
|
|
|
/// attribute-value ::= bool-literal
|
|
|
|
/// | integer-literal
|
|
|
|
/// | float-literal
|
|
|
|
/// | string-literal
|
|
|
|
/// | `[` (attribute-value (`,` attribute-value)*)? `]`
|
|
|
|
///
|
|
|
|
Attribute *Parser::parseAttribute() {
|
2018-07-10 10:05:38 +08:00
|
|
|
switch (getToken().getKind()) {
|
2018-07-05 11:45:39 +08:00
|
|
|
case Token::kw_true:
|
|
|
|
consumeToken(Token::kw_true);
|
2018-07-11 01:59:53 +08:00
|
|
|
return builder.getBoolAttr(true);
|
2018-07-05 11:45:39 +08:00
|
|
|
case Token::kw_false:
|
|
|
|
consumeToken(Token::kw_false);
|
2018-07-11 01:59:53 +08:00
|
|
|
return builder.getBoolAttr(false);
|
2018-07-05 11:45:39 +08:00
|
|
|
|
|
|
|
case Token::integer: {
|
2018-07-10 10:05:38 +08:00
|
|
|
auto val = getToken().getUInt64IntegerValue();
|
2018-07-05 11:45:39 +08:00
|
|
|
if (!val.hasValue() || (int64_t)val.getValue() < 0)
|
|
|
|
return (emitError("integer too large for attribute"), nullptr);
|
|
|
|
consumeToken(Token::integer);
|
2018-07-11 01:59:53 +08:00
|
|
|
return builder.getIntegerAttr((int64_t)val.getValue());
|
2018-07-05 11:45:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case Token::minus: {
|
|
|
|
consumeToken(Token::minus);
|
2018-07-10 10:05:38 +08:00
|
|
|
if (getToken().is(Token::integer)) {
|
|
|
|
auto val = getToken().getUInt64IntegerValue();
|
2018-07-05 11:45:39 +08:00
|
|
|
if (!val.hasValue() || (int64_t)-val.getValue() >= 0)
|
|
|
|
return (emitError("integer too large for attribute"), nullptr);
|
|
|
|
consumeToken(Token::integer);
|
2018-07-11 01:59:53 +08:00
|
|
|
return builder.getIntegerAttr((int64_t)-val.getValue());
|
2018-07-05 11:45:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return (emitError("expected constant integer or floating point value"),
|
|
|
|
nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
case Token::string: {
|
2018-07-10 10:05:38 +08:00
|
|
|
auto val = getToken().getStringValue();
|
2018-07-05 11:45:39 +08:00
|
|
|
consumeToken(Token::string);
|
2018-07-11 01:59:53 +08:00
|
|
|
return builder.getStringAttr(val);
|
2018-07-05 11:45:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case Token::l_bracket: {
|
|
|
|
consumeToken(Token::l_bracket);
|
|
|
|
SmallVector<Attribute*, 4> elements;
|
|
|
|
|
|
|
|
auto parseElt = [&]() -> ParseResult {
|
|
|
|
elements.push_back(parseAttribute());
|
|
|
|
return elements.back() ? ParseSuccess : ParseFailure;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (parseCommaSeparatedList(Token::r_bracket, parseElt))
|
|
|
|
return nullptr;
|
2018-07-11 01:59:53 +08:00
|
|
|
return builder.getArrayAttr(elements);
|
2018-07-05 11:45:39 +08:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
// TODO: Handle floating point.
|
|
|
|
return (emitError("expected constant attribute value"), nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Attribute dictionary.
|
|
|
|
///
|
2018-07-10 00:00:25 +08:00
|
|
|
/// attribute-dict ::= `{` `}`
|
|
|
|
/// | `{` attribute-entry (`,` attribute-entry)* `}`
|
|
|
|
/// attribute-entry ::= bare-id `:` attribute-value
|
2018-07-05 11:45:39 +08:00
|
|
|
///
|
|
|
|
ParseResult Parser::parseAttributeDict(
|
|
|
|
SmallVectorImpl<NamedAttribute> &attributes) {
|
|
|
|
consumeToken(Token::l_brace);
|
|
|
|
|
|
|
|
auto parseElt = [&]() -> ParseResult {
|
|
|
|
// We allow keywords as attribute names.
|
2018-07-10 10:05:38 +08:00
|
|
|
if (getToken().isNot(Token::bare_identifier, Token::inttype) &&
|
|
|
|
!getToken().isKeyword())
|
2018-07-05 11:45:39 +08:00
|
|
|
return emitError("expected attribute name");
|
2018-07-11 01:59:53 +08:00
|
|
|
auto nameId = builder.getIdentifier(getTokenSpelling());
|
2018-07-05 11:45:39 +08:00
|
|
|
consumeToken();
|
|
|
|
|
|
|
|
if (!consumeIf(Token::colon))
|
|
|
|
return emitError("expected ':' in attribute list");
|
|
|
|
|
|
|
|
auto attr = parseAttribute();
|
|
|
|
if (!attr) return ParseFailure;
|
|
|
|
|
|
|
|
attributes.push_back({nameId, attr});
|
|
|
|
return ParseSuccess;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (parseCommaSeparatedList(Token::r_brace, parseElt))
|
|
|
|
return ParseFailure;
|
|
|
|
|
|
|
|
return ParseSuccess;
|
|
|
|
}
|
|
|
|
|
2018-06-28 02:03:08 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Polyhedral structures.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-07-11 01:08:27 +08:00
|
|
|
/// Lower precedence ops (all at the same precedence level). LNoOp is false in
|
|
|
|
/// the boolean sense.
|
|
|
|
enum AffineLowPrecOp {
|
|
|
|
/// Null value.
|
|
|
|
LNoOp,
|
|
|
|
Add,
|
|
|
|
Sub
|
|
|
|
};
|
2018-06-28 02:03:08 +08:00
|
|
|
|
2018-07-11 01:08:27 +08:00
|
|
|
/// Higher precedence ops - all at the same precedence level. HNoOp is false in
|
|
|
|
/// the boolean sense.
|
|
|
|
enum AffineHighPrecOp {
|
|
|
|
/// Null value.
|
|
|
|
HNoOp,
|
|
|
|
Mul,
|
|
|
|
FloorDiv,
|
|
|
|
CeilDiv,
|
|
|
|
Mod
|
|
|
|
};
|
2018-06-30 09:09:29 +08:00
|
|
|
|
2018-07-11 01:08:27 +08:00
|
|
|
namespace {
|
|
|
|
/// This is a specialized parser for AffineMap's, maintaining the state
|
|
|
|
/// transient to their bodies.
|
|
|
|
class AffineMapParser : public Parser {
|
|
|
|
public:
|
|
|
|
explicit AffineMapParser(ParserState &state) : Parser(state) {}
|
2018-07-05 11:45:39 +08:00
|
|
|
|
2018-07-11 01:08:27 +08:00
|
|
|
AffineMap *parseAffineMapInline();
|
2018-07-05 11:45:39 +08:00
|
|
|
|
2018-07-11 01:08:27 +08:00
|
|
|
private:
|
|
|
|
unsigned getNumDims() const { return dims.size(); }
|
|
|
|
unsigned getNumSymbols() const { return symbols.size(); }
|
2018-06-28 02:03:08 +08:00
|
|
|
|
2018-07-12 12:31:07 +08:00
|
|
|
/// Returns true if the only identifiers the parser accepts in affine
|
|
|
|
/// expressions are symbolic identifiers.
|
|
|
|
bool isPureSymbolic() const { return pureSymbolic; }
|
|
|
|
void setSymbolicParsing(bool val) { pureSymbolic = val; }
|
|
|
|
|
2018-07-11 01:08:27 +08:00
|
|
|
// Binary affine op parsing.
|
|
|
|
AffineLowPrecOp consumeIfLowPrecOp();
|
|
|
|
AffineHighPrecOp consumeIfHighPrecOp();
|
2018-06-28 02:03:08 +08:00
|
|
|
|
2018-07-11 01:08:27 +08:00
|
|
|
// Identifier lists for polyhedral structures.
|
|
|
|
ParseResult parseDimIdList();
|
|
|
|
ParseResult parseSymbolIdList();
|
|
|
|
ParseResult parseDimOrSymbolId(bool isDim);
|
|
|
|
|
|
|
|
AffineExpr *parseAffineExpr();
|
|
|
|
AffineExpr *parseParentheticalExpr();
|
|
|
|
AffineExpr *parseNegateExpression(AffineExpr *lhs);
|
|
|
|
AffineExpr *parseIntegerExpr();
|
|
|
|
AffineExpr *parseBareIdExpr();
|
|
|
|
|
|
|
|
AffineExpr *getBinaryAffineOpExpr(AffineHighPrecOp op, AffineExpr *lhs,
|
|
|
|
AffineExpr *rhs);
|
|
|
|
AffineExpr *getBinaryAffineOpExpr(AffineLowPrecOp op, AffineExpr *lhs,
|
|
|
|
AffineExpr *rhs);
|
|
|
|
AffineExpr *parseAffineOperandExpr(AffineExpr *lhs);
|
|
|
|
AffineExpr *parseAffineLowPrecOpExpr(AffineExpr *llhs,
|
|
|
|
AffineLowPrecOp llhsOp);
|
|
|
|
AffineExpr *parseAffineHighPrecOpExpr(AffineExpr *llhs,
|
|
|
|
AffineHighPrecOp llhsOp);
|
|
|
|
|
|
|
|
private:
|
|
|
|
// TODO(bondhugula): could just use an vector/ArrayRef and scan the numbers.
|
|
|
|
llvm::StringMap<unsigned> dims;
|
|
|
|
llvm::StringMap<unsigned> symbols;
|
2018-07-12 12:31:07 +08:00
|
|
|
/// True if the parser should allow only symbolic identifiers in affine
|
|
|
|
/// expressions.
|
|
|
|
bool pureSymbolic = false;
|
2018-07-11 01:08:27 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
2018-07-04 11:16:08 +08:00
|
|
|
|
2018-07-10 00:00:25 +08:00
|
|
|
/// Create an affine binary high precedence op expression (mul's, div's, mod)
|
2018-07-11 01:08:27 +08:00
|
|
|
AffineExpr *AffineMapParser::getBinaryAffineOpExpr(AffineHighPrecOp op,
|
|
|
|
AffineExpr *lhs,
|
|
|
|
AffineExpr *rhs) {
|
2018-07-12 12:31:07 +08:00
|
|
|
// TODO: make the error location info accurate.
|
2018-07-04 11:16:08 +08:00
|
|
|
switch (op) {
|
|
|
|
case Mul:
|
2018-07-10 00:00:25 +08:00
|
|
|
if (!lhs->isSymbolic() && !rhs->isSymbolic()) {
|
|
|
|
emitError("non-affine expression: at least one of the multiply "
|
|
|
|
"operands has to be either a constant or symbolic");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-07-11 01:59:53 +08:00
|
|
|
return builder.getMulExpr(lhs, rhs);
|
2018-07-04 11:16:08 +08:00
|
|
|
case FloorDiv:
|
2018-07-10 00:00:25 +08:00
|
|
|
if (!rhs->isSymbolic()) {
|
|
|
|
emitError("non-affine expression: right operand of floordiv "
|
|
|
|
"has to be either a constant or symbolic");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-07-11 01:59:53 +08:00
|
|
|
return builder.getFloorDivExpr(lhs, rhs);
|
2018-07-04 11:16:08 +08:00
|
|
|
case CeilDiv:
|
2018-07-10 00:00:25 +08:00
|
|
|
if (!rhs->isSymbolic()) {
|
|
|
|
emitError("non-affine expression: right operand of ceildiv "
|
|
|
|
"has to be either a constant or symbolic");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-07-11 01:59:53 +08:00
|
|
|
return builder.getCeilDivExpr(lhs, rhs);
|
2018-07-04 11:16:08 +08:00
|
|
|
case Mod:
|
2018-07-10 00:00:25 +08:00
|
|
|
if (!rhs->isSymbolic()) {
|
|
|
|
emitError("non-affine expression: right operand of mod "
|
|
|
|
"has to be either a constant or symbolic");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-07-11 01:59:53 +08:00
|
|
|
return builder.getModExpr(lhs, rhs);
|
2018-07-04 11:16:08 +08:00
|
|
|
case HNoOp:
|
|
|
|
llvm_unreachable("can't create affine expression for null high prec op");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 00:00:25 +08:00
|
|
|
/// Create an affine binary low precedence op expression (add, sub).
|
2018-07-11 01:08:27 +08:00
|
|
|
AffineExpr *AffineMapParser::getBinaryAffineOpExpr(AffineLowPrecOp op,
|
|
|
|
AffineExpr *lhs,
|
|
|
|
AffineExpr *rhs) {
|
2018-07-04 11:16:08 +08:00
|
|
|
switch (op) {
|
|
|
|
case AffineLowPrecOp::Add:
|
2018-07-11 01:59:53 +08:00
|
|
|
return builder.getAddExpr(lhs, rhs);
|
2018-07-04 11:16:08 +08:00
|
|
|
case AffineLowPrecOp::Sub:
|
2018-07-11 01:59:53 +08:00
|
|
|
return builder.getSubExpr(lhs, rhs);
|
2018-07-04 11:16:08 +08:00
|
|
|
case AffineLowPrecOp::LNoOp:
|
|
|
|
llvm_unreachable("can't create affine expression for null low prec op");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Consume this token if it is a lower precedence affine op (there are only two
|
2018-07-10 00:00:25 +08:00
|
|
|
/// precedence levels).
|
2018-07-11 01:08:27 +08:00
|
|
|
AffineLowPrecOp AffineMapParser::consumeIfLowPrecOp() {
|
2018-07-10 10:05:38 +08:00
|
|
|
switch (getToken().getKind()) {
|
2018-07-04 11:16:08 +08:00
|
|
|
case Token::plus:
|
|
|
|
consumeToken(Token::plus);
|
|
|
|
return AffineLowPrecOp::Add;
|
|
|
|
case Token::minus:
|
|
|
|
consumeToken(Token::minus);
|
|
|
|
return AffineLowPrecOp::Sub;
|
|
|
|
default:
|
|
|
|
return AffineLowPrecOp::LNoOp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Consume this token if it is a higher precedence affine op (there are only
|
|
|
|
/// two precedence levels)
|
2018-07-11 01:08:27 +08:00
|
|
|
AffineHighPrecOp AffineMapParser::consumeIfHighPrecOp() {
|
2018-07-10 10:05:38 +08:00
|
|
|
switch (getToken().getKind()) {
|
2018-07-04 11:16:08 +08:00
|
|
|
case Token::star:
|
|
|
|
consumeToken(Token::star);
|
|
|
|
return Mul;
|
|
|
|
case Token::kw_floordiv:
|
|
|
|
consumeToken(Token::kw_floordiv);
|
|
|
|
return FloorDiv;
|
|
|
|
case Token::kw_ceildiv:
|
|
|
|
consumeToken(Token::kw_ceildiv);
|
|
|
|
return CeilDiv;
|
|
|
|
case Token::kw_mod:
|
|
|
|
consumeToken(Token::kw_mod);
|
|
|
|
return Mod;
|
|
|
|
default:
|
|
|
|
return HNoOp;
|
|
|
|
}
|
2018-06-30 09:09:29 +08:00
|
|
|
}
|
|
|
|
|
2018-07-10 00:00:25 +08:00
|
|
|
/// Parse a high precedence op expression list: mul, div, and mod are high
|
|
|
|
/// precedence binary ops, i.e., parse a
|
|
|
|
/// expr_1 op_1 expr_2 op_2 ... expr_n
|
|
|
|
/// where op_1, op_2 are all a AffineHighPrecOp (mul, div, mod).
|
|
|
|
/// All affine binary ops are left associative.
|
|
|
|
/// Given llhs, returns (llhs llhsOp lhs) op rhs, or (lhs op rhs) if llhs is
|
|
|
|
/// null. If no rhs can be found, returns (llhs llhsOp lhs) or lhs if llhs is
|
|
|
|
/// null.
|
|
|
|
AffineExpr *
|
2018-07-11 01:08:27 +08:00
|
|
|
AffineMapParser::parseAffineHighPrecOpExpr(AffineExpr *llhs,
|
|
|
|
AffineHighPrecOp llhsOp) {
|
|
|
|
AffineExpr *lhs = parseAffineOperandExpr(llhs);
|
2018-07-10 00:00:25 +08:00
|
|
|
if (!lhs)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// Found an LHS. Parse the remaining expression.
|
2018-07-11 01:08:27 +08:00
|
|
|
if (AffineHighPrecOp op = consumeIfHighPrecOp()) {
|
2018-07-10 00:00:25 +08:00
|
|
|
if (llhs) {
|
|
|
|
AffineExpr *expr = getBinaryAffineOpExpr(llhsOp, llhs, lhs);
|
|
|
|
if (!expr)
|
|
|
|
return nullptr;
|
2018-07-11 01:08:27 +08:00
|
|
|
return parseAffineHighPrecOpExpr(expr, op);
|
2018-07-10 00:00:25 +08:00
|
|
|
}
|
|
|
|
// No LLHS, get RHS
|
2018-07-11 01:08:27 +08:00
|
|
|
return parseAffineHighPrecOpExpr(lhs, op);
|
2018-07-10 00:00:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is the last operand in this expression.
|
|
|
|
if (llhs)
|
|
|
|
return getBinaryAffineOpExpr(llhsOp, llhs, lhs);
|
|
|
|
|
|
|
|
// No llhs, 'lhs' itself is the expression.
|
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse an affine expression inside parentheses.
|
|
|
|
///
|
|
|
|
/// affine-expr ::= `(` affine-expr `)`
|
2018-07-11 01:08:27 +08:00
|
|
|
AffineExpr *AffineMapParser::parseParentheticalExpr() {
|
2018-07-10 00:00:25 +08:00
|
|
|
if (!consumeIf(Token::l_paren))
|
|
|
|
return (emitError("expected '('"), nullptr);
|
2018-07-10 10:05:38 +08:00
|
|
|
if (getToken().is(Token::r_paren))
|
2018-07-10 00:00:25 +08:00
|
|
|
return (emitError("no expression inside parentheses"), nullptr);
|
2018-07-11 01:08:27 +08:00
|
|
|
auto *expr = parseAffineExpr();
|
2018-07-10 00:00:25 +08:00
|
|
|
if (!expr)
|
|
|
|
return nullptr;
|
|
|
|
if (!consumeIf(Token::r_paren))
|
|
|
|
return (emitError("expected ')'"), nullptr);
|
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse the negation expression.
|
|
|
|
///
|
|
|
|
/// affine-expr ::= `-` affine-expr
|
2018-07-11 01:08:27 +08:00
|
|
|
AffineExpr *AffineMapParser::parseNegateExpression(AffineExpr *lhs) {
|
2018-07-10 00:00:25 +08:00
|
|
|
if (!consumeIf(Token::minus))
|
|
|
|
return (emitError("expected '-'"), nullptr);
|
|
|
|
|
2018-07-11 01:08:27 +08:00
|
|
|
AffineExpr *operand = parseAffineOperandExpr(lhs);
|
2018-07-10 00:00:25 +08:00
|
|
|
// Since negation has the highest precedence of all ops (including high
|
|
|
|
// precedence ops) but lower than parentheses, we are only going to use
|
|
|
|
// parseAffineOperandExpr instead of parseAffineExpr here.
|
|
|
|
if (!operand)
|
|
|
|
// Extra error message although parseAffineOperandExpr would have
|
|
|
|
// complained. Leads to a better diagnostic.
|
|
|
|
return (emitError("missing operand of negation"), nullptr);
|
2018-07-11 01:59:53 +08:00
|
|
|
auto *minusOne = builder.getConstantExpr(-1);
|
|
|
|
return builder.getMulExpr(minusOne, operand);
|
2018-07-10 00:00:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse a bare id that may appear in an affine expression.
|
|
|
|
///
|
|
|
|
/// affine-expr ::= bare-id
|
2018-07-11 01:08:27 +08:00
|
|
|
AffineExpr *AffineMapParser::parseBareIdExpr() {
|
2018-07-10 10:05:38 +08:00
|
|
|
if (getToken().isNot(Token::bare_identifier))
|
2018-07-10 00:00:25 +08:00
|
|
|
return (emitError("expected bare identifier"), nullptr);
|
|
|
|
|
2018-07-10 10:05:38 +08:00
|
|
|
StringRef sRef = getTokenSpelling();
|
2018-07-12 12:31:07 +08:00
|
|
|
// dims, symbols are all pairwise distinct.
|
2018-07-10 00:00:25 +08:00
|
|
|
if (dims.count(sRef)) {
|
2018-07-12 12:31:07 +08:00
|
|
|
if (isPureSymbolic())
|
|
|
|
return (emitError("identifier used is not a symbolic identifier"),
|
|
|
|
nullptr);
|
2018-07-10 00:00:25 +08:00
|
|
|
consumeToken(Token::bare_identifier);
|
2018-07-11 01:59:53 +08:00
|
|
|
return builder.getDimExpr(dims.lookup(sRef));
|
2018-07-10 00:00:25 +08:00
|
|
|
}
|
2018-07-12 12:31:07 +08:00
|
|
|
|
2018-07-10 00:00:25 +08:00
|
|
|
if (symbols.count(sRef)) {
|
|
|
|
consumeToken(Token::bare_identifier);
|
2018-07-11 01:59:53 +08:00
|
|
|
return builder.getSymbolExpr(symbols.lookup(sRef));
|
2018-07-10 00:00:25 +08:00
|
|
|
}
|
2018-07-12 12:31:07 +08:00
|
|
|
|
|
|
|
return (emitError("use of undeclared identifier"), nullptr);
|
2018-07-10 00:00:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse a positive integral constant appearing in an affine expression.
|
|
|
|
///
|
|
|
|
/// affine-expr ::= integer-literal
|
2018-07-11 01:08:27 +08:00
|
|
|
AffineExpr *AffineMapParser::parseIntegerExpr() {
|
2018-07-10 00:00:25 +08:00
|
|
|
// No need to handle negative numbers separately here. They are naturally
|
|
|
|
// handled via the unary negation operator, although (FIXME) MININT_64 still
|
|
|
|
// not correctly handled.
|
2018-07-10 10:05:38 +08:00
|
|
|
if (getToken().isNot(Token::integer))
|
2018-07-10 00:00:25 +08:00
|
|
|
return (emitError("expected integer"), nullptr);
|
|
|
|
|
2018-07-10 10:05:38 +08:00
|
|
|
auto val = getToken().getUInt64IntegerValue();
|
2018-07-10 00:00:25 +08:00
|
|
|
if (!val.hasValue() || (int64_t)val.getValue() < 0) {
|
|
|
|
return (emitError("constant too large for affineint"), nullptr);
|
|
|
|
}
|
|
|
|
consumeToken(Token::integer);
|
2018-07-11 01:59:53 +08:00
|
|
|
return builder.getConstantExpr((int64_t)val.getValue());
|
2018-07-10 00:00:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses an expression that can be a valid operand of an affine expression.
|
2018-07-10 04:47:52 +08:00
|
|
|
/// lhs: if non-null, lhs is an affine expression that is the lhs of a binary
|
|
|
|
/// operator, the rhs of which is being parsed. This is used to determine
|
|
|
|
/// whether an error should be emitted for a missing right operand.
|
2018-07-10 00:00:25 +08:00
|
|
|
// Eg: for an expression without parentheses (like i + j + k + l), each
|
|
|
|
// of the four identifiers is an operand. For i + j*k + l, j*k is not an
|
|
|
|
// operand expression, it's an op expression and will be parsed via
|
|
|
|
// parseAffineHighPrecOpExpression(). However, for i + (j*k) + -l, (j*k) and -l
|
|
|
|
// are valid operands that will be parsed by this function.
|
2018-07-11 01:08:27 +08:00
|
|
|
AffineExpr *AffineMapParser::parseAffineOperandExpr(AffineExpr *lhs) {
|
2018-07-10 10:05:38 +08:00
|
|
|
switch (getToken().getKind()) {
|
2018-07-10 00:00:25 +08:00
|
|
|
case Token::bare_identifier:
|
2018-07-11 01:08:27 +08:00
|
|
|
return parseBareIdExpr();
|
2018-07-10 00:00:25 +08:00
|
|
|
case Token::integer:
|
2018-07-11 01:08:27 +08:00
|
|
|
return parseIntegerExpr();
|
2018-07-10 00:00:25 +08:00
|
|
|
case Token::l_paren:
|
2018-07-11 01:08:27 +08:00
|
|
|
return parseParentheticalExpr();
|
2018-07-10 00:00:25 +08:00
|
|
|
case Token::minus:
|
2018-07-11 01:08:27 +08:00
|
|
|
return parseNegateExpression(lhs);
|
2018-07-10 04:47:52 +08:00
|
|
|
case Token::kw_ceildiv:
|
|
|
|
case Token::kw_floordiv:
|
|
|
|
case Token::kw_mod:
|
|
|
|
case Token::plus:
|
|
|
|
case Token::star:
|
|
|
|
if (lhs)
|
|
|
|
emitError("missing right operand of binary operator");
|
|
|
|
else
|
|
|
|
emitError("missing left operand of binary operator");
|
|
|
|
return nullptr;
|
2018-07-10 00:00:25 +08:00
|
|
|
default:
|
|
|
|
if (lhs)
|
2018-07-10 04:47:52 +08:00
|
|
|
emitError("missing right operand of binary operator");
|
2018-07-10 00:00:25 +08:00
|
|
|
else
|
|
|
|
emitError("expected affine expression");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-04 11:16:08 +08:00
|
|
|
/// Parse affine expressions that are bare-id's, integer constants,
|
|
|
|
/// parenthetical affine expressions, and affine op expressions that are a
|
|
|
|
/// composition of those.
|
2018-06-30 09:09:29 +08:00
|
|
|
///
|
2018-07-04 11:16:08 +08:00
|
|
|
/// All binary op's associate from left to right.
|
|
|
|
///
|
|
|
|
/// {add, sub} have lower precedence than {mul, div, and mod}.
|
|
|
|
///
|
2018-07-10 04:47:52 +08:00
|
|
|
/// Add, sub'are themselves at the same precedence level. Mul, floordiv,
|
|
|
|
/// ceildiv, and mod are at the same higher precedence level. Negation has
|
|
|
|
/// higher precedence than any binary op.
|
2018-07-04 11:16:08 +08:00
|
|
|
///
|
|
|
|
/// llhs: the affine expression appearing on the left of the one being parsed.
|
2018-07-10 00:00:25 +08:00
|
|
|
/// This function will return ((llhs llhsOp lhs) op rhs) if llhs is non null,
|
|
|
|
/// and lhs op rhs otherwise; if there is no rhs, llhs llhsOp lhs is returned if
|
|
|
|
/// llhs is non-null; otherwise lhs is returned. This is to deal with left
|
2018-07-04 11:16:08 +08:00
|
|
|
/// associativity.
|
|
|
|
///
|
|
|
|
/// Eg: when the expression is e1 + e2*e3 + e4, with e1 as llhs, this function
|
2018-07-10 00:00:25 +08:00
|
|
|
/// will return the affine expr equivalent of (e1 + (e2*e3)) + e4, where (e2*e3)
|
|
|
|
/// will be parsed using parseAffineHighPrecOpExpr().
|
2018-07-11 01:08:27 +08:00
|
|
|
AffineExpr *AffineMapParser::parseAffineLowPrecOpExpr(AffineExpr *llhs,
|
|
|
|
AffineLowPrecOp llhsOp) {
|
2018-07-10 04:47:52 +08:00
|
|
|
AffineExpr *lhs;
|
2018-07-11 01:08:27 +08:00
|
|
|
if (!(lhs = parseAffineOperandExpr(llhs)))
|
2018-07-10 00:00:25 +08:00
|
|
|
return nullptr;
|
2018-07-04 11:16:08 +08:00
|
|
|
|
|
|
|
// Found an LHS. Deal with the ops.
|
2018-07-11 01:08:27 +08:00
|
|
|
if (AffineLowPrecOp lOp = consumeIfLowPrecOp()) {
|
2018-07-04 11:16:08 +08:00
|
|
|
if (llhs) {
|
2018-07-09 11:51:38 +08:00
|
|
|
AffineExpr *sum = getBinaryAffineOpExpr(llhsOp, llhs, lhs);
|
2018-07-11 01:08:27 +08:00
|
|
|
return parseAffineLowPrecOpExpr(sum, lOp);
|
2018-07-04 11:16:08 +08:00
|
|
|
}
|
|
|
|
// No LLHS, get RHS and form the expression.
|
2018-07-11 01:08:27 +08:00
|
|
|
return parseAffineLowPrecOpExpr(lhs, lOp);
|
2018-07-10 00:00:25 +08:00
|
|
|
}
|
2018-07-11 01:08:27 +08:00
|
|
|
if (AffineHighPrecOp hOp = consumeIfHighPrecOp()) {
|
2018-07-04 11:16:08 +08:00
|
|
|
// We have a higher precedence op here. Get the rhs operand for the llhs
|
|
|
|
// through parseAffineHighPrecOpExpr.
|
2018-07-11 01:08:27 +08:00
|
|
|
AffineExpr *highRes = parseAffineHighPrecOpExpr(lhs, hOp);
|
2018-07-10 00:00:25 +08:00
|
|
|
if (!highRes)
|
|
|
|
return nullptr;
|
2018-07-11 01:08:27 +08:00
|
|
|
|
2018-07-04 11:16:08 +08:00
|
|
|
// If llhs is null, the product forms the first operand of the yet to be
|
2018-07-10 00:00:25 +08:00
|
|
|
// found expression. If non-null, the op to associate with llhs is llhsOp.
|
2018-07-04 11:16:08 +08:00
|
|
|
AffineExpr *expr =
|
2018-07-09 11:51:38 +08:00
|
|
|
llhs ? getBinaryAffineOpExpr(llhsOp, llhs, highRes) : highRes;
|
2018-07-11 01:08:27 +08:00
|
|
|
|
2018-07-10 00:00:25 +08:00
|
|
|
// Recurse for subsequent low prec op's after the affine high prec op
|
|
|
|
// expression.
|
2018-07-11 01:08:27 +08:00
|
|
|
if (AffineLowPrecOp nextOp = consumeIfLowPrecOp())
|
|
|
|
return parseAffineLowPrecOpExpr(expr, nextOp);
|
2018-07-04 11:16:08 +08:00
|
|
|
return expr;
|
|
|
|
}
|
2018-07-10 00:00:25 +08:00
|
|
|
// Last operand in the expression list.
|
|
|
|
if (llhs)
|
|
|
|
return getBinaryAffineOpExpr(llhsOp, llhs, lhs);
|
|
|
|
// No llhs, 'lhs' itself is the expression.
|
|
|
|
return lhs;
|
2018-07-04 11:16:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse an affine expression.
|
2018-07-10 00:00:25 +08:00
|
|
|
/// affine-expr ::= `(` affine-expr `)`
|
|
|
|
/// | `-` affine-expr
|
|
|
|
/// | affine-expr `+` affine-expr
|
|
|
|
/// | affine-expr `-` affine-expr
|
|
|
|
/// | affine-expr `*` affine-expr
|
|
|
|
/// | affine-expr `floordiv` affine-expr
|
|
|
|
/// | affine-expr `ceildiv` affine-expr
|
|
|
|
/// | affine-expr `mod` affine-expr
|
|
|
|
/// | bare-id
|
|
|
|
/// | integer-literal
|
|
|
|
///
|
|
|
|
/// Additional conditions are checked depending on the production. For eg., one
|
|
|
|
/// of the operands for `*` has to be either constant/symbolic; the second
|
|
|
|
/// operand for floordiv, ceildiv, and mod has to be a positive integer.
|
2018-07-11 01:08:27 +08:00
|
|
|
AffineExpr *AffineMapParser::parseAffineExpr() {
|
|
|
|
return parseAffineLowPrecOpExpr(nullptr, AffineLowPrecOp::LNoOp);
|
2018-06-30 09:09:29 +08:00
|
|
|
}
|
|
|
|
|
2018-07-04 11:16:08 +08:00
|
|
|
/// Parse a dim or symbol from the lists appearing before the actual expressions
|
2018-07-11 01:08:27 +08:00
|
|
|
/// of the affine map. Update our state to store the dimensional/symbolic
|
2018-07-04 11:16:08 +08:00
|
|
|
/// identifier. 'dim': whether it's the dim list or symbol list that is being
|
|
|
|
/// parsed.
|
2018-07-11 01:08:27 +08:00
|
|
|
ParseResult AffineMapParser::parseDimOrSymbolId(bool isDim) {
|
2018-07-10 10:05:38 +08:00
|
|
|
if (getToken().isNot(Token::bare_identifier))
|
2018-07-04 11:16:08 +08:00
|
|
|
return emitError("expected bare identifier");
|
2018-07-10 10:05:38 +08:00
|
|
|
auto sRef = getTokenSpelling();
|
2018-06-30 09:09:29 +08:00
|
|
|
consumeToken(Token::bare_identifier);
|
2018-07-11 01:08:27 +08:00
|
|
|
if (dims.count(sRef))
|
2018-07-04 11:16:08 +08:00
|
|
|
return emitError("dimensional identifier name reused");
|
2018-07-11 01:08:27 +08:00
|
|
|
if (symbols.count(sRef))
|
2018-07-04 11:16:08 +08:00
|
|
|
return emitError("symbolic identifier name reused");
|
2018-07-11 01:08:27 +08:00
|
|
|
if (isDim)
|
|
|
|
dims.insert({sRef, dims.size()});
|
2018-07-04 11:16:08 +08:00
|
|
|
else
|
2018-07-11 01:08:27 +08:00
|
|
|
symbols.insert({sRef, symbols.size()});
|
2018-07-04 11:16:08 +08:00
|
|
|
return ParseSuccess;
|
2018-06-30 09:09:29 +08:00
|
|
|
}
|
|
|
|
|
2018-07-04 11:16:08 +08:00
|
|
|
/// Parse the list of symbolic identifiers to an affine map.
|
2018-07-11 01:08:27 +08:00
|
|
|
ParseResult AffineMapParser::parseSymbolIdList() {
|
|
|
|
if (!consumeIf(Token::l_bracket))
|
|
|
|
return emitError("expected '['");
|
2018-06-30 09:09:29 +08:00
|
|
|
|
2018-07-11 01:08:27 +08:00
|
|
|
auto parseElt = [&]() -> ParseResult { return parseDimOrSymbolId(false); };
|
2018-06-30 09:09:29 +08:00
|
|
|
return parseCommaSeparatedList(Token::r_bracket, parseElt);
|
|
|
|
}
|
|
|
|
|
2018-07-04 11:16:08 +08:00
|
|
|
/// Parse the list of dimensional identifiers to an affine map.
|
2018-07-11 01:08:27 +08:00
|
|
|
ParseResult AffineMapParser::parseDimIdList() {
|
2018-06-30 09:09:29 +08:00
|
|
|
if (!consumeIf(Token::l_paren))
|
|
|
|
return emitError("expected '(' at start of dimensional identifiers list");
|
|
|
|
|
2018-07-11 01:08:27 +08:00
|
|
|
auto parseElt = [&]() -> ParseResult { return parseDimOrSymbolId(true); };
|
2018-06-30 09:09:29 +08:00
|
|
|
return parseCommaSeparatedList(Token::r_paren, parseElt);
|
|
|
|
}
|
|
|
|
|
2018-07-04 11:16:08 +08:00
|
|
|
/// Parse an affine map definition.
|
2018-06-30 09:09:29 +08:00
|
|
|
///
|
2018-07-10 00:00:25 +08:00
|
|
|
/// affine-map-inline ::= dim-and-symbol-id-lists `->` multi-dim-affine-expr
|
|
|
|
/// (`size` `(` dim-size (`,` dim-size)* `)`)?
|
|
|
|
/// dim-size ::= affine-expr | `min` `(` affine-expr ( `,` affine-expr)+ `)`
|
2018-06-30 09:09:29 +08:00
|
|
|
///
|
2018-07-10 00:00:25 +08:00
|
|
|
/// multi-dim-affine-expr ::= `(` affine-expr (`,` affine-expr)* `)
|
|
|
|
// TODO(bondhugula): parse range size information.
|
2018-07-11 01:08:27 +08:00
|
|
|
AffineMap *AffineMapParser::parseAffineMapInline() {
|
2018-06-30 09:09:29 +08:00
|
|
|
// List of dimensional identifiers.
|
2018-07-11 01:08:27 +08:00
|
|
|
if (parseDimIdList())
|
2018-07-05 11:45:39 +08:00
|
|
|
return nullptr;
|
2018-06-30 09:09:29 +08:00
|
|
|
|
|
|
|
// Symbols are optional.
|
2018-07-10 10:05:38 +08:00
|
|
|
if (getToken().is(Token::l_bracket)) {
|
2018-07-11 01:08:27 +08:00
|
|
|
if (parseSymbolIdList())
|
2018-07-05 11:45:39 +08:00
|
|
|
return nullptr;
|
2018-06-30 09:09:29 +08:00
|
|
|
}
|
|
|
|
if (!consumeIf(Token::arrow)) {
|
2018-07-05 11:45:39 +08:00
|
|
|
return (emitError("expected '->' or '['"), nullptr);
|
2018-06-30 09:09:29 +08:00
|
|
|
}
|
|
|
|
if (!consumeIf(Token::l_paren)) {
|
|
|
|
emitError("expected '(' at start of affine map range");
|
2018-07-05 11:45:39 +08:00
|
|
|
return nullptr;
|
2018-06-30 09:09:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SmallVector<AffineExpr *, 4> exprs;
|
|
|
|
auto parseElt = [&]() -> ParseResult {
|
2018-07-11 01:08:27 +08:00
|
|
|
auto *elt = parseAffineExpr();
|
2018-06-30 09:09:29 +08:00
|
|
|
ParseResult res = elt ? ParseSuccess : ParseFailure;
|
|
|
|
exprs.push_back(elt);
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Parse a multi-dimensional affine expression (a comma-separated list of 1-d
|
2018-07-04 11:16:08 +08:00
|
|
|
// affine expressions); the list cannot be empty.
|
|
|
|
// Grammar: multi-dim-affine-expr ::= `(` affine-expr (`,` affine-expr)* `)
|
|
|
|
if (parseCommaSeparatedList(Token::r_paren, parseElt, false))
|
2018-07-05 11:45:39 +08:00
|
|
|
return nullptr;
|
2018-06-30 09:09:29 +08:00
|
|
|
|
2018-07-12 12:31:07 +08:00
|
|
|
// Parse optional range sizes.
|
|
|
|
// (`size` `(` dim-size (`,` dim-size)* `)`)?
|
|
|
|
// TODO: check if sizes are non-negative whenever they are constant.
|
|
|
|
SmallVector<AffineExpr *, 4> rangeSizes;
|
|
|
|
if (consumeIf(Token::kw_size)) {
|
|
|
|
// Location of the l_paren token (if it exists) for error reporting later.
|
|
|
|
auto loc = getToken().getLoc();
|
|
|
|
if (!consumeIf(Token::l_paren))
|
|
|
|
return (emitError("expected '(' at start of affine map range"), nullptr);
|
|
|
|
|
|
|
|
auto parseRangeSize = [&]() -> ParseResult {
|
|
|
|
auto *elt = parseAffineExpr();
|
|
|
|
ParseResult res = elt ? ParseSuccess : ParseFailure;
|
|
|
|
rangeSizes.push_back(elt);
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
|
|
|
|
setSymbolicParsing(true);
|
|
|
|
if (parseCommaSeparatedList(Token::r_paren, parseRangeSize, false))
|
|
|
|
return nullptr;
|
|
|
|
if (exprs.size() > rangeSizes.size())
|
|
|
|
return (emitError(loc, "fewer range sizes than range expressions"),
|
|
|
|
nullptr);
|
|
|
|
if (exprs.size() < rangeSizes.size())
|
|
|
|
return (emitError(loc, "more range sizes than range expressions"),
|
|
|
|
nullptr);
|
|
|
|
}
|
|
|
|
|
2018-07-04 11:16:08 +08:00
|
|
|
// Parsed a valid affine map.
|
2018-07-12 12:31:07 +08:00
|
|
|
return builder.getAffineMap(dims.size(), symbols.size(), exprs, rangeSizes);
|
2018-06-28 02:03:08 +08:00
|
|
|
}
|
|
|
|
|
2018-07-11 01:08:27 +08:00
|
|
|
AffineMap *Parser::parseAffineMapInline() {
|
|
|
|
return AffineMapParser(state).parseAffineMapInline();
|
|
|
|
}
|
|
|
|
|
2018-06-23 01:39:19 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-07-08 06:48:26 +08:00
|
|
|
// SSA
|
2018-06-23 01:39:19 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-07-08 06:48:26 +08:00
|
|
|
/// Parse a SSA operand for an instruction or statement.
|
|
|
|
///
|
|
|
|
/// ssa-use ::= ssa-id | ssa-constant
|
|
|
|
///
|
|
|
|
ParseResult Parser::parseSSAUse() {
|
2018-07-10 10:05:38 +08:00
|
|
|
if (getToken().is(Token::percent_identifier)) {
|
|
|
|
StringRef name = getTokenSpelling().drop_front();
|
2018-07-08 06:48:26 +08:00
|
|
|
consumeToken(Token::percent_identifier);
|
|
|
|
// TODO: Return this use.
|
|
|
|
(void)name;
|
|
|
|
return ParseSuccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Parse SSA constants.
|
|
|
|
|
|
|
|
return emitError("expected SSA operand");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse a (possibly empty) list of SSA operands.
|
|
|
|
///
|
|
|
|
/// ssa-use-list ::= ssa-use (`,` ssa-use)*
|
|
|
|
/// ssa-use-list-opt ::= ssa-use-list?
|
|
|
|
///
|
|
|
|
ParseResult Parser::parseOptionalSSAUseList(Token::Kind endToken) {
|
|
|
|
// TODO: Build and return this.
|
|
|
|
return parseCommaSeparatedList(
|
|
|
|
endToken, [&]() -> ParseResult { return parseSSAUse(); });
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse an SSA use with an associated type.
|
|
|
|
///
|
|
|
|
/// ssa-use-and-type ::= ssa-use `:` type
|
|
|
|
ParseResult Parser::parseSSAUseAndType() {
|
|
|
|
if (parseSSAUse())
|
|
|
|
return ParseFailure;
|
|
|
|
|
|
|
|
if (!consumeIf(Token::colon))
|
|
|
|
return emitError("expected ':' and type for SSA operand");
|
|
|
|
|
|
|
|
if (!parseType())
|
|
|
|
return ParseFailure;
|
|
|
|
|
|
|
|
return ParseSuccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse a (possibly empty) list of SSA operands with types.
|
|
|
|
///
|
|
|
|
/// ssa-use-and-type-list ::= ssa-use-and-type (`,` ssa-use-and-type)*
|
|
|
|
///
|
|
|
|
ParseResult Parser::parseOptionalSSAUseAndTypeList(Token::Kind endToken) {
|
|
|
|
// TODO: Build and return this.
|
|
|
|
return parseCommaSeparatedList(
|
|
|
|
endToken, [&]() -> ParseResult { return parseSSAUseAndType(); });
|
|
|
|
}
|
|
|
|
|
2018-06-24 07:03:42 +08:00
|
|
|
|
2018-07-10 10:05:38 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CFG Functions
|
|
|
|
//===----------------------------------------------------------------------===//
|
2018-06-24 07:03:42 +08:00
|
|
|
|
|
|
|
namespace {
|
2018-07-10 10:05:38 +08:00
|
|
|
/// This is a specialized parser for CFGFunction's, maintaining the state
|
|
|
|
/// transient to their bodies.
|
|
|
|
class CFGFunctionParser : public Parser {
|
2018-07-09 11:51:38 +08:00
|
|
|
public:
|
2018-07-11 01:08:27 +08:00
|
|
|
CFGFunctionParser(ParserState &state, CFGFunction *function)
|
|
|
|
: Parser(state), function(function), builder(function) {}
|
|
|
|
|
|
|
|
ParseResult parseFunctionBody();
|
|
|
|
|
|
|
|
private:
|
2018-06-25 02:18:29 +08:00
|
|
|
CFGFunction *function;
|
|
|
|
llvm::StringMap<std::pair<BasicBlock*, SMLoc>> blocksByName;
|
2018-07-10 10:05:38 +08:00
|
|
|
|
|
|
|
/// This builder intentionally shadows the builder in the base class, with a
|
|
|
|
/// more specific builder type.
|
2018-07-09 11:51:38 +08:00
|
|
|
CFGFuncBuilder builder;
|
2018-06-25 02:18:29 +08:00
|
|
|
|
2018-06-24 07:03:42 +08:00
|
|
|
/// Get the basic block with the specified name, creating it if it doesn't
|
2018-06-25 02:18:29 +08:00
|
|
|
/// already exist. The location specified is the point of use, which allows
|
|
|
|
/// us to diagnose references to blocks that are not defined precisely.
|
|
|
|
BasicBlock *getBlockNamed(StringRef name, SMLoc loc) {
|
|
|
|
auto &blockAndLoc = blocksByName[name];
|
|
|
|
if (!blockAndLoc.first) {
|
2018-07-02 11:28:00 +08:00
|
|
|
blockAndLoc.first = new BasicBlock();
|
2018-06-25 02:18:29 +08:00
|
|
|
blockAndLoc.second = loc;
|
2018-06-24 07:03:42 +08:00
|
|
|
}
|
2018-06-25 02:18:29 +08:00
|
|
|
return blockAndLoc.first;
|
2018-06-24 07:03:42 +08:00
|
|
|
}
|
2018-07-10 10:05:38 +08:00
|
|
|
|
|
|
|
ParseResult parseBasicBlock();
|
|
|
|
OperationInst *parseCFGOperation();
|
|
|
|
TerminatorInst *parseTerminator();
|
2018-06-24 07:03:42 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2018-07-10 10:05:38 +08:00
|
|
|
ParseResult CFGFunctionParser::parseFunctionBody() {
|
|
|
|
if (!consumeIf(Token::l_brace))
|
|
|
|
return emitError("expected '{' in CFG function");
|
|
|
|
|
2018-06-24 07:03:42 +08:00
|
|
|
// Make sure we have at least one block.
|
2018-07-10 10:05:38 +08:00
|
|
|
if (getToken().is(Token::r_brace))
|
2018-06-24 07:03:42 +08:00
|
|
|
return emitError("CFG functions must have at least one basic block");
|
|
|
|
|
|
|
|
// Parse the list of blocks.
|
|
|
|
while (!consumeIf(Token::r_brace))
|
2018-07-10 10:05:38 +08:00
|
|
|
if (parseBasicBlock())
|
2018-06-24 07:03:42 +08:00
|
|
|
return ParseFailure;
|
|
|
|
|
2018-06-25 02:18:29 +08:00
|
|
|
// Verify that all referenced blocks were defined. Iteration over a
|
|
|
|
// StringMap isn't determinstic, but this is good enough for our purposes.
|
2018-07-10 10:05:38 +08:00
|
|
|
for (auto &elt : blocksByName) {
|
2018-06-25 02:18:29 +08:00
|
|
|
auto *bb = elt.second.first;
|
2018-07-02 11:28:00 +08:00
|
|
|
if (!bb->getFunction())
|
2018-06-25 02:18:29 +08:00
|
|
|
return emitError(elt.second.second,
|
|
|
|
"reference to an undefined basic block '" +
|
|
|
|
elt.first() + "'");
|
|
|
|
}
|
|
|
|
|
2018-07-10 10:05:38 +08:00
|
|
|
getModule()->functionList.push_back(function);
|
2018-06-24 07:03:42 +08:00
|
|
|
return ParseSuccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Basic block declaration.
|
|
|
|
///
|
|
|
|
/// basic-block ::= bb-label instruction* terminator-stmt
|
|
|
|
/// bb-label ::= bb-id bb-arg-list? `:`
|
|
|
|
/// bb-id ::= bare-id
|
|
|
|
/// bb-arg-list ::= `(` ssa-id-and-type-list? `)`
|
|
|
|
///
|
2018-07-10 10:05:38 +08:00
|
|
|
ParseResult CFGFunctionParser::parseBasicBlock() {
|
|
|
|
SMLoc nameLoc = getToken().getLoc();
|
|
|
|
auto name = getTokenSpelling();
|
2018-06-24 07:03:42 +08:00
|
|
|
if (!consumeIf(Token::bare_identifier))
|
|
|
|
return emitError("expected basic block name");
|
2018-06-25 02:18:29 +08:00
|
|
|
|
2018-07-10 10:05:38 +08:00
|
|
|
auto *block = getBlockNamed(name, nameLoc);
|
2018-06-24 07:03:42 +08:00
|
|
|
|
|
|
|
// If this block has already been parsed, then this is a redefinition with the
|
|
|
|
// same block name.
|
2018-07-02 11:28:00 +08:00
|
|
|
if (block->getFunction())
|
2018-06-25 02:18:29 +08:00
|
|
|
return emitError(nameLoc, "redefinition of block '" + name.str() + "'");
|
|
|
|
|
2018-07-02 11:28:00 +08:00
|
|
|
// Add the block to the function.
|
2018-07-10 10:05:38 +08:00
|
|
|
function->push_back(block);
|
2018-06-24 07:03:42 +08:00
|
|
|
|
2018-07-08 06:48:26 +08:00
|
|
|
// If an argument list is present, parse it.
|
|
|
|
if (consumeIf(Token::l_paren)) {
|
|
|
|
if (parseOptionalSSAUseAndTypeList(Token::r_paren))
|
|
|
|
return ParseFailure;
|
|
|
|
|
|
|
|
// TODO: attach it.
|
|
|
|
}
|
2018-06-24 07:03:42 +08:00
|
|
|
|
|
|
|
if (!consumeIf(Token::colon))
|
|
|
|
return emitError("expected ':' after basic block name");
|
|
|
|
|
2018-07-09 11:51:38 +08:00
|
|
|
// Set the insertion point to the block we want to insert new operations into.
|
2018-07-10 10:05:38 +08:00
|
|
|
builder.setInsertionPoint(block);
|
2018-07-09 11:51:38 +08:00
|
|
|
|
2018-06-29 11:45:33 +08:00
|
|
|
// Parse the list of operations that make up the body of the block.
|
2018-07-10 10:05:38 +08:00
|
|
|
while (getToken().isNot(Token::kw_return, Token::kw_br)) {
|
|
|
|
auto loc = getToken().getLoc();
|
|
|
|
auto *inst = parseCFGOperation();
|
2018-07-02 11:28:00 +08:00
|
|
|
if (!inst)
|
2018-06-29 11:45:33 +08:00
|
|
|
return ParseFailure;
|
2018-07-02 11:28:00 +08:00
|
|
|
|
2018-07-07 01:46:19 +08:00
|
|
|
// We just parsed an operation. If it is a recognized one, verify that it
|
|
|
|
// is structurally as we expect. If not, produce an error with a reasonable
|
|
|
|
// source location.
|
2018-07-09 11:51:38 +08:00
|
|
|
if (auto *opInfo = inst->getAbstractOperation(builder.getContext()))
|
2018-07-07 01:46:19 +08:00
|
|
|
if (auto error = opInfo->verifyInvariants(inst))
|
|
|
|
return emitError(loc, error);
|
2018-06-29 11:45:33 +08:00
|
|
|
}
|
2018-06-24 07:03:42 +08:00
|
|
|
|
2018-07-10 10:05:38 +08:00
|
|
|
auto *term = parseTerminator();
|
2018-07-02 11:28:00 +08:00
|
|
|
if (!term)
|
2018-06-29 11:45:33 +08:00
|
|
|
return ParseFailure;
|
2018-06-24 07:03:42 +08:00
|
|
|
|
2018-06-29 11:45:33 +08:00
|
|
|
return ParseSuccess;
|
|
|
|
}
|
2018-06-24 07:03:42 +08:00
|
|
|
|
2018-06-29 11:45:33 +08:00
|
|
|
/// Parse the CFG operation.
|
|
|
|
///
|
|
|
|
/// TODO(clattner): This is a change from the MLIR spec as written, it is an
|
|
|
|
/// experiment that will eliminate "builtin" instructions as a thing.
|
|
|
|
///
|
|
|
|
/// cfg-operation ::=
|
|
|
|
/// (ssa-id `=`)? string '(' ssa-use-list? ')' attribute-dict?
|
|
|
|
/// `:` function-type
|
|
|
|
///
|
2018-07-10 10:05:38 +08:00
|
|
|
OperationInst *CFGFunctionParser::parseCFGOperation() {
|
2018-07-08 06:48:26 +08:00
|
|
|
StringRef resultID;
|
2018-07-10 10:05:38 +08:00
|
|
|
if (getToken().is(Token::percent_identifier)) {
|
|
|
|
resultID = getTokenSpelling().drop_front();
|
2018-07-08 06:48:26 +08:00
|
|
|
consumeToken();
|
|
|
|
if (!consumeIf(Token::equal))
|
|
|
|
return (emitError("expected '=' after SSA name"), nullptr);
|
|
|
|
}
|
2018-06-29 11:45:33 +08:00
|
|
|
|
2018-07-10 10:05:38 +08:00
|
|
|
if (getToken().isNot(Token::string))
|
2018-07-02 11:28:00 +08:00
|
|
|
return (emitError("expected operation name in quotes"), nullptr);
|
2018-06-29 11:45:33 +08:00
|
|
|
|
2018-07-10 10:05:38 +08:00
|
|
|
auto name = getToken().getStringValue();
|
2018-06-29 11:45:33 +08:00
|
|
|
if (name.empty())
|
2018-07-02 11:28:00 +08:00
|
|
|
return (emitError("empty operation name is invalid"), nullptr);
|
2018-06-29 11:45:33 +08:00
|
|
|
|
|
|
|
consumeToken(Token::string);
|
|
|
|
|
|
|
|
if (!consumeIf(Token::l_paren))
|
2018-07-05 11:45:39 +08:00
|
|
|
return (emitError("expected '(' to start operand list"), nullptr);
|
2018-06-29 11:45:33 +08:00
|
|
|
|
2018-07-08 06:48:26 +08:00
|
|
|
// Parse the operand list.
|
|
|
|
parseOptionalSSAUseList(Token::r_paren);
|
2018-07-05 11:45:39 +08:00
|
|
|
|
|
|
|
SmallVector<NamedAttribute, 4> attributes;
|
2018-07-10 10:05:38 +08:00
|
|
|
if (getToken().is(Token::l_brace)) {
|
2018-07-05 11:45:39 +08:00
|
|
|
if (parseAttributeDict(attributes))
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-06-29 11:45:33 +08:00
|
|
|
|
2018-07-08 06:48:26 +08:00
|
|
|
// TODO: Don't drop result name and operand names on the floor.
|
2018-07-11 01:59:53 +08:00
|
|
|
auto nameId = builder.getIdentifier(name);
|
2018-07-10 10:05:38 +08:00
|
|
|
return builder.createOperation(nameId, attributes);
|
2018-06-23 01:39:19 +08:00
|
|
|
}
|
|
|
|
|
2018-06-25 02:18:29 +08:00
|
|
|
/// Parse the terminator instruction for a basic block.
|
|
|
|
///
|
|
|
|
/// terminator-stmt ::= `br` bb-id branch-use-list?
|
|
|
|
/// branch-use-list ::= `(` ssa-use-and-type-list? `)`
|
|
|
|
/// terminator-stmt ::=
|
|
|
|
/// `cond_br` ssa-use `,` bb-id branch-use-list? `,` bb-id branch-use-list?
|
|
|
|
/// terminator-stmt ::= `return` ssa-use-and-type-list?
|
|
|
|
///
|
2018-07-10 10:05:38 +08:00
|
|
|
TerminatorInst *CFGFunctionParser::parseTerminator() {
|
|
|
|
switch (getToken().getKind()) {
|
2018-06-25 02:18:29 +08:00
|
|
|
default:
|
2018-07-02 11:28:00 +08:00
|
|
|
return (emitError("expected terminator at end of basic block"), nullptr);
|
2018-06-25 02:18:29 +08:00
|
|
|
|
|
|
|
case Token::kw_return:
|
|
|
|
consumeToken(Token::kw_return);
|
2018-07-10 10:05:38 +08:00
|
|
|
return builder.createReturnInst();
|
2018-06-25 02:18:29 +08:00
|
|
|
|
|
|
|
case Token::kw_br: {
|
|
|
|
consumeToken(Token::kw_br);
|
2018-07-10 10:05:38 +08:00
|
|
|
auto destBB = getBlockNamed(getTokenSpelling(), getToken().getLoc());
|
2018-06-25 02:18:29 +08:00
|
|
|
if (!consumeIf(Token::bare_identifier))
|
2018-07-02 11:28:00 +08:00
|
|
|
return (emitError("expected basic block name"), nullptr);
|
2018-07-10 10:05:38 +08:00
|
|
|
return builder.createBranchInst(destBB);
|
2018-06-25 02:18:29 +08:00
|
|
|
}
|
2018-07-08 06:48:26 +08:00
|
|
|
// TODO: cond_br.
|
2018-06-25 02:18:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 10:05:38 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ML Functions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
/// Refined parser for MLFunction bodies.
|
|
|
|
class MLFunctionParser : public Parser {
|
|
|
|
public:
|
|
|
|
MLFunction *function;
|
|
|
|
|
|
|
|
/// This builder intentionally shadows the builder in the base class, with a
|
|
|
|
/// more specific builder type.
|
|
|
|
// TODO: MLFuncBuilder builder;
|
|
|
|
|
|
|
|
MLFunctionParser(ParserState &state, MLFunction *function)
|
|
|
|
: Parser(state), function(function) {}
|
|
|
|
|
|
|
|
ParseResult parseFunctionBody();
|
|
|
|
Statement *parseStatement(ParentType parent);
|
|
|
|
ForStmt *parseForStmt(ParentType parent);
|
|
|
|
IfStmt *parseIfStmt(ParentType parent);
|
|
|
|
ParseResult parseNestedStatements(NodeStmt *parent);
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
ParseResult MLFunctionParser::parseFunctionBody() {
|
|
|
|
if (!consumeIf(Token::l_brace))
|
|
|
|
return emitError("expected '{' in ML function");
|
|
|
|
|
2018-06-29 08:02:32 +08:00
|
|
|
// Make sure we have at least one statement.
|
2018-07-10 10:05:38 +08:00
|
|
|
if (getToken().is(Token::r_brace))
|
2018-06-29 08:02:32 +08:00
|
|
|
return emitError("ML function must end with return statement");
|
|
|
|
|
|
|
|
// Parse the list of instructions.
|
|
|
|
while (!consumeIf(Token::kw_return)) {
|
2018-07-04 08:51:28 +08:00
|
|
|
auto *stmt = parseStatement(function);
|
2018-06-29 08:02:32 +08:00
|
|
|
if (!stmt)
|
|
|
|
return ParseFailure;
|
|
|
|
function->stmtList.push_back(stmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: parse return statement operands
|
|
|
|
if (!consumeIf(Token::r_brace))
|
|
|
|
emitError("expected '}' in ML function");
|
|
|
|
|
2018-07-10 10:05:38 +08:00
|
|
|
getModule()->functionList.push_back(function);
|
2018-06-29 08:02:32 +08:00
|
|
|
|
|
|
|
return ParseSuccess;
|
|
|
|
}
|
|
|
|
|
2018-07-04 08:51:28 +08:00
|
|
|
/// Statement.
|
2018-06-29 08:02:32 +08:00
|
|
|
///
|
2018-07-10 10:05:38 +08:00
|
|
|
/// ml-stmt ::= instruction | ml-for-stmt | ml-if-stmt
|
|
|
|
///
|
2018-07-04 08:51:28 +08:00
|
|
|
/// TODO: fix terminology in MLSpec document. ML functions
|
|
|
|
/// contain operation statements, not instructions.
|
|
|
|
///
|
2018-07-10 10:05:38 +08:00
|
|
|
Statement *MLFunctionParser::parseStatement(ParentType parent) {
|
|
|
|
switch (getToken().getKind()) {
|
2018-06-29 08:02:32 +08:00
|
|
|
default:
|
2018-07-04 08:51:28 +08:00
|
|
|
//TODO: parse OperationStmt
|
|
|
|
return (emitError("expected statement"), nullptr);
|
|
|
|
|
|
|
|
case Token::kw_for:
|
|
|
|
return parseForStmt(parent);
|
|
|
|
|
|
|
|
case Token::kw_if:
|
|
|
|
return parseIfStmt(parent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// For statement.
|
|
|
|
///
|
2018-07-10 10:05:38 +08:00
|
|
|
/// ml-for-stmt ::= `for` ssa-id `=` lower-bound `to` upper-bound
|
|
|
|
/// (`step` integer-literal)? `{` ml-stmt* `}`
|
2018-07-04 08:51:28 +08:00
|
|
|
///
|
2018-07-10 10:05:38 +08:00
|
|
|
ForStmt *MLFunctionParser::parseForStmt(ParentType parent) {
|
2018-07-04 08:51:28 +08:00
|
|
|
consumeToken(Token::kw_for);
|
|
|
|
|
|
|
|
//TODO: parse loop header
|
|
|
|
ForStmt *stmt = new ForStmt(parent);
|
|
|
|
if (parseNestedStatements(stmt)) {
|
|
|
|
delete stmt;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return stmt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If statement.
|
|
|
|
///
|
2018-07-10 10:05:38 +08:00
|
|
|
/// ml-if-head ::= `if` ml-if-cond `{` ml-stmt* `}`
|
|
|
|
/// | ml-if-head `else` `if` ml-if-cond `{` ml-stmt* `}`
|
|
|
|
/// ml-if-stmt ::= ml-if-head
|
|
|
|
/// | ml-if-head `else` `{` ml-stmt* `}`
|
2018-07-04 08:51:28 +08:00
|
|
|
///
|
2018-07-10 10:05:38 +08:00
|
|
|
IfStmt *
|
|
|
|
MLFunctionParser::parseIfStmt(PointerUnion<MLFunction *, NodeStmt *> parent) {
|
2018-07-04 08:51:28 +08:00
|
|
|
consumeToken(Token::kw_if);
|
|
|
|
|
|
|
|
//TODO: parse condition
|
|
|
|
IfStmt *stmt = new IfStmt(parent);
|
|
|
|
if (parseNestedStatements(stmt)) {
|
|
|
|
delete stmt;
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-06-29 08:02:32 +08:00
|
|
|
|
2018-07-04 08:51:28 +08:00
|
|
|
int clauseNum = 0;
|
|
|
|
while (consumeIf(Token::kw_else)) {
|
|
|
|
if (consumeIf(Token::kw_if)) {
|
|
|
|
//TODO: parse condition
|
|
|
|
}
|
|
|
|
ElseClause * clause = new ElseClause(stmt, clauseNum);
|
|
|
|
++clauseNum;
|
|
|
|
if (parseNestedStatements(clause)) {
|
|
|
|
delete clause;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stmt;
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Parse `{` ml-stmt* `}`
|
|
|
|
///
|
2018-07-10 10:05:38 +08:00
|
|
|
ParseResult MLFunctionParser::parseNestedStatements(NodeStmt *parent) {
|
2018-07-04 08:51:28 +08:00
|
|
|
if (!consumeIf(Token::l_brace))
|
|
|
|
return emitError("expected '{' before statement list");
|
|
|
|
|
|
|
|
if (consumeIf(Token::r_brace)) {
|
|
|
|
// TODO: parse OperationStmt
|
|
|
|
return ParseSuccess;
|
2018-06-29 08:02:32 +08:00
|
|
|
}
|
2018-07-04 08:51:28 +08:00
|
|
|
|
|
|
|
while (!consumeIf(Token::r_brace)) {
|
|
|
|
auto *stmt = parseStatement(parent);
|
|
|
|
if (!stmt)
|
|
|
|
return ParseFailure;
|
|
|
|
parent->children.push_back(stmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ParseSuccess;
|
2018-06-29 08:02:32 +08:00
|
|
|
}
|
2018-06-25 02:18:29 +08:00
|
|
|
|
2018-06-24 07:03:42 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Top-level entity parsing.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-07-11 01:08:27 +08:00
|
|
|
namespace {
|
|
|
|
/// This parser handles entities that are only valid at the top level of the
|
|
|
|
/// file.
|
|
|
|
class ModuleParser : public Parser {
|
|
|
|
public:
|
|
|
|
explicit ModuleParser(ParserState &state) : Parser(state) {}
|
|
|
|
|
|
|
|
ParseResult parseModule();
|
|
|
|
|
|
|
|
private:
|
|
|
|
ParseResult parseAffineMapDef();
|
|
|
|
|
|
|
|
// Functions.
|
|
|
|
ParseResult parseFunctionSignature(StringRef &name, FunctionType *&type);
|
|
|
|
ParseResult parseExtFunc();
|
|
|
|
ParseResult parseCFGFunc();
|
|
|
|
ParseResult parseMLFunc();
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
/// Affine map declaration.
|
|
|
|
///
|
|
|
|
/// affine-map-def ::= affine-map-id `=` affine-map-inline
|
|
|
|
///
|
|
|
|
ParseResult ModuleParser::parseAffineMapDef() {
|
|
|
|
assert(getToken().is(Token::hash_identifier));
|
|
|
|
|
|
|
|
StringRef affineMapId = getTokenSpelling().drop_front();
|
|
|
|
|
|
|
|
// Check for redefinitions.
|
|
|
|
auto *&entry = getState().affineMapDefinitions[affineMapId];
|
|
|
|
if (entry)
|
|
|
|
return emitError("redefinition of affine map id '" + affineMapId + "'");
|
|
|
|
|
|
|
|
consumeToken(Token::hash_identifier);
|
|
|
|
|
|
|
|
// Parse the '='
|
|
|
|
if (!consumeIf(Token::equal))
|
|
|
|
return emitError("expected '=' in affine map outlined definition");
|
|
|
|
|
|
|
|
entry = parseAffineMapInline();
|
|
|
|
if (!entry)
|
|
|
|
return ParseFailure;
|
|
|
|
|
|
|
|
getModule()->affineMapList.push_back(entry);
|
|
|
|
return ParseSuccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse a function signature, starting with a name and including the parameter
|
|
|
|
/// list.
|
|
|
|
///
|
|
|
|
/// argument-list ::= type (`,` type)* | /*empty*/
|
|
|
|
/// function-signature ::= function-id `(` argument-list `)` (`->` type-list)?
|
|
|
|
///
|
|
|
|
ParseResult ModuleParser::parseFunctionSignature(StringRef &name,
|
|
|
|
FunctionType *&type) {
|
|
|
|
if (getToken().isNot(Token::at_identifier))
|
|
|
|
return emitError("expected a function identifier like '@foo'");
|
|
|
|
|
|
|
|
name = getTokenSpelling().drop_front();
|
|
|
|
consumeToken(Token::at_identifier);
|
|
|
|
|
|
|
|
if (getToken().isNot(Token::l_paren))
|
|
|
|
return emitError("expected '(' in function signature");
|
|
|
|
|
|
|
|
SmallVector<Type *, 4> arguments;
|
|
|
|
if (parseTypeList(arguments))
|
|
|
|
return ParseFailure;
|
|
|
|
|
|
|
|
// Parse the return type if present.
|
|
|
|
SmallVector<Type *, 4> results;
|
|
|
|
if (consumeIf(Token::arrow)) {
|
|
|
|
if (parseTypeList(results))
|
|
|
|
return ParseFailure;
|
|
|
|
}
|
|
|
|
type = builder.getFunctionType(arguments, results);
|
|
|
|
return ParseSuccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// External function declarations.
|
|
|
|
///
|
|
|
|
/// ext-func ::= `extfunc` function-signature
|
|
|
|
///
|
|
|
|
ParseResult ModuleParser::parseExtFunc() {
|
|
|
|
consumeToken(Token::kw_extfunc);
|
|
|
|
|
|
|
|
StringRef name;
|
|
|
|
FunctionType *type = nullptr;
|
|
|
|
if (parseFunctionSignature(name, type))
|
|
|
|
return ParseFailure;
|
|
|
|
|
|
|
|
// Okay, the external function definition was parsed correctly.
|
|
|
|
getModule()->functionList.push_back(new ExtFunction(name, type));
|
|
|
|
return ParseSuccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CFG function declarations.
|
|
|
|
///
|
|
|
|
/// cfg-func ::= `cfgfunc` function-signature `{` basic-block+ `}`
|
|
|
|
///
|
|
|
|
ParseResult ModuleParser::parseCFGFunc() {
|
|
|
|
consumeToken(Token::kw_cfgfunc);
|
|
|
|
|
|
|
|
StringRef name;
|
|
|
|
FunctionType *type = nullptr;
|
|
|
|
if (parseFunctionSignature(name, type))
|
|
|
|
return ParseFailure;
|
|
|
|
|
|
|
|
// Okay, the CFG function signature was parsed correctly, create the function.
|
|
|
|
auto function = new CFGFunction(name, type);
|
|
|
|
|
|
|
|
return CFGFunctionParser(getState(), function).parseFunctionBody();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ML function declarations.
|
|
|
|
///
|
|
|
|
/// ml-func ::= `mlfunc` ml-func-signature `{` ml-stmt* ml-return-stmt `}`
|
|
|
|
///
|
|
|
|
ParseResult ModuleParser::parseMLFunc() {
|
|
|
|
consumeToken(Token::kw_mlfunc);
|
|
|
|
|
|
|
|
StringRef name;
|
|
|
|
FunctionType *type = nullptr;
|
|
|
|
|
|
|
|
// FIXME: Parse ML function signature (args + types)
|
|
|
|
// by passing pointer to SmallVector<identifier> into parseFunctionSignature
|
|
|
|
if (parseFunctionSignature(name, type))
|
|
|
|
return ParseFailure;
|
|
|
|
|
|
|
|
// Okay, the ML function signature was parsed correctly, create the function.
|
|
|
|
auto function = new MLFunction(name, type);
|
|
|
|
|
|
|
|
return MLFunctionParser(getState(), function).parseFunctionBody();
|
|
|
|
}
|
|
|
|
|
2018-06-23 01:39:19 +08:00
|
|
|
/// This is the top-level module parser.
|
2018-07-11 01:08:27 +08:00
|
|
|
ParseResult ModuleParser::parseModule() {
|
2018-06-23 01:39:19 +08:00
|
|
|
while (1) {
|
2018-07-10 10:05:38 +08:00
|
|
|
switch (getToken().getKind()) {
|
2018-06-23 01:39:19 +08:00
|
|
|
default:
|
|
|
|
emitError("expected a top level entity");
|
2018-07-11 01:08:27 +08:00
|
|
|
return ParseFailure;
|
2018-06-23 01:39:19 +08:00
|
|
|
|
2018-07-04 11:16:08 +08:00
|
|
|
// If we got to the end of the file, then we're done.
|
2018-06-23 01:39:19 +08:00
|
|
|
case Token::eof:
|
2018-07-11 01:08:27 +08:00
|
|
|
return ParseSuccess;
|
2018-06-23 01:39:19 +08:00
|
|
|
|
|
|
|
// If we got an error token, then the lexer already emitted an error, just
|
|
|
|
// stop. Someday we could introduce error recovery if there was demand for
|
|
|
|
// it.
|
|
|
|
case Token::error:
|
2018-07-11 01:08:27 +08:00
|
|
|
return ParseFailure;
|
2018-06-23 01:39:19 +08:00
|
|
|
|
2018-07-11 01:08:27 +08:00
|
|
|
case Token::hash_identifier:
|
|
|
|
if (parseAffineMapDef())
|
|
|
|
return ParseFailure;
|
2018-06-24 07:03:42 +08:00
|
|
|
break;
|
|
|
|
|
2018-07-11 01:08:27 +08:00
|
|
|
case Token::kw_extfunc:
|
|
|
|
if (parseExtFunc())
|
|
|
|
return ParseFailure;
|
2018-06-23 01:39:19 +08:00
|
|
|
break;
|
2018-07-04 11:16:08 +08:00
|
|
|
|
2018-07-11 01:08:27 +08:00
|
|
|
case Token::kw_cfgfunc:
|
|
|
|
if (parseCFGFunc())
|
|
|
|
return ParseFailure;
|
2018-06-28 02:03:08 +08:00
|
|
|
break;
|
2018-06-23 01:39:19 +08:00
|
|
|
|
2018-06-29 08:02:32 +08:00
|
|
|
case Token::kw_mlfunc:
|
2018-07-11 01:08:27 +08:00
|
|
|
if (parseMLFunc())
|
|
|
|
return ParseFailure;
|
2018-06-29 08:02:32 +08:00
|
|
|
break;
|
|
|
|
|
2018-07-04 11:16:08 +08:00
|
|
|
// TODO: affine entity declarations, etc.
|
2018-06-23 01:39:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-07-04 04:24:09 +08:00
|
|
|
void mlir::defaultErrorReporter(const llvm::SMDiagnostic &error) {
|
|
|
|
const auto &sourceMgr = *error.getSourceMgr();
|
|
|
|
sourceMgr.PrintMessage(error.getLoc(), error.getKind(), error.getMessage());
|
|
|
|
}
|
|
|
|
|
2018-06-23 01:39:19 +08:00
|
|
|
/// This parses the file specified by the indicated SourceMgr and returns an
|
|
|
|
/// MLIR module if it was valid. If not, it emits diagnostics and returns null.
|
2018-06-25 10:17:35 +08:00
|
|
|
Module *mlir::parseSourceFile(llvm::SourceMgr &sourceMgr, MLIRContext *context,
|
2018-07-04 04:24:09 +08:00
|
|
|
SMDiagnosticHandlerTy errorReporter) {
|
2018-07-11 01:08:27 +08:00
|
|
|
// This is the result module we are parsing into.
|
|
|
|
std::unique_ptr<Module> module(new Module(context));
|
|
|
|
|
|
|
|
ParserState state(sourceMgr, module.get(),
|
2018-07-12 04:26:23 +08:00
|
|
|
errorReporter ? errorReporter : defaultErrorReporter);
|
2018-07-11 01:08:27 +08:00
|
|
|
if (ModuleParser(state).parseModule())
|
|
|
|
return nullptr;
|
2018-07-07 01:46:19 +08:00
|
|
|
|
|
|
|
// Make sure the parse module has no other structural problems detected by the
|
|
|
|
// verifier.
|
2018-07-11 01:08:27 +08:00
|
|
|
module->verify();
|
|
|
|
return module.release();
|
2018-06-23 01:39:19 +08:00
|
|
|
}
|