Skip to content

Commit

Permalink
[IR][Debug] Add dump and print for debugging (NFC)
Browse files Browse the repository at this point in the history
- Add two utility functions for debugging. With this change,
  developers could use

  `print op->dump()` or `print op->print()`

  inside a debugger (lldb or gdb). Object op could be either
  of type Object or ObjectRef.

  Note that the existing tvm::Dump(ref) does not work on
  objects of type tvm::Object. Sometimes lldb cannot locate
  this Dump function inside a visitor context.

Signed-off-by: Wei Pan <weip@nvidia.com>
  • Loading branch information
wpan11nv committed Apr 1, 2020
1 parent afb8bf0 commit 3206cd5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions include/tvm/runtime/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ class Object {
return *this;
}

void dump() const;
std::string print() const;

protected:
// The fields of the base object cell.
/*! \brief Type index(tag) that indicates the type of the object. */
Expand Down Expand Up @@ -567,6 +570,9 @@ class ObjectRef {
/*! \brief type indicate the container type. */
using ContainerType = Object;

void dump() const;
std::string print() const;

protected:
/*! \brief Internal pointer that backs the reference. */
ObjectPtr<Object> data_;
Expand Down
20 changes: 20 additions & 0 deletions src/runtime/object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
#include "runtime_base.h"

namespace tvm {

// Forward declaration.
std::string PrettyPrint(const tvm::runtime::ObjectRef& node);

namespace runtime {

/*! \brief Type information */
Expand Down Expand Up @@ -203,6 +207,22 @@ uint32_t Object::TypeKey2Index(const std::string& key) {
return TypeContext::Global()->TypeKey2Index(key);
}

void Object::dump() const {
std::cerr << print();
}

std::string Object::print() const {
auto ref = GetRef<ObjectRef>(this);
return tvm::PrettyPrint(ref);
}

void ObjectRef::dump() const {
std::cerr << print();
}

std::string ObjectRef::print() const {
return tvm::PrettyPrint(*this);
}

TVM_REGISTER_GLOBAL("runtime.ObjectHash")
.set_body_typed([](ObjectRef obj) {
Expand Down

0 comments on commit 3206cd5

Please sign in to comment.