Truly independent web browser
LibJS: Keep collections deferred while JIT code is installed
Shannon Boothf7c1cb93d0f0
diff --git a/Libraries/LibJS/Rust/src/jit/code.rs b/Libraries/LibJS/Rust/src/jit/code.rsindex 1c426822a76..ad5986441bc 100644--- a/Libraries/LibJS/Rust/src/jit/code.rs+++ b/Libraries/LibJS/Rust/src/jit/code.rs@@ -16,6 +16,7 @@ use libjs_jit::code::{ExitKind, Site};use super::entry_table::{JIT_ENTRY_SLOT_MASK, JitEntryTable, NO_JIT_ENTRY_SLOT};use super::executable_memory::ExecutableMemory;use crate::bytecode::executable::{Executable, PropertyLookupCache, PropertyLookupCacheTier};+use crate::gc::defer_gc::DeferGc;use crate::gc::visitor::{Trace, Visitor};use crate::interpreter::vm::Vm;use crate::layout::cell::{CellHeader, Gc};@@ -182,6 +183,7 @@ struct MaterializedObject {}impl JitCode {+ #[allow(clippy::too_many_arguments)]pub fn new(memory: ExecutableMemory,entry_offset: u32,@@ -190,6 +192,7 @@ impl JitCode {sites: Vec<Site>,embedded_cells: Vec<Gc<CellHeader>>,snapshot_executables: Vec<SnapshotExecutable>,+ _defer_gc: &DeferGc<'_>,) -> Self {let entry_at = |offset: u32| {assert!((offset as usize) < memory.size());@@ -632,12 +635,12 @@ impl Executable {/// Makes new frames of the executable run in its JIT code. Frames of it that run in the interpreter count down/// `tier_up_budget` from here, and continue in the code at an on-stack replacement entry once it runs out.- pub fn install_jit_code(&self, vm: &Vm, code: Box<JitCode>, tier_up_budget: i32) {+ pub fn install_jit_code(&self, vm: &Vm, code: JitCode, tier_up_budget: i32) {let (table, slot) = self.jit_entry_table_slot(vm).expect("only executables with a JIT entry slot are compiled");table.set_entry(slot, Some(code.entry() as *const c_void));- assert!(table.set_code(slot, Some(code)).is_none());+ table.attach_code(slot, code);self.set_jit_compile_state(CompileState::Installed);self.head.tier_up_budget.set(tier_up_budget);}@@ -697,7 +700,7 @@ impl Executable {fn detach_jit_code(&self, vm: &Vm) {let state = self.jit_state();let (table, slot) = self.jit_entry_table_slot(vm).expect("the code is attached");- drop(table.set_code(slot, None));+ table.detach_code(slot);vm.jit.count_coverage("discard");let discard_count = state.discard_count.get() + 1;state.discard_count.set(discard_count);diff --git a/Libraries/LibJS/Rust/src/jit/entry_table.rs b/Libraries/LibJS/Rust/src/jit/entry_table.rsindex 149eef76db2..c1d1ba82b94 100644--- a/Libraries/LibJS/Rust/src/jit/entry_table.rs+++ b/Libraries/LibJS/Rust/src/jit/entry_table.rs@@ -149,7 +149,7 @@ impl JitEntryTable {}/// Frees the slot of the executable whose head is at `owner`, which died, and returns its code.- pub fn free(&self, slot: u32, owner: u64) -> Option<Box<JitCode>> {+ pub fn free(&self, slot: u32, owner: u64) -> Option<JitCode> {let slot = slot & JIT_ENTRY_SLOT_MASK;if slot == NO_JIT_ENTRY_SLOT {return None;@@ -158,11 +158,10 @@ impl JitEntryTable {self.is_owned_by(slot, owner),"only the owner of a JIT entry slot frees it");- let code = self.code_cell(slot).take();self.entry_cell(slot).set(self.not_compiled_entry);self.owner_cell(slot).set(0);self.free_slots.borrow_mut().push(slot);- code+ self.code_cell(slot).take().map(|code| *code)}/// Whether the slot belongs to the executable whose head is at `owner`.@@ -177,15 +176,21 @@ impl JitEntryTable {/// The code attached to the slot.pub fn code(&self, slot: u32) -> Option<&JitCode> {- // SAFETY: The code stays in the slot until set_code() or free() replaces it, which callers do not do while+ // SAFETY: The code stays in the slot until detach_code() or free() takes it, which callers do not do while// they use the reference.unsafe { (*self.code_cell(slot).as_ptr()).as_deref() }}- /// Attaches `code` to the slot, and returns the code it had.- pub fn set_code(&self, slot: u32, code: Option<Box<JitCode>>) -> Option<Box<JitCode>> {+ /// Attaches `code` to the slot, which has none.+ pub fn attach_code(&self, slot: u32, code: JitCode) {assert_ne!(slot & JIT_ENTRY_SLOT_MASK, NO_JIT_ENTRY_SLOT);- self.code_cell(slot).replace(code)+ assert!(self.code_cell(slot).replace(Some(Box::new(code))).is_none());+ }++ /// Drops the code attached to the slot.+ pub fn detach_code(&self, slot: u32) {+ assert_ne!(slot & JIT_ENTRY_SLOT_MASK, NO_JIT_ENTRY_SLOT);+ drop(self.code_cell(slot).take());}}diff --git a/Libraries/LibJS/Rust/src/jit/mod.rs b/Libraries/LibJS/Rust/src/jit/mod.rsindex fbf606b01c7..c592d3865f5 100644--- a/Libraries/LibJS/Rust/src/jit/mod.rs+++ b/Libraries/LibJS/Rust/src/jit/mod.rs@@ -34,6 +34,7 @@ use std::collections::{BTreeMap, HashMap};use std::rc::Rc;use crate::bytecode::executable::Executable;+use crate::gc::defer_gc::{DeferGc, Deferred};use crate::gc::visitor::{Trace, Visitor};use crate::interpreter::dispatch_tables::DispatchTable;use crate::interpreter::vm::Vm;@@ -378,7 +379,7 @@ impl JitState {self.compile_queue.get()}- pub(crate) fn add_job(&self, job: CompileJob) -> u64 {+ pub(crate) fn add_job(&self, job: CompileJob, _defer_gc: &DeferGc<'_>) -> u64 {let id = self.next_job_id.get();self.next_job_id.set(id + 1);self.jobs.borrow_mut().insert(id, job);@@ -397,8 +398,8 @@ impl JitState {.retain(|_, job| !core::ptr::eq(job.executable.as_ptr(), executable));}- pub(crate) fn take_job(&self, id: u64) -> Option<CompileJob> {- self.jobs.borrow_mut().remove(&id)+ pub(crate) fn take_job<'d>(&self, id: u64, defer_gc: &'d DeferGc<'_>) -> Option<Deferred<'d, CompileJob>> {+ self.jobs.borrow_mut().remove(&id).map(|job| defer_gc.cover(job))}/// The entry trampoline (see `libjs_jit::codegen::generate_entry_trampoline()`).diff --git a/Libraries/LibJS/Rust/src/jit/tier_up.rs b/Libraries/LibJS/Rust/src/jit/tier_up.rsindex e3533721595..9f01247ab71 100644--- a/Libraries/LibJS/Rust/src/jit/tier_up.rs+++ b/Libraries/LibJS/Rust/src/jit/tier_up.rs@@ -342,6 +342,7 @@ fn install(compiled: CompiledCode,memory: ExecutableMemory,snapshot: SnapshotExecutables,+ defer_gc: &DeferGc<'_>,) {let embedded_cells = compiled.embedded_cells@@ -360,6 +361,7 @@ fn install(compiled.sites,embedded_cells,snapshot,+ defer_gc,).with_invalidation(id, compiled.invalidation_patches).with_frame_offsets(compiled.site_index_frame_offset, compiled.register_dump_frame_offset)@@ -370,7 +372,7 @@ fn install(// queued (from the tier-up check of another executable, like a callee's), or because they exited from the// code before. Those frames keep counting, so that they continue in the code at its on-stack replacement// entry rather than run in the interpreter until they return.- executable.install_jit_code(vm, Box::new(code), compile_wait_budget(&vm.jit.options));+ executable.install_jit_code(vm, code, compile_wait_budget(&vm.jit.options));}type SnapshotExecutables = Vec<SnapshotExecutable>;@@ -382,6 +384,7 @@ fn finish_compile(result: CompileResult,memory: Option<ExecutableMemory>,snapshot_executables: SnapshotExecutables,+ defer_gc: &DeferGc<'_>,) {assert_eq!(executable.jit_compile_state(), CompileState::Queued);let options = &vm.jit.options;@@ -411,7 +414,7 @@ fn finish_compile(eprintln!("JIT code for {}:\n{dump}", super::describe_executable(&executable));}if let Some(memory) = memory {- install(vm, executable, compiled, memory, snapshot_executables);+ install(vm, executable, compiled, memory, snapshot_executables, defer_gc);}if executable.jit_compile_state() != CompileState::Installed {executable.set_jit_compile_state(CompileState::None);@@ -467,16 +470,26 @@ fn compile(.pop().expect("one code was allocated")});- finish_compile(vm, executable, result, memory, snapshot_executables.into_inner());+ finish_compile(+ vm,+ executable,+ result,+ memory,+ snapshot_executables.into_inner(),+ &defer_gc,+ );drop(defer_gc);return;}- let job_id = vm.jit.add_job(CompileJob {- executable,- cells: cells.into_inner(),- snapshot_executables: snapshot_executables.into_inner(),- });+ let job_id = vm.jit.add_job(+ CompileJob {+ executable,+ cells: cells.into_inner(),+ snapshot_executables: snapshot_executables.into_inner(),+ },+ &defer_gc,+ );drop(defer_gc);vm.jit.compile_queue().submit(job_id, snapshot);}@@ -504,7 +517,11 @@ pub fn install_finished_compiles(vm: &Vm) {let Some(queue) = vm.jit.started_compile_queue() else {return;};- install_compile_results(vm, vm.jit.compile_results_to_install(queue.take_finished()));+ install_compile_results(+ vm,+ vm.jit.compile_results_to_install(queue.take_finished()),+ &DeferGc::new(vm),+ );}/// Waits for the compile job of `executable` to finish and installs its code, and that of every other job that@@ -514,17 +531,18 @@ pub(crate) fn wait_for_compile_job(vm: &Vm, executable: &Executable) {return;};// NB: The job may have finished already, and be held back by the "stress-install" option.- install_compile_results(vm, vm.jit.take_held_compile_results());+ install_compile_results(vm, vm.jit.take_held_compile_results(), &DeferGc::new(vm));while executable.jit_compile_state() == CompileState::Queued {- install_compile_results(vm, queue.wait_for_finished());+ install_compile_results(vm, queue.wait_for_finished(), &DeferGc::new(vm));}}-fn install_compile_results(vm: &Vm, results: Vec<(u64, CompileResult)>) {+/// Installs the code of finished compile jobs.+fn install_compile_results(vm: &Vm, results: Vec<(u64, CompileResult)>, defer_gc: &DeferGc<'_>) {let finished: Vec<(CompileJob, CompileResult)> = results.into_iter()// NB: Jobs abandoned meanwhile have no entry anymore.- .filter_map(|(id, result)| Some((vm.jit.take_job(id)?, result)))+ .filter_map(|(id, result)| Some((vm.jit.take_job(id, defer_gc)?.into_inner(), result))).collect();if finished.is_empty() {return;@@ -552,6 +570,6 @@ fn install_compile_results(vm: &Vm, results: Vec<(u64, CompileResult)>) {for ((job, result), install) in finished.into_iter().zip(verdicts) {let memory = install.then(|| memories.next().expect("there is memory for each installed code"));- finish_compile(vm, job.executable, result, memory, job.snapshot_executables);+ finish_compile(vm, job.executable, result, memory, job.snapshot_executables, defer_gc);}}