Skip to content

[Feature Request] Function Overloading #265

@Ubospica

Description

@Ubospica

Goal and motivation. Supporting function overloading in tvm-ffi. This would be helpful for libraries that use overloading in their code. For example, XGrammar has a lot of overloading functions. Supporting overloading can unblock it from migrating to tvm-ffi.

Method.

  1. According to our guideline that the C ABI should be stable and minimal, we should keep the C ABI unchanged.
  2. Extend tvm::ffi::Function to implement a chain to maintain all function pointers in the order of registration.
  3. When calling functions, try functions in order until the first matched.

This also aligns with nanobind's method to handle overloaded functions. It allocates a contiguous block of memory to store all function overloads. Its memory layout:

+------------------+  ← nb_func* (PyObject_VAR_HEAD)
| ob_refcnt        |
| ob_type          |
| ob_size          |  ← number of overloads (Py_SIZE)
+------------------+
| vectorcall       |  ← function pointer: call dispatcher
| max_nargs        |  ← maximum number of arguments among all overloads
| complex_call     |  ← boolean: whether a complex call path is required
| doc_uniform      |  ← boolean: whether all overloads share the same docstring
+------------------+  ← sizeof(nb_func)
| func_data[0]     |  ← metadata for the first overload
|   (overload 0)   |
+------------------+
| func_data[1]     |  ← metadata for the second overload
|   (overload 1)   |
+------------------+
| ...              |
+------------------+
| func_data[N-1]   |  ← metadata for the last overload
|   (overload N-1) |
+------------------+

cc @junrushao @tqchen @DarkSharpness

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions