Skip to content

Truly independent web browser

LibJS/JIT: Read missing properties from the VM's keyed lookup cache

Andreas Klingf9ff567ecf72
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/probes/keyed.rs b/Libraries/LibJS/JIT/Rust/src/codegen/probes/keyed.rs
index 22cc6444af3..a378c73c7e0 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/probes/keyed.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/probes/keyed.rs
@@ -30,9 +30,10 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
/// Looks up the own data property that the string `key` names on the
/// object in `base` in the VM's keyed property lookup cache, like the
/// runtime's keyed get does when the access's own cache has nothing for
- /// it (see `RuntimeLayout::keyed_lookup_cache_entries`): puts it in
- /// `output` and jumps to `done`, or jumps to `miss`. Clobbers the cache
- /// registers and the scratch register.
+ /// it (see `RuntimeLayout::keyed_lookup_cache_entries`): puts it, or
+ /// `undefined` where the entry says the object and its prototype chain
+ /// have no such property, in `output` and jumps to `done`, or jumps to
+ /// `miss`. Clobbers the cache registers and the scratch register.
fn emit_keyed_lookup_cache_get(
&mut self,
base: Gpr,
@@ -42,13 +43,41 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
miss: Label,
done: Label,
) {
- let entries = self.runtime.layout.keyed_lookup_cache_entries;
- let entry_type = self.runtime.layout.property_lookup_cache_entry_type_get_own_property;
- let Some(property) = self.emit_vm_keyed_cache_probe(base, key, &registers, entries, entry_type, miss) else {
+ let layout = self.runtime.layout;
+ let entry_type = layout.property_lookup_cache_entry_type_get_own_property;
+ let missing = (layout.keyed_lookup_cache_prototype_chain_validity != 0).then(|| self.masm.new_label());
+ let Some(property) = self.emit_vm_keyed_cache_probe(
+ base,
+ key,
+ &registers,
+ layout.keyed_lookup_cache_entries,
+ entry_type,
+ miss,
+ missing.unwrap_or(miss),
+ ) else {
return;
};
- let CacheRegisters { entry, .. } = registers;
+ let CacheRegisters { object, shape, entry } = registers;
let scratch = self.pinned.scratch;
+ if let Some(missing) = missing {
+ let own = self.masm.new_label();
+ self.masm.jump(own);
+ // NB: The cache entry is in the object register (see `emit_vm_keyed_cache_entry()`).
+ self.masm.bind(missing);
+ self.masm.branch32_memory_imm(
+ Condition::NotEqual,
+ &Address::new(object, layout.keyed_lookup_cache_entry_type as i32),
+ layout.property_lookup_cache_entry_type_get_missing_property as i32,
+ miss,
+ );
+ self.masm.move64(entry, object);
+ self.emit_unbox_cell(object, base);
+ let validity = Address::new(entry, layout.keyed_lookup_cache_prototype_chain_validity as i32);
+ self.branch_unless_property_absence_applies(object, shape, &validity, miss);
+ self.masm.move_imm64(output, value::UNDEFINED);
+ self.masm.jump(done);
+ self.masm.bind(own);
+ }
self.masm.load64(entry, &property);
self.branch_on_tag(Condition::Equal, entry, value::ACCESSOR_TAG, scratch, miss);
self.masm.move64(output, entry);
@@ -71,7 +100,8 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
) {
let entries = self.runtime.layout.keyed_store_cache_entries;
let entry_type = self.runtime.layout.property_lookup_cache_entry_type_change_own_property;
- let Some(property) = self.emit_vm_keyed_cache_probe(base, key, &registers, entries, entry_type, miss) else {
+ let Some(property) = self.emit_vm_keyed_cache_probe(base, key, &registers, entries, entry_type, miss, miss)
+ else {
return;
};
let CacheRegisters { entry, .. } = registers;
@@ -85,10 +115,13 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
/// `base` in a VM-wide keyed cache with `entries` (laid out like the
/// keyed property lookup cache's), with `entry_type` and the shape's
/// dictionary generation, and returns the address of the named property
- /// at its offset in the object. Branches to `miss` otherwise, and
- /// returns nothing (having jumped to `miss`) if `entries` is 0. Clobbers
- /// the cache registers and the scratch register; the address uses the
- /// shape register and the scratch register.
+ /// at its offset in the object. Branches to `other_type`, with the entry
+ /// in the object register, where the entry has another type, and to
+ /// `miss` where there is none, and returns nothing (having jumped to
+ /// `miss`) if `entries` is 0. Clobbers the cache registers and the
+ /// scratch register; the address uses the shape register and the scratch
+ /// register.
+ #[allow(clippy::too_many_arguments)]
fn emit_vm_keyed_cache_probe(
&mut self,
base: Gpr,
@@ -97,6 +130,7 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
entries: u64,
entry_type: u32,
miss: Label,
+ other_type: Label,
) -> Option<Address> {
if !self.emit_vm_keyed_cache_entry(base, key, registers, entries, miss) {
return None;
@@ -109,7 +143,7 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
Condition::NotEqual,
&Address::new(cache_entry, layout.keyed_lookup_cache_entry_type as i32),
entry_type as i32,
- miss,
+ other_type,
);
// The own property at the entry's offset.
self.masm.load32(
@@ -379,7 +413,11 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
self.masm.jump(done);
self.masm.bind(missing);
- self.branch_unless_property_absence_applies(object, shape, entry, fail);
+ let validity = Address::new(
+ entry,
+ layout.property_lookup_cache_entry_prototype_chain_validity as i32,
+ );
+ self.branch_unless_property_absence_applies(object, shape, &validity, fail);
self.masm.move_imm64(output, value::UNDEFINED);
self.masm.jump(done);
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/probes/property.rs b/Libraries/LibJS/JIT/Rust/src/codegen/probes/property.rs
index a6ded4d8de0..9e10dd4f1f2 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/probes/property.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/probes/property.rs
@@ -208,16 +208,23 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
Address::indexed(registers.shape, scratch, Scale::Eight, 0)
}
- /// Branches to `miss` unless the `GetMissingProperty` entry in `entry`
- /// for the shape of `object` 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
+ /// 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`.
- pub(super) fn branch_unless_property_absence_applies(&mut self, object: Gpr, shape: Gpr, entry: Gpr, miss: Label) {
+ pub(super) fn branch_unless_property_absence_applies(
+ &mut self,
+ object: Gpr,
+ shape: Gpr,
+ validity: &Address,
+ miss: Label,
+ ) {
let scratch = self.pinned.scratch;
let layout = self.runtime.layout;
let offsets = self.runtime.offsets;
@@ -262,13 +269,7 @@ 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,
- &Address::new(
- entry,
- layout.property_lookup_cache_entry_prototype_chain_validity as i32,
- ),
- );
+ 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.
@@ -355,7 +356,11 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
layout.property_lookup_cache_entry_type_get_missing_property as i32,
present,
);
- self.branch_unless_property_absence_applies(object, shape, entry, probe);
+ let validity = Address::new(
+ entry,
+ layout.property_lookup_cache_entry_prototype_chain_validity as i32,
+ );
+ self.branch_unless_property_absence_applies(object, shape, &validity, probe);
self.masm.move_imm64(output, value::UNDEFINED);
self.masm.jump(done);
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs b/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs
index fcb7f279752..1baddd581a1 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs
@@ -1320,6 +1320,7 @@ fn fake_object_layout() -> RuntimeLayout {
keyed_lookup_cache_dictionary_generation: 8,
keyed_lookup_cache_shape: 16,
keyed_lookup_cache_name: 40,
+ keyed_lookup_cache_prototype_chain_validity: 0,
property_lookup_cache_polymorphic_tag: 1,
property_lookup_cache_polymorphic_entry_count: 4,
property_lookup_cache_entry_size: 64,
diff --git a/Libraries/LibJS/JIT/Rust/src/snapshot.rs b/Libraries/LibJS/JIT/Rust/src/snapshot.rs
index d8f87ac0813..970b3247a61 100644
--- a/Libraries/LibJS/JIT/Rust/src/snapshot.rs
+++ b/Libraries/LibJS/JIT/Rust/src/snapshot.rs
@@ -1870,6 +1870,11 @@ pub struct RuntimeLayout {
pub keyed_lookup_cache_dictionary_generation: u32,
pub keyed_lookup_cache_shape: u32,
pub keyed_lookup_cache_name: u32,
+ /// The offset in an entry of the prototype chain validity (a cell
+ /// pointer, or null for shapes without a prototype) that a
+ /// `GetMissingProperty` entry relies on, or 0 if JIT code does not use
+ /// such entries.
+ pub keyed_lookup_cache_prototype_chain_validity: u32,
/// The tag bits of `PropertyLookupCache::data` of a polymorphic cache,
/// whose entries follow its most recently used one.
pub property_lookup_cache_polymorphic_tag: u64,
diff --git a/Libraries/LibJS/Rust/src/bytecode/executable.rs b/Libraries/LibJS/Rust/src/bytecode/executable.rs
index 55acf4bbd25..543dbcfdf5f 100644
--- a/Libraries/LibJS/Rust/src/bytecode/executable.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/executable.rs
@@ -911,6 +911,7 @@ pub struct KeyedPropertyLookupCacheLayout {
pub shape_dictionary_generation: u32,
pub shape: u32,
pub property_name: u32,
+ pub prototype_chain_validity: u32,
}
/// The VM-wide cache of string-keyed GetByValue lookups. Lookups are copied out, so that no entry is borrowed while
@@ -951,6 +952,7 @@ impl KeyedPropertyLookupCache {
shape_dictionary_generation: (lookup + offset_of!(KeyedPropertyLookup, shape_dictionary_generation)) as u32,
shape: (lookup + offset_of!(KeyedPropertyLookup, shape)) as u32,
property_name: offset_of!(KeyedPropertyLookupCacheEntry, property_name) as u32,
+ prototype_chain_validity: (lookup + offset_of!(KeyedPropertyLookup, prototype_chain_validity)) as u32,
}
}
diff --git a/Libraries/LibJS/Rust/src/jit/runtime_info.rs b/Libraries/LibJS/Rust/src/jit/runtime_info.rs
index 55e3c58cd73..37d95c796bb 100644
--- a/Libraries/LibJS/Rust/src/jit/runtime_info.rs
+++ b/Libraries/LibJS/Rust/src/jit/runtime_info.rs
@@ -368,6 +368,7 @@ fn runtime_layout() -> RuntimeLayout {
keyed_lookup_cache_dictionary_generation: 0,
keyed_lookup_cache_shape: 0,
keyed_lookup_cache_name: 0,
+ keyed_lookup_cache_prototype_chain_validity: 0,
}
}
@@ -456,6 +457,7 @@ pub fn runtime_info(vm: &Vm, realm: Gc<Realm>, allocation: AllocationInfos) -> R
layout.keyed_lookup_cache_dictionary_generation = keyed_lookup_cache.shape_dictionary_generation;
layout.keyed_lookup_cache_shape = keyed_lookup_cache.shape;
layout.keyed_lookup_cache_name = keyed_lookup_cache.property_name;
+ layout.keyed_lookup_cache_prototype_chain_validity = keyed_lookup_cache.prototype_chain_validity;
layout.single_ascii_character_strings = vm.single_ascii_character_strings_address();
layout.fly_string_cache = vm.fly_string_cache().as_ptr().expose_provenance() as u64;
layout.numeric_string_cache = vm.numeric_string_cache().as_ptr().expose_provenance() as u64;
diff --git a/Tests/LibJS/Runtime/jit/vm-keyed-cache-entries.js b/Tests/LibJS/Runtime/jit/vm-keyed-cache-entries.js
new file mode 100644
index 00000000000..8b70ce4d00c
--- /dev/null
+++ b/Tests/LibJS/Runtime/jit/vm-keyed-cache-entries.js
@@ -0,0 +1,85 @@
+// JIT code at keyed accesses that saw too many (shape, key) pairs for their own caches looks string keys up in the
+// VM's keyed lookup cache, like the interpreter, where missing properties read undefined. The functions are compiled
+// once their sites have given up on their own caches.
+
+const keys = [];
+for (let i = 0; i < 600; ++i) keys.push(`key${i}`);
+
+function compiledAfter(parameters, body, warmUp) {
+ const fn = new Function(...parameters, `return ${body};`);
+ jit.prepare(fn);
+ warmUp(fn);
+ jit.compile(fn);
+ return fn;
+}
+
+describe("keyed accesses through the VM's lookup cache in JIT code", () => {
+ test("missing and present properties of many shapes", () => {
+ // Objects with every even or every odd key, and one key of their own, so that the VM's lookup cache holds
+ // entries for missing and for present properties of many shapes, which also share entries.
+ const objects = [];
+ for (let i = 0; i < 8; ++i) {
+ const object = {};
+ object[`own${i}`] = i;
+ for (let k = i % 2; k < keys.length; k += 2) object[keys[k]] = k;
+ objects.push(object);
+ }
+ const expected = (object, k) => (k % 2 === objects.indexOf(object) % 2 ? k : undefined);
+ const get = compiledAfter(["object", "key"], "object[key]", get => {
+ for (const object of objects) for (const key of keys) get(object, key);
+ });
+ for (let round = 0; round < 3; ++round) {
+ for (const object of objects) {
+ for (let k = 0; k < keys.length; ++k) expect(get(object, keys[k])).toBe(expected(object, k));
+ }
+ }
+ });
+
+ test("properties that the prototype chain gains later", () => {
+ const prototype = {};
+ const objects = [];
+ for (let i = 0; i < 40; ++i) {
+ const object = Object.create(prototype);
+ object[`own${i}`] = i;
+ objects.push(object);
+ }
+ const get = compiledAfter(["object", "key"], "object[key]", get => {
+ for (const object of objects) for (const key of keys) get(object, key);
+ });
+ for (const object of objects) expect(get(object, "key3")).toBeUndefined();
+ prototype.key3 = "inherited";
+ for (const object of objects) expect(get(object, "key3")).toBe("inherited");
+ Object.prototype.key4 = "very inherited";
+ try {
+ for (const object of objects) expect(get(object, "key4")).toBe("very inherited");
+ } finally {
+ delete Object.prototype.key4;
+ }
+ for (const object of objects) expect(get(object, "key4")).toBeUndefined();
+ });
+
+ test("objects that answer for themselves", () => {
+ const objects = [];
+ for (let i = 0; i < 40; ++i) objects.push({ [`own${i}`]: i });
+ const get = compiledAfter(["object", "key"], "object[key]", get => {
+ for (const object of objects) for (const key of keys) get(object, key);
+ });
+ const reads = [];
+ const proxy = new Proxy(
+ {},
+ {
+ get(target, key) {
+ reads.push(key);
+ return "trapped";
+ },
+ }
+ );
+ expect(get(proxy, "key1")).toBe("trapped");
+ expect(reads).toEqual(["key1"]);
+ const args = (function () {
+ return arguments;
+ })(1, 2);
+ expect(get(args, "key1")).toBeUndefined();
+ expect(get(args, "length")).toBe(2);
+ });
+});