Truly independent web browser
LibJS/JIT: Add properties through the VM's keyed store cache
Andreas Kling6cebdaba778d
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/named_storage.rs b/Libraries/LibJS/JIT/Rust/src/codegen/named_storage.rsindex 8e438527d72..426cdcfb5b2 100644--- a/Libraries/LibJS/JIT/Rust/src/codegen/named_storage.rs+++ b/Libraries/LibJS/JIT/Rust/src/codegen/named_storage.rs@@ -46,8 +46,10 @@ pub(super) struct DeferredStorageGrowth {object: Gpr,/// Holds the property count of the new shape.needed: Gpr,- /// The cache entry, whose new shape the addition reloads.+ /// The cache entry, whose new shape (at `new_shape`) the addition+ /// reloads.cache_entry: Gpr,+ new_shape: i32,shape: Gpr,}@@ -74,13 +76,14 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {/// The label that grows the named property storage of the object in/// `registers.object` to hold `needed` properties and continues at- /// `resume`, with the new shape of `registers.entry` in- /// `registers.shape`, or goes to `miss`. Or `miss` itself if JIT code- /// cannot grow storage.+ /// `resume`, with the new shape of `registers.entry` (at `new_shape` in+ /// it) in `registers.shape`, or goes to `miss`. Or `miss` itself if JIT+ /// code cannot grow storage.pub(super) fn storage_growth(&mut self,node: NodeId,registers: &CacheRegisters,+ new_shape: i32,needed: Gpr,resume: Label,miss: Label,@@ -97,6 +100,7 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {object: registers.object,needed,cache_entry: registers.entry,+ new_shape,shape: registers.shape,});entry@@ -109,7 +113,6 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {}let registers = Self::growth_registers().expect("growth needs its registers");let subroutine = self.masm.new_label();- let layout = self.runtime.layout;for growth in growths {self.annotate(super::CodeAnnotation::Deferred(growth.node));self.masm.bind(growth.entry);@@ -119,10 +122,8 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {self.masm.call(subroutine);self.masm.branch_test32(Condition::Zero, self.pinned.scratch, u32::MAX, growth.miss);- self.masm.load64(- growth.shape,- &Address::new(growth.cache_entry, layout.property_lookup_cache_entry_shape as i32),- );+ self.masm+ .load64(growth.shape, &Address::new(growth.cache_entry, growth.new_shape));self.masm.jump(growth.resume);}self.annotate(super::CodeAnnotation::Tail("grow named property storage"));diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/probes/keyed.rs b/Libraries/LibJS/JIT/Rust/src/codegen/probes/keyed.rsindex a378c73c7e0..16a38441b23 100644--- a/Libraries/LibJS/JIT/Rust/src/codegen/probes/keyed.rs+++ b/Libraries/LibJS/JIT/Rust/src/codegen/probes/keyed.rs@@ -12,6 +12,7 @@use super::super::Codegen;use super::megamorphic::EntryShape;use super::megamorphic::MegamorphicKey;+use super::property::AdditionEntryFields;use super::property::CacheProbe;use super::property::CacheRegisters;use super::property::DeferredCacheProbe;@@ -85,30 +86,62 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {}/// Stores `source` into the writable own data property that the string- /// `key` names on the object in `base`, if the VM's keyed property- /// store cache has it (see `RuntimeLayout::keyed_store_cache_entries`),- /// and jumps to `done`, or jumps to `miss`. Clobbers the cache registers- /// and the scratch register.+ /// `key` names on the object in `base`, or adds it, if the VM's keyed+ /// property store cache has the change or the addition (see+ /// `RuntimeLayout::keyed_store_cache_entries`), and jumps to `done`, or+ /// jumps to `miss`. Uses `storage` for additions, and clobbers the cache+ /// registers and the scratch register.+ #[allow(clippy::too_many_arguments)]fn emit_keyed_store_cache_put(&mut self,+ node: NodeId,base: Gpr,key: Gpr,source: Gpr,registers: CacheRegisters,+ storage: Gpr,miss: Label,done: Label,) {- 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, ®isters, entries, entry_type, miss, miss)- else {+ let layout = self.runtime.layout;+ let entry_type = layout.property_lookup_cache_entry_type_change_own_property;+ let addition = (layout.keyed_lookup_cache_new_shape != 0).then(|| self.masm.new_label());+ let Some(property) = self.emit_vm_keyed_cache_probe(+ base,+ key,+ ®isters,+ layout.keyed_store_cache_entries,+ entry_type,+ miss,+ addition.unwrap_or(miss),+ ) else {return;};- let CacheRegisters { entry, .. } = registers;+ let CacheRegisters { object, entry, .. } = registers;self.masm.load64(entry, &property);self.branch_on_tag(Condition::Equal, entry, value::ACCESSOR_TAG, entry, miss);self.masm.store64(&property, source);self.masm.jump(done);+ let Some(addition) = addition else {+ return;+ };+ // NB: The cache entry is in the object register, and the shape it+ // was found for in the shape register (see+ // `emit_vm_keyed_cache_entry()`).+ self.masm.bind(addition);+ self.masm.move64(entry, object);+ self.emit_unbox_cell(object, base);+ // NB: Finding the entry matched the shape and its dictionary generation.+ let fields = AdditionEntryFields {+ entry_type: layout.keyed_lookup_cache_entry_type as i32,+ from_shape: None,+ dictionary_generation: None,+ prototype_chain_validity: layout.keyed_lookup_cache_prototype_chain_validity as i32,+ new_shape: layout.keyed_lookup_cache_new_shape as i32,+ property_offset: layout.keyed_lookup_cache_property_offset as i32,+ };+ self.emit_add_own_property(node, ®isters, fields, source, storage, miss);+ self.masm.jump(done);}/// Finds the entry for the string `key` and the shape of the object in@@ -484,11 +517,12 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {self.masm.load64(shape, &Address::new(object, self.runtime.offsets.object_shape as i32));let registers = CacheRegisters { object, shape, entry };- self.emit_add_own_property(node, ®isters, source, storage, fail);+ let fields = self.site_addition_entry_fields();+ self.emit_add_own_property(node, ®isters, fields, source, storage, fail);self.masm.jump(stored);self.masm.bind(store_cache);- self.emit_keyed_store_cache_put(base, key, source, registers, fail, stored);+ self.emit_keyed_store_cache_put(node, base, key, source, registers, storage, fail, stored);self.masm.bind(stored);self.masm.move_imm32(output, 1);self.bind_probe_failure(output, 0, failure, done);diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/probes/property.rs b/Libraries/LibJS/JIT/Rust/src/codegen/probes/property.rsindex 9e10dd4f1f2..8016f8fd6b4 100644--- a/Libraries/LibJS/JIT/Rust/src/codegen/probes/property.rs+++ b/Libraries/LibJS/JIT/Rust/src/codegen/probes/property.rs@@ -63,6 +63,20 @@ pub(in crate::codegen) struct CacheRegisters {pub(in crate::codegen) entry: Gpr,}+/// The offsets in a cache entry of the parts of an addition (see+/// `Codegen::emit_add_own_property()`). Entries that the shape they add to+/// and its dictionary generation were already matched for have no+/// `from_shape` or `dictionary_generation` to check.+#[derive(Clone, Copy)]+pub(super) struct AdditionEntryFields {+ pub entry_type: i32,+ pub from_shape: Option<i32>,+ pub dictionary_generation: Option<i32>,+ pub prototype_chain_validity: i32,+ pub new_shape: i32,+ pub property_offset: i32,+}+impl<M: PortableMacroAssembler> Codegen<'_, M> {fn cache_registers(&self, node: NodeId) -> CacheRegisters {CacheRegisters {@@ -485,24 +499,26 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {);self.masm.bind(transition);let storage = self.temp(node, 3);- self.emit_add_own_property(node, ®isters, source, storage, probe);+ let fields = self.site_addition_entry_fields();+ self.emit_add_own_property(node, ®isters, fields, source, storage, probe);self.masm.bind(stored);self.masm.move_imm32(output, 1);self.bind_probe_failure(output, 0, failure, done);Ok(())}- /// Adds the property of the `AddOwnProperty` entry in `registers.entry`- /// to the object in `registers.object`, whose shape is in- /// `registers.shape`, like `asm_try_put_by_id_cache()` does: the object- /// takes the entry's new shape, and `source` goes in the new property's- /// slot. Storage that must grow grows out of line (see+ /// Adds the property of the `AddOwnProperty` entry in `registers.entry`,+ /// laid out like `fields` says, to the object in `registers.object`,+ /// whose shape is in `registers.shape`, like `asm_try_put_by_id_cache()`+ /// does: the object takes the entry's new shape, and `source` goes in the+ /// new property's slot. Storage that must grow grows out of line (see/// `named_storage`), or the addition is left to `miss`. Uses `storage`/// and clobbers the shape register.pub(super) fn emit_add_own_property(&mut self,node: NodeId,registers: &CacheRegisters,+ fields: AdditionEntryFields,source: Gpr,storage: Gpr,miss: Label,@@ -514,16 +530,14 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {self.masm.branch32_memory_imm(Condition::NotEqual,- &Address::new(entry, layout.property_lookup_cache_entry_type as i32),+ &Address::new(entry, fields.entry_type),layout.property_lookup_cache_entry_type_add_own_property as i32,miss,);- self.masm.branch64_memory(- Condition::NotEqual,- &Address::new(entry, layout.property_lookup_cache_entry_from_shape as i32),- shape,- miss,- );+ if let Some(from_shape) = fields.from_shape {+ self.masm+ .branch64_memory(Condition::NotEqual, &Address::new(entry, from_shape), shape, miss);+ }// Extensible objects that may cache property additions, without a// magical length (whose additions depend on the key).let flags = layout.object_flag_is_extensible@@ -541,22 +555,17 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {);// NB: Only additions that make dictionary shapes need the dictionary// generations to match, but requiring it of all of them is safe.- self.branch_unless_dictionary_generation_matches(shape, entry, miss);+ if let Some(dictionary_generation) = fields.dictionary_generation {+ let expected = Address::new(entry, dictionary_generation);+ self.branch_unless_dictionary_generation_is(shape, expected, [shape, scratch], miss);+ }let valid_chain = 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, &Address::new(entry, fields.prototype_chain_validity));self.masm.branch_test64(Condition::Zero, shape, u64::MAX, valid_chain);self.branch_unless_prototype_chain_valid(shape, miss);self.masm.bind(valid_chain);- self.masm.load64(- shape,- &Address::new(entry, layout.property_lookup_cache_entry_shape as i32),- );+ self.masm.load64(shape, &Address::new(entry, fields.new_shape));self.masm.branch_test64(Condition::Zero, shape, u64::MAX, miss);// The capacity of the inline or heap storage must hold every@@ -582,22 +591,33 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {self.masm.load32(storage, &Address::new(shape, layout.shape_property_count as i32));let has_room = self.masm.new_label();- let grow = self.storage_growth(node, registers, storage, has_room, miss);+ let grow = self.storage_growth(node, registers, fields.new_shape, storage, has_room, miss);self.masm.branch32(Condition::Above, storage, scratch, grow);self.masm.bind(has_room);self.masm.store64(&Address::new(object, offsets.object_shape as i32), shape);- self.masm.load32(- scratch,- &Address::new(entry, layout.property_lookup_cache_entry_property_offset as i32),- );+ self.masm.load32(scratch, &Address::new(entry, fields.property_offset));self.masm.load64(storage, &Address::new(object, offsets.object_named_properties as i32));self.masm.store64(&Address::indexed(storage, scratch, Scale::Eight, 0), source);}+ /// Where an entry of a property lookup cache keeps the parts of an+ /// addition.+ pub(super) fn site_addition_entry_fields(&self) -> AdditionEntryFields {+ let layout = self.runtime.layout;+ AdditionEntryFields {+ entry_type: layout.property_lookup_cache_entry_type as i32,+ from_shape: Some(layout.property_lookup_cache_entry_from_shape as i32),+ dictionary_generation: Some(layout.property_lookup_cache_entry_dictionary_generation as i32),+ prototype_chain_validity: layout.property_lookup_cache_entry_prototype_chain_validity as i32,+ new_shape: layout.property_lookup_cache_entry_shape as i32,+ property_offset: layout.property_lookup_cache_entry_property_offset as i32,+ }+ }+/// The runtime probes of the cache probe nodes, out of line.pub(in crate::codegen) fn emit_deferred_cache_probes(&mut self) -> Result<(), CompileFailure> {for deferred in std::mem::take(&mut self.deferred_cache_probes) {diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs b/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rsindex 1baddd581a1..5583161fefe 100644--- a/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs+++ b/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs@@ -1321,6 +1321,7 @@ fn fake_object_layout() -> RuntimeLayout {keyed_lookup_cache_shape: 16,keyed_lookup_cache_name: 40,keyed_lookup_cache_prototype_chain_validity: 0,+ keyed_lookup_cache_new_shape: 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.rsindex 970b3247a61..1ad39434a62 100644--- a/Libraries/LibJS/JIT/Rust/src/snapshot.rs+++ b/Libraries/LibJS/JIT/Rust/src/snapshot.rs@@ -1875,6 +1875,10 @@ pub struct RuntimeLayout {/// `GetMissingProperty` entry relies on, or 0 if JIT code does not use/// such entries.pub keyed_lookup_cache_prototype_chain_validity: u32,+ /// The offset in an entry of the store cache of the shape (a cell+ /// pointer) that an `AddOwnProperty` entry gives the objects it adds+ /// the property to, or 0 if JIT code does not use such entries.+ pub keyed_lookup_cache_new_shape: 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.rsindex 543dbcfdf5f..6c222854d56 100644--- a/Libraries/LibJS/Rust/src/bytecode/executable.rs+++ b/Libraries/LibJS/Rust/src/bytecode/executable.rs@@ -912,6 +912,7 @@ pub struct KeyedPropertyLookupCacheLayout {pub shape: u32,pub property_name: u32,pub prototype_chain_validity: u32,+ pub new_shape: u32,}/// The VM-wide cache of string-keyed GetByValue lookups. Lookups are copied out, so that no entry is borrowed while@@ -953,6 +954,7 @@ impl KeyedPropertyLookupCache {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,+ new_shape: (lookup + offset_of!(KeyedPropertyLookup, new_shape)) as u32,}}diff --git a/Libraries/LibJS/Rust/src/jit/runtime_info.rs b/Libraries/LibJS/Rust/src/jit/runtime_info.rsindex 37d95c796bb..74c3ae33e8a 100644--- a/Libraries/LibJS/Rust/src/jit/runtime_info.rs+++ b/Libraries/LibJS/Rust/src/jit/runtime_info.rs@@ -369,6 +369,7 @@ fn runtime_layout() -> RuntimeLayout {keyed_lookup_cache_shape: 0,keyed_lookup_cache_name: 0,keyed_lookup_cache_prototype_chain_validity: 0,+ keyed_lookup_cache_new_shape: 0,}}@@ -458,6 +459,7 @@ pub fn runtime_info(vm: &Vm, realm: Gc<Realm>, allocation: AllocationInfos) -> Rlayout.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.keyed_lookup_cache_new_shape = keyed_lookup_cache.new_shape;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.jsindex 8b70ce4d00c..66c223b6fbc 100644--- a/Tests/LibJS/Runtime/jit/vm-keyed-cache-entries.js+++ b/Tests/LibJS/Runtime/jit/vm-keyed-cache-entries.js@@ -1,6 +1,7 @@// 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.+// VM's keyed caches, like the interpreter: missing properties read undefined, and stores add properties to objects of+// the shapes the store cache remembers additions for. 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}`);@@ -13,7 +14,7 @@ function compiledAfter(parameters, body, warmUp) {return fn;}-describe("keyed accesses through the VM's lookup cache in JIT code", () => {+describe("keyed accesses through the VM's caches 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.@@ -82,4 +83,39 @@ describe("keyed accesses through the VM's lookup cache in JIT code", () => {expect(get(args, "key1")).toBeUndefined();expect(get(args, "length")).toBe(2);});++ test("additions of many keys", () => {+ const make = () => {+ const object = {};+ for (let k = 0; k < keys.length; k += 3) object[keys[k]] = k;+ return object;+ };+ const put = compiledAfter(["object", "key", "value"], "(object[key] = value)", put => {+ for (let round = 0; round < 3; ++round) {+ const object = make();+ for (const key of keys) put(object, key, key);+ }+ });+ for (let round = 0; round < 3; ++round) {+ const object = make();+ for (let k = 0; k < keys.length; ++k) put(object, keys[k], -k);+ for (let k = 0; k < keys.length; ++k) expect(object[keys[k]]).toBe(-k);+ expect(Object.keys(object).length).toBe(keys.length);+ }+ const sealed = make();+ Object.preventExtensions(sealed);+ put(sealed, "key1", 1);+ expect(Object.hasOwn(sealed, "key1")).toBeFalse();+ const prototype = {};+ const stores = [];+ Object.defineProperty(prototype, "key1", {+ set(value) {+ stores.push(value);+ },+ });+ const inheriting = Object.assign(Object.create(prototype), make());+ put(inheriting, "key1", "set");+ expect(stores).toEqual(["set"]);+ expect(Object.hasOwn(inheriting, "key1")).toBeFalse();+ });});