Skip to content

Truly independent web browser

LibJS/JIT: Inline calls of known callees whatever their feedback saw

Andreas Klingd24431d8dd87
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/globals.rs b/Libraries/LibJS/JIT/Rust/src/builder/globals.rs
index ca7b86a61ea..3aea999a028 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/globals.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/globals.rs
@@ -335,6 +335,9 @@ impl GraphBuilder<'_> {
if let Some(cell) = value.cell {
self.embed(cell);
}
+ if let Some(index) = value.inline_executable {
+ self.inlinable_functions.insert(value.bits, index);
+ }
if let Some(intrinsic) = value.intrinsic {
self.intrinsics.insert(value.bits, intrinsic);
}
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs b/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs
index c8026e188b1..467b0fadc0c 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs
@@ -56,6 +56,7 @@ use crate::ir::NodeId;
use crate::ir::Op;
use crate::ir::ShapeCheck;
use crate::ir::value;
+use crate::snapshot::CallFeedbackSnapshot;
use crate::snapshot::CellId;
use crate::snapshot::ConstructTarget;
use crate::snapshot::Forwarding;
@@ -920,7 +921,7 @@ impl<'a> GraphBuilder<'a> {
Ok(Flow::Continue)
}
- /// Inlines a `Call`, if its feedback saw a single callee that qualifies,
+ /// Inlines a `Call`, if its callee is a function the code knows, or its feedback saw a single callee that qualifies,
/// or a single function its callee forwarded every call to: through
/// `Function.prototype.call` (`f.call(this_arg, ...)`), through
/// `Function.prototype.apply` with the arguments object the code never
@@ -942,6 +943,26 @@ impl<'a> GraphBuilder<'a> {
if let Some(flow) = self.try_inline_closure_call(instruction)? {
return Ok(Some(flow));
}
+ // NB: A callee the code knows exactly is what the call calls, whatever callees its feedback saw.
+ if let Some(index) = self.constant_inlinable_function(*callee) {
+ let callback = self
+ .call_feedback(*call_feedback)
+ .filter(|feedback| feedback.inline_executable == Some(index))
+ .and_then(|feedback| forwarded_callback(&feedback));
+ let call = InlinedCallSite {
+ callback,
+ ..InlinedCallSite::new(
+ index,
+ InlineValue::Operand(*this_value),
+ arguments.iter().copied().map(InlineValue::Operand).collect(),
+ *argument_count,
+ *dst,
+ )
+ };
+ if let Some(flow) = self.inline_call(call)? {
+ return Ok(Some(flow));
+ }
+ }
// NB: The feedback of a builtin's calls comes from all its callers,
// whose callbacks it calls, so it says nothing about this one.
// Calls of its callback inline what its call site passed it.
@@ -977,16 +998,7 @@ impl<'a> GraphBuilder<'a> {
return Ok(None);
};
if let Some(index) = feedback.inline_executable {
- // NB: Builtins that take callbacks record them as forwarded calls.
- let callback = feedback
- .forwarded
- .filter(|forwarded| forwarded.forwarding == Forwarding::Callback)
- .and_then(|forwarded| {
- Some(CallbackTarget {
- index: forwarded.inline_executable?,
- closures: feedback.flags & call_feedback_flags::FORWARDED_CLOSURES != 0,
- })
- });
+ let callback = forwarded_callback(&feedback);
let call = InlinedCallSite {
callee: Some(*callee),
callback,
@@ -1558,7 +1570,7 @@ impl<'a> GraphBuilder<'a> {
InlinedCallKind::Construct
};
let Some(InlinableCallee { executable, .. }) =
- self.inlinable_callee(target.executable, kind, false, &inlined_above)
+ self.inlinable_callee(target.executable, kind, false, true, &inlined_above)
else {
return false;
};
@@ -2058,6 +2070,19 @@ impl<'a> GraphBuilder<'a> {
&self,
callee_executable: &crate::snapshot::ExecutableSnapshot,
inlined_above: &[CellId],
+ ) -> bool {
+ self.may_inline_unchecked_calls_of_below(callee_executable, inlined_above)
+ && self.may_speculate(ExitKind::BadCallTarget)
+ }
+
+ /// Like `may_inline_calls_of_below()`, for a call that inlines its
+ /// callee without checking it, since the code knows exactly what it is:
+ /// such a call cannot exit for another callee, even where checks of
+ /// the callee exited before.
+ fn may_inline_unchecked_calls_of_below(
+ &self,
+ callee_executable: &crate::snapshot::ExecutableSnapshot,
+ inlined_above: &[CellId],
) -> bool {
const MAX_RECURSION_DEPTH: usize = 2;
let recursion_depth = core::iter::once(&self.function)
@@ -2068,7 +2093,6 @@ impl<'a> GraphBuilder<'a> {
.count();
recursion_depth <= MAX_RECURSION_DEPTH
&& self.callers.len() + inlined_above.len() < self.snapshot.options.inlining.max_depth as usize
- && self.may_speculate(ExitKind::BadCallTarget)
}
/// Whether a call with a few callees (see `try_inline_callee_cases()`)
@@ -2096,7 +2120,8 @@ impl<'a> GraphBuilder<'a> {
}
/// The snapshot executable `index`, if a call of the
- /// kind `kind` (a closure call if `closure`) may inline it here once the
+ /// kind `kind` (a closure call if `closure`, which checks its callee if
+ /// `checks_callee`) may inline it here once the
/// executables `inlined_above` (their cells) are inlined here, one in the
/// other: the checks of `inline_call()` that do not depend on the values
/// at the call site, which `may_inline_super_constructor()` makes ahead
@@ -2106,12 +2131,18 @@ impl<'a> GraphBuilder<'a> {
index: u32,
kind: InlinedCallKind,
closure: bool,
+ checks_callee: bool,
inlined_above: &[CellId],
) -> Option<InlinableCallee<'a>> {
let snapshot = self.snapshot;
let callee_executable = snapshot.executables.get(index as usize)?;
callee_executable.function?;
- if !self.may_inline_calls_of_below(callee_executable, inlined_above) {
+ let may_inline = if checks_callee {
+ self.may_inline_calls_of_below(callee_executable, inlined_above)
+ } else {
+ self.may_inline_unchecked_calls_of_below(callee_executable, inlined_above)
+ };
+ if !may_inline {
return None;
}
let decoded = super::decoded_function(callee_executable, &self.decoded[index as usize]).ok()?;
@@ -2215,6 +2246,7 @@ impl<'a> GraphBuilder<'a> {
(Some(_), Some(_)) => InlinedCallKind::DerivedConstruct,
},
closure.is_some(),
+ callee.is_some() || check_closure || construct.is_some(),
&[],
)
else {
@@ -2457,3 +2489,14 @@ impl<'a> GraphBuilder<'a> {
Ok(Flow::Ended)
}
}
+
+/// The callback a call of a builtin that takes callbacks passed it, which such builtins record as forwarded calls.
+fn forwarded_callback(feedback: &CallFeedbackSnapshot) -> Option<CallbackTarget> {
+ let forwarded = feedback
+ .forwarded
+ .filter(|forwarded| forwarded.forwarding == Forwarding::Callback)?;
+ Some(CallbackTarget {
+ index: forwarded.inline_executable?,
+ closures: feedback.flags & call_feedback_flags::FORWARDED_CLOSURES != 0,
+ })
+}
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/tests.rs b/Libraries/LibJS/JIT/Rust/src/builder/tests.rs
index 41f9bff8a5e..975bc36b4af 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/tests.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/tests.rs
@@ -2764,6 +2764,7 @@ fn captured_object_bindings_are_guarded_before_becoming_constants() {
bits,
cell: Some(object),
intrinsic: None,
+ inline_executable: None,
typed_array: None,
has_instance: None,
},
@@ -2818,6 +2819,7 @@ fn captured_typed_arrays_use_guarded_fixed_lengths() {
bits,
cell: Some(object),
intrinsic: None,
+ inline_executable: None,
has_instance: None,
typed_array: Some(TypedArraySnapshot {
element: TypedArrayElement::Uint16,
@@ -2946,6 +2948,7 @@ mod globals {
assigned: true,
value: GlobalValueSnapshot {
bits: crate::ir::value::UNDEFINED,
+ inline_executable: None,
cell: None,
intrinsic: None,
has_instance: None,
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs b/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs
index feb85d2ec24..2a359f90d21 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs
@@ -2467,6 +2467,7 @@ fn global_variables_the_snapshot_knows() {
bits,
cell: None,
intrinsic: None,
+ inline_executable: None,
typed_array: None,
has_instance: None,
};
@@ -2640,6 +2641,7 @@ fn instance_of_global_functions_walks_prototype_chains() {
assigned: false,
value: Some(GlobalValueSnapshot {
bits: function_value,
+ inline_executable: None,
cell: None,
intrinsic: None,
typed_array: None,
diff --git a/Libraries/LibJS/JIT/Rust/src/snapshot.rs b/Libraries/LibJS/JIT/Rust/src/snapshot.rs
index c954e308c5c..ca1dc1871f0 100644
--- a/Libraries/LibJS/JIT/Rust/src/snapshot.rs
+++ b/Libraries/LibJS/JIT/Rust/src/snapshot.rs
@@ -233,6 +233,9 @@ pub struct GlobalValueSnapshot {
pub cell: Option<CellId>,
/// Which intrinsic the value is, if it is one the compiler knows.
pub intrinsic: Option<Intrinsic>,
+ /// The snapshot executable of the value, if it is a function whose
+ /// calls JIT code may inline.
+ pub inline_executable: Option<u32>,
/// How `instanceof` with the value on its right-hand side runs, if the
/// value is a function that inherits `%Function.prototype%`'s
/// `@@hasInstance` (which can never change): OrdinaryHasInstance with its
diff --git a/Libraries/LibJS/Rust/src/jit/snapshot.rs b/Libraries/LibJS/Rust/src/jit/snapshot.rs
index 4c1ed8e7a0a..f8937a81534 100644
--- a/Libraries/LibJS/Rust/src/jit/snapshot.rs
+++ b/Libraries/LibJS/Rust/src/jit/snapshot.rs
@@ -535,16 +535,17 @@ impl SnapshotBuilder<'_> {
PropertyCacheSnapshot { kind, entries }
}
- /// What the global variable caches of `executable`, whose frames run in `realm`, know about the global variables
- /// of the realm, as far as it still holds.
- fn globals(&mut self, executable: Gc<Executable>, realm: Gc<Realm>) -> GlobalsSnapshot {
+ /// What the global variable caches of `executable`, whose frames run in the realm of `caller`, know about the global
+ /// variables of the realm, as far as it still holds.
+ fn globals(&mut self, executable: Gc<Executable>, caller: &Entry) -> GlobalsSnapshot {
+ let realm = caller.realm;
let object = realm.global_object();
let environment = realm.global_declarative_environment();
let environment_serial = environment.environment_serial_number();
let shape = object.shape();
let mut caches = Vec::with_capacity(executable.global_variable_caches().len());
for cache in executable.global_variable_caches() {
- caches.push(self.global_cache(cache, realm, object, environment, environment_serial, shape));
+ caches.push(self.global_cache(cache, object, environment, environment_serial, shape, caller));
}
GlobalsSnapshot {
object: self.cell(object),
@@ -558,12 +559,13 @@ impl SnapshotBuilder<'_> {
fn global_cache(
&mut self,
cache: &GlobalVariableCache,
- realm: Gc<Realm>,
object: Gc<Object>,
environment: Gc<DeclarativeEnvironment>,
environment_serial: u64,
shape: Gc<Shape>,
+ caller: &Entry,
) -> Option<GlobalCacheSnapshot> {
+ let realm = caller.realm;
if cache.environment_serial_number.get() != environment_serial {
return None;
}
@@ -585,7 +587,7 @@ impl SnapshotBuilder<'_> {
&object,
entry.property_offset,
),
- value: self.global_value(value, realm),
+ value: self.global_value(value, realm, Some(caller)),
});
}
if !cache.has_environment_binding_index.get() {
@@ -597,14 +599,17 @@ impl SnapshotBuilder<'_> {
index,
mutable: environment.binding_is_mutable(index as usize),
assigned: environment.binding_may_have_been_assigned(index as usize),
- value: (!value.is_empty()).then(|| self.global_value(value, realm)),
+ value: (!value.is_empty()).then(|| self.global_value(value, realm, Some(caller))),
})
}
- fn global_value(&mut self, value: Value, realm: Gc<Realm>) -> GlobalValueSnapshot {
+ /// The value of a global variable. Functions are inlining candidates of the call sites of `caller`, if given,
+ /// since the code may call them after passing them on, which no call feedback of `caller` records.
+ fn global_value(&mut self, value: Value, realm: Gc<Realm>, caller: Option<&Entry>) -> GlobalValueSnapshot {
let cell = value.is_cell().then(|| value.as_cell());
GlobalValueSnapshot {
bits: value.0,
+ inline_executable: caller.and_then(|caller| self.inline_executable_index(cell, caller)),
cell: cell.map(|cell| self.cell(cell)),
intrinsic: intrinsic_of(self.vm, cell),
has_instance: self.ordinary_has_instance(value, realm),
@@ -1306,7 +1311,7 @@ impl SnapshotBuilder<'_> {
},
// NB: Builtins written in JavaScript never access global variables by name.
globals: (!matches!(entry.function, Some(InlinedFunction::Builtin(_))))
- .then(|| self.globals(executable, realm)),
+ .then(|| self.globals(executable, entry)),
super_constructor,
has_instance_elements,
literal_instance_fields,
@@ -1371,7 +1376,7 @@ impl SnapshotBuilder<'_> {
}
let value = environment.get_initialized_binding_value_direct(coordinate.index as usize);
if value.is_object() {
- bindings.push((coordinate, self.global_value(value, realm)));
+ bindings.push((coordinate, self.global_value(value, realm, None)));
}
}
bindings
diff --git a/Tests/LibJS/JIT/ir/constant-callees-inline-whatever-the-feedback-saw.js b/Tests/LibJS/JIT/ir/constant-callees-inline-whatever-the-feedback-saw.js
new file mode 100644
index 00000000000..37a5d7bf337
--- /dev/null
+++ b/Tests/LibJS/JIT/ir/constant-callees-inline-whatever-the-feedback-saw.js
@@ -0,0 +1,53 @@
+// A call whose callee the code knows exactly inlines it, even where the call site saw other callees.
+
+function apply(f, x) {
+ return f(x);
+}
+function increment(x) {
+ return x + 1;
+}
+function decrement(x) {
+ return x - 1;
+}
+const doubled = x => x * 2;
+function caller(n) {
+ let sum = 0;
+ for (let i = 0; i < n; i++) sum += apply(doubled, i);
+ return sum;
+}
+jit.prepare(caller);
+jit.prepare(apply);
+for (let i = 0; i < 10; ++i) {
+ apply(increment, i);
+ apply(decrement, i);
+ caller(10);
+}
+jit.compile(caller);
+
+// CHECK-FUNCTION: caller
+// CHECK-PASS: graph building
+// CHECK-NOT: CallDirect
+// CHECK-NOT: CallSlowPath Call
+// CHECK: Int32Mul
+
+// A known builtin that takes a callback still inlines the calls of the callback its call site passed it.
+
+const todos = {
+ items: [{ completed: false }, { completed: true }],
+ active: function () {
+ return this.items.filter(function (item) {
+ return !item.completed;
+ });
+ },
+ count: function () {
+ return this.active().length;
+ },
+};
+jit.prepare(todos.count);
+for (let i = 0; i < 10; ++i) todos.count();
+jit.compile(todos.count);
+
+// CHECK-FUNCTION: count
+// CHECK-PASS: graph building
+// CHECK-NOT: CallDirect @e2
+// CHECK: CallArrayPush