Skip to content

Truly independent web browser

LibJS/JIT: Skip accessor checks on loads of writable properties

Andreas Klingfb069179b1b4
diff --git a/Libraries/LibJS/JIT/ARCHITECTURE.md b/Libraries/LibJS/JIT/ARCHITECTURE.md
index ff1dc858e52..8d5fe3a2426 100644
--- a/Libraries/LibJS/JIT/ARCHITECTURE.md
+++ b/Libraries/LibJS/JIT/ARCHITECTURE.md
@@ -167,8 +167,8 @@ Exits: E = eager, L = lazy. Every node that can exit has a frame state. "Call" m
| AssumeValid | Exits if the code was invalidated; a patchable no-op (section 5). | E | Any | | |
| AssumeLegacyArgumentsUnread | After an instruction whose calls may run code reading the legacy `arguments` of inlined frames; escape analysis makes it an `AssumeValid` or removes it (section 4, "Legacy arguments"). | E | Any | | |
| **Object memory** | | | | | |
-| LoadNamed | A named property at an offset; exits for accessors. | E | Named | | cse |
-| StoreNamed | Stores an existing named property; exits for accessors. | E | Named | Named | |
+| LoadNamed | A named property at an offset; exits for accessors, unless the shape check of its object input ensures that it `holds_data` (the own property is writable in the checked shape, and accessor properties never are). Loads from prototypes always check: no check guards their constant object as an input, so they could move above their guards. | E unless `holds_data` | Named | | cse |
+| StoreNamed | Stores an existing named property; exits for accessors, unless it `holds_data` like a `LoadNamed`. | E unless `holds_data` | Named | Named | |
| AddNamed | Adds a property to a fresh object and gives it the new shape. | | Named | Named | |
| LoadAccessorFunction | The getter or setter of an accessor property. | E | Named | | |
| HasInPrototypeChain | Whether input 1 is on input 0's prototype chain (the OrdinaryHasInstance loop). | E | Named | | |
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/caches.rs b/Libraries/LibJS/JIT/Rust/src/builder/caches.rs
index 38962f44ddb..ca9dede44f8 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/caches.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/caches.rs
@@ -82,6 +82,9 @@ pub(super) struct CacheEntry {
pub holds_accessor: bool,
/// Whether a `Set` writes a data property the object has.
pub writes_data_property: bool,
+ /// Whether the property holds data while its holder keeps its shape (see
+ /// `PropertyCacheEntrySnapshot::property_holds_data`).
+ pub property_holds_data: bool,
/// The function of the accessor the property held, which an access may
/// call inline.
pub accessor_function: Option<AccessorFunctionSnapshot>,
@@ -163,6 +166,7 @@ impl CacheEntry {
action,
holds_accessor: entry.holds_accessor,
writes_data_property: entry.writes_data_property,
+ property_holds_data: entry.property_holds_data,
accessor_function: entry.accessor_function,
prototype_property: entry
.prototype_property
@@ -273,7 +277,11 @@ impl GraphBuilder<'_> {
) -> Option<NodeId> {
let entries = self.entries_for_known_shape(object, entries);
let shapes = entries.iter().map(|entry| self.shape_guard(entry)).collect::<Vec<_>>();
- let one_action = entries.iter().all(|entry| entry.action == entries[0].action);
+ // NB: The action applies the first entry's, so entries that differ in whether the property holds data do not
+ // share one.
+ let one_action = entries.iter().all(|entry| {
+ entry.action == entries[0].action && entry.property_holds_data == entries[0].property_holds_data
+ });
self.dispatch_on_shapes(object, entries, &shapes, one_action, apply)
}
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/property_access.rs b/Libraries/LibJS/JIT/Rust/src/builder/property_access.rs
index 96c30c83315..08bf1e4e261 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/property_access.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/property_access.rs
@@ -150,13 +150,34 @@ impl GraphBuilder<'_> {
}
pub(super) fn load_named(&mut self, object: NodeId, offset: u32) -> NodeId {
+ self.load_named_holding(object, offset, false)
+ }
+
+ /// Loads the named property at `offset` of `object`, which checks before
+ /// ensure holds data if `holds_data`.
+ pub(super) fn load_named_holding(&mut self, object: NodeId, offset: u32, holds_data: bool) -> NodeId {
let address = self.cell_address(object);
- self.emit_checked(Op::LoadNamed { offset }, vec![object, address], Some(Repr::Tagged))
+ let op = Op::LoadNamed { offset, holds_data };
+ if holds_data {
+ return self.emit(op, vec![object, address], Some(Repr::Tagged));
+ }
+ self.emit_checked(op, vec![object, address], Some(Repr::Tagged))
}
pub(super) fn store_named(&mut self, object: NodeId, offset: u32, value: NodeId) {
+ self.store_named_holding(object, offset, value, false);
+ }
+
+ /// Stores into the named property at `offset` of `object`, which checks
+ /// before ensure holds data if `holds_data`.
+ pub(super) fn store_named_holding(&mut self, object: NodeId, offset: u32, value: NodeId, holds_data: bool) {
let address = self.cell_address(object);
- self.emit_checked(Op::StoreNamed { offset }, vec![object, value, address], None);
+ let op = Op::StoreNamed { offset, holds_data };
+ if holds_data {
+ self.emit(op, vec![object, value, address], None);
+ return;
+ }
+ self.emit_checked(op, vec![object, value, address], None);
}
/// Loads the property a `Get` entry describes, from the object itself or
@@ -171,7 +192,7 @@ impl GraphBuilder<'_> {
valid,
} = holder
else {
- return self.load_named(object, offset);
+ return self.load_named_holding(object, offset, entry.property_holds_data);
};
self.check_prototype_chain(validity, valid);
self.embed(prototype);
@@ -769,7 +790,7 @@ impl GraphBuilder<'_> {
let Action::Set { offset, .. } = entry.action else {
unreachable!("only sets store");
};
- builder.store_named(object, offset, value);
+ builder.store_named_holding(object, offset, value, entry.property_holds_data);
None
});
Ok(true)
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/property_additions.rs b/Libraries/LibJS/JIT/Rust/src/builder/property_additions.rs
index 7e2615989b2..171c5274495 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/property_additions.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/property_additions.rs
@@ -110,7 +110,9 @@ impl GraphBuilder<'_> {
None,
);
}
- Action::Set { offset, .. } => builder.store_named(object, offset, value),
+ Action::Set { offset, .. } => {
+ builder.store_named_holding(object, offset, value, entry.property_holds_data);
+ }
Action::Get { .. } => unreachable!("additions only add and set"),
}
None
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/tests.rs b/Libraries/LibJS/JIT/Rust/src/builder/tests.rs
index 15cb7cc830f..28eaf6baab9 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/tests.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/tests.rs
@@ -841,6 +841,7 @@ pub(crate) mod property_access {
prototype_property_inline_executable: None,
prototype_property_watchpoint: None,
accessor_function: None,
+ property_holds_data: false,
holds_accessor: false,
}
}
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/mod.rs b/Libraries/LibJS/JIT/Rust/src/codegen/mod.rs
index 405b35d9075..3fd0e7f4f43 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/mod.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/mod.rs
@@ -1628,24 +1628,28 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
self.masm.move_imm64(scratch, function.0);
self.masm.branch64_memory(Condition::NotEqual, &field, scratch, exit);
}
- Op::LoadNamed { offset } => {
- let exit = self.exit_site(node_id);
+ Op::LoadNamed { offset, holds_data } => {
+ let exit = (!holds_data).then(|| self.exit_site(node_id));
let output = self.output(node_id);
let slot = self.named_property_address(output, *offset)?;
let cell = self.cell_address_of(node_id, output, 1);
let storage = Address::new(cell, self.runtime.offsets.object_named_properties as i32);
self.masm.load64(output, &storage);
- self.emit_exit_if_accessor(&slot, exit);
+ if let Some(exit) = exit {
+ self.emit_exit_if_accessor(&slot, exit);
+ }
self.masm.load64(output, &slot);
}
- Op::StoreNamed { offset } => {
- let exit = self.exit_site(node_id);
+ Op::StoreNamed { offset, holds_data } => {
+ let exit = (!holds_data).then(|| self.exit_site(node_id));
let storage = Gpr(self.allocation.node(node_id).temps[0]);
let slot = self.named_property_address(storage, *offset)?;
let cell = self.cell_address_of(node_id, storage, 2);
let storage_field = Address::new(cell, self.runtime.offsets.object_named_properties as i32);
self.masm.load64(storage, &storage_field);
- self.emit_exit_if_accessor(&slot, exit);
+ if let Some(exit) = exit {
+ self.emit_exit_if_accessor(&slot, exit);
+ }
self.masm.store64(&slot, self.input(node_id, 1));
}
Op::AddNamed { offset, shape, .. } => self.emit_add_named(node_id, *offset, *shape)?,
diff --git a/Libraries/LibJS/JIT/Rust/src/ir/dump.rs b/Libraries/LibJS/JIT/Rust/src/ir/dump.rs
index 06d09a73306..15a36c6373c 100644
--- a/Libraries/LibJS/JIT/Rust/src/ir/dump.rs
+++ b/Libraries/LibJS/JIT/Rust/src/ir/dump.rs
@@ -260,8 +260,12 @@ fn op_text(layout: &FrameLayout, op: &Op, inputs: &str) -> String {
Op::CheckAccessorFunction { offset, part, function } => {
with_inputs(&format!("CheckAccessorFunction +{offset} {part:?} {:#x}", function.0))
}
- Op::LoadNamed { offset } => with_inputs(&format!("LoadNamed +{offset}")),
- Op::StoreNamed { offset } => with_inputs(&format!("StoreNamed +{offset}")),
+ Op::LoadNamed { offset, holds_data } => {
+ with_inputs(&format!("LoadNamed +{offset}")) + if *holds_data { " data" } else { "" }
+ }
+ Op::StoreNamed { offset, holds_data } => {
+ with_inputs(&format!("StoreNamed +{offset}")) + if *holds_data { " data" } else { "" }
+ }
Op::AddNamed { offset, shape, .. } => with_inputs(&format!("AddNamed +{offset} to {:#x}", shape.0)),
Op::ShapeSwitch { cases } => {
let cases = cases
diff --git a/Libraries/LibJS/JIT/Rust/src/ir/mod.rs b/Libraries/LibJS/JIT/Rust/src/ir/mod.rs
index 82fb8197cf8..8f1989ac517 100644
--- a/Libraries/LibJS/JIT/Rust/src/ir/mod.rs
+++ b/Libraries/LibJS/JIT/Rust/src/ir/mod.rs
@@ -876,10 +876,12 @@ pub enum Op {
part: AccessorPart,
},
/// Loads the named property at `offset` of the object in input 0. Exits
- /// if the property holds an accessor. An optional input 1 is the
- /// object's `CellAddress`.
+ /// if the property holds an accessor, unless checks before it ensure
+ /// that it `holds_data`. An optional input 1 is the object's
+ /// `CellAddress`.
LoadNamed {
offset: u32,
+ holds_data: bool,
},
/// Adds a property to the object in input 0 (whose `CellAddress` is
/// input 2), made by an `AllocateObject` with room for it, which has the
@@ -893,10 +895,12 @@ pub enum Op {
property_count: u32,
},
/// Stores input 1 into the named property at `offset` of the object in
- /// input 0, which already exists. Exits if the property holds an accessor.
- /// An optional input 2 is the object's `CellAddress`.
+ /// input 0, which already exists. Exits if the property holds an accessor,
+ /// unless checks before it ensure that it `holds_data`. An optional input
+ /// 2 is the object's `CellAddress`.
StoreNamed {
offset: u32,
+ holds_data: bool,
},
// Control nodes. Each block ends with exactly one of these.
@@ -1858,8 +1862,8 @@ impl Op {
| Op::CheckPrototypeChainValid { .. }
| Op::LoadGlobalBinding { .. }
| Op::StoreGlobalBinding { .. }
- | Op::LoadNamed { .. }
- | Op::StoreNamed { .. }
+ | Op::LoadNamed { holds_data: false, .. }
+ | Op::StoreNamed { holds_data: false, .. }
| Op::ShapeSwitch { .. } => Some(ExitKind::BadShape),
_ => None,
}
diff --git a/Libraries/LibJS/JIT/Rust/src/passes/check_elimination.rs b/Libraries/LibJS/JIT/Rust/src/passes/check_elimination.rs
index b6f83d680d7..e1c39dd0a3d 100644
--- a/Libraries/LibJS/JIT/Rust/src/passes/check_elimination.rs
+++ b/Libraries/LibJS/JIT/Rust/src/passes/check_elimination.rs
@@ -505,7 +505,7 @@ fn visit_node(
};
facts.set_contents(location, input(1));
}
- Op::StoreNamed { offset } => {
+ Op::StoreNamed { offset, .. } => {
let (object, value) = (input(0), input(1));
facts.forget_named_properties_at(offset);
facts.set_contents(Location::Named { object, offset }, value);
@@ -609,7 +609,7 @@ fn loaded_location(graph: &Graph, decisions: &Decisions, load: NodeId) -> Option
let node = graph.node(load);
let input = |index: usize| decisions.value(graph, node.inputs[index]);
Some(match node.op {
- Op::LoadNamed { offset } => Location::Named {
+ Op::LoadNamed { offset, .. } => Location::Named {
object: input(0),
offset,
},
diff --git a/Libraries/LibJS/JIT/Rust/src/passes/escape_analysis.rs b/Libraries/LibJS/JIT/Rust/src/passes/escape_analysis.rs
index 7e7ba0b5087..d420a602876 100644
--- a/Libraries/LibJS/JIT/Rust/src/passes/escape_analysis.rs
+++ b/Libraries/LibJS/JIT/Rust/src/passes/escape_analysis.rs
@@ -163,13 +163,13 @@ fn analyze(graph: &Graph, placed: &[NodeId], escaping: &[NodeId]) -> Analysis {
};
let accesses_object = match &data.op {
Op::EmptyToUndefined | Op::CellAddress | Op::CheckObject => true,
- Op::LoadNamed { offset } => *offset < count,
+ Op::LoadNamed { offset, .. } => *offset < count,
Op::CheckShape { shapes } => shapes
.iter()
.any(|check| check.shape == shape && check.dictionary_generation.is_none()),
- Op::InitializeNamed { offset } | Op::StoreNamed { offset } if index != 1 => *offset < count,
+ Op::InitializeNamed { offset } | Op::StoreNamed { offset, .. } if index != 1 => *offset < count,
Op::AddNamed { offset, .. } if index != 1 => *offset == count,
- Op::InitializeNamed { offset } | Op::StoreNamed { offset } | Op::AddNamed { offset, .. } => {
+ Op::InitializeNamed { offset } | Op::StoreNamed { offset, .. } | Op::AddNamed { offset, .. } => {
stored_into.push((root, roots.get(&data.inputs[0]).copied(), *offset));
true
}
@@ -184,7 +184,7 @@ fn analyze(graph: &Graph, placed: &[NodeId], escaping: &[NodeId]) -> Analysis {
let loaded = placed
.iter()
.filter_map(|node| match graph.node(*node).op {
- Op::LoadNamed { offset } => roots.get(&graph.node(*node).inputs[0]).map(|root| (*root, offset)),
+ Op::LoadNamed { offset, .. } => roots.get(&graph.node(*node).inputs[0]).map(|root| (*root, offset)),
_ => None,
})
.collect::<Vec<_>>();
@@ -483,14 +483,14 @@ fn replace_objects(graph: &mut Graph, analysis: &Analysis) -> Result<(), Vec<Nod
shapes.insert(root, (shape, property_count));
removed.insert(node.index());
}
- Op::InitializeNamed { offset } | Op::StoreNamed { offset } if object.is_some() => {
+ Op::InitializeNamed { offset } | Op::StoreNamed { offset, .. } if object.is_some() => {
let root = analysis
.root(object.expect("checked"))
.expect("replaced objects have roots");
properties.insert((root, offset), replacements.resolve(data.inputs[1]));
removed.insert(node.index());
}
- Op::LoadNamed { offset } if object.is_some() => {
+ Op::LoadNamed { offset, .. } if object.is_some() => {
let root = analysis
.root(object.expect("checked"))
.expect("replaced objects have roots");
diff --git a/Libraries/LibJS/JIT/Rust/src/snapshot.rs b/Libraries/LibJS/JIT/Rust/src/snapshot.rs
index 2ce925df457..4e0d5b8b321 100644
--- a/Libraries/LibJS/JIT/Rust/src/snapshot.rs
+++ b/Libraries/LibJS/JIT/Rust/src/snapshot.rs
@@ -703,6 +703,11 @@ pub struct PropertyCacheEntrySnapshot {
/// for `GetOwnProperty` entries the getter of the accessor the property
/// held when the interpreter last called it.
pub accessor_function: Option<AccessorFunctionSnapshot>,
+ /// Whether the receiver's own property is writable in its shape: only
+ /// data properties are, so loads and stores of it need not check for an
+ /// accessor while the object keeps the shape. Always false for
+ /// properties of prototypes.
+ pub property_holds_data: bool,
/// Whether the property held an accessor: for `GetOwnProperty` entries
/// when the interpreter last called its getter, and for entries of a
/// prototype when the snapshot was taken. Loads of the property as data
diff --git a/Libraries/LibJS/Rust/src/jit/snapshot.rs b/Libraries/LibJS/Rust/src/jit/snapshot.rs
index 50d78739ceb..339fd4289d5 100644
--- a/Libraries/LibJS/Rust/src/jit/snapshot.rs
+++ b/Libraries/LibJS/Rust/src/jit/snapshot.rs
@@ -254,6 +254,30 @@ fn prototype_property_object(entry: &PropertyLookupCacheEntryData) -> Option<Gc<
value.is_object().then(|| value.as_object())
}
+/// Whether the own property an entry reads or writes is writable in the shape of the object, which makes it a data
+/// property for as long as the object keeps the shape (see `Object::storage_set()`). Loads of the properties of
+/// prototypes always check, as they are guarded by no check of the object they load from.
+fn property_holds_data(entry: &PropertyLookupCacheEntryData) -> bool {
+ if !matches!(
+ entry.entry_type,
+ PropertyLookupCacheEntryType::GetOwnProperty | PropertyLookupCacheEntryType::ChangeOwnProperty
+ ) {
+ return false;
+ }
+ let Some(shape) = entry.shape else {
+ return false;
+ };
+ let mut writable = false;
+ shape.for_each_property_in_insertion_order(|_, metadata| {
+ if metadata.offset != entry.property_offset {
+ return core::ops::ControlFlow::Continue(());
+ }
+ writable = metadata.attributes.is_writable();
+ core::ops::ControlFlow::Break(())
+ });
+ writable
+}
+
/// The value the prototype of a GetPropertyInPrototypeChain or ChangePropertyInPrototypeChain entry holds in the
/// property, if the entry still applies.
fn prototype_property_value(entry: &PropertyLookupCacheEntryData) -> Option<Value> {
@@ -480,6 +504,7 @@ impl SnapshotBuilder<'_> {
})
.map(|set| self.cell(set)),
accessor_function,
+ property_holds_data: property_holds_data(&entry),
holds_accessor: entry_holds_accessor(&entry),
});
}
diff --git a/Libraries/LibJS/Rust/src/runtime/object.rs b/Libraries/LibJS/Rust/src/runtime/object.rs
index 81c7328ebc4..b537cd6c541 100644
--- a/Libraries/LibJS/Rust/src/runtime/object.rs
+++ b/Libraries/LibJS/Rust/src/runtime/object.rs
@@ -481,6 +481,15 @@ fn find_intrinsic_accessor(vm: &Vm, object: &Object, property_key: &PropertyKey)
intrinsics.remove(property_key.as_string())
}
+/// The attributes a property holding `value` gets: accessor properties have no [[Writable]] attribute, so that a shape
+/// tells that its writable properties hold data (which JIT code relies on).
+fn attributes_for(value: Value, mut attributes: PropertyAttributes) -> PropertyAttributes {
+ if value.is_accessor() {
+ attributes.set_writable(false);
+ }
+ attributes
+}
+
fn remove_intrinsic_accessor(vm: &Vm, object: &Object, property_key: &PropertyKey) {
if let Some(intrinsics) = vm
.intrinsic_accessors()
@@ -2824,6 +2833,7 @@ impl Object {
value_and_attributes: ValueAndAttributes,
) -> Option<u32> {
let ValueAndAttributes { value, attributes, .. } = value_and_attributes;
+ let attributes = attributes_for(value, attributes);
if property_key.is_number() && self.indexed_has(property_key.as_number()) {
self.indexed_put(property_key.as_number(), value, attributes);
@@ -2860,6 +2870,7 @@ impl Object {
) -> Option<u32> {
debug_assert!(!self.storage_has(property_key));
let ValueAndAttributes { value, attributes, .. } = value_and_attributes;
+ let attributes = attributes_for(value, attributes);
if property_key.is_number() {
self.indexed_put(property_key.as_number(), value, attributes);
diff --git a/Tests/LibJS/Runtime/jit/data-property-loads.js b/Tests/LibJS/Runtime/jit/data-property-loads.js
new file mode 100644
index 00000000000..024eba85571
--- /dev/null
+++ b/Tests/LibJS/Runtime/jit/data-property-loads.js
@@ -0,0 +1,66 @@
+// JIT code loads and stores properties that the checked shape marks writable
+// without checking for an accessor, as accessor properties are never
+// writable. Properties the shape does not mark writable are still checked.
+// These tests run every function many times, so that with a low JIT
+// threshold they are compiled.
+
+const iterations = 100;
+
+describe("loads and stores of data properties", () => {
+ test("accessors with the shape of a read-only data property", () => {
+ // NB: Either shape may be the first the property lookup cache lists.
+ function loadWritableFirst(object) {
+ return object.x;
+ }
+ function loadReadOnlyFirst(object) {
+ return object.x;
+ }
+ const writable = { x: 1 };
+ const readOnly = Object.defineProperty({}, "x", { value: 2, enumerable: true, configurable: true });
+ loadWritableFirst(readOnly);
+ loadReadOnlyFirst(writable);
+ for (let i = 0; i < iterations; ++i) {
+ for (const load of [loadWritableFirst, loadReadOnlyFirst]) {
+ expect(load(writable)).toBe(1);
+ expect(load(readOnly)).toBe(2);
+ }
+ }
+ const accessor = Object.defineProperty({}, "x", {
+ get() {
+ return 3;
+ },
+ enumerable: true,
+ configurable: true,
+ });
+ for (let i = 0; i < iterations; ++i) {
+ expect(loadWritableFirst(accessor)).toBe(3);
+ expect(loadReadOnlyFirst(accessor)).toBe(3);
+ }
+ });
+
+ test("properties redefined as accessors", () => {
+ function load(object) {
+ return object.x;
+ }
+ function store(object, value) {
+ object.x = value;
+ }
+ const object = { x: 1 };
+ for (let i = 0; i < iterations; ++i) {
+ store(object, i);
+ expect(load(object)).toBe(i);
+ }
+ let stored;
+ Object.defineProperty(object, "x", {
+ get() {
+ return "getter";
+ },
+ set(value) {
+ stored = value;
+ },
+ });
+ store(object, "value");
+ expect(stored).toBe("value");
+ expect(load(object)).toBe("getter");
+ });
+});