Skip to content

Truly independent web browser

LibJS: Read passed arguments in JIT code and inline their readers

Andreas Kling9783778935e6
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/arguments.rs b/Libraries/LibJS/JIT/Rust/src/builder/arguments.rs
index 4f45312348d..fc4c3c5c945 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/arguments.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/arguments.rs
@@ -48,6 +48,7 @@ use crate::bytecode::Instruction;
use crate::bytecode::MAPPED_ARGUMENTS;
use crate::bytecode::OpCode;
use crate::bytecode::Operand;
+use crate::bytecode::SlotKind;
use crate::code::ExitKind;
use crate::code::Repr;
use crate::ir::Graph;
@@ -103,6 +104,38 @@ pub(super) fn can_create_virtual_arguments(function: &super::Function<'_>, instr
&& function.may_write_parameter_bindings)
}
+/// The index of `instruction` of `function` if it is a `GetArgument` of a
+/// constant index that may be a passed argument.
+fn constant_index(function: &super::Function<'_>, instruction: &Instruction) -> Option<u32> {
+ let Instruction::GetArgument { index, .. } = *instruction else {
+ return None;
+ };
+ let SlotKind::Constant(constant) = function.layout.slot_kind(index) else {
+ return None;
+ };
+ function
+ .executable
+ .constants
+ .get(constant as usize)
+ .and_then(|bits| value::as_int32(*bits))
+ .and_then(|index| u32::try_from(index).ok())
+}
+
+/// Whether `instruction` of `function` is a `GetArgument` of a constant
+/// index that may be a passed argument.
+pub(super) fn has_constant_index(function: &super::Function<'_>, instruction: &Instruction) -> bool {
+ constant_index(function, instruction).is_some()
+}
+
+/// Whether `function` reads an argument of a constant index that a call
+/// passing `passed_argument_count` arguments does not pass, which an inlined
+/// call of it could not read (see `try_build_argument()`).
+pub(super) fn reads_argument_not_passed(function: &super::Function<'_>, passed_argument_count: u32) -> bool {
+ function.instructions.iter().any(|instruction| {
+ constant_index(function, &instruction.instruction).is_some_and(|index| index >= passed_argument_count)
+ })
+}
+
/// Whether a node of `graph` takes a virtual arguments object as an input,
/// which only a real object can be.
pub(super) fn uses_virtual_arguments(graph: &Graph) -> bool {
@@ -181,6 +214,16 @@ impl GraphBuilder<'_> {
Some(self.frame.slots[slot].node)
}
+ /// `inlined_call_argument()` of the constant index that `index` is, if it
+ /// is one.
+ fn inlined_call_argument_at(&self, index: NodeId) -> Option<NodeId> {
+ self.graph
+ .constant_value(index)
+ .and_then(value::as_int32)
+ .and_then(|index| u32::try_from(index).ok())
+ .and_then(|index| self.inlined_call_argument(index))
+ }
+
/// The values the call of the inlined callee being built passed it, all
/// of them, as arguments of a call it inlines, and their count; `None`
/// outside inlined callees.
@@ -239,13 +282,7 @@ impl GraphBuilder<'_> {
// NB: Inlined callees read constant indices of what their call passed.
if self.function.is_virtual() {
let index = self.read(property)?;
- let Some(value) = self
- .graph
- .constant_value(index)
- .and_then(value::as_int32)
- .and_then(|index| u32::try_from(index).ok())
- .and_then(|index| self.inlined_call_argument(index))
- else {
+ let Some(value) = self.inlined_call_argument_at(index) else {
return Ok(false);
};
self.write(dst, value)?;
@@ -261,6 +298,39 @@ impl GraphBuilder<'_> {
Ok(true)
}
+ /// Builds `dst = arguments[index]` of a function that reads its arguments
+ /// from the frame (`GetArgument`), which has no arguments object, like
+ /// `try_build_arguments_index()` does for a virtual one. Returns false,
+ /// having built nothing that matters, where it can only be the slow path.
+ pub(super) fn try_build_argument(&mut self, dst: Operand, index: Operand) -> Result<bool, CompileFailure> {
+ // NB: Inlined callees read constant indices of what their call passed, as their current values: a mapped
+ // arguments object aliases the parameters, and an unmapped one belongs to a function that never writes
+ // them. Calls that do not pass an index that the callee reads are not inlined (see `inline_call()`), and
+ // anything else has the graph built again without inlining such callees (see `can_inline()`).
+ if self.function.is_virtual() {
+ self.virtualize_arguments.inlined_callees_virtualized.set(true);
+ let index = self.read(index)?;
+ let Some(value) = self.inlined_call_argument_at(index) else {
+ return Err(CompileFailure::UnsupportedInstruction {
+ pc: self.function.pc,
+ opcode: OpCode::GetArgument,
+ reason: "reads an argument that the inlined call did not pass",
+ });
+ };
+ self.write(dst, value)?;
+ return Ok(true);
+ }
+ // NB: Frame memory has the argument slots that the function never writes, and the slow path the others.
+ if self.function.writes_arguments || !self.may_speculate(ExitKind::ArgumentsIndex) {
+ return Ok(false);
+ }
+ let index = self.read(index)?;
+ let arguments_base = self.function.layout.arguments_base();
+ let argument = self.emit_checked(Op::LoadArgument { arguments_base }, vec![index], Some(Repr::Tagged));
+ self.write(dst, argument)?;
+ Ok(true)
+ }
+
/// Builds `slice.call(arguments)` and `slice.call(arguments, start)` with
/// a virtual arguments object, where `slice` is `Array.prototype.slice`,
/// as a `SliceArguments`. Returns `None`, having built nothing, for
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs b/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs
index b2bad6cb504..8fb75d4405c 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs
@@ -140,6 +140,10 @@ fn can_inline(callee: &Function<'_>, virtual_arguments: bool, derived_construct:
if opcode == OpCode::Catch {
return !super::has_run(callee.executable, instruction.pc);
}
+ // NB: An inlined callee reads what its call passed for constant indices (see `try_build_argument()`).
+ if opcode == OpCode::GetArgument {
+ return virtual_arguments && arguments::has_constant_index(callee, &instruction.instruction);
+ }
let reads_frame_fields = matches!(
opcode,
OpCode::GetLexicalEnvironment
@@ -150,7 +154,6 @@ fn can_inline(callee: &Function<'_>, virtual_arguments: bool, derived_construct:
| OpCode::LeavePrivateEnvironment
| OpCode::CreateArguments
| OpCode::CreateRestParams
- | OpCode::GetArgument
| OpCode::CallDirectEval
| OpCode::CallDirectEvalWithArgumentArray
| OpCode::Debugger
@@ -2084,10 +2087,12 @@ impl<'a> GraphBuilder<'a> {
// create the object from.
let virtual_arguments =
self.virtualize_arguments.compiled_function && self.virtualize_arguments.inlined_callees;
- let creates_arguments = decoded
- .instructions
- .iter()
- .any(|instruction| instruction.instruction.opcode() == OpCode::CreateArguments);
+ let uses_passed_arguments = decoded.instructions.iter().any(|instruction| {
+ matches!(
+ instruction.instruction.opcode(),
+ OpCode::CreateArguments | OpCode::GetArgument
+ )
+ });
// NB: A derived constructor that passes its arguments on to super() keeps them all, like legacy `arguments`.
let derived_construct = kind == InlinedCallKind::DerivedConstruct;
let forwards_arguments_to_super = derived_construct
@@ -2097,7 +2102,7 @@ impl<'a> GraphBuilder<'a> {
.any(|instruction| instruction.instruction.opcode() == OpCode::SuperCallWithForwardedArguments);
let has_legacy_arguments = callee_executable.supports_legacy_arguments
|| (closure && callee_executable.is_non_strict_function())
- || (virtual_arguments && creates_arguments)
+ || (virtual_arguments && uses_passed_arguments)
|| forwards_arguments_to_super;
let callee_function = Function::new(index, callee_executable, decoded, has_legacy_arguments).ok()?;
if !can_inline(&callee_function, virtual_arguments, derived_construct) {
@@ -2170,6 +2175,10 @@ impl<'a> GraphBuilder<'a> {
else {
return Ok(None);
};
+ // NB: What a real frame would read for an argument that the call did not pass depends on %Object.prototype%.
+ if arguments::reads_argument_not_passed(&callee_function, passed_argument_count) {
+ return Ok(None);
+ }
let function = callee_executable.function.expect("inlined callees have a function");
let instruction_count = callee_function.instructions.len();
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/mod.rs b/Libraries/LibJS/JIT/Rust/src/builder/mod.rs
index 35ab6602145..7454f520ab7 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/mod.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/mod.rs
@@ -567,7 +567,15 @@ impl<'a> GraphBuilder<'a> {
pc: 0,
reason: "no executable",
})?;
- let function = Function::new(0, executable, decoded_function(executable, &decoded[0])?, false)?;
+ let decoded_root = decoded_function(executable, &decoded[0])?;
+ // NB: `GetArgument` reads the argument slots from frame memory, which only the slots it keeps live hold the
+ // current values of: a dead slot may be in sync with `undefined` instead (see `dead_frame()`). Inlined
+ // callees that read their passed arguments keep them live the same way (see `inlinable_callee()`).
+ let reads_argument_slots = decoded_root
+ .instructions
+ .iter()
+ .any(|instruction| instruction.instruction.opcode() == OpCode::GetArgument);
+ let function = Function::new(0, executable, decoded_root, reads_argument_slots)?;
let arguments_base = function.layout.arguments_base();
let legacy_arguments = if executable.supports_legacy_arguments {
arguments_base..arguments_base + function.layout.number_of_arguments
@@ -1472,6 +1480,11 @@ impl<'a> GraphBuilder<'a> {
return Ok(None);
}
}
+ Instruction::GetArgument { dst, index, .. } => {
+ if !self.try_build_argument(dst, index)? {
+ return Ok(None);
+ }
+ }
Instruction::NewArray { dst, ref elements, .. } => {
if elements.is_empty()
&& let Some(flow) = self.try_build_spread_forwarding(dst)?
diff --git a/Tests/LibJS/Runtime/arguments-length-and-index-reads.js b/Tests/LibJS/Runtime/arguments-length-and-index-reads.js
index 0275231de62..bbbdcb49203 100644
--- a/Tests/LibJS/Runtime/arguments-length-and-index-reads.js
+++ b/Tests/LibJS/Runtime/arguments-length-and-index-reads.js
@@ -831,3 +831,31 @@ describe("functions inside functions that use the arguments object", () => {
expect(sloppy(1)).toEqual([undefined, 1]);
});
});
+
+describe("compiled functions", () => {
+ test("parameter written before a branch, then read through arguments[i]", () => {
+ let calls = 0;
+ function sideEffect() {
+ ++calls;
+ }
+ const object = {
+ method(a, flag) {
+ a = 7;
+ if (flag) sideEffect();
+ return arguments[0];
+ },
+ };
+ function plain(a, flag) {
+ a = 7;
+ if (flag) sideEffect();
+ return arguments[0];
+ }
+ let wrong = 0;
+ for (let i = 0; i < 20000; ++i) {
+ if (object.method(1, i & 1) !== 7) ++wrong;
+ if (plain(1, i & 1) !== 7) ++wrong;
+ }
+ expect(wrong).toBe(0);
+ expect(calls).toBe(20000);
+ });
+});