Truly independent web browser
LibJS/JIT: Keep element stores in loops from forgetting what they knew
Andreas Klingbbc11189f3a6
diff --git a/Libraries/LibJS/JIT/ARCHITECTURE.md b/Libraries/LibJS/JIT/ARCHITECTURE.mdindex 59bf9534dee..1c0fef3de23 100644--- a/Libraries/LibJS/JIT/ARCHITECTURE.md+++ b/Libraries/LibJS/JIT/ARCHITECTURE.md@@ -184,7 +184,8 @@ Exits: E = eager, L = lazy. Every node that can exit has a frame state. "Call" m| ProbeGlobalCache, ProbeGlobalStore | The global variable a global variable cache is for (a data property of the global object of the cached shape, or an initialized binding of the global declarative environment), like `GetGlobal` and `SetGlobal`; the empty value, or false, where the cache does not apply. Inputs: the realm and the executable (and for stores, the value first). | | Any | (Store) Any | || ProbeHasProperty | Whether the object has the property the key names, as its own (`hasOwnProperty`) or at all (`in`), where the VM can tell without running code: from the VM's keyed cache and, for `in`, the elements inline, and from the runtime's lookup out of line; the empty value otherwise. | | Any | | saves GPRs || AppendElement | Appends an element below the capacity. | | Elements | Elements | |-| CallArrayPush | Appends an element, growing the elements, preserving every register. Pushes of several values that do not fit call it for each. | | Elements | Elements | allocates |+| CallArrayPush | Appends an element, growing the elements, preserving every register. Pushes of several values that do not fit call it for each, and so do stores at the length that do not fit. | | Elements | Elements | allocates |+| CallStoreHoleyElement | Stores an element below the length of the holey elements of an array that `BranchCondition::HoleFillableArray` established runs no code, growing the elements where they have no room for it (or making them a dictionary, where the store is sparse), and making them packed where it filled their last hole, preserving every register. | | Elements | Elements | allocates || **Strings and for-in** | | | | | || StringLength, LoadStringCodeUnit | The length of a string, and a code unit of a readable string at an index in bounds, which the refinements of their branches establish. Strings never change. | | | | cse || SingleCharacterString | The VM's string of an ASCII code unit (refined below 128). | | | | cse |diff --git a/Libraries/LibJS/JIT/Rust/src/builder/checks.rs b/Libraries/LibJS/JIT/Rust/src/builder/checks.rsindex a220d59bad8..1c5de3327da 100644--- a/Libraries/LibJS/JIT/Rust/src/builder/checks.rs+++ b/Libraries/LibJS/JIT/Rust/src/builder/checks.rs@@ -404,7 +404,7 @@ impl GraphBuilder<'_> {hot: Vec::new(),cold: Vec::new(),next_case: Vec::new(),- exits: false,+ exits: None,})}@@ -414,7 +414,14 @@ impl GraphBuilder<'_> {/// other code, so it keeps what it knew before the instruction. For/// instructions whose slow path only handles rare cases.pub(super) fn exit_instead_of_slow_path(&mut self, slow_paths: &mut SlowPaths) {- slow_paths.exits = self.may_speculate(ExitKind::SlowPath);+ self.exit_with_instead_of_slow_path(slow_paths, ExitKind::SlowPath);+ }++ /// Like `exit_instead_of_slow_path()`, exiting with `kind`, for+ /// instructions whose other speculations exit with `ExitKind::SlowPath`+ /// and should not go once such an exit was taken.+ pub(super) fn exit_with_instead_of_slow_path(&mut self, slow_paths: &mut SlowPaths, kind: ExitKind) {+ slow_paths.exits = self.may_speculate(kind).then_some(kind);}/// Ends the current block with a branch on `condition` of `inputs`,@@ -669,15 +676,10 @@ impl GraphBuilder<'_> {/// edge they took jumps to, with the operands the slow path wrote, if/// any edge was taken.fn build_slow_path(&mut self, slow_paths: &SlowPaths) -> Option<(BlockId, Vec<NodeId>)> {- if slow_paths.exits {+ if let Some(kind) = slow_paths.exits {for edge in &slow_paths.cold {self.block = *edge;- let exit = self.set_control(- Op::Exit {- kind: ExitKind::SlowPath,- },- Vec::new(),- );+ let exit = self.set_control(Op::Exit { kind }, Vec::new());self.graph.nodes[exit.index()].frame_state = Some(slow_paths.frame_state);}return None;@@ -720,9 +722,9 @@ pub(in crate::builder) struct SlowPaths {/// The edges that `GraphBuilder::branch_to_next_case()` took since the/// last case started.next_case: Vec<BlockId>,- /// Whether the cold edges exit instead (see+ /// The kind of exit the cold edges take instead, if they exit (see/// `GraphBuilder::exit_instead_of_slow_path()`).- exits: bool,+ exits: Option<ExitKind>,}impl SlowPaths {diff --git a/Libraries/LibJS/JIT/Rust/src/builder/keyed.rs b/Libraries/LibJS/JIT/Rust/src/builder/keyed.rsindex e25b8242d88..36684b0e528 100644--- a/Libraries/LibJS/JIT/Rust/src/builder/keyed.rs+++ b/Libraries/LibJS/JIT/Rust/src/builder/keyed.rs@@ -41,6 +41,8 @@ use crate::snapshot::Forwarding;use crate::snapshot::Intrinsic;use crate::snapshot::keyed_feedback_bits::HOLEY;use crate::snapshot::keyed_feedback_bits::INT32_INDEX;+use crate::snapshot::keyed_feedback_bits::KEY_KINDS_MASK;+use crate::snapshot::keyed_feedback_bits::OTHER_ELEMENTS;use crate::snapshot::keyed_feedback_bits::OTHER_KEY;use crate::snapshot::keyed_feedback_bits::OUT_OF_BOUNDS;use crate::snapshot::keyed_feedback_bits::PACKED;@@ -142,8 +144,8 @@ impl GraphBuilder<'_> {/// `index`, refined to be in the bounds of the packed elements of the/// array `object` (at `address`), taking a slow path where it is not,/// except for the index of the next element: there, `value` becomes the- /// next element where the array has room for it, like- /// `Array.prototype.push` would append it, and the access is done.+ /// next element like `Array.prototype.push` would append it, growing+ /// the elements where they have no room for it, and the access is done.pub(super) fn element_index_or_append(&mut self,slow_paths: &mut SlowPaths,@@ -162,12 +164,20 @@ impl GraphBuilder<'_> {let capacity = self.emit(Op::LoadElementsCapacity, vec![array_address], Some(Repr::Int32));// NB: Lengths above i32::MAX are negative as int32 values, which an// unsigned comparison keeps from passing as room for an element.- let next_index = self.branch_to_slow_path(- slow_paths,- BranchCondition::IndexInBounds,- vec![next_index, capacity],- true,- );+ // Appends without room grow the elements through the runtime,+ // which runs no code either, but at such lengths, whose keys are+ // no indices, which the slow path stores.+ let room = BranchCondition::IndexInBounds;+ let (hot, cold) = self.branch_with_cold_side(room, vec![next_index, capacity], true);+ self.block = cold;+ let zero = self.int32_constant(0);+ let nonnegative = BranchCondition::Int32(Comparison::GreaterThanEquals);+ self.branch_to_slow_path(slow_paths, nonnegative, vec![next_index, zero], true);+ self.graph.blocks[self.block.index()].is_cold = true;+ self.emit(Op::CallArrayPush, vec![array, value], Some(Repr::Tagged));+ self.end_hot_path(slow_paths, Vec::new());+ self.block = hot;+ let next_index = self.refine(room, true, vec![next_index, capacity]);self.emit(Op::AppendElement,vec![array_address, next_index, value],@@ -337,6 +347,22 @@ impl GraphBuilder<'_> {let kinds = self.elements_kinds_seen(bits);let named = bits & (STRING_KEY | SYMBOL_KEY) != 0;let mut slow_paths = self.start_slow_paths(None)?;+ // NB: Stores of int32 indices into packed or holey elements fill holes and append inline, so their slow path+ // only stores out of bounds otherwise, which is rare, as are stores out of the bounds of typed arrays+ // where the feedback saw none. Exiting there instead keeps loops that store elements free of code that+ // may change what they read. The exits are out of bounds ones, so that taking one keeps inline appends,+ // which go once their check of the array exits with `ExitKind::SlowPath`. Where that happened, appends+ // take the slow path, which they would otherwise exit at too.+ let appends = self.may_speculate(ExitKind::SlowPath);+ let out_of_bounds_typed_arrays = !self.typed_array_elements_seen(bits).is_empty() && bits & OUT_OF_BOUNDS != 0;+ if appends+ && bits & KEY_KINDS_MASK == INT32_INDEX+ && bits & OTHER_ELEMENTS == 0+ && !kinds.is_empty()+ && !out_of_bounds_typed_arrays+ {+ self.exit_with_instead_of_slow_path(&mut slow_paths, ExitKind::OutOfBounds);+ }let object = self.read(base)?;let key = self.read(property)?;let value = self.read(src)?;@@ -347,7 +373,6 @@ impl GraphBuilder<'_> {// NB: Appends and hole fills check that the prototype chain is the// realm's default one. Appends exit for arrays that push could// not append to, and hole fills take the slow path.- let appends = self.may_speculate(ExitKind::SlowPath);if appends || kinds.contains(&ElementsKind::Holey) {self.embed(self.runtime.array_prototype);self.embed(self.runtime.object_prototype);@@ -368,16 +393,16 @@ impl GraphBuilder<'_> {let kind = kinds[case];let stored = builder.stored_element(slow_paths, kind, value);let address = builder.cell_address(object);+ if kind == ElementsKind::Holey {+ builder.store_holey_element(slow_paths, address, index, stored);+ return;+ }let (storage, index) = if kind == ElementsKind::Packed && appends {let index = builder.element_index_or_append(slow_paths, object, address, index, stored);(address, index)} else {builder.element_index_in_bounds(slow_paths, kind, address, index)};- if kind == ElementsKind::Holey {- builder.store_holey_element(slow_paths, address, index, stored);- return;- }builder.store_element(kind, storage, index, stored);builder.end_hot_path(slow_paths, Vec::new());},@@ -398,30 +423,54 @@ impl GraphBuilder<'_> {Ok(Flow::Continue)}- /// Stores `value` at the checked `index` of the holey elements of the- /// object at `address`. A hole takes a new element, which runs no code- /// where the object is an array with a plain prototype chain (see- /// `BranchCondition::HoleFillableArray`); other holes take a slow path.+ /// Stores `value` at `index` of the holey elements of the object at+ /// `address`, taking a slow path unless the index is below their length.+ /// A hole takes a new element, and elements without room for it grow,+ /// which runs no code where the object is an array with a plain prototype+ /// chain (see `BranchCondition::HoleFillableArray`); other holes take a+ /// slow path.fn store_holey_element(&mut self, slow_paths: &mut SlowPaths, address: NodeId, index: NodeId, value: NodeId) {let kind = ElementsKind::Holey;+ let in_bounds = BranchCondition::IndexInBounds;+ let length = self.emit(Op::LoadElementsLength, vec![address], Some(Repr::Int32));+ let index = self.branch_to_slow_path(slow_paths, in_bounds, vec![index, length], true);+ // NB: Elements made with a length, like `new Array(length)`, have no room yet, and grow as they get filled.+ let capacity = self.emit(Op::LoadElementsCapacity, vec![address], Some(Repr::Int32));+ let (room, no_room) = self.branch_with_cold_side(in_bounds, vec![index, capacity], true);+ self.block = no_room;+ // NB: Lengths above i32::MAX let negative int32 keys, which are no indices, pass as below the length.+ let zero = self.int32_constant(0);+ let nonnegative = BranchCondition::Int32(Comparison::GreaterThanEquals);+ let element_index = self.branch_to_slow_path(slow_paths, nonnegative, vec![index, zero], true);+ self.graph.blocks[self.block.index()].is_cold = true;+ let array = self.branch_to_slow_path(slow_paths, BranchCondition::HoleFillableArray, vec![address], true);+ self.graph.blocks[self.block.index()].is_cold = true;+ self.emit(Op::CallStoreHoleyElement, vec![array, element_index, value], None);+ self.end_hot_path(slow_paths, Vec::new());++ self.block = room;+ let index = self.refine(in_bounds, true, vec![index, capacity]);let old = self.emit(Op::LoadElementAt { kind }, vec![address, index], Some(Repr::Tagged));let empty = self.constant(value::EMPTY);let (hole, element) = self.branch_both_ways(BranchCondition::TaggedEquals { equal: true }, vec![old, empty]);self.block = hole;- self.branch_to_slow_path(slow_paths, BranchCondition::HoleFillableArray, vec![address], true);+ let array = self.branch_to_slow_path(slow_paths, BranchCondition::HoleFillableArray, vec![address], true);// NB: A store to the last index may make the elements packed, which- // the slow path does. The index is below the length, so the- // length is not 0.- let length = self.emit(Op::LoadElementsLength, vec![address], Some(Repr::Int32));+ // the runtime does. The index is below the length, so the length+ // is not 0.let one = self.int32_constant(1);let last = self.emit(Op::Int32WrappingBinary { op: BinaryOp::Sub },vec![length, one],Some(Repr::Int32),);- let index_before_last =- self.branch_to_slow_path(slow_paths, BranchCondition::IndexInBounds, vec![index, last], true);+ let (before_last, at_last) = self.branch_with_cold_side(in_bounds, vec![index, last], true);+ self.block = at_last;+ self.emit(Op::CallStoreHoleyElement, vec![array, index, value], None);+ self.end_hot_path(slow_paths, Vec::new());+ self.block = before_last;+ let index_before_last = self.refine(in_bounds, true, vec![index, last]);self.store_element(kind, address, index_before_last, value);self.end_hot_path(slow_paths, Vec::new());diff --git a/Libraries/LibJS/JIT/Rust/src/code.rs b/Libraries/LibJS/JIT/Rust/src/code.rsindex 30c64f58a81..632489a5c83 100644--- a/Libraries/LibJS/JIT/Rust/src/code.rs+++ b/Libraries/LibJS/JIT/Rust/src/code.rs@@ -118,7 +118,9 @@ pub enum ExitKind {/// An object accessed by index did not have the elements kind (packed or/// holey array, or typed array of one kind) its keyed feedback had seen.BadElements,- /// An index was out of the bounds of the elements, or hit a hole.+ /// An index was out of the bounds of the elements, or hit a hole, or a+ /// keyed store compiled to exit instead of running its slow path met a+ /// case only its slow path handles.OutOfBounds,/// A value had another type than the instruction's feedback had seen.BadType,diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/arrays.rs b/Libraries/LibJS/JIT/Rust/src/codegen/arrays.rsindex dcd3f131256..6004449b952 100644--- a/Libraries/LibJS/JIT/Rust/src/codegen/arrays.rs+++ b/Libraries/LibJS/JIT/Rust/src/codegen/arrays.rs@@ -196,4 +196,24 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {self.masm.move64(output, M::RETURN_GPRS[0]);self.emit_restore_registers(&saved, None);}++ /// `CallStoreHoleyElement`: calls `RuntimeInfo::store_holey_element`,+ /// saving the registers the call clobbers.+ pub(in crate::codegen) fn emit_call_store_holey_element(&mut self, node: NodeId) {+ let saved = slow_path_saved_registers::<M>().iter().collect::<Vec<_>>();+ self.emit_save_registers(&saved);+ // NB: The inputs may be in each other's argument registers, which the saves area holds the values of.+ for (input, argument) in M::ARGUMENT_GPRS.iter().take(3).enumerate() {+ let value = self.input(node, input);+ match saved.iter().position(|register| *register == value) {+ Some(index) => {+ let address = self.save_address(index);+ self.masm.load64(*argument, &address);+ }+ None => self.masm.move64(*argument, value),+ }+ }+ self.masm.call_absolute(self.runtime.store_holey_element);+ self.emit_restore_registers(&saved, None);+ }}diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/mod.rs b/Libraries/LibJS/JIT/Rust/src/codegen/mod.rsindex 02751e58177..5044b304d21 100644--- a/Libraries/LibJS/JIT/Rust/src/codegen/mod.rs+++ b/Libraries/LibJS/JIT/Rust/src/codegen/mod.rs@@ -1748,6 +1748,7 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {Op::LoadElementsCapacity => self.emit_load_elements_capacity(node_id),Op::AppendElement => self.emit_append_element_node(node_id),Op::CallArrayPush => self.emit_call_array_push(node_id),+ Op::CallStoreHoleyElement => self.emit_call_store_holey_element(node_id),Op::OpenArrayIteration => self.emit_open_array_iteration(node_id),Op::LoadArrayIteratorArray => self.emit_load_array_iterator_array(node_id),Op::LoadArrayIteratorIndex => self.emit_load_array_iterator_index(node_id),diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/slow_path_calls.rs b/Libraries/LibJS/JIT/Rust/src/codegen/slow_path_calls.rsindex 853c97ef03b..cd1435ac77f 100644--- a/Libraries/LibJS/JIT/Rust/src/codegen/slow_path_calls.rs+++ b/Libraries/LibJS/JIT/Rust/src/codegen/slow_path_calls.rs@@ -180,6 +180,7 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {| Op::ProbePropertyStore { .. }| Op::ProbeKeyedStore { .. }| Op::CallArrayPush+ | Op::CallStoreHoleyElement| Op::OpenArrayIteration| Op::AllocateObject { .. }| Op::AllocateArray { .. }diff --git a/Libraries/LibJS/JIT/Rust/src/ir/dump.rs b/Libraries/LibJS/JIT/Rust/src/ir/dump.rsindex 2269e59b831..79550645667 100644--- a/Libraries/LibJS/JIT/Rust/src/ir/dump.rs+++ b/Libraries/LibJS/JIT/Rust/src/ir/dump.rs@@ -306,6 +306,7 @@ fn op_text(layout: &FrameLayout, op: &Op, inputs: &str) -> String {| Op::LoadElementsCapacity| Op::AppendElement| Op::CallArrayPush+ | Op::CallStoreHoleyElement| Op::OpenArrayIteration| Op::LoadArrayIteratorArray| Op::LoadArrayIteratorIndexdiff --git a/Libraries/LibJS/JIT/Rust/src/ir/mod.rs b/Libraries/LibJS/JIT/Rust/src/ir/mod.rsindex 0b83e975af9..389bfb01cab 100644--- a/Libraries/LibJS/JIT/Rust/src/ir/mod.rs+++ b/Libraries/LibJS/JIT/Rust/src/ir/mod.rs@@ -1089,6 +1089,14 @@ pub enum Op {/// registers the call clobbers and restores them afterwards, so it is no/// call for the register allocator. The node's value is the new length.CallArrayPush,+ /// Stores the tagged input 2 at the `Repr::Int32` index input 1, below the+ /// length of the holey elements of the array at the `Repr::Pointer` input+ /// 0, through `RuntimeInfo::store_holey_element`, which grows the elements+ /// where they have no room for it (or makes them a dictionary, where the+ /// store is sparse), and makes them packed if it filled their last hole. A branch on `BranchCondition::HoleFillableArray`+ /// established that this runs no code. It saves the registers the call+ /// clobbers, like `CallArrayPush`.+ CallStoreHoleyElement,// For-of loops over arrays./// `RuntimeInfo::open_array_iteration` of the tagged iterable input 0:@@ -1594,7 +1602,7 @@ impl Op {},// NB: The keys of a cache never change.Op::LoadPropertyIteratorKeyCount | Op::LoadPropertyIteratorKey => P::PURE,- Op::CallArrayPush => P {+ Op::CallArrayPush | Op::CallStoreHoleyElement => P {reads: Locations::ELEMENTS_AND_COUNTS,writes: Locations::ELEMENTS_AND_COUNTS,allocates: true,@@ -1741,6 +1749,7 @@ impl Op {Op::LoadElementsCapacity => "LoadElementsCapacity",Op::AppendElement => "AppendElement",Op::CallArrayPush => "CallArrayPush",+ Op::CallStoreHoleyElement => "CallStoreHoleyElement",Op::OpenArrayIteration => "OpenArrayIteration",Op::LoadArrayIteratorArray => "LoadArrayIteratorArray",Op::LoadArrayIteratorIndex => "LoadArrayIteratorIndex",diff --git a/Libraries/LibJS/JIT/Rust/src/passes/verify.rs b/Libraries/LibJS/JIT/Rust/src/passes/verify.rsindex 9804027d529..f9a75de0546 100644--- a/Libraries/LibJS/JIT/Rust/src/passes/verify.rs+++ b/Libraries/LibJS/JIT/Rust/src/passes/verify.rs@@ -530,7 +530,7 @@ fn expected_input_reprs(graph: &Graph, node: NodeId) -> Option<Vec<Repr>> {Op::ArrayCreate => Some(vec![Repr::Int32, Repr::Tagged]),Op::LoadStringCodeUnit => Some(vec![Repr::Pointer, Repr::Int32]),Op::SingleCharacterString | Op::Int32Abs | Op::Int32ToFloat64 => Some(vec![Repr::Int32]),- Op::AppendElement => Some(vec![Repr::Pointer, Repr::Int32, Repr::Tagged]),+ Op::AppendElement | Op::CallStoreHoleyElement => Some(vec![Repr::Pointer, Repr::Int32, Repr::Tagged]),Op::LoadElementsLength | Op::LoadElementsCapacity => Some(vec![Repr::Pointer]),Op::LoadPropertyIteratorKey => Some(vec![Repr::Tagged, Repr::Int32]),Op::OpenArrayIteration => Some(tagged(1)),diff --git a/Libraries/LibJS/JIT/Rust/src/regalloc/mod.rs b/Libraries/LibJS/JIT/Rust/src/regalloc/mod.rsindex a248ff9aef0..bb210329cfc 100644--- a/Libraries/LibJS/JIT/Rust/src/regalloc/mod.rs+++ b/Libraries/LibJS/JIT/Rust/src/regalloc/mod.rs@@ -105,6 +105,7 @@ pub fn saves_only_gprs(op: &Op) -> bool {} | Op::InitializeFrame { clobbers_floats: true }| Op::EnsureFrameInitialized { clobbers_floats: true }| Op::CallArrayPush+ | Op::CallStoreHoleyElement| Op::OpenArrayIteration| Op::AddNamed { .. }| Op::ProbeKeyedStore { .. }diff --git a/Libraries/LibJS/JIT/Rust/src/snapshot.rs b/Libraries/LibJS/JIT/Rust/src/snapshot.rsindex b48cb7599e6..22c28b049b1 100644--- a/Libraries/LibJS/JIT/Rust/src/snapshot.rs+++ b/Libraries/LibJS/JIT/Rust/src/snapshot.rs@@ -1020,6 +1020,13 @@ pub struct RuntimeInfo {/// observable step, growing its packed elements, and returns the new/// length as a value.pub array_push: u64,+ /// `void libjs_jit_store_holey_element(Object*, u32 index, u64 value)`:+ /// stores `value` at `index`, below the length of the holey elements of+ /// an array whose prototype chain has no indexed properties, growing the+ /// elements where they have no room for it (or making them a dictionary,+ /// where the store is sparse), and makes them packed if it filled their+ /// last hole.+ pub store_holey_element: u64,/// `u64 libjs_jit_slice_arguments(VM*, ExecutionContext* frame, i32 start)`:/// `Array.prototype.slice.call(arguments, start)` with the arguments/// object of `frame`, which the code never created, as an array value.diff --git a/Libraries/LibJS/Rust/src/interpreter/slow_paths/mod.rs b/Libraries/LibJS/Rust/src/interpreter/slow_paths/mod.rsindex e149b9c6d95..0f06dec8b30 100644--- a/Libraries/LibJS/Rust/src/interpreter/slow_paths/mod.rs+++ b/Libraries/LibJS/Rust/src/interpreter/slow_paths/mod.rs@@ -388,8 +388,7 @@ impl RuntimeFunctions for Runtime {if property_access::try_put_by_value_cache(vm, instruction, values) {return SlowPathControl::continue_at(pc + op::PutByValue::LENGTH);}- // NB: Stores that fill a hole of an array, which JIT code leaves to its slow path, take the interpreter's quick- // way for them.+ // NB: Stores that fill a hole of an array take the interpreter's quick way for them.if property_access::put_kind_from_operand(instruction.kind) == PutKind::Normal&& property_access::try_put_by_value_holey_array(values){@@ -723,8 +722,8 @@ impl RuntimeFunctions for Runtime {) -> bool {let handled = property_access::try_put_by_value_holey_array(values);if handled {- // NB: The interpreter only comes here for stores that fill a hole or need more storage, which JIT code- // does not handle.+ // NB: The interpreter only comes here for stores that fill a hole or need more storage, which its fast path+ // does not handle. JIT code handles them itself where nothing can observe it.feedback::record_keyed_bits(vm,instruction.keyed_feedback,diff --git a/Libraries/LibJS/Rust/src/jit/calls.rs b/Libraries/LibJS/Rust/src/jit/calls.rsindex b00ceb16f53..1ffa2d32f4b 100644--- a/Libraries/LibJS/Rust/src/jit/calls.rs+++ b/Libraries/LibJS/Rust/src/jit/calls.rs@@ -921,6 +921,22 @@ pub extern "C" fn libjs_jit_array_push(array: &Array, value: u64) -> u64 {Value::from_f64(f64::from(array.indexed_array_like_size())).0}+/// `void libjs_jit_store_holey_element(Object*, u32 index, u64 value)`: stores `value` at `index`, below the length of+/// the holey elements of the object, growing the elements where they have no room for it (or making them a dictionary,+/// where the store is sparse), and makes them packed if it filled their last hole.+///+/// JIT code calls this with a live array whose prototype chain is the realm's default one without indexed properties,+/// so that the store runs no code.+pub extern "C" fn libjs_jit_store_holey_element(object: &Object, index: u32, value: u64) {+ // NB: Branches on holey elements take packed ones too.+ assert!(matches!(+ object.indexed_storage_kind(),+ IndexedStorageKind::Holey | IndexedStorageKind::Packed+ ));+ assert!(index < object.indexed_array_like_size());+ object.indexed_put(index, Value(value), DEFAULT_ATTRIBUTES);+}+/// `u64 libjs_jit_primitive_to_string(VM*, u64 primitive)`: `String(primitive)`, which is ToString of the primitive,/// but the descriptive string of a symbol.///diff --git a/Libraries/LibJS/Rust/src/jit/runtime_info.rs b/Libraries/LibJS/Rust/src/jit/runtime_info.rsindex b756dc1ca75..ff3e46b6e14 100644--- a/Libraries/LibJS/Rust/src/jit/runtime_info.rs+++ b/Libraries/LibJS/Rust/src/jit/runtime_info.rs@@ -481,6 +481,7 @@ pub fn runtime_info(vm: &Vm, realm: Gc<Realm>, allocation: AllocationInfos) -> R0},array_push: super::calls::libjs_jit_array_push as *const () as u64,+ store_holey_element: super::calls::libjs_jit_store_holey_element as *const () as u64,slice_arguments: super::calls::libjs_jit_slice_arguments as *const () as u64,open_array_iteration: super::intrinsics::libjs_jit_open_array_iteration as *const () as u64,// NB: Filled in by capture_snapshot(), which lists it as an embedded cell.diff --git a/Tests/LibJS/JIT/ir/hole-filling-stores.js b/Tests/LibJS/JIT/ir/hole-filling-stores.jsindex 1b3cd3aa9d6..1ba8e39950b 100644--- a/Tests/LibJS/JIT/ir/hole-filling-stores.js+++ b/Tests/LibJS/JIT/ir/hole-filling-stores.js@@ -1,7 +1,8 @@// Stores into holes of holey arrays fill them inline where the array can get-// a new element without running any code. Holes of other objects, stores that-// need more storage, and stores to the last index, which may make the-// elements packed, take the slow path, in cold blocks.+// a new element without running any code. Stores that need more storage, and+// stores to the last index, which may make the elements packed, call the+// runtime in cold blocks, which runs no code for such arrays either. Holes of+// other objects exit, so no path through the store runs other code.function fill(n) {const a = new Array(n);@@ -17,7 +18,12 @@ jit.compile(fill);// CHECK: BranchElementsKind Holey// CHECK: [[ADDRESS:v[0-9]+]]:pointer = CellAddress// CHECK-NEXT: [[LENGTH:v[0-9]+]]:int32 = LoadElementsLength ([[ADDRESS]])-// CHECK: [[OLD:v[0-9]+]] = LoadElementAt Holey ([[ADDRESS]], [[INDEX:v[0-9]+]])+// CHECK-NEXT: BranchIndexInBounds ([[INDEX:v[0-9]+]], [[LENGTH]])+// CHECK-NOT: cold+// CHECK: [[CAPACITY:v[0-9]+]]:int32 = LoadElementsCapacity ([[ADDRESS]])+// CHECK-NEXT: BranchIndexInBounds ([[INDEX]], [[CAPACITY]])+// CHECK-NOT: cold+// CHECK: [[OLD:v[0-9]+]] = LoadElementAt Holey ([[ADDRESS]], [[INDEX]])// CHECK-NEXT: BranchTaggedEquals ([[OLD]], #empty)// CHECK-NOT: cold// CHECK: BranchHoleFillableArray ([[ADDRESS]])@@ -26,8 +32,8 @@ jit.compile(fill);// CHECK-NEXT: BranchIndexInBounds ([[INDEX]], [[LAST]])// CHECK-NOT: cold// CHECK: StoreElementAt Holey ([[ADDRESS]], [[INDEX]],-// CHECK-NOT: CallSlowPath// CHECK: cold:// CHECK-NOT: StoreElementAt-// CHECK: CallSlowPath PutByValue-// CHECK-COUNT-1: CallSlowPath PutByValue+// CHECK: CallStoreHoleyElement ([[ADDRESS]], [[INDEX]],+// CHECK-COUNT-2: CallStoreHoleyElement+// CHECK-COUNT-0: CallSlowPath PutByValuediff --git a/Tests/LibJS/Runtime/jit/hole-filling-stores.js b/Tests/LibJS/Runtime/jit/hole-filling-stores.jsindex c6d3b3852ed..ddff220063f 100644--- a/Tests/LibJS/Runtime/jit/hole-filling-stores.js+++ b/Tests/LibJS/Runtime/jit/hole-filling-stores.js@@ -110,6 +110,18 @@ describe("Hole-filling stores in JIT code", () => {}});+ test("negative keys of arrays longer than 2^31", () => {+ warmUp();+ const array = new Array(2 ** 32 - 1);+ store(array, -5, "negative");+ strictStore(array, -6, "also negative");+ expect(array[-5]).toBe("negative");+ expect(array[-6]).toBe("also negative");+ expect(array[2 ** 32 - 5]).toBeUndefined();+ expect(array.length).toBe(2 ** 32 - 1);+ expect(Object.keys(array)).toEqual(["-5", "-6"]);+ });+test("arrays that cannot get new elements", () => {warmUp();const array = Object.preventExtensions(holeyArray(8));