1
1
use cranelift_codegen:: Context ;
2
2
use cranelift_codegen:: control:: ControlPlane ;
3
+ use cranelift_codegen:: incremental_cache:: CacheKvStore ;
3
4
use cranelift_codegen:: ir:: Signature ;
4
5
use cranelift_codegen:: isa:: { TargetFrontendConfig , TargetIsa } ;
5
6
use cranelift_module:: {
@@ -90,7 +91,28 @@ impl<T: Module> Module for UnwindModule<T> {
90
91
ctx : & mut Context ,
91
92
ctrl_plane : & mut ControlPlane ,
92
93
) -> ModuleResult < ( ) > {
93
- self . module . define_function_with_control_plane ( func, ctx, ctrl_plane) ?;
94
+ if std:: env:: var ( "CG_CLIF_FUNCTION_CACHE" ) . as_deref ( ) == Ok ( "naive" ) {
95
+ if ctx. func . layout . blocks ( ) . nth ( 1 ) . is_none ( )
96
+ || ctx. func . layout . blocks ( ) . nth ( 2 ) . is_none ( )
97
+ {
98
+ ctx. compile ( self . module . isa ( ) , ctrl_plane) ?;
99
+ } else {
100
+ ctx. compile_with_cache ( self . module . isa ( ) , & mut Cache , ctrl_plane) ?;
101
+ }
102
+ } else {
103
+ ctx. compile ( self . module . isa ( ) , ctrl_plane) ?;
104
+ }
105
+ let res = ctx. compiled_code ( ) . unwrap ( ) ;
106
+
107
+ let alignment = res. buffer . alignment as u64 ;
108
+ let relocs = res
109
+ . buffer
110
+ . relocs ( )
111
+ . iter ( )
112
+ . map ( |reloc| ModuleReloc :: from_mach_reloc ( & reloc, & ctx. func , func) )
113
+ . collect :: < Vec < _ > > ( ) ;
114
+ self . module . define_function_bytes ( func, alignment, res. buffer . data ( ) , & relocs) ?;
115
+
94
116
self . unwind_context . add_function ( & mut self . module , func, ctx) ;
95
117
Ok ( ( ) )
96
118
}
@@ -109,3 +131,29 @@ impl<T: Module> Module for UnwindModule<T> {
109
131
self . module . define_data ( data_id, data)
110
132
}
111
133
}
134
+
135
+ struct Cache ;
136
+
137
+ impl Cache {
138
+ fn file_for_key ( & self , key : & [ u8 ] ) -> String {
139
+ let mut path = key. iter ( ) . map ( |b| format ! ( "{:02x}" , b) ) . collect :: < String > ( ) ;
140
+ path. push_str ( ".clif_cache" ) ;
141
+ "/home/bjorn/Projects/cg_clif/cache/" . to_owned ( ) + & path
142
+ }
143
+ }
144
+
145
+ impl CacheKvStore for Cache {
146
+ fn get ( & self , key : & [ u8 ] ) -> Option < std:: borrow:: Cow < ' _ , [ u8 ] > > {
147
+ let path = self . file_for_key ( key) ;
148
+ if std:: fs:: exists ( & path) . unwrap ( ) {
149
+ Some ( std:: fs:: read ( path) . unwrap ( ) . into ( ) )
150
+ } else {
151
+ None
152
+ }
153
+ }
154
+
155
+ fn insert ( & mut self , key : & [ u8 ] , val : Vec < u8 > ) {
156
+ let path = self . file_for_key ( key) ;
157
+ std:: fs:: write ( path, val) . unwrap ( ) ;
158
+ }
159
+ }
0 commit comments