Skip to content

Truly independent web browser

LibJS: Hoist loads of unwritten slots out of loops that call

Andreas Klingdab89c46db6c
diff --git a/Libraries/LibJS/JIT/ARCHITECTURE.md b/Libraries/LibJS/JIT/ARCHITECTURE.md
index 6bfbea353b2..a1ca8cc7dfe 100644
--- a/Libraries/LibJS/JIT/ARCHITECTURE.md
+++ b/Libraries/LibJS/JIT/ARCHITECTURE.md
@@ -52,7 +52,7 @@ frame memory (`LoadSlot`) where the code starts using it: every slot live at an
a node that writes the frame wrote, right after it. Merges and loop headers never write frame
memory: a slot the paths disagree on (or a loop assigns) gets a phi, except that a slot every path
loaded, and memory still holds, is loaded again at the merge (loop invariant code motion takes such
-loads out of loops). The arguments and the `this` value register that no instruction writes keep
+loads out of loops, those of `Graph::unwritten_slots` even out of loops that call). The arguments and the `this` value register that no instruction writes keep
the values the function started with (`Graph::unwritten_slots`), and nothing stores to them, so the
values loaded from them need no spill slots (section 7). Frame memory is written
only where something reads it: before calls that read the frame and before `Suspend` (`StoreSlot`),
@@ -417,7 +417,7 @@ unreachable. Each one keeps the graph valid (`passes/verify.rs`), and IR tests i
| Check elimination | A forward dataflow of facts removes checks that passed already, decides branches that a branch on the same values decided before, and reuses loads whose value is known; may turn shape checks into `AssumeValid` plus a stable shape dependency. |
| Simplification | Again, for what the branches that check elimination decided left foldable (like the double path of `Math.abs` of an int32). Int32 checks of boxed doubles become `CheckFloat64Int32` of the doubles. |
| Global value numbering | Again, for the nodes that check elimination and simplification made the same. |
-| Loop invariant code motion | Gives loops entered from several blocks a merged preheader, then moves effect-free nodes with inputs from outside the loop to it (but calls in cold blocks); nodes that may exit too, exiting to the loop header, and not where that kind of exit was taken there before. Those from blocks that not every iteration runs, but for cold ones, move speculatively: they exit with `Hoisted`, and stay in the loop where such an exit was taken there before. |
+| Loop invariant code motion | Gives loops entered from several blocks a merged preheader, then moves effect-free nodes with inputs from outside the loop to it (but calls in cold blocks), and loads of slots nothing writes even where the loop calls; nodes that may exit too, exiting to the loop header, and not where that kind of exit was taken there before. Those from blocks that not every iteration runs, but for cold ones, move speculatively: they exit with `Hoisted`, and stay in the loop where such an exit was taken there before. |
| Bounds check elimination | Bounds checks against constant counts go away where the index is known to be in range: induction variables of loops whose back edge compares the grown counter with a constant bound, merges of values from outside a loop, and int32 arithmetic on them that cannot overflow. The counter an on-stack replacement entry brings is checked against the bound once where it enters, unless such a check exited there before. With the same ranges, `((x << k) + c) >> k` becomes `x + (c >> k)` where nothing overflows and `c` is a multiple of 2^k. Int32 additions, subtractions and multiplications become `Int32WrappingBinary` where the ranges of their operands keep the result an int32 (and not -0), with operands also bounded by the masks and shifts of any int32, and by the dominating bounds checks and in-bounds branches of indices. |
| Interrupt check elimination | Small innermost loops that count towards an end lose their `CheckInterrupt`, if they run under 5000 times or a loop around them keeps its check (section 9, "Interrupts"). |
| Exit unboxing | Frame states take the unboxed value of an int32, float64 or bool boxing that only frame states use, so that exits box it instead of the fast path. |
diff --git a/Libraries/LibJS/JIT/Rust/src/passes/licm.rs b/Libraries/LibJS/JIT/Rust/src/passes/licm.rs
index 393128a2d39..6e5ff1c3f20 100644
--- a/Libraries/LibJS/JIT/Rust/src/passes/licm.rs
+++ b/Libraries/LibJS/JIT/Rust/src/passes/licm.rs
@@ -8,7 +8,9 @@
//!
//! Nodes in a loop whose inputs are all defined outside of it, which have
//! no effects and read no memory the loop may write, move to the loop's
-//! preheader: the one block outside the loop that enters it. Loops entered
+//! preheader: the one block outside the loop that enters it. Loads of the
+//! slots that nothing writes (`Graph::unwritten_slots`) read memory no loop
+//! writes, even one that calls. Loops entered
//! from several blocks (like the function entry and an on-stack replacement
//! entry) first get a preheader that merges what those bring.
//!
@@ -202,7 +204,14 @@ pub fn run(graph: &mut Graph) {
let is_cold = graph.block(block_id).is_cold;
for node in graph.block(block_id).body.clone() {
let data = graph.node(node);
- let properties = data.op.properties();
+ let mut properties = data.op.properties();
+ // OPTIMIZATION: Nothing writes these slots, so their loads leave even loops that call, like the
+ // reloads the builder puts at loop headers (see `GraphBuilder::merge()`).
+ if let Op::LoadSlot { slot } = data.op
+ && graph.is_unwritten_slot(slot)
+ {
+ properties.reads = Locations::NONE;
+ }
let invariant_inputs = data
.inputs
.iter()
diff --git a/Libraries/LibJS/JIT/Rust/src/passes/tests.rs b/Libraries/LibJS/JIT/Rust/src/passes/tests.rs
index 96950ce8b26..7af3ba0d525 100644
--- a/Libraries/LibJS/JIT/Rust/src/passes/tests.rs
+++ b/Libraries/LibJS/JIT/Rust/src/passes/tests.rs
@@ -350,6 +350,66 @@ fn element_counts_move_out_of_loops_that_only_store_elements() {
assert_eq!(graph.blocks[1].body, vec![store]);
}
+/// A loop that calls, and loads a slot that nothing writes and one that something may: only the first load moves out
+/// of the loop, since code the call runs may write any frame memory but that of slots nothing writes.
+#[test]
+fn loads_of_slots_that_nothing_writes_move_out_of_loops_that_call() {
+ let mut graph = Graph::default();
+ for predecessors in [vec![], vec![BlockId(0), BlockId(1)], vec![BlockId(1)]] {
+ graph.add_block(Block {
+ predecessors,
+ ..Block::default()
+ });
+ }
+ graph.unwritten_slots = crate::bitset::BitSet::new(8);
+ graph.unwritten_slots.insert(7);
+ let enter = node(&mut graph, Op::Jump { target: BlockId(1) }, vec![], None);
+ graph.blocks[0].control = Some(enter);
+ let unwritten = node(&mut graph, Op::LoadSlot { slot: 7 }, vec![], Some(Repr::Tagged));
+ let written = node(&mut graph, Op::LoadSlot { slot: 6 }, vec![], Some(Repr::Tagged));
+ let call = node(
+ &mut graph,
+ Op::Generic {
+ opcode: crate::bytecode::OpCode::Call,
+ executable: 0,
+ pc: 0,
+ },
+ vec![],
+ None,
+ );
+ graph.nodes[call.index()].frame_state = Some(graph.add_frame_state(crate::ir::FrameState {
+ executable: 0,
+ pc: 0,
+ mode: crate::code::ResumeMode::ResumeAt,
+ values: Vec::new(),
+ in_frame: Vec::new(),
+ parent: None,
+ passed_argument_count: None,
+ legacy_arguments: 0..0,
+ }));
+ let more = node(
+ &mut graph,
+ Op::Branch {
+ condition: BranchCondition::Nullish,
+ if_true: BlockId(1),
+ if_false: BlockId(2),
+ },
+ vec![written],
+ None,
+ );
+ graph.blocks[1].is_loop_header = true;
+ graph.blocks[1].body.extend([unwritten, written, call]);
+ graph.blocks[1].control = Some(more);
+ let ret = node(&mut graph, Op::Return, vec![unwritten], None);
+ graph.blocks[2].control = Some(ret);
+ verify(&graph).unwrap();
+
+ super::licm::run(&mut graph);
+ assert_eq!(graph.blocks[0].body, vec![unwritten], "{:?}", graph.blocks);
+ assert_eq!(graph.blocks[1].body, vec![written, call]);
+ verify(&graph).unwrap();
+}
+
#[test]
fn typed_array_storage_offsets_are_reused_across_stores_but_not_calls() {
use crate::ir::{ElementsKind, TypedArrayElement};