Skip to content

Truly independent web browser

LibJS/JIT: Exit instead of rejoining the slow path of typed array length

Andreas Kling2254e5c7609b
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/property_access.rs b/Libraries/LibJS/JIT/Rust/src/builder/property_access.rs
index 65e7c339855..62d719d59e6 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/property_access.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/property_access.rs
@@ -297,7 +297,8 @@ impl GraphBuilder<'_> {
/// `%TypedArray%.prototype.length` on the same prototype: the length the
/// typed array caches, after checking the getter and that the object is
/// a typed array. Typed arrays without a cached length (empty, detached
- /// or viewing a resizable buffer) take the instruction's slow path.
+ /// or viewing a resizable buffer) exit, or take the instruction's slow
+ /// path where that happened before.
pub(super) fn try_build_typed_array_length(
&mut self,
dst: Operand,
@@ -345,6 +346,9 @@ impl GraphBuilder<'_> {
return Ok(false);
}
let mut slow_paths = self.start_slow_paths(Some(Repr::Tagged))?;
+ // NB: Exiting instead of running the slow path keeps loops bounded by the length free of code that may
+ // change what they read.
+ self.exit_instead_of_slow_path(&mut slow_paths);
let (object, checked_getter) = self.check_accessor_call(base, &entries, AccessorPart::Getter)?;
debug_assert!(checked_getter.is_none(), "all entries have the same checked getter");
let typed_array = self.emit_checked(Op::CheckTypedArray, vec![object], Some(Repr::Tagged));
diff --git a/Tests/LibJS/JIT/ir/typed-array-length.js b/Tests/LibJS/JIT/ir/typed-array-length.js
index 076ec991c10..824b23912c2 100644
--- a/Tests/LibJS/JIT/ir/typed-array-length.js
+++ b/Tests/LibJS/JIT/ir/typed-array-length.js
@@ -39,3 +39,31 @@ if (polymorphicLengthOf(new Float64Array(2)) !== 2) throw new Error("wrong lengt
// CHECK: CheckAccessorFunction
// CHECK-NEXT: CheckTypedArray
// CHECK-NEXT: LoadTypedArrayLength
+
+// The rare cases of the length (an empty, detached or out-of-bounds typed array) exit instead of running the slow
+// path, so a loop bounded by the length runs no code that could change it, and its checks move out of the loop.
+function countEqual(left, right) {
+ let count = 0;
+ for (let i = 0; i < left.length; ++i) {
+ if (left[i] === right[i]) ++count;
+ }
+ return count;
+}
+const left = new Uint8Array([1, 2, 3, 4]);
+const right = new Uint8Array([1, 0, 3, 0]);
+jit.prepare(countEqual);
+for (let i = 0; i < 10; ++i) countEqual(left, right);
+jit.compile(countEqual);
+if (countEqual(left, right) !== 2) throw new Error("wrong count");
+if (countEqual(new Uint8Array(0), right) !== 0) throw new Error("wrong count of an empty typed array");
+
+// CHECK-FUNCTION: countEqual
+// CHECK-PASS: graph building
+// CHECK: BranchInt32GreaterThan ({{v[0-9]+}}, #i32:0) -> {{b[0-9]+}}, [[COLD:b[0-9]+]]
+// CHECK: [[COLD]] <- {{b[0-9]+}} cold:
+// CHECK-NEXT: Exit SlowPath
+// CHECK-NOT: CallSlowPath GetLength
+// CHECK-PASS: loop invariant code motion
+// CHECK: CheckAccessorFunction
+// CHECK: loop:
+// CHECK-NOT: CheckAccessorFunction
diff --git a/Tests/LibJS/Runtime/jit/typed-array-length.js b/Tests/LibJS/Runtime/jit/typed-array-length.js
index 044d76b1cba..1e6ac0ca012 100644
--- a/Tests/LibJS/Runtime/jit/typed-array-length.js
+++ b/Tests/LibJS/Runtime/jit/typed-array-length.js
@@ -17,9 +17,13 @@ test("typed arrays of several kinds read their length", () => {
expect(jit.exitCount(lengthOf)).toBe(0);
});
-test("typed arrays without a cached length take the slow path", () => {
+test("typed arrays without a cached length exit, then take the slow path", () => {
const lengthOf = compileLength();
expect(lengthOf(new Uint8Array(0))).toBe(0);
+ if (jit.enabled && !jit.forcesExits) expect(jit.exitCount(lengthOf)).toBe(1);
+ jit.discard(lengthOf);
+ expect(jit.compile(lengthOf)).toBe(jit.enabled);
+ expect(lengthOf(new Uint8Array(0))).toBe(0);
const detached = new Uint8Array(new ArrayBuffer(8));
detached.buffer.transfer();
@@ -33,7 +37,8 @@ test("typed arrays without a cached length take the slow path", () => {
const fixed = new Uint8Array(buffer, 8, 4);
buffer.resize(10);
expect(lengthOf(fixed)).toBe(0);
- expect(jit.exitCount(lengthOf)).toBe(0);
+ // NB: An exit forced before the one of the empty typed array leaves the site unrecorded, so the code exits again.
+ if (!jit.forcesExits) expect(jit.exitCount(lengthOf)).toBe(0);
});
test("objects that inherit from a typed array prototype call the getter", () => {