Skip to content

Commit b919f15

Browse files
committed
Use OnceLock in CraneliftCodegenBackend
1 parent 216f62c commit b919f15

File tree

2 files changed

+10
-11
lines changed
  • compiler

2 files changed

+10
-11
lines changed

compiler/rustc_codegen_cranelift/src/lib.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ extern crate rustc_driver;
3232
use std::any::Any;
3333
use std::cell::Cell;
3434
use std::sync::Arc;
35-
use std::sync::RwLock;
35+
use std::sync::OnceLock;
3636

3737
use cranelift_codegen::isa::TargetIsa;
3838
use cranelift_codegen::settings::{self, Configurable};
@@ -167,7 +167,7 @@ impl CodegenCx {
167167
}
168168

169169
pub struct CraneliftCodegenBackend {
170-
pub config: RwLock<Option<BackendConfig>>,
170+
pub config: OnceLock<BackendConfig>,
171171
}
172172

173173
impl 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]
353351
pub fn __rustc_codegen_backend() -> Box<dyn CodegenBackend> {
354-
Box::new(CraneliftCodegenBackend { config: RwLock::new(None) })
352+
Box::new(CraneliftCodegenBackend { config: OnceLock::new() })
355353
}

compiler/rustc_data_structures/src/marker.rs

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ cfg_match! {
9393
[std::sync::mpsc::Sender<T> where T: DynSend]
9494
[std::sync::mpsc::Receiver<T> where T: DynSend]
9595
[std::sync::Arc<T> where T: ?Sized + DynSync + DynSend]
96+
[std::sync::OnceLock<T> where T: DynSend]
9697
[std::sync::LazyLock<T, F> where T: DynSend, F: DynSend]
9798
[std::thread::JoinHandle<T> where T]
9899
[std::collections::HashSet<K, S> where K: DynSend, S: DynSend]

0 commit comments

Comments
 (0)