Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit d4d270d

Browse files
committed
Merge BACKEND_CONFIG and CURRENT_MODULE thread locals
1 parent 6fac7f0 commit d4d270d

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

src/driver/jit.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@ use cranelift_jit::{JITBuilder, JITModule};
1414
use crate::{prelude::*, BackendConfig};
1515
use crate::{CodegenCx, CodegenMode};
1616

17+
struct JitState {
18+
backend_config: BackendConfig,
19+
jit_module: JITModule,
20+
}
21+
1722
thread_local! {
18-
pub static BACKEND_CONFIG: RefCell<Option<BackendConfig>> = RefCell::new(None);
19-
pub static CURRENT_MODULE: RefCell<Option<JITModule>> = RefCell::new(None);
23+
static LAZY_JIT_STATE: RefCell<Option<JitState>> = RefCell::new(None);
2024
}
2125

2226
pub(crate) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
@@ -106,10 +110,6 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
106110
// useful as some dynamic linkers use it as a marker to jump over.
107111
argv.push(std::ptr::null());
108112

109-
BACKEND_CONFIG.with(|tls_backend_config| {
110-
assert!(tls_backend_config.borrow_mut().replace(backend_config).is_none())
111-
});
112-
113113
let start_sig = Signature {
114114
params: vec![
115115
AbiParam::new(jit_module.target_config().pointer_type()),
@@ -121,8 +121,11 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
121121
let start_func_id = jit_module.declare_function("main", Linkage::Import, &start_sig).unwrap();
122122
let finalized_start: *const u8 = jit_module.get_finalized_function(start_func_id);
123123

124-
CURRENT_MODULE
125-
.with(|current_module| assert!(current_module.borrow_mut().replace(jit_module).is_none()));
124+
LAZY_JIT_STATE.with(|lazy_jit_state| {
125+
let mut lazy_jit_state = lazy_jit_state.borrow_mut();
126+
assert!(lazy_jit_state.is_none());
127+
*lazy_jit_state = Some(JitState { backend_config, jit_module });
128+
});
126129

127130
let f: extern "C" fn(c_int, *const *const c_char) -> c_int =
128131
unsafe { ::std::mem::transmute(finalized_start) };
@@ -136,11 +139,11 @@ extern "C" fn __clif_jit_fn(instance_ptr: *const Instance<'static>) -> *const u8
136139
// lift is used to ensure the correct lifetime for instance.
137140
let instance = tcx.lift(unsafe { *instance_ptr }).unwrap();
138141

139-
CURRENT_MODULE.with(|jit_module| {
140-
let mut jit_module = jit_module.borrow_mut();
141-
let jit_module = jit_module.as_mut().unwrap();
142-
let backend_config =
143-
BACKEND_CONFIG.with(|backend_config| backend_config.borrow().clone().unwrap());
142+
LAZY_JIT_STATE.with(|lazy_jit_state| {
143+
let mut lazy_jit_state = lazy_jit_state.borrow_mut();
144+
let lazy_jit_state = lazy_jit_state.as_mut().unwrap();
145+
let jit_module = &mut lazy_jit_state.jit_module;
146+
let backend_config = lazy_jit_state.backend_config.clone();
144147

145148
let name = tcx.symbol_name(instance).name.to_string();
146149
let sig = crate::abi::get_function_sig(tcx, jit_module.isa().triple(), instance);

0 commit comments

Comments
 (0)