Skip to content

Truly independent web browser

LibJS/JIT: Rotate loops that on-stack replacement enters in their body

Andreas Klingdfda8dcd1641
diff --git a/Libraries/LibJS/JIT/ARCHITECTURE.md b/Libraries/LibJS/JIT/ARCHITECTURE.md
index 3fad946b08c..59bf9534dee 100644
--- a/Libraries/LibJS/JIT/ARCHITECTURE.md
+++ b/Libraries/LibJS/JIT/ARCHITECTURE.md
@@ -386,6 +386,7 @@ unreachable. Each one keeps the graph valid (`passes/verify.rs`), and IR tests i
| Pass | Contract |
|---|---|
+| Loop rotation | A loop that an on-stack replacement entry enters at another block than the code before it does (the entry at a test at the bottom that does more than branch, like `i < this.length`, enters the body) has no header that dominates it. The blocks that the block entered from before the loop dominates (the test) are copied for the way in from before the loop, so that the loop has one entry, unless that would merge it with an inner loop. |
| Trivial phi removal | Phis whose inputs are all one value (or the phi itself) become that value. |
| Truncation | Int32 additions, subtractions and unsigned right shifts whose users only read the low 32 bits of their results (bitwise operations, shifts, wrapping arithmetic, integer typed array stores) and no frame state holds become `Int32WrappingBinary`, which never exits. Comparisons of uint32 values (`x >>> 0`) first become unsigned comparisons of the shifted values. |
| Simplification | Constant folding and strength reduction; branches on constants become jumps; unboxing a box is the boxed value, and so is an int32 check of the boxed double of an int32; int32 operations that leave their left-hand side as it is, like `x \| 0`, become it. |
diff --git a/Libraries/LibJS/JIT/Rust/src/passes/loop_rotation.rs b/Libraries/LibJS/JIT/Rust/src/passes/loop_rotation.rs
new file mode 100644
index 00000000000..6a2bb74be1e
--- /dev/null
+++ b/Libraries/LibJS/JIT/Rust/src/passes/loop_rotation.rs
@@ -0,0 +1,410 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! Loop rotation.
+//!
+//! Loops with their test at the bottom start at the test: the code before the loop jumps there, and the test jumps
+//! back to the body. An on-stack replacement entry at a test that does more than branch (like `i < this.length`, whose
+//! property load starts the test's block) runs the branch itself and enters the body, so the loop has two entries:
+//! the test, from before the loop, and the body. Such a loop is irreducible: none of its blocks dominates the others,
+//! and loop invariant code motion leaves it alone.
+//!
+//! This pass rotates such loops: the blocks that the test dominates are copied for the paths that enter the test from
+//! before the loop, which then run the copy and enter the body (or leave the loop) from there, like the on-stack
+//! replacement entry does. The body becomes the loop's only entry, its header, and the test its latch. Values the
+//! test makes only reach the blocks it leads to through their phis, since it dominates none of them, so the copy's
+//! edges bring its values to those phis.
+
+use super::dominators::Dominators;
+use super::edit;
+use crate::bitset::BitSet;
+use crate::ir::Block;
+use crate::ir::BlockId;
+use crate::ir::FrameStateId;
+use crate::ir::Graph;
+use crate::ir::Node;
+use crate::ir::NodeId;
+use crate::ir::Op;
+
+/// How many nodes the blocks of a test that gets copied may have at most.
+const MAX_COPIED_NODES: usize = 64;
+
+pub fn run(graph: &mut Graph) {
+ if graph.osr_entries.is_empty() {
+ return;
+ }
+ let mut rotated = false;
+ // NB: Each rotation makes one irreducible loop reducible; a graph has fewer such loops than blocks.
+ for _ in 0..graph.blocks.len() {
+ let dominators = Dominators::compute(graph);
+ let Some((test, region, sources)) = find_rotatable_test(graph, &dominators) else {
+ break;
+ };
+ rotate(graph, test, &region, &sources);
+ rotated = true;
+ }
+ if rotated {
+ lay_out_in_reverse_post_order(graph);
+ edit::remove_trivial_phis(graph);
+ }
+}
+
+/// The blocks a depth-first walk from the entries reaches each block from over a retreating edge: an edge to a block
+/// the walk is still in.
+fn retreating_edges(graph: &Graph) -> Vec<Vec<BlockId>> {
+ let mut sources = vec![Vec::new(); graph.blocks.len()];
+ let mut state = vec![0u8; graph.blocks.len()];
+ const ON_STACK: u8 = 1;
+ const DONE: u8 = 2;
+ for entry in graph.entries() {
+ if state[entry.index()] != 0 {
+ continue;
+ }
+ let mut stack = vec![(entry, 0usize)];
+ state[entry.index()] = ON_STACK;
+ while let Some((block, next)) = stack.last_mut() {
+ let block = *block;
+ let successors = graph.successors(block);
+ let Some(successor) = successors.get(*next).copied() else {
+ state[block.index()] = DONE;
+ stack.pop();
+ continue;
+ };
+ *next += 1;
+ match state[successor.index()] {
+ 0 => {
+ state[successor.index()] = ON_STACK;
+ stack.push((successor, 0));
+ }
+ ON_STACK => sources[successor.index()].push(block),
+ _ => {}
+ }
+ }
+ }
+ sources
+}
+
+/// A block that a retreating edge it does not dominate leads to, with the blocks it dominates, if they can be copied
+/// for the paths that enter it from elsewhere, and the sources of the retreating edges to it.
+fn find_rotatable_test(graph: &Graph, dominators: &Dominators) -> Option<(BlockId, BitSet, Vec<BlockId>)> {
+ let retreating = retreating_edges(graph);
+ let entries = graph.entries().collect::<Vec<_>>();
+ 'blocks: for (index, sources) in retreating.iter().enumerate() {
+ let test = BlockId::from_index(index);
+ if sources.is_empty()
+ || entries.contains(&test)
+ || sources.iter().all(|source| dominators.dominates(test, *source))
+ {
+ continue;
+ }
+ let mut region = BitSet::new(graph.blocks.len());
+ let mut node_count = 0;
+ for (block, block_sources) in retreating.iter().enumerate() {
+ let block_id = BlockId::from_index(block);
+ if !dominators.is_reachable(block_id) || !dominators.dominates(test, block_id) {
+ continue;
+ }
+ region.insert(block);
+ node_count += graph.block_nodes(block_id).count();
+ // NB: Copying a loop inside the region would take more than copying its blocks.
+ if node_count > MAX_COPIED_NODES || (block != index && !block_sources.is_empty()) {
+ continue 'blocks;
+ }
+ }
+ let enters_from_before = graph
+ .block(test)
+ .predecessors
+ .iter()
+ .any(|predecessor| !sources.contains(predecessor));
+ // NB: Where the other entry enters an inner loop at its header, the test leads to that header, and rotating
+ // would make it the header of both loops: the inner loop would be no loop of its own anymore.
+ let leads_to_loop_header = region.iter().any(|block| {
+ graph
+ .successors(BlockId::from_index(block))
+ .iter()
+ .any(|successor| !region.contains(successor.index()) && graph.block(*successor).is_loop_header)
+ });
+ if sources.iter().any(|source| region.contains(source.index())) || !enters_from_before || leads_to_loop_header {
+ continue;
+ }
+ return Some((test, region, sources.clone()));
+ }
+ None
+}
+
+/// Copies the blocks of `region`, which `test` dominates, for the predecessors of `test` that enter it from before
+/// the loop (all but the `sources` of retreating edges), and makes those predecessors branch to the copy of `test`.
+fn rotate(graph: &mut Graph, test: BlockId, region: &BitSet, sources: &[BlockId]) {
+ let entering = graph
+ .block(test)
+ .predecessors
+ .iter()
+ .map(|predecessor| !sources.contains(predecessor))
+ .collect::<Vec<_>>();
+
+ // The copies of the blocks, and of their nodes, with the inputs and frame states of the originals for now. The
+ // copy of the test takes the phi inputs from the predecessors that enter it.
+ let mut block_copy = vec![None; graph.blocks.len()];
+ let mut node_copy: Vec<Option<NodeId>> = vec![None; graph.nodes.len()];
+ let blocks = region.iter().map(BlockId::from_index).collect::<Vec<_>>();
+ for &block in &blocks {
+ let data = graph.block(block);
+ let copy = graph.add_block(Block {
+ bytecode_start: data.bytecode_start,
+ is_cold: data.is_cold,
+ start_frame_state: data.start_frame_state,
+ ..Block::default()
+ });
+ block_copy[block.index()] = Some(copy);
+ }
+ let copied = |block: BlockId, block_copy: &[Option<BlockId>]| block_copy[block.index()].expect("in the region");
+ let test_copy = copied(test, &block_copy);
+ for phi in graph.block(test).phis.clone() {
+ let inputs = graph
+ .node(phi)
+ .inputs
+ .iter()
+ .zip(&entering)
+ .filter(|(_, entering)| **entering)
+ .map(|(input, _)| *input)
+ .collect::<Vec<_>>();
+ node_copy[phi.index()] = Some(if inputs.iter().all(|input| *input == inputs[0]) {
+ inputs[0]
+ } else {
+ let data = graph.node(phi);
+ let copy = graph.add_node(Node::new(Op::Phi, inputs, data.repr, data.pc));
+ graph.blocks[test_copy.index()].phis.push(copy);
+ copy
+ });
+ }
+ let mut copies = Vec::new();
+ for &block in &blocks {
+ let copy = copied(block, &block_copy);
+ let data = graph.block(block).clone();
+ let phis = if block == test { Vec::new() } else { data.phis.clone() };
+ for (node, is_phi) in phis
+ .iter()
+ .map(|node| (*node, true))
+ .chain(data.body.iter().chain(&data.control).map(|node| (*node, false)))
+ {
+ let new = graph.add_node(graph.node(node).clone());
+ node_copy.resize(graph.nodes.len(), None);
+ node_copy[node.index()] = Some(new);
+ copies.push(new);
+ let copy_data = &mut graph.blocks[copy.index()];
+ if is_phi {
+ copy_data.phis.push(new);
+ } else if Some(node) == data.control {
+ copy_data.control = Some(new);
+ } else {
+ copy_data.body.push(new);
+ }
+ }
+ if block != test {
+ graph.blocks[copy.index()].predecessors = data
+ .predecessors
+ .iter()
+ .map(|predecessor| copied(*predecessor, &block_copy))
+ .collect();
+ }
+ }
+ let map =
+ |node: NodeId, node_copy: &[Option<NodeId>]| node_copy.get(node.index()).copied().flatten().unwrap_or(node);
+
+ // The copies use the copies of the region's values, and exit with copies of the frame states that refer to them.
+ // Frame states that refer to none (like those of the callers of inlined calls) stay shared.
+ let mut frame_state_copy: Vec<Option<FrameStateId>> = vec![None; graph.frame_states.len()];
+ let mut copy_frame_state = |graph: &mut Graph, id: FrameStateId| -> FrameStateId {
+ let chain = graph.frame_state_chain(id);
+ let mut copied_parent = None;
+ for id in chain.iter().rev() {
+ if let Some(copy) = frame_state_copy[id.index()] {
+ copied_parent = Some(copy);
+ continue;
+ }
+ let mut state = graph.frame_state(*id).clone();
+ for (_, value) in &mut state.values {
+ *value = map(*value, &node_copy);
+ }
+ if copied_parent.is_some() {
+ state.parent = copied_parent;
+ }
+ let copy = if state == *graph.frame_state(*id) {
+ *id
+ } else {
+ graph.add_frame_state(state)
+ };
+ frame_state_copy[id.index()] = Some(copy);
+ copied_parent = Some(copy);
+ }
+ copied_parent.expect("the chain is not empty")
+ };
+ for &copy in &copies {
+ let inputs = graph
+ .node(copy)
+ .inputs
+ .iter()
+ .map(|input| map(*input, &node_copy))
+ .collect();
+ graph.nodes[copy.index()].inputs = inputs;
+ if let Some(frame_state) = graph.node(copy).frame_state {
+ graph.nodes[copy.index()].frame_state = Some(copy_frame_state(graph, frame_state));
+ }
+ }
+ for &block in &blocks {
+ let copy = copied(block, &block_copy);
+ for successor in graph.successors(copy).iter().copied().collect::<Vec<_>>() {
+ if let Some(successor_copy) = block_copy[successor.index()] {
+ graph.replace_successor(copy, successor, successor_copy);
+ }
+ }
+ if let Some(frame_state) = graph.block(copy).start_frame_state {
+ graph.blocks[copy.index()].start_frame_state = Some(copy_frame_state(graph, frame_state));
+ }
+ }
+
+ // The edges of the copies out of the region bring the copies of the values the originals bring.
+ for &block in &blocks {
+ let copy = copied(block, &block_copy);
+ for successor in graph.successors(block).iter().copied().collect::<Vec<_>>() {
+ if region.contains(successor.index()) {
+ continue;
+ }
+ let positions = graph
+ .block(successor)
+ .predecessors
+ .iter()
+ .enumerate()
+ .filter(|(_, predecessor)| **predecessor == block)
+ .map(|(position, _)| position)
+ .collect::<Vec<_>>();
+ // NB: A block that branches to the same block twice is its predecessor twice.
+ let already = graph
+ .block(successor)
+ .predecessors
+ .iter()
+ .filter(|predecessor| **predecessor == copy)
+ .count();
+ let position = positions[already];
+ graph.blocks[successor.index()].predecessors.push(copy);
+ for phi in graph.block(successor).phis.clone() {
+ let input = map(graph.node(phi).inputs[position], &node_copy);
+ graph.nodes[phi.index()].inputs.push(input);
+ }
+ }
+ }
+
+ // The blocks of the loop that the test leads to are its header now, which a rotation of a loop around it must not
+ // make the header of that loop too.
+ for &block in &blocks {
+ for successor in graph.successors(block).iter().copied().collect::<Vec<_>>() {
+ if !region.contains(successor.index()) && reaches_without(graph, successor, sources, test) {
+ graph.blocks[successor.index()].is_loop_header = true;
+ }
+ }
+ }
+ graph.blocks[test.index()].is_loop_header = false;
+
+ // The predecessors that entered the test enter its copy instead, and the test keeps the others.
+ let predecessors = graph.block(test).predecessors.clone();
+ for (predecessor, entering) in predecessors.iter().zip(&entering) {
+ if *entering {
+ graph.replace_successor(*predecessor, test, test_copy);
+ }
+ }
+ graph.blocks[test_copy.index()].predecessors = predecessors
+ .iter()
+ .zip(&entering)
+ .filter(|(_, entering)| **entering)
+ .map(|(predecessor, _)| *predecessor)
+ .collect();
+ graph.blocks[test.index()].predecessors = predecessors
+ .iter()
+ .zip(&entering)
+ .filter(|(_, entering)| !**entering)
+ .map(|(predecessor, _)| *predecessor)
+ .collect();
+ for phi in graph.block(test).phis.clone() {
+ let inputs = graph
+ .node(phi)
+ .inputs
+ .iter()
+ .zip(&entering)
+ .filter(|(_, entering)| !**entering)
+ .map(|(input, _)| *input)
+ .collect();
+ graph.nodes[phi.index()].inputs = inputs;
+ }
+ for (guard, nodes) in graph.legacy_arguments_guards.clone() {
+ if let Some(copy) = node_copy.get(guard.index()).copied().flatten() {
+ let nodes = nodes.iter().map(|node| map(*node, &node_copy)).collect();
+ graph.legacy_arguments_guards.push((copy, nodes));
+ }
+ }
+ for node in graph.slow_paths_initializing_frame.clone() {
+ if let Some(copy) = node_copy.get(node.index()).copied().flatten() {
+ graph.slow_paths_initializing_frame.push(copy);
+ }
+ }
+}
+
+/// Whether a path that does not go through `avoided` leads from `start` to one of `targets`.
+fn reaches_without(graph: &Graph, start: BlockId, targets: &[BlockId], avoided: BlockId) -> bool {
+ let mut visited = BitSet::new(graph.blocks.len());
+ let mut pending = vec![start];
+ while let Some(block) = pending.pop() {
+ if targets.contains(&block) {
+ return true;
+ }
+ if block == avoided || visited.contains(block.index()) {
+ continue;
+ }
+ visited.insert(block.index());
+ pending.extend(graph.successors(block).iter().copied());
+ }
+ false
+}
+
+/// Orders the blocks so that each comes after its predecessors but those that reach it over a retreating edge, and
+/// marks the blocks such edges lead to as loop headers too.
+fn lay_out_in_reverse_post_order(graph: &mut Graph) {
+ let mut post_order = Vec::with_capacity(graph.blocks.len());
+ let mut visited = vec![false; graph.blocks.len()];
+ // NB: The walk starts at the on-stack replacement entries, so that block 0 comes first.
+ let entries = graph.entries().collect::<Vec<_>>();
+ for &entry in entries.iter().rev() {
+ if visited[entry.index()] {
+ continue;
+ }
+ visited[entry.index()] = true;
+ let mut stack = vec![(entry, graph.successors(entry).len())];
+ while let Some((block, remaining)) = stack.last_mut() {
+ let block = *block;
+ if *remaining == 0 {
+ post_order.push(block);
+ stack.pop();
+ continue;
+ }
+ *remaining -= 1;
+ // NB: Successors are visited last to first, so that the first comes right after its block.
+ let successor = graph.successors(block)[*remaining];
+ if !visited[successor.index()] {
+ visited[successor.index()] = true;
+ stack.push((successor, graph.successors(successor).len()));
+ }
+ }
+ }
+ post_order.reverse();
+ graph.reorder_blocks(&post_order);
+ for index in 0..graph.blocks.len() {
+ let is_cold = graph.blocks[index].is_cold;
+ let reached_from_later = graph.blocks[index]
+ .predecessors
+ .iter()
+ .any(|predecessor| predecessor.index() >= index && !(graph.block(*predecessor).is_cold && !is_cold));
+ graph.blocks[index].is_loop_header |= reached_from_later;
+ }
+}
diff --git a/Libraries/LibJS/JIT/Rust/src/passes/mod.rs b/Libraries/LibJS/JIT/Rust/src/passes/mod.rs
index 3dc4c839b82..45d843a4e0c 100644
--- a/Libraries/LibJS/JIT/Rust/src/passes/mod.rs
+++ b/Libraries/LibJS/JIT/Rust/src/passes/mod.rs
@@ -25,6 +25,7 @@ mod facts;
mod frame_initialization;
mod gvn;
mod licm;
+mod loop_rotation;
mod representation;
mod simplify;
mod truncation;
@@ -44,6 +45,10 @@ struct Pass {
/// The passes, in the order they run.
const PASSES: &[Pass] = &[
+ Pass {
+ name: "loop rotation",
+ run: loop_rotation::run,
+ },
Pass {
name: "trivial phi removal",
run: edit::remove_trivial_phis,
diff --git a/Tests/LibJS/JIT/ir/loop-rotation.js b/Tests/LibJS/JIT/ir/loop-rotation.js
new file mode 100644
index 00000000000..de9888705f2
--- /dev/null
+++ b/Tests/LibJS/JIT/ir/loop-rotation.js
@@ -0,0 +1,34 @@
+// OPTIONS: stress-osr
+// An on-stack replacement entry at the test at the bottom of a loop that reads the bound from a property runs the
+// test's branch itself and enters the loop's body, while the code before the loop enters it at the test. Loop rotation
+// copies the test for the way in from before the loop, so that the body becomes the loop's only entry, and loop
+// invariant code motion moves the loads of `this` out of it.
+
+function Holder(values) {
+ this.values = values;
+ this.count = values.length;
+}
+Holder.prototype.sum = function sum() {
+ let total = 0;
+ for (let i = 0; i < this.count; ++i) total += this.values[i];
+ return total;
+};
+const holder = new Holder([1, 2, 3, 4]);
+jit.prepare(Holder.prototype.sum);
+for (let i = 0; i < 10; ++i) holder.sum();
+jit.compile(Holder.prototype.sum);
+
+// CHECK-FUNCTION: sum
+// CHECK-PASS: loop rotation
+// CHECK: osr entry at pc
+// CHECK-PASS: loop invariant code motion
+// CHECK: CheckShape
+// CHECK: CheckShape
+// CHECK: CheckShape
+// CHECK: loop:
+// CHECK-NOT: CheckShape
+// CHECK-NOT: LoadNamed
+// CHECK: LoadElementAt Packed
+// CHECK-NOT: CheckShape
+// CHECK-NOT: LoadNamed
+// CHECK: osr entry at pc
diff --git a/Tests/LibJS/Runtime/jit/loops-with-property-tests.js b/Tests/LibJS/Runtime/jit/loops-with-property-tests.js
new file mode 100644
index 00000000000..e5a82319745
--- /dev/null
+++ b/Tests/LibJS/Runtime/jit/loops-with-property-tests.js
@@ -0,0 +1,105 @@
+// Loops whose test reads the bound from a property, or runs other code, and that compiled code enters at that test.
+// These tests run each loop long enough to be compiled with a low JIT threshold first, often from inside it.
+
+const iterations = 100;
+
+describe("Loops with tests that do more than compare", () => {
+ test("bounds read from properties", () => {
+ function Holder(values) {
+ this.values = values;
+ this.count = values.length;
+ }
+ Holder.prototype.sum = function () {
+ let total = 0;
+ for (let i = 0; i < this.count; ++i) total += this.values[i];
+ return total;
+ };
+ const holder = new Holder(Array.from({ length: 1000 }, (_, i) => i));
+ for (let i = 0; i < iterations; ++i) expect(holder.sum()).toBe(499500);
+ expect(new Holder([]).sum()).toBe(0);
+ expect(new Holder([1.5, 2]).sum()).toBe(3.5);
+ });
+
+ test("tests that run code", () => {
+ let calls = 0;
+ const object = {
+ get length() {
+ ++calls;
+ return 300;
+ },
+ };
+ function count(object) {
+ let n = 0;
+ for (let i = 0; i < object.length; ++i) ++n;
+ return n;
+ }
+ for (let i = 0; i < iterations; ++i) {
+ calls = 0;
+ expect(count(object)).toBe(300);
+ expect(calls).toBe(301);
+ }
+ });
+
+ test("bounds that change in the loop", () => {
+ function shrink(object) {
+ let n = 0;
+ for (let i = 0; i < object.length; ++i) {
+ if (i === 100) object.length = 150;
+ ++n;
+ }
+ return n;
+ }
+ for (let i = 0; i < iterations; ++i) expect(shrink({ length: 1000 })).toBe(150);
+ expect(shrink({ length: 50 })).toBe(50);
+ expect(shrink({ length: "80" })).toBe(80);
+ });
+
+ test("while loops and loops that never run", () => {
+ function drain(queue) {
+ let total = 0;
+ while (queue.items.length > 0) total += queue.items.pop();
+ return total;
+ }
+ for (let i = 0; i < iterations; ++i) {
+ expect(drain({ items: Array.from({ length: 200 }, (_, j) => j) })).toBe(19900);
+ expect(drain({ items: [] })).toBe(0);
+ }
+ });
+
+ test("nested loops", () => {
+ function total(rows) {
+ let sum = 0;
+ for (let r = 0; r < rows.length; ++r) {
+ for (let c = 0; c < rows[r].length; ++c) sum += rows[r][c];
+ }
+ return sum;
+ }
+ const rows = Array.from({ length: 30 }, (_, r) => Array.from({ length: r }, (_, c) => c));
+ let expected = 0;
+ for (let r = 0; r < 30; ++r) expected += (r * (r - 1)) / 2;
+ for (let i = 0; i < iterations; ++i) expect(total(rows)).toBe(expected);
+ expect(total([[1], [], [2, 3]])).toBe(6);
+ });
+
+ test("stores into arrays made with a length", () => {
+ function Signal(length, scale) {
+ this.length = length;
+ this.samples = new Array(length);
+ this.scale = scale;
+ }
+ Signal.prototype.fill = function () {
+ for (let i = 0; i < this.length; ++i) this.samples[i] = Math.round(i * this.scale) % 16;
+ return this.samples;
+ };
+ for (let i = 0; i < 20; ++i) {
+ const samples = new Signal(1000, 0.75).fill();
+ expect(samples.length).toBe(1000);
+ expect(Object.keys(samples).length).toBe(1000);
+ expect(samples[999]).toBe(Math.round(999 * 0.75) % 16);
+ expect(samples.indexOf(undefined)).toBe(-1);
+ }
+ const signal = new Signal(10, 1);
+ signal.samples.length = 5;
+ expect(signal.fill()).toEqual([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
+ });
+});