Commit 46eac56
authored
[FFI][ABI] Introduce weak rc support (#18259)
This PR adds weak ref counter support to the FFI ABI.
Weak rc is useful when we want to break cyclic dependencies.
- When a strong rc goes to zero, we call the destructor of the object, but not freeing the memory
- When both strong and weak rc goes to zero, we call the memory free operation
The weak rc mechanism is useful when we want to break cyclic dependencies in object, where the
weak rc can keep memory alive but the destructor is called.
As of now, because we deliberately avoid cyles in codebase, we do not have strong use-case for weak rc.
However, given weak rc is common practice in shared_ptr, Rust RC, and also used in torch's c10::intrusive_ptr.
It is better to make sure the ABI is future compatible to such use-cases before we freeze.
This PR implements weak rc as a u32 counter and strong rc as a u64 counter, with the following
design consideration.
- Weak rc is very rarely used and u32 is sufficient.
- Keeping weak rc in u32 allows us to keep object header size to 24 bytes,
saving extra 8 bytes(considering alignment)
We also need to update deleter to take flags that consider both weak and
strong deletion events. The implementation tries to optimize common case where
both strong and weak goes to 0 at the same time and call deleter once
with both flags set.1 parent b67650f commit 46eac56
File tree
16 files changed
+475
-58
lines changed- ffi
- include/tvm/ffi
- python/tvm_ffi/cython
- src/ffi
- tests/cpp
- jvm/native/src/main/native
- src/tir/transforms
- web/src
16 files changed
+475
-58
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
156 | 156 | | |
157 | 157 | | |
158 | 158 | | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
159 | 189 | | |
160 | 190 | | |
161 | 191 | | |
| |||
166 | 196 | | |
167 | 197 | | |
168 | 198 | | |
169 | | - | |
170 | | - | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
171 | 207 | | |
172 | | - | |
173 | | - | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
174 | 215 | | |
175 | 216 | | |
176 | 217 | | |
| |||
307 | 348 | | |
308 | 349 | | |
309 | 350 | | |
310 | | - | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
311 | 360 | | |
312 | | - | |
313 | | - | |
314 | 361 | | |
315 | 362 | | |
316 | | - | |
| 363 | + | |
317 | 364 | | |
318 | 365 | | |
319 | 366 | | |
| |||
470 | 517 | | |
471 | 518 | | |
472 | 519 | | |
473 | | - | |
| 520 | + | |
474 | 521 | | |
475 | 522 | | |
476 | 523 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
36 | | - | |
| 36 | + | |
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
| |||
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
78 | | - | |
| 78 | + | |
| 79 | + | |
79 | 80 | | |
80 | 81 | | |
81 | 82 | | |
| |||
96 | 97 | | |
97 | 98 | | |
98 | 99 | | |
99 | | - | |
| 100 | + | |
| 101 | + | |
100 | 102 | | |
101 | 103 | | |
102 | 104 | | |
| |||
136 | 138 | | |
137 | 139 | | |
138 | 140 | | |
139 | | - | |
| 141 | + | |
140 | 142 | | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
147 | 153 | | |
148 | 154 | | |
149 | 155 | | |
| |||
182 | 188 | | |
183 | 189 | | |
184 | 190 | | |
185 | | - | |
| 191 | + | |
186 | 192 | | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
194 | 204 | | |
195 | 205 | | |
196 | 206 | | |
| |||
0 commit comments