feat(ffi)!: add __ffi_convert__ type attribute and Python-defined type support#503
feat(ffi)!: add __ffi_convert__ type attribute and Python-defined type support#503junrushao wants to merge 1 commit intoapache:mainfrom
Conversation
…pe support in C++ Architecture: - Introduce `__ffi_convert__` type attribute: a per-type Function that performs typed `AnyView -> TObjectRef` conversion via `TypeTraits::TryCastFromAnyView`. Registered through new `RegisterConvertTypeAttr<T>()` template and the `ObjectDef<T>::ref<TObjectRef>()` builder method. - Add Python-defined type infrastructure in dataclass.cc: `PyClassDeleter` (ref-release + free), `MakeFFINew` (calloc-based factory registered as `__ffi_new__`), field getter/setter dispatch (`GetFieldGetter`, `MakeFieldSetter`), and `__ffi_shallow_copy__` for deep-copy support. - Expose `ffi.FunctionFromExternC` global function to allow Python-side construction of Function objects from raw C function pointers. - Register `__ffi_convert__` for all built-in static types (Object, String, Bytes, Error, Function, Shape, Tensor, Array, Map, List, Dict). Public Interfaces: - New C++ API: `reflection::RegisterConvertTypeAttr<TObjectRef>(type_index, type_key)`. - New C++ API: `ObjectDef<T>::ref<TObjectRef>()` builder method. - New type attribute key: `reflection::type_attr::kConvert` (`__ffi_convert__`). - New global functions: `ffi.GetFieldGetter`, `ffi.MakeFieldSetter`, `ffi.MakeFFINew`, `ffi.RegisterAutoInit`, `ffi.FunctionFromExternC`. - ABI-compatible: all additions are new type attributes and global functions; no existing signatures changed. UI/UX: none Behavioral Changes: - All built-in object types now have `__ffi_convert__` registered at static init, enabling Python-side TypeSchema to look up typed conversion functions for field setters without Python-level dispatch. - Python-defined types (py_class) can now allocate objects entirely from C++ with proper ref-counting, field destruction, and shallow-copy support. - Object base type now explicitly sets `structural_eq_hash_kind` to `kTVMFFISEqHashKindUnsupported` during registration. Docs: no documentation files updated; gaps exist for the new `__ffi_convert__` type attribute, `ObjectDef::ref<>()` API, and Python-defined type C++ support functions. These should be documented in docs/concepts/ and docs/guides/. Tests: no new tests in this commit; validation deferred to integration with the Python-side py_class decorator. Existing tests were not modified. Untested Edge Cases: - PyClassDeleter with deeply nested Any fields containing cyclic references. - MakeFFINew allocation failure path (OOM). - FunctionFromExternC with invalid function pointers. - __ffi_convert__ for user-defined types with custom TryCastFromAnyView. BREAKING CHANGE: Object base type registration now sets structural_eq_hash_kind to kTVMFFISEqHashKindUnsupported explicitly, which may affect types that previously inherited an uninitialized default.
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the Foreign Function Interface (FFI) by introducing a new mechanism for type conversion and robust support for Python-defined types. These changes streamline the interoperability between C++ and Python, allowing for more flexible and type-safe data exchange, particularly for custom object types defined in Python. The new conversion attribute and Python type infrastructure lay the groundwork for more seamless integration and advanced reflection capabilities. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces significant enhancements to TVM's FFI reflection system. The main features are the __ffi_convert__ type attribute for typed AnyView -> TObjectRef conversions and the infrastructure to support Python-defined types. This includes mechanisms for custom object allocation, deletion, and field access. The changes are well-structured and include registrations for built-in types and updates to testing. My review found one minor issue related to a hardcoded value that could be improved for better maintainability.
Note: Security Review did not run due to the size of the PR.
| template <typename TObjectRef> | ||
| TVM_FFI_INLINE ObjectDef& ref() { |
There was a problem hiding this comment.
@tqchen Not sure if the best naming. Perhaps .traits<TObjectRef> can be better?
Summary
__ffi_convert__type attribute: a per-typeFunctionthat performs typedAnyView -> TObjectRefconversion viaTypeTraits::TryCastFromAnyView, registered throughRegisterConvertTypeAttr<T>()and the newObjectDef<T>::ref<TObjectRef>()builder methoddataclass.cc:PyClassDeleter,MakeFFINew(calloc-based factory), field getter/setter dispatch, and__ffi_shallow_copy__for deep-copy support__ffi_convert__for all built-in static types (Object, String, Bytes, Error, Function, Shape, Tensor, Array, Map, List, Dict)ffi.FunctionFromExternCglobal function for Python-side construction ofFunctionobjects from raw C function pointersTest plan
BREAKING CHANGE: Object base type registration now explicitly sets
structural_eq_hash_kindtokTVMFFISEqHashKindUnsupported, which may affect types that previously inherited an uninitialized default.