Skip to content

Truly independent web browser

LibJS: Only poll every back edge in loops that reach an OSR entry

Andreas Klingdf2cbdd7c1be
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/osr.rs b/Libraries/LibJS/JIT/Rust/src/builder/osr.rs
index 291b21d7d79..fe40f5a8615 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/osr.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/osr.rs
@@ -124,6 +124,7 @@ impl GraphBuilder<'_> {
});
}
self.graph.osr_entries.push((pc, block));
+ self.record_osr_loop_back_edges(pc);
// NB: The loops the entry leads into other than at their header may
// only be reached from here over their back edges.
@@ -137,4 +138,34 @@ impl GraphBuilder<'_> {
}
Ok(())
}
+
+ /// 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.
+ 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 {
+ return;
+ };
+ // NB: The loop of a back edge is the innermost loop it is in. Its
+ // header may be the block of the back edge itself, like the test
+ // at the bottom of a for loop.
+ let Some(entered_loop) = cfg
+ .loops
+ .iter()
+ .filter(|current_loop| current_loop.blocks.contains(&back_edge_block))
+ .min_by_key(|current_loop| current_loop.blocks.len())
+ 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);
+ }
+ }
+ }
+ }
}
diff --git a/Libraries/LibJS/JIT/Rust/src/code.rs b/Libraries/LibJS/JIT/Rust/src/code.rs
index 10af75fd931..df60a5fc779 100644
--- a/Libraries/LibJS/JIT/Rust/src/code.rs
+++ b/Libraries/LibJS/JIT/Rust/src/code.rs
@@ -24,6 +24,9 @@ pub struct CompiledCode {
/// can continue there, with the same calling convention as the entry
/// point.
pub osr_entries: Vec<(u32, u32)>,
+ /// 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 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.rs
index c2df9d0ff90..6a573423134 100644
--- a/Libraries/LibJS/JIT/Rust/src/ir/mod.rs
+++ b/Libraries/LibJS/JIT/Rust/src/ir/mod.rs
@@ -1904,6 +1904,10 @@ pub struct Graph {
/// already running in the interpreter enters compiled code, with the pc of
/// the loop back edge each one starts at. The frame holds every value.
pub osr_entries: Vec<(u32, BlockId)>,
+ /// The pcs of the loop back edges in the loops of the on-stack
+ /// 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 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.rs
index d5bd6c99684..824b0cec129 100644
--- a/Libraries/LibJS/JIT/Rust/src/lib.rs
+++ b/Libraries/LibJS/JIT/Rust/src/lib.rs
@@ -130,6 +130,7 @@ pub fn compile_for<M: asm::PortableMacroAssembler>(
} else {
Vec::new()
},
+ osr_loop_back_edges: graph.osr_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.rs
index 551ea8c968b..b8c5959b737 100644
--- a/Libraries/LibJS/Rust/src/jit/code.rs
+++ b/Libraries/LibJS/Rust/src/jit/code.rs
@@ -120,6 +120,9 @@ pub struct JitCode {
snapshot_executables: Vec<SnapshotExecutable>,
/// Where a frame running in the interpreter at the loop back edge `pc` can continue in this code.
osr_entries: Vec<(u32, JitEntry)>,
+ /// 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>,
/// Identifies the code among all code the VM installed, for the dependencies it registers (see
/// `super::dependencies`).
id: u64,
@@ -134,6 +137,7 @@ impl JitCode {
memory: ExecutableMemory,
entry_offset: u32,
osr_entry_offsets: &[(u32, u32)],
+ osr_loop_back_edges: Vec<u32>,
sites: Vec<Site>,
embedded_cells: Vec<Gc<CellHeader>>,
snapshot_executables: Vec<SnapshotExecutable>,
@@ -156,6 +160,7 @@ impl JitCode {
embedded_cells,
snapshot_executables,
osr_entries,
+ osr_loop_back_edges,
id: 0,
invalidation_patches: Vec::new(),
invalidated: Cell::new(false),
@@ -198,8 +203,10 @@ impl JitCode {
.map(|(_, entry)| *entry)
}
- pub fn has_osr_entries(&self) -> bool {
- !self.osr_entries.is_empty()
+ /// Whether a frame running in the interpreter at the loop back edge `pc` reaches an on-stack replacement entry
+ /// without leaving the loop it is in.
+ pub fn reaches_osr_entry_from(&self, pc: u32) -> bool {
+ self.osr_loop_back_edges.contains(&pc)
}
pub fn site(&self, index: u32) -> &Site {
diff --git a/Libraries/LibJS/Rust/src/jit/entry_exit.rs b/Libraries/LibJS/Rust/src/jit/entry_exit.rs
index 4ef960a671c..f2e8d1447fe 100644
--- a/Libraries/LibJS/Rust/src/jit/entry_exit.rs
+++ b/Libraries/LibJS/Rust/src/jit/entry_exit.rs
@@ -85,8 +85,9 @@ pub fn enter_jit_code_at_loop(vm: &Vm, frame: NonNull<ExecutionContext>, pc: u32
let code = executable.jit_code().expect("the executable has JIT code");
let Some(entry) = code.osr_entry(pc) else {
// NB: A fixed budget can keep expiring at the same back edge in nested loops, missing the code's OSR entry
- // forever. Try the next back edge when the code has an entry at another loop.
- if code.has_osr_entries() {
+ // forever. Try the next back edge when the code has an entry at a loop this one is in. Frames in other
+ // 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);
}
return None;
diff --git a/Libraries/LibJS/Rust/src/jit/tier_up.rs b/Libraries/LibJS/Rust/src/jit/tier_up.rs
index 8c7365e1583..4cdac11b426 100644
--- a/Libraries/LibJS/Rust/src/jit/tier_up.rs
+++ b/Libraries/LibJS/Rust/src/jit/tier_up.rs
@@ -307,6 +307,7 @@ fn install(
memory,
compiled.entry_offset,
&compiled.osr_entries,
+ compiled.osr_loop_back_edges,
compiled.sites,
embedded_cells,
snapshot,