forked from apache/tvm
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Diagnostics][Relay][InferType] Refactor InferType to work on whole m…
…odule, and use new diagnostics. (apache#6274) * Refactor the type checker to use diagnostics Although this patch is very large and seemingly disjoint the fixes are required to get it working for the entire stack. I started with first changing InferType to use the diagnostics, these weren't yet in the pass manager so this required changes to module and module pass. InferType wasn't actually written correctly as a pass requring refactoring there, then in order to add spans to AST it required turning on AnnotateSpans which in term required changes to the parser, and module to make it possible to use the errors. These changes to parse and module required changes to diagnostics and InferType. Althought seemingly disconnected there are hidden cycles between the components which require simultaneous change in order to remove the old error reporting. A huge change due to this patch is that the module no longer implicitly type checks functions which are added. * Apply suggestions from code review Co-authored-by: Robert Kimball <bobkimball@gmail.com> Co-authored-by: Junru Shao <junrushao1994@gmail.com> * Apply suggestions from code review Co-authored-by: Tristan Konolige <tristan.konolige@gmail.com> * Clean up parser * CR feedback * Apply Bobs suggestions * Fix up Python interface for diagnostics * Fix test_ir_parser and formatting * Fix cpplint * Fix lint * Fix format * More lint * Fix format * Kill dead doc comment * Fix documentation comment * Rebase fixups * Add docs for type.h * Fix parser.cc * Fix unittests * Fix black * Skip previously typechecked functions * fix ACL * Fix numerous issues * Add repr method * Fix issue with Pytest, I am ready to cry * Fix the rest of tests * Kill dead code * Fix dignostic tests * Fix more tests * fix more tests (#11) * Fix diagnostic.py deinit bug * Fix deinit issue * Format * Tweak disabling of override * Format * Fix BYOC * Fix TensorArray stuff * Fix PyTorch * Format * Format Co-authored-by: Robert Kimball <bobkimball@gmail.com> Co-authored-by: Junru Shao <junrushao1994@gmail.com> Co-authored-by: Tristan Konolige <tristan.konolige@gmail.com> Co-authored-by: Cody Yu <comaniac0422@gmail.com> Co-authored-by: Zhi <5145158+zhiics@users.noreply.github.com>
- Loading branch information
Showing
128 changed files
with
4,274 additions
and
2,594 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,4 +57,3 @@ git checkout "$repo_revision" | |
|
||
cd "driver" | ||
scons install_prefix="$install_path" install | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,262 @@ | ||
/* | ||
* 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_IR_DIAGNOSTIC_H_ | ||
#define TVM_IR_DIAGNOSTIC_H_ | ||
|
||
#include <tvm/ir/module.h> | ||
#include <tvm/ir/span.h> | ||
#include <tvm/parser/source_map.h> | ||
#include <tvm/runtime/container.h> | ||
#include <tvm/runtime/object.h> | ||
#include <tvm/support/logging.h> | ||
|
||
#include <fstream> | ||
#include <string> | ||
#include <utility> | ||
#include <vector> | ||
|
||
namespace tvm { | ||
|
||
using tvm::parser::SourceMap; | ||
using tvm::runtime::TypedPackedFunc; | ||
|
||
extern const char* kTVM_INTERNAL_ERROR_MESSAGE; | ||
|
||
#define ICHECK_INDENT " " | ||
|
||
#define ICHECK_BINARY_OP(name, op, x, y) \ | ||
if (dmlc::LogCheckError _check_err = dmlc::LogCheck##name(x, y)) \ | ||
dmlc::LogMessageFatal(__FILE__, __LINE__).stream() \ | ||
<< kTVM_INTERNAL_ERROR_MESSAGE << std::endl \ | ||
<< ICHECK_INDENT << "Check failed: " << #x " " #op " " #y << *(_check_err.str) << ": " | ||
|
||
#define ICHECK(x) \ | ||
if (!(x)) \ | ||
dmlc::LogMessageFatal(__FILE__, __LINE__).stream() \ | ||
<< kTVM_INTERNAL_ERROR_MESSAGE << ICHECK_INDENT << "Check failed: " #x << " == false: " | ||
|
||
#define ICHECK_LT(x, y) ICHECK_BINARY_OP(_LT, <, x, y) | ||
#define ICHECK_GT(x, y) ICHECK_BINARY_OP(_GT, >, x, y) | ||
#define ICHECK_LE(x, y) ICHECK_BINARY_OP(_LE, <=, x, y) | ||
#define ICHECK_GE(x, y) ICHECK_BINARY_OP(_GE, >=, x, y) | ||
#define ICHECK_EQ(x, y) ICHECK_BINARY_OP(_EQ, ==, x, y) | ||
#define ICHECK_NE(x, y) ICHECK_BINARY_OP(_NE, !=, x, y) | ||
#define ICHECK_NOTNULL(x) \ | ||
((x) == nullptr ? dmlc::LogMessageFatal(__FILE__, __LINE__).stream() \ | ||
<< kTVM_INTERNAL_ERROR_MESSAGE << __INDENT << "Check not null: " #x \ | ||
<< ' ', \ | ||
(x) : (x)) // NOLINT(*) | ||
|
||
/*! \brief The diagnostic level, controls the printing of the message. */ | ||
enum class DiagnosticLevel : int { | ||
kBug = 10, | ||
kError = 20, | ||
kWarning = 30, | ||
kNote = 40, | ||
kHelp = 50, | ||
}; | ||
|
||
class DiagnosticBuilder; | ||
|
||
/*! \brief A compiler diagnostic. */ | ||
class Diagnostic; | ||
|
||
/*! \brief A compiler diagnostic message. */ | ||
class DiagnosticNode : public Object { | ||
public: | ||
/*! \brief The level. */ | ||
DiagnosticLevel level; | ||
/*! \brief The span at which to report an error. */ | ||
Span span; | ||
/*! \brief The diagnostic message. */ | ||
String message; | ||
|
||
// override attr visitor | ||
void VisitAttrs(AttrVisitor* v) { | ||
v->Visit("level", &level); | ||
v->Visit("span", &span); | ||
v->Visit("message", &message); | ||
} | ||
|
||
bool SEqualReduce(const DiagnosticNode* other, SEqualReducer equal) const { | ||
return equal(this->level, other->level) && equal(this->span, other->span) && | ||
equal(this->message, other->message); | ||
} | ||
|
||
static constexpr const char* _type_key = "Diagnostic"; | ||
TVM_DECLARE_FINAL_OBJECT_INFO(DiagnosticNode, Object); | ||
}; | ||
|
||
class Diagnostic : public ObjectRef { | ||
public: | ||
TVM_DLL Diagnostic(DiagnosticLevel level, Span span, const std::string& message); | ||
|
||
static DiagnosticBuilder Bug(Span span); | ||
static DiagnosticBuilder Error(Span span); | ||
static DiagnosticBuilder Warning(Span span); | ||
static DiagnosticBuilder Note(Span span); | ||
static DiagnosticBuilder Help(Span span); | ||
|
||
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(Diagnostic, ObjectRef, DiagnosticNode); | ||
}; | ||
|
||
/*! | ||
* \brief A wrapper around std::stringstream to build a diagnostic. | ||
*/ | ||
class DiagnosticBuilder { | ||
public: | ||
/*! \brief The level. */ | ||
DiagnosticLevel level; | ||
|
||
/*! \brief The source name. */ | ||
SourceName source_name; | ||
|
||
/*! \brief The span of the diagnostic. */ | ||
Span span; | ||
|
||
template <typename T> | ||
DiagnosticBuilder& operator<<(const T& val) { // NOLINT(*) | ||
stream_ << val; | ||
return *this; | ||
} | ||
|
||
DiagnosticBuilder() : level(DiagnosticLevel::kError), source_name(), span(Span()) {} | ||
|
||
DiagnosticBuilder(const DiagnosticBuilder& builder) | ||
: level(builder.level), source_name(builder.source_name), span(builder.span) {} | ||
|
||
DiagnosticBuilder(DiagnosticLevel level, Span span) : level(level), span(span) {} | ||
|
||
operator Diagnostic() { return Diagnostic(this->level, this->span, this->stream_.str()); } | ||
|
||
private: | ||
std::stringstream stream_; | ||
friend class Diagnostic; | ||
}; | ||
|
||
/*! | ||
* \brief A diagnostic context for recording errors against a source file. | ||
*/ | ||
class DiagnosticContext; | ||
|
||
/*! \brief Display diagnostics in a given display format. | ||
* | ||
* A diagnostic renderer is responsible for converting the | ||
* raw diagnostics into consumable output. | ||
* | ||
* For example the terminal renderer will render a sequence | ||
* of compiler diagnostics to std::out and std::err in | ||
* a human readable form. | ||
*/ | ||
class DiagnosticRendererNode : public Object { | ||
public: | ||
TypedPackedFunc<void(DiagnosticContext ctx)> renderer; | ||
|
||
// override attr visitor | ||
void VisitAttrs(AttrVisitor* v) {} | ||
|
||
static constexpr const char* _type_key = "DiagnosticRenderer"; | ||
TVM_DECLARE_FINAL_OBJECT_INFO(DiagnosticRendererNode, Object); | ||
}; | ||
|
||
class DiagnosticRenderer : public ObjectRef { | ||
public: | ||
TVM_DLL DiagnosticRenderer(TypedPackedFunc<void(DiagnosticContext ctx)> render); | ||
TVM_DLL DiagnosticRenderer() | ||
: DiagnosticRenderer(TypedPackedFunc<void(DiagnosticContext ctx)>()) {} | ||
|
||
void Render(const DiagnosticContext& ctx); | ||
|
||
DiagnosticRendererNode* operator->() { | ||
CHECK(get() != nullptr); | ||
return static_cast<DiagnosticRendererNode*>(get_mutable()); | ||
} | ||
|
||
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(DiagnosticRenderer, ObjectRef, DiagnosticRendererNode); | ||
}; | ||
|
||
class DiagnosticContextNode : public Object { | ||
public: | ||
/*! \brief The Module to report against. */ | ||
IRModule module; | ||
|
||
/*! \brief The set of diagnostics to report. */ | ||
Array<Diagnostic> diagnostics; | ||
|
||
/*! \brief The renderer set for the context. */ | ||
DiagnosticRenderer renderer; | ||
|
||
void VisitAttrs(AttrVisitor* v) { | ||
v->Visit("module", &module); | ||
v->Visit("diagnostics", &diagnostics); | ||
} | ||
|
||
bool SEqualReduce(const DiagnosticContextNode* other, SEqualReducer equal) const { | ||
return equal(module, other->module) && equal(diagnostics, other->diagnostics); | ||
} | ||
|
||
static constexpr const char* _type_key = "DiagnosticContext"; | ||
TVM_DECLARE_FINAL_OBJECT_INFO(DiagnosticContextNode, Object); | ||
}; | ||
|
||
class DiagnosticContext : public ObjectRef { | ||
public: | ||
TVM_DLL DiagnosticContext(const IRModule& module, const DiagnosticRenderer& renderer); | ||
TVM_DLL static DiagnosticContext Default(const IRModule& source_map); | ||
|
||
/*! \brief Emit a diagnostic. | ||
* \param diagnostic The diagnostic to emit. | ||
*/ | ||
void Emit(const Diagnostic& diagnostic); | ||
|
||
/*! \brief Emit a diagnostic and then immediately attempt to render all errors. | ||
* | ||
* \param diagnostic The diagnostic to emit. | ||
* | ||
* Note: this will raise an exception if you would like to instead continue execution | ||
* use the Emit method instead. | ||
*/ | ||
void EmitFatal(const Diagnostic& diagnostic); | ||
|
||
/*! \brief Render the errors and raise a DiagnosticError exception. */ | ||
void Render(); | ||
|
||
DiagnosticContextNode* operator->() { | ||
CHECK(get() != nullptr); | ||
return static_cast<DiagnosticContextNode*>(get_mutable()); | ||
} | ||
|
||
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(DiagnosticContext, ObjectRef, DiagnosticContextNode); | ||
}; | ||
|
||
DiagnosticRenderer TerminalRenderer(std::ostream& ostream); | ||
|
||
} // namespace tvm | ||
#endif // TVM_IR_DIAGNOSTIC_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.