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}
305
306#[repr(C)]
308pub struct TVMFFIShapeCell {
309 pub data: *const i64,
310 pub size: usize,
311}
312
313pub type TVMFFIFieldGetter =
315 unsafe extern "C" fn(field: *mut c_void, result: *mut TVMFFIAny) -> i32;
316
317pub type TVMFFIFieldSetter =
319 unsafe extern "C" fn(field: *mut c_void, value: *const TVMFFIAny) -> i32;
320
321#[repr(C)]
323pub struct TVMFFIFieldInfo {
324 pub name: TVMFFIByteArray,
326 pub doc: TVMFFIByteArray,
328 pub metadata: TVMFFIByteArray,
330 pub flags: i64,
332 pub size: i64,
334 pub alignment: i64,
336 pub offset: i64,
338 pub getter: Option<TVMFFIFieldGetter>,
340 pub setter: *mut c_void,
349 pub default_value_or_factory: TVMFFIAny,
354 pub field_static_type_index: i32,
356}
357
358pub type TVMFFIObjectCreator = unsafe extern "C" fn(result: *mut TVMFFIObjectHandle) -> i32;
360
361#[repr(C)]
363pub struct TVMFFIMethodInfo {
364 pub name: TVMFFIByteArray,
366 pub doc: TVMFFIByteArray,
368 pub metadata: TVMFFIByteArray,
370 pub flags: i64,
372 pub method: TVMFFIAny,
375}
376
377#[repr(C)]
382pub struct TVMFFITypeMetadata {
383 pub doc: TVMFFIByteArray,
385 pub creator: Option<TVMFFIObjectCreator>,
387 pub total_size: i32,
391 pub structural_eq_hash_kind: i32,
393}
394
395#[repr(C)]
401pub struct TVMFFITypeAttrColumn {
402 pub data: *const TVMFFIAny,
404 pub size: i32,
407 pub begin_index: i32,
411}
412
413#[repr(C)]
415pub struct TVMFFITypeInfo {
416 pub type_index: i32,
419 pub type_depth: i32,
421 pub type_key: TVMFFIByteArray,
423 pub type_acenstors: *const *const TVMFFITypeInfo,
427 pub type_key_hash: u64,
429 pub num_fields: i32,
431 pub num_methods: i32,
433 pub fields: *const TVMFFIFieldInfo,
435 pub methods: *const TVMFFIMethodInfo,
437 pub metadata: *const TVMFFITypeMetadata,
439}
440
441#[repr(C)]
444pub struct TVMFFIObjectAllocHeader {
445 pub delete_space: Option<unsafe extern "C" fn(ptr: *mut c_void)>,
446}
447
448#[repr(C)]
450pub struct TVMFFICustomAllocator {
451 pub allocate: Option<
452 unsafe extern "C" fn(
453 size: usize,
454 alignment: usize,
455 type_index: i32,
456 context: *mut c_void,
457 ) -> *mut c_void,
458 >,
459 pub context: *mut c_void,
460}
461
462unsafe extern "C" {
463 pub fn TVMFFIGetCustomAllocator() -> *mut TVMFFICustomAllocator;
464 pub fn TVMFFISetCustomAllocator(allocator: *mut TVMFFICustomAllocator) -> i32;
465
466 pub fn TVMFFITypeKeyToIndex(type_key: *const TVMFFIByteArray, out_tindex: *mut i32) -> i32;
467 pub fn TVMFFITypeRegisterAttr(
468 type_index: i32,
469 attr_name: *const TVMFFIByteArray,
470 attr_value: *const TVMFFIAny,
471 ) -> i32;
472 pub fn TVMFFIGetTypeAttrColumn(
473 attr_name: *const TVMFFIByteArray,
474 ) -> *const TVMFFITypeAttrColumn;
475 pub fn TVMFFIFunctionGetGlobal(
476 name: *const TVMFFIByteArray,
477 out: *mut TVMFFIObjectHandle,
478 ) -> i32;
479 pub fn TVMFFIFunctionSetGlobal(
480 name: *const TVMFFIByteArray,
481 f: TVMFFIObjectHandle,
482 can_override: i32,
483 ) -> i32;
484 pub fn TVMFFIFunctionCreate(
485 self_ptr: *mut c_void,
486 safe_call: TVMFFISafeCallType,
487 deleter: Option<unsafe extern "C" fn(*mut c_void)>,
488 out: *mut TVMFFIObjectHandle,
489 ) -> i32;
490 pub fn TVMFFIAnyViewToOwnedAny(any_view: *const TVMFFIAny, out: *mut TVMFFIAny) -> i32;
491 pub fn TVMFFIFunctionCall(
492 func: TVMFFIObjectHandle,
493 args: *const TVMFFIAny,
494 num_args: i32,
495 result: *mut TVMFFIAny,
496 ) -> i32;
497 pub fn TVMFFIErrorMoveFromRaised(result: *mut TVMFFIObjectHandle);
498 pub fn TVMFFIErrorSetRaised(error: TVMFFIObjectHandle);
499 pub fn TVMFFIErrorSetRaisedFromCStr(kind: *const i8, message: *const i8);
500 pub fn TVMFFIErrorCreate(
501 kind: *const TVMFFIByteArray,
502 message: *const TVMFFIByteArray,
503 backtrace: *const TVMFFIByteArray,
504 out: *mut TVMFFIObjectHandle,
505 ) -> i32;
506 pub fn TVMFFITensorFromDLPack(
507 from: *mut c_void,
508 require_alignment: i32,
509 require_contiguous: i32,
510 out: *mut TVMFFIObjectHandle,
511 ) -> i32;
512 pub fn TVMFFITensorToDLPack(from: TVMFFIObjectHandle, out: *mut *mut c_void) -> i32;
513 pub fn TVMFFITensorFromDLPackVersioned(
514 from: *mut c_void,
515 require_alignment: i32,
516 require_contiguous: i32,
517 out: *mut TVMFFIObjectHandle,
518 ) -> i32;
519 pub fn TVMFFITensorToDLPackVersioned(from: TVMFFIObjectHandle, out: *mut *mut c_void) -> i32;
520 pub fn TVMFFIStringFromByteArray(input: *const TVMFFIByteArray, out: *mut TVMFFIAny) -> i32;
521 pub fn TVMFFIBytesFromByteArray(input: *const TVMFFIByteArray, out: *mut TVMFFIAny) -> i32;
522 pub fn TVMFFIDataTypeFromString(str: *const TVMFFIByteArray, out: *mut DLDataType) -> i32;
523 pub fn TVMFFIDataTypeToString(dtype: *const DLDataType, out: *mut TVMFFIAny) -> i32;
524 pub fn TVMFFITraceback(
525 filename: *const i8,
526 lineno: i32,
527 func: *const i8,
528 cross_ffi_boundary: i32,
529 ) -> *const TVMFFIByteArray;
530 pub fn TVMFFIGetTypeInfo(type_index: i32) -> *const TVMFFITypeInfo;
531 pub fn TVMFFITestingDummyTarget() -> i32;
532}