23 #ifndef TVM_RUNTIME_OBJECT_H_ 24 #define TVM_RUNTIME_OBJECT_H_ 27 #include <tvm/runtime/logging.h> 30 #include <type_traits> 39 #ifndef TVM_OBJECT_ATOMIC_REF_COUNTER 40 #define TVM_OBJECT_ATOMIC_REF_COUNTER 1 43 #if TVM_OBJECT_ATOMIC_REF_COUNTER 45 #endif // TVM_OBJECT_ATOMIC_REF_COUNTER 178 std::string
GetTypeKey()
const {
return TypeIndex2Key(type_index_); }
188 template <
typename TargetType>
189 inline bool IsInstance()
const;
194 inline bool unique()
const;
200 static std::string TypeIndex2Key(uint32_t tindex);
206 static size_t TypeIndex2KeyHash(uint32_t tindex);
212 static uint32_t TypeKey2Index(
const std::string& key);
214 #if TVM_OBJECT_ATOMIC_REF_COUNTER 220 static constexpr
const char* _type_key =
"runtime.Object";
226 static constexpr
bool _type_final =
false;
227 static constexpr uint32_t _type_child_slots = 0;
228 static constexpr
bool _type_child_slots_can_overflow =
true;
230 static constexpr
bool _type_has_method_visit_attrs =
true;
231 static constexpr
bool _type_has_method_sequal_reduce =
false;
232 static constexpr
bool _type_has_method_shash_reduce =
false;
259 uint32_t type_index_{0};
267 FDeleter deleter_ =
nullptr;
271 "RefCounter ABI check.");
290 static uint32_t GetOrAllocRuntimeTypeIndex(
const std::string& key, uint32_t static_tindex,
291 uint32_t parent_tindex, uint32_t type_child_slots,
292 bool type_child_slots_can_overflow);
296 inline void IncRef();
301 inline void DecRef();
308 inline int use_count()
const;
314 bool DerivedFrom(uint32_t parent_tindex)
const;
321 friend class ObjectInternal;
336 template <
typename RelayRefType,
typename ObjectType>
347 template <
typename SubRef,
typename BaseRef>
348 inline SubRef
Downcast(BaseRef ref);
355 template <
typename T>
372 template <
typename U>
375 static_assert(std::is_base_of<T, U>::value,
376 "can only assign of child class ObjectPtr to parent");
383 : data_(other.data_) {
384 other.data_ =
nullptr;
390 template <
typename Y>
392 : data_(other.data_) {
393 static_assert(std::is_base_of<T, Y>::value,
394 "can only assign of child class ObjectPtr to parent");
395 other.data_ =
nullptr;
404 std::swap(data_, other.data_);
409 T*
get()
const {
return static_cast<T*
>(data_); }
443 if (data_ !=
nullptr) {
449 int use_count()
const {
return data_ !=
nullptr ? data_->use_count() : 0; }
451 bool unique()
const {
return data_ !=
nullptr && data_->use_count() == 1; }
457 bool operator==(std::nullptr_t null)
const {
return data_ ==
nullptr; }
459 bool operator!=(std::nullptr_t null)
const {
return data_ !=
nullptr; }
469 if (data !=
nullptr) {
497 template <
typename RelayRefType,
typename ObjType>
499 template <
typename BaseType,
typename ObjType>
537 bool defined()
const {
return data_ !=
nullptr; }
539 const Object*
get()
const {
return data_.get(); }
543 bool unique()
const {
return data_.unique(); }
557 template <
typename ObjectType>
558 inline const ObjectType* as()
const;
563 static constexpr
bool _type_is_nullable =
true;
576 template <
typename T>
578 return T(std::move(ref.
data_));
592 template <
typename ObjectType>
600 friend class ObjectInternal;
601 template <
typename SubRef,
typename BaseRef>
602 friend SubRef
Downcast(BaseRef ref);
613 template <
typename BaseType,
typename ObjectType>
620 template <
typename T>
622 return std::hash<Object*>()(a.
get());
630 template <
typename T>
641 #define TVM_DECLARE_BASE_OBJECT_INFO(TypeName, ParentType) \ 642 static_assert(!ParentType::_type_final, "ParentObj marked as final"); \ 643 static uint32_t RuntimeTypeIndex() { \ 644 static_assert(TypeName::_type_child_slots == 0 || ParentType::_type_child_slots == 0 || \ 645 TypeName::_type_child_slots < ParentType::_type_child_slots, \ 646 "Need to set _type_child_slots when parent specifies it."); \ 647 if (TypeName::_type_index != ::tvm::runtime::TypeIndex::kDynamic) { \ 648 return TypeName::_type_index; \ 650 return _GetOrAllocRuntimeTypeIndex(); \ 652 static uint32_t _GetOrAllocRuntimeTypeIndex() { \ 653 static uint32_t tindex = Object::GetOrAllocRuntimeTypeIndex( \ 654 TypeName::_type_key, TypeName::_type_index, ParentType::_GetOrAllocRuntimeTypeIndex(), \ 655 TypeName::_type_child_slots, TypeName::_type_child_slots_can_overflow); \ 664 #define TVM_DECLARE_FINAL_OBJECT_INFO(TypeName, ParentType) \ 665 static const constexpr bool _type_final = true; \ 666 static const constexpr int _type_child_slots = 0; \ 667 TVM_DECLARE_BASE_OBJECT_INFO(TypeName, ParentType) 670 #if defined(__GNUC__) 671 #define TVM_ATTRIBUTE_UNUSED __attribute__((unused)) 673 #define TVM_ATTRIBUTE_UNUSED 676 #define TVM_STR_CONCAT_(__x, __y) __x##__y 677 #define TVM_STR_CONCAT(__x, __y) TVM_STR_CONCAT_(__x, __y) 679 #define TVM_OBJECT_REG_VAR_DEF static TVM_ATTRIBUTE_UNUSED uint32_t __make_Object_tid 687 #define TVM_REGISTER_OBJECT_TYPE(TypeName) \ 688 TVM_STR_CONCAT(TVM_OBJECT_REG_VAR_DEF, __COUNTER__) = TypeName::_GetOrAllocRuntimeTypeIndex() 694 #define TVM_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TypeName) \ 695 TypeName(const TypeName& other) = default; \ 696 TypeName(TypeName&& other) = default; \ 697 TypeName& operator=(const TypeName& other) = default; \ 698 TypeName& operator=(TypeName&& other) = default; 706 #define TVM_DEFINE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName) \ 707 TypeName() = default; \ 708 explicit TypeName(::tvm::runtime::ObjectPtr<::tvm::runtime::Object> n) : ParentType(n) {} \ 709 TVM_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TypeName); \ 710 const ObjectName* operator->() const { return static_cast<const ObjectName*>(data_.get()); } \ 711 const ObjectName* get() const { return operator->(); } \ 712 using ContainerType = ObjectName; 721 #define TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName) \ 722 explicit TypeName(::tvm::runtime::ObjectPtr<::tvm::runtime::Object> n) : ParentType(n) {} \ 723 TVM_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TypeName); \ 724 const ObjectName* operator->() const { return static_cast<const ObjectName*>(data_.get()); } \ 725 const ObjectName* get() const { return operator->(); } \ 726 static constexpr bool _type_is_nullable = false; \ 727 using ContainerType = ObjectName; 737 #define TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName) \ 738 TypeName() = default; \ 739 TVM_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TypeName); \ 740 explicit TypeName(::tvm::runtime::ObjectPtr<::tvm::runtime::Object> n) : ParentType(n) {} \ 741 ObjectName* operator->() const { return static_cast<ObjectName*>(data_.get()); } \ 742 using ContainerType = ObjectName; 751 #define TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName) \ 752 explicit TypeName(::tvm::runtime::ObjectPtr<::tvm::runtime::Object> n) : ParentType(n) {} \ 753 TVM_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TypeName); \ 754 ObjectName* operator->() const { return static_cast<ObjectName*>(data_.get()); } \ 755 ObjectName* get() const { return operator->(); } \ 756 static constexpr bool _type_is_nullable = false; \ 757 using ContainerType = ObjectName; 778 #define TVM_DEFINE_OBJECT_REF_COW_METHOD(ObjectName) \ 779 ObjectName* CopyOnWrite() { \ 780 ICHECK(data_ != nullptr); \ 781 if (!data_.unique()) { \ 782 auto n = make_object<ObjectName>(*(operator->())); \ 783 ObjectPtr<Object>(std::move(n)).swap(data_); \ 785 return static_cast<ObjectName*>(data_.get()); \ 790 #if TVM_OBJECT_ATOMIC_REF_COUNTER 792 inline void Object::IncRef() { ref_counter_.fetch_add(1, std::memory_order_relaxed); }
795 if (ref_counter_.fetch_sub(1, std::memory_order_release) == 1) {
796 std::atomic_thread_fence(std::memory_order_acquire);
797 if (this->deleter_ !=
nullptr) {
798 (*this->deleter_)(
this);
803 inline int Object::use_count()
const {
return ref_counter_.load(std::memory_order_relaxed); }
810 if (--ref_counter_ == 0) {
811 if (this->deleter_ !=
nullptr) {
812 (*this->deleter_)(
this);
817 inline int Object::use_count()
const {
return ref_counter_; }
819 #endif // TVM_OBJECT_ATOMIC_REF_COUNTER 821 template <
typename TargetType>
823 const Object*
self =
this;
826 if (
self !=
nullptr) {
828 if (std::is_same<TargetType, Object>::value)
return true;
829 if (TargetType::_type_final) {
832 return self->
type_index_ == TargetType::RuntimeTypeIndex();
836 uint32_t begin = TargetType::RuntimeTypeIndex();
838 if (TargetType::_type_child_slots != 0) {
839 uint32_t end = begin + TargetType::_type_child_slots;
840 if (self->type_index_ >= begin && self->type_index_ < end)
return true;
842 if (self->type_index_ == begin)
return true;
844 if (!TargetType::_type_child_slots_can_overflow)
return false;
846 if (self->type_index_ < TargetType::RuntimeTypeIndex())
return false;
848 return self->DerivedFrom(TargetType::RuntimeTypeIndex());
857 template <
typename ObjectType>
859 if (data_ !=
nullptr && data_->IsInstance<ObjectType>()) {
860 return static_cast<ObjectType*
>(data_.get());
866 template <
typename RefType,
typename ObjType>
867 inline RefType
GetRef(
const ObjType* ptr) {
868 static_assert(std::is_base_of<typename RefType::ContainerType, ObjType>::value,
869 "Can only cast to the ref of same container type");
870 if (!RefType::_type_is_nullable) {
871 ICHECK(ptr !=
nullptr);
873 return RefType(
ObjectPtr<Object>(const_cast<Object*>(static_cast<const Object*>(ptr))));
876 template <
typename BaseType,
typename ObjType>
878 static_assert(std::is_base_of<BaseType, ObjType>::value,
879 "Can only cast to the ref of same container type");
883 template <
typename SubRef,
typename BaseRef>
886 ICHECK(ref->template IsInstance<typename SubRef::ContainerType>())
887 <<
"Downcast from " << ref->GetTypeKey() <<
" to " << SubRef::ContainerType::_type_key
890 ICHECK(SubRef::_type_is_nullable) <<
"Downcast from nullptr to not nullable reference of " 891 << SubRef::ContainerType::_type_key;
893 return SubRef(std::move(ref.data_));
899 #endif // TVM_RUNTIME_OBJECT_H_ runtime::Map.
Definition: object.h:70
Return Value container, Unlike TVMArgValue, which only holds reference and do not delete the underlyi...
Definition: packed_func.h:734
Object(const Object &other)
Definition: object.h:245
A custom smart pointer for Object.
Definition: object.h:356
runtime::Module.
Definition: object.h:62
ObjectRef equal functor.
Definition: object.h:627
Internal base class to handle conversion to POD values.
Definition: packed_func.h:485
SubRef Downcast(BaseRef ref)
Downcast a base reference type to a more specific type.
Definition: object.h:884
bool operator!=(std::nullptr_t null) const
Definition: object.h:459
void reset()
reset the content of ptr to be nullptr
Definition: object.h:442
size_t operator()(const ObjectRef &a) const
Definition: object.h:618
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:36
runtime::String.
Definition: object.h:66
Object()
Definition: object.h:239
bool operator==(const ObjectRef &other) const
Comparator.
Definition: object.h:521
void IncRef()
developer function, increases reference counter.
Definition: object.h:792
size_t GetTypeKeyHash() const
Definition: object.h:182
bool operator!=(const ObjectRef &other) const
Comparator.
Definition: object.h:527
static ObjectPtr< ObjectType > GetDataPtr(const ObjectRef &ref)
Internal helper function get data_ as ObjectPtr of ObjectType.
Definition: object.h:593
base class of all object containers.
Definition: object.h:165
RelayRefType GetRef(const ObjectType *ptr)
Get a reference type from a raw object ptr type.
static void FFIClearAfterMove(ObjectRef *ref)
Clear the object ref data field without DecRef after we successfully moved the field.
Definition: object.h:585
Namespace for the list of type index.
Definition: object.h:55
void DecRef()
developer function, decrease reference counter.
Definition: object.h:794
bool IsInstance() const
Definition: object.h:822
static uint32_t RuntimeTypeIndex()
Definition: object.h:223
bool operator<(const ObjectRef &other) const
Comparator.
Definition: object.h:533
Internal auxiliary struct for TypedPackedFunc to indicate a movable argument.
Definition: packed_func.h:650
ObjectPtr< BaseType > GetObjectPtr(ObjectType *ptr)
Get an object ptr type from a raw object ptr.
bool defined() const
Definition: object.h:537
Object(Object &&other)
Definition: object.h:247
bool operator()(const ObjectRef &a, const ObjectRef &b) const
Definition: object.h:628
ObjectRef hash functor.
Definition: object.h:617
std::atomic< int32_t > RefCounterType
Definition: object.h:215
bool unique() const
Definition: object.h:855
T & operator*() const
Definition: object.h:417
ObjectPtr< Object > data_
Internal pointer that backs the reference.
Definition: object.h:567
const Object * operator->() const
Definition: object.h:541
T * operator->() const
Definition: object.h:413
bool same_as(const ObjectRef &other) const
Comparator.
Definition: object.h:515
static T DowncastNoCheck(ObjectRef ref)
Internal helper function downcast a ref without check.
Definition: object.h:577
ObjectPtr< T > & operator=(ObjectPtr< T > &&other)
move assignment
Definition: object.h:436
bool unique() const
Definition: object.h:543
ObjectPtr(std::nullptr_t)
default constructor
Definition: object.h:361
Object & operator=(const Object &other)
Definition: object.h:249
runtime::ShapeTuple.
Definition: object.h:72
static uint32_t _GetOrAllocRuntimeTypeIndex()
Definition: object.h:222
bool operator==(std::nullptr_t null) const
Definition: object.h:457
ObjectRef(ObjectPtr< Object > data)
Constructor from existing object ptr.
Definition: object.h:509
Base class of all object reference.
Definition: object.h:504
Base class of object allocators that implements make. Use curiously recurring template pattern...
Definition: memory.h:60
std::string GetTypeKey() const
Definition: object.h:178
Root object type.
Definition: object.h:58
T * get() const
Definition: object.h:409
ObjectPtr(const ObjectPtr< T > &other)
copy constructor
Definition: object.h:366
size_t operator()(const ObjectPtr< T > &a) const
Definition: object.h:621
uint32_t type_index() const
Definition: object.h:173
bool unique() const
Definition: object.h:451
runtime::Array.
Definition: object.h:68
void swap(ObjectPtr< T > &other)
Swap this array with another Object.
Definition: object.h:403
size_t operator()(const ObjectPtr< T > &a, const ObjectPtr< T > &b) const
Definition: object.h:631
A single argument value to PackedFunc. Containing both type_code and TVMValue.
Definition: packed_func.h:583
int use_count() const
Definition: object.h:449
ObjectPtr()
default constructor
Definition: object.h:359
ObjectPtr(ObjectPtr< Y > &&other)
move constructor
Definition: object.h:391
~ObjectPtr()
destructor
Definition: object.h:398
Definition: packed_func.h:1257
uint32_t type_index_
Type index(tag) that indicates the type of the object.
Definition: object.h:259
int use_count() const
Definition: object.h:545
Object & operator=(Object &&other)
Definition: object.h:252
bool operator!=(const ObjectPtr< T > &other) const
Definition: object.h:455
const ObjectType * as() const
Try to downcast the internal Object to a raw pointer of a corresponding type.
Definition: object.h:858
bool operator==(const ObjectPtr< T > &other) const
Definition: object.h:453
Object * get_mutable() const
Definition: object.h:569
Managed reference to RelayRefTypeNode.
Definition: type.h:557
ObjectPtr(ObjectPtr< T > &&other)
move constructor
Definition: object.h:382
ObjectPtr(const ObjectPtr< U > &other)
copy constructor
Definition: object.h:373
Type index is allocated during runtime.
Definition: object.h:78
ObjectPtr< T > & operator=(const ObjectPtr< T > &other)
copy assignment
Definition: object.h:425
runtime::NDArray.
Definition: object.h:64