mbrookhart commented on a change in pull request #6162:
URL: https://github.com/apache/incubator-tvm/pull/6162#discussion_r465838414
##########
File path: python/tvm/error.py
##########
@@ -121,3 +121,7 @@ class OpAttributeUnImplemented(OpError,
NotImplementedError):
"Attribute {} is not supported in operator {}".format(
attr_name, op_name))
"""
+
+@register_error
+class DiagnosticError(TVMError):
Review comment:
Docstring instead of pass?
##########
File path: src/parser/parser.cc
##########
@@ -1144,62 +1215,95 @@ class Parser {
}
// We need a zero-arity case for constructors.
- if (expr.as<ConstructorNode>()) {
- return Expr(Call(expr, {}));
- } else {
- return expr;
+ if (auto ctor_node = expr.as<ConstructorNode>()) {
+ if (ctor_node->inputs.size() == 0) {
+ return Expr(Call(expr, {}));
+ }
}
+
+ return expr;
});
}
+ Expr GetOp(const std::string& op_name, const Token& tok) {
+ try {
+ return Op::Get(op_name);
+ } catch (dmlc::Error e) {
+ this->diag_ctx->Emit(DiagnosticBuilder(DiagnosticLevel::Error, tok->span)
+ << "operator `" << op_name
+ << "` not found, perhaps you forgot to register
it?");
+ return Expr();
+ }
+ }
+
Expr ParseAtomicExpr() {
- return ConsumeWhitespace<Expr>([this] {
+ DLOG(INFO) << "Parser::ParseAtomicExpr";
+ auto expr = ConsumeWhitespace<Expr>([this] {
auto next = Peek();
switch (next->token_type) {
case TokenType::Integer:
case TokenType::Float: {
Consume(next->token_type);
auto number = NumberToNDArray(next);
- Expr e = Constant(number);
+ Expr e = Constant(number, next->span);
return e;
}
case TokenType::Boolean: {
Consume(TokenType::Boolean);
int value = Downcast<tvm::Integer>(next->data);
auto boolean = BooleanToNDarray(value);
- Expr e = Constant(boolean);
+ Expr e = Constant(boolean, next->span);
return e;
}
+ // Parse a local of the form `%x`.
case TokenType::Local: {
Consume(TokenType::Local);
return Expr(LookupLocal(next));
}
+ // Parse a local of the form `@x`.
case TokenType::Global: {
auto string = next.ToString();
Consume(TokenType::Global);
auto global = global_names.Get(string);
if (!global) {
+ // TODO(@jroesch): fix global's needing span information
auto global_var = GlobalVar(string);
global_names.Add(string, global_var);
return Expr(global_var);
} else {
return Expr(global.value());
}
}
+ // Parse a local of the form `x`.
+ // Right now we fail to parse `x.y`.
case TokenType::Identifier: {
- auto string = next.ToString();
- Consume(TokenType::Identifier);
- auto ctor = ctors.Get(string);
+ auto ctor = ctors.Get(next.ToString());
if (ctor) {
+ Consume(TokenType::Identifier);
return Expr(ctor.value());
} else {
- return Expr(Op::Get(string));
+ auto idents = ParseHierName();
+ std::stringstream op_name;
+ int i = 0;
+ int periods = idents.size() - 1;
+ for (auto ident : idents) {
+ op_name << ident;
+ if (i < periods) {
+ op_name << ".";
+ i++;
+ }
+ }
Review comment:
Comment on this loop? Maybe make it a utility? It's a bit tricky to
parse what this does from the surrounding code.
##########
File path: src/parser/parser.cc
##########
@@ -1231,14 +1335,38 @@ class Parser {
}
}
default: {
- std::stringstream msg;
- msg << "expected an expression found " << Pretty(next->token_type);
- diag_ctx.Emit({next->line, next->column, msg.str()});
- diag_ctx.Render(std::cout);
+ this->diag_ctx->EmitFatal(DiagnosticBuilder(DiagnosticLevel::Error,
next->span)
+ << "expected an expression found "
+ << Pretty(next->token_type));
return Expr();
}
}
});
+
+ if (WhenMatch(TokenType::Period)) {
+ auto index = Match(TokenType::Integer).ToNumber();
+ expr = relay::TupleGetItem(expr, index);
+ }
+
+ return expr;
+ }
+
+ /*! \brief Parse a hierarchical name. */
+ Array<String> ParseHierName() {
Review comment:
Maybe expand this to ParseHierarchicalName? It's not a common
shortening, took a while to figure out what the function name meant when I saw
it in code.
##########
File path: src/parser/token.h
##########
@@ -85,6 +86,9 @@ enum TokenType {
Extern,
Match,
PartialMatch,
+ Metadata,
Review comment:
+1
----------------------------------------------------------------
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]