Truly independent web browser
LibJS: Skip the slow path of IteratorClose without a return method
Aliaksandr Kalenik2efc6d6bda28
diff --git a/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S b/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.Sindex 646e6679ece..f4360ba1cb0 100644--- a/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S+++ b/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S@@ -6789,6 +6789,28 @@ asm_handler_ObjectPropertyIteratorNext:br x10asm_handler_ObjectPropertyIteratorNext_hot_end:+.p2align 4+asm_handler_IteratorClose:+ ldr x0, [x28, #88]+ ldr x1, [x0, #152]+ cbz x1, .Lasm_IteratorClose.assert_failure_0+ ldr w2, [x21, #24]+ ldr w0, [x21, #4]+ ldr x0, [x27, x0, lsl #3]+ lsl x2, x2, #3+ adds x1, x1, x2+ bl CSYM(asm_try_get_by_id_cache)+ lsr x0, x0, #48+ and w0, w0, #0xfffe+ mov w9, #32766+ cmp w0, w9+ b.ne .Lasm_IteratorClose.ssa_block_1+ ldrb w9, [x21, #32]+ add x21, x21, #32+ ldr x10, [x19, x9, lsl #3]+ br x10+asm_handler_IteratorClose_hot_end:+.p2align 4asm_handler_IsCallable:ldr w0, [x21, #8]@@ -17101,47 +17123,54 @@ asm_handler_ObjectPropertyIteratorNext_cold:ldr x10, [x19, x9, lsl #3]br x10asm_handler_ObjectPropertyIteratorNext_cold_end:-.p2align 4-asm_handler_IteratorClose:+asm_handler_IteratorClose_cold:+// Cold paths for IteratorClose+.Lasm_IteratorClose.ssa_block_1:mov x0, x20sub w1, w21, w26str w1, [x28, #64]ldr w9, [x21, #4]ldr x10, [x27, x9, lsl #3]-.Lasm_IteratorClose.scalar_value_ready_0:+.Lasm_IteratorClose.scalar_value_ready_1:mov x3, x10ldr w9, [x21, #8]ldr x10, [x27, x9, lsl #3]-.Lasm_IteratorClose.scalar_value_ready_1:+.Lasm_IteratorClose.scalar_value_ready_2:mov x4, x10ldr w9, [x21, #12]ldr x10, [x27, x9, lsl #3]-.Lasm_IteratorClose.scalar_value_ready_2:+.Lasm_IteratorClose.scalar_value_ready_3:mov x5, x10ldr w9, [x21, #20]ldr x10, [x27, x9, lsl #3]-.Lasm_IteratorClose.scalar_value_ready_3:+.Lasm_IteratorClose.scalar_value_ready_4:mov x6, x10mov x0, x20sub w1, w21, w26mov x2, x21bl CSYM(asm_slow_path_iterator_close)- tbnz x0, #63, .Lasm_IteratorClose.scalar_result_done_4- tbz x0, #32, .Lasm_IteratorClose.scalar_result_done_4+ tbz x0, #63, 1f+ b .Lasm_IteratorClose.scalar_result_done_5+1:+ tbnz x0, #32, 1f+ b .Lasm_IteratorClose.scalar_result_done_5+1:add x21, x26, w0, uxtwldrb w9, [x21]ldr x10, [x19, x9, lsl #3]br x10-.Lasm_IteratorClose.scalar_result_done_4:+.Lasm_IteratorClose.scalar_result_done_5:tbz x0, #63, 1fb .Lexit1:- tbz x0, #32, .Lasm_IteratorClose.slow_path_transfer_5+ tbnz x0, #32, 1f+ b .Lasm_IteratorClose.slow_path_transfer_6+1:add x21, x26, w0, uxtwldrb w9, [x21]ldr x10, [x19, x9, lsl #3]br x10-.Lasm_IteratorClose.slow_path_transfer_5:+.Lasm_IteratorClose.slow_path_transfer_6:ldr x28, [x20]ldr x9, [x28, #88]ldr x26, [x9, #40]@@ -17150,10 +17179,7 @@ asm_handler_IteratorClose:ldrb w9, [x21]ldr x10, [x19, x9, lsl #3]br x10-asm_handler_IteratorClose_hot_end:-asm_handler_IteratorClose_cold:asm_handler_IteratorClose_cold_end:-.p2align 4asm_handler_IteratorNext:mov x0, x20@@ -22208,6 +22234,8 @@ asm_assertion_failure_traps:brk #0.Lasm_ObjectPropertyIteratorNext.assert_predicate_failure_65:brk #0+.Lasm_IteratorClose.assert_failure_0:+ brk #0.Lasm_LeavePrivateEnvironment.assert_predicate_failure_3:brk #0.Lexit:diff --git a/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S b/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.Sindex 0f500a95047..aa34b4b2e9d 100644--- a/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S+++ b/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S@@ -6950,6 +6950,29 @@ asm_handler_ObjectPropertyIteratorNext:jmp [r12 + rax * 8]asm_handler_ObjectPropertyIteratorNext_hot_end:+.p2align 6+asm_handler_IteratorClose:+ mov rax, QWORD PTR [rbx - 40]+ mov rdx, QWORD PTR [rax + 152]+ test rdx, rdx+ jz .Lasm_IteratorClose.assert_failure_0+ mov eax, DWORD PTR [r14 + r13 + 24]+ mov ecx, DWORD PTR [r14 + r13 + 4]+ mov rcx, QWORD PTR [rbx + rcx * 8]+ shl rax, 3+ add rdx, rax+ mov rdi, rcx+ mov rsi, rdx+ call CSYM(asm_try_get_by_id_cache)+ shr rax, 48+ and ax, 65534+ cmp ax, 32766+ jne .Lasm_IteratorClose.ssa_block_1+ add r13d, 32+ movzx eax, BYTE PTR [r14 + r13]+ jmp [r12 + rax * 8]+asm_handler_IteratorClose_hot_end:+.p2align 6asm_handler_IsCallable:mov eax, DWORD PTR [r14 + r13 + 8]@@ -16713,26 +16736,27 @@ asm_handler_ObjectPropertyIteratorNext_cold:movzx eax, BYTE PTR [r14 + r13]jmp [r12 + rax * 8]asm_handler_ObjectPropertyIteratorNext_cold_end:-.p2align 4-asm_handler_IteratorClose:+asm_handler_IteratorClose_cold:+# Cold paths for IteratorClose+.Lasm_IteratorClose.ssa_block_1:mov DWORD PTR [rbx + -64], r13dsub rsp, 16lea r11, [r14 + r13]mov r10d, DWORD PTR [r11 + 4]mov rax, QWORD PTR [rbx + r10 * 8]-.Lasm_IteratorClose.scalar_value_ready_0:+.Lasm_IteratorClose.scalar_value_ready_1:mov rcx, raxmov r10d, DWORD PTR [r11 + 8]mov rax, QWORD PTR [rbx + r10 * 8]-.Lasm_IteratorClose.scalar_value_ready_1:+.Lasm_IteratorClose.scalar_value_ready_2:mov r8, raxmov r10d, DWORD PTR [r11 + 12]mov rax, QWORD PTR [rbx + r10 * 8]-.Lasm_IteratorClose.scalar_value_ready_2:+.Lasm_IteratorClose.scalar_value_ready_3:mov r9, raxmov r10d, DWORD PTR [r11 + 20]mov rax, QWORD PTR [rbx + r10 * 8]-.Lasm_IteratorClose.scalar_value_ready_3:+.Lasm_IteratorClose.scalar_value_ready_4:mov QWORD PTR [rsp], raxmov rdi, QWORD PTR [rbp - 48]mov esi, r13d@@ -16740,21 +16764,21 @@ asm_handler_IteratorClose:call CSYM(asm_slow_path_iterator_close)add rsp, 16test rax, rax- js .Lasm_IteratorClose.scalar_result_done_4+ js .Lasm_IteratorClose.scalar_result_done_5bt rax, 32- jnc .Lasm_IteratorClose.scalar_result_done_4+ jnc .Lasm_IteratorClose.scalar_result_done_5mov r13d, eaxmovzx eax, BYTE PTR [r14 + r13]jmp [r12 + rax * 8]-.Lasm_IteratorClose.scalar_result_done_4:+.Lasm_IteratorClose.scalar_result_done_5:test rax, raxjs .Lexitbt rax, 32- jnc .Lasm_IteratorClose.slow_path_transfer_5+ jnc .Lasm_IteratorClose.slow_path_transfer_6mov r13d, eaxmovzx eax, BYTE PTR [r14 + r13]jmp [r12 + rax * 8]-.Lasm_IteratorClose.slow_path_transfer_5:+.Lasm_IteratorClose.slow_path_transfer_6:mov rcx, QWORD PTR [rbp - 48]mov rbx, QWORD PTR [rcx]add rbx, 128@@ -16763,10 +16787,7 @@ asm_handler_IteratorClose:mov r13d, eaxmovzx eax, BYTE PTR [r14 + r13]jmp [r12 + rax * 8]-asm_handler_IteratorClose_hot_end:-asm_handler_IteratorClose_cold:asm_handler_IteratorClose_cold_end:-.p2align 4asm_handler_IteratorNext:mov DWORD PTR [rbx + -64], r13d@@ -21659,6 +21680,8 @@ asm_assertion_failure_traps:ud2.Lasm_ObjectPropertyIteratorNext.assert_predicate_failure_65:ud2+.Lasm_IteratorClose.assert_failure_0:+ ud2.Lasm_LeavePrivateEnvironment.assert_predicate_failure_3:ud2.Lexit:diff --git a/Libraries/LibJS/Interpreter/interpreter.flap b/Libraries/LibJS/Interpreter/interpreter.flapindex cbb355391b6..5e5aa25fa01 100644--- a/Libraries/LibJS/Interpreter/interpreter.flap+++ b/Libraries/LibJS/Interpreter/interpreter.flap@@ -4555,7 +4555,22 @@ handler IteratorClose(completion_type: Completion::Type,completion_value: Operand,cache: PropertyLookupCacheIndex-) @cold = call_slow_path(iterator_close);+) {+ let slow = || @cold {+ call_slow_path(iterator_close);+ };++ # An iterator without a return method closes without doing anything, for every completion type: the+ # instruction that follows a close for a throw completion throws it again. The cache of the site+ # tells if the method is missing.+ let executable = exec_ctx.executable;+ let caches: u64 = alias(executable.property_lookup_caches);+ let cache_index: u64 = alias(load(cache));+ let cache_pointer = caches + cache_index * PROPERTY_LOOKUP_CACHE_SIZE;+ let return_method: Value = call_helper_with_two_arguments(asm_try_get_by_id_cache, load(iterator_object), cache_pointer);+ guard return_method is Value<Nullish> else slow;+ dispatch_next;+}handler IteratorNext(dst: out Operand,diff --git a/Libraries/LibJS/JIT/Rust/src/builder/checks.rs b/Libraries/LibJS/JIT/Rust/src/builder/checks.rsindex 2f9f58099cc..652e46fda94 100644--- a/Libraries/LibJS/JIT/Rust/src/builder/checks.rs+++ b/Libraries/LibJS/JIT/Rust/src/builder/checks.rs@@ -143,6 +143,7 @@ impl GraphBuilder<'_> {Instruction::GetLength { .. } => self.build_get_length(instruction),Instruction::GetById { .. } => self.build_get_by_id(instruction),Instruction::PutById { .. } => self.build_put_by_id(instruction),+ Instruction::IteratorClose { .. } => self.build_iterator_close(instruction),Instruction::GetGlobal { .. } => self.build_get_global(instruction),Instruction::SetGlobal { .. } => self.build_set_global(instruction),_ => match super::speculation::Operation::of(instruction) {diff --git a/Libraries/LibJS/JIT/Rust/src/builder/handling.rs b/Libraries/LibJS/JIT/Rust/src/builder/handling.rsindex c79f1fb5c69..15c1f95234d 100644--- a/Libraries/LibJS/JIT/Rust/src/builder/handling.rs+++ b/Libraries/LibJS/JIT/Rust/src/builder/handling.rs@@ -290,6 +290,10 @@ pub fn handling(opcode: OpCode) -> Handling {// Iteration over arrays.O::GetIterator | O::IteratorNextUnpack => EXPANDED,+ // Closing an iterator, which does nothing if the cache of the site+ // has no return method for it.+ O::IteratorClose => EXPANDED,+// Conversions that change nothing for objects, and for lengths that// are int32 values already.O::ToObject | O::ToLength => EXPANDED,@@ -338,7 +342,6 @@ pub fn handling(opcode: OpCode) -> Handling {| O::GetLengthWithThis| O::GetMethod| O::GetObjectPropertyIterator- | O::IteratorClose| O::IteratorNext| O::IteratorToArray| O::CacheObjectShapediff --git a/Libraries/LibJS/JIT/Rust/src/builder/named.rs b/Libraries/LibJS/JIT/Rust/src/builder/named.rsindex 566148c58c4..301550fedd8 100644--- a/Libraries/LibJS/JIT/Rust/src/builder/named.rs+++ b/Libraries/LibJS/JIT/Rust/src/builder/named.rs@@ -6,9 +6,11 @@//! Named property accesses that speculation does not cover, built as IR on//! the probes of the interpreter's property lookup caches: `GetById` and-//! `PutById` probe the cache of the instruction, and `GetLength` branches-//! on arrays, other objects (through the cache) and strings. What the-//! probes do not handle, like accessors, takes the instruction's slow path.+//! `PutById` probe the cache of the instruction, `GetLength` branches on+//! arrays, other objects (through the cache) and strings, and+//! `IteratorClose` probes the cache for the return method of the iterator.+//! What the probes do not handle, like accessors, takes the instruction's+//! slow path.use super::Flow;use super::GraphBuilder;@@ -56,6 +58,25 @@ impl GraphBuilder<'_> {Ok(Flow::Continue)}+ /// Builds `IteratorClose` (`Handling::Expanded`): an iterator without a+ /// return method closes without doing anything, for every completion+ /// type, since the instruction after a close for a throw completion+ /// throws it again.+ pub(super) fn build_iterator_close(&mut self, instruction: &Instruction) -> Result<Flow, CompileFailure> {+ let Instruction::IteratorClose {+ iterator_object, cache, ..+ } = *instruction+ else {+ unreachable!("only IteratorClose is built here");+ };+ let mut slow_paths = self.start_slow_paths(None)?;+ let iterator = self.read(iterator_object)?;+ let return_method = self.probe_property_cache(&mut slow_paths, iterator, cache);+ self.branch_to_slow_path(&mut slow_paths, BranchCondition::Nullish, vec![return_method], true);+ self.join_slow_paths(slow_paths, None);+ Ok(Flow::Continue)+ }+/// The property that cache `cache` of the instruction has for the value/// `object`, taking a slow path where it has none.fn probe_property_cache(&mut self, slow_paths: &mut SlowPaths, object: NodeId, cache: u32) -> NodeId {diff --git a/Tests/LibJS/JIT/ir/iterator-close.js b/Tests/LibJS/JIT/ir/iterator-close.jsnew file mode 100644index 00000000000..2db5f1be88d--- /dev/null+++ b/Tests/LibJS/JIT/ir/iterator-close.js@@ -0,0 +1,19 @@+// Closing an iterator probes the cache of the site for its return method,+// and runs the slow path only if the iterator has one.++function firstTwo(pair) {+ const [first, second] = pair;+ return first + second;+}+jit.prepare(firstTwo);+for (let i = 0; i < 10; ++i) firstTwo([1, 2, 3]);+jit.compile(firstTwo);+if (firstTwo([3, 4, 5]) !== 7) throw new Error("wrong sum");++// CHECK-FUNCTION: firstTwo+// CHECK-PASS: graph building+// CHECK: [[RETURN:v\d+]] = ProbePropertyCache cache {{\d+}} ([[ITERATOR:v\d+]])+// CHECK-NEXT: BranchTaggedEquals ([[RETURN]], #empty)+// CHECK: [[REFINED:v\d+]] = RefineNotTaggedEquals ([[RETURN]], #empty)+// CHECK-NEXT: BranchNullish ([[REFINED]])+// CHECK: CallSlowPath IteratorClose {{@\d+}} ([[ITERATOR]],