@@ -32,7 +32,7 @@ extern crate rustc_driver;
32
32
use std:: any:: Any ;
33
33
use std:: cell:: Cell ;
34
34
use std:: sync:: Arc ;
35
- use std:: sync:: RwLock ;
35
+ use std:: sync:: OnceLock ;
36
36
37
37
use cranelift_codegen:: isa:: TargetIsa ;
38
38
use cranelift_codegen:: settings:: { self , Configurable } ;
@@ -167,7 +167,7 @@ impl CodegenCx {
167
167
}
168
168
169
169
pub struct CraneliftCodegenBackend {
170
- pub config : RwLock < Option < BackendConfig > > ,
170
+ pub config : OnceLock < BackendConfig > ,
171
171
}
172
172
173
173
impl CodegenBackend for CraneliftCodegenBackend {
@@ -185,12 +185,10 @@ impl CodegenBackend for CraneliftCodegenBackend {
185
185
}
186
186
}
187
187
188
- let mut config = self . config . write ( ) . unwrap ( ) ;
189
- if config. is_none ( ) {
190
- let new_config = BackendConfig :: from_opts ( & sess. opts . cg . llvm_args )
191
- . unwrap_or_else ( |err| sess. dcx ( ) . fatal ( err) ) ;
192
- * config = Some ( new_config) ;
193
- }
188
+ self . config . get_or_init ( || {
189
+ BackendConfig :: from_opts ( & sess. opts . cg . llvm_args )
190
+ . unwrap_or_else ( |err| sess. dcx ( ) . fatal ( err) )
191
+ } ) ;
194
192
}
195
193
196
194
fn target_features ( & self , sess : & Session , _allow_unstable : bool ) -> Vec < rustc_span:: Symbol > {
@@ -217,7 +215,7 @@ impl CodegenBackend for CraneliftCodegenBackend {
217
215
need_metadata_module : bool ,
218
216
) -> Box < dyn Any + DynSend > {
219
217
tcx. dcx ( ) . abort_if_errors ( ) ;
220
- let config = self . config . read ( ) . unwrap ( ) . clone ( ) . unwrap ( ) ;
218
+ let config = self . config . get ( ) . unwrap ( ) . clone ( ) ;
221
219
match config. codegen_mode {
222
220
CodegenMode :: Aot => driver:: aot:: run_aot ( tcx, config, metadata, need_metadata_module) ,
223
221
CodegenMode :: Jit | CodegenMode :: JitLazy => {
@@ -238,7 +236,7 @@ impl CodegenBackend for CraneliftCodegenBackend {
238
236
) -> ( CodegenResults , FxIndexMap < WorkProductId , WorkProduct > ) {
239
237
downcast_box_any_dyn_send :: < driver:: aot:: OngoingCodegen > ( ongoing_codegen)
240
238
. unwrap ( )
241
- . join ( sess, self . config . read ( ) . unwrap ( ) . as_ref ( ) . unwrap ( ) )
239
+ . join ( sess, self . config . get ( ) . unwrap ( ) )
242
240
}
243
241
244
242
fn link (
@@ -351,5 +349,5 @@ fn build_isa(sess: &Session, backend_config: &BackendConfig) -> Arc<dyn isa::Tar
351
349
/// This is the entrypoint for a hot plugged rustc_codegen_cranelift
352
350
#[ no_mangle]
353
351
pub fn __rustc_codegen_backend ( ) -> Box < dyn CodegenBackend > {
354
- Box :: new ( CraneliftCodegenBackend { config : RwLock :: new ( None ) } )
352
+ Box :: new ( CraneliftCodegenBackend { config : OnceLock :: new ( ) } )
355
353
}
0 commit comments