Skip to content

Truly independent web browser

LibJS/JIT: Exit when a call without inlined frames gets another callee

Andreas Klingf3b044410723
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs b/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs
index baf4e7da5d9..c8026e188b1 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs
@@ -657,14 +657,21 @@ impl<'a> GraphBuilder<'a> {
};
let slots_offset = u64::from(self.runtime.offsets.execution_context_slots);
if let Some(native) = feedback.native_call.filter(|native| native.entry.is_some()) {
- self.reserve_call_frame(slots_offset + 8 * u64::from(*argument_count));
- self.embed(native.function);
- self.embed(native.realm);
// NB: Native calls take the callee, the `this` value and the
// arguments as inputs like direct calls do.
let mut inputs = vec![*callee, *this_value];
inputs.extend(arguments.iter().copied());
- if let Some(inlined_frame_bytes) = self.inlined_frame_bytes_of_call(&inputs) {
+ // NB: Calls without the frames of the inlined calls they are in check their callee (see
+ // `check_call_without_frames_target()`), and are generic where those checks exited before.
+ let without_frames = self.inlined_frame_bytes_of_call(&inputs);
+ if without_frames.is_some() && !self.may_speculate(ExitKind::BadCallTarget) {
+ return Ok(None);
+ }
+ self.reserve_call_frame(slots_offset + 8 * u64::from(*argument_count));
+ self.embed(native.function);
+ self.embed(native.realm);
+ if let Some(inlined_frame_bytes) = without_frames {
+ self.check_callee(*callee, native.function)?;
let op = Op::CallNative {
executable: self.function.index,
pc: self.function.pc,
@@ -752,6 +759,21 @@ impl<'a> GraphBuilder<'a> {
let Handling::Generic(info) = handling(OpCode::Call) else {
unreachable!("calls are generic instructions");
};
+ // NB: Direct calls take the callee, the `this` value if the callee
+ // reads it, and the arguments as inputs (see
+ // `codegen::DIRECT_CALL_MAX_INPUTS`), and store them straight
+ // into the callee's frame, except in inlined callees whose frames
+ // are written whole, and for the virtual arguments object.
+ // NB: Calls without the frames of the inlined calls they are in
+ // always take `this`, for their slow paths.
+ let mut inputs = vec![*callee, *this_value];
+ inputs.extend(arguments.iter().copied());
+ // NB: Calls without those frames check their callee, unless it is constant (see
+ // `check_call_without_frames_target()`), and are generic where those checks exited before.
+ let without_frames = self.inlined_frame_bytes_of_call(&inputs);
+ if without_frames.is_some() && constant_target.is_none() && !self.may_speculate(ExitKind::BadCallTarget) {
+ return Ok(None);
+ }
let slots_offset = u64::from(self.runtime.offsets.execution_context_slots);
let slot_count = u64::from(target.registers_and_locals_and_constants_count)
+ u64::from((*argument_count).max(target.function.formal_parameter_count));
@@ -777,16 +799,10 @@ impl<'a> GraphBuilder<'a> {
self.embed(cell);
}
}
- // NB: Direct calls take the callee, the `this` value if the callee
- // reads it, and the arguments as inputs (see
- // `codegen::DIRECT_CALL_MAX_INPUTS`), and store them straight
- // into the callee's frame, except in inlined callees whose frames
- // are written whole, and for the virtual arguments object.
- // NB: Calls without the frames of the inlined calls they are in
- // always take `this`, for their slow paths.
- let mut inputs = vec![*callee, *this_value];
- inputs.extend(arguments.iter().copied());
- if let Some(inlined_frame_bytes) = self.inlined_frame_bytes_of_call(&inputs) {
+ if let Some(inlined_frame_bytes) = without_frames {
+ if constant_target.is_none() {
+ self.check_call_without_frames_target(*callee, target)?;
+ }
let op = Op::CallDirect {
executable: self.function.index,
pc: self.function.pc,
@@ -813,6 +829,34 @@ impl<'a> GraphBuilder<'a> {
.map(Some)
}
+ /// Checks that the callee of a direct call of `target` that runs without
+ /// the frames of the inlined calls it is in is the target (or a closure
+ /// of its function, if the site called those), exiting otherwise.
+ /// Where such checks exited before, the call is generic instead.
+ ///
+ /// OPTIMIZATION: Calling another callee there takes the frames of the
+ /// inlined calls created by the runtime first, every time,
+ /// which costs more than a generic call. Its exit lets
+ /// the next compilation make one.
+ fn check_call_without_frames_target(
+ &mut self,
+ callee: Operand,
+ target: crate::snapshot::DirectCallTarget,
+ ) -> Result<(), CompileFailure> {
+ if target.closures {
+ let value = self.read(callee)?;
+ self.emit_checked(
+ Op::CheckClosure {
+ shared_data: target.function.shared_data,
+ },
+ vec![value],
+ None,
+ );
+ return Ok(());
+ }
+ self.check_callee(callee, target.function.function)
+ }
+
/// For a direct or native call in an inlined callee that can run
/// without the frames of the inlined calls it is in, with its operands
/// `inputs` as inputs: the bytes of those frames, which the call leaves
diff --git a/Tests/LibJS/JIT/ir/direct-calls-without-inlined-frames.js b/Tests/LibJS/JIT/ir/direct-calls-without-inlined-frames.js
new file mode 100644
index 00000000000..adaf94c8295
--- /dev/null
+++ b/Tests/LibJS/JIT/ir/direct-calls-without-inlined-frames.js
@@ -0,0 +1,25 @@
+// OPTIONS: inline-depth=1
+
+// A direct call in an inlined callee that runs without the frames of the inlined calls checks its callee first and
+// exits for another one, rather than having the runtime create those frames to call it, every time.
+
+function target(x) {
+ return x + 1;
+}
+function apply(f, x) {
+ return f(x);
+}
+function caller(f, n) {
+ let sum = 0;
+ for (let i = 0; i < n; i++) sum += apply(f, i);
+ return sum;
+}
+jit.prepare(caller);
+jit.prepare(apply);
+for (let i = 0; i < 10; ++i) caller(target, 10);
+jit.compile(caller);
+
+// CHECK-FUNCTION: caller
+// CHECK-PASS: graph building
+// CHECK: CheckValue
+// CHECK-NEXT: CallDirect @e1:{{[0-9]+}} without inlined frames