MarisaKirisame commented on a change in pull request #5932:
URL: https://github.com/apache/incubator-tvm/pull/5932#discussion_r447985013
##########
File path: tests/python/relay/test_ir_parser.py
##########
@@ -76,6 +76,7 @@ def graph_equal(lhs, rhs):
def roundtrip(expr):
x = relay.fromtext(expr.astext())
+ import pdb; pdb.set_trace()
Review comment:
remove
##########
File path: src/parser/parser.cc
##########
@@ -0,0 +1,1103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+
+/*!
+ * \file parser.cc
+ * \brief A parser for TVM IR.
+ */
+#include <tvm/ir/module.h>
+#include <tvm/relay/expr.h>
+#include <tvm/relay/function.h>
+#include <tvm/runtime/object.h>
+#include <tvm/runtime/registry.h>
+#include <tvm/node/reflection.h>
+
+#include <fstream>
+
+#include "./diagnostic.h"
+#include "./op_table.h"
+#include "./tokenizer.h"
+
+namespace tvm {
+namespace parser {
+
+using namespace relay;
+using Expr = relay::Expr;
+
+// adtConsDefnList: adtConsDefn (',' adtConsDefn)* ','? ;
+// adtConsDefn: constructorName ('(' typeExpr (',' typeExpr)* ')')? ;
+// matchClauseList: matchClause (',' matchClause)* ','? ;
+// matchClause: pattern '=>' ('{' expr '}' | expr) ;
+// // complete or incomplete match, respectively
+// matchType : 'match' | 'match?' ;
+
+// patternList: '(' pattern (',' pattern)* ')';
+// pattern
+// : '_' # wildcardPattern
+// | localVar (':' typeExpr)? # varPattern
+// | constructorName patternList? # constructorPattern
+// | patternList # tuplePattern
+// ;
+
+struct GlobalFunc {
+ GlobalVar global;
+ Function function;
+ GlobalFunc() : global(), function() {}
+ GlobalFunc(GlobalVar global, Function function) : global(global),
function(function) {}
+ GlobalFunc(const GlobalFunc& gfunc) {
+ this->global = gfunc.global;
+ this->function = gfunc.function;
+ }
+};
+
+struct Definitions {
+ std::vector<GlobalFunc> funcs;
+ std::vector<TypeData> types;
+};
+
+struct SemVer {
+ int major;
+ int minor;
+ int patch;
+};
+
+class MetaRefExpr;
+class MetaRefExprNode : public TempExprNode {
+ public:
+ std::string type_key;
+ uint64_t node_index;
+
+ void VisitAttrs(tvm::AttrVisitor* v) {}
+
+ Expr Realize() const final { return Expr(); }
+
+ static constexpr const char* _type_key = "relay.MetaRefExpr";
+ TVM_DECLARE_FINAL_OBJECT_INFO(MetaRefExprNode, TempExprNode);
+};
+
+class MetaRefExpr : public TempExpr {
+ public:
+ /*!
+ * \brief The constructor
+ * \param expr The original relay expression.
+ * \param kind The annotation kind.
+ */
+ TVM_DLL MetaRefExpr(std::string type_key, uint64_t node_index);
+
+ TVM_DEFINE_OBJECT_REF_METHODS(MetaRefExpr, TempExpr, MetaRefExprNode);
+};
+
+MetaRefExpr::MetaRefExpr(std::string type_key, uint64_t node_index) {
+ auto rnode = make_object<MetaRefExprNode>();
+ rnode->type_key = type_key;
+ rnode->node_index = node_index;
+ data_ = std::move(rnode);
+}
+
+template<typename T>
+struct Scope {
+ std::unordered_map<std::string, T> name_map;
+ Scope() : name_map() {}
+};
+
+template<typename T>
+struct ScopeStack {
+ std::vector<Scope<T>> scope_stack;
+
+ void Add(const std::string& name, const T& value) {
+ if (!this->scope_stack.size()) {
+ LOG(FATAL) << "internal issue";
+ }
+ this->scope_stack.back().name_map.insert({ name, value });
+ }
+
+ T Lookup(const std::string& name) {
+ for (auto scope = this->scope_stack.rbegin(); scope !=
this->scope_stack.rend(); scope++) {
+ auto it = scope->name_map.find(name);
+ if (it != scope->name_map.end()) {
+ return it->second;
+ }
+ }
+ return T();
+ }
+
+ void PushStack() {
+ this->scope_stack.push_back(Scope<T>());
+ }
+
+ void PopStack() {
+ this->scope_stack.pop_back();
+ }
+};
+
+template<typename T>
+struct InternTable {
+ std::unordered_map<std::string, T> table;
+ void Add(const std::string& name, const T& t) {
+ auto it = table.find(name);
+ if (it != table.end()) {
+ LOG(FATAL) << "duplicate name";
+ } else {
+ table.insert({ name, t});
+ }
+ }
+
+ T Get(const std::string& name) {
+ auto it = table.find(name);
+ if (it != table.end()) {
+ return it->second;
+ } else {
+ return T();
+ }
+ }
+};
+
+struct Parser {
+ /*! \brief The diagnostic context used for error reporting. */
+ DiagnosticContext diag_ctx;
+
+ /*! \brief The current position in the token stream. */
+ int pos;
+
+ /*! \brief The token stream for the parser. */
+ std::vector<Token> tokens;
+
+ /*! \brief The configured operator table. */
+ OperatorTable op_table;
+
+ /*! \brief Configure the whitespace mode, right now we ignore all
whitespace. */
+ bool ignore_whitespace;
+
+ /*! \brief A global mapping for GlobalVar. */
+ InternTable<GlobalVar> global_names;
+
+ /*! \brief A global mapping for type definitions. */
+ InternTable<GlobalTypeVar> type_names;
+
+ /*! \brief A mapping from graph variable to expression, i.e., `%0 = expr`. */
+ std::unordered_map<int, Expr> graph_ctx;
+
+ /*! \brief The set of type scopes used for generics. */
+ ScopeStack<TypeVar> type_scopes;
+
+ /*! \brief The set of expression scopes used for lexical scope. */
+ ScopeStack<Var> expr_scopes;
+
+ Parser(std::vector<Token> tokens, OperatorTable op_table, Source source)
+ : diag_ctx(source), pos(0), tokens(tokens), op_table(op_table),
ignore_whitespace(true) {}
+
+ void DisplayNextN(int n) {
+ std::cout << "remaining tokens: " << std::endl;
+ auto bound = std::min(pos + n, (int)tokens.size());
+ for (int i = 0; i < bound - pos; i++) {
+ std::cout << tokens[pos + i] << std::endl;
+ }
+ }
+
+ Token Peek() {
+ // For now we ignore all whitespace tokens and comments.
+ // We can tweak this behavior later to enable white space sensitivity in
the parser.
+ while (pos < tokens.size() &&
+ ignore_whitespace && (tokens.at(pos)->token_type ==
TokenType::Whitespace ||
+ tokens.at(pos)->token_type ==
TokenType::Newline ||
+ tokens.at(pos)->token_type ==
TokenType::LineComment ||
+ tokens.at(pos)->token_type ==
TokenType::Comment)) {
+ // std::cout << "pos: " << pos << std::endl;
+ // std::cout << "tokens: " << tokens.size() << std::endl;
+ pos++;
+ }
+
+ if (pos < tokens.size()) {
+ return Token(this->tokens.at(pos));
+ } else {
+ return Token::Null();
+ }
+ }
+
+ // Allow lookahead into the token stream.
+ Token Lookahead(int n) {
+ CHECK_LE(1, n)
+ << "lookahead by > 1 is invalid";
+
+ auto old_pos = pos;
+ for (int i = 0; i < n - 1; i++) {
+ Peek();
+ pos++;
+ }
+
+ auto tok = Peek();
+ pos = old_pos;
+ return tok;
+ }
+
+ void Consume(const TokenType& token) {
+ if (tokens[pos]->token_type != token) {
+ std::string message = "expected a " + Pretty(token) + " found " +
Pretty(Peek()->token_type);
+ this->diag_ctx.Emit({tokens[pos]->line, tokens[pos]->column, message});
+ this->diag_ctx.Render();
+ }
+ pos++;
+ }
+
+ Token Match(const TokenType& token_type) {
+ auto tok = Peek();
+ Consume(token_type);
+ return tok;
+ }
+
+ bool WhenMatch(const TokenType& token_type) {
+ if (Peek()->token_type == token_type) {
+ Consume(token_type);
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ void AddGraphBinding(const Token& token, const Expr& expr) {
+ auto graph_no = token.ToNumber();
+ this->graph_ctx.insert({graph_no, expr});
+ }
+
+ Expr LookupGraphBinding(const Token& token) {
+ auto graph_no = token.ToNumber();
+ return this->graph_ctx.at(graph_no);
+ }
+
+ Var BindVar(const std::string& name, const relay::Type& type_annotation) {
+ auto var = Var(name, type_annotation);
+ this->expr_scopes.Add(name, var);
+ return var;
+ }
+
+ TypeVar BindTypeVar(const std::string& name, const TypeKind type_kind) {
+ auto type_var = TypeVar(name, type_kind);
+ this->type_scopes.Add(name, type_var);
+ return type_var;
+ }
+
+ Var LookupLocal(const Token& local) {
+ auto var = this->expr_scopes.Lookup(local.ToString());
+ if (!var.defined()) {
+ diag_ctx.Emit({ local->line, local->column, "this local variable has not
been previously declared"});
+ }
+ return var;
+ }
+
+ TypeVar LookupTypeVar(const Token& ident) {
+ auto var = this->type_scopes.Lookup(ident.ToString());
+ if (!var.defined()) {
+ diag_ctx.Emit({ ident->line, ident->column, "this type variable has not
been previously declared anywhere, perhaps a typo?"});
+ }
+ return var;
+ }
+
+ void PushScope() {
+ this->expr_scopes.PushStack();
+ }
+
+ void PopScopes(int n) {
+ for (int i = 0; i < n; i++) {
+ this->expr_scopes.PopStack();
+ }
+ }
+
+ void PushTypeScope() {
+ this->type_scopes.PushStack();
+ }
+
+ void PopTypeScopes(int n) {
+ for (int i = 0; i < n; i++) {
+ this->type_scopes.PopStack();
+ }
+ }
+
+ NDArray NumberToNDArray(const Token& token) {
+ if (token->token_type == TokenType::Integer) {
+ DLContext ctx({.device_type = DLDeviceType::kDLCPU, .device_id = 0});
+ auto dtype = String2DLDataType("int32");
+ auto data = NDArray::Empty({}, dtype, ctx);
+ auto array = reinterpret_cast<int32_t*>(data->data);
+ // revisit this, literal node issue.
+ int64_t value = Downcast<tvm::Integer>(token->data);
+ array[0] = (int32_t)value;
+ return data;
+ } else if (token->token_type == TokenType::Float) {
+ DLContext ctx({.device_type = DLDeviceType::kDLCPU, .device_id = 0});
+ auto dtype = String2DLDataType("float32");
+ auto data = NDArray::Empty({}, dtype, ctx);
+ auto array = reinterpret_cast<float*>(data->data);
+ // revisit this, literal node issue.
+ float value = Downcast<tvm::FloatImm>(token->data)->value;
+ array[0] = value;
+ return data;
+ } else {
+ throw "foo";
+ }
+ }
+
+ NDArray BooleanToNDarray(bool value) {
+ DLContext ctx({.device_type = DLDeviceType::kDLCPU, .device_id = 0});
+ auto dtype = String2DLDataType("bool");
+ auto data = NDArray::Empty({}, dtype, ctx);
+ auto array = reinterpret_cast<bool*>(data->data);
+ array[0] = value;
+ return data;
+ }
+
+ [[noreturn]] void ParseError(const Token& token, const std::string& msg) {
+ throw std::runtime_error(msg);
+ }
+
+ IRModule ParseModule() {
+ // Parse the semver header at the top of the module.
+ auto _version = ParseSemVer();
+ // Parse the definitions.
+ auto defs = ParseDefinitions();
+ // Parse the metadata section at the end.
+ auto metadata = ParseMetadata();
+ Match(TokenType::EndOfFile);
+ Map<tvm::GlobalVar, BaseFunc> funcs;
+ Map<tvm::GlobalTypeVar, TypeData> types;
+
+ for (auto func : defs.funcs) {
+ funcs.Set(func.global, func.function);
+ }
+
+ for (auto type_def : defs.types) {
+ types.Set(type_def->header, type_def);
+ }
+
+ return IRModule(funcs, types);
+ }
+
+ SemVer ParseSemVer() {
+ // Consume(TokenType::Unknown);
+ return SemVer{.major = 0, .minor = 0, .patch = 0};
+ }
+
+ Definitions ParseDefinitions() {
+ Definitions defs;
+
+ while (true) {
+ auto next = Peek();
+ switch (next->token_type) {
+ case TokenType::Defn: {
+ Consume(TokenType::Defn);
+ auto global_name = Match(TokenType::Global).ToString();
+ auto global = GlobalVar(global_name);
+ global_names.Add(global_name, global);
+ auto func = ParseFunctionDef();
+ defs.funcs.push_back(GlobalFunc(global, func));
+ continue;
+ }
+ case TokenType::TypeDef: {
+ defs.types.push_back(ParseTypeDef());
+ continue;
+ }
+ default:
+ return defs;
+ }
+ }
+ }
+
+ TypeData ParseTypeDef() {
+ // Match the `type` keyword.
+ Match(TokenType::TypeDef);
+ // Parse the type's identifier.
+ auto type_id = Match(TokenType::Identifier).ToString();
+ auto type_global = tvm::GlobalTypeVar(type_id, TypeKind::kTypeData);
+ type_names.Add(type_id, type_global);
+
+ Array<TypeVar> generics;
+
+ bool should_pop = false;
+ if (Peek()->token_type == TokenType::LSquare) {
+ // If we have generics we need to add a type scope.
+ PushTypeScope();
+ should_pop = true;
+ generics = ParseSequence<TypeVar>(TokenType::LSquare, TokenType::Comma,
TokenType::RSquare, [&]() {
+ auto type_var_name = Match(TokenType::Identifier).ToString();
+ return BindTypeVar(type_var_name, TypeKind::kType);
+ });
+ }
+
+ // Parse the list of constructors.
+ auto ctors = ParseSequence<tvm::Constructor>(TokenType::LCurly,
TokenType::Comma, TokenType::RCurly, [&]() {
+ // First match the name of the constructor.
+ auto ctor = Match(TokenType::Identifier).ToString();
+ // Match the optional field list.
+ if (Peek()->token_type != TokenType::OpenParen) {
+ return tvm::Constructor(ctor, {}, type_global);
+ } else {
+ auto arg_types = ParseSequence<Type>(TokenType::OpenParen,
TokenType::Comma, TokenType::CloseParen, [&]() {
+ return ParseType();
+ });
+ return tvm::Constructor(ctor, arg_types, type_global);
+ }
+ });
+
+ // Now pop the type scope.
+ if (should_pop) {
+ PopTypeScopes(1);
+ }
+
+ return TypeData(type_global, generics, ctors);
+ }
+
+ template <typename R>
+ R Bracket(TokenType open, TokenType close, std::function<R()> parser) {
Review comment:
const & for std::function
##########
File path: src/parser/parser.cc
##########
@@ -0,0 +1,1103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+
+/*!
+ * \file parser.cc
+ * \brief A parser for TVM IR.
+ */
+#include <tvm/ir/module.h>
+#include <tvm/relay/expr.h>
+#include <tvm/relay/function.h>
+#include <tvm/runtime/object.h>
+#include <tvm/runtime/registry.h>
+#include <tvm/node/reflection.h>
+
+#include <fstream>
+
+#include "./diagnostic.h"
+#include "./op_table.h"
+#include "./tokenizer.h"
+
+namespace tvm {
+namespace parser {
+
+using namespace relay;
+using Expr = relay::Expr;
+
+// adtConsDefnList: adtConsDefn (',' adtConsDefn)* ','? ;
+// adtConsDefn: constructorName ('(' typeExpr (',' typeExpr)* ')')? ;
+// matchClauseList: matchClause (',' matchClause)* ','? ;
+// matchClause: pattern '=>' ('{' expr '}' | expr) ;
+// // complete or incomplete match, respectively
+// matchType : 'match' | 'match?' ;
+
+// patternList: '(' pattern (',' pattern)* ')';
+// pattern
+// : '_' # wildcardPattern
+// | localVar (':' typeExpr)? # varPattern
+// | constructorName patternList? # constructorPattern
+// | patternList # tuplePattern
+// ;
+
+struct GlobalFunc {
+ GlobalVar global;
+ Function function;
+ GlobalFunc() : global(), function() {}
+ GlobalFunc(GlobalVar global, Function function) : global(global),
function(function) {}
+ GlobalFunc(const GlobalFunc& gfunc) {
+ this->global = gfunc.global;
+ this->function = gfunc.function;
+ }
+};
+
+struct Definitions {
+ std::vector<GlobalFunc> funcs;
+ std::vector<TypeData> types;
+};
+
+struct SemVer {
+ int major;
+ int minor;
+ int patch;
+};
+
+class MetaRefExpr;
+class MetaRefExprNode : public TempExprNode {
+ public:
+ std::string type_key;
+ uint64_t node_index;
+
+ void VisitAttrs(tvm::AttrVisitor* v) {}
+
+ Expr Realize() const final { return Expr(); }
+
+ static constexpr const char* _type_key = "relay.MetaRefExpr";
+ TVM_DECLARE_FINAL_OBJECT_INFO(MetaRefExprNode, TempExprNode);
+};
+
+class MetaRefExpr : public TempExpr {
+ public:
+ /*!
+ * \brief The constructor
+ * \param expr The original relay expression.
+ * \param kind The annotation kind.
+ */
+ TVM_DLL MetaRefExpr(std::string type_key, uint64_t node_index);
+
+ TVM_DEFINE_OBJECT_REF_METHODS(MetaRefExpr, TempExpr, MetaRefExprNode);
+};
+
+MetaRefExpr::MetaRefExpr(std::string type_key, uint64_t node_index) {
+ auto rnode = make_object<MetaRefExprNode>();
+ rnode->type_key = type_key;
+ rnode->node_index = node_index;
+ data_ = std::move(rnode);
+}
+
+template<typename T>
+struct Scope {
+ std::unordered_map<std::string, T> name_map;
+ Scope() : name_map() {}
+};
+
+template<typename T>
+struct ScopeStack {
+ std::vector<Scope<T>> scope_stack;
+
+ void Add(const std::string& name, const T& value) {
+ if (!this->scope_stack.size()) {
+ LOG(FATAL) << "internal issue";
+ }
+ this->scope_stack.back().name_map.insert({ name, value });
+ }
+
+ T Lookup(const std::string& name) {
+ for (auto scope = this->scope_stack.rbegin(); scope !=
this->scope_stack.rend(); scope++) {
+ auto it = scope->name_map.find(name);
+ if (it != scope->name_map.end()) {
+ return it->second;
+ }
+ }
+ return T();
+ }
+
+ void PushStack() {
+ this->scope_stack.push_back(Scope<T>());
+ }
+
+ void PopStack() {
+ this->scope_stack.pop_back();
+ }
+};
+
+template<typename T>
+struct InternTable {
+ std::unordered_map<std::string, T> table;
+ void Add(const std::string& name, const T& t) {
+ auto it = table.find(name);
+ if (it != table.end()) {
+ LOG(FATAL) << "duplicate name";
+ } else {
+ table.insert({ name, t});
+ }
+ }
+
+ T Get(const std::string& name) {
+ auto it = table.find(name);
+ if (it != table.end()) {
+ return it->second;
+ } else {
+ return T();
+ }
+ }
+};
+
+struct Parser {
+ /*! \brief The diagnostic context used for error reporting. */
+ DiagnosticContext diag_ctx;
+
+ /*! \brief The current position in the token stream. */
+ int pos;
+
+ /*! \brief The token stream for the parser. */
+ std::vector<Token> tokens;
+
+ /*! \brief The configured operator table. */
+ OperatorTable op_table;
+
+ /*! \brief Configure the whitespace mode, right now we ignore all
whitespace. */
+ bool ignore_whitespace;
+
+ /*! \brief A global mapping for GlobalVar. */
+ InternTable<GlobalVar> global_names;
+
+ /*! \brief A global mapping for type definitions. */
+ InternTable<GlobalTypeVar> type_names;
+
+ /*! \brief A mapping from graph variable to expression, i.e., `%0 = expr`. */
+ std::unordered_map<int, Expr> graph_ctx;
+
+ /*! \brief The set of type scopes used for generics. */
+ ScopeStack<TypeVar> type_scopes;
+
+ /*! \brief The set of expression scopes used for lexical scope. */
+ ScopeStack<Var> expr_scopes;
+
+ Parser(std::vector<Token> tokens, OperatorTable op_table, Source source)
+ : diag_ctx(source), pos(0), tokens(tokens), op_table(op_table),
ignore_whitespace(true) {}
+
+ void DisplayNextN(int n) {
+ std::cout << "remaining tokens: " << std::endl;
+ auto bound = std::min(pos + n, (int)tokens.size());
+ for (int i = 0; i < bound - pos; i++) {
+ std::cout << tokens[pos + i] << std::endl;
+ }
+ }
+
+ Token Peek() {
+ // For now we ignore all whitespace tokens and comments.
+ // We can tweak this behavior later to enable white space sensitivity in
the parser.
+ while (pos < tokens.size() &&
+ ignore_whitespace && (tokens.at(pos)->token_type ==
TokenType::Whitespace ||
+ tokens.at(pos)->token_type ==
TokenType::Newline ||
+ tokens.at(pos)->token_type ==
TokenType::LineComment ||
+ tokens.at(pos)->token_type ==
TokenType::Comment)) {
+ // std::cout << "pos: " << pos << std::endl;
+ // std::cout << "tokens: " << tokens.size() << std::endl;
+ pos++;
+ }
+
+ if (pos < tokens.size()) {
+ return Token(this->tokens.at(pos));
+ } else {
+ return Token::Null();
+ }
+ }
+
+ // Allow lookahead into the token stream.
+ Token Lookahead(int n) {
+ CHECK_LE(1, n)
+ << "lookahead by > 1 is invalid";
+
+ auto old_pos = pos;
+ for (int i = 0; i < n - 1; i++) {
+ Peek();
+ pos++;
+ }
+
+ auto tok = Peek();
+ pos = old_pos;
+ return tok;
+ }
+
+ void Consume(const TokenType& token) {
+ if (tokens[pos]->token_type != token) {
+ std::string message = "expected a " + Pretty(token) + " found " +
Pretty(Peek()->token_type);
+ this->diag_ctx.Emit({tokens[pos]->line, tokens[pos]->column, message});
+ this->diag_ctx.Render();
+ }
+ pos++;
+ }
+
+ Token Match(const TokenType& token_type) {
+ auto tok = Peek();
+ Consume(token_type);
+ return tok;
+ }
+
+ bool WhenMatch(const TokenType& token_type) {
+ if (Peek()->token_type == token_type) {
+ Consume(token_type);
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ void AddGraphBinding(const Token& token, const Expr& expr) {
+ auto graph_no = token.ToNumber();
+ this->graph_ctx.insert({graph_no, expr});
+ }
+
+ Expr LookupGraphBinding(const Token& token) {
+ auto graph_no = token.ToNumber();
+ return this->graph_ctx.at(graph_no);
+ }
+
+ Var BindVar(const std::string& name, const relay::Type& type_annotation) {
+ auto var = Var(name, type_annotation);
+ this->expr_scopes.Add(name, var);
+ return var;
+ }
+
+ TypeVar BindTypeVar(const std::string& name, const TypeKind type_kind) {
+ auto type_var = TypeVar(name, type_kind);
+ this->type_scopes.Add(name, type_var);
+ return type_var;
+ }
+
+ Var LookupLocal(const Token& local) {
+ auto var = this->expr_scopes.Lookup(local.ToString());
+ if (!var.defined()) {
+ diag_ctx.Emit({ local->line, local->column, "this local variable has not
been previously declared"});
+ }
+ return var;
+ }
+
+ TypeVar LookupTypeVar(const Token& ident) {
+ auto var = this->type_scopes.Lookup(ident.ToString());
+ if (!var.defined()) {
+ diag_ctx.Emit({ ident->line, ident->column, "this type variable has not
been previously declared anywhere, perhaps a typo?"});
+ }
+ return var;
+ }
+
+ void PushScope() {
+ this->expr_scopes.PushStack();
+ }
+
+ void PopScopes(int n) {
+ for (int i = 0; i < n; i++) {
+ this->expr_scopes.PopStack();
+ }
+ }
+
+ void PushTypeScope() {
+ this->type_scopes.PushStack();
+ }
+
+ void PopTypeScopes(int n) {
+ for (int i = 0; i < n; i++) {
+ this->type_scopes.PopStack();
+ }
+ }
+
+ NDArray NumberToNDArray(const Token& token) {
+ if (token->token_type == TokenType::Integer) {
+ DLContext ctx({.device_type = DLDeviceType::kDLCPU, .device_id = 0});
+ auto dtype = String2DLDataType("int32");
+ auto data = NDArray::Empty({}, dtype, ctx);
+ auto array = reinterpret_cast<int32_t*>(data->data);
+ // revisit this, literal node issue.
+ int64_t value = Downcast<tvm::Integer>(token->data);
+ array[0] = (int32_t)value;
+ return data;
+ } else if (token->token_type == TokenType::Float) {
+ DLContext ctx({.device_type = DLDeviceType::kDLCPU, .device_id = 0});
+ auto dtype = String2DLDataType("float32");
+ auto data = NDArray::Empty({}, dtype, ctx);
+ auto array = reinterpret_cast<float*>(data->data);
+ // revisit this, literal node issue.
+ float value = Downcast<tvm::FloatImm>(token->data)->value;
+ array[0] = value;
+ return data;
+ } else {
+ throw "foo";
+ }
+ }
+
+ NDArray BooleanToNDarray(bool value) {
+ DLContext ctx({.device_type = DLDeviceType::kDLCPU, .device_id = 0});
+ auto dtype = String2DLDataType("bool");
+ auto data = NDArray::Empty({}, dtype, ctx);
+ auto array = reinterpret_cast<bool*>(data->data);
+ array[0] = value;
+ return data;
+ }
+
+ [[noreturn]] void ParseError(const Token& token, const std::string& msg) {
+ throw std::runtime_error(msg);
+ }
+
+ IRModule ParseModule() {
+ // Parse the semver header at the top of the module.
+ auto _version = ParseSemVer();
+ // Parse the definitions.
+ auto defs = ParseDefinitions();
+ // Parse the metadata section at the end.
+ auto metadata = ParseMetadata();
+ Match(TokenType::EndOfFile);
+ Map<tvm::GlobalVar, BaseFunc> funcs;
+ Map<tvm::GlobalTypeVar, TypeData> types;
+
+ for (auto func : defs.funcs) {
+ funcs.Set(func.global, func.function);
+ }
+
+ for (auto type_def : defs.types) {
+ types.Set(type_def->header, type_def);
+ }
+
+ return IRModule(funcs, types);
+ }
+
+ SemVer ParseSemVer() {
+ // Consume(TokenType::Unknown);
+ return SemVer{.major = 0, .minor = 0, .patch = 0};
+ }
+
+ Definitions ParseDefinitions() {
+ Definitions defs;
+
+ while (true) {
+ auto next = Peek();
+ switch (next->token_type) {
+ case TokenType::Defn: {
+ Consume(TokenType::Defn);
+ auto global_name = Match(TokenType::Global).ToString();
+ auto global = GlobalVar(global_name);
+ global_names.Add(global_name, global);
+ auto func = ParseFunctionDef();
+ defs.funcs.push_back(GlobalFunc(global, func));
+ continue;
+ }
+ case TokenType::TypeDef: {
+ defs.types.push_back(ParseTypeDef());
+ continue;
+ }
+ default:
+ return defs;
+ }
+ }
+ }
+
+ TypeData ParseTypeDef() {
+ // Match the `type` keyword.
+ Match(TokenType::TypeDef);
+ // Parse the type's identifier.
+ auto type_id = Match(TokenType::Identifier).ToString();
+ auto type_global = tvm::GlobalTypeVar(type_id, TypeKind::kTypeData);
+ type_names.Add(type_id, type_global);
+
+ Array<TypeVar> generics;
+
+ bool should_pop = false;
+ if (Peek()->token_type == TokenType::LSquare) {
+ // If we have generics we need to add a type scope.
+ PushTypeScope();
+ should_pop = true;
+ generics = ParseSequence<TypeVar>(TokenType::LSquare, TokenType::Comma,
TokenType::RSquare, [&]() {
+ auto type_var_name = Match(TokenType::Identifier).ToString();
+ return BindTypeVar(type_var_name, TypeKind::kType);
+ });
+ }
+
+ // Parse the list of constructors.
+ auto ctors = ParseSequence<tvm::Constructor>(TokenType::LCurly,
TokenType::Comma, TokenType::RCurly, [&]() {
+ // First match the name of the constructor.
+ auto ctor = Match(TokenType::Identifier).ToString();
+ // Match the optional field list.
+ if (Peek()->token_type != TokenType::OpenParen) {
+ return tvm::Constructor(ctor, {}, type_global);
+ } else {
+ auto arg_types = ParseSequence<Type>(TokenType::OpenParen,
TokenType::Comma, TokenType::CloseParen, [&]() {
+ return ParseType();
+ });
+ return tvm::Constructor(ctor, arg_types, type_global);
+ }
+ });
+
+ // Now pop the type scope.
+ if (should_pop) {
+ PopTypeScopes(1);
+ }
+
+ return TypeData(type_global, generics, ctors);
+ }
+
+ template <typename R>
+ R Bracket(TokenType open, TokenType close, std::function<R()> parser) {
+ Match(open);
+ R result = parser();
+ Match(close);
+ return result;
+ }
+
+ template <typename R>
+ R Parens(std::function<R()> parser) {
+ return Bracket(TokenType::OpenParen, TokenType::CloseParen, parser);
+ }
+
+ template <typename R>
+ R Block(std::function<R()> parser) {
Review comment:
const & for std::function
##########
File path: src/parser/parser.cc
##########
@@ -0,0 +1,1103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+
+/*!
+ * \file parser.cc
+ * \brief A parser for TVM IR.
+ */
+#include <tvm/ir/module.h>
+#include <tvm/relay/expr.h>
+#include <tvm/relay/function.h>
+#include <tvm/runtime/object.h>
+#include <tvm/runtime/registry.h>
+#include <tvm/node/reflection.h>
+
+#include <fstream>
+
+#include "./diagnostic.h"
+#include "./op_table.h"
+#include "./tokenizer.h"
+
+namespace tvm {
+namespace parser {
+
+using namespace relay;
+using Expr = relay::Expr;
+
+// adtConsDefnList: adtConsDefn (',' adtConsDefn)* ','? ;
+// adtConsDefn: constructorName ('(' typeExpr (',' typeExpr)* ')')? ;
+// matchClauseList: matchClause (',' matchClause)* ','? ;
+// matchClause: pattern '=>' ('{' expr '}' | expr) ;
+// // complete or incomplete match, respectively
+// matchType : 'match' | 'match?' ;
+
+// patternList: '(' pattern (',' pattern)* ')';
+// pattern
+// : '_' # wildcardPattern
+// | localVar (':' typeExpr)? # varPattern
+// | constructorName patternList? # constructorPattern
+// | patternList # tuplePattern
+// ;
+
+struct GlobalFunc {
+ GlobalVar global;
+ Function function;
+ GlobalFunc() : global(), function() {}
+ GlobalFunc(GlobalVar global, Function function) : global(global),
function(function) {}
+ GlobalFunc(const GlobalFunc& gfunc) {
+ this->global = gfunc.global;
+ this->function = gfunc.function;
+ }
+};
+
+struct Definitions {
+ std::vector<GlobalFunc> funcs;
+ std::vector<TypeData> types;
+};
+
+struct SemVer {
+ int major;
+ int minor;
+ int patch;
+};
+
+class MetaRefExpr;
+class MetaRefExprNode : public TempExprNode {
+ public:
+ std::string type_key;
+ uint64_t node_index;
+
+ void VisitAttrs(tvm::AttrVisitor* v) {}
+
+ Expr Realize() const final { return Expr(); }
+
+ static constexpr const char* _type_key = "relay.MetaRefExpr";
+ TVM_DECLARE_FINAL_OBJECT_INFO(MetaRefExprNode, TempExprNode);
+};
+
+class MetaRefExpr : public TempExpr {
+ public:
+ /*!
+ * \brief The constructor
+ * \param expr The original relay expression.
+ * \param kind The annotation kind.
+ */
+ TVM_DLL MetaRefExpr(std::string type_key, uint64_t node_index);
+
+ TVM_DEFINE_OBJECT_REF_METHODS(MetaRefExpr, TempExpr, MetaRefExprNode);
+};
+
+MetaRefExpr::MetaRefExpr(std::string type_key, uint64_t node_index) {
+ auto rnode = make_object<MetaRefExprNode>();
+ rnode->type_key = type_key;
+ rnode->node_index = node_index;
+ data_ = std::move(rnode);
+}
+
+template<typename T>
+struct Scope {
+ std::unordered_map<std::string, T> name_map;
+ Scope() : name_map() {}
+};
+
+template<typename T>
+struct ScopeStack {
+ std::vector<Scope<T>> scope_stack;
+
+ void Add(const std::string& name, const T& value) {
+ if (!this->scope_stack.size()) {
+ LOG(FATAL) << "internal issue";
+ }
+ this->scope_stack.back().name_map.insert({ name, value });
+ }
+
+ T Lookup(const std::string& name) {
+ for (auto scope = this->scope_stack.rbegin(); scope !=
this->scope_stack.rend(); scope++) {
+ auto it = scope->name_map.find(name);
+ if (it != scope->name_map.end()) {
+ return it->second;
+ }
+ }
+ return T();
+ }
+
+ void PushStack() {
+ this->scope_stack.push_back(Scope<T>());
+ }
+
+ void PopStack() {
+ this->scope_stack.pop_back();
+ }
+};
+
+template<typename T>
+struct InternTable {
+ std::unordered_map<std::string, T> table;
+ void Add(const std::string& name, const T& t) {
+ auto it = table.find(name);
+ if (it != table.end()) {
+ LOG(FATAL) << "duplicate name";
+ } else {
+ table.insert({ name, t});
+ }
+ }
+
+ T Get(const std::string& name) {
+ auto it = table.find(name);
+ if (it != table.end()) {
+ return it->second;
+ } else {
+ return T();
+ }
+ }
+};
+
+struct Parser {
+ /*! \brief The diagnostic context used for error reporting. */
+ DiagnosticContext diag_ctx;
+
+ /*! \brief The current position in the token stream. */
+ int pos;
+
+ /*! \brief The token stream for the parser. */
+ std::vector<Token> tokens;
+
+ /*! \brief The configured operator table. */
+ OperatorTable op_table;
+
+ /*! \brief Configure the whitespace mode, right now we ignore all
whitespace. */
+ bool ignore_whitespace;
+
+ /*! \brief A global mapping for GlobalVar. */
+ InternTable<GlobalVar> global_names;
+
+ /*! \brief A global mapping for type definitions. */
+ InternTable<GlobalTypeVar> type_names;
+
+ /*! \brief A mapping from graph variable to expression, i.e., `%0 = expr`. */
+ std::unordered_map<int, Expr> graph_ctx;
+
+ /*! \brief The set of type scopes used for generics. */
+ ScopeStack<TypeVar> type_scopes;
+
+ /*! \brief The set of expression scopes used for lexical scope. */
+ ScopeStack<Var> expr_scopes;
+
+ Parser(std::vector<Token> tokens, OperatorTable op_table, Source source)
+ : diag_ctx(source), pos(0), tokens(tokens), op_table(op_table),
ignore_whitespace(true) {}
+
+ void DisplayNextN(int n) {
+ std::cout << "remaining tokens: " << std::endl;
+ auto bound = std::min(pos + n, (int)tokens.size());
+ for (int i = 0; i < bound - pos; i++) {
+ std::cout << tokens[pos + i] << std::endl;
+ }
+ }
+
+ Token Peek() {
+ // For now we ignore all whitespace tokens and comments.
+ // We can tweak this behavior later to enable white space sensitivity in
the parser.
+ while (pos < tokens.size() &&
+ ignore_whitespace && (tokens.at(pos)->token_type ==
TokenType::Whitespace ||
+ tokens.at(pos)->token_type ==
TokenType::Newline ||
+ tokens.at(pos)->token_type ==
TokenType::LineComment ||
+ tokens.at(pos)->token_type ==
TokenType::Comment)) {
+ // std::cout << "pos: " << pos << std::endl;
+ // std::cout << "tokens: " << tokens.size() << std::endl;
+ pos++;
+ }
+
+ if (pos < tokens.size()) {
+ return Token(this->tokens.at(pos));
+ } else {
+ return Token::Null();
+ }
+ }
+
+ // Allow lookahead into the token stream.
+ Token Lookahead(int n) {
+ CHECK_LE(1, n)
+ << "lookahead by > 1 is invalid";
+
+ auto old_pos = pos;
+ for (int i = 0; i < n - 1; i++) {
+ Peek();
+ pos++;
+ }
+
+ auto tok = Peek();
+ pos = old_pos;
+ return tok;
+ }
+
+ void Consume(const TokenType& token) {
+ if (tokens[pos]->token_type != token) {
+ std::string message = "expected a " + Pretty(token) + " found " +
Pretty(Peek()->token_type);
+ this->diag_ctx.Emit({tokens[pos]->line, tokens[pos]->column, message});
+ this->diag_ctx.Render();
+ }
+ pos++;
+ }
+
+ Token Match(const TokenType& token_type) {
+ auto tok = Peek();
+ Consume(token_type);
+ return tok;
+ }
+
+ bool WhenMatch(const TokenType& token_type) {
+ if (Peek()->token_type == token_type) {
+ Consume(token_type);
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ void AddGraphBinding(const Token& token, const Expr& expr) {
+ auto graph_no = token.ToNumber();
+ this->graph_ctx.insert({graph_no, expr});
+ }
+
+ Expr LookupGraphBinding(const Token& token) {
+ auto graph_no = token.ToNumber();
+ return this->graph_ctx.at(graph_no);
+ }
+
+ Var BindVar(const std::string& name, const relay::Type& type_annotation) {
+ auto var = Var(name, type_annotation);
+ this->expr_scopes.Add(name, var);
+ return var;
+ }
+
+ TypeVar BindTypeVar(const std::string& name, const TypeKind type_kind) {
+ auto type_var = TypeVar(name, type_kind);
+ this->type_scopes.Add(name, type_var);
+ return type_var;
+ }
+
+ Var LookupLocal(const Token& local) {
+ auto var = this->expr_scopes.Lookup(local.ToString());
+ if (!var.defined()) {
+ diag_ctx.Emit({ local->line, local->column, "this local variable has not
been previously declared"});
+ }
+ return var;
+ }
+
+ TypeVar LookupTypeVar(const Token& ident) {
+ auto var = this->type_scopes.Lookup(ident.ToString());
+ if (!var.defined()) {
+ diag_ctx.Emit({ ident->line, ident->column, "this type variable has not
been previously declared anywhere, perhaps a typo?"});
+ }
+ return var;
+ }
+
+ void PushScope() {
+ this->expr_scopes.PushStack();
+ }
+
+ void PopScopes(int n) {
+ for (int i = 0; i < n; i++) {
+ this->expr_scopes.PopStack();
+ }
+ }
+
+ void PushTypeScope() {
+ this->type_scopes.PushStack();
+ }
+
+ void PopTypeScopes(int n) {
+ for (int i = 0; i < n; i++) {
+ this->type_scopes.PopStack();
+ }
+ }
+
+ NDArray NumberToNDArray(const Token& token) {
+ if (token->token_type == TokenType::Integer) {
+ DLContext ctx({.device_type = DLDeviceType::kDLCPU, .device_id = 0});
+ auto dtype = String2DLDataType("int32");
+ auto data = NDArray::Empty({}, dtype, ctx);
+ auto array = reinterpret_cast<int32_t*>(data->data);
+ // revisit this, literal node issue.
+ int64_t value = Downcast<tvm::Integer>(token->data);
+ array[0] = (int32_t)value;
+ return data;
+ } else if (token->token_type == TokenType::Float) {
+ DLContext ctx({.device_type = DLDeviceType::kDLCPU, .device_id = 0});
+ auto dtype = String2DLDataType("float32");
+ auto data = NDArray::Empty({}, dtype, ctx);
+ auto array = reinterpret_cast<float*>(data->data);
+ // revisit this, literal node issue.
+ float value = Downcast<tvm::FloatImm>(token->data)->value;
+ array[0] = value;
+ return data;
+ } else {
+ throw "foo";
+ }
+ }
+
+ NDArray BooleanToNDarray(bool value) {
+ DLContext ctx({.device_type = DLDeviceType::kDLCPU, .device_id = 0});
+ auto dtype = String2DLDataType("bool");
+ auto data = NDArray::Empty({}, dtype, ctx);
+ auto array = reinterpret_cast<bool*>(data->data);
+ array[0] = value;
+ return data;
+ }
+
+ [[noreturn]] void ParseError(const Token& token, const std::string& msg) {
+ throw std::runtime_error(msg);
+ }
+
+ IRModule ParseModule() {
+ // Parse the semver header at the top of the module.
+ auto _version = ParseSemVer();
+ // Parse the definitions.
+ auto defs = ParseDefinitions();
+ // Parse the metadata section at the end.
+ auto metadata = ParseMetadata();
+ Match(TokenType::EndOfFile);
+ Map<tvm::GlobalVar, BaseFunc> funcs;
+ Map<tvm::GlobalTypeVar, TypeData> types;
+
+ for (auto func : defs.funcs) {
+ funcs.Set(func.global, func.function);
+ }
+
+ for (auto type_def : defs.types) {
+ types.Set(type_def->header, type_def);
+ }
+
+ return IRModule(funcs, types);
+ }
+
+ SemVer ParseSemVer() {
+ // Consume(TokenType::Unknown);
+ return SemVer{.major = 0, .minor = 0, .patch = 0};
+ }
+
+ Definitions ParseDefinitions() {
+ Definitions defs;
+
+ while (true) {
+ auto next = Peek();
+ switch (next->token_type) {
+ case TokenType::Defn: {
+ Consume(TokenType::Defn);
+ auto global_name = Match(TokenType::Global).ToString();
+ auto global = GlobalVar(global_name);
+ global_names.Add(global_name, global);
+ auto func = ParseFunctionDef();
+ defs.funcs.push_back(GlobalFunc(global, func));
+ continue;
+ }
+ case TokenType::TypeDef: {
+ defs.types.push_back(ParseTypeDef());
+ continue;
+ }
+ default:
+ return defs;
+ }
+ }
+ }
+
+ TypeData ParseTypeDef() {
+ // Match the `type` keyword.
+ Match(TokenType::TypeDef);
+ // Parse the type's identifier.
+ auto type_id = Match(TokenType::Identifier).ToString();
+ auto type_global = tvm::GlobalTypeVar(type_id, TypeKind::kTypeData);
+ type_names.Add(type_id, type_global);
+
+ Array<TypeVar> generics;
+
+ bool should_pop = false;
+ if (Peek()->token_type == TokenType::LSquare) {
+ // If we have generics we need to add a type scope.
+ PushTypeScope();
+ should_pop = true;
+ generics = ParseSequence<TypeVar>(TokenType::LSquare, TokenType::Comma,
TokenType::RSquare, [&]() {
+ auto type_var_name = Match(TokenType::Identifier).ToString();
+ return BindTypeVar(type_var_name, TypeKind::kType);
+ });
+ }
+
+ // Parse the list of constructors.
+ auto ctors = ParseSequence<tvm::Constructor>(TokenType::LCurly,
TokenType::Comma, TokenType::RCurly, [&]() {
+ // First match the name of the constructor.
+ auto ctor = Match(TokenType::Identifier).ToString();
+ // Match the optional field list.
+ if (Peek()->token_type != TokenType::OpenParen) {
+ return tvm::Constructor(ctor, {}, type_global);
+ } else {
+ auto arg_types = ParseSequence<Type>(TokenType::OpenParen,
TokenType::Comma, TokenType::CloseParen, [&]() {
+ return ParseType();
+ });
+ return tvm::Constructor(ctor, arg_types, type_global);
+ }
+ });
+
+ // Now pop the type scope.
+ if (should_pop) {
+ PopTypeScopes(1);
+ }
+
+ return TypeData(type_global, generics, ctors);
+ }
+
+ template <typename R>
+ R Bracket(TokenType open, TokenType close, std::function<R()> parser) {
+ Match(open);
+ R result = parser();
+ Match(close);
+ return result;
+ }
+
+ template <typename R>
+ R Parens(std::function<R()> parser) {
+ return Bracket(TokenType::OpenParen, TokenType::CloseParen, parser);
+ }
+
+ template <typename R>
+ R Block(std::function<R()> parser) {
+ return Bracket(TokenType::LCurly, TokenType::RCurly, parser);
+ }
+
+ Expr ParseBindingExpr() {
+ // We use a loop here so that the stack depth
+ // does not grow linearly with a sequence of
+ // graph or let bindings.
+ //
+ // Assuming we start at call depth k, we will
+ // enter k + c call frames to parse the RHS
+ // of the bindings where `c` is the depth
+ // of recursion needed by RHS.
+ //
+ // If RHS is a call expresssion the c=1.
+ //
+ // Once we have parsed the RHS we will be
+ // back at depth K, and will return to
+ // this loop header to parse another
+ // graph or let binding.
+ //
+ // This ensures for n sequential bindings
+ // the call depth will be the same before
+ // and after parsing the n bindings.
+ std::vector<std::pair<Var, Expr>> bindings;
+ int scopes = 0;
+
+ while (true) {
+ auto next = Peek();
+ if (next->token_type == TokenType::Graph && Lookahead(2)->token_type ==
TokenType::Equal) {
+ Match(TokenType::Graph);
+ Match(TokenType::Equal);
+ auto val = this->ParseExprBinOp();
+ Match(TokenType::Semicolon);
+ AddGraphBinding(next, val);
+ } else if (next->token_type == TokenType::Let) {
+ // Parse the 'let'.
+ Consume(TokenType::Let);
+
+ // Parse the local '%<id>'.
+ auto local_tok = Match(TokenType::Local);
+ auto string = local_tok.ToString();
+
+ // Parse the optional type annotation (':' <type>).
+ Type type;
+ if (WhenMatch(TokenType::Colon)) {
+ type = ParseType();
+ }
+
+ auto var = BindVar(string, type);
+
+ // Parse the '=';
+ Match(TokenType::Equal);
+
+ // Parse the body, and the ';'.
+ auto val = this->ParseExprBinOp();
+ Consume(TokenType::Semicolon);
+
+ // Add the bindings to the local data structure.
+ bindings.push_back({ var, val });
+ scopes++;
+ PushScope();
+ } else {
+ // This is the only case we will increase the stack
+ // depth.
+ //
+ // If we parse a program which is a sequence of N bindings
+ // followed by a single body expression we will end up with
+ // a call depth of 3, the first call to ParseExpr, then
+ // ParseBindingExpr, then finally ParseExpr once more.
+
+ auto body = this->ParseExpr();
+
+ // Remove the same number of scopes we added.
+ PopScopes(scopes);
+
+ if (bindings.size() == 0) {
+ return body;
+ } else {
+ // We can now build the let binding up backwards.
+ for (auto binding = bindings.rbegin(); binding != bindings.rend();
binding++) {
+ body = relay::Let(binding->first, binding->second, body);
+ }
+ return body;
+ }
+ }
+ }
+ }
+
+ std::string HackTokensAsString(int n) {
+ std::stringstream key;
+ n = std::min((int)(tokens.size() - pos), n);
+ for (int i = 0; i < n; i++) {
+ key << ToString(tokens.at(pos + i)->token_type);
+ }
+ return key.str();
+ }
+
+ std::vector<Rule> ParseOp() {
+ std::vector<Rule> matched;
+ Peek();
+ for (int i = 4; i > 0; i--) {
+ auto key = HackTokensAsString(i);
+ auto it = this->op_table.this_is_a_hack.find(key);
+ if (it != this->op_table.this_is_a_hack.end()) {
+ pos = pos + i;
+ matched.push_back(it->second);
+ }
+ }
+
+ return matched;
+ }
+
+ void DebugStack(const std::vector<Expr>& exprs, const std::vector<Rule>&
rules) {
+ std::cout << "Expr Stack: ";
+ for (auto expr : exprs) {
+ std::cout << expr << ", ";
+ }
+
+ std::cout << std::endl;
+ std::cout << "Op Stack: ";
+ for (auto rule : rules) {
+ std::cout << rule.op << ", ";
+ }
+
+ std::cout << std::endl;
+ }
+
+
+ // Provides parsing a sequence of the form: <star> (T <sep>)*
<tokens_for_before_stop> <stop>.
+ // the intended use case of the before stop parser to is allow a customized
parsing rule for things
+ // such as attributes.
+ template<typename T>
+ Array<T> ParseSequence(TokenType start, TokenType sep, TokenType stop,
std::function<T()> parse, std::function<void()> before_stop = nullptr) {
+ Match(start);
+ if (WhenMatch(stop)) {
+ return Array<T>();
+ } else {
+ auto data = parse();
+ Array<T> elements = { data };
+
+ // parse '(' expr ')'
+ // if we are at the end invoke leftover parser
+ if (Peek()->token_type == stop && before_stop) { before_stop(); }
+ if (WhenMatch(stop)) {
+ return elements;
+ // parse '( expr ',' * ')'
+ } else if (WhenMatch(sep)) {
+ // if we are at the end invoke leftover parser
+ if (Peek()->token_type == stop && before_stop) { before_stop(); }
+ while (true) {
+ if (WhenMatch(stop)) {
+ break;
+ } else {
+ auto data = parse();
+ WhenMatch(sep);
+ elements.push_back(data);
+ }
+ }
+ return elements;
+ } else {
+ LOG(FATAL) << "issue";
+ return Array<T>(nullptr);
+ }
+ }
+ }
+
+ Array<tvm::PrimExpr> ParseShape() {
+ auto dims = ParseSequence<tvm::PrimExpr>(TokenType::OpenParen,
TokenType::Comma, TokenType::CloseParen, [&]() {
+ auto tok = Match(TokenType::Integer);
+ return Downcast<tvm::PrimExpr>(tok->data);
+ });
+ return dims;
+ }
+
+ Type ParseFunctionType() {
+ auto ty_params = ParseSequence<Type>(
+ TokenType::OpenParen,
+ TokenType::Comma,
+ TokenType::CloseParen, [&]() {
+ return ParseType();
+ });
+
+ Match(TokenType::Minus);
+ Match(TokenType::RAngle);
+ auto ret_type = ParseType();
+
+ return relay::FuncType(ty_params, ret_type, {}, {});
+ }
+
+ // Parses a user defined ADT or type variable.
+ Type ParseNonPrimitiveType(const Token& tok) {
+ std::cout << "inside of prim type " << tok << std::endl;
+ auto name = tok.ToString();
+ Type head_type;
+ auto global_type = type_names.Get(name);
+
+ if (!global_type.defined()) {
+ head_type = LookupTypeVar(tok);
+ } else {
+ head_type = global_type;
+ }
+
+ CHECK(head_type.defined())
+ << "head type must be defined";
+
+ Array<Type> arg_types;
+ if (Peek()->token_type == TokenType::LSquare) {
+ arg_types = ParseSequence<Type>(TokenType::LSquare, TokenType::Comma,
TokenType::RSquare, [&]() {
+ return ParseType();
+ });
+ }
+
+ if (arg_types.size()) {
+ return TypeCall(head_type, arg_types);
+ } else {
+ return head_type;
+ }
+ }
+
+ Type ParseType() {
+ auto tok = Peek();
+
+ if (tok->token_type == TokenType::OpenParen) {
+ auto tys = ParseSequence<relay::Type>(
+ TokenType::OpenParen,
+ TokenType::Comma,
+ TokenType::CloseParen, [&]() {
+ return ParseType();
+ });
+ return relay::TupleType(tys);
+ } else if (WhenMatch(TokenType::Fn)) {
+ return ParseFunctionType();
+ } else if (WhenMatch(TokenType::Identifier)) {
+ auto id = tok.ToString();
+ if (id == "Tensor") {
+ Match(TokenType::LSquare);
+ auto shape = ParseShape();
+ Match(TokenType::Comma);
+ auto dtype_tok = Match(TokenType::Identifier);
+ auto dtype = DataType(String2DLDataType(dtype_tok.ToString()));
+ Match(TokenType::RSquare);
+ return TensorType(shape, dtype);
+ } else {
+ auto ty = tok.ToString();
+ if (ty.rfind("int", 0) == 0 || ty.find("float", 0) == 0 ||
ty.find("uint", 0) == 0 || ty.find("bool", 0) == 0) {
+ // Need to do better error handling here.
+ auto dtype = DataType(String2DLDataType(tok.ToString()));
+ return TensorType({}, dtype);
+ } else {
+ return ParseNonPrimitiveType(tok);
+ }
+ }
+ } if (WhenMatch(TokenType::Underscore)) {
+ return IncompleteType();
+ } else {
+ std::stringstream msg;
+ msg << "failed to parse type found ";
+ msg << tok;
+ diag_ctx.Emit({ tok->line, tok->column, msg.str() });
+ diag_ctx.Render();
+ return Type();
+ }
+ }
+
+ Attrs ParseAttrs(const std::string& type_key) {
+ Map<String, ObjectRef> kwargs;
+ auto attrs = tvm::ReflectionVTable::Global()->CreateObject(type_key,
kwargs);
+ LOG(FATAL) << Attrs();
+ return Attrs();
+ }
+
+ Function ParseFunctionDef() {
+ PushScope();
+ PushTypeScope();
+
+ Array<TypeVar> generics;
+ if (Peek()->token_type == TokenType::LSquare) {
+ // If we have generics we need to add a type scope.
+ PushTypeScope();
+ generics = ParseSequence<TypeVar>(TokenType::LSquare, TokenType::Comma,
TokenType::RSquare, [&]() {
+ auto type_var_name = Match(TokenType::Identifier).ToString();
+ return BindTypeVar(type_var_name, TypeKind::kType);
+ });
+ }
+
+ auto params = ParseSequence<Var>(TokenType::OpenParen, TokenType::Comma,
TokenType::CloseParen, [&]() {
+ auto token = Match(TokenType::Local);
+ auto string = token.ToString();
+ Type type;
+ if (WhenMatch(TokenType::Colon)) {
+ type = ParseType();
+ }
+ return BindVar(string, type);
+ });
+
+ Type ret_type;
+ if (WhenMatch(TokenType::Minus)) {
+ Match(TokenType::RAngle);
+ ret_type = ParseType();
+ }
+
+ auto body = Block<Expr>([&]() {
+ return ParseExpr();
+ });
+
+ PopTypeScopes(1);
+ PopScopes(1);
+
+ return relay::Function(params, body, ret_type, generics);
+ }
+
+ Expr ParseIf() {
+ Consume(TokenType::If);
+ auto guard = Parens<Expr>([&] {
+ return ParseExpr();
+ });
+
+ auto true_branch = Block<Expr>([&] {
+ return ParseExpr();
+ });
+
+ Match(TokenType::Else);
+
+ auto false_branch = Block<Expr>([&] {
+ return ParseExpr();
+ });
+
+ return relay::If(guard, true_branch, false_branch);
+ }
+
+ Expr ParseMatch(bool is_partial) {
+ LOG(FATAL) << "parse match";
+ }
+
+ Expr ParseExpr() {
+ return ConsumeWhitespace<Expr>([this] {
+ std::vector<Expr> exprs;
+
+ while (true) {
+ auto next = Peek();
+ switch (next->token_type) {
+ // For graph or let, match first rhs, then invoke ParseBindingExpr
+ // ParseBindingExpression then parse_lhs() parse_rhs() ';' continue
+ case TokenType::Let:
+ exprs.push_back(ParseBindingExpr());
+ break;
+ case TokenType::Match:
+ case TokenType::PartialMatch:
+ bool is_partial = next->token_type == PartialMatch;
+ Consume(nest->token_type);
+ exprs.push_back(ParseMatch(is_partial));
+ break;
+ case TokenType::If: {
+ exprs.push_back(ParseIf());
+ break;
+ }
+ case TokenType::Graph:
+ if (Lookahead(2)->token_type == TokenType::Equal) {
+ exprs.push_back(ParseBindingExpr());
+ break;
+ }
+ // intentional fall through here.
+ default: {
+ DisplayNextN(100);
Review comment:
name the magic number
##########
File path: tests/python/relay/test_ir_parser2.py
##########
@@ -0,0 +1,920 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you 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.
+import tvm
+from tvm import te
+from tvm import relay
+import pytest
+from numpy import isclose
+from typing import Union
+from functools import wraps
+raises_parse_error = pytest.mark.xfail(raises=tvm._ffi.base.TVMError)
+
+SEMVER = "v0.0.4"
+
+BINARY_OPS = {
+ "*": relay.multiply,
+ "/": relay.divide,
+ "+": relay.add,
+ "-": relay.subtract,
+ "<": relay.less,
+ ">": relay.greater,
+ "<=": relay.less_equal,
+ ">=": relay.greater_equal,
+ "==": relay.equal,
+ "!=": relay.not_equal,
+}
+
+TYPES = {
+ "int8",
+ "int16",
+ "int32",
+ "int64",
+
+ "uint8",
+ "uint16",
+ "uint32",
+ "uint64",
+
+ "float16",
+ "float32",
+ "float64",
+
+ "bool",
+
+ "int8x4",
+ "uint1x4",
+ "float16x4",
+}
+
+LIST_DEFN = """
+type List[A] {
+ Cons(A, List[A]),
+ Nil,
+}
+"""
+
+def assert_graph_equal(lhs, rhs):
+ tvm.ir.assert_structural_equal(lhs, rhs, map_free_vars=True)
+
+def graph_equal(lhs, rhs):
+ return tvm.ir.structural_equal(lhs, rhs, map_free_vars=True)
+
+
+def roundtrip(expr):
+ x = relay.fromtext(expr.astext())
+ assert_graph_equal(x, expr)
+
+
+def parse_text(code):
+ expr = tvm.parser.parse_expr(code)
+ roundtrip(expr)
+ return expr
+
+
+def parses_as(code, expr):
+ # type: (str, relay.Expr) -> bool
+ parsed = parse_text(code)
+ result = graph_equal(parsed, expr)
+ return result
+
+def parse_module(code):
+ mod = tvm.parser.parse(code)
+ roundtrip(mod)
+ return mod
+
+
+def assert_parses_as(code, expr):
+ parsed = parse_text(code)
+ assert_graph_equal(parsed, expr)
+
+def assert_parse_module_as(code, mod):
+ import pdb; pdb.set_trace()
Review comment:
remove other pdb
##########
File path: src/parser/parser.cc
##########
@@ -0,0 +1,1103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+
+/*!
+ * \file parser.cc
+ * \brief A parser for TVM IR.
+ */
+#include <tvm/ir/module.h>
+#include <tvm/relay/expr.h>
+#include <tvm/relay/function.h>
+#include <tvm/runtime/object.h>
+#include <tvm/runtime/registry.h>
+#include <tvm/node/reflection.h>
+
+#include <fstream>
+
+#include "./diagnostic.h"
+#include "./op_table.h"
+#include "./tokenizer.h"
+
+namespace tvm {
+namespace parser {
+
+using namespace relay;
+using Expr = relay::Expr;
+
+// adtConsDefnList: adtConsDefn (',' adtConsDefn)* ','? ;
+// adtConsDefn: constructorName ('(' typeExpr (',' typeExpr)* ')')? ;
+// matchClauseList: matchClause (',' matchClause)* ','? ;
+// matchClause: pattern '=>' ('{' expr '}' | expr) ;
+// // complete or incomplete match, respectively
+// matchType : 'match' | 'match?' ;
+
+// patternList: '(' pattern (',' pattern)* ')';
+// pattern
+// : '_' # wildcardPattern
+// | localVar (':' typeExpr)? # varPattern
+// | constructorName patternList? # constructorPattern
+// | patternList # tuplePattern
+// ;
+
+struct GlobalFunc {
+ GlobalVar global;
+ Function function;
+ GlobalFunc() : global(), function() {}
+ GlobalFunc(GlobalVar global, Function function) : global(global),
function(function) {}
+ GlobalFunc(const GlobalFunc& gfunc) {
+ this->global = gfunc.global;
+ this->function = gfunc.function;
+ }
+};
+
+struct Definitions {
+ std::vector<GlobalFunc> funcs;
+ std::vector<TypeData> types;
+};
+
+struct SemVer {
+ int major;
+ int minor;
+ int patch;
+};
+
+class MetaRefExpr;
+class MetaRefExprNode : public TempExprNode {
+ public:
+ std::string type_key;
+ uint64_t node_index;
+
+ void VisitAttrs(tvm::AttrVisitor* v) {}
+
+ Expr Realize() const final { return Expr(); }
+
+ static constexpr const char* _type_key = "relay.MetaRefExpr";
+ TVM_DECLARE_FINAL_OBJECT_INFO(MetaRefExprNode, TempExprNode);
+};
+
+class MetaRefExpr : public TempExpr {
+ public:
+ /*!
+ * \brief The constructor
+ * \param expr The original relay expression.
+ * \param kind The annotation kind.
+ */
+ TVM_DLL MetaRefExpr(std::string type_key, uint64_t node_index);
+
+ TVM_DEFINE_OBJECT_REF_METHODS(MetaRefExpr, TempExpr, MetaRefExprNode);
+};
+
+MetaRefExpr::MetaRefExpr(std::string type_key, uint64_t node_index) {
+ auto rnode = make_object<MetaRefExprNode>();
+ rnode->type_key = type_key;
+ rnode->node_index = node_index;
+ data_ = std::move(rnode);
+}
+
+template<typename T>
+struct Scope {
+ std::unordered_map<std::string, T> name_map;
+ Scope() : name_map() {}
+};
+
+template<typename T>
+struct ScopeStack {
+ std::vector<Scope<T>> scope_stack;
+
+ void Add(const std::string& name, const T& value) {
+ if (!this->scope_stack.size()) {
+ LOG(FATAL) << "internal issue";
+ }
+ this->scope_stack.back().name_map.insert({ name, value });
+ }
+
+ T Lookup(const std::string& name) {
+ for (auto scope = this->scope_stack.rbegin(); scope !=
this->scope_stack.rend(); scope++) {
+ auto it = scope->name_map.find(name);
+ if (it != scope->name_map.end()) {
+ return it->second;
+ }
+ }
+ return T();
+ }
+
+ void PushStack() {
+ this->scope_stack.push_back(Scope<T>());
+ }
+
+ void PopStack() {
+ this->scope_stack.pop_back();
+ }
+};
+
+template<typename T>
+struct InternTable {
+ std::unordered_map<std::string, T> table;
+ void Add(const std::string& name, const T& t) {
+ auto it = table.find(name);
+ if (it != table.end()) {
+ LOG(FATAL) << "duplicate name";
+ } else {
+ table.insert({ name, t});
+ }
+ }
+
+ T Get(const std::string& name) {
+ auto it = table.find(name);
+ if (it != table.end()) {
+ return it->second;
+ } else {
+ return T();
+ }
+ }
+};
+
+struct Parser {
+ /*! \brief The diagnostic context used for error reporting. */
+ DiagnosticContext diag_ctx;
+
+ /*! \brief The current position in the token stream. */
+ int pos;
+
+ /*! \brief The token stream for the parser. */
+ std::vector<Token> tokens;
+
+ /*! \brief The configured operator table. */
+ OperatorTable op_table;
+
+ /*! \brief Configure the whitespace mode, right now we ignore all
whitespace. */
+ bool ignore_whitespace;
+
+ /*! \brief A global mapping for GlobalVar. */
+ InternTable<GlobalVar> global_names;
+
+ /*! \brief A global mapping for type definitions. */
+ InternTable<GlobalTypeVar> type_names;
+
+ /*! \brief A mapping from graph variable to expression, i.e., `%0 = expr`. */
+ std::unordered_map<int, Expr> graph_ctx;
+
+ /*! \brief The set of type scopes used for generics. */
+ ScopeStack<TypeVar> type_scopes;
+
+ /*! \brief The set of expression scopes used for lexical scope. */
+ ScopeStack<Var> expr_scopes;
+
+ Parser(std::vector<Token> tokens, OperatorTable op_table, Source source)
+ : diag_ctx(source), pos(0), tokens(tokens), op_table(op_table),
ignore_whitespace(true) {}
+
+ void DisplayNextN(int n) {
+ std::cout << "remaining tokens: " << std::endl;
+ auto bound = std::min(pos + n, (int)tokens.size());
+ for (int i = 0; i < bound - pos; i++) {
+ std::cout << tokens[pos + i] << std::endl;
+ }
+ }
+
+ Token Peek() {
+ // For now we ignore all whitespace tokens and comments.
+ // We can tweak this behavior later to enable white space sensitivity in
the parser.
+ while (pos < tokens.size() &&
+ ignore_whitespace && (tokens.at(pos)->token_type ==
TokenType::Whitespace ||
+ tokens.at(pos)->token_type ==
TokenType::Newline ||
+ tokens.at(pos)->token_type ==
TokenType::LineComment ||
+ tokens.at(pos)->token_type ==
TokenType::Comment)) {
+ // std::cout << "pos: " << pos << std::endl;
+ // std::cout << "tokens: " << tokens.size() << std::endl;
+ pos++;
+ }
+
+ if (pos < tokens.size()) {
+ return Token(this->tokens.at(pos));
+ } else {
+ return Token::Null();
+ }
+ }
+
+ // Allow lookahead into the token stream.
+ Token Lookahead(int n) {
+ CHECK_LE(1, n)
+ << "lookahead by > 1 is invalid";
+
+ auto old_pos = pos;
+ for (int i = 0; i < n - 1; i++) {
+ Peek();
+ pos++;
+ }
+
+ auto tok = Peek();
+ pos = old_pos;
+ return tok;
+ }
+
+ void Consume(const TokenType& token) {
+ if (tokens[pos]->token_type != token) {
+ std::string message = "expected a " + Pretty(token) + " found " +
Pretty(Peek()->token_type);
+ this->diag_ctx.Emit({tokens[pos]->line, tokens[pos]->column, message});
+ this->diag_ctx.Render();
+ }
+ pos++;
+ }
+
+ Token Match(const TokenType& token_type) {
+ auto tok = Peek();
+ Consume(token_type);
+ return tok;
+ }
+
+ bool WhenMatch(const TokenType& token_type) {
+ if (Peek()->token_type == token_type) {
+ Consume(token_type);
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ void AddGraphBinding(const Token& token, const Expr& expr) {
+ auto graph_no = token.ToNumber();
+ this->graph_ctx.insert({graph_no, expr});
+ }
+
+ Expr LookupGraphBinding(const Token& token) {
+ auto graph_no = token.ToNumber();
+ return this->graph_ctx.at(graph_no);
+ }
+
+ Var BindVar(const std::string& name, const relay::Type& type_annotation) {
+ auto var = Var(name, type_annotation);
+ this->expr_scopes.Add(name, var);
+ return var;
+ }
+
+ TypeVar BindTypeVar(const std::string& name, const TypeKind type_kind) {
+ auto type_var = TypeVar(name, type_kind);
+ this->type_scopes.Add(name, type_var);
+ return type_var;
+ }
+
+ Var LookupLocal(const Token& local) {
+ auto var = this->expr_scopes.Lookup(local.ToString());
+ if (!var.defined()) {
+ diag_ctx.Emit({ local->line, local->column, "this local variable has not
been previously declared"});
+ }
+ return var;
+ }
+
+ TypeVar LookupTypeVar(const Token& ident) {
+ auto var = this->type_scopes.Lookup(ident.ToString());
+ if (!var.defined()) {
+ diag_ctx.Emit({ ident->line, ident->column, "this type variable has not
been previously declared anywhere, perhaps a typo?"});
+ }
+ return var;
+ }
+
+ void PushScope() {
+ this->expr_scopes.PushStack();
+ }
+
+ void PopScopes(int n) {
+ for (int i = 0; i < n; i++) {
+ this->expr_scopes.PopStack();
+ }
+ }
+
+ void PushTypeScope() {
+ this->type_scopes.PushStack();
+ }
+
+ void PopTypeScopes(int n) {
+ for (int i = 0; i < n; i++) {
+ this->type_scopes.PopStack();
+ }
+ }
+
+ NDArray NumberToNDArray(const Token& token) {
+ if (token->token_type == TokenType::Integer) {
+ DLContext ctx({.device_type = DLDeviceType::kDLCPU, .device_id = 0});
+ auto dtype = String2DLDataType("int32");
+ auto data = NDArray::Empty({}, dtype, ctx);
+ auto array = reinterpret_cast<int32_t*>(data->data);
+ // revisit this, literal node issue.
+ int64_t value = Downcast<tvm::Integer>(token->data);
+ array[0] = (int32_t)value;
+ return data;
+ } else if (token->token_type == TokenType::Float) {
+ DLContext ctx({.device_type = DLDeviceType::kDLCPU, .device_id = 0});
+ auto dtype = String2DLDataType("float32");
+ auto data = NDArray::Empty({}, dtype, ctx);
+ auto array = reinterpret_cast<float*>(data->data);
+ // revisit this, literal node issue.
+ float value = Downcast<tvm::FloatImm>(token->data)->value;
+ array[0] = value;
+ return data;
+ } else {
+ throw "foo";
+ }
+ }
+
+ NDArray BooleanToNDarray(bool value) {
+ DLContext ctx({.device_type = DLDeviceType::kDLCPU, .device_id = 0});
+ auto dtype = String2DLDataType("bool");
+ auto data = NDArray::Empty({}, dtype, ctx);
+ auto array = reinterpret_cast<bool*>(data->data);
+ array[0] = value;
+ return data;
+ }
+
+ [[noreturn]] void ParseError(const Token& token, const std::string& msg) {
+ throw std::runtime_error(msg);
+ }
+
+ IRModule ParseModule() {
+ // Parse the semver header at the top of the module.
+ auto _version = ParseSemVer();
+ // Parse the definitions.
+ auto defs = ParseDefinitions();
+ // Parse the metadata section at the end.
+ auto metadata = ParseMetadata();
+ Match(TokenType::EndOfFile);
+ Map<tvm::GlobalVar, BaseFunc> funcs;
+ Map<tvm::GlobalTypeVar, TypeData> types;
+
+ for (auto func : defs.funcs) {
+ funcs.Set(func.global, func.function);
+ }
+
+ for (auto type_def : defs.types) {
+ types.Set(type_def->header, type_def);
+ }
+
+ return IRModule(funcs, types);
+ }
+
+ SemVer ParseSemVer() {
+ // Consume(TokenType::Unknown);
+ return SemVer{.major = 0, .minor = 0, .patch = 0};
+ }
+
+ Definitions ParseDefinitions() {
+ Definitions defs;
+
+ while (true) {
+ auto next = Peek();
+ switch (next->token_type) {
+ case TokenType::Defn: {
+ Consume(TokenType::Defn);
+ auto global_name = Match(TokenType::Global).ToString();
+ auto global = GlobalVar(global_name);
+ global_names.Add(global_name, global);
+ auto func = ParseFunctionDef();
+ defs.funcs.push_back(GlobalFunc(global, func));
+ continue;
+ }
+ case TokenType::TypeDef: {
+ defs.types.push_back(ParseTypeDef());
+ continue;
+ }
+ default:
+ return defs;
+ }
+ }
+ }
+
+ TypeData ParseTypeDef() {
+ // Match the `type` keyword.
+ Match(TokenType::TypeDef);
+ // Parse the type's identifier.
+ auto type_id = Match(TokenType::Identifier).ToString();
+ auto type_global = tvm::GlobalTypeVar(type_id, TypeKind::kTypeData);
+ type_names.Add(type_id, type_global);
+
+ Array<TypeVar> generics;
+
+ bool should_pop = false;
+ if (Peek()->token_type == TokenType::LSquare) {
+ // If we have generics we need to add a type scope.
+ PushTypeScope();
+ should_pop = true;
+ generics = ParseSequence<TypeVar>(TokenType::LSquare, TokenType::Comma,
TokenType::RSquare, [&]() {
+ auto type_var_name = Match(TokenType::Identifier).ToString();
+ return BindTypeVar(type_var_name, TypeKind::kType);
+ });
+ }
+
+ // Parse the list of constructors.
+ auto ctors = ParseSequence<tvm::Constructor>(TokenType::LCurly,
TokenType::Comma, TokenType::RCurly, [&]() {
+ // First match the name of the constructor.
+ auto ctor = Match(TokenType::Identifier).ToString();
+ // Match the optional field list.
+ if (Peek()->token_type != TokenType::OpenParen) {
+ return tvm::Constructor(ctor, {}, type_global);
+ } else {
+ auto arg_types = ParseSequence<Type>(TokenType::OpenParen,
TokenType::Comma, TokenType::CloseParen, [&]() {
+ return ParseType();
+ });
+ return tvm::Constructor(ctor, arg_types, type_global);
+ }
+ });
+
+ // Now pop the type scope.
+ if (should_pop) {
+ PopTypeScopes(1);
+ }
+
+ return TypeData(type_global, generics, ctors);
+ }
+
+ template <typename R>
+ R Bracket(TokenType open, TokenType close, std::function<R()> parser) {
+ Match(open);
+ R result = parser();
+ Match(close);
+ return result;
+ }
+
+ template <typename R>
+ R Parens(std::function<R()> parser) {
Review comment:
const & for std::function
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]