Skip to content

Truly independent web browser

LibJS: Stop sinking entry loads in the JIT's builder

Andreas Kling35706d049dcd
diff --git a/Libraries/LibJS/JIT/ARCHITECTURE.md b/Libraries/LibJS/JIT/ARCHITECTURE.md
index e7492e0c517..6bfbea353b2 100644
--- a/Libraries/LibJS/JIT/ARCHITECTURE.md
+++ b/Libraries/LibJS/JIT/ARCHITECTURE.md
@@ -52,10 +52,9 @@ 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 loads at the start of the function move down to where their uses meet,
-outside of loops, as far as nothing on the way may write the slot's memory. 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. Frame memory is written
+loads out of loops). 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`),
where the compiled function assigns an argument that its legacy `arguments` shows (which any code it
calls can read), and by exits and leaves through the translator.
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/mod.rs b/Libraries/LibJS/JIT/Rust/src/builder/mod.rs
index d0065634608..35ab6602145 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/mod.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/mod.rs
@@ -84,7 +84,6 @@ use crate::ir::NodeId;
use crate::ir::Op;
use crate::ir::SuspendKind;
use crate::ir::value;
-use crate::passes::dominators::Dominators;
use crate::snapshot::CallFeedbackSnapshot;
use crate::snapshot::CellId;
use crate::snapshot::ExecutableSnapshot;
@@ -299,176 +298,6 @@ fn loops_replacing_environments(cfg: &Cfg, instructions: &[DecodedInstruction])
.collect()
}
-/// Whether `op` may write the frame memory of `slot` (an operand) in a frame whose arguments start at
-/// `arguments_base`.
-fn writes_slot_memory(op: &Op, slot: u32, arguments_base: u32) -> bool {
- match op {
- Op::StoreSlot { slot: stored } => *stored == slot,
- // NB: They empty the registers after the reserved ones and the locals, and copy the constants.
- Op::InitializeFrame | Op::EnsureFrameInitialized => (RESERVED_REGISTER_COUNT..arguments_base).contains(&slot),
- // NB: They write fields of the frame, not its slots.
- Op::PublishFrame | Op::SetLexicalEnvironment | Op::LeavePrivateEnvironment => false,
- _ => {
- let properties = op.properties();
- properties.runs_code || properties.writes.intersects(Locations::FRAME)
- }
- }
-}
-
-/// OPTIMIZATION: Moves the loads at the start of the compiled function (see `GraphBuilder::load_entry_slots()`) down
-/// the dominator tree, to the block where their uses meet, or the closest one outside of loops above
-/// it: those of slots that keep their values (`Graph::unwritten_slots`) anywhere, and the others where
-/// nothing on the way there may write their frame memory. Values that only code far from the start or on
-/// a few paths uses then do not take registers or spill slots from the start.
-fn sink_entry_loads(graph: &mut Graph, arguments_base: u32) {
- let entry = BlockId(0);
- let loads = graph
- .block(entry)
- .body
- .iter()
- .copied()
- .filter(|node| matches!(graph.node(*node).op, Op::LoadSlot { .. }))
- .collect::<Vec<_>>();
- if loads.is_empty() {
- return;
- }
- let mut is_load = BitSet::new(graph.nodes.len());
- for load in &loads {
- is_load.insert(load.index());
- }
- let dominators = Dominators::compute(graph);
- // NB: A phi uses its input at the end of the predecessor it comes from, a frame state uses its values (and those
- // of its parents) where its node is, and the frame state at the start of a block uses its values before the
- // block, at the end of its immediate dominator.
- let mut meet: HashMap<NodeId, BlockId> = HashMap::default();
- let add_use = |value: NodeId, block: BlockId, meet: &mut HashMap<NodeId, BlockId>| {
- if !is_load.contains(value.index()) {
- return;
- }
- let mut common = *meet.entry(value).or_insert(block);
- while !dominators.dominates(common, block) {
- common = dominators
- .idom(common)
- .expect("the entry dominates every use of its loads");
- }
- meet.insert(value, common);
- };
- let add_frame_state_uses = |mut frame_state: Option<FrameStateId>, block: BlockId, meet: &mut HashMap<_, _>| {
- while let Some(id) = frame_state {
- let state = graph.frame_state(id);
- for (_, value) in &state.values {
- add_use(*value, block, meet);
- }
- frame_state = state.parent;
- }
- };
- for (index, block) in graph.blocks.iter().enumerate() {
- let block_id = BlockId::from_index(index);
- if let Some(dominator) = dominators.idom(block_id) {
- add_frame_state_uses(block.start_frame_state, dominator, &mut meet);
- }
- for phi in &block.phis {
- for (input, predecessor) in graph.node(*phi).inputs.iter().zip(&block.predecessors) {
- add_use(*input, *predecessor, &mut meet);
- }
- }
- for node in block.body.iter().chain(&block.control) {
- let data = graph.node(*node);
- for input in &data.inputs {
- add_use(*input, block_id, &mut meet);
- }
- add_frame_state_uses(data.frame_state, block_id, &mut meet);
- }
- }
- let mut in_loop = BitSet::new(graph.blocks.len());
- for (index, block) in graph.blocks.iter().enumerate() {
- let header = BlockId::from_index(index);
- if block.is_loop_header && dominators.is_reachable(header) {
- let back_edge_sources = dominators.back_edge_sources(graph, header);
- for member in dominators.loop_blocks(graph, header, &back_edge_sources).iter() {
- in_loop.insert(member);
- }
- }
- }
- for load in loads {
- let Some(mut target) = meet.get(&load).copied() else {
- continue;
- };
- while in_loop.contains(target.index()) {
- target = dominators.idom(target).expect("the entry is in no loop");
- }
- if target == entry {
- continue;
- }
- let Op::LoadSlot { slot } = graph.node(load).op else {
- unreachable!("only loads sink");
- };
- let keeps_value = graph.is_unwritten_slot(slot);
- // NB: The other slots sink no further than the first node that may write their memory, on the way to the
- // target block (every block from where it can be reached, before it) or in it.
- let writes = |node: &NodeId| !keeps_value && writes_slot_memory(&graph.node(*node).op, slot, arguments_base);
- if !keeps_value {
- let mut on_the_way = BitSet::new(graph.blocks.len());
- let mut pending = graph.block(target).predecessors.clone();
- while let Some(block) = pending.pop() {
- if block == target || on_the_way.contains(block.index()) {
- continue;
- }
- on_the_way.insert(block.index());
- pending.extend(graph.block(block).predecessors.iter().copied());
- }
- if on_the_way.iter().any(|block| {
- graph.blocks[block]
- .body
- .iter()
- .chain(&graph.blocks[block].control)
- .any(writes)
- }) {
- continue;
- }
- }
- // NB: The load goes before the first node of the block that uses it, ahead of the nodes built for the same
- // instruction before that one, like a load of an operand the instruction reads, or at the end of the
- // block. Refinements stay at the heads of their blocks, and refine values their single predecessors
- // branched on, which therefore use them as well.
- let uses = |node: &NodeId| {
- let data = graph.node(*node);
- let mut frame_state = data.frame_state;
- let mut used = data.inputs.contains(&load);
- while let Some(id) = frame_state.filter(|_| !used) {
- let state = graph.frame_state(id);
- used = state.values.iter().any(|(_, value)| *value == load);
- frame_state = state.parent;
- }
- used
- };
- let body = &graph.blocks[target.index()].body;
- let heads = body
- .iter()
- .take_while(|node| matches!(graph.node(**node).op, Op::Refine { .. }))
- .count();
- if body[..heads].iter().any(uses) {
- continue;
- }
- if body[..heads].iter().any(writes) {
- continue;
- }
- let mut position = heads
- + body[heads..]
- .iter()
- .position(|node| uses(node) || writes(node))
- .unwrap_or(body.len() - heads);
- if let Some(user) = body.get(position) {
- let pc = graph.node(*user).pc;
- while position > heads && graph.node(body[position - 1]).pc == pc {
- position -= 1;
- }
- }
- graph.blocks[entry.index()].body.retain(|node| *node != load);
- graph.blocks[target.index()].body.insert(position, load);
- }
-}
-
/// What ends the instruction currently being built.
enum Flow {
/// The block continues with the next instruction.
@@ -828,7 +657,6 @@ impl<'a> GraphBuilder<'a> {
}
}
graph.unwritten_slots = unwritten_slots;
- sink_entry_loads(&mut graph, self.function.layout.arguments_base());
Ok(graph)
}
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/tests.rs b/Libraries/LibJS/JIT/Rust/src/builder/tests.rs
index 92a9a932f6b..41f9bff8a5e 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/tests.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/tests.rs
@@ -217,9 +217,9 @@ fn straight_line_code_renames_moves_and_passes_values_to_slow_paths() {
&dump_of(&program),
"
[[B0]]:
+ [[V5]] = LoadSlot a0
Jump [[B1]]
[[B1]] (pc 0) <- [[B0]]:
- [[V5]] = LoadSlot a0
InitializeFrame
[[V6]] = CallSlowPath Exp @7 (#10, [[V5]]) [[[FS0]]]
[[V7]] = EmptyToUndefined ([[V6]])
@@ -256,10 +256,10 @@ fn if_else_merges_with_phis() {
&dump_of(&program),
"
[[B0]]:
- Jump [[B1]]
- [[B1]] (pc 0) <- [[B0]]:
[[V2]] = LoadSlot r5
[[V1]] = LoadSlot a0
+ Jump [[B1]]
+ [[B1]] (pc 0) <- [[B0]]:
BranchTruthy ([[V2]]) -> [[B2]], [[B3]], fallback [[B4]]
[[B2]] <- [[B1]]:
Jump [[B8]]
@@ -302,9 +302,9 @@ fn writes_to_constant_operands_go_to_the_frame() {
&dump_of(&program),
"
[[B0]]:
+ [[V1]] = LoadSlot a0
Jump [[B1]]
[[B1]] (pc 0) <- [[B0]]:
- [[V1]] = LoadSlot a0
StoreSlot c0 ([[V1]])
[[V3]] = LoadSlot c0
CallSlowPath ToString @3 ([[V3]]) [[[FS0]]]
@@ -447,9 +447,9 @@ fn nested_loops_get_phis_for_slots_slow_paths_assign() {
&dump_of(&program),
"
[[B0]]:
+ [[V7]] = LoadSlot a0
Jump [[B1]]
[[B1]] (pc 0) <- [[B0]]:
- [[V7]] = LoadSlot a0
Jump [[B2]]
[[B2]] (pc 4) <- [[B1]], [[B10]] loop:
[[V6]] = Phi (#0, [[V20]])
@@ -505,9 +505,9 @@ fn truthiness_values_and_tag_branches() {
&dump_of(&program),
"
[[B0]]:
+ [[V1]] = LoadSlot a0
Jump [[B1]]
[[B1]] (pc 0) <- [[B0]]:
- [[V1]] = LoadSlot a0
BranchTruthy ([[V1]]) -> [[B2]], [[B3]], fallback [[B4]]
[[B2]] <- [[B1]]:
Jump [[B7]]
@@ -701,9 +701,9 @@ fn instructions_that_never_ran_exit_with_the_live_values() {
&dump(&graph, &test_layout()),
"
[[B0]]:
+ [[V2]] = LoadSlot a0
Jump [[B1]]
[[B1]] (pc 0) <- [[B0]]:
- [[V2]] = LoadSlot a0
Exit NoFeedback [[[FS0]]]
frame states:
[[FS0]]: pc 6 at {r5=#10, r6=[[V2]]}
@@ -732,10 +732,10 @@ fn calls_write_back_their_arguments_and_values_survive_them() {
&dump_of(&program),
"
[[B0]]:
- Jump [[B1]]
- [[B1]] (pc 0) <- [[B0]]:
[[V2]] = LoadSlot r5
[[V1]] = LoadSlot a0
+ Jump [[B1]]
+ [[B1]] (pc 0) <- [[B0]]:
[[V3]] = CallSlowPath Call @3 ([[V2]], [[V2]], [[V1]]) [[[FS0]]]
Return ([[V1]])
frame states:
@@ -804,9 +804,9 @@ fn reserved_registers_hold_values_like_other_registers() {
&dump_of(&program),
"
[[B0]]:
+ [[A]] = LoadSlot a0
Jump [[B1]]
[[B1]] (pc 0) <- [[B0]]:
- [[A]] = LoadSlot a0
[[X]] = CallSlowPath Exp @0 ([[A]], [[A]]) [[[FS0]]]
[[Y]] = CallSlowPath Exp @6 ([[X]], [[A]]) [[[FS1]]]
[[RESULT]] = EmptyToUndefined ([[X]])
@@ -919,9 +919,9 @@ pub(crate) mod property_access {
&dump_with_caches(&program, caches, Vec::new()),
"
[[B0]]:
+ [[V1]] = LoadSlot a0
Jump [[B1]]
[[B1]] (pc 0) <- [[B0]]:
- [[V1]] = LoadSlot a0
CheckObject ([[V1]]) [[[FS0]]]
[[V3]]:pointer = CellAddress ([[V1]])
CheckShape [0x100] ([[V1]], [[V3]]) [[[FS0]]]
@@ -954,9 +954,9 @@ pub(crate) mod property_access {
&dump_with_caches(&program, caches, Vec::new()),
"
[[B0]]:
+ [[V1]] = LoadSlot a0
Jump [[B1]]
[[B1]] (pc 0) <- [[B0]]:
- [[V1]] = LoadSlot a0
CheckObject ([[V1]]) [[[FS0]]]
[[V3]]:pointer = CellAddress ([[V1]])
CheckShape [0x100, 0x200] ([[V1]], [[V3]]) [[[FS0]]]
@@ -1061,9 +1061,9 @@ pub(crate) mod property_access {
&dump_with_caches(&program, vec![cache(vec![entry])], Vec::new()),
"
[[B0]]:
+ [[V1]] = LoadSlot a0
Jump [[B1]]
[[B1]] (pc 0) <- [[B0]]:
- [[V1]] = LoadSlot a0
CheckObject ([[V1]]) [[[FS0]]]
[[V3]]:pointer = CellAddress ([[V1]])
CheckShape [0x100@3] ([[V1]], [[V3]]) [[[FS0]]]
@@ -1279,9 +1279,9 @@ pub(crate) mod property_access {
&dump_with_caches(&program, vec![cache(vec![entry])], Vec::new()),
"
[[B0]]:
+ [[V1]] = LoadSlot a0
Jump [[B1]]
[[B1]] (pc 0) <- [[B0]]:
- [[V1]] = LoadSlot a0
CheckObject ([[V1]]) [[[FS0]]]
[[V3]]:pointer = CellAddress ([[V1]])
CheckShape [0x100] ([[V1]], [[V3]]) [[[FS0]]]
@@ -1436,9 +1436,9 @@ pub(crate) mod property_access {
&dump_keyed(&program, vec![other_key, keyed(GetOwnProperty, 2)], Vec::new()),
"
[[B0]]:
+ [[V2]] = LoadSlot a0
Jump [[B1]]
[[B1]] (pc 0) <- [[B0]]:
- [[V2]] = LoadSlot a0
CheckObject ([[V2]]) [[[FS0]]]
[[V4]]:pointer = CellAddress ([[V2]])
CheckShape [0x100] ([[V2]], [[V4]]) [[[FS0]]]
@@ -1573,14 +1573,14 @@ pub(crate) mod property_access {
&dump_with_caches(&program, caches, Vec::new()),
"
[[B0]]:
+ [[V13]] = LoadSlot l1
+ [[V1]] = LoadSlot a0
Jump [[B1]]
[[B1]] (pc 0) <- [[B0]]:
- [[V1]] = LoadSlot a0
CheckObject ([[V1]]) [[[FS0]]]
[[V4]]:pointer = CellAddress ([[V1]])
CheckShape [0x100] ([[V1]], [[V4]]) [[[FS0]]]
StoreNamed +2 ([[V1]], #10, [[V4]]) [[[FS0]]]
- [[V13]] = LoadSlot l1
[[V14]] = CallSlowPath Exp @15 ([[V13]], #10) [[[FS1]]]
CheckShape [0x100] ([[V1]], [[V4]]) [[[FS2]]]
[[V19]] = LoadNamed +2 ([[V1]], [[V4]]) [[[FS2]]]
@@ -1857,9 +1857,9 @@ pub(crate) mod inlining {
&dump_inlined(&caller(), &callee, function(true, false)),
"
[[B0]]:
+ [[V4]] = LoadSlot a0
Jump [[B1]]
[[B1]] (pc 0) <- [[B0]]:
- [[V4]] = LoadSlot a0
CheckValue 0xfff9000000005000 ([[V4]]) [[[FS0]]]
Jump [[B2]]
[[B2]] (pc 0) <- [[B1]]:
@@ -1891,9 +1891,9 @@ pub(crate) mod inlining {
&dump_inlined(&caller(), &callee, function(true, false)),
"
[[B0]]:
+ [[V4]] = LoadSlot a0
Jump [[B1]]
[[B1]] (pc 0) <- [[B0]]:
- [[V4]] = LoadSlot a0
CheckValue 0xfff9000000005000 ([[V4]]) [[[FS0]]]
Jump [[B2]]
[[B2]] (pc 0) <- [[B1]]:
@@ -2012,9 +2012,9 @@ pub(crate) mod inlining {
&dump_inlined(&caller(), &program(r(7)), function(true, false)),
"
[[B0]]:
+ [[V4]] = LoadSlot a0
Jump [[B1]]
[[B1]] (pc 0) <- [[B0]]:
- [[V4]] = LoadSlot a0
CheckValue 0xfff9000000005000 ([[V4]]) [[[FS0]]]
Jump [[B2]]
[[B2]] (pc 0) <- [[B1]]:
@@ -2098,9 +2098,9 @@ pub(crate) mod inlining {
&dump(&optimized_graph(&snapshot), &test_layout()),
"
[[B0]]:
+ [[V5]] = LoadSlot a0
Jump [[B1]]
[[B1]] (pc 0) <- [[B0]]:
- [[V5]] = LoadSlot a0
CheckValue 0xfff9000000005000 ([[V5]]) [[[FS0]]]
Jump [[B2]]
[[B2]] (pc 0) <- [[B1]]:
diff --git a/Libraries/LibJS/JIT/Rust/src/regalloc/tests.rs b/Libraries/LibJS/JIT/Rust/src/regalloc/tests.rs
index c6531329b6c..b8bcb7d0390 100644
--- a/Libraries/LibJS/JIT/Rust/src/regalloc/tests.rs
+++ b/Libraries/LibJS/JIT/Rust/src/regalloc/tests.rs
@@ -67,9 +67,9 @@ fn straight_line_code() {
&dump_allocation(&graph, &allocation),
"
b0:
+ v6 LoadSlot -> r0
v7 Jump
b1:
- v6 LoadSlot -> r0
v14 InitializeFrame temps r1, r2
v11 CallSlowPath (#10, r0) -> r0
v12 EmptyToUndefined (r0) -> r1
@@ -107,10 +107,10 @@ fn values_live_across_calls_are_spilled_at_their_definition() {
&dump_allocation(&graph, &allocation),
"
b0:
- v8 Jump
- b1:
v6 LoadSlot -> r0
v7 LoadSlot -> r1 spill s0
+ v8 Jump
+ b1:
v9 CallSlowPath (r0, r0, r1) -> r0 exit {6=s0}
moves s0 -> r0
v10 Return (r0)
@@ -144,10 +144,10 @@ fn values_loaded_from_unwritten_slots_are_reloaded_from_the_frame() {
&dump_allocation(&graph, &allocation),
"
b0:
- v8 Jump
- b1:
v6 LoadSlot -> r0
v7 LoadSlot -> r1
+ v8 Jump
+ b1:
v9 CallSlowPath (r0, r0, r1) -> r0 exit {6=frame12}
moves frame12 -> r0
v10 Return (r0)
diff --git a/Tests/LibJS/JIT/ir/licm-loop-bound-check.js b/Tests/LibJS/JIT/ir/licm-loop-bound-check.js
index 384be04f836..d72d68e3b46 100644
--- a/Tests/LibJS/JIT/ir/licm-loop-bound-check.js
+++ b/Tests/LibJS/JIT/ir/licm-loop-bound-check.js
@@ -15,7 +15,7 @@ jit.compile(total);
// CHECK: CheckInt32
// CHECK-PASS: loop invariant code motion
// CHECK: [[COUNT:v[0-9]+]] = LoadSlot a1
-// CHECK-NEXT: [[BOUND:v[0-9]+]]:int32 = CheckInt32 ([[COUNT]])
+// CHECK: [[BOUND:v[0-9]+]]:int32 = CheckInt32 ([[COUNT]])
// CHECK-NOT: CheckInt32 ([[COUNT]])
// CHECK: loop:
// CHECK: Int32LessThan ({{v[0-9]+}}, [[BOUND]])
diff --git a/Tests/LibJS/JIT/ir/math-and-string-builtins.js b/Tests/LibJS/JIT/ir/math-and-string-builtins.js
index e5d2cb089ff..e00783e07f3 100644
--- a/Tests/LibJS/JIT/ir/math-and-string-builtins.js
+++ b/Tests/LibJS/JIT/ir/math-and-string-builtins.js
@@ -13,8 +13,8 @@ jit.compile(floorPlusAbs);
// CHECK-FUNCTION: floorPlusAbs
// CHECK-PASS: graph building
+// CHECK: [[X:v[0-9]+]] = LoadSlot a0
// CHECK: CheckValue {{0x[0-9a-f]+}} ({{v[0-9]+}})
-// CHECK-NEXT: [[X:v[0-9]+]] = LoadSlot a0
// CHECK-NEXT: BranchInt32Value ([[X]]) -> [[INT32:b[0-9]+]], [[OTHER:b[0-9]+]]
// CHECK: [[INT32]] <- {{b[0-9]+}}:
// CHECK-NEXT: [[INTEGER:v[0-9]+]] = RefineInt32Value ([[X]])
diff --git a/Tests/LibJS/JIT/ir/representation-phis.js b/Tests/LibJS/JIT/ir/representation-phis.js
index 8ec76b792bb..73803777456 100644
--- a/Tests/LibJS/JIT/ir/representation-phis.js
+++ b/Tests/LibJS/JIT/ir/representation-phis.js
@@ -39,7 +39,7 @@ jit.compile(pickAfterCall);
// CHECK-FUNCTION: pickAfterCall
// CHECK-PASS: representation selection
// CHECK: [[A:v[0-9]+]] = LoadSlot a1
-// CHECK-NEXT: [[CHECKED:v[0-9]+]]:int32 = CheckInt32 ([[A]])
+// CHECK: [[CHECKED:v[0-9]+]]:int32 = CheckInt32 ([[A]])
// CHECK-NEXT: Jump
// CHECK: [[X:v[0-9]+]]:int32 = Phi ([[CHECKED]], #i32:1)
// CHECK-NOT: CheckInt32 ([[X]])