@@ -59,7 +59,7 @@ class TypeTable {
5959 /* ! \brief type methods informaton */
6060 std::vector<TVMFFIMethodInfo> type_methods_data;
6161 /* ! \brief extra information */
62- TVMFFITypeExtraInfo extra_info_data ;
62+ TVMFFITypeMetadata metadata_data ;
6363 // NOTE: the indices in [index, index + num_reserved_slots) are
6464 // reserved for the child-class of this type.
6565 /* ! \brief Total number of slots reserved for the type and its children. */
@@ -100,10 +100,14 @@ class TypeTable {
100100 this ->num_methods = 0 ;
101101 this ->fields = nullptr ;
102102 this ->methods = nullptr ;
103- this ->extra_info = nullptr ;
103+ this ->metadata = nullptr ;
104104 }
105105 };
106106
107+ struct TypeAttrColumnData : public TVMFFITypeAttrColumn {
108+ std::vector<Any> data_;
109+ };
110+
107111 int32_t GetOrAllocTypeIndex (String type_key, int32_t static_type_index, int32_t type_depth,
108112 int32_t num_child_slots, bool child_slots_can_overflow,
109113 int32_t parent_type_index) {
@@ -219,19 +223,49 @@ class TypeTable {
219223 entry->num_methods = static_cast <int32_t >(entry->type_methods_data .size ());
220224 }
221225
222- void RegisterTypeExtraInfo (int32_t type_index, const TVMFFITypeExtraInfo* extra_info ) {
226+ void RegisterTypeMetadata (int32_t type_index, const TVMFFITypeMetadata* metadata ) {
223227 Entry* entry = GetTypeEntry (type_index);
224- if (entry->extra_info != nullptr ) {
228+ if (entry->metadata != nullptr ) {
225229 TVM_FFI_LOG_AND_THROW (RuntimeError)
226230 << " Overriding " << ToStringView (entry->type_key ) << " , possible causes:\n "
227231 << " - two ObjectDef<T>() calls for the same T \n "
228232 << " - when we forget to assign _type_key to ObjectRef<Y> that inherits from T\n "
229233 << " - another type with the same key is already registered\n "
230234 << " Cross check the reflection registration." ;
231235 }
232- entry->extra_info_data = *extra_info;
233- entry->extra_info_data .doc = this ->CopyString (extra_info->doc );
234- entry->extra_info = &(entry->extra_info_data );
236+ entry->metadata_data = *metadata;
237+ entry->metadata_data .doc = this ->CopyString (metadata->doc );
238+ entry->metadata = &(entry->metadata_data );
239+ }
240+
241+ void RegisterTypeAttr (int32_t type_index, const TVMFFIByteArray* name, const TVMFFIAny* value) {
242+ AnyView value_view = AnyView::CopyFromTVMFFIAny (*value);
243+ String name_str (*name);
244+ size_t column_index = 0 ;
245+ auto it = type_attr_name_to_column_index_.find (name_str);
246+ if (it == type_attr_name_to_column_index_.end ()) {
247+ column_index = type_attr_columns_.size ();
248+ type_attr_columns_.emplace_back (std::make_unique<TypeAttrColumnData>());
249+ type_attr_name_to_column_index_.Set (name_str, column_index);
250+ }
251+ TypeAttrColumnData* column = type_attr_columns_[column_index].get ();
252+ if (column->data_ .size () < static_cast <size_t >(type_index + 1 )) {
253+ column->data_ .resize (type_index + 1 , Any (nullptr ));
254+ column->data = reinterpret_cast <const TVMFFIAny*>(column->data_ .data ());
255+ column->size = column->data_ .size ();
256+ }
257+ if (type_index == kTVMFFINone ) return ;
258+ if (column->data_ [type_index] != nullptr ) {
259+ TVM_FFI_THROW (RuntimeError) << " Type attribute `" << name_str << " ` is already set for type `"
260+ << TypeIndexToTypeKey (type_index) << " `" ;
261+ }
262+ column->data_ [type_index] = value_view;
263+ }
264+ const TVMFFITypeAttrColumn* GetTypeAttrColumn (const TVMFFIByteArray* name) {
265+ String name_str (*name);
266+ auto it = type_attr_name_to_column_index_.find (name_str);
267+ if (it == type_attr_name_to_column_index_.end ()) return nullptr ;
268+ return type_attr_columns_[(*it).second ].get ();
235269 }
236270
237271 void Dump (int min_children_count) {
@@ -284,11 +318,11 @@ class TypeTable {
284318 this ->GetOrAllocTypeIndex (Object::_type_key, Object::_type_index, Object::_type_depth,
285319 Object::_type_child_slots, Object::_type_child_slots_can_overflow,
286320 -1 );
287- TVMFFITypeExtraInfo info;
321+ TVMFFITypeMetadata info;
288322 info.total_size = sizeof (Object);
289323 info.creator = nullptr ;
290324 info.doc = TVMFFIByteArray{nullptr , 0 };
291- RegisterTypeExtraInfo (Object::_type_index, &info);
325+ RegisterTypeMetadata (Object::_type_index, &info);
292326 // reserve the static types
293327 ReserveBuiltinTypeIndex (StaticTypeKey::kTVMFFINone , TypeIndex::kTVMFFINone );
294328 ReserveBuiltinTypeIndex (StaticTypeKey::kTVMFFIInt , TypeIndex::kTVMFFIInt );
@@ -328,6 +362,9 @@ class TypeTable {
328362 std::vector<std::unique_ptr<Entry>> type_table_;
329363 Map<String, int64_t > type_key2index_;
330364 std::vector<Any> any_pool_;
365+ // type attribute columns
366+ std::vector<std::unique_ptr<TypeAttrColumnData>> type_attr_columns_;
367+ Map<String, int64_t > type_attr_name_to_column_index_;
331368};
332369
333370void MakeObjectFromPackedArgs (ffi::PackedArgs args, Any* ret) {
@@ -343,12 +380,12 @@ void MakeObjectFromPackedArgs(ffi::PackedArgs args, Any* ret) {
343380 TVM_FFI_ICHECK (args.size () % 2 == 1 );
344381 const TVMFFITypeInfo* type_info = TVMFFIGetTypeInfo (type_index);
345382
346- if (type_info->extra_info == nullptr || type_info->extra_info ->creator == nullptr ) {
383+ if (type_info->metadata == nullptr || type_info->metadata ->creator == nullptr ) {
347384 TVM_FFI_THROW (RuntimeError) << " Type `" << TypeIndexToTypeKey (type_index)
348385 << " ` does not support reflection creation" ;
349386 }
350387 TVMFFIObjectHandle handle;
351- TVM_FFI_CHECK_SAFE_CALL (type_info->extra_info ->creator (&handle));
388+ TVM_FFI_CHECK_SAFE_CALL (type_info->metadata ->creator (&handle));
352389 ObjectPtr<Object> ptr =
353390 details::ObjectUnsafe::ObjectPtrFromOwned<Object>(static_cast <TVMFFIObject*>(handle));
354391
@@ -437,12 +474,25 @@ int TVMFFITypeRegisterMethod(int32_t type_index, const TVMFFIMethodInfo* info) {
437474 TVM_FFI_SAFE_CALL_END ();
438475}
439476
440- int TVMFFITypeRegisterExtraInfo (int32_t type_index, const TVMFFITypeExtraInfo* extra_info ) {
477+ int TVMFFITypeRegisterMetadata (int32_t type_index, const TVMFFITypeMetadata* metadata ) {
441478 TVM_FFI_SAFE_CALL_BEGIN ();
442- tvm::ffi::TypeTable::Global ()->RegisterTypeExtraInfo (type_index, extra_info );
479+ tvm::ffi::TypeTable::Global ()->RegisterTypeMetadata (type_index, metadata );
443480 TVM_FFI_SAFE_CALL_END ();
444481}
445482
483+ int TVMFFITypeRegisterAttr (int32_t type_index, const TVMFFIByteArray* name,
484+ const TVMFFIAny* value) {
485+ TVM_FFI_SAFE_CALL_BEGIN ();
486+ tvm::ffi::TypeTable::Global ()->RegisterTypeAttr (type_index, name, value);
487+ TVM_FFI_SAFE_CALL_END ();
488+ }
489+
490+ const TVMFFITypeAttrColumn* TVMFFIGetTypeAttrColumn (const TVMFFIByteArray* name) {
491+ TVM_FFI_LOG_EXCEPTION_CALL_BEGIN ();
492+ return tvm::ffi::TypeTable::Global ()->GetTypeAttrColumn (name);
493+ TVM_FFI_LOG_EXCEPTION_CALL_END (TVMFFIGetTypeAttrColumn);
494+ }
495+
446496int32_t TVMFFITypeGetOrAllocIndex (const TVMFFIByteArray* type_key, int32_t static_type_index,
447497 int32_t type_depth, int32_t num_child_slots,
448498 int32_t child_slots_can_overflow, int32_t parent_type_index) {
0 commit comments