Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/arch/pass_infra.rst
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ Multiple ``PassInstrument`` instances can be registed into a single

class PassInstrument : public ObjectRef {
public:
TVM_DEFINE_OBJECT_REF_METHODS(PassInstrument, ObjectRef, PassInstrumentNode);
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(PassInstrument, ObjectRef, PassInstrumentNode);
};

} // namespace instrument
Expand Down
4 changes: 1 addition & 3 deletions docs/arch/runtime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,7 @@ Each ``Object`` subclass will override this to register its members. Here is an
namespace refl = tvm::ffi::reflection;
refl::ObjectDef<IntImmNode>().def_ro("value", &IntImmNode::value);
}

static constexpr const char* _type_key = "ir.IntImm";
TVM_DECLARE_FINAL_OBJECT_INFO(IntImmNode, PrimExprNode);
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.IntImm", IntImmNode, PrimExprNode);
};
// in cc file
TVM_FFI_STATIC_INIT_BLOCK({ IntImmNode::RegisterReflection(); });
Expand Down
6 changes: 2 additions & 4 deletions ffi/docs/guides/cpp_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@ class MyIntPairObj : public tvm::ffi::Object {

// Required: declare type information
// to register a dynamic type index through the system
static constexpr const char* _type_key = "example.MyIntPair";
// This macro registers the class with the FFI system to set up the right type index
TVM_FFI_DECLARE_FINAL_OBJECT_INFO(MyIntPairObj, tvm::ffi::Object);
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("example.MyIntPair", MyIntPairObj, tvm::ffi::Object);
Copy link
Contributor

Choose a reason for hiding this comment

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

We need formatting here.

};

void ExampleObjectPtr() {
Expand Down Expand Up @@ -138,7 +136,7 @@ class MyIntPair : public tvm::ffi::ObjectRef {

// Required: define object reference methods
// This macro provides the necessary methods for ObjectRef functionality
TVM_FFI_DEFINE_OBJECT_REF_METHODS(MyIntPair, tvm::ffi::ObjectRef, MyIntPairObj);
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(MyIntPair, tvm::ffi::ObjectRef, MyIntPairObj);
};

void ExampleObjectRef() {
Expand Down
5 changes: 2 additions & 3 deletions ffi/docs/guides/python_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,7 @@ public:
TestIntPairObj(int64_t a, int64_t b) : a(a), b(b) {}

// Required: declare type information
static constexpr const char* _type_key = "testing.TestIntPair";
TVM_FFI_DECLARE_FINAL_OBJECT_INFO(TestIntPairObj, tvm::ffi::Object);
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("testing.TestIntPair", TestIntPairObj, tvm::ffi::Object);
Copy link
Contributor

Choose a reason for hiding this comment

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

Ditto. In markdown we don't have auto formatting.

};

// Step 2: Define the reference wrapper (user-facing interface)
Expand All @@ -201,7 +200,7 @@ public:
}

// Required: define object reference methods
TVM_FFI_DEFINE_OBJECT_REF_METHODS(TestIntPair, tvm::ffi::ObjectRef, TestIntPairObj);
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(TestIntPair, tvm::ffi::ObjectRef, TestIntPairObj);
};

TVM_FFI_STATIC_INIT_BLOCK({
Expand Down
3 changes: 1 addition & 2 deletions ffi/include/tvm/ffi/container/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,8 @@ class ArrayObj : public Object, public details::InplaceArrayBase<ArrayObj, TVMFF

/// \cond Doxygen_Suppress
static constexpr const int32_t _type_index = TypeIndex::kTVMFFIArray;
static constexpr const char* _type_key = StaticTypeKey::kTVMFFIArray;
static const constexpr bool _type_final = true;
TVM_FFI_DECLARE_STATIC_OBJECT_INFO(ArrayObj, Object);
TVM_FFI_DECLARE_OBJECT_INFO_STATIC(StaticTypeKey::kTVMFFIArray, ArrayObj, Object);
/// \endcond

private:
Expand Down
3 changes: 1 addition & 2 deletions ffi/include/tvm/ffi/container/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,8 @@ class MapObj : public Object {

/// \cond Doxygen_Suppress
static constexpr const int32_t _type_index = TypeIndex::kTVMFFIMap;
static constexpr const char* _type_key = StaticTypeKey::kTVMFFIMap;
static const constexpr bool _type_final = true;
TVM_FFI_DECLARE_STATIC_OBJECT_INFO(MapObj, Object);
TVM_FFI_DECLARE_OBJECT_INFO_STATIC(StaticTypeKey::kTVMFFIMap, MapObj, Object);
/// \endcond

/*!
Expand Down
5 changes: 2 additions & 3 deletions ffi/include/tvm/ffi/container/shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ class ShapeObj : public Object, public TVMFFIShapeCell {

/// \cond Doxygen_Suppress
static constexpr const uint32_t _type_index = TypeIndex::kTVMFFIShape;
static constexpr const char* _type_key = StaticTypeKey::kTVMFFIShape;
TVM_FFI_DECLARE_STATIC_OBJECT_INFO(ShapeObj, Object);
TVM_FFI_DECLARE_OBJECT_INFO_STATIC(StaticTypeKey::kTVMFFIShape, ShapeObj, Object);
/// \endcond
};

Expand Down Expand Up @@ -212,7 +211,7 @@ class Shape : public ObjectRef {
int64_t Product() const { return get()->Product(); }

/// \cond Doxygen_Suppress
TVM_FFI_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(Shape, ObjectRef, ShapeObj);
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Shape, ObjectRef, ShapeObj);
/// \endcond

private:
Expand Down
5 changes: 2 additions & 3 deletions ffi/include/tvm/ffi/container/tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ class TensorObj : public Object, public DLTensor {
public:
/// \cond Doxygen_Suppress
static constexpr const uint32_t _type_index = TypeIndex::kTVMFFITensor;
static constexpr const char* _type_key = StaticTypeKey::kTVMFFITensor;
TVM_FFI_DECLARE_STATIC_OBJECT_INFO(TensorObj, Object);
TVM_FFI_DECLARE_OBJECT_INFO_STATIC(StaticTypeKey::kTVMFFITensor, TensorObj, Object);
/// \endcond

/*!
Expand Down Expand Up @@ -363,7 +362,7 @@ class Tensor : public ObjectRef {
DLManagedTensorVersioned* ToDLPackVersioned() const { return get_mutable()->ToDLPackVersioned(); }

/// \cond Doxygen_Suppress
TVM_FFI_DEFINE_OBJECT_REF_METHODS(Tensor, ObjectRef, TensorObj);
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(Tensor, ObjectRef, TensorObj);
/// \endcond

protected:
Expand Down
5 changes: 2 additions & 3 deletions ffi/include/tvm/ffi/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ class ErrorObj : public Object, public TVMFFIErrorCell {
public:
/// \cond Doxygen_Suppress
static constexpr const int32_t _type_index = TypeIndex::kTVMFFIError;
static constexpr const char* _type_key = "ffi.Error";
TVM_FFI_DECLARE_STATIC_OBJECT_INFO(ErrorObj, Object);
TVM_FFI_DECLARE_OBJECT_INFO_STATIC("ffi.Error", ErrorObj, Object);
/// \endcond
};

Expand Down Expand Up @@ -196,7 +195,7 @@ class Error : public ObjectRef, public std::exception {
}

/// \cond Doxygen_Suppress
TVM_FFI_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(Error, ObjectRef, ErrorObj);
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Error, ObjectRef, ErrorObj);
/// \endcond
};

Expand Down
6 changes: 3 additions & 3 deletions ffi/include/tvm/ffi/extra/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ class TVM_FFI_EXTRA_CXX_API ModuleObj : public Object {

/// \cond Doxygen_Suppress
static constexpr const int32_t _type_index = TypeIndex::kTVMFFIModule;
static constexpr const char* _type_key = StaticTypeKey::kTVMFFIModule;
static constexpr const bool _type_mutable = true;
static const constexpr bool _type_final = true;
TVM_FFI_DECLARE_STATIC_OBJECT_INFO(ModuleObj, Object);
TVM_FFI_DECLARE_OBJECT_INFO_STATIC(StaticTypeKey::kTVMFFIModule, ModuleObj, Object);
/// \endcond

protected:
Expand Down Expand Up @@ -234,7 +234,7 @@ class Module : public ObjectRef {
const ffi::TypedFunction<void(String, void*)>& callback);

/// \cond Doxygen_Suppress
TVM_FFI_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(Module, ObjectRef, ModuleObj);
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Module, ObjectRef, ModuleObj);
/// \endcond
};

Expand Down
5 changes: 2 additions & 3 deletions ffi/include/tvm/ffi/function.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ class FunctionObj : public Object, public TVMFFIFunctionCell {
}
/// \cond Doxygen_Suppress
static constexpr const uint32_t _type_index = TypeIndex::kTVMFFIFunction;
static constexpr const char* _type_key = StaticTypeKey::kTVMFFIFunction;
TVM_FFI_DECLARE_STATIC_OBJECT_INFO(FunctionObj, Object);
TVM_FFI_DECLARE_OBJECT_INFO_STATIC(StaticTypeKey::kTVMFFIFunction, FunctionObj, Object);
/// \endcond

protected:
Expand Down Expand Up @@ -594,7 +593,7 @@ class Function : public ObjectRef {
TVM_FFI_INLINE bool operator!=(std::nullptr_t) const { return data_ != nullptr; }

/// \cond Doxygen_Suppress
TVM_FFI_DEFINE_OBJECT_REF_METHODS(Function, ObjectRef, FunctionObj);
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(Function, ObjectRef, FunctionObj);
/// \endcond

class Registry;
Expand Down
106 changes: 48 additions & 58 deletions ffi/include/tvm/ffi/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ TVM_FFI_INLINE bool IsObjectInstance(int32_t object_type_index);
* 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_DECLARE_FINAL_OBJECT_INFO
* 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:
Expand All @@ -177,8 +177,8 @@ TVM_FFI_INLINE bool IsObjectInstance(int32_t object_type_index);
* 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_BASE_OBJECT_INFO for object classes that can be sub-classed.
* - Use TVM_FFI_DECLARE_FINAL_OBJECT_INFO for object classes that cannot be sub-classed.
* - 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.
Expand Down Expand Up @@ -276,7 +276,7 @@ class Object {
/*! \brief The structural equality and hash kind of the type */
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindUnsupported;
// The following functions are provided by macro
// TVM_FFI_DECLARE_BASE_OBJECT_INFO and TVM_DECLARE_FINAL_OBJECT_INFO
// TVM_FFI_DECLARE_OBJECT_INFO and TVM_FFI_DECLARE_OBJECT_INFO_FINAL
/*!
* \brief Get the runtime allocated type index of the type
* \note Getting this information may need dynamic calls into a global table.
Expand Down Expand Up @@ -885,20 +885,24 @@ struct ObjectPtrEqual {
/// \endcond

/*!
* \brief Helper macro to declare a object that comes with static type index.
* \brief Helper macro to declare object information with static type index.
*
* \param TypeKey The type key of the current type.
* \param TypeName The name of the current type.
* \param ParentType The name of the ParentType
*/
#define TVM_FFI_DECLARE_STATIC_OBJECT_INFO(TypeName, ParentType) \
static int32_t RuntimeTypeIndex() { return TypeName::_type_index; } \
#define TVM_FFI_DECLARE_OBJECT_INFO_STATIC(TypeKey, TypeName, ParentType) \
static constexpr const char* _type_key = TypeKey; \
static int32_t RuntimeTypeIndex() { return TypeName::_type_index; } \
TVM_FFI_REGISTER_STATIC_TYPE_INFO(TypeName, ParentType)

/*!
* \brief helper macro to declare a base object type that can be inherited.
* \brief Helper macro to declare object information with type key already defined in class.
*
* \param TypeName The name of the current type.
* \param ParentType The name of the ParentType
*/
#define TVM_FFI_DECLARE_BASE_OBJECT_INFO(TypeName, ParentType) \
#define TVM_FFI_DECLARE_OBJECT_INFO_PREDEFINED_TYPE_KEY(TypeName, ParentType) \
static constexpr int32_t _type_depth = ParentType::_type_depth + 1; \
static int32_t _GetOrAllocRuntimeTypeIndex() { \
static_assert(!ParentType::_type_final, "ParentType marked as final"); \
Expand All @@ -916,14 +920,27 @@ struct ObjectPtrEqual {
static inline int32_t _type_index = _GetOrAllocRuntimeTypeIndex()

/*!
* \brief helper macro to declare type information in a final class.
* \brief Helper macro to declare object information with dynamic type index.
*
* \param TypeKey The type key of the current type.
* \param TypeName The name of the current type.
* \param ParentType The name of the ParentType
*/
#define TVM_FFI_DECLARE_FINAL_OBJECT_INFO(TypeName, ParentType) \
static const constexpr int _type_child_slots [[maybe_unused]] = 0; \
static const constexpr bool _type_final [[maybe_unused]] = true; \
TVM_FFI_DECLARE_BASE_OBJECT_INFO(TypeName, ParentType)
#define TVM_FFI_DECLARE_OBJECT_INFO(TypeKey, TypeName, ParentType) \
static constexpr const char* _type_key = TypeKey; \
TVM_FFI_DECLARE_OBJECT_INFO_PREDEFINED_TYPE_KEY(TypeName, ParentType)

/*!
* \brief Helper macro to declare object information with dynamic type index and is final.
*
* \param TypeKey The type key of the current type.
* \param TypeName The name of the current type.
* \param ParentType The name of the ParentType
*/
#define TVM_FFI_DECLARE_OBJECT_INFO_FINAL(TypeKey, TypeName, ParentType) \
static const constexpr int _type_child_slots [[maybe_unused]] = 0; \
static const constexpr bool _type_final [[maybe_unused]] = true; \
TVM_FFI_DECLARE_OBJECT_INFO(TypeKey, TypeName, ParentType)

/*!
* \brief Define object reference methods.
Expand All @@ -935,13 +952,15 @@ struct ObjectPtrEqual {
* \note This macro also defines the default constructor that puts the ObjectRef
* in undefined state initially.
*/
#define TVM_FFI_DEFINE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName) \
TypeName() = default; \
explicit TypeName(::tvm::ffi::ObjectPtr<ObjectName> n) : ParentType(n) {} \
explicit TypeName(::tvm::ffi::UnsafeInit tag) : ParentType(tag) {} \
TVM_FFI_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TypeName) \
const ObjectName* operator->() const { return static_cast<const ObjectName*>(data_.get()); } \
const ObjectName* get() const { return operator->(); } \
#define TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(TypeName, ParentType, ObjectName) \
TypeName() = default; \
explicit TypeName(::tvm::ffi::ObjectPtr<ObjectName> n) : ParentType(n) {} \
explicit TypeName(::tvm::ffi::UnsafeInit tag) : ParentType(tag) {} \
TVM_FFI_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TypeName) \
using __PtrType = std::conditional_t<ObjectName::_type_mutable, ObjectName*, const ObjectName*>; \
__PtrType operator->() const { return static_cast<__PtrType>(data_.get()); } \
__PtrType get() const { return static_cast<__PtrType>(data_.get()); } \
static constexpr bool _type_is_nullable = true; \
using ContainerType = ObjectName

/*!
Expand All @@ -951,46 +970,17 @@ struct ObjectPtrEqual {
* \param ParentType The parent type of the objectref
* \param ObjectName The type name of the object.
*/
#define TVM_FFI_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName) \
explicit TypeName(::tvm::ffi::UnsafeInit tag) : ParentType(tag) {} \
TVM_FFI_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TypeName) \
const ObjectName* operator->() const { return static_cast<const ObjectName*>(data_.get()); } \
const ObjectName* get() const { return operator->(); } \
static constexpr bool _type_is_nullable = false; \
using ContainerType = ObjectName

/*!
* \brief Define object reference methods of whose content is mutable.
* \param TypeName The object type name
* \param ParentType The parent type of the objectref
* \param ObjectName The type name of the object.
* \note We recommend making objects immutable when possible.
* This macro is only reserved for objects that stores runtime states.
*/
#define TVM_FFI_DEFINE_MUTABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName) \
TypeName() = default; \
explicit TypeName(::tvm::ffi::UnsafeInit tag) : ParentType(tag) {} \
TVM_FFI_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TypeName) \
explicit TypeName(::tvm::ffi::ObjectPtr<ObjectName> n) : ParentType(n) {} \
ObjectName* operator->() const { return static_cast<ObjectName*>(data_.get()); } \
using ContainerType = ObjectName

/*!
* \brief Define object reference methods that is both not nullable and mutable.
*
* \param TypeName The object type name
* \param ParentType The parent type of the objectref
* \param ObjectName The type name of the object.
*/
#define TVM_FFI_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName) \
explicit TypeName(::tvm::ffi::UnsafeInit tag) : ParentType(tag) {} \
TVM_FFI_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TypeName) \
ObjectName* operator->() const { return static_cast<ObjectName*>(data_.get()); } \
ObjectName* get() const { return operator->(); } \
static constexpr bool _type_is_nullable = false; \
#define TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(TypeName, ParentType, ObjectName) \
explicit TypeName(::tvm::ffi::UnsafeInit tag) : ParentType(tag) {} \
TVM_FFI_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TypeName) \
using __PtrType = std::conditional_t<ObjectName::_type_mutable, ObjectName*, const ObjectName*>; \
__PtrType operator->() const { return static_cast<__PtrType>(data_.get()); } \
__PtrType get() const { return static_cast<__PtrType>(data_.get()); } \
static constexpr bool _type_is_nullable = false; \
using ContainerType = ObjectName

namespace details {

template <typename TargetType>
TVM_FFI_INLINE bool IsObjectInstance(int32_t object_type_index) {
static_assert(std::is_base_of_v<Object, TargetType>);
Expand Down
10 changes: 4 additions & 6 deletions ffi/include/tvm/ffi/reflection/access_path.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,8 @@ class AccessStepObj : public Object {
inline bool StepEqual(const AccessStep& other) const;

/// \cond Doxygen_Suppress
static constexpr const char* _type_key = "ffi.reflection.AccessStep";
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindConstTreeNode;
TVM_FFI_DECLARE_FINAL_OBJECT_INFO(AccessStepObj, Object);
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ffi.reflection.AccessStep", AccessStepObj, Object);
/// \endcond
};

Expand Down Expand Up @@ -162,7 +161,7 @@ class AccessStep : public ObjectRef {
}

/// \cond Doxygen_Suppress
TVM_FFI_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(AccessStep, ObjectRef, AccessStepObj);
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(AccessStep, ObjectRef, AccessStepObj);
/// \endcond
};

Expand Down Expand Up @@ -286,9 +285,8 @@ class AccessPathObj : public Object {
inline bool IsPrefixOf(const AccessPath& other) const;

/// \cond Doxygen_Suppress
static constexpr const char* _type_key = "ffi.reflection.AccessPath";
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindConstTreeNode;
TVM_FFI_DECLARE_FINAL_OBJECT_INFO(AccessPathObj, Object);
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ffi.reflection.AccessPath", AccessPathObj, Object);
/// \endcond

private:
Expand Down Expand Up @@ -358,7 +356,7 @@ class AccessPath : public ObjectRef {
}

/// \cond Doxygen_Suppress
TVM_FFI_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(AccessPath, ObjectRef, AccessPathObj);
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(AccessPath, ObjectRef, AccessPathObj);
/// \endcond

private:
Expand Down
Loading
Loading