@@ -51,6 +51,9 @@ unsafe extern "C" {
51
51
pub ( crate ) fn LLVMGetNamedFunction ( M : & Module , Name : * const c_char ) -> Option < & Value > ;
52
52
}
53
53
54
+
55
+
56
+
54
57
#[ repr( C ) ]
55
58
#[ derive( Copy , Clone , PartialEq ) ]
56
59
pub ( crate ) enum LLVMRustVerifierFailureAction {
@@ -62,76 +65,166 @@ pub(crate) enum LLVMRustVerifierFailureAction {
62
65
#[ cfg( llvm_enzyme) ]
63
66
pub ( crate ) use self :: Enzyme_AD :: * ;
64
67
65
- #[ cfg( llvm_enzyme) ]
68
+ // #[cfg(llvm_enzyme)]
66
69
pub ( crate ) mod Enzyme_AD {
67
- use std:: ffi:: { CString , c_char} ;
70
+ use std:: ffi:: CString ;
71
+ //use std::ffi::{CString, c_char};
68
72
69
73
use libc:: c_void;
70
74
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 ;
75
+ type SetFlag = unsafe extern "C" fn ( * mut c_void , u8 ) ;
76
+
77
+ #[ derive( Debug ) ]
78
+ pub ( crate ) struct EnzymeFns {
79
+ pub set_cl : SetFlag ,
80
+ }
81
+
82
+ #[ derive( Debug ) ]
83
+ pub ( crate ) struct EnzymeWrapper {
84
+ EnzymePrintPerf : * mut c_void ,
85
+ EnzymePrintActivity : * mut c_void ,
86
+ EnzymePrintType : * mut c_void ,
87
+ EnzymeFunctionToAnalyze : * mut c_void ,
88
+ EnzymePrint : * mut c_void ,
89
+ EnzymeStrictAliasing : * mut c_void ,
90
+ looseTypeAnalysis : * mut c_void ,
91
+ EnzymeInline : * mut c_void ,
92
+ RustTypeRules : * mut c_void ,
93
+
94
+ EnzymeSetCLBool : EnzymeFns ,
95
+ EnzymeSetCLString : EnzymeFns ,
96
+ pub registerEnzymeAndPassPipeline : * const c_void ,
97
+ lib : libloading:: Library ,
85
98
}
86
- pub ( crate ) fn set_print_perf ( print : bool ) {
87
- unsafe {
88
- EnzymeSetCLBool ( std:: ptr:: addr_of_mut!( EnzymePrintPerf ) , print as u8 ) ;
99
+ fn call_dynamic ( ) -> Result < EnzymeWrapper , Box < dyn std:: error:: Error > > {
100
+ fn load_ptr ( lib : & libloading:: Library , bytes : & [ u8 ] ) -> Result < * mut c_void , Box < dyn std:: error:: Error > > {
101
+ // Safety: symbol lookup from a loaded shared object.
102
+ unsafe {
103
+ let s: libloading:: Symbol < ' _ , * mut c_void > = lib. get ( bytes) ?;
104
+ let s = s. try_as_raw_ptr ( ) . unwrap ( ) ;
105
+ Ok ( s as * mut c_void )
106
+ }
107
+ }
108
+ dbg ! ( "starting" ) ;
109
+ dbg ! ( "Loading Enzyme" ) ;
110
+ let lib = unsafe { libloading:: Library :: new ( "/home/manuel/prog/rust/build/x86_64-unknown-linux-gnu/enzyme/lib/libEnzyme-21.so" ) ?} ;
111
+ dbg ! ( "second" ) ;
112
+ let EnzymeSetCLBool : libloading:: Symbol < ' _ , SetFlag > = unsafe { lib. get ( b"EnzymeSetCLBool" ) ?} ;
113
+ dbg ! ( "third" ) ;
114
+ let registerEnzymeAndPassPipeline =
115
+ load_ptr ( & lib, b"registerEnzymeAndPassPipeline" ) . unwrap ( ) as * const c_void ;
116
+ dbg ! ( "fourth" ) ;
117
+ //let EnzymeSetCLBool: libloading::Symbol<'_, unsafe extern "C" fn(&mut c_void, u8) -> ()> = unsafe{lib.get(b"registerEnzymeAndPassPipeline")?};
118
+ //let EnzymeSetCLBool = unsafe {EnzymeSetCLBool.try_as_raw_ptr().unwrap()};
119
+ let EnzymeSetCLString : libloading:: Symbol < ' _ , SetFlag > = unsafe { lib. get ( b"EnzymeSetCLString" ) ?} ;
120
+ dbg ! ( "done" ) ;
121
+ //let EnzymeSetCLString = unsafe {EnzymeSetCLString.try_as_raw_ptr().unwrap()};
122
+
123
+ let EnzymePrintPerf = load_ptr ( & lib, b"EnzymePrintPerf" ) . unwrap ( ) ;
124
+ let EnzymePrintActivity = load_ptr ( & lib, b"EnzymePrintActivity" ) . unwrap ( ) ;
125
+ let EnzymePrintType = load_ptr ( & lib, b"EnzymePrintType" ) . unwrap ( ) ;
126
+ let EnzymeFunctionToAnalyze = load_ptr ( & lib, b"EnzymeFunctionToAnalyze" ) . unwrap ( ) ;
127
+ let EnzymePrint = load_ptr ( & lib, b"EnzymePrint" ) . unwrap ( ) ;
128
+
129
+ let EnzymeStrictAliasing = load_ptr ( & lib, b"EnzymeStrictAliasing" ) . unwrap ( ) ;
130
+ let looseTypeAnalysis = load_ptr ( & lib, b"looseTypeAnalysis" ) . unwrap ( ) ;
131
+ let EnzymeInline = load_ptr ( & lib, b"EnzymeInline" ) . unwrap ( ) ;
132
+ let RustTypeRules = load_ptr ( & lib, b"RustTypeRules" ) . unwrap ( ) ;
133
+
134
+ let wrap = EnzymeWrapper {
135
+ EnzymePrintPerf ,
136
+ EnzymePrintActivity ,
137
+ EnzymePrintType ,
138
+ EnzymeFunctionToAnalyze ,
139
+ EnzymePrint ,
140
+ EnzymeStrictAliasing ,
141
+ looseTypeAnalysis,
142
+ EnzymeInline ,
143
+ RustTypeRules ,
144
+ //EnzymeSetCLBool: EnzymeFns {set_cl: unsafe{*EnzymeSetCLBool}},
145
+ //EnzymeSetCLString: EnzymeFns {set_cl: unsafe{*EnzymeSetCLString}},
146
+ EnzymeSetCLBool : EnzymeFns { set_cl : * EnzymeSetCLBool } ,
147
+ EnzymeSetCLString : EnzymeFns { set_cl : * EnzymeSetCLString } ,
148
+ registerEnzymeAndPassPipeline,
149
+ lib
150
+ } ;
151
+ dbg ! ( & wrap) ;
152
+ Ok ( wrap)
89
153
}
90
- }
91
- pub ( crate ) fn set_print_activity ( print : bool ) {
92
- unsafe {
93
- EnzymeSetCLBool ( std:: ptr:: addr_of_mut!( EnzymePrintActivity ) , print as u8 ) ;
154
+ use std:: sync:: Mutex ;
155
+ unsafe impl Sync for EnzymeWrapper { }
156
+ unsafe impl Send for EnzymeWrapper { }
157
+ impl EnzymeWrapper {
158
+ pub ( crate ) fn current ( ) -> & ' static Mutex < EnzymeWrapper > {
159
+ use std:: sync:: OnceLock ;
160
+ static CELL : OnceLock < Mutex < EnzymeWrapper > > = OnceLock :: new ( ) ;
161
+ fn init_enzyme ( ) -> Mutex < EnzymeWrapper > {
162
+ call_dynamic ( ) . unwrap ( ) . into ( )
163
+ }
164
+ CELL . get_or_init ( || init_enzyme ( ) )
94
165
}
95
- }
96
- pub ( crate ) fn set_print_type ( print : bool ) {
97
- unsafe {
98
- EnzymeSetCLBool ( std:: ptr:: addr_of_mut!( EnzymePrintType ) , print as u8 ) ;
166
+ pub ( crate ) fn set_print_perf ( & mut self , print : bool ) {
167
+ unsafe {
168
+ //(self.EnzymeSetCLBool.set_cl)(self.EnzymePrintPerf, print as u8);
169
+ //(self.EnzymeSetCLBool)(std::ptr::addr_of_mut!(self.EnzymePrintPerf), print as u8);
170
+ }
99
171
}
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
- ) ;
172
+
173
+ pub ( crate ) fn set_print_activity ( & mut self , print : bool ) {
174
+ unsafe {
175
+ //(self.EnzymeSetCLBool.set_cl)(self.EnzymePrintActivity, print as u8);
176
+ //(self.EnzymeSetCLBool)(std::ptr::addr_of_mut!(self.EnzymePrintActivity), print as u8);
177
+ }
108
178
}
109
- }
110
- pub ( crate ) fn set_print ( print : bool ) {
111
- unsafe {
112
- EnzymeSetCLBool ( std:: ptr:: addr_of_mut!( EnzymePrint ) , print as u8 ) ;
179
+
180
+ pub ( crate ) fn set_print_type ( & mut self , print : bool ) {
181
+ unsafe {
182
+ // (self.EnzymeSetCLBool.set_cl)(self.EnzymePrintType, print as u8);
183
+ }
113
184
}
114
- }
115
- pub ( crate ) fn set_strict_aliasing ( strict : bool ) {
116
- unsafe {
117
- EnzymeSetCLBool ( std:: ptr:: addr_of_mut!( EnzymeStrictAliasing ) , strict as u8 ) ;
185
+
186
+ pub ( crate ) fn set_print_type_fun ( & mut self , fun_name : & str ) {
187
+ let _c_fun_name = CString :: new ( fun_name) . unwrap ( ) ;
188
+ //unsafe {
189
+ // (self.EnzymeSetCLString.set_cl)(
190
+ // self.EnzymeFunctionToAnalyze,
191
+ // c_fun_name.as_ptr() as *const c_char,
192
+ // );
193
+ //}
118
194
}
119
- }
120
- pub ( crate ) fn set_loose_types ( loose : bool ) {
121
- unsafe {
122
- EnzymeSetCLBool ( std:: ptr:: addr_of_mut!( looseTypeAnalysis) , loose as u8 ) ;
195
+
196
+ pub ( crate ) fn set_print ( & mut self , print : bool ) {
197
+ unsafe {
198
+ //(self.EnzymeSetCLBool.set_cl)(self.EnzymePrint, print as u8);
199
+ }
123
200
}
124
- }
125
- pub ( crate ) fn set_inline ( val : bool ) {
126
- unsafe {
127
- EnzymeSetCLBool ( std:: ptr:: addr_of_mut!( EnzymeInline ) , val as u8 ) ;
201
+
202
+ pub ( crate ) fn set_strict_aliasing ( & mut self , strict : bool ) {
203
+ unsafe {
204
+ //(self.EnzymeSetCLBool.set_cl)(self.EnzymeStrictAliasing, strict as u8);
205
+ }
128
206
}
129
- }
130
- pub ( crate ) fn set_rust_rules ( val : bool ) {
131
- unsafe {
132
- EnzymeSetCLBool ( std:: ptr:: addr_of_mut!( RustTypeRules ) , val as u8 ) ;
207
+
208
+ pub ( crate ) fn set_loose_types ( & mut self , loose : bool ) {
209
+ unsafe {
210
+ //(self.EnzymeSetCLBool.set_cl)(self.looseTypeAnalysis, loose as u8);
211
+ }
212
+ }
213
+
214
+ pub ( crate ) fn set_inline ( & mut self , val : bool ) {
215
+ unsafe {
216
+ //(self.EnzymeSetCLBool.set_cl)(self.EnzymeInline, val as u8);
217
+ }
218
+ }
219
+
220
+ pub ( crate ) fn set_rust_rules ( & mut self , val : bool ) {
221
+ unsafe {
222
+ //(self.EnzymeSetCLBool.set_cl)(self.RustTypeRules, val as u8);
223
+ }
133
224
}
134
225
}
226
+
227
+
135
228
}
136
229
137
230
#[ cfg( not( llvm_enzyme) ) ]
0 commit comments