Skip to content

Truly independent web browser

LibJS/JIT: Give the stages of a compile a zone for their own data

Andreas Kling7c350b2f8e9c
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/mod.rs b/Libraries/LibJS/JIT/Rust/src/builder/mod.rs
index 1cad96bff05..77aa3dccf30 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/mod.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/mod.rs
@@ -90,12 +90,14 @@ use crate::snapshot::Intrinsic;
use crate::snapshot::RuntimeInfo;
use crate::snapshot::Snapshot;
use ak::Zone;
+use ak::ZoneVec;
pub(crate) use slow_paths::is_ssa_slow_path_output;
pub(crate) use slow_paths::js_call_operands;
use std::cell::Cell;
use std::cell::OnceCell;
-/// Builds the IR for `snapshot.executables[0]`.
+/// Builds the IR for `snapshot.executables[0]`, with the graph and the work data of the build both in `zone`.
+#[cfg(test)]
pub fn build_graph<'z>(snapshot: &Snapshot, zone: &'z Zone) -> Result<Graph<'z>, CompileFailure> {
build_graph_with_feedback(
snapshot,
@@ -109,6 +111,21 @@ pub fn build_graph<'z>(snapshot: &Snapshot, zone: &'z Zone) -> Result<Graph<'z>,
)
}
+/// Like `build_graph()`, with the "did this instruction ever run" oracle given by the caller.
+#[cfg(test)]
+pub fn build_graph_with_feedback<'z>(
+ snapshot: &Snapshot,
+ has_run: &dyn Fn(u32) -> bool,
+ zone: &'z Zone,
+) -> Result<Graph<'z>, CompileFailure> {
+ let mut attempts = Attempts::new(snapshot, has_run, zone);
+ loop {
+ if let Some(graph) = attempts.build(zone)? {
+ return Ok(graph);
+ }
+ }
+}
+
/// Whether the instruction at `pc` ever ran in the interpreter, as far as the
/// feedback it records tells: an instruction that records feedback ran if any
/// of its slots recorded something. Instructions without feedback count as
@@ -140,51 +157,99 @@ pub fn has_run(snapshot: &Snapshot, executable: &ExecutableSnapshot, pc: u32) ->
recorded.iter().all(Option::is_none) || recorded.contains(&Some(true))
}
-/// Like `build_graph()`, with the "did this instruction ever run" oracle
-/// given by the caller.
-pub fn build_graph_with_feedback<'z>(
- snapshot: &Snapshot,
- has_run: &dyn Fn(u32) -> bool,
- zone: &'z Zone,
-) -> Result<Graph<'z>, CompileFailure> {
- let decoded = (0..snapshot.executables.len())
- .map(|_| OnceCell::new())
- .collect::<Vec<_>>();
- let virtualized_inlined_arguments = Cell::new(false);
- let build = |virtualize_arguments, virtualize_inlined_arguments, build_osr_entries| {
- GraphBuilder::new(
+/// The builds of the IR for `snapshot.executables[0]`: one, or more where a graph came out that cannot be used.
+///
+/// Where an inlined callee had a virtual arguments object, a graph that needs it, or a build that failed, is built
+/// again without those (see `arguments`), which then are not inlined. A graph that needs the arguments object of the
+/// compiled function itself is built again without its on-stack replacement entries, and then with a real arguments
+/// object.
+pub struct Attempts<'a> {
+ snapshot: &'a Snapshot,
+ /// Whether the instruction at a pc ever ran. The instructions that never ran become unconditional exits.
+ has_run: &'a dyn Fn(u32) -> bool,
+ /// Where the builds keep their own data. It holds the data of all the builds until its owner resets it.
+ work: &'a Zone,
+ decoded: Vec<OnceCell<Result<DecodedFunction, CompileFailure>>>,
+ virtualized_inlined_arguments: Cell<bool>,
+ next: Attempt,
+}
+
+/// What a build of `Attempts` does differently from the first one.
+#[derive(Clone, Copy)]
+enum Attempt {
+ First,
+ WithoutVirtualArgumentsOfInlinedCallees,
+ WithoutOsrEntries,
+ WithoutVirtualArguments,
+}
+
+impl<'a> Attempts<'a> {
+ pub fn new(snapshot: &'a Snapshot, has_run: &'a dyn Fn(u32) -> bool, work: &'a Zone) -> Self {
+ Self {
snapshot,
has_run,
- &decoded,
- ArgumentsVirtualization {
- compiled_function: virtualize_arguments,
- inlined_callees: virtualize_inlined_arguments,
- inlined_callees_virtualized: &virtualized_inlined_arguments,
- },
- build_osr_entries,
- zone,
- )?
- .build()
- };
- // NB: Where an inlined callee had a virtual arguments object, a graph
- // that needs it, or a build that failed, is built again without
- // those (see `arguments`), which then are not inlined.
- let graph = match build(true, true, true) {
- Ok(graph) if !arguments::uses_virtual_arguments(&graph) => return Ok(graph),
- _ if virtualized_inlined_arguments.get() => build(true, false, true)?,
- result => result?,
- };
- if !arguments::uses_virtual_arguments(&graph) {
- return Ok(graph);
- }
- if !graph.osr_entries.is_empty() {
- let graph = build(true, false, false)?;
- if !arguments::uses_virtual_arguments(&graph) {
- return Ok(graph);
+ work,
+ decoded: (0..snapshot.executables.len()).map(|_| OnceCell::new()).collect(),
+ virtualized_inlined_arguments: Cell::new(false),
+ next: Attempt::First,
}
}
- // Something needs the arguments object itself.
- build(false, false, true)
+
+ /// Builds a graph in `zone`. Returns `None` for a graph that cannot be used: the caller then drops what is in
+ /// `zone` and calls this again, which builds the graph in another way.
+ pub fn build<'z>(&mut self, zone: &'z Zone) -> Result<Option<Graph<'z>>, CompileFailure> {
+ let build = |virtualize_arguments, virtualize_inlined_arguments, build_osr_entries| {
+ GraphBuilder::new(
+ self.snapshot,
+ self.has_run,
+ &self.decoded,
+ ArgumentsVirtualization {
+ compiled_function: virtualize_arguments,
+ inlined_callees: virtualize_inlined_arguments,
+ inlined_callees_virtualized: &self.virtualized_inlined_arguments,
+ },
+ build_osr_entries,
+ zone,
+ self.work,
+ )?
+ .build()
+ };
+ let graph = match self.next {
+ Attempt::First => match build(true, true, true) {
+ Ok(graph) if !arguments::uses_virtual_arguments(&graph) => return Ok(Some(graph)),
+ _ if self.virtualized_inlined_arguments.get() => {
+ self.next = Attempt::WithoutVirtualArgumentsOfInlinedCallees;
+ return Ok(None);
+ }
+ result => result?,
+ },
+ Attempt::WithoutVirtualArgumentsOfInlinedCallees => {
+ let graph = build(true, false, true)?;
+ if !arguments::uses_virtual_arguments(&graph) {
+ return Ok(Some(graph));
+ }
+ graph
+ }
+ Attempt::WithoutOsrEntries => {
+ let graph = build(true, false, false)?;
+ if !arguments::uses_virtual_arguments(&graph) {
+ return Ok(Some(graph));
+ }
+ // Something needs the arguments object itself.
+ self.next = Attempt::WithoutVirtualArguments;
+ return Ok(None);
+ }
+ Attempt::WithoutVirtualArguments => return build(false, false, true).map(Some),
+ };
+ // NB: The graph uses the virtual arguments object of the compiled function.
+ self.next = if graph.osr_entries.is_empty() {
+ // Something needs the arguments object itself.
+ Attempt::WithoutVirtualArguments
+ } else {
+ Attempt::WithoutOsrEntries
+ };
+ Ok(None)
+ }
}
/// Where the graph builder may build virtual arguments objects (see
@@ -538,8 +603,10 @@ struct GraphBuilder<'a, 'z> {
decoded: &'a [OnceCell<Result<DecodedFunction, CompileFailure>>],
graph: Graph<'z>,
+ /// Where the builder keeps its own data. Nothing of the graph is there.
+ work: &'a Zone,
/// The blocks in linear order, as they are started.
- order: Vec<BlockId>,
+ order: ZoneVec<'a, BlockId>,
/// The constant node of each value and representation.
constants: HashMap<(u64, Repr), NodeId>,
@@ -577,6 +644,7 @@ impl<'a, 'z> GraphBuilder<'a, 'z> {
virtualize_arguments: ArgumentsVirtualization<'a>,
build_osr_entries: bool,
zone: &'z Zone,
+ work: &'a Zone,
) -> Result<Self, CompileFailure> {
let executable = snapshot.executables.first().ok_or(CompileFailure::InvalidBytecode {
pc: 0,
@@ -606,7 +674,8 @@ impl<'a, 'z> GraphBuilder<'a, 'z> {
legacy_arguments,
..Graph::new_in(zone)
},
- order: Vec::new(),
+ work,
+ order: ZoneVec::new_in(work),
constants: HashMap::default(),
block: BlockId(0),
// NB: Replaced by the frame of the first block built.
@@ -2119,7 +2188,7 @@ impl<'a, 'z> GraphBuilder<'a, 'z> {
/// on the control word of a conditional jump, and after instructions
/// that never continue with the next one.
fn end_generic_instruction(&mut self, instruction: &Instruction, node: NodeId) -> Result<Flow, CompileFailure> {
- let mut targets = Vec::new();
+ let mut targets = ZoneVec::new_in(self.work);
instruction.for_each_jump_target(|label| targets.push(label));
let is_conditional_jump = !targets.is_empty();
if is_conditional_jump {
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/tests.rs b/Libraries/LibJS/JIT/Rust/src/builder/tests.rs
index 18835a6eae7..3bebb1c3852 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/tests.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/tests.rs
@@ -49,7 +49,14 @@ pub(crate) fn build_with_layout(program: &Program, layout: FrameLayout, never_ra
.collect::<Vec<_>>();
let mut graph =
build_graph_with_feedback(&snapshot, &|pc| !never_ran_pcs.contains(&pc), crate::test_zone()).unwrap();
- crate::passes::optimize(&mut graph, &layout, true, &mut Dominators::new()).unwrap();
+ crate::passes::optimize(
+ &mut graph,
+ &layout,
+ true,
+ &mut Dominators::new(),
+ &mut crate::test_work(),
+ )
+ .unwrap();
graph
}
@@ -61,6 +68,7 @@ pub(crate) fn optimized_graph(snapshot: &Snapshot) -> Graph<'static> {
&snapshot.executables[0].layout,
true,
&mut Dominators::new(),
+ &mut crate::test_work(),
)
.unwrap();
graph
diff --git a/Libraries/LibJS/JIT/Rust/src/lib.rs b/Libraries/LibJS/JIT/Rust/src/lib.rs
index b3e1397442f..f1177fc23d3 100644
--- a/Libraries/LibJS/JIT/Rust/src/lib.rs
+++ b/Libraries/LibJS/JIT/Rust/src/lib.rs
@@ -77,6 +77,12 @@ pub(crate) fn test_zone() -> &'static ak::Zone {
ZONE.with(|zone| *zone)
}
+/// A zone for the work data of the passes that a test runs.
+#[cfg(test)]
+pub(crate) fn test_work() -> ak::Zone {
+ ak::Zone::new(1 << 30, c"test")
+}
+
/// Compiles `snapshot.executables[0]` for the host architecture with the memory of `scratch`, into `compiled` in
/// place of what it holds. After a failure, `compiled` holds no complete code.
pub fn compile(snapshot: &Snapshot, scratch: &mut Scratch, compiled: &mut CompiledCode) -> Result<(), CompileFailure> {
@@ -133,10 +139,20 @@ fn run_stages<M: asm::PortableMacroAssembler>(
if !snapshot.is_complete() {
return Err(CompileFailure::TooLarge);
}
- let Scratch { zone, tables } = scratch;
- // NB: Nothing of the compile before this one is in use (see `Scratch`), and its graph is gone.
- zone.reset();
- let mut graph = builder::build_graph_with_feedback(snapshot, has_run, zone)?;
+ let Scratch { zone, work, tables } = scratch;
+ // NB: Nothing of the compile before this one is in use (see `Scratch`).
+ work.reset();
+ let mut attempts = builder::Attempts::new(snapshot, has_run, work);
+ let mut graph = loop {
+ // NB: This drops the graph of the compile before this one, and a graph that the builder gave up.
+ zone.reset();
+ if let Some(graph) = attempts.build(zone)? {
+ break graph;
+ }
+ };
+ drop(attempts);
+ // NB: The builder is done, and nothing of the graph is in its work data.
+ work.reset();
let executable = &snapshot.executables[0];
let pass_dumps = if snapshot.options.dump_passes {
Some(passes::optimize_with_dumps(
@@ -144,6 +160,7 @@ fn run_stages<M: asm::PortableMacroAssembler>(
&executable.layout,
snapshot.options.verify_ir,
&mut tables.dominators,
+ work,
)?)
} else {
passes::optimize(
@@ -151,6 +168,7 @@ fn run_stages<M: asm::PortableMacroAssembler>(
&executable.layout,
snapshot.options.verify_ir,
&mut tables.dominators,
+ work,
)?;
None
};
diff --git a/Libraries/LibJS/JIT/Rust/src/passes/bounds_check_elimination.rs b/Libraries/LibJS/JIT/Rust/src/passes/bounds_check_elimination.rs
index 9cc797bbd82..1d30c0a2204 100644
--- a/Libraries/LibJS/JIT/Rust/src/passes/bounds_check_elimination.rs
+++ b/Libraries/LibJS/JIT/Rust/src/passes/bounds_check_elimination.rs
@@ -44,6 +44,7 @@
//! the operation: an index is below the elements' count there.
use ak::ColumnFull;
+use ak::Zone;
use super::dominators::Dominators;
use super::edit::Constants;
@@ -514,7 +515,7 @@ fn entering_counters(
entering
}
-pub fn run(graph: &mut Graph, dominators: &mut Dominators) -> Result<(), ColumnFull> {
+pub fn run(graph: &mut Graph, dominators: &mut Dominators, _work: &Zone) -> Result<(), ColumnFull> {
let mut phi_blocks = HashMap::default();
let mut checks = Vec::new();
let mut shifts = Vec::new();
diff --git a/Libraries/LibJS/JIT/Rust/src/passes/branch_fusion.rs b/Libraries/LibJS/JIT/Rust/src/passes/branch_fusion.rs
index 3e2bc9675f3..700d2ee313b 100644
--- a/Libraries/LibJS/JIT/Rust/src/passes/branch_fusion.rs
+++ b/Libraries/LibJS/JIT/Rust/src/passes/branch_fusion.rs
@@ -24,6 +24,7 @@ use crate::ir::Comparison;
use crate::ir::Graph;
use crate::ir::NodeId;
use crate::ir::Op;
+use ak::Zone;
/// The comparison of swapped operands that has the same result.
fn mirrored(comparison: Comparison) -> Comparison {
@@ -175,7 +176,7 @@ fn add_predecessor(graph: &mut Graph, target: BlockId, predecessor: BlockId, lik
}
}
-pub fn run(graph: &mut Graph) {
+pub fn run(graph: &mut Graph, _work: &Zone) {
// NB: Threading moves the uses of each phi's inputs to the branches.
let uses = count_uses(graph);
thread_boolean_phis(graph, &uses);
diff --git a/Libraries/LibJS/JIT/Rust/src/passes/branch_splitting.rs b/Libraries/LibJS/JIT/Rust/src/passes/branch_splitting.rs
index 24d55be3b2a..45bc1c94048 100644
--- a/Libraries/LibJS/JIT/Rust/src/passes/branch_splitting.rs
+++ b/Libraries/LibJS/JIT/Rust/src/passes/branch_splitting.rs
@@ -13,6 +13,7 @@
//! the key the cache holds, not a phi that also holds `undefined`.
use ak::ColumnFull;
+use ak::Zone;
use super::dominators::Dominators;
use crate::bitset::BitSet;
@@ -369,7 +370,7 @@ fn split_one(graph: &mut Graph, dominators: &mut Dominators) -> Result<bool, Col
Ok(false)
}
-pub fn run(graph: &mut Graph, dominators: &mut Dominators) -> Result<(), ColumnFull> {
+pub fn run(graph: &mut Graph, dominators: &mut Dominators, _work: &Zone) -> Result<(), ColumnFull> {
for _ in 0..graph.blocks.len() {
if !split_one(graph, dominators)? {
break;
diff --git a/Libraries/LibJS/JIT/Rust/src/passes/check_elimination.rs b/Libraries/LibJS/JIT/Rust/src/passes/check_elimination.rs
index d2cc1a63537..9e54446eb70 100644
--- a/Libraries/LibJS/JIT/Rust/src/passes/check_elimination.rs
+++ b/Libraries/LibJS/JIT/Rust/src/passes/check_elimination.rs
@@ -56,6 +56,7 @@ use crate::ir::Op;
use crate::ir::ShapeCheck;
use crate::ir::value;
use crate::snapshot::CellId;
+use ak::Zone;
/// What the analysis decided for the nodes of one visit of the graph.
struct Decisions {
@@ -96,7 +97,7 @@ impl Decisions {
}
}
-pub fn run(graph: &mut Graph) {
+pub fn run(graph: &mut Graph, _work: &Zone) {
let mut constants = Constants::new(graph);
let block_count = graph.blocks.len();
// The facts flowing along each edge, from the latest visit of its source.
diff --git a/Libraries/LibJS/JIT/Rust/src/passes/dce.rs b/Libraries/LibJS/JIT/Rust/src/passes/dce.rs
index 8c49883b086..7cda94360dc 100644
--- a/Libraries/LibJS/JIT/Rust/src/passes/dce.rs
+++ b/Libraries/LibJS/JIT/Rust/src/passes/dce.rs
@@ -14,6 +14,7 @@ use super::edit;
use crate::bitset::BitSet;
use crate::ir::Graph;
use crate::ir::Op;
+use ak::Zone;
/// Whether a node can be dropped when its value is unused.
fn is_removable(op: &Op) -> bool {
@@ -29,7 +30,7 @@ fn is_removable(op: &Op) -> bool {
&& !properties.runs_code
}
-pub fn run(graph: &mut Graph) {
+pub fn run(graph: &mut Graph, _work: &Zone) {
remove_dead_nodes(graph);
// NB: Branches whose edges did the same are left jumping where they go,
// which leaves what only they used dead.
diff --git a/Libraries/LibJS/JIT/Rust/src/passes/enumerated_keys.rs b/Libraries/LibJS/JIT/Rust/src/passes/enumerated_keys.rs
index f25e9ceab5d..5d71a9ebf9f 100644
--- a/Libraries/LibJS/JIT/Rust/src/passes/enumerated_keys.rs
+++ b/Libraries/LibJS/JIT/Rust/src/passes/enumerated_keys.rs
@@ -13,8 +13,9 @@
use crate::ir::Graph;
use crate::ir::Op;
+use ak::Zone;
-pub fn run(graph: &mut Graph) {
+pub fn run(graph: &mut Graph, _work: &Zone) {
for block in 0..graph.blocks.len() {
for index in 0..graph.blocks[block].body.len() {
let node = graph.blocks[block].body[index];
diff --git a/Libraries/LibJS/JIT/Rust/src/passes/escape_analysis.rs b/Libraries/LibJS/JIT/Rust/src/passes/escape_analysis.rs
index 1f703b75cbb..1a25e6be2f4 100644
--- a/Libraries/LibJS/JIT/Rust/src/passes/escape_analysis.rs
+++ b/Libraries/LibJS/JIT/Rust/src/passes/escape_analysis.rs
@@ -63,6 +63,7 @@ use crate::ir::Op;
use crate::ir::VirtualObjectKind;
use crate::ir::value;
use crate::snapshot::CellId;
+use ak::Zone;
/// The shape and property count each object has at some point, which
/// `AddNamed` nodes change.
@@ -396,7 +397,7 @@ fn frame_state_values(graph: &Graph, frame_state: FrameStateId) -> Vec<NodeId> {
/// The value of each property of each replaced object at one point.
type Properties = HashMap<(NodeId, u32), NodeId>;
-pub fn run(graph: &mut Graph) {
+pub fn run(graph: &mut Graph, _work: &Zone) {
let placed = placed_nodes(graph);
let mut escaping = Vec::new();
loop {
diff --git a/Libraries/LibJS/JIT/Rust/src/passes/exit_unboxing.rs b/Libraries/LibJS/JIT/Rust/src/passes/exit_unboxing.rs
index a6ba887e87e..1594e927cdc 100644
--- a/Libraries/LibJS/JIT/Rust/src/passes/exit_unboxing.rs
+++ b/Libraries/LibJS/JIT/Rust/src/passes/exit_unboxing.rs
@@ -16,12 +16,13 @@
use crate::bitset::BitSet;
use crate::ir::Graph;
use crate::ir::Op;
+use ak::Zone;
fn is_boxing(op: &Op) -> bool {
matches!(op, Op::BoxInt32 | Op::BoxFloat64 | Op::BoxBool)
}
-pub fn run(graph: &mut Graph) {
+pub fn run(graph: &mut Graph, _work: &Zone) {
// NB: Boxings that other nodes use stay in frame states, where they
// cost nothing more than the unboxed value would.
let mut used = BitSet::new(graph.nodes.len());
diff --git a/Libraries/LibJS/JIT/Rust/src/passes/gvn.rs b/Libraries/LibJS/JIT/Rust/src/passes/gvn.rs
index 050bdefd8b5..54b048fcb34 100644
--- a/Libraries/LibJS/JIT/Rust/src/passes/gvn.rs
+++ b/Libraries/LibJS/JIT/Rust/src/passes/gvn.rs
@@ -13,6 +13,7 @@
//! on the same values, so this one cannot fail.
use ak::ColumnFull;
+use ak::Zone;
use super::dominators::Dominators;
use super::edit;
@@ -40,7 +41,7 @@ fn is_numbered(op: &Op) -> bool {
&& !op.is_control()
}
-pub fn run(graph: &mut Graph, dominators: &mut Dominators) -> Result<(), ColumnFull> {
+pub fn run(graph: &mut Graph, dominators: &mut Dominators, _work: &Zone) -> Result<(), ColumnFull> {
run_with_hash(graph, dominators, |node, replacements| {
let mut hash = FastHasher::default();
discriminant(&node.op).hash(&mut hash);
diff --git a/Libraries/LibJS/JIT/Rust/src/passes/interrupt_check_elimination.rs b/Libraries/LibJS/JIT/Rust/src/passes/interrupt_check_elimination.rs
index 36995dc5b94..7f091a0489c 100644
--- a/Libraries/LibJS/JIT/Rust/src/passes/interrupt_check_elimination.rs
+++ b/Libraries/LibJS/JIT/Rust/src/passes/interrupt_check_elimination.rs
@@ -26,6 +26,7 @@
//! nothing else uses, without which the loop would not count anything.
use ak::ColumnFull;
+use ak::Zone;
use super::dominators::Dominators;
use crate::bitset::BitSet;
@@ -229,7 +230,7 @@ fn has_loop_around(graph: &Graph, loop_: &Loop) -> bool {
false
}
-pub fn run(graph: &mut Graph, dominators: &mut Dominators) -> Result<(), ColumnFull> {
+pub fn run(graph: &mut Graph, dominators: &mut Dominators, _work: &Zone) -> Result<(), ColumnFull> {
if !graph.nodes.iter().any(|node| matches!(node.op, Op::CheckInterrupt)) {
return Ok(());
}
diff --git a/Libraries/LibJS/JIT/Rust/src/passes/key_strings.rs b/Libraries/LibJS/JIT/Rust/src/passes/key_strings.rs
index a798cad9335..8352e916108 100644
--- a/Libraries/LibJS/JIT/Rust/src/passes/key_strings.rs
+++ b/Libraries/LibJS/JIT/Rust/src/passes/key_strings.rs
@@ -32,8 +32,9 @@ use crate::bitset::BitSet;
use crate::ir::Graph;
use crate::ir::NodeId;
use crate::ir::Op;
+use ak::Zone;
-pub fn run(graph: &mut Graph) {
+pub fn run(graph: &mut Graph, _work: &Zone) {
let mut pending = Vec::new();
for block in &graph.blocks {
for node in block.nodes() {
diff --git a/Libraries/LibJS/JIT/Rust/src/passes/licm.rs b/Libraries/LibJS/JIT/Rust/src/passes/licm.rs
index f2fdacfb4c3..0c0ca32a8a2 100644
--- a/Libraries/LibJS/JIT/Rust/src/passes/licm.rs
+++ b/Libraries/LibJS/JIT/Rust/src/passes/licm.rs
@@ -26,6 +26,7 @@
//! other checks moving.
use ak::ColumnFull;
+use ak::Zone;
use super::dominators::Dominators;
use super::edit;
@@ -144,7 +145,7 @@ fn find_loops(graph: &Graph, dominators: &Dominators) -> Vec<Loop> {
loops
}
-pub fn run(graph: &mut Graph, dominators: &mut Dominators) -> Result<(), ColumnFull> {
+pub fn run(graph: &mut Graph, dominators: &mut Dominators, _work: &Zone) -> Result<(), ColumnFull> {
// NB: Loops have a back edge from a later block or the block itself.
let has_loops = graph.blocks.iter().enumerate().any(|(index, block)| {
block
diff --git a/Libraries/LibJS/JIT/Rust/src/passes/loop_rotation.rs b/Libraries/LibJS/JIT/Rust/src/passes/loop_rotation.rs
index 4c3570fa3c1..f7f59edf290 100644
--- a/Libraries/LibJS/JIT/Rust/src/passes/loop_rotation.rs
+++ b/Libraries/LibJS/JIT/Rust/src/passes/loop_rotation.rs
@@ -19,6 +19,7 @@
//! edges bring its values to those phis.
use ak::ColumnFull;
+use ak::Zone;
use ak::ZoneVec;
use super::dominators::Dominators;
@@ -34,7 +35,7 @@ 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, dominators: &mut Dominators) -> Result<(), ColumnFull> {
+pub fn run(graph: &mut Graph, dominators: &mut Dominators, _work: &Zone) -> Result<(), ColumnFull> {
if graph.osr_entries.is_empty() {
return Ok(());
}
diff --git a/Libraries/LibJS/JIT/Rust/src/passes/mod.rs b/Libraries/LibJS/JIT/Rust/src/passes/mod.rs
index e267fc7512d..54afe1bdf3a 100644
--- a/Libraries/LibJS/JIT/Rust/src/passes/mod.rs
+++ b/Libraries/LibJS/JIT/Rust/src/passes/mod.rs
@@ -39,6 +39,7 @@ pub mod verify;
mod tests;
use ak::ColumnFull;
+use ak::Zone;
use crate::bytecode::FrameLayout;
use crate::ir::Graph;
@@ -53,10 +54,23 @@ struct Pass {
/// How a pass is run.
#[derive(Clone, Copy)]
enum Run {
- Plain(fn(&mut Graph)),
+ Plain(fn(&mut Graph, &Zone)),
/// The pass computes the dominators of the graph into the tables it gets. It fails if the graph is too large
/// for them.
- WithDominators(fn(&mut Graph, &mut Dominators) -> Result<(), ColumnFull>),
+ WithDominators(fn(&mut Graph, &mut Dominators, &Zone) -> Result<(), ColumnFull>),
+}
+
+impl Run {
+ /// Runs the pass. It keeps its own data in `work`, which holds none of it when this returns.
+ fn run(self, graph: &mut Graph, dominators: &mut Dominators, work: &mut Zone) -> Result<(), ColumnFull> {
+ work.scope(|work| match self {
+ Run::Plain(run) => {
+ run(graph, work);
+ Ok(())
+ }
+ Run::WithDominators(run) => run(graph, dominators, work),
+ })
+ }
}
/// The passes, in the order they run.
@@ -67,7 +81,7 @@ const PASSES: &[Pass] = &[
},
Pass {
name: "trivial phi removal",
- run: Run::Plain(edit::remove_trivial_phis),
+ run: Run::Plain(|graph, _| edit::remove_trivial_phis(graph)),
},
Pass {
name: "truncation",
@@ -155,8 +169,9 @@ pub fn optimize(
layout: &FrameLayout,
verify: bool,
dominators: &mut Dominators,
+ work: &mut Zone,
) -> Result<(), ColumnFull> {
- run_passes(graph, layout, verify, dominators, &mut |_, _| {})
+ run_passes(graph, layout, verify, dominators, work, &mut |_, _| {})
}
/// `optimize()`, returning the graph as text after building it and after
@@ -166,9 +181,10 @@ pub fn optimize_with_dumps(
layout: &FrameLayout,
verify: bool,
dominators: &mut Dominators,
+ work: &mut Zone,
) -> Result<String, ColumnFull> {
let mut text = String::new();
- run_passes(graph, layout, verify, dominators, &mut |graph, after| {
+ run_passes(graph, layout, verify, dominators, work, &mut |graph, after| {
text.push_str(&format!("=== after {after}\n"));
text.push_str(&crate::ir::dump(graph, layout));
})?;
@@ -180,6 +196,7 @@ fn run_passes(
layout: &FrameLayout,
verify: bool,
dominators: &mut Dominators,
+ work: &mut Zone,
observe: &mut dyn FnMut(&Graph, &str),
) -> Result<(), ColumnFull> {
let verify = verify || cfg!(debug_assertions) || cfg!(test);
@@ -190,19 +207,16 @@ fn run_passes(
check(graph, layout, verify, dominators, "graph building");
observe(graph, "graph building");
for pass in PASSES {
- match pass.run {
- Run::Plain(run) => run(graph),
- Run::WithDominators(run) => run(graph, dominators)?,
- }
+ pass.run.run(graph, dominators, work)?;
check(graph, layout, verify, dominators, pass.name);
observe(graph, pass.name);
}
// NB: Nodes that took different refinements of one value compute the
// same once the refinements are gone, and nothing moves anymore.
edit::remove_refinements(graph);
- gvn::run(graph, dominators)?;
+ Run::WithDominators(gvn::run).run(graph, dominators, work)?;
// NB: Checks that merged took their frame states along, which may have been the last uses of what those refer to.
- dce::run(graph);
+ Run::Plain(dce::run).run(graph, dominators, work)?;
check(graph, layout, verify, dominators, "refinement removal");
observe(graph, "refinement removal");
frame_initialization::run(graph, layout);
diff --git a/Libraries/LibJS/JIT/Rust/src/passes/representation.rs b/Libraries/LibJS/JIT/Rust/src/passes/representation.rs
index 8e9b21d01cd..e3ecd8e6094 100644
--- a/Libraries/LibJS/JIT/Rust/src/passes/representation.rs
+++ b/Libraries/LibJS/JIT/Rust/src/passes/representation.rs
@@ -32,6 +32,7 @@ use crate::ir::Node;
use crate::ir::NodeId;
use crate::ir::Op;
use crate::ir::value;
+use ak::Zone;
/// An unboxed representation phis can take.
struct Unboxed {
@@ -85,7 +86,7 @@ const UNBOXED: [Unboxed; 3] = [
},
];
-pub fn run(graph: &mut Graph) {
+pub fn run(graph: &mut Graph, _work: &Zone) {
for unboxed in &UNBOXED {
unbox_phis(graph, unboxed);
}
diff --git a/Libraries/LibJS/JIT/Rust/src/passes/simplify.rs b/Libraries/LibJS/JIT/Rust/src/passes/simplify.rs
index e6e8f02af81..b267953df53 100644
--- a/Libraries/LibJS/JIT/Rust/src/passes/simplify.rs
+++ b/Libraries/LibJS/JIT/Rust/src/passes/simplify.rs
@@ -29,6 +29,7 @@ use crate::ir::Graph;
use crate::ir::NodeId;
use crate::ir::Op;
use crate::ir::value;
+use ak::Zone;
/// The truthiness of a constant, where it does not depend on the heap.
fn constant_truthiness(bits: u64, repr: Repr) -> Option<bool> {
@@ -318,14 +319,14 @@ fn constant_number(graph: &Graph, node: NodeId) -> Option<f64> {
}
}
-pub fn run(graph: &mut Graph) {
+pub fn run(graph: &mut Graph, _work: &Zone) {
simplify(graph, true);
}
/// Simplification after check elimination, which keeps every int32 check of
/// a double: check elimination may have relied on what one established, like
/// the int32 that an unboxing in a branch it decided takes.
-pub fn run_after_check_elimination(graph: &mut Graph) {
+pub fn run_after_check_elimination(graph: &mut Graph, _work: &Zone) {
simplify(graph, false);
}
diff --git a/Libraries/LibJS/JIT/Rust/src/passes/tests.rs b/Libraries/LibJS/JIT/Rust/src/passes/tests.rs
index 35e01a0b89c..afa33397737 100644
--- a/Libraries/LibJS/JIT/Rust/src/passes/tests.rs
+++ b/Libraries/LibJS/JIT/Rust/src/passes/tests.rs
@@ -224,7 +224,7 @@ fn simplification_removes_number_checks_of_numeric_constants() {
graph.blocks[0].control = Some(ret);
verify(&graph).unwrap();
- super::simplify::run(&mut graph);
+ super::simplify::run(&mut graph, crate::test_zone());
verify(&graph).unwrap();
assert_eq!(!graph.blocks[0].body.contains(&check), expected.is_some());
let input = graph.node(boxed).inputs[0];
@@ -294,7 +294,7 @@ fn simplification_removes_int32_checks_that_number_checks_convert() {
graph.blocks[2].control = Some(other_ret);
verify(&graph).unwrap();
- super::simplify::run(&mut graph);
+ super::simplify::run(&mut graph, crate::test_zone());
verify(&graph).unwrap();
assert!(!graph.blocks[0].body.contains(&check));
assert_eq!(graph.node(result).inputs, vec![double]);
@@ -346,7 +346,7 @@ fn element_counts_move_out_of_loops_that_only_store_elements() {
graph.blocks[2].control = Some(ret);
verify(&graph).unwrap();
- super::licm::run(&mut graph, &mut Dominators::new()).unwrap();
+ super::licm::run(&mut graph, &mut Dominators::new(), crate::test_zone()).unwrap();
assert!(graph.blocks[0].body.contains(&length), "{:?}", graph.blocks);
assert_eq!(graph.blocks[1].body, vec![store]);
}
@@ -405,7 +405,7 @@ fn loads_of_slots_that_nothing_writes_move_out_of_loops_that_call() {
graph.blocks[2].control = Some(ret);
verify(&graph).unwrap();
- super::licm::run(&mut graph, &mut Dominators::new()).unwrap();
+ super::licm::run(&mut graph, &mut Dominators::new(), crate::test_zone()).unwrap();
assert_eq!(graph.blocks[0].body, vec![unwritten], "{:?}", graph.blocks);
assert_eq!(graph.blocks[1].body, vec![written, call]);
verify(&graph).unwrap();
@@ -490,7 +490,7 @@ fn typed_array_storage_offsets_are_reused_across_stores_but_not_calls() {
let ret = node(&mut graph, Op::Return, vec![object], None);
graph.blocks[0].control = Some(ret);
verify(&graph).unwrap();
- super::check_elimination::run(&mut graph);
+ super::check_elimination::run(&mut graph, crate::test_zone());
assert!(!graph.blocks[0].body.contains(&second));
assert_eq!(graph.node(second_data).inputs[0], first);
assert!(graph.blocks[0].body.contains(&third));
@@ -545,7 +545,7 @@ fn typed_array_storage_metadata_moves_out_of_loops_that_only_store_elements() {
let ret = node(&mut graph, Op::Return, vec![object], None);
graph.blocks[2].control = Some(ret);
verify(&graph).unwrap();
- super::licm::run(&mut graph, &mut Dominators::new()).unwrap();
+ super::licm::run(&mut graph, &mut Dominators::new(), crate::test_zone()).unwrap();
for metadata in [length, offset, data] {
assert!(graph.blocks[0].body.contains(&metadata));
}
@@ -594,7 +594,7 @@ fn branches_on_known_kinds_fold() {
}
verify(&graph).unwrap();
- super::check_elimination::run(&mut graph);
+ super::check_elimination::run(&mut graph, crate::test_zone());
let branches = graph
.blocks
.iter()
@@ -653,7 +653,7 @@ fn branches_on_whether_values_of_known_kinds_are_empty_fold() {
}
verify(&graph).unwrap();
- super::check_elimination::run(&mut graph);
+ super::check_elimination::run(&mut graph, crate::test_zone());
let control = graph.blocks[0].control.unwrap();
assert!(
matches!(graph.node(control).op, Op::Jump { target: BlockId(1) }),
@@ -715,7 +715,7 @@ fn check_elimination_keeps_checks_that_a_later_entry_reaches_blocks_before_it()
graph.blocks[4].control = Some(ret);
verify(&graph).unwrap();
- super::check_elimination::run(&mut graph);
+ super::check_elimination::run(&mut graph, crate::test_zone());
verify(&graph).unwrap();
assert_eq!(graph.blocks[4].body, vec![second]);
}
@@ -734,7 +734,7 @@ fn simplification_removes_integer_double_round_trips() {
graph.blocks[0].control = Some(ret);
verify(&graph).unwrap();
- super::simplify::run(&mut graph);
+ super::simplify::run(&mut graph, crate::test_zone());
verify(&graph).unwrap();
assert!(!graph.blocks[0].body.contains(&integer));
assert_eq!(
@@ -778,7 +778,7 @@ fn simplification_tests_the_truthiness_of_boxed_int32_values_unboxed() {
}
verify(&graph).unwrap();
- super::simplify::run(&mut graph);
+ super::simplify::run(&mut graph, crate::test_zone());
verify(&graph).unwrap();
let branch = graph.blocks[0].control.unwrap();
assert_eq!(
@@ -807,7 +807,7 @@ fn simplification_keeps_double_integer_round_trips() {
graph.blocks[0].control = Some(ret);
verify(&graph).unwrap();
- super::simplify::run(&mut graph);
+ super::simplify::run(&mut graph, crate::test_zone());
verify(&graph).unwrap();
assert_eq!(graph.node(converted).inputs, vec![integer]);
assert!(graph.blocks[0].body.contains(&integer));
@@ -841,7 +841,7 @@ fn simplification_converts_constant_doubles_to_integers() {
graph.blocks[0].control = Some(ret);
verify(&graph).unwrap();
- super::simplify::run(&mut graph);
+ super::simplify::run(&mut graph, crate::test_zone());
verify(&graph).unwrap();
assert_eq!(
graph.constant_value(graph.node(ret).inputs[0]),
@@ -891,7 +891,7 @@ fn constant_bounds_checks_only_fold_when_the_unsigned_index_is_in_bounds() {
graph.blocks[0].body.extend([check, boxed]);
graph.blocks[0].control = Some(ret);
verify(&graph).unwrap();
- super::simplify::run(&mut graph);
+ super::simplify::run(&mut graph, crate::test_zone());
assert_eq!(graph.blocks[0].body.contains(&check), !folds);
verify(&graph).unwrap();
}
@@ -1007,7 +1007,7 @@ fn hoisted_checks_gate_exits_by_the_loop_headers_executable() {
graph.blocks[2].control = Some(ret);
graph.exit_sites.push((failed_executable, 77, ExitKind::NotObject));
verify(&graph).unwrap();
- super::licm::run(&mut graph, &mut Dominators::new()).unwrap();
+ super::licm::run(&mut graph, &mut Dominators::new(), crate::test_zone()).unwrap();
verify(&graph).unwrap();
assert_eq!(graph.blocks[0].body.contains(&check), failed_executable == 0);
let recovery = graph.frame_state(graph.node(check).frame_state.unwrap());
@@ -1168,7 +1168,7 @@ fn overflow_checks_removed_by_bounds_checks_check_the_counters_of_their_counts()
graph.blocks[5].control = Some(ret);
verify(&graph).unwrap();
- super::bounds_check_elimination::run(&mut graph, &mut Dominators::new()).unwrap();
+ super::bounds_check_elimination::run(&mut graph, &mut Dominators::new(), crate::test_zone()).unwrap();
verify(&graph).unwrap();
let guarded = graph.blocks[1].body.iter().any(|node| {
diff --git a/Libraries/LibJS/JIT/Rust/src/passes/truncation.rs b/Libraries/LibJS/JIT/Rust/src/passes/truncation.rs
index 1df75cfcd5e..3aeae2c7423 100644
--- a/Libraries/LibJS/JIT/Rust/src/passes/truncation.rs
+++ b/Libraries/LibJS/JIT/Rust/src/passes/truncation.rs
@@ -39,6 +39,7 @@ use crate::ir::Graph;
use crate::ir::NodeId;
use crate::ir::Op;
use crate::ir::TypedArrayElement;
+use ak::Zone;
/// The operations that wrap where their users only read the low 32 bits.
fn can_wrap(op: &Op) -> bool {
@@ -167,7 +168,7 @@ fn compare_uint32_bits(graph: &mut Graph) {
}
}
-pub fn run(graph: &mut Graph) {
+pub fn run(graph: &mut Graph, _work: &Zone) {
compare_uint32_bits(graph);
let count = graph.nodes.len();
let mut wrapping = BitSet::new(count);
diff --git a/Libraries/LibJS/JIT/Rust/src/regalloc/tests.rs b/Libraries/LibJS/JIT/Rust/src/regalloc/tests.rs
index e75454de480..985b9796b77 100644
--- a/Libraries/LibJS/JIT/Rust/src/regalloc/tests.rs
+++ b/Libraries/LibJS/JIT/Rust/src/regalloc/tests.rs
@@ -1244,6 +1244,7 @@ fn float64_values_get_floating_point_registers() {
&snapshot.executables[0].layout,
true,
&mut Dominators::new(),
+ &mut crate::test_work(),
)
.unwrap();
let allocation = allocate_and_verify(&graph);
diff --git a/Libraries/LibJS/JIT/Rust/src/scratch.rs b/Libraries/LibJS/JIT/Rust/src/scratch.rs
index 013b5db2f96..ce1c9f68b21 100644
--- a/Libraries/LibJS/JIT/Rust/src/scratch.rs
+++ b/Libraries/LibJS/JIT/Rust/src/scratch.rs
@@ -21,6 +21,9 @@ pub struct Scratch {
/// Where the graph of a compile is, with all the lists of its nodes, blocks and frame states. A compile starts
/// with a zone that holds nothing.
pub(crate) zone: Zone,
+ /// Where the stages of a compile keep the data that only they use. The stage that is done leaves nothing in it
+ /// for the next one: the driver drops the data of the builder, and of each pass, when it ends.
+ pub(crate) work: Zone,
pub(crate) tables: Tables,
}
@@ -40,15 +43,16 @@ impl Default for Scratch {
}
}
-/// The most bytes that the zone of a graph gives out. It is address space, which costs nothing before it is used, and
-/// it is far above what a graph of the largest function that the builder accepts takes.
-const GRAPH_ZONE_LIMIT: usize = 1 << 34;
+/// The most bytes that a zone of a compile gives out. It is address space, which costs nothing before it is used, and
+/// it is far above what the largest function that the builder accepts takes.
+const ZONE_LIMIT: usize = 1 << 34;
impl Scratch {
/// A scratch, which takes no memory before a compile uses it.
pub fn new() -> Self {
Self {
- zone: Zone::new(GRAPH_ZONE_LIMIT, c"JIT graph"),
+ zone: Zone::new(ZONE_LIMIT, c"JIT graph"),
+ work: Zone::new(ZONE_LIMIT, c"JIT work"),
tables: Tables {
artifact: ArtifactWriter::new(),
sites: SiteScratch::new(),
@@ -58,17 +62,21 @@ impl Scratch {
}
}
- /// Drops the data of the compile that ended. The zone keeps its memory, so that the next compile of the thread
+ /// Drops the data of the compile that ended. The zones keep their memory, so that the next compile of the thread
/// does not take it from the system again: a thread that has no compile for a while gives it back (see
/// `release_memory()`).
pub(crate) fn end_compile(&mut self) {
- self.zone.reset();
+ for zone in [&mut self.zone, &mut self.work] {
+ zone.reset();
+ }
}
/// Gives the memory of the scratch back to the system. The next compile takes memory again.
pub fn release_memory(&mut self) {
- self.zone.reset();
- self.zone.trim(0);
+ for zone in [&mut self.zone, &mut self.work] {
+ zone.reset();
+ zone.trim(0);
+ }
self.tables.artifact.release_memory();
self.tables.sites.release_memory();
self.tables.regalloc.release_memory();