Skip to content

Truly independent web browser

LibJS/JIT: Inline calls of functions with up to 64 bound arguments

Andreas Kling10b47c7577d4
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs b/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs
index 2daa6888325..e82e1036756 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs
@@ -777,7 +777,16 @@ impl GraphBuilder<'_> {
let Some(bound_function) = feedback.target else {
return Ok(None);
};
- let bound_arguments = &forwarded.bound_arguments[..usize::from(forwarded.bound_argument_count)];
+ let start = forwarded.bound_arguments_start as usize;
+ let Some(bound_arguments) = self
+ .function
+ .executable
+ .feedback
+ .bound_arguments
+ .get(start..start + forwarded.bound_argument_count as usize)
+ else {
+ return Ok(None);
+ };
InlinedCallSite {
callee: Some(*callee),
expected_callee: Some(bound_function),
@@ -791,7 +800,7 @@ impl GraphBuilder<'_> {
.map(InlineValue::Constant)
.chain(arguments.iter().copied().map(InlineValue::Operand))
.collect(),
- u32::from(forwarded.bound_argument_count) + argument_count,
+ forwarded.bound_argument_count + argument_count,
*dst,
)
}
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/tests.rs b/Libraries/LibJS/JIT/Rust/src/builder/tests.rs
index 497ee812c81..cd7a7088111 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/tests.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/tests.rs
@@ -2296,7 +2296,7 @@ pub(crate) mod inlining {
argument_count: 1,
inline_executable: Some(1),
bound_this: value::UNDEFINED,
- bound_arguments: [value::int32(7), 0, 0, 0],
+ bound_arguments_start: 0,
bound_argument_count: 1,
};
@@ -2336,11 +2336,34 @@ pub(crate) mod inlining {
snapshot.executables[0].feedback.call[0].target = Some(CellId(0x9100));
snapshot.executables[0].feedback.call[0].inline_executable = None;
snapshot.executables[0].feedback.call[0].forwarded = Some(forwarded(Forwarding::Bound));
+ snapshot.executables[0].feedback.bound_arguments = vec![value::int32(7)];
let graph = optimized_graph(&snapshot);
let text = dump(&graph, &test_layout());
assert!(text.contains("CheckValue 0xfff9000000009100"), "{text}");
assert!(text.contains("Return (#7)"), "{text}");
assert!(graph.embedded_cells.contains(&CellId(0x9100)));
+
+ // Five bound arguments from the middle of the table: the callee gets the last.
+ let fifth_argument = Operand::from_raw(test_layout().arguments_base() + 4);
+ let fifth = assemble(|_| vec![Instruction::Enter, Instruction::Return { value: fifth_argument }]);
+ let mut snapshot = snapshot_with_callee(&caller, &fifth, function(true, false));
+ snapshot.executables[1].layout.number_of_arguments = 5;
+ snapshot.executables[0].feedback.call[0].target = Some(CellId(0x9100));
+ snapshot.executables[0].feedback.call[0].inline_executable = None;
+ let mut five = forwarded(Forwarding::Bound);
+ five.bound_arguments_start = 2;
+ five.bound_argument_count = 5;
+ snapshot.executables[0].feedback.call[0].forwarded = Some(five);
+ snapshot.executables[0].feedback.bound_arguments = (1..=8).map(value::int32).collect();
+ let text = dump(&optimized_graph(&snapshot), &test_layout());
+ assert!(text.contains("CheckValue 0xfff9000000009100"), "{text}");
+ assert!(text.contains("Return (#7)"), "{text}");
+
+ // Bound arguments past the end of the table are not inlined.
+ five.bound_arguments_start = 4;
+ snapshot.executables[0].feedback.call[0].forwarded = Some(five);
+ let text = dump(&optimized_graph(&snapshot), &test_layout());
+ assert!(!text.contains("CheckValue 0xfff9000000009100"), "{text}");
}
#[test]
diff --git a/Libraries/LibJS/JIT/Rust/src/snapshot.rs b/Libraries/LibJS/JIT/Rust/src/snapshot.rs
index 289eb5193e4..e1e96627a8c 100644
--- a/Libraries/LibJS/JIT/Rust/src/snapshot.rs
+++ b/Libraries/LibJS/JIT/Rust/src/snapshot.rs
@@ -391,6 +391,9 @@ pub struct FeedbackSnapshot {
pub value: Vec<u16>,
pub call: Vec<CallFeedbackSnapshot>,
pub keyed: Vec<KeyedFeedbackSnapshot>,
+ /// The bound arguments of the bound functions that call sites forwarded
+ /// to, as NaN-boxed values (see `ForwardedCallSnapshot::bound_arguments_start`).
+ pub bound_arguments: Vec<u64>,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
@@ -463,11 +466,18 @@ pub struct ForwardedCallSnapshot {
/// The index in `Snapshot::executables` of `target`'s executable, if it
/// is an inlining candidate.
pub inline_executable: Option<u32>,
- /// For bound functions: the bound `this` and the first
- /// `bound_argument_count` bound arguments, as NaN-boxed values.
+ /// For bound functions that are inlining candidates: the bound `this`, as
+ /// a NaN-boxed value, and where the bound arguments start in
+ /// `FeedbackSnapshot::bound_arguments`, and how many there are.
pub bound_this: u64,
- pub bound_arguments: [u64; 4],
- pub bound_argument_count: u8,
+ pub bound_arguments_start: u32,
+ pub bound_argument_count: u32,
+}
+
+impl ForwardedCallSnapshot {
+ /// The most bound arguments a bound function can have for its calls to be
+ /// inlined, as each one becomes a constant of the inlined call.
+ pub const MAX_INLINED_BOUND_ARGUMENTS: usize = 64;
}
/// A call target JIT code can call directly, building its frame like the
diff --git a/Libraries/LibJS/Rust/src/jit/snapshot.rs b/Libraries/LibJS/Rust/src/jit/snapshot.rs
index e7d55351905..cf09b589c24 100644
--- a/Libraries/LibJS/Rust/src/jit/snapshot.rs
+++ b/Libraries/LibJS/Rust/src/jit/snapshot.rs
@@ -111,6 +111,8 @@ struct SnapshotBuilder<'vm> {
lexical_environment_templates: Vec<Option<LexicalEnvironmentTemplateSnapshot>>,
/// The environment templates of the direct calls of the executable whose snapshot is being made.
environment_templates: Vec<EnvironmentTemplateSnapshot>,
+ /// The bound arguments of the call feedback of the executable whose snapshot is being made.
+ bound_arguments: Vec<u64>,
/// For the constructors among the inlining candidates, by index: the shape of the `this` value their inlined
/// constructs create, whose entries their megamorphic caches describe.
construct_this_shapes: HashMap<u32, Gc<Shape>>,
@@ -714,37 +716,44 @@ impl SnapshotBuilder<'_> {
/// Describes how the single callee of a call site forwarded its calls, if the snapshot can describe it.
fn forwarded_call(&mut self, call: &CallFeedback, caller: &Entry) -> Option<ForwardedCallSnapshot> {
let target = call.forwarded_target()?;
+ let forwarding = match call.forwarding.get() {
+ forwarding if forwarding == CallFeedbackForwarding::Apply as u8 => Forwarding::Apply,
+ forwarding if forwarding == CallFeedbackForwarding::Call as u8 => Forwarding::Call,
+ forwarding if forwarding == CallFeedbackForwarding::Bound as u8 => Forwarding::Bound,
+ forwarding if forwarding == CallFeedbackForwarding::Callback as u8 => Forwarding::Callback,
+ _ => return None,
+ };
+ let bound = match forwarding {
+ Forwarding::Bound => Some(call.target()?.downcast::<BoundFunction>()?),
+ _ => None,
+ };
let mut snapshot = ForwardedCallSnapshot {
- forwarding: match call.forwarding.get() {
- forwarding if forwarding == CallFeedbackForwarding::Apply as u8 => Forwarding::Apply,
- forwarding if forwarding == CallFeedbackForwarding::Call as u8 => Forwarding::Call,
- forwarding if forwarding == CallFeedbackForwarding::Bound as u8 => Forwarding::Bound,
- forwarding if forwarding == CallFeedbackForwarding::Callback as u8 => Forwarding::Callback,
- _ => return None,
- },
+ forwarding,
target: self.cell(target),
target_intrinsic: intrinsic_of(self.vm, Some(target)),
argument_count: u32::from(call.forwarded_argument_count.get()),
- inline_executable: None,
+ inline_executable: match bound {
+ Some(bound) if bound.bound_arguments().len() > ForwardedCallSnapshot::MAX_INLINED_BOUND_ARGUMENTS => {
+ None
+ }
+ _ => self.inline_executable_index(Some(target), caller),
+ },
bound_this: 0,
- bound_arguments: [0; 4],
+ bound_arguments_start: 0,
bound_argument_count: 0,
};
- if snapshot.forwarding == Forwarding::Bound {
- let bound = call.target()?.downcast::<BoundFunction>()?;
- let bound_argument_count = bound.bound_arguments_count();
- if bound_argument_count > snapshot.bound_arguments.len() {
- return None;
- }
+ // NB: Only calls of inlining candidates use the bound values.
+ if let Some(bound) = bound
+ && snapshot.inline_executable.is_some()
+ {
// NB: The bound function, which the code checks the callee against and so embeds, keeps its bound values
// alive.
+ let bound_arguments = bound.bound_arguments();
snapshot.bound_this = bound.bound_this().0;
- for index in 0..bound_argument_count {
- snapshot.bound_arguments[index] = bound.bound_argument(index).0;
- }
- snapshot.bound_argument_count = bound_argument_count as u8;
+ snapshot.bound_arguments_start = u32::try_from(self.bound_arguments.len()).ok()?;
+ snapshot.bound_argument_count = u32::try_from(bound_arguments.len()).ok()?;
+ self.bound_arguments.extend(bound_arguments.iter().map(|value| value.0));
}
- snapshot.inline_executable = self.inline_executable_index(Some(target), caller);
Some(snapshot)
}
@@ -943,6 +952,7 @@ impl SnapshotBuilder<'_> {
value: feedback.value.iter().map(core::cell::Cell::get).collect(),
call,
keyed,
+ bound_arguments: core::mem::take(&mut self.bound_arguments),
};
let construct_this_shape = entry
@@ -1232,6 +1242,7 @@ pub fn capture_snapshot(
compiled_function_fields,
closure_templates: Vec::new(),
environment_templates: Vec::new(),
+ bound_arguments: Vec::new(),
lexical_environment_templates: Vec::new(),
construct_this_shapes: HashMap::new(),
};
diff --git a/Libraries/LibJS/Rust/src/runtime/bound_function.rs b/Libraries/LibJS/Rust/src/runtime/bound_function.rs
index bda74eb77ae..93d65f25ff2 100644
--- a/Libraries/LibJS/Rust/src/runtime/bound_function.rs
+++ b/Libraries/LibJS/Rust/src/runtime/bound_function.rs
@@ -159,7 +159,7 @@ impl BoundFunction {
self.bound_arguments()[index]
}
- fn bound_arguments(&self) -> &[Value] {
+ pub fn bound_arguments(&self) -> &[Value] {
self.bound_arguments
.get()
.expect("a bound function has its bound arguments")
diff --git a/Tests/LibJS/JIT/programs/bound-calls-with-many-bound-arguments.js b/Tests/LibJS/JIT/programs/bound-calls-with-many-bound-arguments.js
new file mode 100644
index 00000000000..05063707a2b
--- /dev/null
+++ b/Tests/LibJS/JIT/programs/bound-calls-with-many-bound-arguments.js
@@ -0,0 +1,16 @@
+// Calls of bound functions with more bound arguments than the old limit of four are inlined, and calls of bound
+// functions with more bound arguments than the inlining limit are not: both must see every bound argument.
+function sum(...values) {
+ let total = 0;
+ for (const value of values) total += value;
+ return total + values.length * 1000;
+}
+const count = n => Array.from({ length: n }, (_, index) => index + 1);
+for (const n of [5, 16, 64, 65, 200]) {
+ const bound = sum.bind(null, ...count(n));
+ // NB: Each caller has its own executable, so its call site sees only one bound function.
+ const call = Function("bound", "return argument => bound(argument);")(bound);
+ let result = 0;
+ for (let k = 0; k < 200; k++) result = call(k);
+ print(n, result);
+}