Skip to content

Commit abc8ae8

Browse files
authored
[FFI][REFACTOR] Streamline Object Declare Macros (#18289)
1 parent c655f14 commit abc8ae8

211 files changed

Lines changed: 1179 additions & 1966 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/arch/pass_infra.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ Multiple ``PassInstrument`` instances can be registed into a single
451451
452452
class PassInstrument : public ObjectRef {
453453
public:
454-
TVM_DEFINE_OBJECT_REF_METHODS(PassInstrument, ObjectRef, PassInstrumentNode);
454+
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(PassInstrument, ObjectRef, PassInstrumentNode);
455455
};
456456

457457
} // namespace instrument

docs/arch/runtime.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,7 @@ Each ``Object`` subclass will override this to register its members. Here is an
227227
namespace refl = tvm::ffi::reflection;
228228
refl::ObjectDef<IntImmNode>().def_ro("value", &IntImmNode::value);
229229
}
230-
231-
static constexpr const char* _type_key = "ir.IntImm";
232-
TVM_DECLARE_FINAL_OBJECT_INFO(IntImmNode, PrimExprNode);
230+
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.IntImm", IntImmNode, PrimExprNode);
233231
};
234232
// in cc file
235233
TVM_FFI_STATIC_INIT_BLOCK({ IntImmNode::RegisterReflection(); });

ffi/docs/guides/cpp_guide.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,7 @@ class MyIntPairObj : public tvm::ffi::Object {
105105

106106
// Required: declare type information
107107
// to register a dynamic type index through the system
108-
static constexpr const char* _type_key = "example.MyIntPair";
109-
// This macro registers the class with the FFI system to set up the right type index
110-
TVM_FFI_DECLARE_FINAL_OBJECT_INFO(MyIntPairObj, tvm::ffi::Object);
108+
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("example.MyIntPair", MyIntPairObj, tvm::ffi::Object);
111109
};
112110

113111
void ExampleObjectPtr() {
@@ -138,7 +136,7 @@ class MyIntPair : public tvm::ffi::ObjectRef {
138136
139137
// Required: define object reference methods
140138
// This macro provides the necessary methods for ObjectRef functionality
141-
TVM_FFI_DEFINE_OBJECT_REF_METHODS(MyIntPair, tvm::ffi::ObjectRef, MyIntPairObj);
139+
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(MyIntPair, tvm::ffi::ObjectRef, MyIntPairObj);
142140
};
143141
144142
void ExampleObjectRef() {

ffi/docs/guides/python_guide.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,7 @@ public:
188188
TestIntPairObj(int64_t a, int64_t b) : a(a), b(b) {}
189189

190190
// Required: declare type information
191-
static constexpr const char* _type_key = "testing.TestIntPair";
192-
TVM_FFI_DECLARE_FINAL_OBJECT_INFO(TestIntPairObj, tvm::ffi::Object);
191+
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("testing.TestIntPair", TestIntPairObj, tvm::ffi::Object);
193192
};
194193

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

203202
// Required: define object reference methods
204-
TVM_FFI_DEFINE_OBJECT_REF_METHODS(TestIntPair, tvm::ffi::ObjectRef, TestIntPairObj);
203+
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(TestIntPair, tvm::ffi::ObjectRef, TestIntPairObj);
205204
};
206205

207206
TVM_FFI_STATIC_INIT_BLOCK({

ffi/include/tvm/ffi/container/array.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,8 @@ class ArrayObj : public Object, public details::InplaceArrayBase<ArrayObj, TVMFF
157157

158158
/// \cond Doxygen_Suppress
159159
static constexpr const int32_t _type_index = TypeIndex::kTVMFFIArray;
160-
static constexpr const char* _type_key = StaticTypeKey::kTVMFFIArray;
161160
static const constexpr bool _type_final = true;
162-
TVM_FFI_DECLARE_STATIC_OBJECT_INFO(ArrayObj, Object);
161+
TVM_FFI_DECLARE_OBJECT_INFO_STATIC(StaticTypeKey::kTVMFFIArray, ArrayObj, Object);
163162
/// \endcond
164163

165164
private:

ffi/include/tvm/ffi/container/map.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,8 @@ class MapObj : public Object {
7373

7474
/// \cond Doxygen_Suppress
7575
static constexpr const int32_t _type_index = TypeIndex::kTVMFFIMap;
76-
static constexpr const char* _type_key = StaticTypeKey::kTVMFFIMap;
7776
static const constexpr bool _type_final = true;
78-
TVM_FFI_DECLARE_STATIC_OBJECT_INFO(MapObj, Object);
77+
TVM_FFI_DECLARE_OBJECT_INFO_STATIC(StaticTypeKey::kTVMFFIMap, MapObj, Object);
7978
/// \endcond
8079

8180
/*!

ffi/include/tvm/ffi/container/shape.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ class ShapeObj : public Object, public TVMFFIShapeCell {
5353

5454
/// \cond Doxygen_Suppress
5555
static constexpr const uint32_t _type_index = TypeIndex::kTVMFFIShape;
56-
static constexpr const char* _type_key = StaticTypeKey::kTVMFFIShape;
57-
TVM_FFI_DECLARE_STATIC_OBJECT_INFO(ShapeObj, Object);
56+
TVM_FFI_DECLARE_OBJECT_INFO_STATIC(StaticTypeKey::kTVMFFIShape, ShapeObj, Object);
5857
/// \endcond
5958
};
6059

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

214213
/// \cond Doxygen_Suppress
215-
TVM_FFI_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(Shape, ObjectRef, ShapeObj);
214+
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Shape, ObjectRef, ShapeObj);
216215
/// \endcond
217216

218217
private:

ffi/include/tvm/ffi/container/tensor.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ class TensorObj : public Object, public DLTensor {
121121
public:
122122
/// \cond Doxygen_Suppress
123123
static constexpr const uint32_t _type_index = TypeIndex::kTVMFFITensor;
124-
static constexpr const char* _type_key = StaticTypeKey::kTVMFFITensor;
125-
TVM_FFI_DECLARE_STATIC_OBJECT_INFO(TensorObj, Object);
124+
TVM_FFI_DECLARE_OBJECT_INFO_STATIC(StaticTypeKey::kTVMFFITensor, TensorObj, Object);
126125
/// \endcond
127126

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

365364
/// \cond Doxygen_Suppress
366-
TVM_FFI_DEFINE_OBJECT_REF_METHODS(Tensor, ObjectRef, TensorObj);
365+
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(Tensor, ObjectRef, TensorObj);
367366
/// \endcond
368367

369368
protected:

ffi/include/tvm/ffi/error.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ class ErrorObj : public Object, public TVMFFIErrorCell {
8787
public:
8888
/// \cond Doxygen_Suppress
8989
static constexpr const int32_t _type_index = TypeIndex::kTVMFFIError;
90-
static constexpr const char* _type_key = "ffi.Error";
91-
TVM_FFI_DECLARE_STATIC_OBJECT_INFO(ErrorObj, Object);
90+
TVM_FFI_DECLARE_OBJECT_INFO_STATIC("ffi.Error", ErrorObj, Object);
9291
/// \endcond
9392
};
9493

@@ -196,7 +195,7 @@ class Error : public ObjectRef, public std::exception {
196195
}
197196

198197
/// \cond Doxygen_Suppress
199-
TVM_FFI_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(Error, ObjectRef, ErrorObj);
198+
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Error, ObjectRef, ErrorObj);
200199
/// \endcond
201200
};
202201

ffi/include/tvm/ffi/extra/module.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ class TVM_FFI_EXTRA_CXX_API ModuleObj : public Object {
146146

147147
/// \cond Doxygen_Suppress
148148
static constexpr const int32_t _type_index = TypeIndex::kTVMFFIModule;
149-
static constexpr const char* _type_key = StaticTypeKey::kTVMFFIModule;
149+
static constexpr const bool _type_mutable = true;
150150
static const constexpr bool _type_final = true;
151-
TVM_FFI_DECLARE_STATIC_OBJECT_INFO(ModuleObj, Object);
151+
TVM_FFI_DECLARE_OBJECT_INFO_STATIC(StaticTypeKey::kTVMFFIModule, ModuleObj, Object);
152152
/// \endcond
153153

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

236236
/// \cond Doxygen_Suppress
237-
TVM_FFI_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(Module, ObjectRef, ModuleObj);
237+
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Module, ObjectRef, ModuleObj);
238238
/// \endcond
239239
};
240240

0 commit comments

Comments
 (0)