From ec1d7fc34236b0d969a572a7243e80236fda78fd Mon Sep 17 00:00:00 2001 From: Wei Pan Date: Fri, 27 Mar 2020 09:23:35 -0700 Subject: [PATCH] [IR][Debug] Add dump and print for debugging (NFC) - 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 --- include/tvm/runtime/object.h | 6 ++++++ src/runtime/object.cc | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/include/tvm/runtime/object.h b/include/tvm/runtime/object.h index 717bf5e72fc47..c03cc5ed76940 100644 --- a/include/tvm/runtime/object.h +++ b/include/tvm/runtime/object.h @@ -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. */ @@ -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 data_; diff --git a/src/runtime/object.cc b/src/runtime/object.cc index 0d85b9dab42cc..04e64d2963cde 100644 --- a/src/runtime/object.cc +++ b/src/runtime/object.cc @@ -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 */ @@ -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(this); + return std::move(tvm::PrettyPrint(ref)); +} + +void ObjectRef::dump() const { + std::cerr << print(); +} + +std::string ObjectRef::print() const { + return std::move(tvm::PrettyPrint(*this)); +} TVM_REGISTER_GLOBAL("runtime.ObjectHash") .set_body_typed([](ObjectRef obj) {