You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/concepts/any.rst
+15-27Lines changed: 15 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,9 +27,7 @@ values of a wide variety of types, including primitives, objects, and strings.
27
27
Unlike ``std::any``, it is designed for zero-copy inter-language exchange without RTTI,
28
28
featuring a fixed 16-byte layout with built-in reference counting and ownership semantics.
29
29
30
-
This tutorial covers everything you need to know about :cpp:class:`~tvm::ffi::Any` and :cpp:class:`~tvm::ffi::AnyView`:
31
-
common usage patterns, ownership semantics, and memory layout.
32
-
30
+
This tutorial covers common usage patterns, ownership semantics, and memory layout.
33
31
34
32
Common Usage
35
33
------------
@@ -169,6 +167,8 @@ Compare with ``nullptr`` to check for ``None``:
169
167
}
170
168
171
169
170
+
.. _any-ownership:
171
+
172
172
Ownership
173
173
---------
174
174
@@ -195,8 +195,8 @@ The core distinction between :cpp:class:`tvm::ffi::Any` and
195
195
- Function inputs
196
196
- Return values, storage
197
197
198
-
Code Examples
199
-
~~~~~~~~~~~~~~
198
+
Examples
199
+
~~~~~~~~
200
200
201
201
:cpp:class:`~tvm::ffi::AnyView` is a lightweight, non-owning view. Copying it simply
202
202
copies 16 bytes with no reference count updates, making it ideal for passing arguments without overhead:
@@ -245,25 +245,9 @@ Destruction Semantics in C
245
245
246
246
In C, which lacks RAII, you must manually destroy :cpp:class:`~tvm::ffi::Any` objects
247
247
by calling :cpp:func:`TVMFFIObjectDecRef` for heap-allocated objects.
248
+
Destroying an :cpp:class:`~tvm::ffi::AnyView` is effectively a no-op - just clear its contents.
248
249
249
-
.. code-block:: cpp
250
-
251
-
void destroy_any(TVMFFIAny* any) {
252
-
if (any->type_index >= kTVMFFIStaticObjectBegin) {
253
-
// Decrement the reference count of the heap-allocated object
254
-
TVMFFIObjectDecRef(any->v_obj);
255
-
}
256
-
*any = (TVMFFIAny){0};
257
-
}
258
-
259
-
In contrast, destroying an :cpp:class:`~tvm::ffi::AnyView` is effectively a no-op - just clear its contents.
260
-
261
-
.. code-block:: cpp
262
-
263
-
void destroy_any_view(TVMFFIAny* any_view) {
264
-
*any_view = (TVMFFIAny){0};
265
-
}
266
-
250
+
See :ref:`abi-destruct-any` for C code examples.
267
251
268
252
Layout
269
253
------
@@ -310,6 +294,8 @@ It is effectively a layout-stable 16-byte tagged union.
310
294
* The first 4 bytes (:cpp:member:`TVMFFIAny::type_index`) serve as a tag identifying the stored type.
311
295
* The last 8 bytes hold the actual value - either stored inline for atomic types (e.g., ``int64_t``, ``float64``, ``void*``) or as a pointer to a heap-allocated object.
312
296
297
+
.. _any-atomic-types:
298
+
313
299
Atomic Types
314
300
~~~~~~~~~~~~
315
301
@@ -371,6 +357,8 @@ Note that raw pointers like :c:struct:`DLTensor* <DLTensor>` and ``char*`` also
371
357
These pointers carry no ownership, so the caller must ensure the pointed-to data outlives
372
358
the :cpp:class:`~tvm::ffi::AnyView` or :cpp:class:`~tvm::ffi::Any`.
void (*update_backtrace)(...); // Hook to append/replace backtrace
251
-
} TVMFFIErrorCell;
230
+
This design is called a **packed function**, because it "packs" all arguments into a single array of type-erased :cpp:type:`tvm::ffi::AnyView`,
231
+
and further unifies calling convention across all languages without resorting to JIT compilation.
252
232
253
-
**Print an Error**. The example code below shows how to print an error message and backtrace.
233
+
More specifically, this mechanism enables the following scenarios:
254
234
255
-
.. code-block:: cpp
256
-
257
-
#include <tvm/ffi/c_api.h>
235
+
- **Dynamic languages**. Well-optimized bindings are provided for, e.g. Python, to translate arguments into packed function format, and translate return value back to the host language.
236
+
- **Static languages**. Metaprogramming techniques, such as C++ templates, are usually available to directly instantiate packed format on stack, saving the need for dynamic examination.
237
+
- **Cross-language callbacks**. Language-agnostic :cpp:class:`tvm::ffi::Function` makes it easy to call between languages without depending on language-specific features such as GIL.
0 commit comments