Skip to content

Truly independent web browser

LibJS: Keep arguments of JIT calls that do not fit inline in one list

Shannon Booth2375d0dcef05
diff --git a/Libraries/LibJS/Rust/src/gc/root.rs b/Libraries/LibJS/Rust/src/gc/root.rs
index 7009e924b99..6c3069915c4 100644
--- a/Libraries/LibJS/Rust/src/gc/root.rs
+++ b/Libraries/LibJS/Rust/src/gc/root.rs
@@ -133,6 +133,10 @@ impl<'vm, T: Trace + Clone + 'static> MarkedVec<'vm, T> {
self.values.get()?.get().borrow().get(index).cloned()
}
+ pub fn resize(&self, length: usize, value: T) {
+ self.rooted_values().borrow_mut().resize(length, value);
+ }
+
pub fn set(&self, index: usize, value: T) {
self.rooted_values().borrow_mut()[index] = value;
}
diff --git a/Libraries/LibJS/Rust/src/interpreter/slow_paths/mod.rs b/Libraries/LibJS/Rust/src/interpreter/slow_paths/mod.rs
index 48628c1d53b..5eabf9b53f6 100644
--- a/Libraries/LibJS/Rust/src/interpreter/slow_paths/mod.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/slow_paths/mod.rs
@@ -32,6 +32,49 @@ fn vm_from_helper_argument<'vm>(argument: u64) -> &'vm Vm {
unsafe { &*core::ptr::with_exposed_provenance::<Vm>(argument as usize) }
}
+/// The slow path of Call, for JIT code too, whose arguments are not in an operand record.
+pub fn call_slow_path(
+ vm: &Vm,
+ pc: u32,
+ instruction: &op::Call,
+ values: &mut op::CallValues,
+ arguments: &[Value],
+) -> SlowPathControl {
+ let control = calls::call(vm, pc, instruction, values, arguments);
+ feedback::record_value_after(vm, control, instruction.value_feedback, || values.dst)
+}
+
+/// The slow path of CallDirectEval, for JIT code too, whose arguments are not in an operand record.
+pub fn call_direct_eval_slow_path(
+ vm: &Vm,
+ pc: u32,
+ instruction: &op::CallDirectEval,
+ values: &mut op::CallDirectEvalValues,
+ arguments: &[Value],
+) -> SlowPathControl {
+ feedback::record_call(vm, instruction.call_feedback, values.callee, call_feedback_flags::NONE);
+ let control = calls::call_direct_eval(vm, pc, instruction, values, arguments);
+ feedback::record_value_after(vm, control, instruction.value_feedback, || values.dst)
+}
+
+/// The slow path of CallConstruct, for JIT code too, whose arguments are not in an operand record.
+pub fn call_construct_slow_path(
+ vm: &Vm,
+ pc: u32,
+ instruction: &op::CallConstruct,
+ values: &mut op::CallConstructValues,
+ arguments: &[Value],
+) -> SlowPathControl {
+ feedback::record_call(
+ vm,
+ instruction.call_feedback,
+ values.callee,
+ call_feedback_flags::SAW_CONSTRUCT,
+ );
+ let control = calls::call_construct(vm, pc, instruction, values, arguments);
+ feedback::record_value_after(vm, control, instruction.value_feedback, || values.dst)
+}
+
impl RuntimeFunctions for Runtime {
// Arithmetic, comparisons, conversions and the jumps on comparisons: operators.rs.
@@ -1120,8 +1163,7 @@ impl RuntimeFunctions for Runtime {
values: &mut op::CallValues,
arguments: &mut [Value],
) -> SlowPathControl {
- let control = calls::call(vm, pc, instruction, values, arguments);
- feedback::record_value_after(vm, control, instruction.value_feedback, || values.dst)
+ call_slow_path(vm, pc, instruction, values, arguments)
}
fn call_direct_eval(
@@ -1131,9 +1173,7 @@ impl RuntimeFunctions for Runtime {
values: &mut op::CallDirectEvalValues,
arguments: &mut [Value],
) -> SlowPathControl {
- feedback::record_call(vm, instruction.call_feedback, values.callee, call_feedback_flags::NONE);
- let control = calls::call_direct_eval(vm, pc, instruction, values, arguments);
- feedback::record_value_after(vm, control, instruction.value_feedback, || values.dst)
+ call_direct_eval_slow_path(vm, pc, instruction, values, arguments)
}
fn call_with_argument_array(
@@ -1165,14 +1205,7 @@ impl RuntimeFunctions for Runtime {
values: &mut op::CallConstructValues,
arguments: &mut [Value],
) -> SlowPathControl {
- feedback::record_call(
- vm,
- instruction.call_feedback,
- values.callee,
- call_feedback_flags::SAW_CONSTRUCT,
- );
- let control = calls::call_construct(vm, pc, instruction, values, arguments);
- feedback::record_value_after(vm, control, instruction.value_feedback, || values.dst)
+ call_construct_slow_path(vm, pc, instruction, values, arguments)
}
fn call_construct_with_argument_array(
diff --git a/Libraries/LibJS/Rust/src/jit/calls.rs b/Libraries/LibJS/Rust/src/jit/calls.rs
index ca9fd0c6919..0b0ce3bf491 100644
--- a/Libraries/LibJS/Rust/src/jit/calls.rs
+++ b/Libraries/LibJS/Rust/src/jit/calls.rs
@@ -7,6 +7,7 @@
//! Calls from JIT code: the call instructions it leaves to the runtime, and the runtime functions its direct calls,
//! the call stub and its call intrinsics use.
+use core::ops::Deref;
use core::ptr::NonNull;
use libjs_abi::register;
@@ -20,8 +21,9 @@ use crate::bytecode::executable::Executable;
use crate::bytecode::instruction::OpCode;
use crate::bytecode::instruction_at;
use crate::bytecode::op;
-use crate::gc::root::MarkedVec;
+use crate::gc::root::{MarkedValues, MarkedVec};
use crate::interpreter::runtime_functions::{Runtime, RuntimeFunctions, SlowPathControl, handle_asm_exception};
+use crate::interpreter::slow_paths;
use crate::interpreter::slow_paths::calls::{
CallType, execute_asm_call, record_callback, throw_error, throw_if_needed_for_asm_call,
};
@@ -399,7 +401,7 @@ fn call_other_function_from_jit(
&& array_like.indexed_storage_kind() == IndexedStorageKind::Packed
&& u64::from(array_like.indexed_packed_elements_span_size()) >= length
{
- let mut arguments = ArgumentStorage::filled(vm, length as usize, |slots| {
+ let arguments = ArgumentStorage::filled(vm, length as usize, |slots| {
array_like.copy_indexed_packed_elements(slots);
});
return Some(call_from_jit_or_generic(
@@ -407,7 +409,7 @@ fn call_other_function_from_jit(
site,
this_value,
target_this,
- arguments.values(),
+ &arguments.values(),
unwrap_depth + 1,
));
}
@@ -456,7 +458,7 @@ fn call_other_function_from_jit(
unwrap_depth + 1,
);
}
- let mut argument_list = ArgumentStorage::filled(vm, bound_argument_count + arguments.len(), |slots| {
+ let argument_list = ArgumentStorage::filled(vm, bound_argument_count + arguments.len(), |slots| {
let (bound_slots, argument_slots) = slots.split_at_mut(bound_argument_count);
for (index, slot) in bound_slots.iter_mut().enumerate() {
*slot = bound_function.bound_argument(index);
@@ -468,7 +470,7 @@ fn call_other_function_from_jit(
site,
target,
bound_function.bound_this(),
- argument_list.values(),
+ &argument_list.values(),
unwrap_depth + 1,
);
}
@@ -505,15 +507,14 @@ fn call_from_jit_or_generic(
}
/// The argument values of a call from JIT code, which it keeps alive: few enough on the stack, which the garbage
-/// collector scans, and others in a rooted list as well.
+/// collector scans, and others in a rooted list.
///
/// NB: The argument values cannot stay alive in the slots of the calling frame: frames of inlined calls that compiled
/// code pushes for a call are not initialized, and the garbage collector does not visit their registers and locals.
struct ArgumentStorage<'vm> {
inline: [Value; ArgumentStorage::INLINE_CAPACITY],
count: usize,
- heap: Vec<Value>,
- _rooted: Option<MarkedVec<'vm, Value>>,
+ rooted: Option<MarkedVec<'vm, Value>>,
}
impl<'vm> ArgumentStorage<'vm> {
@@ -532,28 +533,40 @@ impl<'vm> ArgumentStorage<'vm> {
let mut storage = Self {
inline: [Value::UNDEFINED; Self::INLINE_CAPACITY],
count,
- heap: Vec::new(),
- _rooted: None,
+ rooted: None,
};
if count <= Self::INLINE_CAPACITY {
fill(&mut storage.inline[..count]);
} else {
- storage.heap = vec![Value::UNDEFINED; count];
- fill(&mut storage.heap);
let rooted = MarkedVec::with_capacity(vm, count);
- for value in &storage.heap {
- rooted.push(*value);
- }
- storage._rooted = Some(rooted);
+ rooted.resize(count, Value::UNDEFINED);
+ fill(&mut rooted.values_mut());
+ storage.rooted = Some(rooted);
}
storage
}
- fn values(&mut self) -> &mut [Value] {
- if self.count <= Self::INLINE_CAPACITY {
- &mut self.inline[..self.count]
- } else {
- &mut self.heap
+ fn values(&self) -> ArgumentValues<'_> {
+ match &self.rooted {
+ Some(rooted) => ArgumentValues::Rooted(rooted.values()),
+ None => ArgumentValues::Inline(&self.inline[..self.count]),
+ }
+ }
+}
+
+/// The values of an ArgumentStorage, which it lends.
+enum ArgumentValues<'a> {
+ Inline(&'a [Value]),
+ Rooted(MarkedValues<'a, Value>),
+}
+
+impl Deref for ArgumentValues<'_> {
+ type Target = [Value];
+
+ fn deref(&self) -> &[Value] {
+ match self {
+ Self::Inline(values) => values,
+ Self::Rooted(values) => values,
}
}
}
@@ -612,11 +625,11 @@ pub unsafe extern "C" fn libjs_jit_call(vm: *const Vm, frame: *mut ExecutionCont
let call = instruction!(Call);
let site = site(call.dst, call.length());
let operands = call.arguments();
- let mut storage = ArgumentStorage::new(vm, operands.iter().map(|operand| load(*operand)));
+ let storage = ArgumentStorage::new(vm, operands.iter().map(|operand| load(*operand)));
let arguments = storage.values();
let callee = load(call.callee);
let this_value = load(call.this_value);
- if let Some(control) = call_from_jit(vm, &site, callee, this_value, arguments, 0) {
+ if let Some(control) = call_from_jit(vm, &site, callee, this_value, &arguments, 0) {
return control.0;
}
let mut values = op::CallValues {
@@ -625,14 +638,14 @@ pub unsafe extern "C" fn libjs_jit_call(vm: *const Vm, frame: *mut ExecutionCont
this_value,
arguments: [],
};
- let control = Runtime::call(vm, pc, call, &mut values, arguments);
+ let control = slow_paths::call_slow_path(vm, pc, call, &mut values, &arguments);
finish_generic_call(vm, &site, control, values.dst)
}
_ if opcode == OpCode::CallConstruct as u8 => {
let call = instruction!(CallConstruct);
let site = site(call.dst, call.length());
let operands = call.arguments();
- let mut storage = ArgumentStorage::new(vm, operands.iter().map(|operand| load(*operand)));
+ let storage = ArgumentStorage::new(vm, operands.iter().map(|operand| load(*operand)));
let arguments = storage.values();
let callee = load(call.callee);
if let Some(function) = value_as_ecmascript_function_object(callee)
@@ -640,21 +653,21 @@ pub unsafe extern "C" fn libjs_jit_call(vm: *const Vm, frame: *mut ExecutionCont
&& function.has_constructor()
&& function.bytecode_executable().is_some()
{
- return construct_ecmascript_function_from_jit(vm, &site, function, arguments).0;
+ return construct_ecmascript_function_from_jit(vm, &site, function, &arguments).0;
}
let mut values = op::CallConstructValues {
dst: Value::EMPTY,
callee,
arguments: [],
};
- let control = Runtime::call_construct(vm, pc, call, &mut values, arguments);
+ let control = slow_paths::call_construct_slow_path(vm, pc, call, &mut values, &arguments);
finish_generic_call(vm, &site, control, values.dst)
}
_ if opcode == OpCode::CallDirectEval as u8 => {
let call = instruction!(CallDirectEval);
let site = site(call.dst, call.length());
let operands = call.arguments();
- let mut storage = ArgumentStorage::new(vm, operands.iter().map(|operand| load(*operand)));
+ let storage = ArgumentStorage::new(vm, operands.iter().map(|operand| load(*operand)));
let arguments = storage.values();
let mut values = op::CallDirectEvalValues {
dst: Value::EMPTY,
@@ -662,7 +675,7 @@ pub unsafe extern "C" fn libjs_jit_call(vm: *const Vm, frame: *mut ExecutionCont
this_value: load(call.this_value),
arguments: [],
};
- let control = Runtime::call_direct_eval(vm, pc, call, &mut values, arguments);
+ let control = slow_paths::call_direct_eval_slow_path(vm, pc, call, &mut values, &arguments);
finish_generic_call(vm, &site, control, values.dst)
}
_ if opcode == OpCode::CallWithArgumentArray as u8 => {
@@ -811,13 +824,13 @@ pub unsafe extern "C" fn libjs_jit_call_forwarding_arguments(
passed_argument_count,
);
}
- let mut arguments = ArgumentStorage::new(
+ let arguments = ArgumentStorage::new(
vm,
frame_ref.arguments()[..passed_argument_count]
.iter()
.map(core::cell::Cell::get),
);
- call_from_jit_or_generic(vm, &site, function, this_argument, arguments.values(), 1).0
+ call_from_jit_or_generic(vm, &site, function, this_argument, &arguments.values(), 1).0
}
/// `i64 libjs_jit_finish_direct_call(VM*, ExecutionContext* frame, u32 pc, u64 status)`: finishes a direct call made
@@ -1037,14 +1050,14 @@ pub extern "C" fn libjs_jit_slice_arguments(vm: &Vm, frame: &ExecutionContext, s
} else {
start.min(length)
};
- let mut arguments = ArgumentStorage::new(
+ let arguments = ArgumentStorage::new(
vm,
frame.arguments()[first as usize..length as usize]
.iter()
.map(core::cell::Cell::get),
);
let realm = vm.current_realm().expect("JIT code runs in a realm");
- Value::from_object(Array::create_from(vm, realm, arguments.values())).0
+ Value::from_object(Array::create_from(vm, realm, &arguments.values())).0
}
/// The GetById slow path of JIT code, before the full one. Generic GetById nodes have no inline cache check, so this