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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2018-06-30 09:09:29 +08:00
|
|
|
#include <stack>
|
2018-06-23 01:39:19 +08:00
|
|
|
|
|
|
|
#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-06-24 07:03:42 +08:00
|
|
|
#include "mlir/IR/CFGFunction.h"
|
2018-06-30 09:09:29 +08:00
|
|
|
#include "mlir/IR/Module.h"
|
2018-06-29 08:02:32 +08:00
|
|
|
#include "mlir/IR/MLFunction.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
|
|
|
|
|
|
|
namespace {
|
2018-06-24 07:03:42 +08:00
|
|
|
class CFGFunctionParserState;
|
2018-06-30 09:09:29 +08:00
|
|
|
class AffineMapParserState;
|
2018-06-24 07:03:42 +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
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Main parser implementation.
|
|
|
|
class Parser {
|
2018-06-29 11:45:33 +08:00
|
|
|
public:
|
2018-06-25 10:17:35 +08:00
|
|
|
Parser(llvm::SourceMgr &sourceMgr, MLIRContext *context,
|
|
|
|
const SMDiagnosticHandlerTy &errorReporter)
|
|
|
|
: context(context),
|
|
|
|
lex(sourceMgr, errorReporter),
|
|
|
|
curToken(lex.lexToken()),
|
|
|
|
errorReporter(errorReporter) {
|
2018-06-23 01:39:19 +08:00
|
|
|
module.reset(new Module());
|
|
|
|
}
|
|
|
|
|
|
|
|
Module *parseModule();
|
|
|
|
private:
|
|
|
|
// State.
|
2018-06-23 13:03:48 +08:00
|
|
|
MLIRContext *const context;
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
const SMDiagnosticHandlerTy &errorReporter;
|
|
|
|
|
2018-06-23 01:39:19 +08:00
|
|
|
// This is the result module we are parsing into.
|
|
|
|
std::unique_ptr<Module> module;
|
|
|
|
|
2018-06-28 02:03:08 +08:00
|
|
|
// A map from affine map identifier to AffineMap.
|
|
|
|
// TODO(andydavis) Remove use of unique_ptr when AffineMaps are bump pointer
|
|
|
|
// allocated.
|
|
|
|
llvm::StringMap<std::unique_ptr<AffineMap>> affineMaps;
|
|
|
|
|
2018-06-23 01:39:19 +08:00
|
|
|
private:
|
|
|
|
// Helper methods.
|
|
|
|
|
|
|
|
/// Emit an error and return failure.
|
2018-06-24 07:03:42 +08:00
|
|
|
ParseResult emitError(const Twine &message) {
|
|
|
|
return emitError(curToken.getLoc(), message);
|
|
|
|
}
|
|
|
|
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() {
|
|
|
|
assert(curToken.isNot(Token::eof, Token::error) &&
|
|
|
|
"shouldn't advance past EOF or errors");
|
|
|
|
curToken = lex.lexToken();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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-06-23 01:39:19 +08:00
|
|
|
assert(curToken.is(kind) && "consumed an unexpected token");
|
|
|
|
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-06-23 06:52:02 +08:00
|
|
|
if (curToken.isNot(kind))
|
|
|
|
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-06-30 09:09:29 +08:00
|
|
|
// Identifiers
|
|
|
|
ParseResult parseDimIdList(SmallVectorImpl<StringRef> &dims,
|
|
|
|
SmallVectorImpl<StringRef> &symbols);
|
|
|
|
ParseResult parseSymbolIdList(SmallVectorImpl<StringRef> &dims,
|
|
|
|
SmallVectorImpl<StringRef> &symbols);
|
|
|
|
StringRef parseDimOrSymbolId(SmallVectorImpl<StringRef> &dims,
|
|
|
|
SmallVectorImpl<StringRef> &symbols,
|
|
|
|
bool symbol);
|
|
|
|
|
2018-06-28 02:03:08 +08:00
|
|
|
// Polyhedral structures
|
|
|
|
ParseResult parseAffineMapDef();
|
2018-06-30 09:09:29 +08:00
|
|
|
AffineMap *parseAffineMapInline(StringRef mapId);
|
|
|
|
AffineExpr *parseAffineExpr(AffineMapParserState &state);
|
2018-06-28 02:03:08 +08:00
|
|
|
|
2018-06-24 07:03:42 +08:00
|
|
|
// Functions.
|
2018-06-23 13:03:48 +08:00
|
|
|
ParseResult parseFunctionSignature(StringRef &name, FunctionType *&type);
|
2018-06-23 01:39:19 +08:00
|
|
|
ParseResult parseExtFunc();
|
2018-06-24 07:03:42 +08:00
|
|
|
ParseResult parseCFGFunc();
|
2018-06-29 08:02:32 +08:00
|
|
|
ParseResult parseMLFunc();
|
2018-06-24 07:03:42 +08:00
|
|
|
ParseResult parseBasicBlock(CFGFunctionParserState &functionState);
|
2018-06-29 08:02:32 +08:00
|
|
|
MLStatement *parseMLStatement(MLFunction *currentFunction);
|
2018-06-25 02:18:29 +08:00
|
|
|
|
2018-06-29 11:45:33 +08:00
|
|
|
ParseResult parseCFGOperation(BasicBlock *currentBB,
|
|
|
|
CFGFunctionParserState &functionState);
|
|
|
|
ParseResult parseTerminator(BasicBlock *currentBB,
|
|
|
|
CFGFunctionParserState &functionState);
|
|
|
|
|
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-06-23 06:52:02 +08:00
|
|
|
if (curToken.is(Token::error))
|
|
|
|
return ParseFailure;
|
|
|
|
|
2018-06-25 10:17:35 +08:00
|
|
|
errorReporter(
|
|
|
|
lex.getSourceMgr().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.
|
|
|
|
if (curToken.is(rightToken)) {
|
|
|
|
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-06-23 06:52:02 +08:00
|
|
|
switch (curToken.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-06-23 13:03:48 +08:00
|
|
|
return Type::getBF16(context);
|
2018-06-23 06:52:02 +08:00
|
|
|
case Token::kw_f16:
|
|
|
|
consumeToken(Token::kw_f16);
|
2018-06-23 13:03:48 +08:00
|
|
|
return Type::getF16(context);
|
2018-06-23 06:52:02 +08:00
|
|
|
case Token::kw_f32:
|
|
|
|
consumeToken(Token::kw_f32);
|
2018-06-23 13:03:48 +08:00
|
|
|
return Type::getF32(context);
|
2018-06-23 06:52:02 +08:00
|
|
|
case Token::kw_f64:
|
|
|
|
consumeToken(Token::kw_f64);
|
2018-06-23 13:03:48 +08:00
|
|
|
return Type::getF64(context);
|
2018-06-30 13:08:05 +08:00
|
|
|
case Token::kw_affineint:
|
|
|
|
consumeToken(Token::kw_affineint);
|
|
|
|
return Type::getAffineInt(context);
|
|
|
|
case Token::inttype: {
|
|
|
|
auto width = curToken.getIntTypeBitwidth();
|
|
|
|
if (!width.hasValue())
|
|
|
|
return (emitError("invalid integer width"), nullptr);
|
|
|
|
consumeToken(Token::inttype);
|
|
|
|
return Type::getInt(width.getValue(), context);
|
|
|
|
}
|
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-06-23 06:52:02 +08:00
|
|
|
if (curToken.is(Token::kw_vector))
|
|
|
|
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
|
|
|
|
|
|
|
if (curToken.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;
|
|
|
|
while (curToken.is(Token::integer)) {
|
|
|
|
// Make sure this integer value is in bound and valid.
|
|
|
|
auto dimension = curToken.getUnsignedIntegerValue();
|
|
|
|
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'.
|
|
|
|
if (curToken.isNot(Token::bare_identifier) ||
|
|
|
|
curToken.getSpelling()[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'.
|
|
|
|
if (curToken.getSpelling().size() != 1)
|
|
|
|
lex.resetPointer(curToken.getSpelling().data()+1);
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
while (curToken.isAny(Token::integer, Token::question)) {
|
|
|
|
if (consumeIf(Token::question)) {
|
|
|
|
dimensions.push_back(-1);
|
|
|
|
} else {
|
|
|
|
// Make sure this integer value is in bound and valid.
|
|
|
|
auto dimension = curToken.getUnsignedIntegerValue();
|
|
|
|
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'.
|
|
|
|
if (curToken.isNot(Token::bare_identifier) ||
|
|
|
|
curToken.getSpelling()[0] != 'x')
|
|
|
|
return emitError("expected 'x' in dimension list");
|
|
|
|
|
|
|
|
// If we had a prefix of 'x', lex the next token immediately after the 'x'.
|
|
|
|
if (curToken.getSpelling().size() != 1)
|
|
|
|
lex.resetPointer(curToken.getSpelling().data()+1);
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
return UnrankedTensorType::get(elementType);
|
|
|
|
return RankedTensorType::get(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-06-30 13:08:05 +08:00
|
|
|
return Type::getInt(1, context);
|
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-06-23 06:52:02 +08:00
|
|
|
assert(curToken.is(Token::l_paren));
|
|
|
|
|
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-06-23 13:03:48 +08:00
|
|
|
return FunctionType::get(arguments, results, context);
|
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-06-23 06:52:02 +08:00
|
|
|
switch (curToken.getKind()) {
|
|
|
|
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-06-30 09:09:29 +08:00
|
|
|
namespace {
|
|
|
|
/// This class represents the transient parser state while parsing an affine
|
|
|
|
/// expression.
|
|
|
|
class AffineMapParserState {
|
|
|
|
public:
|
|
|
|
explicit AffineMapParserState(ArrayRef<StringRef> dims,
|
|
|
|
ArrayRef<StringRef> symbols) :
|
|
|
|
dims_(dims), symbols_(symbols) {}
|
|
|
|
|
|
|
|
unsigned dimCount() const { return dims_.size(); }
|
|
|
|
unsigned symbolCount() const { return symbols_.size(); }
|
|
|
|
|
|
|
|
// Stack operations for affine expression parsing
|
|
|
|
// TODO(bondhugula): all of this will be improved/made more principled
|
|
|
|
void pushAffineExpr(AffineExpr *expr) { exprStack.push(expr); }
|
|
|
|
AffineExpr *popAffineExpr() {
|
|
|
|
auto *t = exprStack.top();
|
|
|
|
exprStack.pop();
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
AffineExpr *topAffineExpr() { return exprStack.top(); }
|
|
|
|
|
|
|
|
ArrayRef<StringRef> getDims() const { return dims_; }
|
|
|
|
ArrayRef<StringRef> getSymbols() const { return symbols_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
const ArrayRef<StringRef> dims_;
|
|
|
|
const ArrayRef<StringRef> symbols_;
|
|
|
|
|
|
|
|
// TEMP: stack to hold affine expressions
|
|
|
|
std::stack<AffineExpr *> exprStack;
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2018-06-28 02:03:08 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Polyhedral structures.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// Affine map declaration.
|
|
|
|
///
|
|
|
|
/// affine-map-def ::= affine-map-id `=` affine-map-inline
|
|
|
|
///
|
|
|
|
ParseResult Parser::parseAffineMapDef() {
|
2018-06-30 02:15:56 +08:00
|
|
|
assert(curToken.is(Token::affine_map_identifier));
|
2018-06-28 02:03:08 +08:00
|
|
|
|
|
|
|
StringRef affineMapId = curToken.getSpelling().drop_front();
|
2018-06-30 09:09:29 +08:00
|
|
|
consumeToken(Token::affine_map_identifier);
|
|
|
|
|
2018-06-28 02:03:08 +08:00
|
|
|
// Check that 'affineMapId' is unique.
|
|
|
|
// TODO(andydavis) Add a unit test for this case.
|
|
|
|
if (affineMaps.count(affineMapId) > 0)
|
2018-06-29 11:45:33 +08:00
|
|
|
return emitError("redefinition of affine map id '" + affineMapId + "'");
|
2018-06-30 09:09:29 +08:00
|
|
|
// Parse the '='
|
|
|
|
if (!consumeIf(Token::equal))
|
|
|
|
return emitError("expected '=' in affine map outlined definition");
|
2018-06-28 02:03:08 +08:00
|
|
|
|
2018-06-30 09:09:29 +08:00
|
|
|
auto *affineMap = parseAffineMapInline(affineMapId);
|
|
|
|
affineMaps[affineMapId].reset(affineMap);
|
|
|
|
if (!affineMap) return ParseFailure;
|
2018-06-28 02:03:08 +08:00
|
|
|
|
2018-06-30 09:09:29 +08:00
|
|
|
module->affineMapList.push_back(affineMap);
|
|
|
|
return affineMap ? ParseSuccess : ParseFailure;
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Parse a multi-dimensional affine expression
|
|
|
|
/// affine-expr ::= `(` affine-expr `)`
|
|
|
|
/// | affine-expr `+` affine-expr
|
|
|
|
/// | affine-expr `-` affine-expr
|
|
|
|
/// | `-`? integer-literal `*` affine-expr
|
|
|
|
/// | `ceildiv` `(` affine-expr `,` integer-literal `)`
|
|
|
|
/// | `floordiv` `(` affine-expr `,` integer-literal `)`
|
|
|
|
/// | affine-expr `mod` integer-literal
|
|
|
|
/// | bare-id
|
|
|
|
/// | `-`? integer-literal
|
|
|
|
/// multi-dim-affine-expr ::= `(` affine-expr (`,` affine-expr)* `)
|
|
|
|
///
|
|
|
|
/// Use 'state' to check if valid identifiers appear.
|
|
|
|
///
|
|
|
|
AffineExpr *Parser::parseAffineExpr(AffineMapParserState &state) {
|
|
|
|
// TODO(bondhugula): complete support for this
|
|
|
|
// The code below is all placeholder / it is wrong / not complete
|
|
|
|
// Operator precedence not considered; pure left to right associativity
|
|
|
|
if (curToken.is(Token::comma)) {
|
|
|
|
emitError("expecting affine expression");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (curToken.isNot(Token::comma, Token::r_paren,
|
|
|
|
Token::eof, Token::error)) {
|
|
|
|
switch (curToken.getKind()) {
|
|
|
|
case Token::bare_identifier: {
|
|
|
|
// TODO(bondhugula): look up state to see if it's a symbol or dim_id and
|
|
|
|
// get its position
|
|
|
|
AffineExpr *expr = AffineDimExpr::get(0, context);
|
|
|
|
state.pushAffineExpr(expr);
|
|
|
|
consumeToken(Token::bare_identifier);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Token::plus: {
|
|
|
|
consumeToken(Token::plus);
|
|
|
|
if (state.topAffineExpr()) {
|
|
|
|
auto lChild = state.popAffineExpr();
|
|
|
|
auto rChild = parseAffineExpr(state);
|
|
|
|
if (rChild) {
|
|
|
|
auto binaryOpExpr = AffineAddExpr::get(lChild, rChild, context);
|
|
|
|
state.popAffineExpr();
|
|
|
|
state.pushAffineExpr(binaryOpExpr);
|
|
|
|
} else {
|
|
|
|
emitError("right operand of + missing");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
emitError("left operand of + missing");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Token::integer: {
|
|
|
|
AffineExpr *expr = AffineConstantExpr::get(
|
|
|
|
curToken.getUnsignedIntegerValue().getValue(), context);
|
|
|
|
state.pushAffineExpr(expr);
|
|
|
|
consumeToken(Token::integer);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Token::l_paren: {
|
|
|
|
consumeToken(Token::l_paren);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Token::r_paren: {
|
|
|
|
consumeToken(Token::r_paren);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
emitError("affine map expr parse impl incomplete/unexpected token");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!state.topAffineExpr()) {
|
|
|
|
// An error will be emitted by parse comma separated list on an empty list
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return state.topAffineExpr();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return empty string if no bare id was found
|
|
|
|
StringRef Parser::parseDimOrSymbolId(SmallVectorImpl<StringRef> &dims,
|
|
|
|
SmallVectorImpl<StringRef> &symbols,
|
|
|
|
bool symbol = false) {
|
|
|
|
if (curToken.isNot(Token::bare_identifier)) {
|
|
|
|
emitError("expected bare identifier");
|
|
|
|
return StringRef();
|
|
|
|
}
|
|
|
|
// TODO(bondhugula): check whether the id already exists in either
|
|
|
|
// state.symbols or state.dims; report error if it does; otherwise create a
|
|
|
|
// new one.
|
|
|
|
StringRef ref = curToken.getSpelling();
|
|
|
|
consumeToken(Token::bare_identifier);
|
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
|
|
|
|
ParseResult Parser::parseSymbolIdList(SmallVectorImpl<StringRef> &dims,
|
|
|
|
SmallVectorImpl<StringRef> &symbols) {
|
|
|
|
if (!consumeIf(Token::l_bracket)) return emitError("expected '['");
|
|
|
|
|
|
|
|
auto parseElt = [&]() -> ParseResult {
|
|
|
|
auto elt = parseDimOrSymbolId(dims, symbols, true);
|
|
|
|
// FIXME(bondhugula): assuming dim arg for now
|
|
|
|
if (!elt.empty()) {
|
|
|
|
symbols.push_back(elt);
|
|
|
|
return ParseSuccess;
|
|
|
|
}
|
|
|
|
return ParseFailure;
|
|
|
|
};
|
|
|
|
return parseCommaSeparatedList(Token::r_bracket, parseElt);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(andy,bondhugula)
|
|
|
|
ParseResult Parser::parseDimIdList(SmallVectorImpl<StringRef> &dims,
|
|
|
|
SmallVectorImpl<StringRef> &symbols) {
|
|
|
|
if (!consumeIf(Token::l_paren))
|
|
|
|
return emitError("expected '(' at start of dimensional identifiers list");
|
|
|
|
|
|
|
|
auto parseElt = [&]() -> ParseResult {
|
|
|
|
auto elt = parseDimOrSymbolId(dims, symbols, false);
|
|
|
|
if (!elt.empty()) {
|
|
|
|
dims.push_back(elt);
|
|
|
|
return ParseSuccess;
|
|
|
|
}
|
|
|
|
return ParseFailure;
|
|
|
|
};
|
|
|
|
|
|
|
|
return parseCommaSeparatedList(Token::r_paren, parseElt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Affine map definition.
|
|
|
|
///
|
|
|
|
/// 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)+ `)`
|
|
|
|
///
|
|
|
|
AffineMap *Parser::parseAffineMapInline(StringRef mapId) {
|
|
|
|
SmallVector<StringRef, 4> dims;
|
|
|
|
SmallVector<StringRef, 4> symbols;
|
|
|
|
|
|
|
|
// List of dimensional identifiers.
|
|
|
|
if (parseDimIdList(dims, symbols)) return nullptr;
|
|
|
|
|
|
|
|
// Symbols are optional.
|
|
|
|
if (curToken.is(Token::l_bracket)) {
|
|
|
|
if (parseSymbolIdList(dims, symbols)) return nullptr;
|
|
|
|
}
|
|
|
|
if (!consumeIf(Token::arrow)) {
|
|
|
|
emitError("expected '->' or '['");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (!consumeIf(Token::l_paren)) {
|
|
|
|
emitError("expected '(' at start of affine map range");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
AffineMapParserState affState(dims, symbols);
|
|
|
|
|
|
|
|
SmallVector<AffineExpr *, 4> exprs;
|
|
|
|
auto parseElt = [&]() -> ParseResult {
|
|
|
|
auto elt = parseAffineExpr(affState);
|
|
|
|
ParseResult res = elt ? ParseSuccess : ParseFailure;
|
|
|
|
exprs.push_back(elt);
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Parse a multi-dimensional affine expression (a comma-separated list of 1-d
|
|
|
|
// affine expressions)
|
|
|
|
if (parseCommaSeparatedList(Token::r_paren, parseElt, false)) return nullptr;
|
|
|
|
|
|
|
|
// Parsed a valid affine map
|
|
|
|
auto *affineMap =
|
|
|
|
AffineMap::get(affState.dimCount(), affState.symbolCount(), exprs,
|
|
|
|
context);
|
|
|
|
|
|
|
|
return affineMap;
|
2018-06-28 02:03:08 +08:00
|
|
|
}
|
|
|
|
|
2018-06-23 01:39:19 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-06-24 07:03:42 +08:00
|
|
|
// Functions
|
2018-06-23 01:39:19 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-06-24 07:03:42 +08:00
|
|
|
|
2018-06-23 01:39:19 +08:00
|
|
|
/// 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)?
|
|
|
|
///
|
2018-06-23 13:03:48 +08:00
|
|
|
ParseResult Parser::parseFunctionSignature(StringRef &name,
|
|
|
|
FunctionType *&type) {
|
2018-06-23 01:39:19 +08:00
|
|
|
if (curToken.isNot(Token::at_identifier))
|
|
|
|
return emitError("expected a function identifier like '@foo'");
|
|
|
|
|
|
|
|
name = curToken.getSpelling().drop_front();
|
|
|
|
consumeToken(Token::at_identifier);
|
|
|
|
|
|
|
|
if (curToken.isNot(Token::l_paren))
|
|
|
|
return emitError("expected '(' in function signature");
|
|
|
|
|
2018-06-30 09:09:29 +08:00
|
|
|
SmallVector<Type*, 4> arguments;
|
|
|
|
if (parseTypeList(arguments))
|
2018-06-23 06:52:02 +08:00
|
|
|
return ParseFailure;
|
|
|
|
|
|
|
|
// Parse the return type if present.
|
2018-06-23 13:03:48 +08:00
|
|
|
SmallVector<Type*, 4> results;
|
2018-06-23 06:52:02 +08:00
|
|
|
if (consumeIf(Token::arrow)) {
|
2018-06-23 13:03:48 +08:00
|
|
|
if (parseTypeList(results))
|
2018-06-23 06:52:02 +08:00
|
|
|
return ParseFailure;
|
|
|
|
}
|
2018-06-23 13:03:48 +08:00
|
|
|
type = FunctionType::get(arguments, results, context);
|
2018-06-23 01:39:19 +08:00
|
|
|
return ParseSuccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// External function declarations.
|
|
|
|
///
|
|
|
|
/// ext-func ::= `extfunc` function-signature
|
|
|
|
///
|
|
|
|
ParseResult Parser::parseExtFunc() {
|
|
|
|
consumeToken(Token::kw_extfunc);
|
|
|
|
|
|
|
|
StringRef name;
|
2018-06-23 13:03:48 +08:00
|
|
|
FunctionType *type = nullptr;
|
|
|
|
if (parseFunctionSignature(name, type))
|
2018-06-23 01:39:19 +08:00
|
|
|
return ParseFailure;
|
|
|
|
|
|
|
|
|
|
|
|
// Okay, the external function definition was parsed correctly.
|
2018-06-24 07:03:42 +08:00
|
|
|
module->functionList.push_back(new ExtFunction(name, type));
|
|
|
|
return ParseSuccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
/// This class represents the transient parser state for the internals of a
|
|
|
|
/// function as we are parsing it, e.g. the names for basic blocks. It handles
|
|
|
|
/// forward references.
|
|
|
|
class CFGFunctionParserState {
|
2018-06-30 09:09:29 +08:00
|
|
|
public:
|
2018-06-25 02:18:29 +08:00
|
|
|
CFGFunction *function;
|
|
|
|
llvm::StringMap<std::pair<BasicBlock*, SMLoc>> blocksByName;
|
|
|
|
|
2018-06-24 07:03:42 +08:00
|
|
|
CFGFunctionParserState(CFGFunction *function) : function(function) {}
|
|
|
|
|
|
|
|
/// 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) {
|
|
|
|
blockAndLoc.first = new BasicBlock(function);
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
|
|
|
|
/// CFG function declarations.
|
|
|
|
///
|
|
|
|
/// cfg-func ::= `cfgfunc` function-signature `{` basic-block+ `}`
|
|
|
|
///
|
|
|
|
ParseResult Parser::parseCFGFunc() {
|
|
|
|
consumeToken(Token::kw_cfgfunc);
|
|
|
|
|
|
|
|
StringRef name;
|
|
|
|
FunctionType *type = nullptr;
|
|
|
|
if (parseFunctionSignature(name, type))
|
|
|
|
return ParseFailure;
|
|
|
|
|
|
|
|
if (!consumeIf(Token::l_brace))
|
|
|
|
return emitError("expected '{' in CFG function");
|
|
|
|
|
|
|
|
// Okay, the CFG function signature was parsed correctly, create the function.
|
|
|
|
auto function = new CFGFunction(name, type);
|
|
|
|
|
|
|
|
// Make sure we have at least one block.
|
|
|
|
if (curToken.is(Token::r_brace))
|
|
|
|
return emitError("CFG functions must have at least one basic block");
|
|
|
|
|
|
|
|
CFGFunctionParserState functionState(function);
|
|
|
|
|
|
|
|
// Parse the list of blocks.
|
|
|
|
while (!consumeIf(Token::r_brace))
|
|
|
|
if (parseBasicBlock(functionState))
|
|
|
|
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.
|
|
|
|
for (auto &elt : functionState.blocksByName) {
|
|
|
|
auto *bb = elt.second.first;
|
|
|
|
if (!bb->getTerminator())
|
|
|
|
return emitError(elt.second.second,
|
|
|
|
"reference to an undefined basic block '" +
|
|
|
|
elt.first() + "'");
|
|
|
|
}
|
|
|
|
|
2018-06-24 07:03:42 +08:00
|
|
|
module->functionList.push_back(function);
|
|
|
|
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? `)`
|
|
|
|
///
|
|
|
|
ParseResult Parser::parseBasicBlock(CFGFunctionParserState &functionState) {
|
|
|
|
SMLoc nameLoc = curToken.getLoc();
|
|
|
|
auto name = curToken.getSpelling();
|
|
|
|
if (!consumeIf(Token::bare_identifier))
|
|
|
|
return emitError("expected basic block name");
|
2018-06-25 02:18:29 +08:00
|
|
|
|
|
|
|
auto block = functionState.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.
|
|
|
|
if (block->getTerminator())
|
2018-06-25 02:18:29 +08:00
|
|
|
return emitError(nameLoc, "redefinition of block '" + name.str() + "'");
|
|
|
|
|
|
|
|
// References to blocks can occur in any order, but we need to reassemble the
|
|
|
|
// function in the order that occurs in the source file. Do this by moving
|
|
|
|
// each block to the end of the list as it is defined.
|
|
|
|
// FIXME: This is inefficient for large functions given that blockList is a
|
|
|
|
// vector. blockList will eventually be an ilist, which will make this fast.
|
|
|
|
auto &blockList = functionState.function->blockList;
|
|
|
|
if (blockList.back() != block) {
|
|
|
|
auto it = std::find(blockList.begin(), blockList.end(), block);
|
|
|
|
assert(it != blockList.end() && "Block has to be in the blockList");
|
|
|
|
std::swap(*it, blockList.back());
|
|
|
|
}
|
2018-06-24 07:03:42 +08:00
|
|
|
|
|
|
|
// TODO: parse bb argument list.
|
|
|
|
|
|
|
|
if (!consumeIf(Token::colon))
|
|
|
|
return emitError("expected ':' after basic block name");
|
|
|
|
|
2018-06-29 11:45:33 +08:00
|
|
|
// Parse the list of operations that make up the body of the block.
|
|
|
|
while (curToken.isNot(Token::kw_return, Token::kw_br)) {
|
|
|
|
if (parseCFGOperation(block, functionState))
|
|
|
|
return ParseFailure;
|
|
|
|
}
|
2018-06-24 07:03:42 +08:00
|
|
|
|
2018-06-29 11:45:33 +08:00
|
|
|
if (parseTerminator(block, functionState))
|
|
|
|
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
|
|
|
|
///
|
|
|
|
ParseResult Parser::
|
|
|
|
parseCFGOperation(BasicBlock *currentBB,
|
|
|
|
CFGFunctionParserState &functionState) {
|
2018-06-24 07:03:42 +08:00
|
|
|
|
2018-06-29 11:45:33 +08:00
|
|
|
// TODO: parse ssa-id.
|
|
|
|
|
|
|
|
if (curToken.isNot(Token::string))
|
|
|
|
return emitError("expected operation name in quotes");
|
|
|
|
|
|
|
|
auto name = curToken.getStringValue();
|
|
|
|
if (name.empty())
|
|
|
|
return emitError("empty operation name is invalid");
|
|
|
|
|
|
|
|
consumeToken(Token::string);
|
|
|
|
|
|
|
|
if (!consumeIf(Token::l_paren))
|
|
|
|
return emitError("expected '(' in operation");
|
|
|
|
|
|
|
|
// TODO: Parse operands.
|
|
|
|
if (!consumeIf(Token::r_paren))
|
|
|
|
return emitError("expected '(' in operation");
|
|
|
|
|
|
|
|
auto nameId = Identifier::get(name, context);
|
|
|
|
new OperationInst(nameId, currentBB);
|
2018-06-24 07:03:42 +08:00
|
|
|
|
2018-06-29 11:45:33 +08:00
|
|
|
// TODO: add instruction the per-function symbol table.
|
2018-06-23 01:39:19 +08:00
|
|
|
return ParseSuccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-06-29 11:45:33 +08:00
|
|
|
ParseResult Parser::parseTerminator(BasicBlock *currentBB,
|
|
|
|
CFGFunctionParserState &functionState) {
|
2018-06-25 02:18:29 +08:00
|
|
|
switch (curToken.getKind()) {
|
|
|
|
default:
|
2018-06-29 11:45:33 +08:00
|
|
|
return emitError("expected terminator at end of basic block");
|
2018-06-25 02:18:29 +08:00
|
|
|
|
|
|
|
case Token::kw_return:
|
|
|
|
consumeToken(Token::kw_return);
|
2018-06-29 11:45:33 +08:00
|
|
|
new ReturnInst(currentBB);
|
|
|
|
return ParseSuccess;
|
2018-06-25 02:18:29 +08:00
|
|
|
|
|
|
|
case Token::kw_br: {
|
|
|
|
consumeToken(Token::kw_br);
|
|
|
|
auto destBB = functionState.getBlockNamed(curToken.getSpelling(),
|
|
|
|
curToken.getLoc());
|
|
|
|
if (!consumeIf(Token::bare_identifier))
|
2018-06-29 11:45:33 +08:00
|
|
|
return emitError("expected basic block name");
|
|
|
|
new BranchInst(destBB, currentBB);
|
|
|
|
return ParseSuccess;
|
2018-06-25 02:18:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-29 08:02:32 +08:00
|
|
|
/// ML function declarations.
|
|
|
|
///
|
|
|
|
/// ml-func ::= `mlfunc` ml-func-signature `{` ml-stmt* ml-return-stmt `}`
|
|
|
|
///
|
|
|
|
ParseResult Parser::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;
|
|
|
|
|
|
|
|
if (!consumeIf(Token::l_brace))
|
|
|
|
return emitError("expected '{' in ML function");
|
|
|
|
|
|
|
|
// Okay, the ML function signature was parsed correctly, create the function.
|
|
|
|
auto function = new MLFunction(name, type);
|
|
|
|
|
|
|
|
// Make sure we have at least one statement.
|
|
|
|
if (curToken.is(Token::r_brace))
|
|
|
|
return emitError("ML function must end with return statement");
|
|
|
|
|
|
|
|
// Parse the list of instructions.
|
|
|
|
while (!consumeIf(Token::kw_return)) {
|
|
|
|
auto *stmt = parseMLStatement(function);
|
|
|
|
if (!stmt)
|
|
|
|
return ParseFailure;
|
|
|
|
function->stmtList.push_back(stmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: parse return statement operands
|
|
|
|
if (!consumeIf(Token::r_brace))
|
|
|
|
emitError("expected '}' in ML function");
|
|
|
|
|
|
|
|
module->functionList.push_back(function);
|
|
|
|
|
|
|
|
return ParseSuccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse an MLStatement
|
|
|
|
/// TODO
|
|
|
|
///
|
|
|
|
MLStatement *Parser::parseMLStatement(MLFunction *currentFunction) {
|
|
|
|
switch (curToken.getKind()) {
|
|
|
|
default:
|
|
|
|
return (emitError("expected ML statement"), nullptr);
|
|
|
|
|
|
|
|
// TODO: add parsing of ML statements
|
|
|
|
}
|
|
|
|
}
|
2018-06-25 02:18:29 +08:00
|
|
|
|
2018-06-24 07:03:42 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Top-level entity parsing.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-06-23 01:39:19 +08:00
|
|
|
/// This is the top-level module parser.
|
|
|
|
Module *Parser::parseModule() {
|
|
|
|
while (1) {
|
|
|
|
switch (curToken.getKind()) {
|
|
|
|
default:
|
|
|
|
emitError("expected a top level entity");
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// If we got to the end of the file, then we're done.
|
|
|
|
case Token::eof:
|
|
|
|
return module.release();
|
|
|
|
|
|
|
|
// 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:
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
case Token::kw_extfunc:
|
2018-06-24 07:03:42 +08:00
|
|
|
if (parseExtFunc()) return nullptr;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Token::kw_cfgfunc:
|
|
|
|
if (parseCFGFunc()) return nullptr;
|
2018-06-23 01:39:19 +08:00
|
|
|
break;
|
2018-06-30 02:15:56 +08:00
|
|
|
case Token::affine_map_identifier:
|
2018-06-28 02:03:08 +08:00
|
|
|
if (parseAffineMapDef()) return nullptr;
|
|
|
|
break;
|
2018-06-23 01:39:19 +08:00
|
|
|
|
2018-06-29 08:02:32 +08:00
|
|
|
case Token::kw_mlfunc:
|
|
|
|
if (parseMLFunc()) return nullptr;
|
|
|
|
break;
|
|
|
|
|
|
|
|
// TODO: affine entity declarations, etc.
|
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,
|
|
|
|
const SMDiagnosticHandlerTy &errorReporter) {
|
|
|
|
return Parser(sourceMgr, context, errorReporter).parseModule();
|
2018-06-23 01:39:19 +08:00
|
|
|
}
|
2018-06-30 09:09:29 +08:00
|
|
|
|