Truly independent web browser
LibJS: Read the completion of a resumed generator in JIT code
Andreas Kling135b4e888446
diff --git a/Libraries/LibJS/JIT/ARCHITECTURE.md b/Libraries/LibJS/JIT/ARCHITECTURE.mdindex b8eb7f195e0..970a522f0c6 100644--- a/Libraries/LibJS/JIT/ARCHITECTURE.md+++ b/Libraries/LibJS/JIT/ARCHITECTURE.md@@ -188,6 +188,7 @@ Exits: E = eager, L = lazy. Every node that can exit has a frame state. "Call" m| OpenArrayIteration | `GetIterator` in fast array mode, through a runtime call that preserves every register: the iterable, if it is an array that iterates with `%Array.prototype.values%` and whose array iterators have the builtin `next`, or the empty value. The iteration then keeps the index of the next element in the iterator operand and the array in the next operand, and makes no iterator. | | Any | | || LoadArrayIteratorArray, LoadArrayIteratorIndex | The iterated array (`undefined` once done) and the low 32 bits of the next index of an array iterator (a `Repr::Pointer` that `BranchCondition::ArrayIterator` established is one). | | Iterators | | || StoreArrayIteratorIndex, FinishArrayIterator | Sets the next index of an array iterator, or makes it done. | | | Iterators | |+| LoadGeneratorCompletionType, LoadGeneratorCompletionValue | The type (as an int32) and the value of the completion a generator object (which a branch on `GeneratorObject` established it is) resumed with. | | Iterators | | || LoadGlobalBinding | A binding of the global declarative environment; exits while uninitialized. | E | Global | | cse || StoreGlobalBinding | Stores a mutable global binding; exits while uninitialized. | E | Global | Global | || **Allocation** | | | | | |@@ -284,8 +285,9 @@ continuation's block, which nothing else in compiled code reaches. Both kinds of`Graph::osr_entries` by pc, since both continue a frame whose memory holds every value at that pc.`GeneratorObject::execute()` runs the generator from its continuation through `run_executable()`,which enters compiled code at the resume entry if the executable has code with one there, and-interprets it otherwise. The function's start (its `Enter` and the initial `Yield`) is compiled-like any function's. Async functions, whose `Await` suspends them, are not compiled yet.+interprets it otherwise. A continuation starts by reading the completion the generator resumed+with (`GetCompletionFields`), which compiled code loads from the generator object. The function's+start (its `Enter` and the initial `Yield`) is compiled like any function's. Async functions, whose `Await` suspends them, are not compiled yet.## 5. Dependencies and invalidationdiff --git a/Libraries/LibJS/JIT/Rust/src/builder/checks.rs b/Libraries/LibJS/JIT/Rust/src/builder/checks.rsindex cd5d2715cba..662a8caa2e4 100644--- a/Libraries/LibJS/JIT/Rust/src/builder/checks.rs+++ b/Libraries/LibJS/JIT/Rust/src/builder/checks.rs@@ -166,6 +166,11 @@ impl GraphBuilder<'_> {Instruction::GetById { .. } => self.build_get_by_id(instruction),Instruction::PutById { .. } => self.build_put_by_id(instruction),Instruction::IteratorClose { .. } => self.build_iterator_close(instruction),+ Instruction::GetCompletionFields {+ type_dst,+ value_dst,+ completion,+ } => self.build_get_completion_fields(type_dst, value_dst, completion),Instruction::GetGlobal { .. } => self.build_get_global(instruction),Instruction::SetGlobal { .. } => self.build_set_global(instruction),_ => match super::speculation::Operation::of(instruction) {diff --git a/Libraries/LibJS/JIT/Rust/src/builder/handling.rs b/Libraries/LibJS/JIT/Rust/src/builder/handling.rsindex 8685413249a..4ade0adda1e 100644--- a/Libraries/LibJS/JIT/Rust/src/builder/handling.rs+++ b/Libraries/LibJS/JIT/Rust/src/builder/handling.rs@@ -291,6 +291,10 @@ pub fn handling(opcode: OpCode) -> Handling {// Iteration over arrays.O::GetIterator | O::IteratorNextUnpack => EXPANDED,+ // The completion a generator resumed with, which the generator+ // object holds.+ O::GetCompletionFields => EXPANDED,+// Closing an iterator, which does nothing if the cache of the site// has no return method for it.O::IteratorClose => EXPANDED,@@ -358,7 +362,6 @@ pub fn handling(opcode: OpCode) -> Handling {| O::DeleteById| O::DeleteByValue| O::DeleteVariable- | O::GetCompletionFields| O::SetCompletionType| O::GetTemplateObject| O::GetPrivateByIddiff --git a/Libraries/LibJS/JIT/Rust/src/builder/mod.rs b/Libraries/LibJS/JIT/Rust/src/builder/mod.rsindex 3fc990bd1ac..4ebd7e65b30 100644--- a/Libraries/LibJS/JIT/Rust/src/builder/mod.rs+++ b/Libraries/LibJS/JIT/Rust/src/builder/mod.rs@@ -1795,6 +1795,31 @@ impl<'a> GraphBuilder<'a> {Ok(Flow::Ended)}+ /// Builds `GetCompletionFields` (`Handling::Expanded`), which a+ /// resumed generator starts with: the type and the value of the+ /// completion it resumed with, which the generator object it finds in+ /// its accumulator holds, and the slow path for other completion+ /// sources (async generators).+ pub(super) fn build_get_completion_fields(+ &mut self,+ type_dst: Operand,+ value_dst: Operand,+ completion: Operand,+ ) -> Result<Flow, CompileFailure> {+ let source = self.read(completion)?;+ let mut slow_paths = self.start_slow_paths(Some(Repr::Tagged))?;+ let object = self.branch_to_slow_path(&mut slow_paths, BranchCondition::Object, vec![source], true);+ let object = self.cell_address(object);+ let generator = self.branch_to_slow_path(&mut slow_paths, BranchCondition::GeneratorObject, vec![object], true);+ let completion_type = self.emit(Op::LoadGeneratorCompletionType, vec![generator], Some(Repr::Int32));+ let completion_type = self.emit(Op::BoxInt32, vec![completion_type], Some(Repr::Tagged));+ let value = self.emit(Op::LoadGeneratorCompletionValue, vec![generator], Some(Repr::Tagged));+ let outputs = self.join_paths(slow_paths, vec![completion_type, value], |_, outputs| outputs);+ self.write(type_dst, outputs[0])?;+ self.write(value_dst, outputs[1])?;+ Ok(Flow::Continue)+ }+/// Builds an instruction as its handling table entry says.fn build_by_handling(&mut self, instruction: &Instruction) -> Result<Flow, CompileFailure> {let opcode = instruction.opcode();diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/mod.rs b/Libraries/LibJS/JIT/Rust/src/codegen/mod.rsindex 46c4d82ccaa..cdd3e446e71 100644--- a/Libraries/LibJS/JIT/Rust/src/codegen/mod.rs+++ b/Libraries/LibJS/JIT/Rust/src/codegen/mod.rs@@ -1713,6 +1713,16 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {Op::OpenArrayIteration => self.emit_open_array_iteration(node_id),Op::LoadArrayIteratorArray => self.emit_load_array_iterator_array(node_id),Op::LoadArrayIteratorIndex => self.emit_load_array_iterator_index(node_id),+ Op::LoadGeneratorCompletionType => {+ let (generator, output) = (self.input(node_id, 0), self.output(node_id));+ let offset = self.runtime.layout.generator_object_pending_completion_type;+ self.masm.load32(output, &Address::new(generator, offset as i32));+ }+ Op::LoadGeneratorCompletionValue => {+ let (generator, output) = (self.input(node_id, 0), self.output(node_id));+ let offset = self.runtime.layout.generator_object_pending_completion_value;+ self.masm.load64(output, &Address::new(generator, offset as i32));+ }Op::StoreArrayIteratorIndex => self.emit_store_array_iterator_index(node_id),Op::FinishArrayIterator => self.emit_finish_array_iterator(node_id),Op::SlowPathOutput { index } => self.emit_slow_path_output(node_id, *index)?,@@ -2183,6 +2193,12 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {BranchCondition::Shape(_) => unreachable!("only refinements of shape switch cases test one shape"),BranchCondition::Float64(_) => unreachable!("float64 comparisons branch above"),BranchCondition::ArrayIterator => self.emit_branch_on_array_iterator(input, target, invert),+ BranchCondition::GeneratorObject => {+ // NB: Cells start with the pointer to their class.+ self.masm.load64(scratch, &Address::new(input, 0));+ let class = self.runtime.layout.generator_object_class as i64;+ self.masm.branch64_imm(when(Condition::Equal), scratch, class, target);+ }BranchCondition::PlainArrayPrototypeChain => {self.emit_branch_on_plain_array_prototype_chain(node, input, target, invert);}diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs b/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rsindex 270f3b25710..aa4bcab5b90 100644--- a/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs+++ b/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs@@ -1229,6 +1229,9 @@ fn fake_object_layout() -> RuntimeLayout {array_iterator_index: 0,array_iterator_kind: 0,array_iterator_kind_value: 0,+ generator_object_class: 0,+ generator_object_pending_completion_value: 0,+ generator_object_pending_completion_type: 0,declarative_environment_binding_values_size: 32,declarative_environment_binding_values_capacity: 40,environment_shape_binding_names: 48,diff --git a/Libraries/LibJS/JIT/Rust/src/ir/dump.rs b/Libraries/LibJS/JIT/Rust/src/ir/dump.rsindex 1017a00b6c4..c28bb0ad081 100644--- a/Libraries/LibJS/JIT/Rust/src/ir/dump.rs+++ b/Libraries/LibJS/JIT/Rust/src/ir/dump.rs@@ -76,6 +76,7 @@ fn condition_text(condition: BranchCondition) -> String {BranchCondition::Extensible => "Extensible".to_string(),BranchCondition::HoleFillableArray => "HoleFillableArray".to_string(),BranchCondition::ArrayIterator => "ArrayIterator".to_string(),+ BranchCondition::GeneratorObject => "GeneratorObject".to_string(),BranchCondition::PlainArrayPrototypeChain => "PlainArrayPrototypeChain".to_string(),BranchCondition::Shape(shape) => format!("Shape {}", shapes_text(std::iter::once(&shape))),BranchCondition::NonNegativeInt32 => "NonNegativeInt32".to_string(),@@ -308,6 +309,8 @@ fn op_text(layout: &FrameLayout, op: &Op, inputs: &str) -> String {| Op::LoadArrayIteratorIndex| Op::StoreArrayIteratorIndex| Op::FinishArrayIterator+ | Op::LoadGeneratorCompletionType+ | Op::LoadGeneratorCompletionValue| Op::LoadPropertyIteratorKeyCount| Op::LoadPropertyIteratorKey => with_inputs(op.name()),Op::CheckBoundsdiff --git a/Libraries/LibJS/JIT/Rust/src/ir/mod.rs b/Libraries/LibJS/JIT/Rust/src/ir/mod.rsindex d3553fda5a9..1ac8b067def 100644--- a/Libraries/LibJS/JIT/Rust/src/ir/mod.rs+++ b/Libraries/LibJS/JIT/Rust/src/ir/mod.rs@@ -139,6 +139,10 @@ pub enum BranchCondition {/// The input is the `Repr::Pointer` of an object: is it an array/// iterator over values, like `%Array.prototype.values%` makes?ArrayIterator,+ /// The input is the `Repr::Pointer` of an object: is it the generator+ /// object of a generator or of an async function (and no async+ /// generator)?+ GeneratorObject,/// The input is the `Repr::Pointer` of an object: is it an array whose/// prototype is the realm's `%Array.prototype%`, and do that and/// `%Object.prototype%` have no indexed properties and nothing that may@@ -1055,6 +1059,13 @@ pub enum Op {/// Sets the `[[IteratedArrayLike]]` of the array iterator at the/// `Repr::Pointer` input 0 to `undefined`, which makes it done.FinishArrayIterator,+ /// The type of the completion the generator object at the+ /// `Repr::Pointer` input 0 resumed with, as the `Repr::Int32` that+ /// `GetCompletionFields` writes.+ LoadGeneratorCompletionType,+ /// The value of the completion the generator object at the+ /// `Repr::Pointer` input 0 resumed with, as a tagged value.+ LoadGeneratorCompletionValue,// For-in loops./// The number of keys of the property iterator cache input 0, as a@@ -1507,7 +1518,10 @@ impl Op {allocates: true,..P::PURE},- Op::LoadArrayIteratorArray | Op::LoadArrayIteratorIndex => P {+ Op::LoadArrayIteratorArray+ | Op::LoadArrayIteratorIndex+ | Op::LoadGeneratorCompletionType+ | Op::LoadGeneratorCompletionValue => P {reads: Locations::ITERATORS,..P::PURE},@@ -1639,6 +1653,8 @@ impl Op {Op::OpenArrayIteration => "OpenArrayIteration",Op::LoadArrayIteratorArray => "LoadArrayIteratorArray",Op::LoadArrayIteratorIndex => "LoadArrayIteratorIndex",+ Op::LoadGeneratorCompletionType => "LoadGeneratorCompletionType",+ Op::LoadGeneratorCompletionValue => "LoadGeneratorCompletionValue",Op::StoreArrayIteratorIndex => "StoreArrayIteratorIndex",Op::FinishArrayIterator => "FinishArrayIterator",Op::SlowPathOutput { .. } => "SlowPathOutput",diff --git a/Libraries/LibJS/JIT/Rust/src/passes/simplify.rs b/Libraries/LibJS/JIT/Rust/src/passes/simplify.rsindex 85af88eae4f..38f6da5dee0 100644--- a/Libraries/LibJS/JIT/Rust/src/passes/simplify.rs+++ b/Libraries/LibJS/JIT/Rust/src/passes/simplify.rs@@ -278,6 +278,7 @@ fn constant_condition(graph: &Graph, condition: BranchCondition, input: NodeId,| BranchCondition::Extensible| BranchCondition::HoleFillableArray| BranchCondition::ArrayIterator+ | BranchCondition::GeneratorObject| BranchCondition::PlainArrayPrototypeChain| BranchCondition::Shape(_) => None,}diff --git a/Libraries/LibJS/JIT/Rust/src/passes/verify.rs b/Libraries/LibJS/JIT/Rust/src/passes/verify.rsindex 91a65475969..cf80255c575 100644--- a/Libraries/LibJS/JIT/Rust/src/passes/verify.rs+++ b/Libraries/LibJS/JIT/Rust/src/passes/verify.rs@@ -486,6 +486,7 @@ fn branch_input_reprs(condition: crate::ir::BranchCondition) -> Vec<Repr> {| crate::ir::BranchCondition::Extensible| crate::ir::BranchCondition::HoleFillableArray| crate::ir::BranchCondition::ArrayIterator+ | crate::ir::BranchCondition::GeneratorObject| crate::ir::BranchCondition::PlainArrayPrototypeChain => vec![Repr::Pointer],crate::ir::BranchCondition::Shape(_) => vec![Repr::Tagged, Repr::Pointer],crate::ir::BranchCondition::PropertyIteratorCacheValid => tagged(2),@@ -529,7 +530,11 @@ fn expected_input_reprs(graph: &Graph, node: NodeId) -> Option<Vec<Repr>> {Op::LoadElementsLength | Op::LoadElementsCapacity => Some(vec![Repr::Pointer]),Op::LoadPropertyIteratorKey => Some(vec![Repr::Tagged, Repr::Int32]),Op::OpenArrayIteration => Some(tagged(1)),- Op::LoadArrayIteratorArray | Op::LoadArrayIteratorIndex | Op::FinishArrayIterator => Some(vec![Repr::Pointer]),+ Op::LoadArrayIteratorArray+ | Op::LoadArrayIteratorIndex+ | Op::FinishArrayIterator+ | Op::LoadGeneratorCompletionType+ | Op::LoadGeneratorCompletionValue => Some(vec![Repr::Pointer]),Op::StoreArrayIteratorIndex => Some(vec![Repr::Pointer, Repr::Int32]),Op::BranchOnPc { .. } => Some(vec![Repr::Int32]),Op::BoxInt32 | Op::CheckNonNegativeInt32 => Some(vec![Repr::Int32]),diff --git a/Libraries/LibJS/JIT/Rust/src/snapshot.rs b/Libraries/LibJS/JIT/Rust/src/snapshot.rsindex cf3de0fd2b1..bdcc919e39f 100644--- a/Libraries/LibJS/JIT/Rust/src/snapshot.rs+++ b/Libraries/LibJS/JIT/Rust/src/snapshot.rs@@ -1654,6 +1654,14 @@ pub struct RuntimeLayout {pub array_iterator_kind: u32,/// The `[[ArrayLikeIterationKind]]` of iterators over values.pub array_iterator_kind_value: u8,+ /// The class (pointer) of `GeneratorObject`s, which starts their cells.+ pub generator_object_class: u64,+ /// `GeneratorObject::pending_completion_value` (value): the value of+ /// the completion a generator resumes with.+ pub generator_object_pending_completion_value: u32,+ /// `GeneratorObject::pending_completion_type` (u32): the type of that+ /// completion, as `GetCompletionFields` writes it.+ pub generator_object_pending_completion_type: u32,/// `DeclarativeEnvironment::binding_values.size` (u64).pub declarative_environment_binding_values_size: u32,/// The capacity (u64) of `DeclarativeEnvironment::binding_values`.diff --git a/Libraries/LibJS/Rust/src/jit/runtime_info.rs b/Libraries/LibJS/Rust/src/jit/runtime_info.rsindex 28d6537e8b6..cb31c3856bd 100644--- a/Libraries/LibJS/Rust/src/jit/runtime_info.rs+++ b/Libraries/LibJS/Rust/src/jit/runtime_info.rs@@ -62,6 +62,7 @@ use crate::runtime::array_iterator::ArrayIterator;use crate::runtime::function_environment::{FUNCTION_ENVIRONMENT_FUNCTION_OBJECT_OFFSET, FUNCTION_ENVIRONMENT_THIS_VALUE_OFFSET,};+use crate::runtime::generator_object::GeneratorObject;use crate::runtime::module_environment::ModuleEnvironment;use crate::runtime::object::{ObjectMethods, PropertyKind};@@ -234,6 +235,9 @@ fn runtime_layout() -> RuntimeLayout {array_iterator_index: offset(offset_of!(ArrayIterator, index)),array_iterator_kind: offset(offset_of!(ArrayIterator, iteration_kind)),array_iterator_kind_value: PropertyKind::Value as u8,+ generator_object_class: core::ptr::from_ref(GeneratorObject::CLASS) as u64,+ generator_object_pending_completion_value: offset(offset_of!(GeneratorObject, pending_completion_value)),+ generator_object_pending_completion_type: offset(offset_of!(GeneratorObject, pending_completion_type)),declarative_environment_binding_values_size: offset(offset_of!(DeclarativeEnvironment, binding_values.size)),declarative_environment_binding_values_capacity: offset(offset_of!(DeclarativeEnvironment,diff --git a/Libraries/LibJS/Rust/src/runtime/generator_object.rs b/Libraries/LibJS/Rust/src/runtime/generator_object.rsindex d2c71fe8554..f8765201e1c 100644--- a/Libraries/LibJS/Rust/src/runtime/generator_object.rs+++ b/Libraries/LibJS/Rust/src/runtime/generator_object.rs@@ -108,9 +108,9 @@ pub struct GeneratorObject {yield_continuation: Cell<u32>,generator_state: Cell<GeneratorState>,generator_brand: Option<&'static str>,- pending_completion_value: Cell<Value>,+ pub(crate) pending_completion_value: Cell<Value>,#[gc(untraced)]- pending_completion_type: Cell<CompletionType>,+ pub(crate) pending_completion_type: Cell<CompletionType>,#[gc(untraced)]execute: GeneratorExecuteFunction,}diff --git a/Tests/LibJS/JIT/ir/generators.js b/Tests/LibJS/JIT/ir/generators.jsindex 498345d2976..0b163a85cfa 100644--- a/Tests/LibJS/JIT/ir/generators.js+++ b/Tests/LibJS/JIT/ir/generators.js@@ -1,6 +1,7 @@// A generator suspends in compiled code like the interpreter's Yield: it writes the slots live at the continuation to// its frame, records the continuation, and returns the value. It resumes at an entry block of the continuation, which-// loads those slots from the frame like an on-stack replacement entry.+// loads those slots from the frame like an on-stack replacement entry, and reads the completion it resumed with from+// its generator object.function* counter(limit) {for (let i = 0; i < limit; ++i) yield i;@@ -24,6 +25,10 @@ jit.compile(counter);// CHECK-NEXT: Suspend pc [[NEXT:[0-9]+]]// CHECK-NEXT: Return ([[VALUE]])// CHECK: [[CONTINUATION]] (pc [[NEXT]]) <- [[RESUME:b[0-9]+]]:+// CHECK: [[GENERATOR:v[0-9]+]]:pointer = RefineGeneratorObject+// CHECK-NEXT: LoadGeneratorCompletionType ([[GENERATOR]])+// CHECK-NEXT: BoxInt32+// CHECK-NEXT: LoadGeneratorCompletionValue ([[GENERATOR]])// CHECK: osr entry at pc [[START]]: {{b[0-9]+}}// CHECK-NEXT: osr entry at pc [[NEXT]]: [[RESUME]]