Commit 1b751b2
committed
[FEAT][Python] Tie Python wrapper lifetime to underlying C++ FFI object
Make a.x is a.x, id(a.x) stable across drop+refetch, and f(x) is x for
FFI returns by attaching a 16-byte tagged-pointer header to every
Object allocation. Implements Tianqi's PyObjectTying doc (2026-05-01)
with adaptations forced by Py_LIMITED_API + Cython 3.x constraints.
Design:
- Two-layer custom-allocator hook in core libtvm_ffi.so:
`TVMFFICustomAllocHeader { delete_space }`,
`TVMFFICustomAllocator { allocate }`, plus
`TVMFFIGetCustomAllocator` / `TVMFFISetCustomAllocator`. libtvm_ffi
installs a builtin default at registry construction so every
Object always carries the 8-byte base header. The Python Cython
module overrides the global default at module load with
`TVMFFIPyAllocate`, which prepends a 16-byte `PyCustomAllocHeader`
encoding the Python wrapper binding.
- Tagged-pointer state machine. `PyCustomAllocHeader` is a single
`uintptr_t tagged_wrapper` field plus the base header (16 B
total). Low bit of `tagged_wrapper` is the "live" tag:
(a) tagged_wrapper == 0 : never wrapped
(b) tagged_wrapper == (uintptr_t)w | 1 : live wrapper at w
(c) tagged_wrapper == (uintptr_t)w : preserved-mem at w
CPython PyObjects are >= 8-byte aligned (PyObject_HEAD = 16 B), so
bit 0 is always free for the tag. Eliminates the 8-byte reserved
pad an earlier draft needed.
- Universal cache-on for `make_ret`. Every FFI return (function
results, callback args, FieldGetter) funnels through
`make_ret_object` which returns the canonical wrapper for a
chandle when one exists. Combined with state-(c) revive on the
cached path, this delivers `a.x is a.x`, stable `id(a.x)` across
drop+refetch, and `f(x) is x` when a function returns the same
chandle it received. The doc proposed a `tp_alloc` + TLS hand-off
for revive; not viable under Py_LIMITED_API
(`PyType_SetSlot` not in 3.12 SABI; Cython 3.x forces
`CYTHON_USE_TYPE_SPECS=0` for limited-API targets). The inline
`make_ret_object` revive branch is the SABI-compatible equivalent
-- bypasses `tp_alloc` by returning the cached PyObject directly,
strictly cheaper than the doc's design (no alloc+dealloc cycle on
revive).
- State (c) via Cython `def __del__` on `CObject`, mapping to
`tp_finalize` (PEP 442). When other C++ holders keep the chandle
alive past the wrapper's Python refcount hitting 0, clear the
live bit (keep the pointer), DecRef the chandle, `Py_INCREF(self)`
to resurrect. Wrapper memory survives at the same address until
revive or chandle death. Shim for
`PyObject_CallFinalizerFromDealloc` (added to SABI in 3.13)
using `PyType_GetSlot` + `Py_SET_REFCNT`/`Py_REFCNT` on 3.12;
paired with `-DCYTHON_USE_TP_FINALIZE=1`.
- Shutdown guard. `g_tvm_ffi_python_alive` flag is set to 0 by an
atexit handler registered at Cython module init; read by
`TVMFFIPyDeleteSpace` before `PyGILState_Ensure` to avoid GIL
acquire after `Py_Finalize` has begun. State-(c) wrapper bytes
on chandles still alive at that point are intentionally leaked
-- process is exiting.
- Frontend-allocation detection by `delete_space` pointer comparison
(`TVMFFIPyIsCanonical`) avoids a flag bit on `TVMFFIObject`.
Pre-Python-init chandles (statically-initialized global functions
in libtvm_ffi.so) carry only the base header; the Python side
detects this and skips the binding install.
Trade-off on `_move()` semantics. Universal cache-on means callback
args alias the caller's wrapper (one wrapper, one chandle ref) and
function returns of the same chandle alias the caller's wrapper.
`_move()` semantics are kept as an API: the rvalue setter on either
caller or callback side eager-detaches the canonical-wrapper binding
before the C++ AnyViewToOwnedAny transfer nulls the source chandle.
`test_function.py::test_rvalue_ref` refactored for the new
aliasing-aware use_count expectations.
Refcount fix. The `make_ret_object` state-(b) cache hit path had a
redundant `Py_INCREF(cached)` leaking +1 wrapper ref per cache hit
(verified via `sys.getrefcount`). The `<object>(<PyObject*>...)` cast
already INCREFs for the owned reference; `return cached` transfers it
to the caller. Removed the explicit INCREF.
`PyClassDeleter` in `extra/dataclass.cc` and the `__ffi_new__` /
`__ffi_shallow_copy__` paths are routed through the same allocator
registry so Python-defined types share the layout and lifetime
semantics.
`TVMFFIPyArgSetterObjectRValueRef_` clears the source's binding
eagerly before the C++ side nulls its `chandle`; otherwise a
downstream cache lookup on the (still-alive) chandle would briefly
see a stale back-pointer.
Tests: full Python suite passes (2332 passed, 19 skipped, 2 xfailed).
New `tests/python/test_pyobject_tying.py` (32 tests) covers state
b/c, cache-on aliasing, `_move()` under cache-on, pickle stress,
threading stress, GC integration with state (c), multi-chandle
isolation, and the weakref limitation. `test_function.py::test_rvalue_ref`
refactored for cache-on aliasing semantics.
TODO carried in `function.pxi::_get_global_func`: name-keyed dict
cache for static-init Function id-stability. Most registry-resident
Functions are allocated at C++ static init (before the Python
allocator is registered) so their chandle has only the base header --
make_ret_object's cache can't reach them. Deferred to keep this
change small.1 parent d73a267 commit 1b751b2
12 files changed
Lines changed: 1409 additions & 98 deletions
File tree
- include/tvm/ffi
- python/tvm_ffi/cython
- src/ffi
- extra
- tests/python
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
| 73 | + | |
73 | 74 | | |
74 | 75 | | |
75 | 76 | | |
| |||
289 | 290 | | |
290 | 291 | | |
291 | 292 | | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
292 | 304 | | |
293 | 305 | | |
294 | 306 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
580 | 580 | | |
581 | 581 | | |
582 | 582 | | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
583 | 660 | | |
584 | 661 | | |
585 | 662 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
| 30 | + | |
30 | 31 | | |
31 | 32 | | |
32 | 33 | | |
| |||
92 | 93 | | |
93 | 94 | | |
94 | 95 | | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
95 | 127 | | |
96 | 128 | | |
97 | 129 | | |
| |||
111 | 143 | | |
112 | 144 | | |
113 | 145 | | |
114 | | - | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
115 | 154 | | |
116 | 155 | | |
117 | 156 | | |
| |||
132 | 171 | | |
133 | 172 | | |
134 | 173 | | |
135 | | - | |
136 | | - | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
137 | 177 | | |
138 | 178 | | |
139 | 179 | | |
| |||
154 | 194 | | |
155 | 195 | | |
156 | 196 | | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
161 | 201 | | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | | - | |
169 | | - | |
170 | | - | |
171 | | - | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
172 | 212 | | |
173 | 213 | | |
174 | 214 | | |
| |||
180 | 220 | | |
181 | 221 | | |
182 | 222 | | |
183 | | - | |
184 | | - | |
185 | | - | |
186 | | - | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
187 | 226 | | |
188 | 227 | | |
189 | 228 | | |
190 | | - | |
| 229 | + | |
191 | 230 | | |
192 | 231 | | |
193 | 232 | | |
| |||
197 | 236 | | |
198 | 237 | | |
199 | 238 | | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
211 | | - | |
212 | | - | |
213 | | - | |
214 | | - | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
215 | 242 | | |
216 | 243 | | |
217 | 244 | | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
218 | 248 | | |
219 | 249 | | |
220 | 250 | | |
221 | | - | |
222 | 251 | | |
223 | | - | |
| 252 | + | |
224 | 253 | | |
225 | 254 | | |
226 | 255 | | |
| |||
232 | 261 | | |
233 | 262 | | |
234 | 263 | | |
235 | | - | |
236 | | - | |
237 | | - | |
238 | | - | |
239 | 264 | | |
240 | 265 | | |
241 | 266 | | |
242 | | - | |
| 267 | + | |
243 | 268 | | |
244 | 269 | | |
245 | 270 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
| 18 | + | |
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| 23 | + | |
23 | 24 | | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
24 | 29 | | |
25 | 30 | | |
26 | 31 | | |
| |||
162 | 167 | | |
163 | 168 | | |
164 | 169 | | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
165 | 179 | | |
166 | 180 | | |
167 | 181 | | |
| |||
361 | 375 | | |
362 | 376 | | |
363 | 377 | | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
364 | 403 | | |
365 | 404 | | |
366 | 405 | | |
| |||
542 | 581 | | |
543 | 582 | | |
544 | 583 | | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
545 | 600 | | |
546 | 601 | | |
0 commit comments