@@ -32,7 +32,7 @@ extern crate rustc_driver;
3232use std:: any:: Any ;
3333use std:: cell:: Cell ;
3434use std:: sync:: Arc ;
35- use std:: sync:: RwLock ;
35+ use std:: sync:: OnceLock ;
3636
3737use cranelift_codegen:: isa:: TargetIsa ;
3838use cranelift_codegen:: settings:: { self , Configurable } ;
@@ -167,7 +167,7 @@ impl CodegenCx {
167167}
168168
169169pub struct CraneliftCodegenBackend {
170- pub config : RwLock < Option < BackendConfig > > ,
170+ pub config : OnceLock < BackendConfig > ,
171171}
172172
173173impl CodegenBackend for CraneliftCodegenBackend {
@@ -185,12 +185,10 @@ impl CodegenBackend for CraneliftCodegenBackend {
185185 }
186186 }
187187
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+ } ) ;
194192 }
195193
196194 fn target_features ( & self , sess : & Session , _allow_unstable : bool ) -> Vec < rustc_span:: Symbol > {
@@ -217,7 +215,7 @@ impl CodegenBackend for CraneliftCodegenBackend {
217215 need_metadata_module : bool ,
218216 ) -> Box < dyn Any + DynSend > {
219217 tcx. dcx ( ) . abort_if_errors ( ) ;
220- let config = self . config . read ( ) . unwrap ( ) . clone ( ) . unwrap ( ) ;
218+ let config = self . config . get ( ) . unwrap ( ) . clone ( ) ;
221219 match config. codegen_mode {
222220 CodegenMode :: Aot => driver:: aot:: run_aot ( tcx, config, metadata, need_metadata_module) ,
223221 CodegenMode :: Jit | CodegenMode :: JitLazy => {
@@ -238,7 +236,7 @@ impl CodegenBackend for CraneliftCodegenBackend {
238236 ) -> ( CodegenResults , FxIndexMap < WorkProductId , WorkProduct > ) {
239237 downcast_box_any_dyn_send :: < driver:: aot:: OngoingCodegen > ( ongoing_codegen)
240238 . unwrap ( )
241- . join ( sess, self . config . read ( ) . unwrap ( ) . as_ref ( ) . unwrap ( ) )
239+ . join ( sess, self . config . get ( ) . unwrap ( ) )
242240 }
243241
244242 fn link (
@@ -351,5 +349,5 @@ fn build_isa(sess: &Session, backend_config: &BackendConfig) -> Arc<dyn isa::Tar
351349/// This is the entrypoint for a hot plugged rustc_codegen_cranelift
352350#[ no_mangle]
353351pub fn __rustc_codegen_backend ( ) -> Box < dyn CodegenBackend > {
354- Box :: new ( CraneliftCodegenBackend { config : RwLock :: new ( None ) } )
352+ Box :: new ( CraneliftCodegenBackend { config : OnceLock :: new ( ) } )
355353}
0 commit comments