rustc_codegen_llvm/llvm/
enzyme_ffi.rs

1#![allow(non_camel_case_types)]
2#![expect(dead_code)]
3
4use libc::{c_char, c_uint};
5
6use super::MetadataKindId;
7use super::ffi::{AttributeKind, BasicBlock, Metadata, Module, Type, Value};
8use crate::llvm::Bool;
9
10#[link(name = "llvm-wrapper", kind = "static")]
11unsafe extern "C" {
12    // Enzyme
13    pub(crate) safe fn LLVMRustHasMetadata(I: &Value, KindID: MetadataKindId) -> bool;
14    pub(crate) fn LLVMRustEraseInstUntilInclusive(BB: &BasicBlock, I: &Value);
15    pub(crate) fn LLVMRustGetLastInstruction<'a>(BB: &BasicBlock) -> Option<&'a Value>;
16    pub(crate) fn LLVMRustDIGetInstMetadata(I: &Value) -> Option<&Metadata>;
17    pub(crate) fn LLVMRustEraseInstFromParent(V: &Value);
18    pub(crate) fn LLVMRustGetTerminator<'a>(B: &BasicBlock) -> &'a Value;
19    pub(crate) fn LLVMRustVerifyFunction(V: &Value, action: LLVMRustVerifierFailureAction) -> Bool;
20    pub(crate) fn LLVMRustHasAttributeAtIndex(V: &Value, i: c_uint, Kind: AttributeKind) -> bool;
21    pub(crate) fn LLVMRustGetArrayNumElements(Ty: &Type) -> u64;
22    pub(crate) fn LLVMRustHasFnAttribute(
23        F: &Value,
24        Name: *const c_char,
25        NameLen: libc::size_t,
26    ) -> bool;
27    pub(crate) fn LLVMRustRemoveFnAttribute(F: &Value, Name: *const c_char, NameLen: libc::size_t);
28    pub(crate) fn LLVMGetFirstFunction(M: &Module) -> Option<&Value>;
29    pub(crate) fn LLVMGetNextFunction(Fn: &Value) -> Option<&Value>;
30    pub(crate) fn LLVMRustRemoveEnumAttributeAtIndex(
31        Fn: &Value,
32        index: c_uint,
33        kind: AttributeKind,
34    );
35}
36
37unsafe extern "C" {
38    // Enzyme
39    pub(crate) fn LLVMDumpModule(M: &Module);
40    pub(crate) fn LLVMDumpValue(V: &Value);
41    pub(crate) fn LLVMGetFunctionCallConv(F: &Value) -> c_uint;
42    pub(crate) fn LLVMGetReturnType(T: &Type) -> &Type;
43    pub(crate) fn LLVMGetParams(Fnc: &Value, parms: *mut &Value);
44    pub(crate) fn LLVMGetNamedFunction(M: &Module, Name: *const c_char) -> Option<&Value>;
45}
46
47#[repr(C)]
48#[derive(Copy, Clone, PartialEq)]
49pub(crate) enum LLVMRustVerifierFailureAction {
50    LLVMAbortProcessAction = 0,
51    LLVMPrintMessageAction = 1,
52    LLVMReturnStatusAction = 2,
53}
54
55#[cfg(llvm_enzyme)]
56pub(crate) use self::Enzyme_AD::*;
57
58#[cfg(llvm_enzyme)]
59pub(crate) mod Enzyme_AD {
60    use std::ffi::{CString, c_char};
61
62    use libc::c_void;
63
64    unsafe extern "C" {
65        pub(crate) fn EnzymeSetCLBool(arg1: *mut ::std::os::raw::c_void, arg2: u8);
66        pub(crate) fn EnzymeSetCLString(arg1: *mut ::std::os::raw::c_void, arg2: *const c_char);
67    }
68    unsafe extern "C" {
69        static mut EnzymePrintPerf: c_void;
70        static mut EnzymePrintActivity: c_void;
71        static mut EnzymePrintType: c_void;
72        static mut EnzymeFunctionToAnalyze: c_void;
73        static mut EnzymePrint: c_void;
74        static mut EnzymeStrictAliasing: c_void;
75        static mut looseTypeAnalysis: c_void;
76        static mut EnzymeInline: c_void;
77        static mut RustTypeRules: c_void;
78    }
79    pub(crate) fn set_print_perf(print: bool) {
80        unsafe {
81            EnzymeSetCLBool(std::ptr::addr_of_mut!(EnzymePrintPerf), print as u8);
82        }
83    }
84    pub(crate) fn set_print_activity(print: bool) {
85        unsafe {
86            EnzymeSetCLBool(std::ptr::addr_of_mut!(EnzymePrintActivity), print as u8);
87        }
88    }
89    pub(crate) fn set_print_type(print: bool) {
90        unsafe {
91            EnzymeSetCLBool(std::ptr::addr_of_mut!(EnzymePrintType), print as u8);
92        }
93    }
94    pub(crate) fn set_print_type_fun(fun_name: &str) {
95        let c_fun_name = CString::new(fun_name).unwrap();
96        unsafe {
97            EnzymeSetCLString(
98                std::ptr::addr_of_mut!(EnzymeFunctionToAnalyze),
99                c_fun_name.as_ptr() as *const c_char,
100            );
101        }
102    }
103    pub(crate) fn set_print(print: bool) {
104        unsafe {
105            EnzymeSetCLBool(std::ptr::addr_of_mut!(EnzymePrint), print as u8);
106        }
107    }
108    pub(crate) fn set_strict_aliasing(strict: bool) {
109        unsafe {
110            EnzymeSetCLBool(std::ptr::addr_of_mut!(EnzymeStrictAliasing), strict as u8);
111        }
112    }
113    pub(crate) fn set_loose_types(loose: bool) {
114        unsafe {
115            EnzymeSetCLBool(std::ptr::addr_of_mut!(looseTypeAnalysis), loose as u8);
116        }
117    }
118    pub(crate) fn set_inline(val: bool) {
119        unsafe {
120            EnzymeSetCLBool(std::ptr::addr_of_mut!(EnzymeInline), val as u8);
121        }
122    }
123    pub(crate) fn set_rust_rules(val: bool) {
124        unsafe {
125            EnzymeSetCLBool(std::ptr::addr_of_mut!(RustTypeRules), val as u8);
126        }
127    }
128}
129
130#[cfg(not(llvm_enzyme))]
131pub(crate) use self::Fallback_AD::*;
132
133#[cfg(not(llvm_enzyme))]
134pub(crate) mod Fallback_AD {
135    #![allow(unused_variables)]
136
137    pub(crate) fn set_inline(val: bool) {
138        unimplemented!()
139    }
140    pub(crate) fn set_print_perf(print: bool) {
141        unimplemented!()
142    }
143    pub(crate) fn set_print_activity(print: bool) {
144        unimplemented!()
145    }
146    pub(crate) fn set_print_type(print: bool) {
147        unimplemented!()
148    }
149    pub(crate) fn set_print_type_fun(fun_name: &str) {
150        unimplemented!()
151    }
152    pub(crate) fn set_print(print: bool) {
153        unimplemented!()
154    }
155    pub(crate) fn set_strict_aliasing(strict: bool) {
156        unimplemented!()
157    }
158    pub(crate) fn set_loose_types(loose: bool) {
159        unimplemented!()
160    }
161    pub(crate) fn set_rust_rules(val: bool) {
162        unimplemented!()
163    }
164}