Skip to content

feat(ffi)!: add __ffi_convert__ type attribute and Python-defined type support#503

Open
junrushao wants to merge 1 commit intoapache:mainfrom
junrushao:2026-03-10/python-type-ffi-support
Open

feat(ffi)!: add __ffi_convert__ type attribute and Python-defined type support#503
junrushao wants to merge 1 commit intoapache:mainfrom
junrushao:2026-03-10/python-type-ffi-support

Conversation

@junrushao
Copy link
Member

@junrushao junrushao commented Mar 10, 2026

Summary

  • Introduce __ffi_convert__ type attribute: a per-type Function that performs typed AnyView -> TObjectRef conversion via TypeTraits::TryCastFromAnyView, registered through RegisterConvertTypeAttr<T>() and the new ObjectDef<T>::ref<TObjectRef>() builder method
  • Add Python-defined type infrastructure in dataclass.cc: PyClassDeleter, MakeFFINew (calloc-based factory), field getter/setter dispatch, and __ffi_shallow_copy__ for deep-copy support
  • Register __ffi_convert__ for all built-in static types (Object, String, Bytes, Error, Function, Shape, Tensor, Array, Map, List, Dict)
  • Expose ffi.FunctionFromExternC global function for Python-side construction of Function objects from raw C function pointers

Test plan

  • Existing C++ tests pass (reflection, object creation, serialization)
  • Existing Python tests pass (c_class / py_class decorator integration)
  • CI lint (clang-format, clang-tidy) passes on changed files
  • Integration testing with Python-side py_class decorator

BREAKING CHANGE: Object base type registration now explicitly sets structural_eq_hash_kind to kTVMFFISEqHashKindUnsupported, which may affect types that previously inherited an uninitialized default.

…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.
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, 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 __ffi_convert__ Type Attribute: Introduced a per-type Function attribute that enables typed AnyView -> TObjectRef conversion via TypeTraits::TryCastFromAnyView, registered through RegisterConvertTypeAttr<T>() and the new ObjectDef<T>::ref<TObjectRef>() builder method.
  • Python-Defined Type Infrastructure: Added comprehensive support for Python-defined types in dataclass.cc, including PyClassDeleter for memory management, MakeFFINew for calloc-based object factory, field getter/setter dispatch, and __ffi_shallow_copy__ for deep-copy support.
  • Built-in Type Conversion Registration: Registered the __ffi_convert__ attribute for all built-in static types, such as Object, String, Bytes, Error, Function, Shape, Tensor, Array, Map, List, and Dict.
  • Exposed ffi.FunctionFromExternC: Provided a global function ffi.FunctionFromExternC to allow Python-side construction of Function objects directly from raw C function pointers.

🧠 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
  • include/tvm/ffi/reflection/init.h
    • Included tvm/ffi/cast.h.
    • Added details::CastFromAny template function for AnyView to TObjectRef conversion.
  • include/tvm/ffi/reflection/registry.h
    • Defined kConvert constant for the new __ffi_convert__ type attribute.
    • Implemented RegisterConvertTypeAttr template function to register the conversion attribute for a given object reference type.
    • Added ObjectDef::ref<TObjectRef>() builder method to simplify registration of the __ffi_convert__ attribute.
  • src/ffi/extra/dataclass.cc
    • Introduced PyClassDeleter for proper memory management of Python-defined objects.
    • Implemented PyClassFieldGetter and GetFieldGetter for generic field access.
    • Added WriteFieldValue and MakeFieldSetter for setting field values, supporting type conversion.
    • Created MakeFFINew to register a factory function for allocating and initializing Python-defined types, including __ffi_shallow_copy__ support.
    • Exposed new FFI functions: ffi.GetFieldGetter, ffi.MakeFieldSetter, ffi.MakeFFINew.
  • src/ffi/function.cc
    • Added ffi.FunctionFromExternC global function to create Function objects from C function pointers.
  • src/ffi/object.cc
    • Included headers for Dict, List, and Tensor containers.
    • Explicitly set structural_eq_hash_kind to kTVMFFISEqHashKindUnsupported for the base Object type.
    • Registered __ffi_convert__ for various built-in types like ObjectRef, String, Bytes, Error, Function, Shape, Tensor, Array, Map, List, and Dict.
  • src/ffi/testing/testing.cc
    • Updated TestIntPairObj definition to use the new ObjectDef::ref<TestIntPair>() method.
Activity
  • No human activity (comments, reviews, etc.) has been recorded for this pull request yet.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

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.

Comment on lines +842 to +843
template <typename TObjectRef>
TVM_FFI_INLINE ObjectDef& ref() {
Copy link
Member Author

Choose a reason for hiding this comment

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

@tqchen Not sure if the best naming. Perhaps .traits<TObjectRef> can be better?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant