Skip to content

feat(ffi)!: centralize object creation with CreateEmptyObject/HasCreator#501

Merged
junrushao merged 1 commit intoapache:mainfrom
junrushao:2026-03-10/centralize-object-creation
Mar 10, 2026
Merged

feat(ffi)!: centralize object creation with CreateEmptyObject/HasCreator#501
junrushao merged 1 commit intoapache:mainfrom
junrushao:2026-03-10/centralize-object-creation

Conversation

@junrushao
Copy link
Member

@junrushao junrushao commented Mar 10, 2026

Summary

  • Add CreateEmptyObject() and HasCreator() inline helpers in function.h that unify the two-step "check creator, call creator" pattern. These try the native metadata->creator fast path first, then fall back to the __ffi_new__ type attribute for Python-defined types.
  • Deduplicate four call sites in creator.h, init.h, reflection_extra.cc, and serialization.cc to use the new centralized helpers.
  • Qualify bare details:: references to ::tvm::ffi::details:: in reflection headers (overload.h, registry.h, init.h) to prevent ADL/lookup ambiguity when included from other namespaces.

Test plan

  • Existing C++ tests pass (object creation via reflection, serialization roundtrip)
  • Existing Python tests pass (c_class / py_class decorator, reflection-based construction)
  • CI lint (clang-format, clang-tidy) passes on changed headers

@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 refactors the Foreign Function Interface (FFI) object creation mechanism by centralizing the logic into new helper functions. This change streamlines the codebase, reduces redundant code, and ensures a consistent approach to instantiating objects, whether they originate from C++ or Python. Additionally, it improves code robustness by explicitly qualifying namespace references in reflection-related headers, mitigating potential naming conflicts.

Highlights

  • Centralized Object Creation: Introduced CreateEmptyObject() and HasCreator() inline helper functions in function.h to unify the process of creating objects, supporting both native C++ creators and Python-defined types with __ffi_new__ attributes.
  • Code Deduplication: Deduplicated object creation logic across four key call sites in creator.h, init.h, reflection_extra.cc, and serialization.cc by adopting the new centralized helper functions.
  • Namespace Qualification: Qualified bare details:: references to ::tvm::ffi::details:: in reflection headers (overload.h, registry.h, init.h) to prevent potential ambiguity issues with Argument-Dependent Lookup (ADL) when included from other namespaces.

🧠 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/function.h
    • Added CreateEmptyObject function to handle object instantiation via native creators or __ffi_new__ attributes.
    • Added HasCreator function to check if a type supports reflection-based creation.
  • include/tvm/ffi/reflection/creator.h
    • Updated ObjectCreator constructor to utilize the new HasCreator function for validation.
    • Modified ObjectCreator::operator() to use CreateEmptyObject for object instantiation.
  • include/tvm/ffi/reflection/init.h
    • Replaced direct metadata and creator checks with the HasCreator helper in MakeInit.
    • Updated the MakeInit lambda to use CreateEmptyObject for object creation.
    • Fully qualified details::TypeSchemaImpl with ::tvm::ffi::details:: to prevent ADL issues.
  • include/tvm/ffi/reflection/overload.h
    • Fully qualified details::OverloadedFunction, details::CreateNewOverload, details::TypeSchema, details::FunctionInfo, and details::OverloadBase with ::tvm::ffi::details::.
  • include/tvm/ffi/reflection/registry.h
    • Fully qualified details::ObjectUnsafe, details::AnyUnsafe, details::FunctionInfo, and details::TypeSchemaImpl with ::tvm::ffi::details::.
  • src/ffi/extra/reflection_extra.cc
    • Replaced the explicit check for metadata->creator and direct creator call with the CreateEmptyObject helper.
  • src/ffi/extra/serialization.cc
    • Replaced the explicit check for metadata->creator and direct creator call with the CreateEmptyObject helper.
Activity
  • No specific activity (comments, reviews, progress) 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 is a great refactoring that centralizes object creation logic into CreateEmptyObject and HasCreator helpers. This significantly reduces code duplication across several files. The changes to qualify details:: namespaces are also a good improvement for code robustness.

I have a couple of suggestions for the new helper functions in include/tvm/ffi/function.h to improve correctness and maintainability. Please see the detailed comments.

@junrushao junrushao force-pushed the 2026-03-10/centralize-object-creation branch from adc2aa5 to 741fcb6 Compare March 10, 2026 17:55
…ator

Add `CreateEmptyObject()` and `HasCreator()` in `function.h` that unify
the two-step pattern (check creator, call creator) used across reflection
code. These functions first try the native `metadata->creator` fast path,
then fall back to the `__ffi_new__` type attribute for Python-defined types.

Deduplicate four call sites in `creator.h`, `init.h`, `reflection_extra.cc`,
and `serialization.cc` to use the new centralized helpers.

Also qualify bare `details::` references to `::tvm::ffi::details::` in
reflection headers (`overload.h`, `registry.h`, `init.h`) to prevent
lookup ambiguity when included from other namespaces.
@junrushao junrushao force-pushed the 2026-03-10/centralize-object-creation branch from 741fcb6 to 8c2dd20 Compare March 10, 2026 18:44
@junrushao junrushao merged commit e268eb1 into apache:main Mar 10, 2026
8 checks passed
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.

2 participants