rustc_codegen_llvm/llvm/
enzyme_ffi.rs

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