Skip to content

Commit 622704b

Browse files
committed
fix(runtime): avoid thread-local compilers
1 parent f3cf7a1 commit 622704b

1 file changed

Lines changed: 17 additions & 91 deletions

File tree

crates/revmc-runtime/src/runtime/worker.rs

Lines changed: 17 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use alloy_primitives::Bytes;
2020
use crossbeam_channel as chan;
2121
use rayon::{ThreadPool, ThreadPoolBuilder};
2222
#[cfg(feature = "llvm")]
23-
use std::{cell::RefCell, fs::File, io::Read, mem::ManuallyDrop, time::Instant};
23+
use std::{fs::File, io::Read, time::Instant};
2424
use std::{
2525
sync::{
2626
Arc,
@@ -253,22 +253,6 @@ impl Drop for WorkerPool {
253253
#[cfg(feature = "llvm")]
254254
fn compile_job(job: CompileJob, config: &RuntimeConfig) -> WorkerResult {
255255
trace!(?job, "received job");
256-
match job.kind {
257-
CompilationKind::Jit => {
258-
JIT_COMPILER.with_borrow_mut(|state| compile_with_state(job, config, state))
259-
}
260-
CompilationKind::Aot => {
261-
AOT_COMPILER.with_borrow_mut(|state| compile_with_state(job, config, state))
262-
}
263-
}
264-
}
265-
266-
#[cfg(feature = "llvm")]
267-
fn compile_with_state(
268-
job: CompileJob,
269-
config: &RuntimeConfig,
270-
state_slot: &mut Option<ManuallyDrop<CompilerState>>,
271-
) -> WorkerResult {
272256
let _span = match job.kind {
273257
CompilationKind::Jit => {
274258
debug_span!("jit_compile", hash=%job.key.code_hash, spec_id=?job.key.spec_id).entered()
@@ -279,26 +263,21 @@ fn compile_with_state(
279263
};
280264
let t0 = Instant::now();
281265

282-
if state_slot.is_none() {
283-
match CompilerState::new(config, job.kind) {
284-
Ok(s) => *state_slot = Some(ManuallyDrop::new(s)),
285-
Err(e) => {
286-
error!(error = %e, "failed to create LLVM backend");
287-
return WorkerResult {
288-
key: job.key,
289-
outcome: Err(e),
290-
kind: job.kind,
291-
sync_notifier: job.sync_notifier,
292-
generation: job.generation,
293-
compile_duration: t0.elapsed(),
294-
timings: CompileTimings::default(),
295-
};
296-
}
266+
let mut compiler = match create_compiler(config, job.kind == CompilationKind::Aot) {
267+
Ok(compiler) => compiler,
268+
Err(e) => {
269+
error!(error = %e, "failed to create LLVM backend");
270+
return WorkerResult {
271+
key: job.key,
272+
outcome: Err(e),
273+
kind: job.kind,
274+
sync_notifier: job.sync_notifier,
275+
generation: job.generation,
276+
compile_duration: t0.elapsed(),
277+
timings: CompileTimings::default(),
278+
};
297279
}
298-
}
299-
300-
let state = state_slot.as_mut().unwrap();
301-
let compiler = &mut state.compiler;
280+
};
302281

303282
if job.kind == CompilationKind::Jit
304283
&& let Some(base) = &config.dump_dir
@@ -310,32 +289,11 @@ fn compile_with_state(
310289
compiler.set_opt_level(job.opt_level);
311290

312291
let outcome = match job.kind {
313-
CompilationKind::Jit => compile_jit_artifact(&job, compiler),
314-
CompilationKind::Aot => compile_aot_artifact(&job, compiler),
292+
CompilationKind::Jit => compile_jit_artifact(&job, &mut compiler),
293+
CompilationKind::Aot => compile_aot_artifact(&job, &mut compiler),
315294
};
316295
let timings = compiler.take_timings();
317296

318-
if let Err(err) = compiler.clear_ir() {
319-
warn!(%err, "clear_ir failed");
320-
}
321-
322-
state.compilations_since_recycle += 1;
323-
if config.tuning.compiler_recycle_threshold > 0
324-
&& state.compilations_since_recycle >= config.tuning.compiler_recycle_threshold
325-
{
326-
debug!(compilations_since_recycle = state.compilations_since_recycle, "recycling compiler");
327-
match CompilerState::new(config, job.kind) {
328-
Ok(new_state) => {
329-
replace_compiler_state(state_slot, new_state);
330-
revmc_llvm::global_gc();
331-
}
332-
Err(e) => {
333-
error!(error = %e, "failed to recreate compiler");
334-
state.compilations_since_recycle = 0;
335-
}
336-
}
337-
}
338-
339297
WorkerResult {
340298
key: job.key,
341299
outcome,
@@ -347,38 +305,6 @@ fn compile_with_state(
347305
}
348306
}
349307

350-
#[cfg(feature = "llvm")]
351-
struct CompilerState {
352-
compiler: EvmCompiler<EvmLlvmBackend>,
353-
compilations_since_recycle: usize,
354-
}
355-
356-
#[cfg(feature = "llvm")]
357-
impl CompilerState {
358-
fn new(config: &RuntimeConfig, kind: CompilationKind) -> Result<Self, String> {
359-
Ok(Self {
360-
compiler: create_compiler(config, kind == CompilationKind::Aot)?,
361-
compilations_since_recycle: 0,
362-
})
363-
}
364-
}
365-
366-
#[cfg(feature = "llvm")]
367-
thread_local! {
368-
static JIT_COMPILER: RefCell<Option<ManuallyDrop<CompilerState>>> = const { RefCell::new(None) };
369-
static AOT_COMPILER: RefCell<Option<ManuallyDrop<CompilerState>>> = const { RefCell::new(None) };
370-
}
371-
372-
#[cfg(feature = "llvm")]
373-
fn replace_compiler_state(
374-
state: &mut Option<ManuallyDrop<CompilerState>>,
375-
new_state: CompilerState,
376-
) {
377-
if let Some(mut old_state) = state.replace(ManuallyDrop::new(new_state)) {
378-
unsafe { ManuallyDrop::drop(&mut old_state) };
379-
}
380-
}
381-
382308
#[cfg(feature = "llvm")]
383309
fn create_compiler(
384310
config: &RuntimeConfig,

0 commit comments

Comments
 (0)