Skip to content

Truly independent web browser

LibJS: Store JIT block liveness in sparse bitsets

Timothy Flynn719cf7ca3f88
diff --git a/Libraries/LibJS/JIT/Rust/src/lib.rs b/Libraries/LibJS/JIT/Rust/src/lib.rs
index baa2a733562..f034c2247a1 100644
--- a/Libraries/LibJS/JIT/Rust/src/lib.rs
+++ b/Libraries/LibJS/JIT/Rust/src/lib.rs
@@ -25,6 +25,7 @@ pub mod options;
pub mod passes;
pub mod regalloc;
pub mod snapshot;
+pub mod sparse_bitset;
use bytecode::OpCode;
use code::CompiledCode;
diff --git a/Libraries/LibJS/JIT/Rust/src/regalloc/liveness.rs b/Libraries/LibJS/JIT/Rust/src/regalloc/liveness.rs
index 86d7d299aef..ed77f754ec1 100644
--- a/Libraries/LibJS/JIT/Rust/src/regalloc/liveness.rs
+++ b/Libraries/LibJS/JIT/Rust/src/regalloc/liveness.rs
@@ -15,11 +15,12 @@ use crate::ir::BlockId;
use crate::ir::Graph;
use crate::ir::NodeId;
use crate::ir::Op;
+use crate::sparse_bitset::SparseBitSet;
use std::cell::OnceCell;
pub(super) struct ValueLiveness {
- pub live_in: Vec<BitSet>,
- pub live_out: Vec<BitSet>,
+ pub live_in: Vec<SparseBitSet>,
+ pub live_out: Vec<SparseBitSet>,
/// For each node, the values whose last use (on this path) it is.
pub dies_at: Vec<Vec<NodeId>>,
/// Values nobody uses.
@@ -138,10 +139,10 @@ impl ValueLiveness {
let mut defined = Vec::with_capacity(block_count);
let mut used = Vec::new();
for block in &block_ids {
- let mut live = BitSet::new(value_count);
+ let mut live = SparseBitSet::new(value_count);
Self::transfer_block(graph, &frames, *block, &mut live, &mut used, |_, _, _| {});
used_before_defined.push(live);
- let mut nodes = BitSet::new(value_count);
+ let mut nodes = SparseBitSet::new(value_count);
for node in graph.block_nodes(*block) {
nodes.insert(node.index());
}
@@ -162,9 +163,9 @@ impl ValueLiveness {
})
.collect::<Vec<_>>();
- let mut live_in = vec![BitSet::new(value_count); block_count];
- let mut live_out = vec![BitSet::new(value_count); block_count];
- let mut entering = BitSet::new(value_count);
+ let mut live_in = vec![SparseBitSet::new(value_count); block_count];
+ let mut live_out = vec![SparseBitSet::new(value_count); block_count];
+ let mut entering = SparseBitSet::new(value_count);
// Visit backwards initially, then only revisit predecessors whose
// successor's live-in set changed. Acyclic regions need one visit.
let mut worklist = block_ids.clone();
@@ -196,12 +197,12 @@ impl ValueLiveness {
let mut dead_outputs = BitSet::new(value_count);
let mut needs_spill = BitSet::new(value_count);
for block in &block_ids {
- let mut live = live_out[block.index()].clone();
+ entering.clone_from(&live_out[block.index()]);
Self::transfer_block(
graph,
&frames,
*block,
- &mut live,
+ &mut entering,
&mut used,
|node, live_after, used| {
if is_allocated_value(graph, node) && !live_after.contains(node.index()) {
@@ -243,7 +244,9 @@ impl ValueLiveness {
if (predecessors.len() > 1 || predecessors.first().is_some_and(|predecessor| predecessor >= block))
&& state_predecessor(graph, *block).is_none()
{
- needs_spill.union_with(&live_in[block.index()]);
+ for value in live_in[block.index()].iter() {
+ needs_spill.insert(value);
+ }
}
}
@@ -265,9 +268,9 @@ impl ValueLiveness {
graph: &Graph,
frames: &[OnceCell<Vec<NodeId>>],
block: BlockId,
- live: &mut BitSet,
+ live: &mut SparseBitSet,
used: &mut Vec<NodeId>,
- mut visit: impl FnMut(NodeId, &BitSet, &[NodeId]),
+ mut visit: impl FnMut(NodeId, &SparseBitSet, &[NodeId]),
) {
let block_data = graph.block(block);
for node in block_data.body.iter().chain(&block_data.control).rev() {
@@ -331,4 +334,59 @@ mod tests {
assert!(!liveness.dead_outputs.contains(value.index()));
assert!(liveness.live_in[0].is_empty());
}
+
+ #[test]
+ fn many_blocks_with_short_live_ranges() {
+ let mut graph = Graph::default();
+ let block_count = 8192;
+ let mut values = Vec::new();
+ for index in 0..block_count {
+ let mut block = Block::default();
+ let value = if index == 0 {
+ graph.add_node(Node::new(Op::LoadSlot { slot: 0 }, vec![], Some(Repr::Tagged), 0))
+ } else {
+ block.predecessors.push(BlockId::from_index(index - 1));
+ graph.add_node(Node::new(
+ Op::CheckObject,
+ vec![values[index - 1]],
+ Some(Repr::Tagged),
+ 0,
+ ))
+ };
+ values.push(value);
+ block.body.push(value);
+ block.control = Some(if index + 1 == block_count {
+ graph.add_node(Node::new(Op::Return, vec![value], None, 0))
+ } else {
+ graph.add_node(Node::new(
+ Op::Jump {
+ target: BlockId::from_index(index + 1),
+ },
+ vec![],
+ None,
+ 0,
+ ))
+ });
+ graph.add_block(block);
+ }
+ let liveness = ValueLiveness::compute(&graph);
+ assert!(liveness.live_in[0].is_empty());
+ assert!(liveness.live_out[block_count - 1].is_empty());
+ for index in 1..block_count {
+ assert_eq!(
+ liveness.live_in[index].iter().collect::<Vec<_>>(),
+ [values[index - 1].index()]
+ );
+ assert_eq!(
+ liveness.live_out[index - 1].iter().collect::<Vec<_>>(),
+ [values[index - 1].index()]
+ );
+ assert_eq!(liveness.dies_at[values[index].index()], [values[index - 1]]);
+ assert!(!liveness.dead_outputs.contains(values[index - 1].index()));
+ }
+ assert_eq!(
+ liveness.dies_at[graph.blocks[block_count - 1].control.unwrap().index()],
+ [values[block_count - 1]]
+ );
+ }
}
diff --git a/Libraries/LibJS/JIT/Rust/src/regalloc/mod.rs b/Libraries/LibJS/JIT/Rust/src/regalloc/mod.rs
index 929aa2c9598..a0577d3d62f 100644
--- a/Libraries/LibJS/JIT/Rust/src/regalloc/mod.rs
+++ b/Libraries/LibJS/JIT/Rust/src/regalloc/mod.rs
@@ -270,7 +270,7 @@ pub struct Allocation {
pub block_end_moves: Vec<Vec<Move>>,
pub spill_slot_count: u32,
/// The values live into each block, indexed by `BlockId`.
- pub live_in: Vec<crate::bitset::BitSet>,
+ pub live_in: Vec<crate::sparse_bitset::SparseBitSet>,
}
impl Allocation {
diff --git a/Libraries/LibJS/JIT/Rust/src/sparse_bitset.rs b/Libraries/LibJS/JIT/Rust/src/sparse_bitset.rs
new file mode 100644
index 00000000000..70b16aaf469
--- /dev/null
+++ b/Libraries/LibJS/JIT/Rust/src/sparse_bitset.rs
@@ -0,0 +1,247 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! A fixed-size set of integers that stores only its nonempty words. Block liveness sets cover the whole graph's
+//! node space but usually contain few values, so allocating that space for every block would be quadratic.
+
+#[derive(Clone, Copy, Default, PartialEq, Eq)]
+struct Word {
+ index: usize,
+ bits: u64,
+}
+
+#[derive(Default, PartialEq, Eq)]
+pub struct SparseBitSet {
+ // Sorted by index, with no empty words.
+ words: Vec<Word>,
+ len: usize,
+}
+
+impl Clone for SparseBitSet {
+ fn clone(&self) -> Self {
+ Self {
+ words: self.words.clone(),
+ len: self.len,
+ }
+ }
+
+ fn clone_from(&mut self, other: &Self) {
+ self.words.clone_from(&other.words);
+ self.len = other.len;
+ }
+}
+
+impl SparseBitSet {
+ /// An empty set that can hold the integers `0..len`, without allocating storage for them.
+ pub fn new(len: usize) -> Self {
+ Self { words: Vec::new(), len }
+ }
+
+ pub fn contains(&self, index: usize) -> bool {
+ assert!(index < self.len, "bit {index} out of range {}", self.len);
+ self.words
+ .binary_search_by_key(&(index / 64), |word| word.index)
+ .is_ok_and(|position| self.words[position].bits & (1 << (index % 64)) != 0)
+ }
+
+ pub fn insert(&mut self, index: usize) {
+ assert!(index < self.len, "bit {index} out of range {}", self.len);
+ let word_index = index / 64;
+ let bits = 1 << (index % 64);
+ match self.words.binary_search_by_key(&word_index, |word| word.index) {
+ Ok(position) => self.words[position].bits |= bits,
+ Err(position) => self.words.insert(
+ position,
+ Word {
+ index: word_index,
+ bits,
+ },
+ ),
+ }
+ }
+
+ pub fn remove(&mut self, index: usize) {
+ assert!(index < self.len, "bit {index} out of range {}", self.len);
+ if let Ok(position) = self.words.binary_search_by_key(&(index / 64), |word| word.index) {
+ self.words[position].bits &= !(1 << (index % 64));
+ if self.words[position].bits == 0 {
+ self.words.remove(position);
+ }
+ }
+ }
+
+ /// Adds every element of `other`. Returns whether this set changed.
+ pub fn union_with(&mut self, other: &Self) -> bool {
+ assert_eq!(self.len, other.len, "union of differently sized sets");
+ // Update the words already present and count the missing ones before growing. Most unions add no words;
+ // the rest merge backwards into the same allocation, without shifting the suffix for each insertion.
+ let mut position = 0;
+ let mut missing = 0;
+ let mut changed = false;
+ for other_word in &other.words {
+ while position < self.words.len() && self.words[position].index < other_word.index {
+ position += 1;
+ }
+ if position < self.words.len() && self.words[position].index == other_word.index {
+ let word = &mut self.words[position];
+ let bits = word.bits | other_word.bits;
+ changed |= bits != word.bits;
+ word.bits = bits;
+ position += 1;
+ } else {
+ missing += 1;
+ }
+ }
+ if missing == 0 {
+ return changed;
+ }
+ let mut left = self.words.len();
+ let mut right = other.words.len();
+ self.words.resize(left + missing, Word::default());
+ let mut output = self.words.len();
+ while right > 0 {
+ let other_word = other.words[right - 1];
+ let word = if left > 0 && self.words[left - 1].index > other_word.index {
+ left -= 1;
+ self.words[left]
+ } else if left > 0 && self.words[left - 1].index == other_word.index {
+ left -= 1;
+ right -= 1;
+ Word {
+ index: other_word.index,
+ bits: self.words[left].bits | other_word.bits,
+ }
+ } else {
+ right -= 1;
+ other_word
+ };
+ output -= 1;
+ self.words[output] = word;
+ }
+ true
+ }
+
+ /// Removes every element of `other`.
+ pub fn subtract(&mut self, other: &Self) {
+ assert_eq!(self.len, other.len, "difference of differently sized sets");
+ let mut position = 0;
+ self.words.retain_mut(|word| {
+ while position < other.words.len() && other.words[position].index < word.index {
+ position += 1;
+ }
+ if position < other.words.len() && other.words[position].index == word.index {
+ word.bits &= !other.words[position].bits;
+ }
+ word.bits != 0
+ });
+ }
+
+ pub fn is_empty(&self) -> bool {
+ self.words.is_empty()
+ }
+
+ /// The elements in increasing order.
+ pub fn iter(&self) -> impl Iterator<Item = usize> + '_ {
+ self.words.iter().flat_map(|word| {
+ let mut bits = word.bits;
+ std::iter::from_fn(move || {
+ if bits == 0 {
+ return None;
+ }
+ let bit = bits.trailing_zeros() as usize;
+ bits &= bits - 1;
+ Some(word.index * 64 + bit)
+ })
+ })
+ }
+}
+
+impl std::fmt::Debug for SparseBitSet {
+ fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ formatter.debug_set().entries(self.iter()).finish()
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::bitset::BitSet;
+
+ #[test]
+ fn storage_depends_on_nonempty_words_not_the_universe() {
+ let mut set = SparseBitSet::new(1_000_000);
+ assert_eq!(set.words.capacity(), 0);
+ for index in [999_999, 64, 63, 65] {
+ set.insert(index);
+ }
+ assert_eq!(set.words.len(), 3);
+ assert_eq!(set.iter().collect::<Vec<_>>(), [63, 64, 65, 999_999]);
+ set.remove(64);
+ assert_eq!(set.words.len(), 3);
+ set.remove(65);
+ assert_eq!(set.words.len(), 2);
+ let copy = set.clone();
+ set.subtract(&copy);
+ assert!(set.is_empty());
+ }
+
+ #[test]
+ fn operations_match_dense_sets() {
+ let mut random = 42u64;
+ let mut next = || {
+ random ^= random << 13;
+ random ^= random >> 7;
+ random ^= random << 17;
+ random as usize
+ };
+ let mut sparse = SparseBitSet::default();
+ let mut dense = BitSet::default();
+ for len in [0, 1, 63, 64, 65, 130, 4097] {
+ sparse.clone_from(&SparseBitSet::new(len));
+ dense.clone_from(&BitSet::new(len));
+ for _ in 0..500 {
+ let mut other_sparse = SparseBitSet::new(len);
+ let mut other_dense = BitSet::new(len);
+ for _ in 0..next() % 32 {
+ if len > 0 {
+ let index = next() % len;
+ other_sparse.insert(index);
+ other_dense.insert(index);
+ }
+ }
+ match next() % 5 {
+ 0 if len > 0 => {
+ let index = next() % len;
+ sparse.insert(index);
+ dense.insert(index);
+ }
+ 1 if len > 0 => {
+ let index = next() % len;
+ sparse.remove(index);
+ dense.remove(index);
+ }
+ 2 => assert_eq!(sparse.union_with(&other_sparse), dense.union_with(&other_dense)),
+ 3 => {
+ sparse.subtract(&other_sparse);
+ dense.subtract(&other_dense);
+ }
+ _ => {
+ sparse.clone_from(&other_sparse);
+ dense.clone_from(&other_dense);
+ }
+ }
+ assert_eq!(sparse.iter().collect::<Vec<_>>(), dense.iter().collect::<Vec<_>>());
+ assert_eq!(sparse.is_empty(), dense.is_empty());
+ assert!(sparse.words.windows(2).all(|words| words[0].index < words[1].index));
+ assert!(sparse.words.iter().all(|word| word.bits != 0));
+ for index in 0..len {
+ assert_eq!(sparse.contains(index), dense.contains(index));
+ }
+ assert_eq!(sparse, sparse.clone());
+ }
+ }
+ }
+}