Class Object#

Inheritance Relationships#

Derived Types#

Class Documentation#

class Object#

Base class of all object containers.

Sub-class of objects should declare the following static constexpr fields:

  • _type_index: Static type index of the object, if assigned to TypeIndex::kTVMFFIDynObject the type index will be assigned during runtime. Runtime type index can be accessed by ObjectType::TypeIndex();

  • _type_key: The unique string identifier of the type.

  • _type_final: Whether the type is terminal type(there is no subclass of the type in the object system). This field is automatically set by macro TVM_FFI_DECLARE_OBJECT_INFO_FINAL It is still OK to sub-class a terminal object type T and construct it using make_object. But IsInstance check will only show that the object type is T(instead of the sub-class).

  • _type_mutable: Whether we would like to expose cast to non-constant pointer ObjectType* from Any/AnyView. By default, we set to false so it is not exposed.

  • _type_s_eq_hash_subclass_kind_fixed: Whether every subclass must retain this type’s structural equality and hash kind. By default, this is false so downstream subclasses may select their own kind.

The following two fields are necessary for base classes that can be sub-classed.

  • _type_child_slots: Number of reserved type index slots for child classes. Used for runtime optimization for type checking in IsInstance. If an object’s type_index is within range of [type_index, type_index + _type_child_slots] Then the object can be quickly decided as sub-class of the current object class. If not, a fallback mechanism is used to check the global type table. Recommendation: set to estimate number of children needed.

  • _type_child_slots_can_overflow: Whether we can add additional child classes even if the number of child classes exceeds the _type_child_slots. A fallback mechanism to check type table will be used. Recommendation: set to false for optimal runtime speed if we know exact number of children.

Two macros are used to declare helper functions in the object:

  • Use TVM_FFI_DECLARE_OBJECT_INFO for object classes that can be sub-classed.

  • Use TVM_FFI_DECLARE_OBJECT_INFO_FINAL for object classes that cannot be sub-classed.

New objects can be created using make_object function. Which will automatically populate the type_index and deleter of the object.

Subclassed by tvm::ffi::EnumObj, tvm::ffi::EnumStateObj, tvm::ffi::ErrorObj, tvm::ffi::FunctionObj, tvm::ffi::MapBaseObj, tvm::ffi::ModuleObj, tvm::ffi::SeqBaseObj, tvm::ffi::ShapeObj, tvm::ffi::StructuralKeyObj, tvm::ffi::StructuralMutatorObj, tvm::ffi::StructuralVisitorObj, tvm::ffi::TensorObj, tvm::ffi::VisitErrorContextObj, tvm::ffi::VisitInterruptObj, tvm::ffi::details::BigIntObj, tvm::ffi::details::BytesObjBase, tvm::ffi::reflection::AccessPathObj, tvm::ffi::reflection::AccessStepObj

Public Functions

inline Object()#
template<typename TargetType>
inline bool IsInstance() const#

Check if the object is an instance of TargetType.

Template Parameters:

TargetType – The target type to be checked.

Returns:

Whether the target type is true.

inline int32_t type_index() const#
Returns:

The internal runtime type index of the object.

inline std::string GetTypeKey() const#

Note

this operation is expensive, can be used for error reporting.

Returns:

the type key of the object.

inline uint64_t GetTypeKeyHash() const#
Returns:

A hash value of the return of GetTypeKey.

inline bool unique() const#

Note

Checking both weak and strong count is needed to ensure correctness in decisions such as copy-on-write in multi-threaded setting.

Returns:

Whether the object has one strong reference and no external weak references.

inline uint64_t use_count() const#

Note

We use STL style naming to be consistent with known API in shared_ptr.

Returns:

The usage count of the cell.

Public Static Functions

static inline std::string TypeIndex2Key(int32_t tindex)#

Get the type key of the corresponding index from runtime.

Parameters:

tindex – The type index.

Returns:

the result.

static inline int32_t RuntimeTypeIndex() noexcept#

Get the runtime allocated type index of the type.

Note

Getting this information may need dynamic calls into a global table.

static inline int32_t _GetOrAllocRuntimeTypeIndex()#

Internal function to get or allocate a runtime index.

Public Static Attributes

static constexpr const char *_type_key = StaticTypeKey::kTVMFFIObject#

The type key of the class.

static constexpr bool _type_final = false#

Whether the class is final.

static constexpr bool _type_mutable = false#

Whether allow mutable access to fields.

static constexpr uint32_t _type_child_slots = 0#

The number of child slots of the class to pre-allocate to this type.

static constexpr bool _type_child_slots_can_overflow = true#

Whether allow additional children beyond pre-specified by _type_child_slots.

static constexpr int32_t _type_index = TypeIndex::kTVMFFIObject#

The static type index of the class.

static constexpr int32_t _type_depth = 0#

The static depth of the class in the object hierarchy.

static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindUnsupported#

The structural equality and hash kind of the type.

static constexpr bool _type_s_eq_hash_subclass_kind_fixed = false#

Whether subclasses must retain this type’s structural equality and hash kind.

Protected Attributes

TVMFFIObject header_#

header field that is the common prefix of all objects