Truly independent web browser
LibJS: Discard JIT code sooner when its exits leave frames in loops
Andreas Kling397e587a66cf
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/osr.rs b/Libraries/LibJS/JIT/Rust/src/builder/osr.rsindex fe40f5a8615..bd750a789c5 100644--- a/Libraries/LibJS/JIT/Rust/src/builder/osr.rs+++ b/Libraries/LibJS/JIT/Rust/src/builder/osr.rs@@ -25,6 +25,7 @@ use super::GraphBuilder;use crate::CompileFailure;use crate::bytecode::OpCode;use crate::bytecode::RESERVED_REGISTER_COUNT;+use crate::bytecode::cfg::Loop;/// Whether instructions with this opcode are loop back edges, which count/// loop iterations in the interpreter.@@ -141,7 +142,9 @@ impl GraphBuilder<'_> {/// Records the loop back edges in the loop of the back edge at `pc`, which/// has an entry. A frame running in the interpreter at one of them reaches- /// the entry once its inner loops finish an iteration.+ /// the entry once its inner loops finish an iteration. Also records the+ /// other back edges of the loops around that loop, from where a frame may+ /// reach the entry on a later iteration.fn record_osr_loop_back_edges(&mut self, pc: u32) {let cfg = &self.function.cfg;let Some(back_edge_block) = cfg.block_for_pc(pc) else {@@ -158,12 +161,26 @@ impl GraphBuilder<'_> {else {return;};- for &block in &entered_loop.blocks {- for instruction in &self.function.instructions[cfg.blocks[block].instructions.clone()] {- if is_loop_back_edge(instruction.instruction.opcode())- && !self.graph.osr_loop_back_edges.contains(&instruction.pc)- {- self.graph.osr_loop_back_edges.push(instruction.pc);+ let back_edges_in = |current_loop: &Loop| {+ current_loop+ .blocks+ .iter()+ .flat_map(|&block| &self.function.instructions[cfg.blocks[block].instructions.clone()])+ .filter(|instruction| is_loop_back_edge(instruction.instruction.opcode()))+ .map(|instruction| instruction.pc)+ .collect::<Vec<_>>()+ };+ for back_edge_pc in back_edges_in(entered_loop) {+ if !self.graph.osr_loop_back_edges.contains(&back_edge_pc) {+ self.graph.osr_loop_back_edges.push(back_edge_pc);+ }+ }+ for outer_loop in cfg.loops.iter().filter(|current_loop| {+ current_loop.blocks.contains(&back_edge_block) && current_loop.blocks.len() > entered_loop.blocks.len()+ }) {+ for back_edge_pc in back_edges_in(outer_loop) {+ if !self.graph.osr_outer_loop_back_edges.contains(&back_edge_pc) {+ self.graph.osr_outer_loop_back_edges.push(back_edge_pc);}}}diff --git a/Libraries/LibJS/JIT/Rust/src/code.rs b/Libraries/LibJS/JIT/Rust/src/code.rsindex df60a5fc779..b42d014c878 100644--- a/Libraries/LibJS/JIT/Rust/src/code.rs+++ b/Libraries/LibJS/JIT/Rust/src/code.rs@@ -27,6 +27,10 @@ pub struct CompiledCode {/// The loop back edges from where a frame running in the interpreter/// reaches one of the `osr_entries` without leaving the loop it is in.pub osr_loop_back_edges: Vec<u32>,+ /// The loop back edges in the loops around those of the `osr_entries`,+ /// from where a frame running in the interpreter may reach one of them+ /// on a later iteration.+ pub osr_outer_loop_back_edges: Vec<u32>,/// The sites of the code, by index: exit and leave stubs pass the index of/// theirs to the runtime, and calls in inlined callees name theirs.pub sites: Vec<Site>,diff --git a/Libraries/LibJS/JIT/Rust/src/ir/mod.rs b/Libraries/LibJS/JIT/Rust/src/ir/mod.rsindex 6a573423134..6c53cb8e1c8 100644--- a/Libraries/LibJS/JIT/Rust/src/ir/mod.rs+++ b/Libraries/LibJS/JIT/Rust/src/ir/mod.rs@@ -1908,6 +1908,10 @@ pub struct Graph {/// replacement entries (including those of the entries), from where a/// frame running in the interpreter reaches an entry.pub osr_loop_back_edges: Vec<u32>,+ /// The pcs of the loop back edges in the loops around the loops of the+ /// on-stack replacement entries, from where a frame running in the+ /// interpreter may reach an entry on a later iteration.+ pub osr_outer_loop_back_edges: Vec<u32>,/// The register-saving `CallSlowPath` nodes that may run before the/// frame is initialized, and so initialize it first if it is not.pub slow_paths_initializing_frame: Vec<NodeId>,diff --git a/Libraries/LibJS/JIT/Rust/src/lib.rs b/Libraries/LibJS/JIT/Rust/src/lib.rsindex 824b0cec129..570a29937e5 100644--- a/Libraries/LibJS/JIT/Rust/src/lib.rs+++ b/Libraries/LibJS/JIT/Rust/src/lib.rs@@ -131,6 +131,7 @@ pub fn compile_for<M: asm::PortableMacroAssembler>(Vec::new()},osr_loop_back_edges: graph.osr_loop_back_edges,+ osr_outer_loop_back_edges: graph.osr_outer_loop_back_edges,embedded_cells: graph.embedded_cells,dependencies: graph.dependencies,invalidation_patches: generated.invalidation_patches,diff --git a/Libraries/LibJS/Rust/src/jit/code.rs b/Libraries/LibJS/Rust/src/jit/code.rsindex b8c5959b737..8d9ea3dfaf6 100644--- a/Libraries/LibJS/Rust/src/jit/code.rs+++ b/Libraries/LibJS/Rust/src/jit/code.rs@@ -123,6 +123,9 @@ pub struct JitCode {/// The loop back edges from where a frame running in the interpreter reaches one of the `osr_entries` without/// leaving the loop it is in.osr_loop_back_edges: Vec<u32>,+ /// The loop back edges in the loops around those of the `osr_entries`, from where a frame running in the+ /// interpreter may reach one of them on a later iteration.+ osr_outer_loop_back_edges: Vec<u32>,/// Identifies the code among all code the VM installed, for the dependencies it registers (see/// `super::dependencies`).id: u64,@@ -161,6 +164,7 @@ impl JitCode {snapshot_executables,osr_entries,osr_loop_back_edges,+ osr_outer_loop_back_edges: Vec::new(),id: 0,invalidation_patches: Vec::new(),invalidated: Cell::new(false),@@ -174,6 +178,12 @@ impl JitCode {self}+ /// Gives the code the loop back edges in the loops around those of its on-stack replacement entries.+ pub fn with_osr_outer_loop_back_edges(mut self, osr_outer_loop_back_edges: Vec<u32>) -> Self {+ self.osr_outer_loop_back_edges = osr_outer_loop_back_edges;+ self+ }+pub fn id(&self) -> u64 {self.id}@@ -209,6 +219,12 @@ impl JitCode {self.osr_loop_back_edges.contains(&pc)}+ /// Whether a frame running in the interpreter at the loop back edge `pc` may reach an on-stack replacement entry+ /// on a later iteration of a loop it is in.+ pub fn may_reach_osr_entry_from(&self, pc: u32) -> bool {+ self.osr_outer_loop_back_edges.contains(&pc)+ }+pub fn site(&self, index: u32) -> &Site {&self.sites[index as usize]}@@ -277,6 +293,11 @@ pub(crate) struct CapturedBindingAccesses {/// it.pub const EXIT_COUNT_BEFORE_DISCARD: u32 = 10;+/// After this many exits, JIT code is discarded sooner, once a frame runs a loop in the interpreter from where it+/// reaches no on-stack replacement entry of the code. Code that exits and leaves frames looping in the interpreter+/// costs more than code that exits as often elsewhere.+pub const EXIT_COUNT_BEFORE_DISCARD_FROM_LOOP: u32 = 5;+/// An executable whose code was discarded this often is not compiled again.pub const MAX_DISCARD_COUNT: u32 = 8;diff --git a/Libraries/LibJS/Rust/src/jit/entry_exit.rs b/Libraries/LibJS/Rust/src/jit/entry_exit.rsindex f2e8d1447fe..95f82f89136 100644--- a/Libraries/LibJS/Rust/src/jit/entry_exit.rs+++ b/Libraries/LibJS/Rust/src/jit/entry_exit.rs@@ -12,7 +12,7 @@ use core::ptr::NonNull;use libjs_abi::register;use libjs_jit::code::SiteKind;-use super::code::{CompileState, EntryStatus, JitEntry, JitResult};+use super::code::{CompileState, EXIT_COUNT_BEFORE_DISCARD_FROM_LOOP, EntryStatus, JitEntry, JitResult};use super::translate::{self, RegisterDump};use super::{executable_of, frame_of, frames_above};use crate::bytecode::executable::Executable;@@ -89,6 +89,12 @@ pub fn enter_jit_code_at_loop(vm: &Vm, frame: NonNull<ExecutionContext>, pc: u32// loops would check at every back edge without ever reaching an entry.if code.reaches_osr_entry_from(pc) {executable.head.tier_up_budget.set(tier_up_costs::LOOP_ITERATION);+ } else if code.exit_count() >= EXIT_COUNT_BEFORE_DISCARD_FROM_LOOP && !code.may_reach_osr_entry_from(pc) {+ // NB: The frame may have exited into this loop, and no later iteration of a loop it is in reaches an entry.+ // Once the code is detached, a later check here compiles the executable again, with what the exits+ // taught it and an entry at this loop.+ vm.jit.count_coverage("discard-from-loop");+ executable.discard_jit_code(vm);}return None;};diff --git a/Libraries/LibJS/Rust/src/jit/tier_up.rs b/Libraries/LibJS/Rust/src/jit/tier_up.rsindex 4cdac11b426..9300a1cedef 100644--- a/Libraries/LibJS/Rust/src/jit/tier_up.rs+++ b/Libraries/LibJS/Rust/src/jit/tier_up.rs@@ -312,7 +312,8 @@ fn install(embedded_cells,snapshot,)- .with_invalidation(id, compiled.invalidation_patches);+ .with_invalidation(id, compiled.invalidation_patches)+ .with_osr_outer_loop_back_edges(compiled.osr_outer_loop_back_edges);vm.jit.dependents.register(vm, executable, id, &compiled.dependencies);// NB: Code may be installed while frames of the executable loop in the interpreter, because the compile was// queued (from the tier-up check of another executable, like a callee's), or because they exited from thediff --git a/Tests/LibJS/JIT/ir/osr-exits-into-loop.js b/Tests/LibJS/JIT/ir/osr-exits-into-loop.jsnew file mode 100644index 00000000000..7876aee72e3--- /dev/null+++ b/Tests/LibJS/JIT/ir/osr-exits-into-loop.js@@ -0,0 +1,26 @@+// Code that keeps exiting into a loop without an OSR entry is discarded after fewer exits than elsewhere, and+// compiled again with an entry at that loop.++function exitsIntoLoop(cold) {+ let sum = 0;+ for (let i = 0; i < 10; ++i) sum += i;+ if (cold) {+ for (let j = 0; j < 5000; ++j) {+ if (jit.inCompiledCode()) return -1;+ sum += j;+ }+ }+ return sum;+}+jit.prepare(exitsIntoLoop);+exitsIntoLoop(false);+jit.compile(exitsIntoLoop);+let calls = 0;+while (exitsIntoLoop(true) !== -1) {+ if (++calls === 7) throw new Error("The loop never continued in recompiled code");+}+if (jit.discardCount(exitsIntoLoop) !== 1) throw new Error("The code was not discarded once");++// CHECK-FUNCTION: exitsIntoLoop+// CHECK-PASS: graph building+// CHECK: osr entry at pc