Skip to content

Truly independent web browser

LibJS: Skip the slow path when converting primitive property keys

Tim Ledbetter05b6d2da348b
diff --git a/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S b/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S
index 8ba2117fd7c..7287dd05203 100644
--- a/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S
+++ b/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S
@@ -4149,6 +4149,22 @@ asm_handler_ThrowIfNullish:
br x10
asm_handler_ThrowIfNullish_hot_end:
+.p2align 4
+asm_handler_ToPrimitiveWithStringHint:
+ ldr w0, [x21, #8]
+ ldr x0, [x27, x0, lsl #3]
+ lsr x9, x0, #48
+ mov w10, #65529
+ cmp x9, x10
+ b.eq .Lasm_ToPrimitiveWithStringHint.ssa_block_1
+ ldr w9, [x21, #4]
+ str x0, [x27, x9, lsl #3]
+ ldrb w9, [x21, #16]
+ add x21, x21, #16
+ ldr x10, [x19, x9, lsl #3]
+ br x10
+asm_handler_ToPrimitiveWithStringHint_hot_end:
+
.p2align 4
asm_handler_BitwiseNot:
ldr w0, [x21, #8]
@@ -12433,8 +12449,9 @@ asm_handler_ToString_hot_end:
asm_handler_ToString_cold:
asm_handler_ToString_cold_end:
-.p2align 4
-asm_handler_ToPrimitiveWithStringHint:
+asm_handler_ToPrimitiveWithStringHint_cold:
+// Cold paths for ToPrimitiveWithStringHint
+.Lasm_ToPrimitiveWithStringHint.ssa_block_1:
mov x0, x20
sub w1, w21, w26
str w1, [x28, #64]
@@ -12446,8 +12463,12 @@ asm_handler_ToPrimitiveWithStringHint:
sub w1, w21, w26
mov x2, x21
bl CSYM(asm_slow_path_to_primitive_with_string_hint)
- tbnz x0, #63, .Lasm_ToPrimitiveWithStringHint.scalar_result_done_1
- tbz x0, #32, .Lasm_ToPrimitiveWithStringHint.scalar_result_done_1
+ tbz x0, #63, 1f
+ b .Lasm_ToPrimitiveWithStringHint.scalar_result_done_1
+1:
+ tbnz x0, #32, 1f
+ b .Lasm_ToPrimitiveWithStringHint.scalar_result_done_1
+1:
ldr w9, [x21, #4]
str x1, [x27, x9, lsl #3]
add x21, x26, w0, uxtw
@@ -12458,7 +12479,9 @@ asm_handler_ToPrimitiveWithStringHint:
tbz x0, #63, 1f
b .Lexit
1:
- tbz x0, #32, .Lasm_ToPrimitiveWithStringHint.slow_path_transfer_2
+ tbnz x0, #32, 1f
+ b .Lasm_ToPrimitiveWithStringHint.slow_path_transfer_2
+1:
add x21, x26, w0, uxtw
ldrb w9, [x21]
ldr x10, [x19, x9, lsl #3]
@@ -12472,10 +12495,7 @@ asm_handler_ToPrimitiveWithStringHint:
ldrb w9, [x21]
ldr x10, [x19, x9, lsl #3]
br x10
-asm_handler_ToPrimitiveWithStringHint_hot_end:
-asm_handler_ToPrimitiveWithStringHint_cold:
asm_handler_ToPrimitiveWithStringHint_cold_end:
-
.p2align 4
asm_handler_ToObject:
mov x0, x20
diff --git a/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S b/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S
index dcd9a43ce2b..9e0ffd79027 100644
--- a/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S
+++ b/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S
@@ -4160,6 +4160,21 @@ asm_handler_ThrowIfNullish:
jmp [r12 + rax * 8]
asm_handler_ThrowIfNullish_hot_end:
+.p2align 6
+asm_handler_ToPrimitiveWithStringHint:
+ mov eax, DWORD PTR [r14 + r13 + 8]
+ mov rax, QWORD PTR [rbx + rax * 8]
+ mov r11, rax
+ shr r11, 48
+ cmp r11w, 65529
+ je .Lasm_ToPrimitiveWithStringHint.ssa_block_1
+ mov r11d, DWORD PTR [r14 + r13 + 4]
+ mov QWORD PTR [rbx + r11 * 8], rax
+ add r13d, 16
+ movzx eax, BYTE PTR [r14 + r13]
+ jmp [r12 + rax * 8]
+asm_handler_ToPrimitiveWithStringHint_hot_end:
+
.p2align 6
asm_handler_BitwiseNot:
mov eax, DWORD PTR [r14 + r13 + 8]
@@ -12209,8 +12224,9 @@ asm_handler_ToString_hot_end:
asm_handler_ToString_cold:
asm_handler_ToString_cold_end:
-.p2align 4
-asm_handler_ToPrimitiveWithStringHint:
+asm_handler_ToPrimitiveWithStringHint_cold:
+# Cold paths for ToPrimitiveWithStringHint
+.Lasm_ToPrimitiveWithStringHint.ssa_block_1:
mov DWORD PTR [rbx + -64], r13d
lea r11, [r14 + r13]
mov r10d, DWORD PTR [r11 + 8]
@@ -12248,10 +12264,7 @@ asm_handler_ToPrimitiveWithStringHint:
mov r13d, eax
movzx eax, BYTE PTR [r14 + r13]
jmp [r12 + rax * 8]
-asm_handler_ToPrimitiveWithStringHint_hot_end:
-asm_handler_ToPrimitiveWithStringHint_cold:
asm_handler_ToPrimitiveWithStringHint_cold_end:
-
.p2align 4
asm_handler_ToObject:
mov DWORD PTR [rbx + -64], r13d
diff --git a/Libraries/LibJS/Interpreter/interpreter.flap b/Libraries/LibJS/Interpreter/interpreter.flap
index 16e5b4509ce..d984c9b6904 100644
--- a/Libraries/LibJS/Interpreter/interpreter.flap
+++ b/Libraries/LibJS/Interpreter/interpreter.flap
@@ -2559,8 +2559,15 @@ handler ToString(
handler ToPrimitiveWithStringHint(
dst: out Operand,
- value: Operand
-) @cold = call_slow_path(to_primitive_with_string_hint);
+ value: in Operand
+) {
+ let primitive = load(value);
+ guard primitive is not Value<Object> else @cold {
+ call_slow_path(to_primitive_with_string_hint);
+ };
+ store(dst, primitive);
+ dispatch_next;
+}
handler ToObject(
dst: out Operand,
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/checks.rs b/Libraries/LibJS/JIT/Rust/src/builder/checks.rs
index 9eeb0be188b..14e3f3b9a4e 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/checks.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/checks.rs
@@ -71,9 +71,12 @@ impl GraphBuilder<'_> {
let value = self.read(src)?;
self.build_throwing_check(BranchCondition::Nullish, vec![value], true)
}
- Instruction::ToObject { dst, value } => self.build_conversion(BranchCondition::Object, dst, value),
+ Instruction::ToObject { dst, value } => self.build_conversion(BranchCondition::Object, true, dst, value),
Instruction::ToLength { dst, value } => {
- self.build_conversion(BranchCondition::NonNegativeInt32, dst, value)
+ self.build_conversion(BranchCondition::NonNegativeInt32, true, dst, value)
+ }
+ Instruction::ToPrimitiveWithStringHint { dst, value } => {
+ self.build_conversion(BranchCondition::Object, false, dst, value)
}
Instruction::ResolveThisBinding => self.build_resolve_this_binding(),
Instruction::GetIterator {
@@ -172,18 +175,19 @@ impl GraphBuilder<'_> {
Ok(self.refine(condition, false, vec![value, empty]))
}
- /// Writes `src` to `dst` where `condition` holds of it, and the result
- /// of the instruction's slow path, in a cold block, otherwise.
+ /// Writes `src` to `dst` where `condition` of it is `unchanged_when`, and
+ /// the result of the instruction's slow path, in a cold block, otherwise.
fn build_conversion(
&mut self,
condition: BranchCondition,
+ unchanged_when: bool,
dst: Operand,
src: Operand,
) -> Result<Flow, CompileFailure> {
let frame_state = self.eager_frame_state();
let value = self.read(src)?;
let slow_path_inputs = self.slow_path_inputs()?;
- let (hot, cold) = self.branch_with_cold_side(condition, vec![value], true);
+ let (hot, cold) = self.branch_with_cold_side(condition, vec![value], unchanged_when);
self.block = cold;
let converted = self.emit_call_slow_path(slow_path_inputs, frame_state, Some(Repr::Tagged));
self.join_blocks(vec![hot, cold]);
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/handling.rs b/Libraries/LibJS/JIT/Rust/src/builder/handling.rs
index 392f47c9a7c..5209fa1a944 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/handling.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/handling.rs
@@ -273,9 +273,9 @@ pub fn handling(opcode: OpCode) -> Handling {
// 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,
+ // Conversions that change nothing for objects, for lengths that are
+ // int32 values already, and for primitives.
+ O::ToObject | O::ToLength | O::ToPrimitiveWithStringHint => EXPANDED,
// Concatenations of strings, and of a string and an integer.
O::ConcatString => EXPANDED,
@@ -314,7 +314,6 @@ pub fn handling(opcode: OpCode) -> Handling {
| O::ThrowNotAFunction
| O::Throw
| O::ToString
- | O::ToPrimitiveWithStringHint
| O::DynamicGetCalleeAndThisFromEnvironment
| O::GetByIdWithThis
| O::PutByIdWithThis
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs b/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs
index 97333f75936..9f7bd04455b 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs
@@ -252,6 +252,7 @@ fn marker_runtime() -> RuntimeInfo {
O::ToInt32,
O::ToObject,
O::ToLength,
+ O::ToPrimitiveWithStringHint,
] {
set(opcode, marker_scalar as *const () as u64);
}
@@ -678,6 +679,13 @@ fn unary_operations() {
} else {
MARKER
};
+ let to_primitive = if (value >> 48) as u16 == value::OBJECT_TAG {
+ MARKER
+ } else if value == value::EMPTY {
+ value::UNDEFINED
+ } else {
+ value
+ };
let (dst, src) = (r(5), argument(0));
for (instruction, expected) in [
(
@@ -714,6 +722,7 @@ fn unary_operations() {
),
(Instruction::ToObject { dst, value: src }, to_object),
(Instruction::ToLength { dst, value: src }, to_length),
+ (Instruction::ToPrimitiveWithStringHint { dst, value: src }, to_primitive),
] {
let (result, _) = run_instruction_with(feedback, instruction.clone(), &[value, value::UNDEFINED]);
assert_eq!(
diff --git a/Tests/LibJS/JIT/ir/computed-key-conversions.js b/Tests/LibJS/JIT/ir/computed-key-conversions.js
new file mode 100644
index 00000000000..d114844010e
--- /dev/null
+++ b/Tests/LibJS/JIT/ir/computed-key-conversions.js
@@ -0,0 +1,18 @@
+// The key conversion of a compound assignment calls into the runtime only for objects, in a cold block. For an
+// int32 key, the check goes away.
+
+function increment(array, n) {
+ for (let i = 0; i < n; ++i) array[i] += 1;
+ return array;
+}
+jit.prepare(increment);
+for (let i = 0; i < 10; ++i) increment([0, 0, 0, 0], 4);
+jit.compile(increment);
+
+// CHECK-FUNCTION: increment
+// CHECK-PASS: graph building
+// CHECK: BranchObject ([[KEY:v[0-9]+]]) -> [[COLD:b[0-9]+]], {{b[0-9]+}}
+// CHECK: [[COLD]] <- {{b[0-9]+}} cold:
+// CHECK-NEXT: CallSlowPath ToPrimitiveWithStringHint {{@[0-9]+}} ([[KEY]])
+// CHECK-PASS: frame initialization
+// CHECK-NOT: ToPrimitiveWithStringHint