Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Frontend][Relay] Add Parser 2.0 #5932

Merged
merged 45 commits into from
Jul 8, 2020
Merged
Show file tree
Hide file tree
Changes from 42 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
b1ba914
Start on new parser
jroesch Apr 25, 2020
7d89a94
First test passing
jroesch Apr 27, 2020
86c33e2
Try and factor into helpers
jroesch Apr 27, 2020
e34afcb
Comments are working
jroesch Apr 28, 2020
c624107
Refactor binding parser to be iterative
jroesch Apr 28, 2020
5e26476
Add number parsing code
jroesch Jun 23, 2020
f6bc944
Add configurable shift-reduce parser for operators
jroesch Jun 23, 2020
d50e750
Operator parsing is done
jroesch Jun 23, 2020
2d8cf54
Add tokenizer fix
jroesch Jun 23, 2020
17a14aa
I hate parsing
jroesch Jun 23, 2020
db7879c
About half-way through parsing functions.
jroesch Jun 24, 2020
415c60d
Add definition parsing
jroesch Jun 24, 2020
ef010e5
Disable some debugging and reorder expression parsing
jroesch Jun 25, 2020
c2e1928
Passing most tests cases
jroesch Jun 26, 2020
5648678
Finish types parsing
jroesch Jun 26, 2020
9b22a4f
Finish ADT parsing
jroesch Jun 27, 2020
98d815a
Finish ADT parsing
jroesch Jun 27, 2020
baa4a64
Reorganize code into files
jroesch Jun 29, 2020
41ca113
Reorganize code into files
jroesch Jun 29, 2020
b3d3975
Fix the line map code
jroesch Jun 29, 2020
1d1cf30
Parse generics on functions on the way to match
jroesch Jun 29, 2020
49a02df
Add a lot of comments
jroesch Jun 30, 2020
929c4c5
Get test cases passing
jroesch Jun 30, 2020
9529e7c
Add some more docs
jroesch Jul 1, 2020
9711638
Fix a couple minor things
jroesch Jul 1, 2020
1c4deb0
Address some feedback
jroesch Jul 7, 2020
e818f4d
Address feedback
jroesch Jul 7, 2020
932db63
Fix extern case
jroesch Jul 7, 2020
4b83ea2
More feedback
jroesch Jul 7, 2020
5546a95
Fix
jroesch Jul 7, 2020
30c6281
Formatting
jroesch Jul 7, 2020
4f0622e
Add parser
jroesch Jul 7, 2020
0c61fda
Cpp lint
jroesch Jul 7, 2020
8aca8f4
Format
jroesch Jul 7, 2020
e700147
Add module string
jroesch Jul 7, 2020
5a0b17e
Small tweaks for older C++ compiler on CI
jroesch Jul 7, 2020
4cd12c3
Format
jroesch Jul 7, 2020
abddaf6
Apply suggestions from code review
jroesch Jul 7, 2020
1eae9e1
Minor tweaks
jroesch Jul 7, 2020
3bc277a
Fix some windows issues
jroesch Jul 7, 2020
ac836a8
More comments and windows issues
jroesch Jul 7, 2020
ef5fbda
Formatting
jroesch Jul 7, 2020
62d9f97
Rename field names
jroesch Jul 7, 2020
e169ea7
Formatting
jroesch Jul 7, 2020
8206bc8
Fix span test
jroesch Jul 7, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ file(GLOB_RECURSE COMPILER_SRCS
src/autotvm/*.cc
src/tir/*.cc
src/driver/*.cc
src/parser/*.cc
src/printer/*.cc
src/support/*.cc
)
Expand Down
18 changes: 9 additions & 9 deletions include/tvm/ir/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,22 @@ class Span;
*/
class SpanNode : public Object {
public:
/*! \brief The source name */
/*! \brief The source name. */
SourceName source;
/*! \brief Line number */
int lineno;
/*! \brief column offset */
int col_offset;
/*! \brief The line number. */
int line;
/*! \brief The column offset. */
int column;

// override attr visitor
void VisitAttrs(AttrVisitor* v) {
v->Visit("source", &source);
v->Visit("lineno", &lineno);
v->Visit("col_offset", &col_offset);
v->Visit("line", &line);
v->Visit("column", &column);
}

bool SEqualReduce(const SpanNode* other, SEqualReducer equal) const {
return equal(source, other->source) && equal(lineno, other->lineno) &&
equal(col_offset, other->col_offset);
return equal(source, other->source) && equal(line, other->line) && equal(column, other->column);
}

static constexpr const char* _type_key = "Span";
Expand Down
40 changes: 40 additions & 0 deletions include/tvm/parser/parser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.
*/

#ifndef TVM_PARSER_PARSER_H_
#define TVM_PARSER_PARSER_H_
/*!
* \file parser.h
* \brief A parser for TVM IR.
*/
#include <tvm/runtime/packed_func.h>
#include <tvm/runtime/registry.h>

#include <fstream>
#include <string>

namespace tvm {
namespace parser {

IRModule Parse(std::string file_name, std::string file_content);

} // namespace parser
} // namespace tvm

#endif // TVM_PARSER_PARSER_H_
3 changes: 3 additions & 0 deletions python/tvm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
# tvm.driver
from .driver import build, lower

# tvm.parser
from . import parser

# others
from . import arith

Expand Down
27 changes: 27 additions & 0 deletions python/tvm/parser/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 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.
"""The under development unified IR parsing infrastructure."""
from . import _ffi_api

def parse(source, source_name="from_string"):
return _ffi_api.ParseModule(source_name, source)

def parse_expr(source):
return _ffi_api.ParseExpr("string", source)

def fromtext(source, source_name="from_string"):
return parse(str(source), str(source_name))
21 changes: 21 additions & 0 deletions python/tvm/parser/_ffi_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# 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.
"""FFI APIs for tvm.ir"""
import tvm._ffi


tvm._ffi._init_api("parser", __name__)
7 changes: 3 additions & 4 deletions src/ir/span.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ TVM_REGISTER_NODE_TYPE(SourceNameNode)
Span::Span(SourceName source, int lineno, int col_offset) {
auto n = make_object<SpanNode>();
n->source = std::move(source);
n->lineno = lineno;
n->col_offset = col_offset;
n->line = lineno;
n->column = col_offset;
data_ = std::move(n);
}

Expand All @@ -78,7 +78,6 @@ TVM_REGISTER_GLOBAL("ir.Span").set_body_typed([](SourceName source, int lineno,
TVM_STATIC_IR_FUNCTOR(ReprPrinter, vtable)
.set_dispatch<SpanNode>([](const ObjectRef& ref, ReprPrinter* p) {
auto* node = static_cast<const SpanNode*>(ref.get());
p->stream << "Span(" << node->source << ", " << node->lineno << ", " << node->col_offset
<< ")";
p->stream << "Span(" << node->source << ", " << node->line << ", " << node->column << ")";
});
} // namespace tvm
2 changes: 1 addition & 1 deletion src/node/structural_equal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

namespace tvm {

// Define the dispatch functio here since primary user is in this file.
// Define the dispatch function here since primary user is in this file.
bool ReflectionVTable::SEqualReduce(const Object* self, const Object* other,
SEqualReducer equal) const {
uint32_t tindex = self->type_index();
Expand Down
176 changes: 176 additions & 0 deletions src/parser/diagnostic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/*
* 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 diagnostic.h
* \brief A new diagnostic interface for TVM error reporting.
*
* A prototype of the new diagnostic reporting interface for TVM.
*
* Eventually we hope to promote this file to the top-level and
* replace the existing errors.h.
*/

#ifndef TVM_PARSER_DIAGNOSTIC_H_
#define TVM_PARSER_DIAGNOSTIC_H_

#include <tvm/ir/span.h>
#include <tvm/runtime/container.h>
#include <tvm/runtime/object.h>

#include <fstream>
#include <string>
#include <utility>
#include <vector>

namespace tvm {
namespace parser {

/*! \brief A program source in any language.
*
* Could represent the source from an ML framework or the internal
* source of a TVM program.
*/
struct Source {
/*! \brief The raw source. */
std::string source;
jroesch marked this conversation as resolved.
Show resolved Hide resolved
/*! \brief A mapping of line breaks into the raw source. */
std::vector<std::pair<int, int>> line_map;

/*! \brief An empty source. */
Source() : source(), line_map() {}

/*! \brief Construct a source from a string. */
explicit Source(const std::string& source) : source(source) {
int index = 0;
int length = 0;
line_map.push_back({index, length});
for (auto c : source) {
if (c == '\n') {
// Record the length of the line.
line_map.back().second = length;
// Bump past the newline.
index += 1;
// Record the start of the next line, and put placeholder for length.
line_map.push_back({index, 0});
// Reset length to zero.
length = 0;
} else {
length += 1;
index += 1;
}
}
line_map.back().second = length;
}

Source(const Source& source) : source(source.source), line_map(source.line_map) {}

/*! \brief Generate an error message at a specific line and column with the
* annotated message.
*
* The error is written directly to the `out` std::ostream.
*
* \param out The output ostream.
* \param line The line at which to report a diagnostic.
* \param line The column at which to report a diagnostic.
* \param msg The message to attach.
*/
void ReportAt(std::ostream& out, int line, int column, const std::string& msg) const {
CHECK(line - 1 <= static_cast<int64_t>(line_map.size()))
<< "requested line: " << (line - 1) << "line_map size: " << line_map.size()
<< "source: " << source;

// Adjust for zero indexing, now have (line_start, line_length);
auto range = line_map.at(line - 1);
int line_start = range.first;
int line_length = range.second;
out << "file:" << line << ":" << column << ": parse error: " << msg << std::endl;
out << " " << source.substr(line_start, line_length) << std::endl;
out << " ";
std::stringstream marker;
for (int i = 1; i <= line_length; i++) {
if (i == column) {
marker << "^";
} else if ((column - i) < 3) {
marker << "~";
} else if ((i - column) < 3) {
marker << "~";
} else {
marker << " ";
}
}
out << marker.str();
out << std::endl;
}
};

/*! \brief The diagnostic level, controls the printing of the message. */
enum DiagnosticLevel {
Bug,
Error,
Warning,
Note,
Help,
};

/*! \brief A diagnostic message. */
struct Diagnostic {
/*! \brief The level. */
DiagnosticLevel level;
/*! \brief The span at which to report an error. */
Span span;
/*! \brief The diagnostic message. */
std::string message;

Diagnostic(int line, int column, const std::string& message)
: level(DiagnosticLevel::Error), span(SourceName(), line, column), message(message) {}
};

/*! \brief A diagnostic context for recording errors against a source file.
* TODO(@jroesch): convert source map and improve in follow up PR, the parser
* assumes a single global file for now.
*/
struct DiagnosticContext {
/*! \brief The source to report against. */
Source source;

/*! \brief The set of diagnostics to report. */
std::vector<Diagnostic> diagnostics;

explicit DiagnosticContext(const Source& source) : source(source) {}

/*! \brief Emit a diagnostic. */
void Emit(const Diagnostic& diagnostic) { diagnostics.push_back(diagnostic); }

// TODO(@jroesch): eventually modularize the rendering interface to provide control of how to
// format errors.
void Render(std::ostream& ostream) {
for (auto diagnostic : diagnostics) {
source.ReportAt(ostream, diagnostic.span->line, diagnostic.span->column, diagnostic.message);
}

if (diagnostics.size()) {
LOG(FATAL) << "parse error occured";
}
}
};

} // namespace parser
} // namespace tvm
#endif // TVM_PARSER_DIAGNOSTIC_H_
Loading