Skip to content

Truly independent web browser

LibJS/JIT: Inline callees that contain for-in loops

Andreas Kling4e81d460a604
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs b/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs
index a26b3d0d939..4d2809c7c42 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs
@@ -109,8 +109,7 @@ pub(super) struct CallbackTarget {
/// Whether the callee can be built as an inlined call: its frame must not be
/// needed in memory outside generic nodes, its environments must stay those
-/// of its function object, it must be fully compilable, and it must not step
-/// for-in loops.
+/// of its function object, and it must be fully compilable.
fn can_inline(callee: &Function<'_>, virtual_arguments: bool) -> bool {
if !callee.written_constants.is_empty() {
return false;
@@ -135,10 +134,7 @@ fn can_inline(callee: &Function<'_>, virtual_arguments: bool) -> bool {
| OpCode::CallDirectEvalWithArgumentArray
| OpCode::Debugger
);
- // NB: Building for-in loops leaves slots in frame memory, which
- // inlined frames do not have.
- let steps_for_in = opcode == OpCode::ObjectPropertyIteratorNext;
- !reads_frame_fields && !steps_for_in && !matches!(handling(opcode), Handling::Unsupported(_))
+ !reads_frame_fields && !matches!(handling(opcode), Handling::Unsupported(_))
})
}
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/property_iterator.rs b/Libraries/LibJS/JIT/Rust/src/builder/property_iterator.rs
index 561cbd3106b..f95343a426b 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/property_iterator.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/property_iterator.rs
@@ -8,6 +8,7 @@
use super::Flow;
use super::GraphBuilder;
+use super::SlotState;
use crate::CompileFailure;
use crate::bytecode::Operand;
use crate::code::Repr;
@@ -34,8 +35,14 @@ impl GraphBuilder<'_> {
let receiver = self.read(receiver)?;
let keys = self.read(keys)?;
let cursor_value = self.read(cursor)?;
- // NB: Once there are no more keys, the value stays what it was.
- let old_value = self.read(dst_value)?;
+ // NB: Once there are no more keys, the value stays what it was. It is
+ // dead before the instruction, which writes it, and inlined frames
+ // drop dead slots at merges: there it becomes undefined, as the
+ // slow path writes it once done.
+ let old_value = match self.peek(dst_value) {
+ SlotState::InMemory if self.function.is_virtual() => self.constant(value::UNDEFINED),
+ _ => self.read(dst_value)?,
+ };
let mut slow_paths = self.start_slow_paths(Some(Repr::Tagged))?;
let receiver = self.branch_to_slow_path(&mut slow_paths, BranchCondition::Object, vec![receiver], true);
self.branch_to_slow_path(
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/tests.rs b/Libraries/LibJS/JIT/Rust/src/builder/tests.rs
index b7c70516b20..f1904025377 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/tests.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/tests.rs
@@ -2107,26 +2107,39 @@ pub(crate) mod inlining {
&creates_environment,
function(true, false)
)));
+ }
- // A for-in loop, which leaves slots in frame memory.
- let for_in = assemble(|_| {
+ #[test]
+ fn for_in_loops_are_inlined() {
+ // function f(o) { for (k in o) {} return k; }
+ let callee = assemble(|label| {
vec![
Instruction::Enter,
+ Instruction::GetObjectPropertyIterator {
+ dst_keys: r(5),
+ dst_receiver: r(6),
+ object: a(0),
+ cache: 0,
+ },
+ Instruction::Mov { dst: r(7), src: c(0) },
Instruction::ObjectPropertyIteratorNext {
- dst_value: r(5),
- dst_done: r(6),
- receiver: a(0),
- keys: a(0),
+ dst_value: l(0),
+ dst_done: l(1),
+ receiver: r(6),
+ keys: r(5),
cursor: r(7),
},
- Instruction::Return { value: r(5) },
+ Instruction::JumpIf {
+ condition: l(1),
+ true_target: label(5),
+ false_target: label(3),
+ },
+ Instruction::Return { value: l(0) },
]
});
- assert!(!is_inlined(&snapshot_with_callee(
- &caller(),
- &for_in,
- function(true, false)
- )));
+ let dump = dump_inlined(&caller(), &callee, function(true, false));
+ assert!(!dump.contains("CallSlowPath Call"), "{dump}");
+ assert!(dump.contains("LoadPropertyIteratorKey"), "{dump}");
}
#[test]
diff --git a/Tests/LibJS/JIT/ir/for-in-keys.js b/Tests/LibJS/JIT/ir/for-in-keys.js
index 6894732e7ac..818c9cc4c67 100644
--- a/Tests/LibJS/JIT/ir/for-in-keys.js
+++ b/Tests/LibJS/JIT/ir/for-in-keys.js
@@ -29,3 +29,25 @@ jit.compile(keys);
// CHECK-NEXT: SlowPathOutput 2 ([[CALL]])
console.log(keys({ x: 1, y: 2 }));
+
+// Callees with for-in loops are inlined, and their loops are built the same way.
+
+function countKeys(o) {
+ let n = 0;
+ for (const k in o) ++n;
+ return n;
+}
+function countKeysOfBoth(a, b) {
+ return countKeys(a) + countKeys(b);
+}
+jit.prepare(countKeys);
+jit.prepare(countKeysOfBoth);
+for (let i = 0; i < 10; ++i) countKeysOfBoth({ a: 1 }, { b: 2, c: 3 });
+jit.compile(countKeysOfBoth);
+
+// CHECK-FUNCTION: countKeysOfBoth
+// CHECK-PASS: frame initialization
+// CHECK-NOT: CallDirect
+// CHECK-COUNT-2: LoadPropertyIteratorKeyCount
+
+console.log(countKeysOfBoth({ x: 1 }, { y: 2, z: 3 }));
diff --git a/Tests/LibJS/Runtime/jit/inlined-for-in-loops.js b/Tests/LibJS/Runtime/jit/inlined-for-in-loops.js
new file mode 100644
index 00000000000..84905a99287
--- /dev/null
+++ b/Tests/LibJS/Runtime/jit/inlined-for-in-loops.js
@@ -0,0 +1,94 @@
+// Callees with for-in loops are inlined into their callers. These tests run
+// every caller and callee many times, so that with a low JIT threshold the
+// callees are inlined, and then make the loops take their slow paths.
+
+const iterations = 100;
+
+describe("for-in loops in inlined callees", () => {
+ test("keys of objects of the shape the loop saw, and of others", () => {
+ function keys(object) {
+ let result = "";
+ for (const key in object) result += key;
+ return result;
+ }
+ function caller(object) {
+ return keys(object) + "!";
+ }
+ for (let i = 0; i < iterations; ++i) expect(caller({ a: 1, b: 2 })).toBe("ab!");
+ expect(caller({ c: 1 })).toBe("c!");
+ expect(caller([5, 6])).toBe("01!");
+ expect(caller("xy")).toBe("01!");
+ expect(caller(null)).toBe("!");
+ for (let i = 0; i < iterations; ++i) expect(caller({ a: 1, b: 2 })).toBe("ab!");
+ });
+
+ test("the loop variable keeps the last key once the loop is done", () => {
+ function lastKey(object) {
+ let key;
+ for (key in object);
+ return key;
+ }
+ function caller(object) {
+ return lastKey(object);
+ }
+ for (let i = 0; i < iterations; ++i) expect(caller({ a: 1, b: 2 })).toBe("b");
+ expect(caller({})).toBeUndefined();
+ expect(caller({ c: 3 })).toBe("c");
+ });
+
+ test("properties deleted during the loop are skipped", () => {
+ function keysDeletingB(object) {
+ const result = [];
+ for (const key in object) {
+ result.push(key);
+ delete object.b;
+ }
+ return result;
+ }
+ function caller() {
+ return keysDeletingB({ a: 1, b: 2, c: 3 });
+ }
+ for (let i = 0; i < iterations; ++i) expect(caller()).toEqual(["a", "c"]);
+ });
+
+ test("exceptions in the loop body leave the inlined callee", () => {
+ function sum(object) {
+ let total = 0;
+ for (const key in object) total += object[key];
+ return total;
+ }
+ function caller(object) {
+ try {
+ return sum(object);
+ } catch (error) {
+ return error.message;
+ }
+ }
+ for (let i = 0; i < iterations; ++i) expect(caller({ a: i, b: 1 })).toBe(i + 1);
+ const throwing = {
+ a: 1,
+ get b() {
+ throw new Error("b");
+ },
+ };
+ expect(caller(throwing)).toBe("b");
+ for (let i = 0; i < iterations; ++i) expect(caller({ a: i, b: 1 })).toBe(i + 1);
+ });
+
+ test("a rarely called callee whose loop runs zero times", () => {
+ function rare() {
+ for (const key in {}) {
+ }
+ }
+ function lengthSquared(point, i) {
+ if (i % 10 === 0) rare();
+ return point.x * point.x + point.y * point.y;
+ }
+ function run(count) {
+ let sum = 0;
+ for (let i = 0; i < count; ++i) sum += lengthSquared({ x: i, y: 1 }, i);
+ return sum;
+ }
+ for (let i = 0; i < iterations; ++i) expect(run(20)).toBe(2490);
+ });
+});