Truly independent web browser
LibJS/JIT: Take methods of prototypes as watched constants
Andreas Kling2d7d70d1f841
diff --git a/Libraries/LibJS/JIT/ARCHITECTURE.md b/Libraries/LibJS/JIT/ARCHITECTURE.mdindex adea0c9f02d..ff1dc858e52 100644--- a/Libraries/LibJS/JIT/ARCHITECTURE.md+++ b/Libraries/LibJS/JIT/ARCHITECTURE.md@@ -359,11 +359,12 @@ compiled again once catches keep happening there, and the recompile calls the ca## 5. Dependencies and invalidationCompiled code may assume facts that hold until a rare runtime event (`code::Dependency`): a-prototype chain stays valid, a shape stays stable, a global binding or a property of the global-object is never assigned, the global declarative environment gets no bindings, no `[[IsHTMLDDA]]`-object exists. Each of the first and the last kinds is a `WatchpointSet` that stays valid-(`runtime/watchpoint_set.rs`), such as one of a realm for the iterator protocol-(`runtime/realm_watchpoints.rs`).+prototype chain stays valid, a shape stays stable, a prototype keeps the value of a property, a+global binding or a property of the global object is never assigned, the global declarative+environment gets no bindings, no `[[IsHTMLDDA]]` object exists. Each of the first, the third and+the last kinds is a `WatchpointSet` that stays valid (`runtime/watchpoint_set.rs`), such as one of+a realm for the iterator protocol (`runtime/realm_watchpoints.rs`) or the one of a prototype's slot+(`Shape::watch_slot_for_replacements()`).- The builder and check elimination record the dependencies they rely on in`Graph::dependencies`.@@ -441,6 +442,14 @@ before, loads of the binding stay plain loads; for a captured binding, that holdloads. Closures are counted where they are created (`SharedFunctionInstanceData::closure_count()`),so compiled code only copies closure templates of functions that have several closures already.+**Methods of prototypes.** A property lookup cache entry that found an object in a prototype (like+a method) is a constant to the builder. Where the prototype's shape watches the property's slot+(`PropertyCacheEntrySnapshot::prototype_property_watchpoint`), the code depends on the slot's+watchpoint set and loads nothing; otherwise it loads the property and checks it against the+constant. Slots that property lookup caches write to have no watchpoint set, and neither do slots+whose value was replaced while watched (`Shape::was_slot_replaced()`): the code their replacement+invalidated learned nothing, while a failed check is an exit that the recompile avoids.+**Polymorphic calls.** A call whose callee is a phi of a few function constants, like the methodsthat a `GetById` found for the shapes it switched on, inlines each of them (or calls the slowpath for those it cannot inline). Where the phi's block is a join of nothing but the edges thatdiff --git a/Libraries/LibJS/JIT/Rust/src/builder/caches.rs b/Libraries/LibJS/JIT/Rust/src/builder/caches.rsindex 1b44a8a193e..38962f44ddb 100644--- a/Libraries/LibJS/JIT/Rust/src/builder/caches.rs+++ b/Libraries/LibJS/JIT/Rust/src/builder/caches.rs@@ -91,6 +91,9 @@ pub(super) struct CacheEntry {/// The snapshot executable of the function `prototype_property` holds,/// if calls of it may be inlined.pub prototype_property_inline_executable: Option<u32>,+ /// The watchpoint set that stays valid while the prototype holds+ /// `prototype_property`, if there is one.+ pub prototype_property_watchpoint: Option<CellId>,}impl CacheEntry {@@ -165,6 +168,7 @@ impl CacheEntry {.prototype_property.map(|cell| (cell, entry.prototype_property_intrinsic)),prototype_property_inline_executable: entry.prototype_property_inline_executable,+ prototype_property_watchpoint: entry.prototype_property_watchpoint,})}diff --git a/Libraries/LibJS/JIT/Rust/src/builder/property_access.rs b/Libraries/LibJS/JIT/Rust/src/builder/property_access.rsindex 88a50cdd58b..96c30c83315 100644--- a/Libraries/LibJS/JIT/Rust/src/builder/property_access.rs+++ b/Libraries/LibJS/JIT/Rust/src/builder/property_access.rs@@ -186,6 +186,14 @@ impl GraphBuilder<'_> {if let Some(index) = entry.prototype_property_inline_executable {self.inlinable_functions.insert(bits, index);}+ // NB: While the watchpoint set is valid, the prototype holds+ // the object in the property, which needs no load.+ if let Some(watchpoint) = entry.prototype_property_watchpoint {+ self.embed(watchpoint);+ self.embed(expected);+ self.assume(Dependency::WatchpointSetValid(watchpoint));+ return self.constant(bits);+ }self.load_constant_named(holder, offset, expected)}_ => self.load_named(holder, offset),diff --git a/Libraries/LibJS/JIT/Rust/src/builder/tests.rs b/Libraries/LibJS/JIT/Rust/src/builder/tests.rsindex da3b289d2f9..15cb7cc830f 100644--- a/Libraries/LibJS/JIT/Rust/src/builder/tests.rs+++ b/Libraries/LibJS/JIT/Rust/src/builder/tests.rs@@ -839,6 +839,7 @@ pub(crate) mod property_access {prototype_property: None,prototype_property_intrinsic: None,prototype_property_inline_executable: None,+ prototype_property_watchpoint: None,accessor_function: None,holds_accessor: false,}diff --git a/Libraries/LibJS/JIT/Rust/src/snapshot.rs b/Libraries/LibJS/JIT/Rust/src/snapshot.rsindex 61968d1a859..2ce925df457 100644--- a/Libraries/LibJS/JIT/Rust/src/snapshot.rs+++ b/Libraries/LibJS/JIT/Rust/src/snapshot.rs@@ -691,6 +691,12 @@ pub struct PropertyCacheEntrySnapshot {/// it is a function JIT code may inline: code that calls the property's/// value as a constant can inline the call without feedback for it.pub prototype_property_inline_executable: Option<u32>,+ /// The `WatchpointSet` cell that stays valid while `prototype` keeps+ /// `prototype_property` in the property and its shape (see+ /// `Shape::watch_slot_for_replacements()` in the runtime), if there is+ /// one: code that depends on it takes the property as a constant+ /// without checking it.+ pub prototype_property_watchpoint: Option<CellId>,/// The function of the accessor the access calls: for `GetPropertyInPrototypeChain` entries, the getter of the/// accessor `prototype` held in the property when the snapshot was/// taken, for `ChangePropertyInPrototypeChain` entries its setter, anddiff --git a/Libraries/LibJS/Rust/src/jit/snapshot.rs b/Libraries/LibJS/Rust/src/jit/snapshot.rsindex fc8369598bc..50d78739ceb 100644--- a/Libraries/LibJS/Rust/src/jit/snapshot.rs+++ b/Libraries/LibJS/Rust/src/jit/snapshot.rs@@ -467,6 +467,18 @@ impl SnapshotBuilder<'_> {prototype_property_inline_executable: prototype_property.and_then(|object| {self.inline_executable_index(Some(Value::from_object(object).as_cell()), caller)}),+ // NB: Watching a slot allocates, which collects no garbage while the snapshot is taken. Slots+ // replaced while watched are checked instead, as invalidated code learns nothing, while the+ // check learns from its exit.+ prototype_property_watchpoint: prototype_property+ .and(entry.prototype)+ .filter(|holder| !holder.shape().was_slot_replaced(entry.property_offset))+ .and_then(|holder| {+ holder+ .shape()+ .watch_slot_for_replacements(self.vm, entry.property_offset)+ })+ .map(|set| self.cell(set)),accessor_function,holds_accessor: entry_holds_accessor(&entry),});diff --git a/Libraries/LibJS/Rust/src/runtime/shape.rs b/Libraries/LibJS/Rust/src/runtime/shape.rsindex 8318dc0b82f..e70722cde24 100644--- a/Libraries/LibJS/Rust/src/runtime/shape.rs+++ b/Libraries/LibJS/Rust/src/runtime/shape.rs@@ -91,6 +91,8 @@ struct RareData {/// What is known of the replacement of the values in the slots of the object that has this prototype shape, by/// offset (see `Shape::watch_slot_for_replacements()`).watched_slots: HashMap<u32, WatchedSlot, RandomState>,+ /// The offsets of the slots whose value changed while watched (see `Shape::was_slot_replaced()`).+ replaced_slots: Vec<u32>,}/// What a prototype shape knows of the replacement of the value in one of the slots of its object.@@ -1093,6 +1095,16 @@ impl Shape {})}+ /// Whether the value in slot `offset` of the object of this prototype shape changed while something relied on it,+ /// which compiled code takes as a sign that it may change again, rather than rely on it once more.+ pub fn was_slot_replaced(&self, offset: u32) -> bool {+ self.storage+ .rare_data+ .borrow()+ .as_ref()+ .is_some_and(|rare_data| rare_data.replaced_slots.contains(&offset))+ }+/// Notes that the value in slot `offset` of the object of this prototype shape changes, which fires the watchpoint/// set of the slot.pub fn note_slot_replacement(&self, vm: &Vm, offset: u32) {@@ -1108,6 +1120,9 @@ impl Shape {Some(WatchedSlot::Watched(watchpoint_set)) => {let watchpoint_set = *watchpoint_set;rare_data.watched_slots.remove(&offset);+ if !rare_data.replaced_slots.contains(&offset) {+ rare_data.replaced_slots.push(offset);+ }watchpoint_set}Some(WatchedSlot::WrittenByCaches) | None => return,diff --git a/Tests/LibJS/JIT/ir/has-property.js b/Tests/LibJS/JIT/ir/has-property.jsindex c263cd9e0ce..c0f299c0fe8 100644--- a/Tests/LibJS/JIT/ir/has-property.js+++ b/Tests/LibJS/JIT/ir/has-property.js@@ -11,7 +11,7 @@ jit.compile(own);// CHECK-FUNCTION: own// CHECK-PASS: frame initialization-// CHECK: CheckValue+// CHECK: CheckShape// CHECK: [[HAS:v[0-9]+]] = ProbeHasOwnProperty ([[O:v[0-9]+]], [[K:v[0-9]+]])// CHECK-NEXT: BranchTaggedEquals ([[HAS]], #empty)// CHECK: cold:diff --git a/Tests/LibJS/Runtime/jit/polymorphic-method-inlining.js b/Tests/LibJS/Runtime/jit/polymorphic-method-inlining.jsindex 0f303c3f958..c77218a4251 100644--- a/Tests/LibJS/Runtime/jit/polymorphic-method-inlining.js+++ b/Tests/LibJS/Runtime/jit/polymorphic-method-inlining.js@@ -218,3 +218,25 @@ describe("inlined methods of callees copied to other registers", () => {for (let i = 0; i < iterations; ++i) expect(call(objects[i % 2])).toBe(i % 2 ? 2 + 2 + 20 : 1 + 1 + 10);});});++describe("methods replaced again and again", () => {+ test("methods redefined on a prototype many times", () => {+ class A {+ value() {+ return 0;+ }+ }+ function call(object) {+ return object.value();+ }+ const object = new A();+ for (let round = 1; round <= 20; ++round) {+ Object.defineProperty(A.prototype, "value", {+ value: () => round,+ writable: true,+ configurable: true,+ });+ for (let i = 0; i < iterations; ++i) expect(call(object)).toBe(round);+ }+ });+});