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

[RUNTIME] Add libbacktrace for backtraces with line numbers #7153

Merged
merged 67 commits into from
Mar 18, 2021

Conversation

tkonolige
Copy link
Contributor

  • Added libbacktrace to 3rdparty
  • Changed build settings to give absolute paths in debug symbols
  • Move CHECK and LOG to tvm/support/logging.h
  • Rename tvm::Error to tvm::CompileError
  • Create new tvm::Error that contains structured information including a backtrace.
  • Replace dmlc::Error with new tvm::Error.
  • Unify dmlc headers to include/tvm/support/dmlc.h and hide LOG and CHECK macros from dmlc.
  • Rename ICHECK, CHECK, and LOG to TVM_ICHECK, TVM_CHECK, TVM_LOG.

This branch is not up to date with main because the changes to check hit a lot of places in the codebase. I'll update it when we are in agreement to merge it.

RFC: https://discuss.tvm.apache.org/t/rfc-line-numbers-in-backtraces/8711

@areusch @junrushao1994 @u99127

@junrushao
Copy link
Member

junrushao commented Dec 22, 2020

Thank you Tristan for the hard work! Would love to ask some questions just for clarification on the forum first :-)

Would you like to also copy your bullet points to the forum as well? Thanks!

Copy link
Member

@junrushao junrushao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's split the renaming to another PR so that this one could be more reviewable...

include/tvm/target/tag.h Outdated Show resolved Hide resolved
include/tvm/support/with.h Outdated Show resolved Hide resolved
CMakeLists.txt Outdated Show resolved Hide resolved
cmake/modules/Libbacktrace.cmake Show resolved Hide resolved
include/tvm/support/logging.h Outdated Show resolved Hide resolved
cmake/modules/Libbacktrace.cmake Show resolved Hide resolved
cmake/modules/Libbacktrace.cmake Show resolved Hide resolved
@junrushao
Copy link
Member

I am not the best guy to review the cmake scripts and the cross-platform part (especially windows). I know a really expert @rkimball - would you like to take a second look if you have time? Thanks a lot!

include/tvm/ir/attrs.h Outdated Show resolved Hide resolved
@tqchen tqchen assigned tqchen and junrushao and unassigned junrushao Dec 23, 2020
@tqchen tqchen added the status: need update need update based on feedbacks label Dec 26, 2020
src/support/logging.cc Outdated Show resolved Hide resolved
@tkonolige tkonolige force-pushed the libbacktrace branch 2 times, most recently from 20b743f to c96b076 Compare January 22, 2021 19:02
@tkonolige
Copy link
Contributor Author

@tqchen @junrushao1994 @antinucleon @areusch dmlc-core has merged the changes needed for the PR. I've update this PR to inject logging macros into dmlc-core, so we now use our new logging macros everywhere. I think this PR is ready for a new round of reviews when you get a chance.

src/runtime/c_runtime_api.cc Outdated Show resolved Hide resolved
include/tvm/runtime/logging.h Show resolved Hide resolved
include/tvm/runtime/logging.h Show resolved Hide resolved
include/tvm/runtime/logging.h Outdated Show resolved Hide resolved
include/tvm/runtime/logging.h Outdated Show resolved Hide resolved
include/tvm/runtime/logging.h Outdated Show resolved Hide resolved
include/tvm/runtime/logging.h Show resolved Hide resolved
@tkonolige
Copy link
Contributor Author

Right now I've put the Error types in the tvm::runtime namespace and then re-exported them from the tvm namespace. Should I just put them in the tvm namespace instead?

@tkonolige tkonolige force-pushed the libbacktrace branch 3 times, most recently from f7f94b1 to be07941 Compare February 1, 2021 23:35
@tkonolige tkonolige force-pushed the libbacktrace branch 2 times, most recently from 08ee22a to f5b083c Compare February 5, 2021 17:20
@tkonolige
Copy link
Contributor Author

@junrushao1994 @tqchen @areusch CI is all green now. Could I get a re-review?

@@ -37,6 +37,15 @@ namespace tvm {
using tvm::parser::SourceMap;
using tvm::runtime::TypedPackedFunc;

/*! \brief The diagnostic level, controls the printing of the message. */
enum class DiagnosticLevel : int {
kBug = 10,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we use terminology that reflects logging levels here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are levels for feedback to the user from compiling a relay expression. I'm not sure if it makes sense to merge them as these are user-facing while logging is more for developers.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think logging will be for users when it is possible to adjust the c log level at runtime

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you want LOG(BUG), LOG(ERROR), LOG(WARNING), LOG(NOTE), LOG(HELP)? Our current levels are INFO, WARNING, ERROR, FATAL. We cause alias INFO to NOTE and then add BUG and HELP?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually don't mind these labels for indicating what a help message contains, but it would be helpful to comment the translation to log levels here so I don't have to look that up. I imagine people will try to work with these labels but will be approaching this from "the build log is too spammy"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There isn't a translation to log messages here. DiagnosticLevel is used within relay and TIR to determine how to print the message (these messages are never hidden or filtered).

@tkonolige
Copy link
Contributor Author

I've switched this to using a submodule.

@junrushao
Copy link
Member

I built and tested locally the backtrace using some existing testcases, and here is the output:

▶ python tests/python/unittest/test_target_target.py
Traceback (most recent call last):
  File "tests/python/unittest/test_target_target.py", line 229, in <module>
    test_target_host_warning()
  File "tests/python/unittest/test_target_target.py", line 218, in test_target_host_warning
    tgt = tvm.target.Target("cuda --host nvidia/jetson-nano", "llvm")
  File "/home/jrshao/Projects/tvm-dev/python/tvm/target/target.py", line 100, in __init__
    self.__init_handle_by_constructor__(
  File "/home/jrshao/Projects/tvm-dev/python/tvm/_ffi/_ctypes/object.py", line 136, in __init_handle_by_constructor__
    handle = __init_by_constructor__(fconstructor, args)
  File "/home/jrshao/Projects/tvm-dev/python/tvm/_ffi/_ctypes/packed_func.py", line 260, in __init_handle_by_constructor__
    raise get_last_ffi_error()
ValueError: Traceback (most recent call last):
  7: TVMFuncCall
        at /home/jrshao/Projects/tvm-dev/src/runtime/c_runtime_api.cc:481
  6: tvm::runtime::PackedFunc::CallPacked(tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*) const
        at /home/jrshao/Projects/tvm-dev/include/tvm/runtime/packed_func.h:1158
  5: std::function<void (tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*)>::operator()(tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*) const
        at /usr/include/c++/10/bits/std_function.h:622
  4: std::_Function_handler<void (tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*), void (*)(tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*)>::_M_invoke(std::_Any_data const&, tvm::runtime::TVMArgs&&, tvm::runtime::TVMRetValue*&&)
        at /usr/include/c++/10/bits/std_function.h:291
  3: std::enable_if<std::__and_<std::is_void<void>, std::__is_invocable<void (*&)(tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*), tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*> >::value, void>::type std::__invoke_r<void, void (*&)(tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*), tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*>(void (*&)(tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*), tvm::runtime::TVMArgs&&, tvm::runtime::TVMRetValue*&&)
        at /usr/include/c++/10/bits/invoke.h:153
  2: void std::__invoke_impl<void, void (*&)(tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*), tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*>(std::__invoke_other, void (*&)(tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*), tvm::runtime::TVMArgs&&, tvm::runtime::TVMRetValue*&&)
        at /usr/include/c++/10/bits/invoke.h:60
  1: tvm::TargetInternal::ConstructorDispatcher(tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*)
        at /home/jrshao/Projects/tvm-dev/src/target/target.cc:471
  0: tvm::Target::Target(tvm::Target, tvm::Target)
        at /home/jrshao/Projects/tvm-dev/src/target/target.cc:377
  File "/home/jrshao/Projects/tvm-dev/src/target/target.cc", line 377
ValueError: Check failed: !n->host.defined() == false: Adding a host to a target whose host field has been defined

Have to say it is really neat! Thank you so much Tristan!

CC: @comaniac @zhiics @yzhliu @icemelon9 @kevinthesun

Copy link
Member

@junrushao junrushao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

Some potential improvements in the future:

  1. Use cmake for libbacktrace in the future. There is no official support but an ongoing PR on the upstream: CMake support ianlancetaylor/libbacktrace#19. Note that it seems to support windows :-)
  2. Not sure if it is a good idea, but perhaps we can strip some unreadable frames in the STL, like this one:
  3: std::enable_if<std::__and_<std::is_void<void>, std::__is_invocable<void (*&)(tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*), tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*> >::value, void>::type std::__invoke_r<void, void (*&)(tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*), tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*>(void (*&)(tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*), tvm::runtime::TVMArgs&&, tvm::runtime::TVMRetValue*&&)
        at /usr/include/c++/10/bits/invoke.h:153

@junrushao
Copy link
Member

I googled around and found some related cmake support for libbacktrace, like libgo: https://go.googlesource.com/gollvm/+/refs/heads/master/cmake/modules/LibbacktraceUtils.cmake

@tkonolige do you think we should switch libbacktrace to using cmake, as the tvm project is built with cmake?

@tqchen tqchen merged commit c976a07 into apache:main Mar 18, 2021
@tqchen
Copy link
Member

tqchen commented Mar 18, 2021

Thanks @tkonolige @rkimball @junrushao1994 @areusch @antinucleon . This PR is now merged

@tqchen tqchen added status: accepted and removed status: need review status: need update need update based on feedbacks labels Mar 18, 2021
@tkonolige
Copy link
Contributor Author

Thanks everyone for putting the time in to review this. I know it was a big PR.

@apivovarov
Copy link
Contributor

apivovarov commented Mar 31, 2021

@tkonolige Looks like libbacktrace build does not look at CMAKE_CXX_COMPILER and CMAKE_C_COMPILER flags.
Regardless of their values it builds libbacktrace for host platform (x86_64)

$ cmake .. \
-DCMAKE_C_COMPILER=mips-linux-uclibc-gnu-gcc \
-DCMAKE_CXX_COMPILER=mips-linux-uclibc-gnu-g++ \
-DCMAKE_C_FLAGS="-march=mips32r2 -DDMLC_LOG_STACK_TRACE=0" \
-DCMAKE_CXX_FLAGS="-march=mips32r2 -D_GLIBCXX_USE_C99 -DDMLC_LOG_STACK_TRACE=0"
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /home/alex/workspace/mips-gcc540-glibc222-64bit-r3.3.0/bin/mips-linux-uclibc-gnu-gcc
-- Check for working C compiler: /home/alex/workspace/mips-gcc540-glibc222-64bit-r3.3.0/bin/mips-linux-uclibc-gnu-gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /home/alex/workspace/mips-gcc540-glibc222-64bit-r3.3.0/bin/mips-linux-uclibc-gnu-g++
-- Check for working CXX compiler: /home/alex/workspace/mips-gcc540-glibc222-64bit-r3.3.0/bin/mips-linux-uclibc-gnu-g++ -- works
...
[  7%] Performing configure step for 'project_libbacktrace'
cd /home/alex/workspace/tvm/build-wyze/libbacktrace && /home/alex/workspace/tvm/cmake/libs/../../3rdparty/libbacktrace/configure --prefix=/home/alex/workspace/tvm/build-wyze/libbacktrace --with-pic
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
...
/bin/bash ./libtool  --tag=CC   --mode=compile gcc -DHAVE_CONFIG_H -I. -I/home/alex/workspace/tvm/cmake/libs/../../3rdparty/libbacktrace    -funwind-tables -frandom-seed=atomic.lo -W -Wall -Wwrite-strings -Wstrict-prototypes -Wmissing-prototypes -Wold-style-definition -Wmissing-format-attribute -Wcast-qual  -g -O2 -c -o atomic.lo /home/alex/workspace/tvm/cmake/libs/../../3rdparty/libbacktrace/atomic.c
...
[ 69%] Building CXX object CMakeFiles/tvm_runtime_objs.dir/src/runtime/graph/graph_runtime.cc.o
/home/alex/workspace/mips-gcc540-glibc222-64bit-r3.3.0/bin/mips-linux-uclibc-gnu-g++  -DDMLC_USE_FOPEN64=0 -DDMLC_USE_LOGGING_LIBRARY="<tvm/runtime/logging.h>" -DNDEBUG -DNDEBUG=1 -DTVM_INDEX_DEFAULT_I64=1 -DTVM_THREADPOOL_USE_OPENMP=0 -DTVM_USE_LIBBACKTRACE=1 -DUSE_FALLBACK_STL_MAP=0 -DTVM_GRAPH_RUNTIME_DEBUG -I/home/alex/workspace/tvm/include -isystem /home/alex/workspace/tvm/3rdparty/dlpack/include -isystem /home/alex/workspace/tvm/3rdparty/dmlc-core/include -isystem /home/alex/workspace/tvm/3rdparty/rang/include -isystem /home/alex/workspace/tvm/3rdparty/compiler-rt -isystem /home/alex/workspace/tvm/3rdparty/picojson -I/home/alex/workspace/tvm/build-wyze/libbacktrace/include -I/home/alex/workspace/tvm/3rdparty/libcrc/include  -std=c++14 -O2 -Wall -fPIC -march=mips32r2 -D_GLIBCXX_USE_C99 -DDMLC_LOG_STACK_TRACE=0   -o CMakeFiles/tvm_runtime_objs.dir/src/runtime/graph/graph_runtime.cc.o -c /home/alex/workspace/tvm/src/runtime/graph/graph_runtime.cc
...
/home/alex/workspace/mips-gcc540-glibc222-64bit-r3.3.0/bin/../lib/gcc/mips-linux-gnu/5.4.0/../../../../mips-linux-gnu/bin/ld: libbacktrace/lib/libbacktrace.a(fileline.o): Relocations in generic ELF (EM: 62)
libbacktrace/lib/libbacktrace.a: error adding symbols: File in wrong format


At least it can be disabled -DUSE_LIBBACKTRACE=0...

@junrushao
Copy link
Member

@apivovarov I believe Tristan is working on this: #7705 (comment)

trevor-m pushed a commit to trevor-m/tvm that referenced this pull request May 6, 2021
)

* [RUNTIME] Add libbacktrace for backtraces with line numbers

Co-authored-by: Robert Kimball <[email protected]>
trevor-m pushed a commit to neo-ai/tvm that referenced this pull request May 11, 2021
)

* [RUNTIME] Add libbacktrace for backtraces with line numbers

Co-authored-by: Robert Kimball <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants