Skip to content

Truly independent web browser

LibJS: Look up JIT code through the VM its callers already have

Andreas Kling6fc8aa973199
diff --git a/Libraries/LibJS/Rust/src/interpreter/vm.rs b/Libraries/LibJS/Rust/src/interpreter/vm.rs
index 1b9a0e40131..302516e8a4a 100644
--- a/Libraries/LibJS/Rust/src/interpreter/vm.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/vm.rs
@@ -1336,7 +1336,7 @@ impl Vm {
let caller = unsafe { &*caller };
let site = return_pc & !INLINED_CALL_SITE_BIT;
let frame_pointer = crate::jit::translate::frame_pointer_from_low_half(context.caller_dst_raw.get());
- for frame in crate::jit::translate::call_site_frames(caller, site, frame_pointer) {
+ for frame in crate::jit::translate::call_site_frames(self, caller, site, frame_pointer) {
callback(StackFrame {
execution_context: caller,
function: Some(frame.function),
diff --git a/Libraries/LibJS/Rust/src/jit/code.rs b/Libraries/LibJS/Rust/src/jit/code.rs
index 618ed77b15b..9962ce6459b 100644
--- a/Libraries/LibJS/Rust/src/jit/code.rs
+++ b/Libraries/LibJS/Rust/src/jit/code.rs
@@ -317,19 +317,14 @@ impl Executable {
self.head.jit_compile_state.set(state as u8);
}
- /// The VM whose heap the executable lives in.
- fn heap_vm(&self) -> &Vm {
- Vm::of_cell(self)
- }
-
- /// The executable's slot in the JIT entry table and the table, if it has one.
- fn jit_entry_table_slot(&self) -> Option<(&JitEntryTable, u32)> {
+ /// The executable's slot in the JIT entry table of `vm`, the VM whose heap the executable lives in, and the table,
+ /// if it has one.
+ fn jit_entry_table_slot<'vm>(&self, vm: &'vm Vm) -> Option<(&'vm JitEntryTable, u32)> {
let slot = self.head.jit_entry_slot.get();
if slot & JIT_ENTRY_SLOT_MASK == NO_JIT_ENTRY_SLOT {
return None;
}
- let table = self
- .heap_vm()
+ let table = vm
.jit
.entry_table
.as_ref()
@@ -370,15 +365,15 @@ impl Executable {
drop(code);
}
- /// The executable's attached JIT code, if any.
- pub fn jit_code(&self) -> Option<&JitCode> {
- let (table, slot) = self.jit_entry_table_slot()?;
+ /// The executable's attached JIT code, if any. `vm` is the VM whose heap the executable lives in.
+ pub fn jit_code<'vm>(&self, vm: &'vm Vm) -> Option<&'vm JitCode> {
+ let (table, slot) = self.jit_entry_table_slot(vm)?;
table.code(slot)
}
/// Visits the cells the executable's attached JIT code keeps alive.
pub fn trace_jit_code(&self, visitor: &mut Visitor) {
- if let Some(code) = self.jit_code() {
+ if let Some(code) = self.jit_code(Vm::of_cell(self)) {
code.trace(visitor);
}
}
@@ -411,9 +406,9 @@ 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, code: Box<JitCode>, tier_up_budget: i32) {
+ pub fn install_jit_code(&self, vm: &Vm, code: Box<JitCode>, tier_up_budget: i32) {
let (table, slot) = self
- .jit_entry_table_slot()
+ .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());
@@ -425,7 +420,7 @@ impl Executable {
/// anymore (see `detach_discarded_jit_code_if_unused()`). The executable may be compiled again later.
pub fn discard_jit_code(&self, vm: &Vm) {
assert_eq!(self.jit_compile_state(), CompileState::Installed);
- let (table, slot) = self.jit_entry_table_slot().expect("the code is attached");
+ let (table, slot) = self.jit_entry_table_slot(vm).expect("the code is attached");
table.set_entry(slot, None);
self.set_jit_compile_state(CompileState::Discarding);
if self.detach_discarded_jit_code_if_unused(vm) {
@@ -475,7 +470,7 @@ impl Executable {
/// Drops the executable's discarded code, which no frame runs anymore.
fn detach_jit_code(&self, vm: &Vm) {
let state = self.jit_state();
- let (table, slot) = self.jit_entry_table_slot().expect("the code is attached");
+ let (table, slot) = self.jit_entry_table_slot(vm).expect("the code is attached");
drop(table.set_code(slot, None));
vm.jit.count_coverage("discard");
let discard_count = state.discard_count.get() + 1;
diff --git a/Libraries/LibJS/Rust/src/jit/dependencies.rs b/Libraries/LibJS/Rust/src/jit/dependencies.rs
index daf3d9eef6e..e18d19e8423 100644
--- a/Libraries/LibJS/Rust/src/jit/dependencies.rs
+++ b/Libraries/LibJS/Rust/src/jit/dependencies.rs
@@ -222,7 +222,7 @@ pub fn invalidate_dependents(vm: &Vm, dependency: Dependency) {
continue;
};
// NB: The executable may have dropped the code since, or have other code by now.
- let Some(code) = executable.jit_code().filter(|code| code.id() == id) else {
+ let Some(code) = executable.jit_code(vm).filter(|code| code.id() == id) else {
continue;
};
if vm.jit.options.log_exits {
diff --git a/Libraries/LibJS/Rust/src/jit/disabled.rs b/Libraries/LibJS/Rust/src/jit/disabled.rs
index 9ce589db561..124deec3847 100644
--- a/Libraries/LibJS/Rust/src/jit/disabled.rs
+++ b/Libraries/LibJS/Rust/src/jit/disabled.rs
@@ -180,7 +180,12 @@ pub mod translate {
u64::from(low_half)
}
- pub fn call_site_frames(_frame: &ExecutionContext, _index: u32, _frame_pointer: u64) -> Vec<CallSiteFrame> {
+ pub fn call_site_frames(
+ _vm: &Vm,
+ _frame: &ExecutionContext,
+ _index: u32,
+ _frame_pointer: u64,
+ ) -> Vec<CallSiteFrame> {
Vec::new()
}
diff --git a/Libraries/LibJS/Rust/src/jit/entry_exit.rs b/Libraries/LibJS/Rust/src/jit/entry_exit.rs
index b5fb91fb6fe..9273888dfc0 100644
--- a/Libraries/LibJS/Rust/src/jit/entry_exit.rs
+++ b/Libraries/LibJS/Rust/src/jit/entry_exit.rs
@@ -69,7 +69,7 @@ pub fn enter_jit_code(vm: &Vm, frame: NonNull<ExecutionContext>) -> JitResult {
let executable = executable_of(frame_ref);
// NB: The executable keeps its code attached while JIT code may run frames of it, even if it is discarded
// meanwhile (see Executable::detach_discarded_jit_code_if_unused()).
- let entry = executable.jit_code().expect("the executable has JIT code").entry();
+ let entry = executable.jit_code(vm).expect("the executable has JIT code").entry();
enter_from_native_code(vm, frame, entry)
}
@@ -82,7 +82,7 @@ pub fn enter_jit_code_at_loop(vm: &Vm, frame: NonNull<ExecutionContext>, pc: u32
if !can_enter_jit_code(vm, &executable) || !frame_ref.frame_initialized.get() {
return None;
}
- let code = executable.jit_code().expect("the executable has JIT code");
+ let code = executable.jit_code(vm).expect("the executable has JIT code");
let Some(entry) = code.osr_entry(pc) else {
// NB: A fixed budget can keep expiring at the same back edge in nested loops, missing the code's OSR entry
// forever. Try the next back edge when the code has an entry at a loop this one is in. Frames in other
@@ -113,7 +113,7 @@ pub fn enter_jit_code_at_continuation(vm: &Vm, frame: NonNull<ExecutionContext>,
return None;
}
let entry = executable
- .jit_code()
+ .jit_code(vm)
.expect("the executable has JIT code")
.osr_entry(pc)?;
vm.jit.count_coverage("resume-entry");
@@ -163,7 +163,7 @@ pub unsafe extern "C" fn libjs_jit_exit(
let root = NonNull::new(frame).expect("JIT code passes its frame");
// NB: The frame's executable keeps the code attached while JIT code runs the frame, also after it was discarded.
let executable = executable_of(frame_of(root));
- let code = executable.jit_code().expect("the code is attached");
+ let code = executable.jit_code(vm).expect("the code is attached");
match code.site(site).kind {
SiteKind::Exit(_) => translate::exit(vm, root, code, site, dump),
SiteKind::Leave => translate::leave(vm, root, code, site, dump),
@@ -220,7 +220,7 @@ pub unsafe extern "C" fn libjs_jit_finish_inlined_direct_call(
let callee = call_site_callee_frame(vm_ref, root);
let innermost = translate::materialize_call_site_frames(vm_ref, root, site, frame_pointer, Some(callee));
let pc = executable_of(frame_of(root))
- .jit_code()
+ .jit_code(vm_ref)
.expect("the code is attached")
.site(site)
.frames[0]
diff --git a/Libraries/LibJS/Rust/src/jit/testing.rs b/Libraries/LibJS/Rust/src/jit/testing.rs
index 89c71d092b4..7edfaf8108a 100644
--- a/Libraries/LibJS/Rust/src/jit/testing.rs
+++ b/Libraries/LibJS/Rust/src/jit/testing.rs
@@ -169,7 +169,7 @@ fn exit_count(vm: &Vm) -> ThrowCompletionOr<Value> {
let function = function_argument(vm)?;
let count = function
.bytecode_executable()
- .and_then(|executable| executable.jit_code().map(super::code::JitCode::exit_count))
+ .and_then(|executable| executable.jit_code(vm).map(super::code::JitCode::exit_count))
.unwrap_or(0);
Ok(Value::from_i32(i32::try_from(count).unwrap_or(i32::MAX)))
}
diff --git a/Libraries/LibJS/Rust/src/jit/tier_up.rs b/Libraries/LibJS/Rust/src/jit/tier_up.rs
index e007dd86fb3..ffe66574b16 100644
--- a/Libraries/LibJS/Rust/src/jit/tier_up.rs
+++ b/Libraries/LibJS/Rust/src/jit/tier_up.rs
@@ -326,7 +326,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(Box::new(code), compile_wait_budget(&vm.jit.options));
+ executable.install_jit_code(vm, Box::new(code), compile_wait_budget(&vm.jit.options));
}
type SnapshotExecutables = Vec<SnapshotExecutable>;
diff --git a/Libraries/LibJS/Rust/src/jit/translate.rs b/Libraries/LibJS/Rust/src/jit/translate.rs
index 1e3f7beae58..399ae7846ed 100644
--- a/Libraries/LibJS/Rust/src/jit/translate.rs
+++ b/Libraries/LibJS/Rust/src/jit/translate.rs
@@ -501,7 +501,7 @@ pub(super) fn exit(vm: &Vm, root: NonNull<ExecutionContext>, code: &JitCode, ind
}
let compiled_executable = executable_of(frame_of(root));
assert!(core::ptr::eq(
- compiled_executable.jit_code().expect("the code is attached"),
+ compiled_executable.jit_code(vm).expect("the code is attached"),
code
));
@@ -667,7 +667,7 @@ pub(super) fn materialize_call_site_frames(
callee: Option<NonNull<ExecutionContext>>,
) -> NonNull<ExecutionContext> {
let compiled_executable = executable_of(frame_of(root));
- let code = compiled_executable.jit_code().expect("the code is attached");
+ let code = compiled_executable.jit_code(vm).expect("the code is attached");
let site = code.site(index);
assert_eq!(site.kind, SiteKind::Call);
assert!(site.frames.len() >= 2);
@@ -739,10 +739,10 @@ pub fn frame_pointer_from_low_half(low_half: u32) -> u64 {
/// View translation: the frames of the inlined calls that a call at the call site `index` of the code running `frame`
/// is in, innermost first, while it runs without them, with the JIT frame at `frame_pointer`.
-pub fn call_site_frames(frame: &ExecutionContext, index: u32, frame_pointer: u64) -> Vec<CallSiteFrame> {
+pub fn call_site_frames(vm: &Vm, frame: &ExecutionContext, index: u32, frame_pointer: u64) -> Vec<CallSiteFrame> {
let compiled_executable = executable_of(frame);
let code = compiled_executable
- .jit_code()
+ .jit_code(vm)
.expect("a frame at a call site in an inlined callee runs its JIT code");
let site = code.site(index);
assert_eq!(site.kind, SiteKind::Call);
@@ -797,7 +797,7 @@ pub fn call_site_frame_arguments(
) -> Vec<Value> {
let compiled_executable = executable_of(frame);
let code = compiled_executable
- .jit_code()
+ .jit_code(vm)
.expect("a frame at a call site in an inlined callee runs its JIT code");
let site = code.site(index);
let caller_frame_state = &site.frames[frame_state + 1];