Skip to content

Truly independent web browser

LibJS/JIT: Specialize gets of missing properties

Andreas Kling248b6f6292e9
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/caches.rs b/Libraries/LibJS/JIT/Rust/src/builder/caches.rs
index eae094e1a06..8ae7fa9ec9e 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/caches.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/caches.rs
@@ -62,6 +62,9 @@ pub(super) enum Action {
shape: CellId,
prototype_chain: Option<(CellId, bool)>,
},
+ /// Reads `undefined`: neither the object nor its prototype chain
+ /// (checked with the validity cell, if there is one) has the property.
+ GetMissing { prototype_chain: Option<(CellId, bool)> },
}
/// One cache entry: its guards and its action.
@@ -103,8 +106,7 @@ pub(super) struct CacheEntry {
impl CacheEntry {
/// The entry in its guard and action form, if compiled code can apply
- /// it: entries of missing properties, and entries without a shape, are
- /// left to the interpreter's code.
+ /// it: entries without a shape are left to the interpreter's code.
pub fn of(entry: &PropertyCacheEntrySnapshot) -> Option<CacheEntry> {
let prototype_holder = || match (entry.prototype, entry.prototype_chain_validity) {
(Some(prototype), Some(validity)) => Some(Holder::Prototype {
@@ -143,6 +145,14 @@ impl CacheEntry {
offset: entry.property_offset,
},
),
+ PropertyCacheEntryType::GetMissingProperty => (
+ entry.shape?,
+ Action::GetMissing {
+ prototype_chain: entry
+ .prototype_chain_validity
+ .map(|validity| (validity, entry.prototype_chain_valid)),
+ },
+ ),
PropertyCacheEntryType::AddOwnProperty => (
entry.shape?,
Action::Add {
@@ -181,6 +191,7 @@ impl CacheEntry {
pub fn is_data_access(&self) -> bool {
match self.action {
Action::Get { .. } => !self.holds_accessor,
+ Action::GetMissing { .. } => true,
Action::Set {
holder: Holder::Receiver,
..
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/property_access.rs b/Libraries/LibJS/JIT/Rust/src/builder/property_access.rs
index d2923ea0f6f..140cfc29ae7 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/property_access.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/property_access.rs
@@ -45,6 +45,7 @@ use crate::ir::Comparison;
use crate::ir::NodeId;
use crate::ir::Op;
use crate::ir::ShapeCheck;
+use crate::ir::value;
use crate::snapshot::CellId;
use crate::snapshot::Intrinsic;
use crate::snapshot::NativeCallTarget;
@@ -183,6 +184,13 @@ impl GraphBuilder<'_> {
/// Loads the property a `Get` entry describes, from the object itself or
/// from the prototype holding it.
pub(super) fn load_entry(&mut self, object: NodeId, entry: &CacheEntry) -> NodeId {
+ if let Action::GetMissing { prototype_chain } = entry.action {
+ self.emit_checked(Op::CheckPropertyAbsenceCacheable, vec![object], None);
+ if let Some((validity, valid)) = prototype_chain {
+ self.check_prototype_chain(validity, valid);
+ }
+ return self.constant(value::UNDEFINED);
+ }
let Action::Get { holder, offset } = entry.action else {
unreachable!("only gets load");
};
@@ -737,9 +745,9 @@ impl GraphBuilder<'_> {
) -> Result<bool, CompileFailure> {
// NB: Loads of properties that hold accessors exit, so these are left to the instruction's handling, which
// calls their getters, unless the getter is inlined (see `try_inline_getter()`).
- let speculatable = entries
- .iter()
- .all(|entry| matches!(entry.action, Action::Get { .. }) && entry.is_data_access());
+ let speculatable = entries.iter().all(|entry| {
+ matches!(entry.action, Action::Get { .. } | Action::GetMissing { .. }) && entry.is_data_access()
+ });
if !speculatable || !self.may_speculate(ExitKind::NotObject) || !self.may_speculate(ExitKind::BadShape) {
return Ok(false);
}
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/property_additions.rs b/Libraries/LibJS/JIT/Rust/src/builder/property_additions.rs
index 9554cee0a3f..c6f9c0971c2 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/property_additions.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/property_additions.rs
@@ -112,7 +112,7 @@ impl GraphBuilder<'_> {
Action::Set { offset, .. } => {
builder.store_named_holding(object, offset, value, entry.property_holds_data);
}
- Action::Get { .. } => unreachable!("additions only add and set"),
+ Action::Get { .. } | Action::GetMissing { .. } => unreachable!("additions only add and set"),
}
None
},
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/tests.rs b/Libraries/LibJS/JIT/Rust/src/builder/tests.rs
index 975bc36b4af..30ee9afbb29 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/tests.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/tests.rs
@@ -1606,13 +1606,16 @@ pub(crate) mod property_access {
Instruction::Return { value: r(7) },
]
});
- let missing = own(PropertyCacheEntryType::GetMissingProperty, 0x100, 1);
+ let shapeless = PropertyCacheEntrySnapshot {
+ shape: None,
+ ..own(GetOwnProperty, 0x100, 1)
+ };
let megamorphic = PropertyCacheSnapshot {
kind: PropertyCacheKind::Megamorphic,
entries: vec![own(GetOwnProperty, 0x100, 1)],
};
let caches = vec![
- cache(vec![missing]),
+ cache(vec![shapeless]),
megamorphic,
cache(vec![own(GetOwnProperty, 0x100, 1)]),
cache(vec![own(PropertyCacheEntryType::AddOwnProperty, 0x100, 1)]),
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/lowering_info.rs b/Libraries/LibJS/JIT/Rust/src/codegen/lowering_info.rs
index d81c169b47c..c0499016332 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/lowering_info.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/lowering_info.rs
@@ -299,6 +299,7 @@ pub fn lowering_info(node: &Node) -> LoweringInfo {
Op::Int32Compare { .. } | Op::TaggedEquals { .. } => plain.with_immediate(1),
// The object's address.
Op::CheckElements { .. } | Op::CheckTypedArray | Op::CheckArray => plain.with_temps(1),
+ Op::CheckPropertyAbsenceCacheable => plain.with_temps(2),
Op::CheckNonNegativeInt32 | Op::LoadTypedArrayLength | Op::LoadTypedArrayDataOffset => plain,
Op::TypedArrayDataAddress => LoweringInfo {
reuses_first_input: true,
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/mod.rs b/Libraries/LibJS/JIT/Rust/src/codegen/mod.rs
index 120e0d1f4ad..baee49a3916 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/mod.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/mod.rs
@@ -1543,6 +1543,14 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
Op::CheckElements { kind } => self.emit_check_elements(node_id, *kind),
Op::CheckTypedArray => self.emit_check_typed_array(node_id),
Op::CheckArray => self.emit_check_array(node_id),
+ Op::CheckPropertyAbsenceCacheable => {
+ let exit = self.exit_site(node_id);
+ let (value, object, class) = (self.input(node_id, 0), self.temp(node_id, 0), self.temp(node_id, 1));
+ let scratch = self.pinned.scratch;
+ self.branch_on_tag(Condition::NotEqual, value, value::OBJECT_TAG, scratch, exit);
+ self.emit_unbox_cell(object, value);
+ self.branch_unless_cacheable_for_property_absence(object, class, exit);
+ }
Op::CheckNonNegativeInt32 => {
let exit = self.exit_site(node_id);
self.masm
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/probes/property.rs b/Libraries/LibJS/JIT/Rust/src/codegen/probes/property.rs
index cf268bf0d7e..cb9c85d2f78 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/probes/property.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/probes/property.rs
@@ -250,14 +250,11 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
/// Branches to `miss` unless a `GetMissingProperty` entry for the shape
/// of `object`, whose prototype chain validity is at `validity`, applies
- /// to it, like `get_with_property_lookup_cache()`: only objects whose
- /// class keeps the default
- /// `ObjectMethods::is_cacheable_for_property_absence` have no other
- /// properties than those of their shape (for the names their entries can
- /// be for), and the property must also be missing from an unchanged
- /// prototype chain. Plain objects, ECMAScript functions, arrays and
- /// String objects are told apart without loading the hook. Clobbers
- /// `shape`.
+ /// to it, like `get_with_property_lookup_cache()`: the object must have
+ /// no other properties than those of its shape (see
+ /// `branch_unless_cacheable_for_property_absence()`), and the property
+ /// must also be missing from an unchanged prototype chain. Clobbers
+ /// `shape` and the scratch register.
pub(super) fn branch_unless_property_absence_applies(
&mut self,
object: Gpr,
@@ -265,6 +262,40 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
validity: &Address,
miss: Label,
) {
+ self.branch_unless_cacheable_for_property_absence(object, shape, miss);
+ let offsets = self.runtime.offsets;
+ let layout = self.runtime.layout;
+ let (has_chain, done) = (self.masm.new_label(), self.masm.new_label());
+ self.masm.load64(shape, validity);
+ self.masm.branch_test64(Condition::NonZero, shape, u64::MAX, has_chain);
+ // Entries without a prototype chain validity are for shapes without
+ // a prototype.
+ self.emit_load_compressed_cell(shape, &Address::new(object, offsets.object_shape as i32));
+ self.masm.branch32_memory_imm(
+ Condition::NotEqual,
+ &Address::new(shape, layout.shape_prototype as i32),
+ 0,
+ miss,
+ );
+ self.masm.jump(done);
+ self.masm.bind(has_chain);
+ self.branch_unless_prototype_chain_valid(shape, miss);
+ self.masm.bind(done);
+ }
+
+ /// Branches to `miss` unless the object `object` has no other properties
+ /// than those of its shape, for the names `GetMissingProperty` entries
+ /// can be for: only objects whose class keeps the default
+ /// `ObjectMethods::is_cacheable_for_property_absence` do. Plain objects,
+ /// ECMAScript functions, arrays and String objects are told apart
+ /// without loading the hook. Clobbers `class` and the scratch register.
+ pub(in crate::codegen) fn branch_unless_cacheable_for_property_absence(
+ &mut self,
+ object: Gpr,
+ class: Gpr,
+ miss: Label,
+ ) {
+ let shape = class;
let scratch = self.pinned.scratch;
let layout = self.runtime.layout;
let offsets = self.runtime.offsets;
@@ -305,22 +336,6 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
);
}
self.masm.bind(cacheable);
- let (has_chain, done) = (self.masm.new_label(), self.masm.new_label());
- self.masm.load64(shape, validity);
- self.masm.branch_test64(Condition::NonZero, shape, u64::MAX, has_chain);
- // Entries without a prototype chain validity are for shapes without
- // a prototype.
- self.emit_load_compressed_cell(shape, &Address::new(object, offsets.object_shape as i32));
- self.masm.branch32_memory_imm(
- Condition::NotEqual,
- &Address::new(shape, layout.shape_prototype as i32),
- 0,
- miss,
- );
- self.masm.jump(done);
- self.masm.bind(has_chain);
- self.branch_unless_prototype_chain_valid(shape, miss);
- self.masm.bind(done);
}
/// A `ProbePropertyCache` node: the data property, or `undefined` for a
diff --git a/Libraries/LibJS/JIT/Rust/src/ir/dump.rs b/Libraries/LibJS/JIT/Rust/src/ir/dump.rs
index 07dd5e72fa2..620263f1cd5 100644
--- a/Libraries/LibJS/JIT/Rust/src/ir/dump.rs
+++ b/Libraries/LibJS/JIT/Rust/src/ir/dump.rs
@@ -349,6 +349,7 @@ fn op_text(layout: &FrameLayout, op: &Op, inputs: &str) -> String {
| Op::CheckInitialized
| Op::CheckTypedArray
| Op::CheckArray
+ | Op::CheckPropertyAbsenceCacheable
| Op::CheckNonNegativeInt32
| Op::LoadTypedArrayLength
| Op::LoadTypedArrayDataOffset
diff --git a/Libraries/LibJS/JIT/Rust/src/ir/info.rs b/Libraries/LibJS/JIT/Rust/src/ir/info.rs
index 0e9e025faa9..56a4c09619d 100644
--- a/Libraries/LibJS/JIT/Rust/src/ir/info.rs
+++ b/Libraries/LibJS/JIT/Rust/src/ir/info.rs
@@ -338,6 +338,7 @@ impl Op {
}
Op::CheckTypedArray => OpInfo::check(ExitKind::BadElements, I::TAGGED),
Op::CheckArray => OpInfo::check(ExitKind::BadType, I::TAGGED),
+ Op::CheckPropertyAbsenceCacheable => OpInfo::check(ExitKind::BadShape, I::TAGGED),
Op::CheckNonNegativeInt32 => OpInfo::check(ExitKind::Overflow, I::INT32),
Op::LoadTypedArrayLength | Op::LoadTypedArrayDataOffset => {
OpInfo::pure(I::POINTER).with_properties(P::reading(Locations::ELEMENT_COUNTS))
diff --git a/Libraries/LibJS/JIT/Rust/src/ir/mod.rs b/Libraries/LibJS/JIT/Rust/src/ir/mod.rs
index 1de5ad0ed47..ee020bbe118 100644
--- a/Libraries/LibJS/JIT/Rust/src/ir/mod.rs
+++ b/Libraries/LibJS/JIT/Rust/src/ir/mod.rs
@@ -472,7 +472,8 @@ define_ops! {
/// `CheckShape`, `CheckValue`, `CheckClosure`, `CheckAccessorFunction`,
/// `CheckElements`, `CheckTypedArray`, `CheckArray`, `CheckNonNegativeInt32`,
/// `CheckIdentityComparable`, `CheckAppendableArray`, `CheckBounds`,
- /// `CheckTypedArrayStorage`, `CheckNotHole`, `CheckInitialized`), and a
+ /// `CheckTypedArrayStorage`, `CheckNotHole`, `CheckInitialized`,
+ /// `CheckPropertyAbsenceCacheable`), and a
/// `Refine` on the edge of a branch, has that input as its value, refined:
/// known to pass. Nodes that rely on the check take the refined value as their
/// input instead of the input itself, so that the check is a data dependency
@@ -714,6 +715,12 @@ define_ops! {
/// Exits with `ExitKind::BadType` unless input 0 is an object with
/// the magical length property of an array.
CheckArray,
+ /// Exits with `ExitKind::BadShape` unless the object in input 0 has no
+ /// other properties than those of its shape, for the names a
+ /// `GetMissingProperty` cache entry can be for, like the interpreter
+ /// checks before it relies on such an entry (see
+ /// `Object::is_cacheable_for_property_absence()` in the runtime).
+ CheckPropertyAbsenceCacheable,
/// The `Repr::Int32` input 0, checked to be nonnegative. Exits with
/// `ExitKind::Overflow` for unsigned counts above i32::MAX.
CheckNonNegativeInt32,
diff --git a/Libraries/LibJS/Rust/src/jit/snapshot.rs b/Libraries/LibJS/Rust/src/jit/snapshot.rs
index 26c664d87c6..4ea3fd487d3 100644
--- a/Libraries/LibJS/Rust/src/jit/snapshot.rs
+++ b/Libraries/LibJS/Rust/src/jit/snapshot.rs
@@ -467,6 +467,15 @@ impl SnapshotBuilder<'_> {
PropertyLookupCacheEntryType::GetPropertyInPrototypeChain => {
PropertyCacheEntryType::GetPropertyInPrototypeChain
}
+ // NB: Like the interpreter, compiled code relies on an entry for a missing property only where the
+ // property is also missing from an unchanged prototype chain, which an entry without a prototype
+ // chain validity can only tell for shapes without a prototype.
+ PropertyLookupCacheEntryType::GetMissingProperty
+ if entry.prototype_chain_validity.is_none()
+ && shape.is_some_and(|shape| shape.prototype().is_some()) =>
+ {
+ continue;
+ }
PropertyLookupCacheEntryType::GetMissingProperty => PropertyCacheEntryType::GetMissingProperty,
};
let prototype_property = prototype_property_object(&entry);
diff --git a/Tests/LibJS/JIT/ir/missing-properties.js b/Tests/LibJS/JIT/ir/missing-properties.js
new file mode 100644
index 00000000000..0129c9d212b
--- /dev/null
+++ b/Tests/LibJS/JIT/ir/missing-properties.js
@@ -0,0 +1,18 @@
+// A get of a property that objects of the shapes the cache saw do not have
+// is undefined after the shape check, a check that the object has no other
+// properties than those of its shape, and the check of its prototype chain,
+// without a probe of the cache.
+
+function onClick(o) {
+ return o.onClick;
+}
+jit.prepare(onClick);
+for (let i = 0; i < 20; ++i) onClick(i % 2 ? { x: i } : { y: i, x: i });
+jit.compile(onClick);
+
+// CHECK-FUNCTION: onClick
+// CHECK-PASS: graph building
+// CHECK: CheckShape
+// CHECK: CheckPropertyAbsenceCacheable
+// CHECK-NOT: ProbePropertyCache
+// CHECK: Return
diff --git a/Tests/LibJS/JIT/programs/gets-of-missing-properties.js b/Tests/LibJS/JIT/programs/gets-of-missing-properties.js
new file mode 100644
index 00000000000..17efdbe8ffb
--- /dev/null
+++ b/Tests/LibJS/JIT/programs/gets-of-missing-properties.js
@@ -0,0 +1,41 @@
+// Gets of properties that objects of a few shapes do not have give undefined, until the property appears on the
+// objects, their prototypes or a prototype that replaces theirs, or the objects are of a class that has properties
+// outside its shape.
+const proto = {};
+function make(i) {
+ switch (i % 4) {
+ case 0:
+ return Object.create(proto);
+ case 1: {
+ const o = Object.create(proto);
+ o.a = i;
+ return o;
+ }
+ case 2:
+ return [i];
+ default:
+ return new String("s" + i);
+ }
+}
+
+function read(o) {
+ return o.missing === undefined ? "u" : String(o.missing);
+}
+
+const objects = [];
+for (let i = 0; i < 8; i++) objects.push(make(i));
+const out = [];
+for (let round = 0; round < 400; round++) {
+ if (round === 100) proto.missing = "proto";
+ if (round === 150) delete proto.missing;
+ if (round === 200) objects[1].missing = "own";
+ if (round === 250) Object.setPrototypeOf(objects[4], { missing: "replaced" });
+ if (round === 300) Array.prototype.missing = "array";
+ if (round === 350) Object.defineProperty(Object.prototype, "missing", { get: () => "getter", configurable: true });
+ let s = "";
+ for (const o of objects) s += read(o);
+ out.push(s);
+}
+delete Array.prototype.missing;
+delete Object.prototype.missing;
+print(out[0], out[120], out[180], out[220], out[260], out[320], out[399]);