Truly independent web browser
LibJS: Inline JIT callees that access super properties
Andreas Kling0cd899ae37e2
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs b/Libraries/LibJS/JIT/Rust/src/builder/inlining.rsindex 993c1dc46fe..5570eed741e 100644--- a/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs+++ b/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs@@ -150,12 +150,18 @@ fn can_inline(callee: &Function<'_>, virtual_arguments: bool) -> bool {// NB: These read the function, the this value or the new target of the running frame, which is the// caller's while an inlined callee runs (see `function_with_frame_as_this_environment()` in the// runtime).- | OpCode::ResolveSuperBase| OpCode::GetNewTarget| OpCode::GetSuperConstructor| OpCode::SuperCallWithArgumentArray);- !reads_frame_fields && !matches!(handling(opcode), Handling::Unsupported(_))+ // NB: The base of `super` property access is built from the callee's home object (see+ // `try_build_resolve_super_base()`).+ let unknown_super_base = opcode == OpCode::ResolveSuperBase+ && callee+ .executable+ .function+ .is_none_or(|function| function.super_base.is_none());+ !reads_frame_fields && !unknown_super_base && !matches!(handling(opcode), Handling::Unsupported(_))})}@@ -1352,6 +1358,43 @@ impl GraphBuilder<'_> {self.inline_call(call)}+ /// `ResolveSuperBase` in an inlined callee, whose home object is known: its prototype, which stays the same while+ /// its shape does. The interpreter's slow path reads the running function, which is the caller's here.+ pub(super) fn try_build_resolve_super_base(&mut self, dst: Operand) -> Result<bool, CompileFailure> {+ if !self.function.is_virtual() {+ return Ok(false);+ }+ let super_base = self+ .function+ .executable+ .function+ .and_then(|function| function.super_base)+ .ok_or(CompileFailure::InvalidBytecode {+ pc: self.function.pc,+ reason: "inlined ResolveSuperBase without a known home object",+ })?;+ self.embed(super_base.home);+ let home = self.boxed_object(super_base.home);+ let home = self.constant(home);+ self.check_shapes(+ home,+ &[ShapeCheck {+ shape: super_base.home_shape,+ dictionary_generation: None,+ }],+ );+ let base = match super_base.prototype {+ Some(prototype) => {+ self.embed(prototype);+ self.boxed_object(prototype)+ }+ None => value::NULL,+ };+ let base = self.constant(base);+ self.write(dst, base)?;+ Ok(true)+ }+/// `this` of an inlined construct of `function`: the object its/// construct creates, an empty plain object of the prototype in its/// "prototype", which must still be the one it was at compile time.@@ -1499,6 +1542,15 @@ impl GraphBuilder<'_> {if !can_inline(&callee_function, virtual_arguments) {return Ok(None);}+ // NB: Closures of a function share its code but may have other home objects, and the base of `super` property+ // access is guarded by a shape check.+ let resolves_super_base = callee_function+ .instructions+ .iter()+ .any(|instruction| instruction.instruction.opcode() == OpCode::ResolveSuperBase);+ if resolves_super_base && (closure.is_some() || !self.may_speculate(ExitKind::BadShape)) {+ return Ok(None);+ }if construct.is_some() && !returns_no_object(&callee_function) {return Ok(None);}diff --git a/Libraries/LibJS/JIT/Rust/src/builder/mod.rs b/Libraries/LibJS/JIT/Rust/src/builder/mod.rsindex ed07e9beb8d..8cf6f71b7b1 100644--- a/Libraries/LibJS/JIT/Rust/src/builder/mod.rs+++ b/Libraries/LibJS/JIT/Rust/src/builder/mod.rs@@ -1642,6 +1642,11 @@ impl<'a> GraphBuilder<'a> {return Ok(None);}}+ Instruction::ResolveSuperBase { dst } => {+ if !self.try_build_resolve_super_base(dst)? {+ return Ok(None);+ }+ }Instruction::CacheObjectShape { object, cache } => {if !self.try_build_cache_object_shape(object, cache)? {return Ok(None);diff --git a/Libraries/LibJS/JIT/Rust/src/builder/tests.rs b/Libraries/LibJS/JIT/Rust/src/builder/tests.rsindex 799b8a7d6c6..56fa148bfc3 100644--- a/Libraries/LibJS/JIT/Rust/src/builder/tests.rs+++ b/Libraries/LibJS/JIT/Rust/src/builder/tests.rs@@ -1627,6 +1627,7 @@ pub(crate) mod inlining {pub(crate) fn function(strict: bool, uses_this: bool) -> InlinedFunctionSnapshot {InlinedFunctionSnapshot {+ super_base: None,function: CellId(CALLEE_FUNCTION),formal_parameter_count: 1,strict,diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/tests.rs b/Libraries/LibJS/JIT/Rust/src/codegen/tests.rsindex 8f34c53adcc..21955215c0c 100644--- a/Libraries/LibJS/JIT/Rust/src/codegen/tests.rs+++ b/Libraries/LibJS/JIT/Rust/src/codegen/tests.rs@@ -3514,6 +3514,7 @@ mod direct_calls {fn target(&self, strict: bool, uses_this: bool) -> DirectCallTarget {DirectCallTarget {function: InlinedFunctionSnapshot {+ super_base: None,function: CellId(self.function.as_ptr() as u64),formal_parameter_count: 3,strict,@@ -4530,6 +4531,7 @@ fn aarch64_direct_calls_disassemble() {snapshot.runtime = runtime();let target = DirectCallTarget {function: InlinedFunctionSnapshot {+ super_base: None,function: CellId(0x5000),formal_parameter_count: 2,strict: false,diff --git a/Libraries/LibJS/JIT/Rust/src/snapshot.rs b/Libraries/LibJS/JIT/Rust/src/snapshot.rsindex d4f592004fe..d670b95ac00 100644--- a/Libraries/LibJS/JIT/Rust/src/snapshot.rs+++ b/Libraries/LibJS/JIT/Rust/src/snapshot.rs@@ -292,6 +292,20 @@ pub struct InlinedFunctionSnapshot {/// The function's `SharedFunctionInstanceData`, which all closures of/// the same function share.pub shared_data: CellId,+ /// What `super` property access in the function resolves its base to,+ /// if JIT code can tell (see `SuperBaseSnapshot`).+ pub super_base: Option<SuperBaseSnapshot>,+}++/// The [[HomeObject]] of a method, an ordinary object with a shape that is+/// not a dictionary, so that its prototype, the base of `super` property+/// access in the method, stays `prototype` while it has `home_shape`.+#[derive(Debug, Clone, Copy, PartialEq, Eq)]+pub struct SuperBaseSnapshot {+ pub home: CellId,+ pub home_shape: CellId,+ /// The prototype, or None for null.+ pub prototype: Option<CellId>,}// NB: The bits of the interpreter's feedback below are those ofdiff --git a/Libraries/LibJS/Rust/src/jit/snapshot.rs b/Libraries/LibJS/Rust/src/jit/snapshot.rsindex 8d837a349de..30008d3beaa 100644--- a/Libraries/LibJS/Rust/src/jit/snapshot.rs+++ b/Libraries/LibJS/Rust/src/jit/snapshot.rs@@ -20,7 +20,7 @@ use libjs_jit::snapshot::{InlinedFunctionSnapshot, InliningLimits, Intrinsic, KeyedFeedbackSnapshot, LexicalEnvironmentTemplateSnapshot,NativeCallTarget, ObjectShapeCacheSnapshot, OrdinaryHasInstanceSnapshot, PropertyCacheEntrySnapshot,PropertyCacheEntryType, PropertyCacheKind, PropertyCacheSnapshot, ShapeSnapshot, Snapshot, StressOptions,- TypedArraySnapshot,+ SuperBaseSnapshot, TypedArraySnapshot,};use super::allocation::{@@ -660,9 +660,24 @@ impl SnapshotBuilder<'_> {global_this: self.cell(realm.global_environment().global_this_value()),realm: self.cell(realm),shared_data: self.cell(function.shared_data()),+ super_base: self.super_base(function),}}+ /// The base of `super` property access in `function`, if its home object gets its prototype from its shape.+ fn super_base(&mut self, function: Gc<EcmascriptFunctionObject>) -> Option<SuperBaseSnapshot> {+ let home = function.home_object()?;+ let home_shape = home.shape();+ if home_shape.is_dictionary() || !home.has_ordinary_get_prototype_of() {+ return None;+ }+ Some(SuperBaseSnapshot {+ home: self.cell(home),+ home_shape: self.cell(home_shape),+ prototype: home_shape.prototype().map(|prototype| self.cell(prototype)),+ })+ }+fn inlined_builtin(&mut self, function: Gc<NativeJavaScriptBackedFunction>) -> InlinedFunctionSnapshot {let realm = function.realm();let shared_data = function.shared_data();@@ -674,6 +689,7 @@ impl SnapshotBuilder<'_> {global_this: self.cell(realm.global_environment().global_this_value()),realm: self.cell(realm),shared_data: self.cell(shared_data),+ super_base: None,}}diff --git a/Libraries/LibJS/Rust/src/runtime/object.rs b/Libraries/LibJS/Rust/src/runtime/object.rsindex 651bf3ce469..3bd9e35c2ad 100644--- a/Libraries/LibJS/Rust/src/runtime/object.rs+++ b/Libraries/LibJS/Rust/src/runtime/object.rs@@ -1910,6 +1910,11 @@ impl Object {// The internal methods, which dispatch through the object's class.+ /// Whether [[GetPrototypeOf]] of the object is OrdinaryGetPrototypeOf(), which reads the prototype of its shape.+ pub fn has_ordinary_get_prototype_of(&self) -> bool {+ self.methods().internal_get_prototype_of.is_none()+ }+pub fn internal_get_prototype_of(&self, vm: &Vm) -> ThrowCompletionOr<Option<Gc<Object>>> {match self.methods().internal_get_prototype_of {Some(internal_get_prototype_of) => internal_get_prototype_of(self, vm),