Skip to main content

rustc_codegen_llvm/llvm/
enzyme_ffi.rs

1#![expect(dead_code)]
2
3use libc::{c_char, c_uchar, c_uint};
4
5use super::MetadataKindId;
6use super::ffi::{AttributeKind, BasicBlock, Metadata, Module, Type, Value};
7use crate::llvm::{Bool, Builder, Context};
8
9// TypeTree types
10pub(crate) type CTypeTreeRef = *mut EnzymeTypeTree;
11
12#[repr(C)]
13#[derive(#[automatically_derived]impl ::core::fmt::Debug for EnzymeTypeTree {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field1_finish(f,
            "EnzymeTypeTree _unused", &&self._unused)
    }
}Debug, #[automatically_derived]impl ::core::marker::Copy for EnzymeTypeTree { }Copy, #[automatically_derived]#[doc(hidden)]unsafe impl ::core::clone::TrivialClone
    for EnzymeTypeTree {
}
#[automatically_derived]impl ::core::clone::Clone for EnzymeTypeTree {
    #[inline]
    fn clone(&self) -> Self {
        let _: ::core::clone::AssertParamIsClone<[u8; 0]>;
        *self
    }
}Clone)]
14pub(crate) struct EnzymeTypeTree {
15    _unused: [u8; 0],
16}
17
18#[repr(u32)]
19#[derive(#[automatically_derived]#[allow(non_camel_case_types)]
impl ::core::fmt::Debug for CConcreteType {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_c_like_enum_write_str(f,
            "DT_Anything DT_Integer DT_Pointer DT_Half DT_Float DT_Double DT_Unknown DT_FP128",
            match self {
                CConcreteType::DT_Anything => 0usize,
                CConcreteType::DT_Integer => 12usize,
                CConcreteType::DT_Pointer => 23usize,
                CConcreteType::DT_Half => 34usize,
                CConcreteType::DT_Float => 42usize,
                CConcreteType::DT_Double => 51usize,
                CConcreteType::DT_Unknown => 61usize,
                CConcreteType::DT_FP128 => 72usize,
            })
    }
}Debug, #[automatically_derived]#[allow(non_camel_case_types)]
impl ::core::marker::Copy for CConcreteType { }Copy, #[automatically_derived]#[doc(hidden)]#[allow(non_camel_case_types)]
unsafe impl ::core::clone::TrivialClone for CConcreteType { }
#[automatically_derived]#[allow(non_camel_case_types)]
impl ::core::clone::Clone for CConcreteType {
    #[inline]
    fn clone(&self) -> Self { *self }
}Clone, #[automatically_derived]#[allow(non_camel_case_types)]
impl ::core::hash::Hash for CConcreteType {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        ::core::hash::Hash::hash(&::core::intrinsics::discriminant_value(self),
            state)
    }
}Hash, #[automatically_derived]#[allow(non_camel_case_types)]
impl ::core::marker::StructuralPartialEq for CConcreteType { }
#[automatically_derived]#[allow(non_camel_case_types)]
impl ::core::cmp::PartialEq for CConcreteType {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        ::core::intrinsics::discriminant_value(self) ==
            ::core::intrinsics::discriminant_value(other)
    }
}PartialEq, #[automatically_derived]#[allow(non_camel_case_types)]
impl ::core::cmp::Eq for CConcreteType { }Eq)]
20#[allow(non_camel_case_types)]
21pub(crate) enum CConcreteType {
22    DT_Anything = 0,
23    DT_Integer = 1,
24    DT_Pointer = 2,
25    DT_Half = 3,
26    DT_Float = 4,
27    DT_Double = 5,
28    DT_Unknown = 6,
29    DT_FP128 = 9,
30}
31
32pub(crate) struct TypeTree {
33    pub(crate) inner: CTypeTreeRef,
34}
35
36#[link(name = "llvm-wrapper", kind = "static")]
37unsafe extern "C" {
38    // Enzyme
39    pub(crate) safe fn LLVMRustHasMetadata(I: &Value, KindID: MetadataKindId) -> bool;
40    pub(crate) fn LLVMRustEraseInstUntilInclusive(BB: &BasicBlock, I: &Value);
41    pub(crate) fn LLVMRustGetLastInstruction<'a>(BB: &BasicBlock) -> Option<&'a Value>;
42    pub(crate) fn LLVMRustDIGetInstMetadata(I: &Value) -> Option<&Metadata>;
43    pub(crate) fn LLVMRustEraseInstFromParent(V: &Value);
44    pub(crate) fn LLVMRustGetTerminator<'a>(B: &BasicBlock) -> &'a Value;
45    pub(crate) fn LLVMRustVerifyFunction(V: &Value, action: LLVMRustVerifierFailureAction) -> Bool;
46    pub(crate) fn LLVMRustHasAttributeAtIndex(V: &Value, i: c_uint, Kind: AttributeKind) -> bool;
47    pub(crate) fn LLVMRustGetArrayNumElements(Ty: &Type) -> u64;
48    pub(crate) fn LLVMRustHasFnAttribute(
49        F: &Value,
50        Name: *const c_uchar, // See "PTR_LEN_STR".
51        NameLen: libc::size_t,
52    ) -> bool;
53    pub(crate) fn LLVMRustRemoveFnAttribute(
54        F: &Value,
55        Name: *const c_uchar, // See "PTR_LEN_STR".
56        NameLen: libc::size_t,
57    );
58    pub(crate) fn LLVMGetFirstFunction<'ll>(M: Module<'ll>) -> Option<&'ll Value>;
59    pub(crate) fn LLVMGetNextFunction(Fn: &Value) -> Option<&Value>;
60    pub(crate) fn LLVMRustRemoveEnumAttributeAtIndex(
61        Fn: &Value,
62        index: c_uint,
63        kind: AttributeKind,
64    );
65    pub(crate) fn LLVMRustPositionBefore<'a>(B: &'a Builder<'_>, I: &'a Value);
66    pub(crate) fn LLVMRustPositionAfter<'a>(B: &'a Builder<'_>, I: &'a Value);
67    pub(crate) fn LLVMRustGetFunctionCall(
68        F: &Value,
69        name: *const c_char,
70        NameLen: libc::size_t,
71    ) -> Option<&Value>;
72
73    pub(crate) safe fn LLVMRustIsCall(V: &Value) -> bool;
74}
75
76unsafe extern "C" {
77    // Enzyme
78    pub(crate) fn LLVMDumpModule<'ll>(M: Module<'ll>);
79    pub(crate) fn LLVMDumpValue(V: &Value);
80    pub(crate) fn LLVMGetFunctionCallConv(F: &Value) -> c_uint;
81    pub(crate) fn LLVMGetParams(Fnc: &Value, params: *mut &Value);
82    pub(crate) fn LLVMGetNamedFunction<'ll>(
83        M: Module<'ll>,
84        Name: *const c_char,
85    ) -> Option<&'ll Value>;
86}
87
88#[repr(C)]
89#[derive(#[automatically_derived]impl ::core::marker::Copy for
    LLVMRustVerifierFailureAction {
}Copy, #[automatically_derived]#[doc(hidden)]unsafe impl ::core::clone::TrivialClone
    for LLVMRustVerifierFailureAction {
}
#[automatically_derived]impl ::core::clone::Clone for
    LLVMRustVerifierFailureAction {
    #[inline]
    fn clone(&self) -> Self { *self }
}Clone, #[automatically_derived]impl ::core::marker::StructuralPartialEq for
    LLVMRustVerifierFailureAction {
}
#[automatically_derived]impl ::core::cmp::PartialEq for
    LLVMRustVerifierFailureAction {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        ::core::intrinsics::discriminant_value(self) ==
            ::core::intrinsics::discriminant_value(other)
    }
}PartialEq)]
90pub(crate) enum LLVMRustVerifierFailureAction {
91    LLVMAbortProcessAction = 0,
92    LLVMPrintMessageAction = 1,
93    LLVMReturnStatusAction = 2,
94}
95
96pub(crate) use self::Enzyme_AD::*;
97
98pub(crate) mod Enzyme_AD {
99    use std::ffi::{c_char, c_void};
100    use std::path::PathBuf;
101    use std::sync::{Mutex, MutexGuard, OnceLock};
102
103    use rustc_session::config::{Sysroot, host_tuple};
104    use rustc_session::filesearch;
105    use rustc_span::bug;
106
107    use super::{CConcreteType, CTypeTreeRef, Context};
108    use crate::llvm::{self, EnzymeTypeTree};
109
110    type EnzymeSetCLBoolFn = unsafe extern "C" fn(*mut c_void, u8);
111    type EnzymeSetCLStringFn = unsafe extern "C" fn(*mut c_void, *const c_char);
112
113    type EnzymeNewTypeTreeFn = unsafe extern "C" fn() -> CTypeTreeRef;
114    type EnzymeNewTypeTreeCTFn =
115        for<'ll> unsafe extern "C" fn(CConcreteType, Context<'ll>) -> CTypeTreeRef;
116    type EnzymeNewTypeTreeTRFn = unsafe extern "C" fn(CTypeTreeRef) -> CTypeTreeRef;
117    type EnzymeFreeTypeTreeFn = unsafe extern "C" fn(CTypeTreeRef);
118    type EnzymeMergeTypeTreeFn = unsafe extern "C" fn(CTypeTreeRef, CTypeTreeRef) -> bool;
119    type EnzymeTypeTreeOnlyEqFn = unsafe extern "C" fn(CTypeTreeRef, i64);
120    type EnzymeTypeTreeData0EqFn = unsafe extern "C" fn(CTypeTreeRef);
121    type EnzymeTypeTreeShiftIndiciesEqFn =
122        unsafe extern "C" fn(CTypeTreeRef, *const c_char, i64, i64, u64);
123    type EnzymeTypeTreeInsertEqFn =
124        for<'ll> unsafe extern "C" fn(CTypeTreeRef, *const i64, usize, CConcreteType, Context<'ll>);
125    type EnzymeTypeTreeToStringFn = unsafe extern "C" fn(CTypeTreeRef) -> *const c_char;
126    type EnzymeTypeTreeToStringFreeFn = unsafe extern "C" fn(*const c_char);
127
128    #[allow(non_snake_case)]
129    pub(crate) struct EnzymeWrapper {
130        EnzymeNewTypeTree: EnzymeNewTypeTreeFn,
131        EnzymeNewTypeTreeCT: EnzymeNewTypeTreeCTFn,
132        EnzymeNewTypeTreeTR: EnzymeNewTypeTreeTRFn,
133        EnzymeFreeTypeTree: EnzymeFreeTypeTreeFn,
134        EnzymeMergeTypeTree: EnzymeMergeTypeTreeFn,
135        EnzymeTypeTreeOnlyEq: EnzymeTypeTreeOnlyEqFn,
136        EnzymeTypeTreeData0Eq: EnzymeTypeTreeData0EqFn,
137        EnzymeTypeTreeShiftIndiciesEq: EnzymeTypeTreeShiftIndiciesEqFn,
138        EnzymeTypeTreeInsertEq: EnzymeTypeTreeInsertEqFn,
139        EnzymeTypeTreeToString: EnzymeTypeTreeToStringFn,
140        EnzymeTypeTreeToStringFree: EnzymeTypeTreeToStringFreeFn,
141
142        EnzymePrintPerf: *mut c_void,
143        EnzymePrintActivity: *mut c_void,
144        EnzymePrintType: *mut c_void,
145        EnzymeFunctionToAnalyze: *mut c_void,
146        EnzymePrint: *mut c_void,
147        EnzymeStrictAliasing: *mut c_void,
148        EnzymeInline: *mut c_void,
149        EnzymeMaxTypeDepth: *mut c_void,
150        RustTypeRules: *mut c_void,
151        looseTypeAnalysis: *mut c_void,
152
153        EnzymeSetCLBool: EnzymeSetCLBoolFn,
154        EnzymeSetCLString: EnzymeSetCLStringFn,
155        pub registerEnzymeAndPassPipeline: *const c_void,
156        lib: libloading::Library,
157    }
158
159    unsafe impl Sync for EnzymeWrapper {}
160    unsafe impl Send for EnzymeWrapper {}
161
162    fn load_ptr_by_symbol_mut_void(
163        lib: &libloading::Library,
164        bytes: &[u8],
165    ) -> Result<*mut c_void, libloading::Error> {
166        unsafe {
167            let s: libloading::Symbol<'_, *mut c_void> = lib.get(bytes)?;
168            // libloading = 0.9.0: try_as_raw_ptr always succeeds and returns Some
169            let s = s.try_as_raw_ptr().unwrap();
170            Ok(s)
171        }
172    }
173
174    // e.g.
175    // load_ptrs_by_symbols_mut_void(ABC, XYZ);
176    // =>
177    // let ABC = load_ptr_mut_void(&lib, b"ABC")?;
178    // let XYZ = load_ptr_mut_void(&lib, b"XYZ")?;
179    macro_rules! load_ptrs_by_symbols_mut_void {
180        ($lib:expr, $($name:ident),* $(,)?) => {
181            $(
182                #[allow(non_snake_case)]
183                let $name = load_ptr_by_symbol_mut_void(&$lib, stringify!($name).as_bytes())?;
184            )*
185        };
186    }
187
188    // e.g.
189    // load_ptrs_by_symbols_fn(ABC: ABCFn, XYZ: XYZFn);
190    // =>
191    // let ABC: libloading::Symbol<'_, ABCFn> = unsafe { lib.get(b"ABC")? };
192    // let XYZ: libloading::Symbol<'_, XYZFn> = unsafe { lib.get(b"XYZ")? };
193    macro_rules! load_ptrs_by_symbols_fn {
194        ($lib:expr, $($name:ident : $ty:ty),* $(,)?) => {
195            $(
196                #[allow(non_snake_case)]
197                let $name: $ty = *unsafe { $lib.get::<$ty>(stringify!($name).as_bytes())? };
198            )*
199        };
200    }
201
202    static ENZYME_INSTANCE: OnceLock<Mutex<EnzymeWrapper>> = OnceLock::new();
203
204    #[derive(#[automatically_derived]impl ::core::fmt::Debug for EnzymeLibraryError {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            Self::NotFound { err: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "NotFound err", &__self_0),
            Self::LoadFailed { err: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "LoadFailed err", &__self_0),
        }
    }
}Debug)]
205    pub(crate) enum EnzymeLibraryError {
206        NotFound { err: String },
207        LoadFailed { err: String },
208    }
209
210    impl From<libloading::Error> for EnzymeLibraryError {
211        fn from(err: libloading::Error) -> Self {
212            Self::LoadFailed { err: ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0:?}", err))
    })format!("{err:?}") }
213        }
214    }
215
216    impl EnzymeWrapper {
217        /// Initialize EnzymeWrapper with the given sysroot if not already initialized.
218        /// Safe to call multiple times - subsequent calls are no-ops due to OnceLock.
219        pub(crate) fn get_or_init(
220            sysroot: &rustc_session::config::Sysroot,
221        ) -> Result<MutexGuard<'static, Self>, EnzymeLibraryError> {
222            let mtx: &'static Mutex<EnzymeWrapper> = ENZYME_INSTANCE.get_or_try_init(|| {
223                let w = Self::call_dynamic(sysroot)?;
224                Ok::<_, EnzymeLibraryError>(Mutex::new(w))
225            })?;
226
227            Ok(mtx.lock().unwrap())
228        }
229
230        /// Get the EnzymeWrapper instance. Panics if not initialized.
231        pub(crate) fn get_instance() -> MutexGuard<'static, Self> {
232            ENZYME_INSTANCE
233                .get()
234                .expect("EnzymeWrapper not initialized. Call get_or_init with sysroot first.")
235                .lock()
236                .unwrap()
237        }
238
239        pub(crate) fn new_type_tree(&self) -> CTypeTreeRef {
240            unsafe { (self.EnzymeNewTypeTree)() }
241        }
242
243        pub(crate) fn new_type_tree_ct<'ll>(
244            &self,
245            t: CConcreteType,
246            ctx: Context<'ll>,
247        ) -> *mut EnzymeTypeTree {
248            unsafe { (self.EnzymeNewTypeTreeCT)(t, ctx) }
249        }
250
251        pub(crate) fn new_type_tree_tr(&self, tree: CTypeTreeRef) -> CTypeTreeRef {
252            unsafe { (self.EnzymeNewTypeTreeTR)(tree) }
253        }
254
255        pub(crate) fn free_type_tree(&self, tree: CTypeTreeRef) {
256            unsafe { (self.EnzymeFreeTypeTree)(tree) }
257        }
258
259        pub(crate) fn merge_type_tree(&self, tree1: CTypeTreeRef, tree2: CTypeTreeRef) -> bool {
260            unsafe { (self.EnzymeMergeTypeTree)(tree1, tree2) }
261        }
262
263        pub(crate) fn tree_only_eq(&self, tree: CTypeTreeRef, num: i64) {
264            unsafe { (self.EnzymeTypeTreeOnlyEq)(tree, num) }
265        }
266
267        pub(crate) fn tree_data0_eq(&self, tree: CTypeTreeRef) {
268            unsafe { (self.EnzymeTypeTreeData0Eq)(tree) }
269        }
270
271        pub(crate) fn shift_indicies_eq(
272            &self,
273            tree: CTypeTreeRef,
274            data_layout: *const c_char,
275            offset: i64,
276            max_size: i64,
277            add_offset: u64,
278        ) {
279            unsafe {
280                (self.EnzymeTypeTreeShiftIndiciesEq)(
281                    tree,
282                    data_layout,
283                    offset,
284                    max_size,
285                    add_offset,
286                )
287            }
288        }
289
290        pub(crate) fn tree_insert_eq<'ll>(
291            &self,
292            tree: CTypeTreeRef,
293            indices: *const i64,
294            len: usize,
295            ct: CConcreteType,
296            ctx: Context<'ll>,
297        ) {
298            unsafe { (self.EnzymeTypeTreeInsertEq)(tree, indices, len, ct, ctx) }
299        }
300
301        pub(crate) fn tree_to_string(&self, tree: *mut EnzymeTypeTree) -> *const c_char {
302            unsafe { (self.EnzymeTypeTreeToString)(tree) }
303        }
304
305        pub(crate) fn tree_to_cstr(&self, tree: *mut EnzymeTypeTree) -> &std::ffi::CStr {
306            let c_str = self.tree_to_string(tree);
307            unsafe { std::ffi::CStr::from_ptr(c_str) }
308        }
309
310        pub(crate) fn tree_to_string_free(&self, ch: *const c_char) {
311            unsafe { (self.EnzymeTypeTreeToStringFree)(ch) }
312        }
313
314        pub(crate) fn get_max_type_depth(&self) -> usize {
315            unsafe { std::ptr::read::<u32>(self.EnzymeMaxTypeDepth as *const u32) as usize }
316        }
317
318        pub(crate) fn set_print_perf(&mut self, print: bool) {
319            unsafe {
320                (self.EnzymeSetCLBool)(self.EnzymePrintPerf, print as u8);
321            }
322        }
323
324        pub(crate) fn set_print_activity(&mut self, print: bool) {
325            unsafe {
326                (self.EnzymeSetCLBool)(self.EnzymePrintActivity, print as u8);
327            }
328        }
329
330        pub(crate) fn set_print_type(&mut self, print: bool) {
331            unsafe {
332                (self.EnzymeSetCLBool)(self.EnzymePrintType, print as u8);
333            }
334        }
335
336        pub(crate) fn set_print_type_fun(&mut self, fun_name: &str) {
337            let c_fun_name = std::ffi::CString::new(fun_name)
338                .unwrap_or_else(|err| ::rustc_span::macros::bug_impl(None,
    format_args!("failed to set_print_type_fun: {0}", err),
    Location::caller())bug!("failed to set_print_type_fun: {err}"));
339            unsafe {
340                (self.EnzymeSetCLString)(self.EnzymeFunctionToAnalyze, c_fun_name.as_ptr());
341            }
342        }
343
344        pub(crate) fn set_print(&mut self, print: bool) {
345            unsafe {
346                (self.EnzymeSetCLBool)(self.EnzymePrint, print as u8);
347            }
348        }
349
350        pub(crate) fn set_strict_aliasing(&mut self, strict: bool) {
351            unsafe {
352                (self.EnzymeSetCLBool)(self.EnzymeStrictAliasing, strict as u8);
353            }
354        }
355
356        pub(crate) fn set_loose_types(&mut self, loose: bool) {
357            unsafe {
358                (self.EnzymeSetCLBool)(self.looseTypeAnalysis, loose as u8);
359            }
360        }
361
362        pub(crate) fn set_inline(&mut self, val: bool) {
363            unsafe {
364                (self.EnzymeSetCLBool)(self.EnzymeInline, val as u8);
365            }
366        }
367
368        pub(crate) fn set_rust_rules(&mut self, val: bool) {
369            unsafe {
370                (self.EnzymeSetCLBool)(self.RustTypeRules, val as u8);
371            }
372        }
373
374        #[allow(non_snake_case)]
375        fn call_dynamic(
376            sysroot: &rustc_session::config::Sysroot,
377        ) -> Result<Self, EnzymeLibraryError> {
378            let enzyme_path = Self::get_enzyme_path(sysroot)?;
379            let lib = unsafe { libloading::Library::new(enzyme_path)? };
380
381            #[allow(non_snake_case)]
let EnzymeNewTypeTree: EnzymeNewTypeTreeFn =
    *unsafe {
            lib.get::<EnzymeNewTypeTreeFn>("EnzymeNewTypeTree".as_bytes())?
        };
#[allow(non_snake_case)]
let EnzymeNewTypeTreeCT: EnzymeNewTypeTreeCTFn =
    *unsafe {
            lib.get::<EnzymeNewTypeTreeCTFn>("EnzymeNewTypeTreeCT".as_bytes())?
        };
#[allow(non_snake_case)]
let EnzymeNewTypeTreeTR: EnzymeNewTypeTreeTRFn =
    *unsafe {
            lib.get::<EnzymeNewTypeTreeTRFn>("EnzymeNewTypeTreeTR".as_bytes())?
        };
#[allow(non_snake_case)]
let EnzymeFreeTypeTree: EnzymeFreeTypeTreeFn =
    *unsafe {
            lib.get::<EnzymeFreeTypeTreeFn>("EnzymeFreeTypeTree".as_bytes())?
        };
#[allow(non_snake_case)]
let EnzymeMergeTypeTree: EnzymeMergeTypeTreeFn =
    *unsafe {
            lib.get::<EnzymeMergeTypeTreeFn>("EnzymeMergeTypeTree".as_bytes())?
        };
#[allow(non_snake_case)]
let EnzymeTypeTreeOnlyEq: EnzymeTypeTreeOnlyEqFn =
    *unsafe {
            lib.get::<EnzymeTypeTreeOnlyEqFn>("EnzymeTypeTreeOnlyEq".as_bytes())?
        };
#[allow(non_snake_case)]
let EnzymeTypeTreeData0Eq: EnzymeTypeTreeData0EqFn =
    *unsafe {
            lib.get::<EnzymeTypeTreeData0EqFn>("EnzymeTypeTreeData0Eq".as_bytes())?
        };
#[allow(non_snake_case)]
let EnzymeTypeTreeShiftIndiciesEq: EnzymeTypeTreeShiftIndiciesEqFn =
    *unsafe {
            lib.get::<EnzymeTypeTreeShiftIndiciesEqFn>("EnzymeTypeTreeShiftIndiciesEq".as_bytes())?
        };
#[allow(non_snake_case)]
let EnzymeTypeTreeInsertEq: EnzymeTypeTreeInsertEqFn =
    *unsafe {
            lib.get::<EnzymeTypeTreeInsertEqFn>("EnzymeTypeTreeInsertEq".as_bytes())?
        };
#[allow(non_snake_case)]
let EnzymeTypeTreeToString: EnzymeTypeTreeToStringFn =
    *unsafe {
            lib.get::<EnzymeTypeTreeToStringFn>("EnzymeTypeTreeToString".as_bytes())?
        };
#[allow(non_snake_case)]
let EnzymeTypeTreeToStringFree: EnzymeTypeTreeToStringFreeFn =
    *unsafe {
            lib.get::<EnzymeTypeTreeToStringFreeFn>("EnzymeTypeTreeToStringFree".as_bytes())?
        };
#[allow(non_snake_case)]
let EnzymeSetCLBool: EnzymeSetCLBoolFn =
    *unsafe { lib.get::<EnzymeSetCLBoolFn>("EnzymeSetCLBool".as_bytes())? };
#[allow(non_snake_case)]
let EnzymeSetCLString: EnzymeSetCLStringFn =
    *unsafe {
            lib.get::<EnzymeSetCLStringFn>("EnzymeSetCLString".as_bytes())?
        };load_ptrs_by_symbols_fn!(
382                lib,
383                EnzymeNewTypeTree: EnzymeNewTypeTreeFn,
384                EnzymeNewTypeTreeCT: EnzymeNewTypeTreeCTFn,
385                EnzymeNewTypeTreeTR: EnzymeNewTypeTreeTRFn,
386                EnzymeFreeTypeTree: EnzymeFreeTypeTreeFn,
387                EnzymeMergeTypeTree: EnzymeMergeTypeTreeFn,
388                EnzymeTypeTreeOnlyEq: EnzymeTypeTreeOnlyEqFn,
389                EnzymeTypeTreeData0Eq: EnzymeTypeTreeData0EqFn,
390                EnzymeTypeTreeShiftIndiciesEq: EnzymeTypeTreeShiftIndiciesEqFn,
391                EnzymeTypeTreeInsertEq: EnzymeTypeTreeInsertEqFn,
392                EnzymeTypeTreeToString: EnzymeTypeTreeToStringFn,
393                EnzymeTypeTreeToStringFree: EnzymeTypeTreeToStringFreeFn,
394                EnzymeSetCLBool: EnzymeSetCLBoolFn,
395                EnzymeSetCLString: EnzymeSetCLStringFn,
396            );
397
398            #[allow(non_snake_case)]
let registerEnzymeAndPassPipeline =
    load_ptr_by_symbol_mut_void(&lib,
            "registerEnzymeAndPassPipeline".as_bytes())?;
#[allow(non_snake_case)]
let EnzymePrintPerf =
    load_ptr_by_symbol_mut_void(&lib, "EnzymePrintPerf".as_bytes())?;
#[allow(non_snake_case)]
let EnzymePrintActivity =
    load_ptr_by_symbol_mut_void(&lib, "EnzymePrintActivity".as_bytes())?;
#[allow(non_snake_case)]
let EnzymePrintType =
    load_ptr_by_symbol_mut_void(&lib, "EnzymePrintType".as_bytes())?;
#[allow(non_snake_case)]
let EnzymeFunctionToAnalyze =
    load_ptr_by_symbol_mut_void(&lib, "EnzymeFunctionToAnalyze".as_bytes())?;
#[allow(non_snake_case)]
let EnzymePrint =
    load_ptr_by_symbol_mut_void(&lib, "EnzymePrint".as_bytes())?;
#[allow(non_snake_case)]
let EnzymeStrictAliasing =
    load_ptr_by_symbol_mut_void(&lib, "EnzymeStrictAliasing".as_bytes())?;
#[allow(non_snake_case)]
let EnzymeInline =
    load_ptr_by_symbol_mut_void(&lib, "EnzymeInline".as_bytes())?;
#[allow(non_snake_case)]
let EnzymeMaxTypeDepth =
    load_ptr_by_symbol_mut_void(&lib, "EnzymeMaxTypeDepth".as_bytes())?;
#[allow(non_snake_case)]
let RustTypeRules =
    load_ptr_by_symbol_mut_void(&lib, "RustTypeRules".as_bytes())?;
#[allow(non_snake_case)]
let looseTypeAnalysis =
    load_ptr_by_symbol_mut_void(&lib, "looseTypeAnalysis".as_bytes())?;load_ptrs_by_symbols_mut_void!(
399                lib,
400                registerEnzymeAndPassPipeline,
401                EnzymePrintPerf,
402                EnzymePrintActivity,
403                EnzymePrintType,
404                EnzymeFunctionToAnalyze,
405                EnzymePrint,
406                EnzymeStrictAliasing,
407                EnzymeInline,
408                EnzymeMaxTypeDepth,
409                RustTypeRules,
410                looseTypeAnalysis,
411            );
412
413            Ok(Self {
414                EnzymeNewTypeTree,
415                EnzymeNewTypeTreeCT,
416                EnzymeNewTypeTreeTR,
417                EnzymeFreeTypeTree,
418                EnzymeMergeTypeTree,
419                EnzymeTypeTreeOnlyEq,
420                EnzymeTypeTreeData0Eq,
421                EnzymeTypeTreeShiftIndiciesEq,
422                EnzymeTypeTreeInsertEq,
423                EnzymeTypeTreeToString,
424                EnzymeTypeTreeToStringFree,
425                EnzymePrintPerf,
426                EnzymePrintActivity,
427                EnzymePrintType,
428                EnzymeFunctionToAnalyze,
429                EnzymePrint,
430                EnzymeStrictAliasing,
431                EnzymeInline,
432                EnzymeMaxTypeDepth,
433                RustTypeRules,
434                looseTypeAnalysis,
435                EnzymeSetCLBool,
436                EnzymeSetCLString,
437                registerEnzymeAndPassPipeline,
438                lib,
439            })
440        }
441
442        fn get_enzyme_path(sysroot: &Sysroot) -> Result<PathBuf, EnzymeLibraryError> {
443            let llvm_version_major = llvm::LLVMRustVersionMajor();
444            let mut searched = Vec::new();
445
446            for p in sysroot.all_paths() {
447                let libdir = filesearch::make_target_lib_path(p, host_tuple());
448
449                let enzyme_path = libdir
450                    .join(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("libEnzyme-{0}",
                llvm_version_major))
    })format!("libEnzyme-{llvm_version_major}"))
451                    .with_extension(std::env::consts::DLL_EXTENSION);
452
453                if enzyme_path.exists() {
454                    return Ok(enzyme_path);
455                }
456
457                searched.push(libdir);
458            }
459
460            Err(EnzymeLibraryError::NotFound {
461                err: ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("failed to find a `libEnzyme-{2}.{0}` in the sysroot candidates:\n* {1}",
                std::env::consts::DLL_EXTENSION,
                searched.iter().map(|p|
                                p.display().to_string()).collect::<Vec<_>>().join("\n* "),
                llvm_version_major))
    })format!(
462                    "failed to find a `libEnzyme-{llvm_version_major}.{}` \
463                    in the sysroot candidates:\n* {}",
464                    std::env::consts::DLL_EXTENSION,
465                    searched
466                        .iter()
467                        .map(|p| p.display().to_string())
468                        .collect::<Vec<_>>()
469                        .join("\n* ")
470                ),
471            })
472        }
473    }
474}
475
476impl TypeTree {
477    pub(crate) fn new() -> TypeTree {
478        let wrapper = EnzymeWrapper::get_instance();
479        let inner = wrapper.new_type_tree();
480        TypeTree { inner }
481    }
482
483    pub(crate) fn from_type<'ll>(t: CConcreteType, ctx: Context<'ll>) -> TypeTree {
484        let wrapper = EnzymeWrapper::get_instance();
485        let inner = wrapper.new_type_tree_ct(t, ctx);
486        TypeTree { inner }
487    }
488
489    pub(crate) fn merge(self, other: Self) -> Self {
490        let wrapper = EnzymeWrapper::get_instance();
491        wrapper.merge_type_tree(self.inner, other.inner);
492        drop(other);
493        self
494    }
495
496    #[must_use]
497    pub(crate) fn shift(
498        self,
499        layout: &str,
500        offset: isize,
501        max_size: isize,
502        add_offset: usize,
503    ) -> Self {
504        let layout = std::ffi::CString::new(layout).unwrap();
505        let wrapper = EnzymeWrapper::get_instance();
506        wrapper.shift_indicies_eq(
507            self.inner,
508            layout.as_ptr(),
509            offset as i64,
510            max_size as i64,
511            add_offset as u64,
512        );
513
514        self
515    }
516
517    pub(crate) fn insert<'ll>(&mut self, indices: &[i64], ct: CConcreteType, ctx: Context<'ll>) {
518        let wrapper = EnzymeWrapper::get_instance();
519        wrapper.tree_insert_eq(self.inner, indices.as_ptr(), indices.len(), ct, ctx);
520    }
521}
522
523impl Clone for TypeTree {
524    fn clone(&self) -> Self {
525        let wrapper = EnzymeWrapper::get_instance();
526        let inner = wrapper.new_type_tree_tr(self.inner);
527        TypeTree { inner }
528    }
529}
530
531impl std::fmt::Display for TypeTree {
532    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
533        let wrapper = EnzymeWrapper::get_instance();
534        let ptr = wrapper.tree_to_string(self.inner);
535        let cstr = unsafe { std::ffi::CStr::from_ptr(ptr) };
536        match cstr.to_str() {
537            Ok(x) => f.write_fmt(format_args!("{0}", x))write!(f, "{}", x)?,
538            Err(err) => f.write_fmt(format_args!("could not parse: {0}", err))write!(f, "could not parse: {}", err)?,
539        }
540
541        // delete C string pointer
542        wrapper.tree_to_string_free(ptr);
543
544        Ok(())
545    }
546}
547
548impl std::fmt::Debug for TypeTree {
549    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
550        <Self as std::fmt::Display>::fmt(self, f)
551    }
552}
553
554impl Drop for TypeTree {
555    fn drop(&mut self) {
556        let wrapper = EnzymeWrapper::get_instance();
557        wrapper.free_type_tree(self.inner)
558    }
559}