Skip to content

Commit

Permalink
[RUNTIME] Add libbacktrace for backtraces with line numbers
Browse files Browse the repository at this point in the history
- Changed build settings to give absolute paths in debug symbols
- Add new TVM error types for internal errors
- Move CHECK and LOG(FATAL) to tvm/runtime/logging.h
  • Loading branch information
tkonolige committed Jan 22, 2021
1 parent 6787d74 commit 20b743f
Show file tree
Hide file tree
Showing 102 changed files with 801 additions and 337 deletions.
41 changes: 40 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ tvm_option(USE_TF_TVMDSOOP "Build with TensorFlow TVMDSOOp" OFF)
tvm_option(USE_FALLBACK_STL_MAP "Use TVM's POD compatible Map" OFF)
tvm_option(USE_ETHOSN "Build with Arm Ethos-N" OFF)
tvm_option(INDEX_DEFAULT_I64 "Defaults the index datatype to int64" ON)
set(_LIBBACKTRACE_DEFAULT OFF)
if(CMAKE_SYSTEM_NAME MATCHES "Darwin" OR CMAKE_SYSTEM_NAME MATCHES "Linux")
set(_LIBBACKTRACE_DEFAULT ON)
endif()
tvm_option(USE_LIBBACKTRACE "Build libbacktrace to supply linenumbers on stack traces" ${_LIBBACKTRACE_DEFAULT})

# 3rdparty libraries
tvm_option(DLPACK_PATH "Path to DLPACK" "3rdparty/dlpack/include")
Expand Down Expand Up @@ -87,6 +92,7 @@ include_directories(${CMAKE_INCLUDE_PATH})
include_directories("include")
include_directories(SYSTEM ${DLPACK_PATH})
include_directories(SYSTEM ${DMLC_PATH})
add_definitions(-DDMLC_USE_LOGGING_LIBRARY=<tvm/runtime/logging.h>)
include_directories(SYSTEM ${RANG_PATH})
include_directories(SYSTEM ${COMPILER_RT_PATH})
include_directories(SYSTEM ${PICOJSON_PATH})
Expand Down Expand Up @@ -379,6 +385,17 @@ set_property(TARGET tvm APPEND PROPERTY LINK_OPTIONS "${TVM_VISIBILITY_FLAGS}")
add_library(tvm_runtime SHARED $<TARGET_OBJECTS:tvm_runtime_objs>)
set_property(TARGET tvm_runtime APPEND PROPERTY LINK_OPTIONS "${TVM_VISIBILITY_FLAGS}")

if(USE_LIBBACKTRACE)
message(STATUS "Building with libbacktrace...")
include(cmake/modules/Libbacktrace.cmake)
target_link_libraries(tvm PRIVATE libbacktrace)
target_link_libraries(tvm_objs PRIVATE libbacktrace)
target_link_libraries(tvm_runtime PRIVATE libbacktrace)
target_link_libraries(tvm_runtime_objs PRIVATE libbacktrace)
else()
add_definitions(-DTVM_BACKTRACE_DISABLED)
endif()

if(USE_MICRO)
# NOTE: cmake doesn't track dependencies at the file level across subdirectories. For the
# Unix Makefiles generator, need to add these explicit target-level dependency)
Expand All @@ -393,7 +410,7 @@ endif()
if(USE_RELAY_DEBUG)
message(STATUS "Building Relay in debug mode...")
target_compile_definitions(tvm_objs PRIVATE "USE_RELAY_DEBUG")
target_compile_definitions(tvm_objs PRIVATE "DMLC_LOG_DEBUG")
target_compile_definitions(tvm_objs PRIVATE "TVM_LOG_DEBUG")
else()
target_compile_definitions(tvm_objs PRIVATE "NDEBUG")
endif(USE_RELAY_DEBUG)
Expand Down Expand Up @@ -526,3 +543,25 @@ if(MSVC)
target_compile_definitions(tvm_objs PRIVATE -DTVM_EXPORTS)
target_compile_definitions(tvm_runtime_objs PRIVATE -DTVM_EXPORTS)
endif()

set(TVM_IS_DEBUG_BUILD OFF)
if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo" OR CMAKE_CXX_FLAGS MATCHES "-g")
set(TVM_IS_DEBUG_BUILD ON)
endif()

# Change relative paths in backtrace to absolute ones
if(TVM_IS_DEBUG_BUILD)
target_compile_options(tvm PRIVATE "-ffile-prefix-map=..=${CMAKE_CURRENT_SOURCE_DIR}")
endif()

# Run dsymutil to generate debugging symbols for backtraces
if(APPLE AND TVM_IS_DEBUG_BUILD)
find_program(DSYMUTIL dsymutil)
mark_as_advanced(DSYMUTIL)
add_custom_command(TARGET tvm
POST_BUILD
COMMAND ${DSYMUTIL} ARGS $<TARGET_FILE:tvm>
COMMENT "Running dsymutil"
VERBATIM
)
endif()
5 changes: 5 additions & 0 deletions cmake/config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,8 @@ set(USE_HEXAGON_SDK /path/to/sdk)

# Whether to use ONNX codegen
set(USE_TARGET_ONNX OFF)

# Whether to use libbacktrace
# Libbacktrace provides line and column information on stack traces from errors. It is only
# supported on linux and macOS.
# set(USE_LIBBACKTRACE OFF)
38 changes: 38 additions & 0 deletions cmake/modules/Libbacktrace.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
include(ExternalProject)

# Git tag for libbacktrace
set(TAG dedbe13fda00253fe5d4f2fb812c909729ed5937)

ExternalProject_Add(project_libbacktrace
PREFIX libbacktrace
GIT_REPOSITORY https://github.com/ianlancetaylor/libbacktrace.git
GIT_TAG ${TAG}
CONFIGURE_COMMAND "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/src/project_libbacktrace/configure"
"--prefix=${CMAKE_CURRENT_BINARY_DIR}/libbacktrace"
INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace"
BUILD_COMMAND make
INSTALL_COMMAND make install
BUILD_BYPRODUCTS "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/lib/libbacktrace.a"
"${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include/backtrace.h"
# disable the builtin update because it rebuilds on every build
UPDATE_DISCONNECTED ON
UPDATE_COMMAND ""
# libbacktrace has a bug on macOS with shared libraries.
PATCH_COMMAND git apply ${CMAKE_CURRENT_LIST_DIR}/libbacktrace_macos.patch
)

# Only rebuild libbacktrace if this file changes
ExternalProject_Add_Step(project_libbacktrace update-new
DEPENDERS configure
DEPENDS "${CMAKE_CURRENT_LIST_DIR}/Libbacktrace.cmake"
COMMAND cd ${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/src/project_libbacktrace; git checkout -f ${TAG}
)

add_library(libbacktrace STATIC IMPORTED)
add_dependencies(libbacktrace project_libbacktrace)
set_property(TARGET libbacktrace
PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/lib/libbacktrace.a)
# create include directory so cmake doesn't complain
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include)
target_include_directories(libbacktrace
INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include>)
13 changes: 13 additions & 0 deletions cmake/modules/libbacktrace_macos.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
diff --git a/macho.c b/macho.c
index 66e101e..d00aea9 100644
--- a/macho.c
+++ b/macho.c
@@ -1268,7 +1268,7 @@ backtrace_initialize (struct backtrace_state *state, const char *filename,
mff = macho_nodebug;
if (!macho_add (state, name, d, 0, NULL, base_address, 0,
error_callback, data, &mff, &mfs))
- return 0;
+ continue;

if (mff != macho_nodebug)
macho_fileline_fn = mff;
Empty file modified include/tvm/auto_scheduler/loop_state.h
100755 → 100644
Empty file.
Empty file modified include/tvm/auto_scheduler/transform_step.h
100755 → 100644
Empty file.
4 changes: 2 additions & 2 deletions include/tvm/ir/attrs.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ inline DataType NullValue<DataType>() {
}

/*! \brief Error thrown during attribute checking. */
struct AttrError : public dmlc::Error {
struct AttrError : public Error {
/*!
* \brief constructor
* \param msg error message
*/
explicit AttrError(std::string msg) : dmlc::Error("AttributeError:" + msg) {}
explicit AttrError(std::string msg) : Error("AttributeError:" + msg) {}
};

/*!
Expand Down
26 changes: 13 additions & 13 deletions include/tvm/ir/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ namespace tvm {
/*!
* \brief A wrapper around std::stringstream to build error.
*
* Can be consumed by Error to construct an error.
* Can be consumed by CompileError to construct an error.
*
* \code
*
* void ReportError(const Error& err);
* void ReportError(const CompileError& err);
*
* void Test(int number) {
* // Use error reporter to construct an error.
Expand All @@ -59,34 +59,34 @@ struct ErrorBuilder {

private:
std::stringstream stream_;
friend class Error;
friend class CompileError;
};

/*!
* \brief Custom Error class to be thrown during compilation.
*/
class Error : public dmlc::Error {
class CompileError : public Error {
public:
/*! \brief Location of the error */
Span span;
/*!
* \brief construct error from message.
* \param msg The message
*/
explicit Error(const std::string& msg) : dmlc::Error(msg), span(nullptr) {}
explicit CompileError(const std::string& msg) : Error(msg), span(nullptr) {}
/*!
* \brief construct error from error builder.
* \param err The error builder
*/
Error(const ErrorBuilder& err) : dmlc::Error(err.stream_.str()), span(nullptr) {} // NOLINT(*)
CompileError(const ErrorBuilder& err) : Error(err.stream_.str()), span(nullptr) {} // NOLINT(*)
/*!
* \brief copy constructor.
* \param other The other ereor.
*/
Error(const Error& other) : dmlc::Error(other.what()), span(other.span) {} // NOLINT(*)
CompileError(const CompileError& other) : Error(other.what()), span(other.span) {} // NOLINT(*)
/*!
* \brief default constructor. */
Error() : dmlc::Error(""), span(nullptr) {}
CompileError() : Error(""), span(nullptr) {}
};

/*!
Expand Down Expand Up @@ -115,13 +115,13 @@ class ErrorReporter {
ErrorReporter() : errors_(), node_to_error_() {}

/*!
* \brief Report a tvm::Error.
* \brief Report a CompileError.
*
* This API is useful for reporting spanned errors.
*
* \param err The error to report.
*/
void Report(const Error& err) {
void Report(const CompileError& err) {
if (!err.span.defined()) {
throw err;
}
Expand All @@ -143,7 +143,7 @@ class ErrorReporter {
*/
void ReportAt(const GlobalVar& global, const ObjectRef& node, std::stringstream& err) {
std::string err_msg = err.str();
this->ReportAt(global, node, Error(err_msg));
this->ReportAt(global, node, CompileError(err_msg));
}

/*!
Expand All @@ -158,7 +158,7 @@ class ErrorReporter {
* \param node The expression or type to report the error at.
* \param err The error to report.
*/
void ReportAt(const GlobalVar& global, const ObjectRef& node, const Error& err);
void ReportAt(const GlobalVar& global, const ObjectRef& node, const CompileError& err);

/*!
* \brief Render all reported errors and exit the program.
Expand All @@ -176,7 +176,7 @@ class ErrorReporter {
inline bool AnyErrors() { return errors_.size() != 0; }

private:
std::vector<Error> errors_;
std::vector<CompileError> errors_;
std::unordered_map<ObjectRef, std::vector<size_t>, ObjectPtrHash, ObjectPtrEqual> node_to_error_;
std::unordered_map<ObjectRef, GlobalVar, ObjectPtrHash, ObjectPtrEqual> node_to_gv_;
};
Expand Down
2 changes: 1 addition & 1 deletion include/tvm/ir/type_relation.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include <tvm/ir/env_func.h>
#include <tvm/ir/module.h>
#include <tvm/ir/type.h>
#include <tvm/support/logging.h>
#include <tvm/runtime/logging.h>

namespace tvm {

Expand Down
2 changes: 1 addition & 1 deletion include/tvm/relay/analysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include <tvm/relay/expr.h>
#include <tvm/relay/function.h>
#include <tvm/relay/type.h>
#include <tvm/support/logging.h>
#include <tvm/runtime/logging.h>

#include <string>
#include <unordered_map>
Expand Down
2 changes: 1 addition & 1 deletion include/tvm/runtime/container.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#ifndef TVM_RUNTIME_CONTAINER_H_
#define TVM_RUNTIME_CONTAINER_H_

#include <dmlc/logging.h>
#include <tvm/runtime/logging.h>
#include <tvm/runtime/memory.h>
#include <tvm/runtime/object.h>

Expand Down
2 changes: 1 addition & 1 deletion include/tvm/runtime/data_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#define TVM_RUNTIME_DATA_TYPE_H_

#include <tvm/runtime/c_runtime_api.h>
#include <tvm/support/logging.h>
#include <tvm/runtime/logging.h>

#include <string>
#include <type_traits>
Expand Down
Loading

0 comments on commit 20b743f

Please sign in to comment.