1#![allow(non_camel_case_types)]
22
23use std::ffi::c_void;
24use std::sync::atomic::AtomicU64;
25
26use crate::dlpack::DLDataType;
27use crate::dlpack::DLDevice;
28
29#[repr(i32)]
31#[derive(Debug, Copy, Clone, PartialEq, Eq)]
32pub enum TVMFFITypeIndex {
33 kTVMFFINone = 0,
35 kTVMFFIInt = 1,
37 kTVMFFIBool = 2,
39 kTVMFFIFloat = 3,
41 kTVMFFIOpaquePtr = 4,
43 kTVMFFIDataType = 5,
45 kTVMFFIDevice = 6,
47 kTVMFFIDLTensorPtr = 7,
49 kTVMFFIRawStr = 8,
51 kTVMFFIByteArrayPtr = 9,
53 kTVMFFIObjectRValueRef = 10,
55 kTVMFFISmallStr = 11,
57 kTVMFFISmallBytes = 12,
59 kTVMFFIUnchanged = 13,
61 kTVMFFIStaticObjectBegin = 64,
63 kTVMFFIStr = 65,
65 kTVMFFIBytes = 66,
67 kTVMFFIError = 67,
69 kTVMFFIFunction = 68,
71 kTVMFFIShape = 69,
73 kTVMFFITensor = 70,
75 kTVMFFIArray = 71,
77 kTVMFFIMap = 72,
82 kTVMFFIModule = 73,
84 kTVMFFIOpaquePyObject = 74,
86 kTVMFFIList = 75,
88 kTVMFFIDict = 76,
90 kTVMFFIVisitInterrupt = 77,
92 kTVMFFIBigInt = 78,
94 kTVMFFIStaticObjectEnd = 79,
96 kTVMFFIDynObjectBegin = 128,
98}
99
100#[repr(i32)]
101#[derive(Debug, Copy, Clone, PartialEq, Eq)]
102pub enum TVMFFIObjectDeleterFlagBitMask {
103 kTVMFFIObjectDeleterFlagBitMaskStrong = 1 << 0,
104 kTVMFFIObjectDeleterFlagBitMaskWeak = 1 << 1,
105 kTVMFFIObjectDeleterFlagBitMaskBoth = (1 << 0) | (1 << 1),
106}
107
108#[repr(i32)]
110#[derive(Debug, Copy, Clone, PartialEq, Eq)]
111pub enum TVMFFIFieldFlagBitMask {
112 kTVMFFIFieldFlagBitMaskWritable = 1 << 0,
113 kTVMFFIFieldFlagBitMaskHasDefault = 1 << 1,
114 kTVMFFIFieldFlagBitMaskIsStaticMethod = 1 << 2,
115 kTVMFFIFieldFlagBitMaskSEqHashIgnore = 1 << 3,
116 kTVMFFIFieldFlagBitMaskSEqHashDefPattern = 1 << 4,
117 kTVMFFIFieldFlagBitMaskDefaultFromFactory = 1 << 5,
118 kTVMFFIFieldFlagBitMaskReprOff = 1 << 6,
119 kTVMFFIFieldFlagBitMaskCompareOff = 1 << 7,
120 kTVMFFIFieldFlagBitMaskHashOff = 1 << 8,
121 kTVMFFIFieldFlagBitMaskInitOff = 1 << 9,
122 kTVMFFIFieldFlagBitMaskKwOnly = 1 << 10,
123 kTVMFFIFieldFlagBitSetterIsFunctionObj = 1 << 11,
124 kTVMFFIFieldFlagBitMaskSEqHashDefSimple = 1 << 12,
125}
126
127#[repr(i32)]
129#[derive(Debug, Copy, Clone, PartialEq, Eq)]
130pub enum TVMFFIDefRegionKind {
131 kTVMFFIDefRegionKindNone = 0,
132 kTVMFFIDefRegionKindPattern = 1,
133 kTVMFFIDefRegionKindSimple = 2,
134}
135
136#[repr(i32)]
138#[derive(Debug, Copy, Clone, PartialEq, Eq)]
139pub enum TVMFFISEqHashKind {
140 kTVMFFISEqHashKindUnsupported = 0,
141 kTVMFFISEqHashKindTreeNode = 1,
142 kTVMFFISEqHashKindFreeVar = 2,
143 kTVMFFISEqHashKindDAGNode = 3,
144 kTVMFFISEqHashKindConstTreeNode = 4,
145 kTVMFFISEqHashKindUniqueInstance = 5,
146}
147
148pub type TVMFFIObjectHandle = *mut c_void;
150pub type TVMFFIObjectDeleter = unsafe extern "C" fn(self_ptr: *mut c_void, flags: i32);
151
152pub const COMBINED_REF_COUNT_MASK_U32: u64 = (1u64 << 32) - 1;
154pub const COMBINED_REF_COUNT_STRONG_ONE: u64 = 1;
155pub const COMBINED_REF_COUNT_WEAK_ONE: u64 = 1u64 << 32;
156pub const COMBINED_REF_COUNT_BOTH_ONE: u64 =
157 COMBINED_REF_COUNT_STRONG_ONE | COMBINED_REF_COUNT_WEAK_ONE;
158
159#[repr(C)]
160pub struct TVMFFIObject {
161 pub combined_ref_count: AtomicU64,
162 pub type_index: i32,
163 pub __padding: u32,
164 pub deleter: Option<TVMFFIObjectDeleter>,
165 #[cfg(target_pointer_width = "32")]
167 __padding: u32,
168}
169
170impl TVMFFIObject {
171 pub fn new() -> Self {
172 Self {
173 combined_ref_count: AtomicU64::new(0),
174 type_index: 0,
175 __padding: 0,
176 deleter: None,
177 }
178 }
179}
180
181#[repr(C)]
183#[derive(Copy, Clone)]
184pub union TVMFFIAnyDataUnion {
185 pub v_int64: i64,
187 pub v_float64: f64,
189 pub v_ptr: *mut c_void,
191 pub v_c_str: *const i8,
193 pub v_obj: *mut TVMFFIObject,
195 pub v_dtype: DLDataType,
197 pub v_device: DLDevice,
199 pub v_bytes: [u8; 8],
201 pub v_uint64: u64,
203}
204
205#[repr(C)]
207#[derive(Copy, Clone)]
208pub struct TVMFFIAny {
209 pub type_index: i32,
212 pub small_str_len: u32,
214 pub data_union: TVMFFIAnyDataUnion,
216}
217
218impl TVMFFIAny {
219 pub fn new() -> Self {
221 Self {
222 type_index: TVMFFITypeIndex::kTVMFFINone as i32,
223 small_str_len: 0,
224 data_union: TVMFFIAnyDataUnion { v_int64: 0 },
225 }
226 }
227}
228
229#[repr(C)]
231pub struct TVMFFIByteArray {
232 pub data: *const u8,
233 pub size: usize,
234}
235
236impl TVMFFIByteArray {
237 pub fn new(data: *const u8, size: usize) -> Self {
238 Self { data, size }
239 }
240 pub fn as_str(&self) -> &str {
248 unsafe { std::str::from_utf8_unchecked(std::slice::from_raw_parts(self.data, self.size)) }
249 }
250 pub unsafe fn from_str(data: &str) -> Self {
260 Self {
261 data: data.as_ptr(),
262 size: data.len(),
263 }
264 }
265}
266
267pub type TVMFFISafeCallType = unsafe extern "C" fn(
269 handle: *mut c_void,
270 args: *const TVMFFIAny,
271 num_args: i32,
272 result: *mut TVMFFIAny,
273) -> i32;
274
275#[repr(C)]
277pub struct TVMFFIFunctionCell {
278 pub safe_call: TVMFFISafeCallType,
280 pub cxx_call: *mut c_void,
281}
282
283unsafe impl Send for TVMFFIFunctionCell {}
284unsafe impl Sync for TVMFFIFunctionCell {}
285
286#[repr(i32)]
287#[derive(Debug, Copy, Clone, PartialEq, Eq)]
288pub enum TVMFFIBacktraceUpdateMode {
289 kTVMFFIBacktraceUpdateModeReplace = 0,
290 kTVMFFIBacktraceUpdateModeAppend = 1,
291}
292
293#[repr(C)]
295pub struct TVMFFIErrorCell {
296 pub kind: TVMFFIByteArray,
297 pub message: TVMFFIByteArray,
298 pub backtrace: TVMFFIByteArray,
299 pub update_backtrace: unsafe extern "C" fn(
300 self_ptr: *mut c_void,
301 backtrace: *const TVMFFIByteArray,
302 update_mode: i32,
303 ),
304 pub cause_chain: TVMFFIObjectHandle,
305 pub extra_context: TVMFFIObjectHandle,
306}
307
308#[repr(C)]
310pub struct TVMFFIShapeCell {
311 pub data: *const i64,
312 pub size: usize,
313}
314
315pub type TVMFFIFieldGetter =
317 unsafe extern "C" fn(field: *mut c_void, result: *mut TVMFFIAny) -> i32;
318
319pub type TVMFFIFieldSetter =
321 unsafe extern "C" fn(field: *mut c_void, value: *const TVMFFIAny) -> i32;
322
323#[repr(C)]
325pub struct TVMFFIFieldInfo {
326 pub name: TVMFFIByteArray,
328 pub doc: TVMFFIByteArray,
330 pub metadata: TVMFFIByteArray,
332 pub flags: i64,
334 pub size: i64,
336 pub alignment: i64,
338 pub offset: i64,
340 pub getter: Option<TVMFFIFieldGetter>,
342 pub setter: *mut c_void,
351 pub default_value_or_factory: TVMFFIAny,
356 pub field_static_type_index: i32,
358}
359
360pub type TVMFFIObjectCreator = unsafe extern "C" fn(result: *mut TVMFFIObjectHandle) -> i32;
362
363#[repr(C)]
365pub struct TVMFFIMethodInfo {
366 pub name: TVMFFIByteArray,
368 pub doc: TVMFFIByteArray,
370 pub metadata: TVMFFIByteArray,
372 pub flags: i64,
374 pub method: TVMFFIAny,
377}
378
379#[repr(C)]
384pub struct TVMFFITypeMetadata {
385 pub doc: TVMFFIByteArray,
387 pub creator: Option<TVMFFIObjectCreator>,
389 pub total_size: i32,
393 pub structural_eq_hash_kind: i32,
395}
396
397#[repr(C)]
403pub struct TVMFFITypeAttrColumn {
404 pub data: *const TVMFFIAny,
406 pub size: i32,
409 pub begin_index: i32,
413}
414
415#[repr(C)]
417pub struct TVMFFITypeInfo {
418 pub type_index: i32,
421 pub type_depth: i32,
423 pub type_key: TVMFFIByteArray,
425 pub type_acenstors: *const *const TVMFFITypeInfo,
429 pub type_key_hash: u64,
431 pub num_fields: i32,
433 pub num_methods: i32,
435 pub fields: *const TVMFFIFieldInfo,
437 pub methods: *const TVMFFIMethodInfo,
439 pub metadata: *const TVMFFITypeMetadata,
441}
442
443#[repr(C)]
446pub struct TVMFFIObjectAllocHeader {
447 pub delete_space: Option<unsafe extern "C" fn(ptr: *mut c_void)>,
448}
449
450#[repr(C)]
452pub struct TVMFFICustomAllocator {
453 pub allocate: Option<
454 unsafe extern "C" fn(
455 size: usize,
456 alignment: usize,
457 type_index: i32,
458 context: *mut c_void,
459 ) -> *mut c_void,
460 >,
461 pub context: *mut c_void,
462}
463
464unsafe extern "C" {
465 pub fn TVMFFIGetCustomAllocator() -> *mut TVMFFICustomAllocator;
466 pub fn TVMFFISetCustomAllocator(allocator: *mut TVMFFICustomAllocator) -> i32;
467
468 pub fn TVMFFITypeKeyToIndex(type_key: *const TVMFFIByteArray, out_tindex: *mut i32) -> i32;
469 pub fn TVMFFITypeRegisterAttr(
470 type_index: i32,
471 attr_name: *const TVMFFIByteArray,
472 attr_value: *const TVMFFIAny,
473 ) -> i32;
474 pub fn TVMFFIGetTypeAttrColumn(
475 attr_name: *const TVMFFIByteArray,
476 ) -> *const TVMFFITypeAttrColumn;
477 pub fn TVMFFIFunctionGetGlobal(
478 name: *const TVMFFIByteArray,
479 out: *mut TVMFFIObjectHandle,
480 ) -> i32;
481 pub fn TVMFFIFunctionSetGlobal(
482 name: *const TVMFFIByteArray,
483 f: TVMFFIObjectHandle,
484 can_override: i32,
485 ) -> i32;
486 pub fn TVMFFIFunctionCreate(
487 self_ptr: *mut c_void,
488 safe_call: TVMFFISafeCallType,
489 deleter: Option<unsafe extern "C" fn(*mut c_void)>,
490 out: *mut TVMFFIObjectHandle,
491 ) -> i32;
492 pub fn TVMFFIAnyViewToOwnedAny(any_view: *const TVMFFIAny, out: *mut TVMFFIAny) -> i32;
493 pub fn TVMFFIFunctionCall(
494 func: TVMFFIObjectHandle,
495 args: *const TVMFFIAny,
496 num_args: i32,
497 result: *mut TVMFFIAny,
498 ) -> i32;
499 pub fn TVMFFIErrorMoveFromRaised(result: *mut TVMFFIObjectHandle);
500 pub fn TVMFFIErrorSetRaised(error: TVMFFIObjectHandle);
501 pub fn TVMFFIErrorSetRaisedFromCStr(kind: *const i8, message: *const i8);
502 pub fn TVMFFIErrorCreate(
503 kind: *const TVMFFIByteArray,
504 message: *const TVMFFIByteArray,
505 backtrace: *const TVMFFIByteArray,
506 out: *mut TVMFFIObjectHandle,
507 ) -> i32;
508 pub fn TVMFFIErrorCreateWithCauseAndExtraContext(
509 kind: *const TVMFFIByteArray,
510 message: *const TVMFFIByteArray,
511 backtrace: *const TVMFFIByteArray,
512 cause_chain: TVMFFIObjectHandle,
513 extra_context: TVMFFIObjectHandle,
514 out: *mut TVMFFIObjectHandle,
515 ) -> i32;
516 pub fn TVMFFITensorFromDLPack(
517 from: *mut c_void,
518 require_alignment: i32,
519 require_contiguous: i32,
520 out: *mut TVMFFIObjectHandle,
521 ) -> i32;
522 pub fn TVMFFITensorToDLPack(from: TVMFFIObjectHandle, out: *mut *mut c_void) -> i32;
523 pub fn TVMFFITensorFromDLPackVersioned(
524 from: *mut c_void,
525 require_alignment: i32,
526 require_contiguous: i32,
527 out: *mut TVMFFIObjectHandle,
528 ) -> i32;
529 pub fn TVMFFITensorToDLPackVersioned(from: TVMFFIObjectHandle, out: *mut *mut c_void) -> i32;
530 pub fn TVMFFIStringFromByteArray(input: *const TVMFFIByteArray, out: *mut TVMFFIAny) -> i32;
531 pub fn TVMFFIBytesFromByteArray(input: *const TVMFFIByteArray, out: *mut TVMFFIAny) -> i32;
532 pub fn TVMFFIDataTypeFromString(str: *const TVMFFIByteArray, out: *mut DLDataType) -> i32;
533 pub fn TVMFFIDataTypeToString(dtype: *const DLDataType, out: *mut TVMFFIAny) -> i32;
534 pub fn TVMFFITraceback(
535 filename: *const i8,
536 lineno: i32,
537 func: *const i8,
538 cross_ffi_boundary: i32,
539 ) -> *const TVMFFIByteArray;
540 pub fn TVMFFIGetTypeInfo(type_index: i32) -> *const TVMFFITypeInfo;
541 pub fn TVMFFITestingDummyTarget() -> i32;
542}