Truly independent web browser
LibJS/JIT: Initialize frames with 16-byte stores of constants
Andreas Klingfced24771fca
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/mod.rs b/Libraries/LibJS/JIT/Rust/src/builder/mod.rsindex c03fc568b6a..f866bde7f2e 100644--- a/Libraries/LibJS/JIT/Rust/src/builder/mod.rs+++ b/Libraries/LibJS/JIT/Rust/src/builder/mod.rs@@ -1386,7 +1386,7 @@ impl<'a> GraphBuilder<'a> {Instruction::Enter => {// NB: Inlined frames are set up by the caller.if !self.function.is_virtual() {- self.emit(Op::InitializeFrame, Vec::new(), None);+ self.emit(Op::InitializeFrame { clobbers_floats: false }, Vec::new(), None);let this_value = self.emit(Op::LoadSlot {slot: THIS_VALUE_REGISTER,diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/mod.rs b/Libraries/LibJS/JIT/Rust/src/codegen/mod.rsindex 8d0dcd291de..9be61ffd261 100644--- a/Libraries/LibJS/JIT/Rust/src/codegen/mod.rs+++ b/Libraries/LibJS/JIT/Rust/src/codegen/mod.rs@@ -1273,8 +1273,10 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {self.masm.load64(self.output(node_id), &address);}Op::StoreSlot { slot } => self.emit_store_slot(node_id, *slot)?,- Op::InitializeFrame => self.emit_initialize_frame(node_id)?,- Op::EnsureFrameInitialized => self.emit_ensure_frame_initialized(node_id)?,+ Op::InitializeFrame { clobbers_floats } => self.emit_initialize_frame(node_id, *clobbers_floats)?,+ Op::EnsureFrameInitialized { clobbers_floats } => {+ self.emit_ensure_frame_initialized(node_id, *clobbers_floats)?;+ }Op::PublishFrame => self.emit_publish_frame(),Op::LoadOuterEnvironment => {let (output, input) = (self.output(node_id), self.input(node_id, 0));@@ -2044,19 +2046,19 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {/// The `Enter` bytecode: empty registers and locals, constants copied/// into the frame, and the frame marked initialized (and published).- fn emit_initialize_frame(&mut self, node: NodeId) -> Result<(), CompileFailure> {+ fn emit_initialize_frame(&mut self, node: NodeId, clobbers_floats: bool) -> Result<(), CompileFailure> {let temps = &self.allocation.node(node).temps;let (value, cursor) = (Gpr(temps[0]), Gpr(temps[1]));- self.emit_frame_initialization(value, cursor)+ self.emit_frame_initialization(value, cursor, clobbers_floats.then_some(M::ARGUMENT_FPRS[0]))}/// `EnsureFrameInitialized`: initializes the frame unless it is.- fn emit_ensure_frame_initialized(&mut self, node: NodeId) -> Result<(), CompileFailure> {+ fn emit_ensure_frame_initialized(&mut self, node: NodeId, clobbers_floats: bool) -> Result<(), CompileFailure> {let temps = &self.allocation.node(node).temps;let (value, cursor) = (Gpr(temps[0]), Gpr(temps[1]));let done = self.masm.new_label();self.branch_if_frame_initialized(done);- self.emit_frame_initialization(value, cursor)?;+ self.emit_frame_initialization(value, cursor, clobbers_floats.then_some(M::ARGUMENT_FPRS[0]))?;self.masm.bind(done);Ok(())}@@ -2077,17 +2079,23 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {/// Publishes the frame and initializes it like `Enter`, with `value` for/// the stored values, and with a loop using `cursor` for large frames.- /// Clobbers the scratch register.- fn emit_frame_initialization(&mut self, value: Gpr, cursor: Gpr) -> Result<(), CompileFailure> {+ /// With a `vector` register, which it clobbers, it stores two slots at+ /// a time. Clobbers the scratch register.+ fn emit_frame_initialization(+ &mut self,+ value: Gpr,+ cursor: Gpr,+ vector: Option<Fpr>,+ ) -> Result<(), CompileFailure> {self.emit_publish_frame();// Up to this many registers and locals are emptied with one store- // each, more in a loop.- const UNROLLED_SLOTS: u32 = 32;+ // each (or per two slots through a vector register), more in a loop.+ let unrolled_slots: u32 = if vector.is_some() { 64 } else { 32 };let layout = self.executables[0].layout;let slots = self.runtime.offsets.execution_context_slots;let first = crate::bytecode::RESERVED_REGISTER_COUNT;let mut fields: Vec<call::ConstantField> = Vec::new();- let unrolled = layout.registers_and_locals_count.saturating_sub(first) <= UNROLLED_SLOTS;+ let unrolled = layout.registers_and_locals_count.saturating_sub(first) <= unrolled_slots;if !unrolled {self.masm.move_imm64(value, value::EMPTY);let start = self.slot_address(first)?;@@ -2108,19 +2116,21 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {fields.push((slots + 8 * (layout.constants_base() + index), 8, *constant));}fields.push((self.runtime.offsets.execution_context_frame_initialized, 1, 1));- self.emit_constant_fields(self.pinned.frame, value, &mut fields)+ self.emit_constant_fields_with_vector(self.pinned.frame, value, &mut fields, vector, &[])}- /// Initializes the frame for slow paths, whose caller-saved registers- /// are saved already. Uses two argument registers and the scratch- /// register, and does not call out or use the native frame's locals.+ /// Initializes the frame for slow paths, whose caller-saved general+ /// purpose registers are saved already, and which clobber the floating+ /// point registers. Uses two argument registers, the scratch register+ /// and the first floating point argument register, and does not call out+ /// or use the native frame's locals.fn emit_frame_initialization_stub(&mut self) -> Result<(), CompileFailure> {let Some(label) = self.frame_initialization_stub else {return Ok(());};self.annotate(CodeAnnotation::Tail("frame initialization (shared by slow paths)"));self.masm.bind(label);- self.emit_frame_initialization(M::ARGUMENT_GPRS[0], M::ARGUMENT_GPRS[1])?;+ self.emit_frame_initialization(M::ARGUMENT_GPRS[0], M::ARGUMENT_GPRS[1], Some(M::ARGUMENT_FPRS[0]))?;self.masm.ret();Ok(())}diff --git a/Libraries/LibJS/JIT/Rust/src/ir/dump.rs b/Libraries/LibJS/JIT/Rust/src/ir/dump.rsindex d9f9f08cd78..929888e7093 100644--- a/Libraries/LibJS/JIT/Rust/src/ir/dump.rs+++ b/Libraries/LibJS/JIT/Rust/src/ir/dump.rs@@ -111,8 +111,8 @@ fn op_text(layout: &FrameLayout, op: &Op, inputs: &str) -> String {Op::LoadSlot { slot } => with_inputs(&format!("LoadSlot {}", slot_name(layout, *slot))),Op::StoreSlot { slot } => with_inputs(&format!("StoreSlot {}", slot_name(layout, *slot))),Op::Phi => with_inputs("Phi"),- Op::InitializeFrame => with_inputs("InitializeFrame"),- Op::EnsureFrameInitialized => with_inputs("EnsureFrameInitialized"),+ Op::InitializeFrame { .. } => with_inputs("InitializeFrame"),+ Op::EnsureFrameInitialized { .. } => with_inputs("EnsureFrameInitialized"),Op::PublishFrame => with_inputs("PublishFrame"),Op::Suspend { continuation, kind } => {let continuation = continuation.map_or("completed".to_string(), |pc| format!("pc {pc}"));diff --git a/Libraries/LibJS/JIT/Rust/src/ir/mod.rs b/Libraries/LibJS/JIT/Rust/src/ir/mod.rsindex fdb9e5d4f51..04bab7d27ef 100644--- a/Libraries/LibJS/JIT/Rust/src/ir/mod.rs+++ b/Libraries/LibJS/JIT/Rust/src/ir/mod.rs@@ -437,7 +437,12 @@ pub enum Op {Phi,/// The `Enter` bytecode: fills the registers and locals with the empty/// value, copies the constants into the frame and marks it initialized.- InitializeFrame,+ /// If `clobbers_floats`, the node it initializes the frame for clobbers+ /// the floating point registers anyway, so it stores through one of+ /// them, two slots at a time.+ InitializeFrame {+ clobbers_floats: bool,+ },/// Makes the frame the running execution context, where other code (the/// garbage collector, stack walks, runtime functions) sees it. Direct/// calls of compiled functions leave their frame unpublished until it@@ -446,7 +451,9 @@ pub enum Op {PublishFrame,/// Does what `InitializeFrame` does, unless the frame is initialized/// already. See `passes::frame_initialization`.- EnsureFrameInitialized,+ EnsureFrameInitialized {+ clobbers_floats: bool,+ },/// The suspension of the `Yield`, `YieldIteratorResult` and `Await`/// bytecodes, right before the `Return` of the yielded or awaited value:/// records in the frame where its generator or async function resumes@@ -1285,7 +1292,7 @@ impl Op {..P::PURE},Op::Phi => P::PURE,- Op::InitializeFrame | Op::EnsureFrameInitialized | Op::PublishFrame => P {+ Op::InitializeFrame { .. } | Op::EnsureFrameInitialized { .. } | Op::PublishFrame => P {writes: Locations::FRAME,..P::PURE},@@ -1597,8 +1604,8 @@ impl Op {Op::LoadSlot { .. } => "LoadSlot",Op::StoreSlot { .. } => "StoreSlot",Op::Phi => "Phi",- Op::InitializeFrame => "InitializeFrame",- Op::EnsureFrameInitialized => "EnsureFrameInitialized",+ Op::InitializeFrame { .. } => "InitializeFrame",+ Op::EnsureFrameInitialized { .. } => "EnsureFrameInitialized",Op::Suspend { .. } => "Suspend",Op::PublishFrame => "PublishFrame",Op::BoxCell => "BoxCell",diff --git a/Libraries/LibJS/JIT/Rust/src/passes/frame_initialization.rs b/Libraries/LibJS/JIT/Rust/src/passes/frame_initialization.rsindex 0db9e22a9e6..280f49bb03b 100644--- a/Libraries/LibJS/JIT/Rust/src/passes/frame_initialization.rs+++ b/Libraries/LibJS/JIT/Rust/src/passes/frame_initialization.rs@@ -73,15 +73,16 @@ impl State {}/// The node to put before a node with `need`, if any, and the state- /// after both.- fn satisfy(self, need: &Need) -> (Option<Op>, State) {+ /// after both. If `clobbers_floats`, that node clobbers the floating+ /// point registers anyway (see `Op::InitializeFrame`).+ fn satisfy(self, need: &Need, clobbers_floats: bool) -> (Option<Op>, State) {const INITIALIZED: State = State {published: Holds::Yes,initialized: Holds::Yes,};match (need, self.initialized, self.published) {- (Need::Initialized, Holds::No, _) => (Some(Op::InitializeFrame), INITIALIZED),- (Need::Initialized, Holds::Maybe, _) => (Some(Op::EnsureFrameInitialized), INITIALIZED),+ (Need::Initialized, Holds::No, _) => (Some(Op::InitializeFrame { clobbers_floats }), INITIALIZED),+ (Need::Initialized, Holds::Maybe, _) => (Some(Op::EnsureFrameInitialized { clobbers_floats }), INITIALIZED),(Need::Published, _, Holds::No | Holds::Maybe) => (Some(Op::PublishFrame),State {@@ -153,7 +154,7 @@ pub fn run(graph: &mut Graph, layout: &FrameLayout) {block.body.iter()- .find(|node| graph.node(**node).op == Op::InitializeFrame)+ .find(|node| matches!(graph.node(**node).op, Op::InitializeFrame { .. })).map(|node| (index, *node))}) else {return;@@ -215,7 +216,9 @@ pub fn run(graph: &mut Graph, layout: &FrameLayout) {}continue;}- let (op, after) = current.satisfy(&need);+ let op = &graph.node(*node).op;+ let clobbers_floats = op.properties().is_call || crate::regalloc::saves_only_gprs(op);+ let (op, after) = current.satisfy(&need, clobbers_floats);if let Some(op) = op {insertions.push((block, index, op));}diff --git a/Libraries/LibJS/JIT/Rust/src/passes/tests.rs b/Libraries/LibJS/JIT/Rust/src/passes/tests.rsindex f2117acfb4d..917ef589592 100644--- a/Libraries/LibJS/JIT/Rust/src/passes/tests.rs+++ b/Libraries/LibJS/JIT/Rust/src/passes/tests.rs@@ -754,7 +754,7 @@ fn frame_writes_after_slow_paths_that_may_initialize_the_frame_check_first() {};let mut graph = Graph::default();graph.add_block(Block::default());- let initialize = node(&mut graph, Op::InitializeFrame, vec![], None);+ let initialize = node(&mut graph, Op::InitializeFrame { clobbers_floats: false }, vec![], None);let call = node(&mut graph,Op::CallSlowPath {@@ -785,7 +785,7 @@ fn frame_writes_after_slow_paths_that_may_initialize_the_frame_check_first() {ops.as_slice(),[Op::CallSlowPath { .. },- Op::EnsureFrameInitialized,+ Op::EnsureFrameInitialized { .. },Op::StoreSlot { .. }]));diff --git a/Libraries/LibJS/JIT/Rust/src/passes/verify.rs b/Libraries/LibJS/JIT/Rust/src/passes/verify.rsindex 9c8f685da7c..3106beeaf86 100644--- a/Libraries/LibJS/JIT/Rust/src/passes/verify.rs+++ b/Libraries/LibJS/JIT/Rust/src/passes/verify.rs@@ -85,8 +85,10 @@ pub fn verify_frame_writes(graph: &Graph, layout: &FrameLayout) -> Result<(), Stlet reader = nodes[position + 1..].iter().find(|next| {let op = &graph.node(**next).op;let properties = op.properties();- let initializes_frame =- matches!(op, Op::InitializeFrame | Op::EnsureFrameInitialized | Op::PublishFrame);+ let initializes_frame = matches!(+ op,+ Op::InitializeFrame { .. } | Op::EnsureFrameInitialized { .. } | Op::PublishFrame+ );!matches!(op, Op::StoreSlot { .. })&& !(is_argument && initializes_frame)&& (properties.reads != Locations::NONE || properties.writes != Locations::NONE)diff --git a/Libraries/LibJS/JIT/Rust/src/regalloc/mod.rs b/Libraries/LibJS/JIT/Rust/src/regalloc/mod.rsindex 18cb15f4321..ed2e991731d 100644--- a/Libraries/LibJS/JIT/Rust/src/regalloc/mod.rs+++ b/Libraries/LibJS/JIT/Rust/src/regalloc/mod.rs@@ -93,15 +93,18 @@ impl RegisterClass {/// Whether the lowering of `op` calls out of line keeping only the general/// purpose registers (register-saving `CallSlowPath`s, the growth of arrays and named storage, the runtime's-/// cache probes, and allocations that call the runtime): it clobbers every-/// floating point register.+/// cache probes, and allocations that call the runtime), or stores constants+/// through a floating point register (frame initialization for such nodes+/// and calls): it clobbers every floating point register.pub fn saves_only_gprs(op: &Op) -> bool {matches!(op,Op::CallSlowPath {saves_registers: true,..- } | Op::CallArrayPush+ } | Op::InitializeFrame { clobbers_floats: true }+ | Op::EnsureFrameInitialized { clobbers_floats: true }+ | Op::CallArrayPush| Op::OpenArrayIteration| Op::AddNamed { .. }| Op::ProbeKeyedStore { .. }@@ -359,7 +362,7 @@ fn constraints(node: &Node, registers: &RegisterSet) -> Constraints {Op::ConcatenateStrings => constraints.temps = 3,Op::StringsEqual => constraints.temps = 1,// A value and a cursor for filling the registers and locals.- Op::InitializeFrame | Op::EnsureFrameInitialized => constraints.temps = 2,+ Op::InitializeFrame { .. } | Op::EnsureFrameInitialized { .. } => constraints.temps = 2,// The object's property storage.Op::StoreNamed { .. } | Op::AddNamed { .. } | Op::CheckAccessorFunction { .. } => constraints.temps = 1,// The object whose prototype comes next.