diff --git a/changes/statement-stack-reads.internal.md b/changes/statement-stack-reads.internal.md new file mode 100644 index 0000000000..5e6f45c919 --- /dev/null +++ b/changes/statement-stack-reads.internal.md @@ -0,0 +1 @@ +- Backend lowering: the statement stack's reads give references to statement handles, and the conditionals place and return handles (`JsStmt::if_head` over the stack). diff --git a/compiler-rust/src/bir/lower/cond.rs b/compiler-rust/src/bir/lower/cond.rs index 722e6f3985..f8f42ee6b6 100644 --- a/compiler-rust/src/bir/lower/cond.rs +++ b/compiler-rust/src/bir/lower/cond.rs @@ -6,8 +6,7 @@ //! before both branches, so no statement is moved once per enclosing //! conditional. A run becomes a child list exactly once. use super::*; -use crate::bir::j::norm; -use crate::bir::{ListId, NodeFlags}; +use crate::bir::NodeFlags; use crate::emit::row::{self, flag}; use crate::js_ir::BinOp; @@ -64,35 +63,6 @@ struct Parts { pub(super) const EMPTY: Span = Span { start: 0, len: 0 }; -/// The statement stack with the implementation, as `norm::simplify_if_head` -/// reads and takes branches. Two references, 16 bytes. Transition: deleted -/// by T6x-a3, when the stack implements `Runs`. -struct StackLists<'s, B: super::js_stmt::JsStmt> { - stack: &'s mut super::out::StatementStack, - js: &'s mut B, -} - -impl norm::IfLists for StackLists<'_, B> { - type Handle = Span; - /// O(recorded empty blocks), with no retained-tail scan. - fn trim(&mut self, b: &mut Bir, span: Span) { self.stack.drop_empty_blocks(self.js, b, span); } - /// O(1): a zero-length run. - fn empty(&mut self, _: &mut Bir) -> Span { EMPTY } - /// O(1) on trimmed runs, plus counted dead-slot skips. - fn last(&mut self, _: &Bir, span: Span) -> Option { self.stack.last(span) } - /// O(1) on trimmed runs, plus counted dead-slot skips. - fn only(&mut self, _: &Bir, span: Span) -> Option { - self.stack.only(span).map(|(_, statement)| statement) - } - /// O(released nodes), leaving other stack runs in place. - fn release(&mut self, b: &mut Bir, span: Span) { - for statement in self.stack.statements(span) { b.release(statement); } - self.stack.kill(span); - } - /// O(run length): move each surviving statement into the pool once. - fn take(&mut self, b: &mut Bir, span: Span) -> ListId { B::list_id(self.stack.take_list(self.js, b, span)) } -} - impl State<'_, B> { /// K1: C:1812, rows R1 to R6 and section 3.7, on entering the node. One /// call is constant, including the two cached branch facts for R5 step 0. @@ -368,13 +338,14 @@ impl State<'_, B> { } /// Fill a reserved slot; an explicit empty block is recorded for the - /// `if_` of an enclosing conditional. O(1). - pub(super) fn place(&mut self, slot: u32, statement: NodeId) { - let handle = B::stmt_from_node(statement); - let empty = self.js.is_empty_block(&self.arena, &handle); - self.statements.fill(slot, handle); - if empty { - self.statements.note_empty_block(slot, statement); + /// `if_` of an enclosing conditional, by the id read before the fill. + /// O(1), and `note_empty_block`'s cost for an empty block. + pub(super) fn place(&mut self, slot: u32, statement: B::S) { + let empty = self.js.is_empty_block(&self.arena, &statement) + .then(|| self.js.stmt_id(&self.arena, &statement)); + self.statements.fill(slot, statement); + if let Some(id) = empty { + self.statements.note_empty_block(slot, id); } } @@ -397,53 +368,40 @@ impl State<'_, B> { self.statements.kill(lower); } - /// Release the statements of a discarded run: one step per statement. + /// Release the statements of a discarded run, each moved out of its + /// slot: one step per slot and per statement, and the nodes released. pub(super) fn release_run(&mut self, run: Span) { - for statement in self.statements.statements(run) { - self.js.release_stmt(&mut self.arena, B::stmt_from_node(statement)); + for statement in self.statements.take_run(run) { + self.js.release_stmt(&mut self.arena, statement); } self.statements.kill(run); } - /// `[return value]`: the statement and the returned value. O(1) on a - /// trimmed run. - fn single_return(&mut self, run: Span) -> Option<(NodeId, NodeId)> { - let (_, statement) = self.statements.only(run)?; - let statement = B::stmt_from_node(statement); - if self.js.stmt_kind(&self.arena, &statement) != row::Kind::Return { - return None; - } - let value = self.js.value_of(&self.arena, &statement)?; - let value = NodeId(self.js.id(&self.arena, value)); - Some((B::stmt_node(statement), value)) + /// `[return value]`: whether the run is one `return` with a value, read + /// in place. O(1) on a trimmed run. + fn single_return(&mut self, run: Span) -> bool { + let Some((_, statement)) = self.statements.only(run) else { + return false; + }; + self.js.stmt_kind(&self.arena, statement) == row::Kind::Return + && self.js.value_of(&self.arena, statement).is_some() } - /// C:5187 take_single_result_assignment: `[target = value]`, the - /// statement and the value. O(1) on a trimmed run. - pub(super) fn single_assignment(&mut self, run: Span, target: VarId) -> Option<(NodeId, NodeId)> { - let (_, statement) = self.statements.only(run)?; - let statement = B::stmt_from_node(statement); - if self.js.stmt_kind(&self.arena, &statement) != row::Kind::Exp { + /// C:5187 take_single_result_assignment: `[target = value]`, the slot of + /// the statement, read in place. O(1) on a trimmed run. + pub(super) fn single_assignment(&mut self, run: Span, target: VarId) -> Option { + let (slot, statement) = self.statements.only(run)?; + if self.js.stmt_kind(&self.arena, statement) != row::Kind::Exp { return None; } - let value = self.js.value_of(&self.arena, &statement)?; + let value = self.js.value_of(&self.arena, statement)?; if self.js.kind(&self.arena, value) != row::Kind::Bin || self.js.aux(&self.arena, value) != BinOp::Eq as u8 || self.js.var_of(&self.arena, self.js.operand(&self.arena, value, 0)) != Some(target) { return None; } - let right = NodeId(self.js.id(&self.arena, self.js.operand(&self.arena, value, 1))); - Some((B::stmt_node(statement), right)) - } - - /// js_stmt_make.ml:186 block_last_is_return_throw_or_continue, on the - /// literal last statement. O(1). - fn terminal(&self, statement: NodeId) -> bool { - matches!( - self.js.stmt_kind(&self.arena, &B::stmt_from_node(statement)), - row::Kind::Return | row::Kind::Throw | row::Kind::Break | row::Kind::Continue - ) + Some(slot) } /// The test without its negation, when it has one. O(1): one step and @@ -458,7 +416,7 @@ impl State<'_, B> { /// `if (test) yes else no` with both lists as given: no rule of J-S3. #[cfg(test)] - fn raw_if(&mut self, test: NodeId, yes: ListId, no: ListId) -> NodeId { + fn raw_if(&mut self, test: NodeId, yes: crate::bir::ListId, no: crate::bir::ListId) -> NodeId { let statement = self.js.if_plain(&mut self.arena, B::from_node(test), B::list_from(yes), B::list_from(no)); B::stmt_node(statement) } @@ -466,7 +424,7 @@ impl State<'_, B> { /// B:1575 simplify_if as one statement. Each run is copied into the /// arena once; all rules receive pool handles, never branch vectors. /// Cost: O(yes + no) statements copied, plus J-S3's. - pub(super) fn if_statement(&mut self, test: NodeId, yes: Span, no: Span) -> NodeId { + pub(super) fn if_statement(&mut self, test: NodeId, yes: Span, no: Span) -> B::S { self.statements.drop_empty_blocks(&mut self.js, &mut self.arena, yes); self.statements.drop_empty_blocks(&mut self.js, &mut self.arena, no); let (yes, no) = if yes.start >= no.start { @@ -476,22 +434,23 @@ impl State<'_, B> { let no = self.statements.take_list(&mut self.js, &mut self.arena, no); (self.statements.take_list(&mut self.js, &mut self.arena, yes), no) }; - let statement = self.js.if_(&mut self.arena, B::from_node(test), yes, no); - B::stmt_node(statement) + self.js.if_(&mut self.arena, B::from_node(test), yes, no) } - /// Shared J-S3 rules 0..6 leave a retained tail on the statement stack. - /// O(consumed statements + test negations), never the untouched tail. - fn if_in_place(&mut self, head: u32, mut test: NodeId, mut yes: Span, mut no: Span) { - let mut lists = StackLists { stack: &mut self.statements, js: &mut self.js }; - if let Some(result) = norm::simplify_if_head( - &mut self.arena, &mut lists, &mut test, &mut yes, &mut no, - ) { + /// J-S3 rules 0..6 through `JsStmt::if_head` over two runs of the + /// stack leave a retained tail on the statement stack. Cost: that of + /// `if_head` (O(consumed statements); a negation stripped counts no + /// step), never the untouched tail; else `if_statement`'s. + fn if_in_place(&mut self, head: u32, test: NodeId, mut yes: Span, mut no: Span) { + let mut condition = B::from_node(test); + if let Some(result) = + self.js.if_head(&mut self.arena, &mut self.statements, &mut condition, &mut yes, &mut no) + { if let Some(statement) = result.statement { self.place(head, statement); } } else { - let statement = self.if_statement(test, yes, no); + let statement = self.if_statement(B::node(condition), yes, no); self.place(head, statement); } } @@ -505,27 +464,28 @@ impl State<'_, B> { } let list = self.statements.take_list(&mut self.js, &mut self.arena, run); let block = self.js.explicit_block(&mut self.arena, list); - // The empty-block table is keyed by the statement's id (on rows its - // row), read before the stack takes the handle. - let empty = self.js.is_empty_block(&self.arena, &block).then(|| NodeId(self.js.stmt_id(&self.arena, &block))); + // The empty-block table is keyed by the statement's id, read before + // the stack takes the handle. + let empty = self.js.is_empty_block(&self.arena, &block) + .then(|| self.js.stmt_id(&self.arena, &block)); self.statements.push(block); - if let Some(block) = empty { - self.statements.note_empty_block(head, block); + if let Some(id) = empty { + self.statements.note_empty_block(head, id); } } /// B:1565 unnest_block over the output from `start`: a block that is its /// only statement is replaced by its statements. Constant when it is not; - /// else one step per statement of the block. - pub(super) fn unnest(&mut self, start: u32) -> Option<(u32, NodeId)> { + /// else one step per statement of the block. Gives the slot of the only + /// statement. + pub(super) fn unnest(&mut self, start: u32) -> Option { loop { let run = self.statements.trim(Self::from(start)); let (slot, block) = self.statements.only(run)?; - let block = B::stmt_from_node(block); - if self.js.stmt_kind(&self.arena, &block) != row::Kind::Block { - return Some((slot, B::stmt_node(block))); + if self.js.stmt_kind(&self.arena, block) != row::Kind::Block { + return Some(slot); } - self.statements.clear(slot); + let block = self.statements.take(slot); let statements = &mut self.statements; self.js.unblock(&mut self.arena, block, |s| statements.push(s)); } @@ -534,11 +494,10 @@ impl State<'_, B> { /// C:7612 compiled_from_effect_block over the output from `start`: a /// lone expression statement becomes the value. O(1), plus `unnest`. fn effect_output(&mut self, start: u32) -> Probed { - let lone = self.unnest(start).map(|(slot, statement)| (slot, B::stmt_from_node(statement))); - let value = match lone { - Some((slot, statement)) if self.js.stmt_kind(&self.arena, &statement) == row::Kind::Exp => { + let value = match self.unnest(start) { + Some(slot) if self.js.stmt_kind(&self.arena, self.statements.at(slot)) == row::Kind::Exp => { + let statement = self.statements.take(slot); let value = self.js.take_value(&mut self.arena, statement); - self.statements.clear(slot); self.statements.trim(Self::from(start)); value } @@ -615,15 +574,17 @@ impl State<'_, B> { test = value; std::mem::swap(&mut yes, &mut no); } - let (Some((then_, _)), Some((else_, _))) = ( + let (Some(then_), Some(else_)) = ( self.single_assignment(yes, target), self.single_assignment(no, target), ) else { return Err((test, yes, no)); }; - let assignment = self.js.take_value(&mut self.arena, B::stmt_from_node(then_)); + let then_ = self.statements.take(then_); + let assignment = self.js.take_value(&mut self.arena, then_); let yes_value = self.js.keep_operand(&mut self.arena, assignment, 1); - let assignment = self.js.take_value(&mut self.arena, B::stmt_from_node(else_)); + let else_ = self.statements.take(else_); + let assignment = self.js.take_value(&mut self.arena, else_); let no_value = self.js.keep_operand(&mut self.arena, assignment, 1); self.kill(yes, no); let value = self.js.cond(&mut self.arena, B::from_node(test), yes_value, no_value); @@ -636,9 +597,9 @@ impl State<'_, B> { &mut self, target: VarId, merged: Result, - ) -> NodeId { + ) -> B::S { match merged { - Ok(value) => B::stmt_node(self.js.assign(&mut self.arena, target, B::from_node(value))), + Ok(value) => self.js.assign(&mut self.arena, target, B::from_node(value)), Err((test, yes, no)) => self.if_statement(test, yes, no), } } @@ -665,12 +626,19 @@ impl State<'_, B> { // Step 3: a free else branch without effects. (Some(yes), None) => self.if_statement(test, yes, EMPTY), (Some(yes), Some(no)) => { - let last = self.statements.last(yes); - if self.single_return(no).is_some() { + // js_stmt_make.ml:186 block_last_is_return_throw_or_continue, + // on the literal last statement. + let terminal = self.statements.last(yes).is_some_and(|last| { + matches!( + self.js.stmt_kind(&self.arena, last), + row::Kind::Return | row::Kind::Throw | row::Kind::Break | row::Kind::Continue + ) + }); + if self.single_return(no) { // Step 4, first form: the then branch follows in place. let test = self.js.not(&mut self.arena, B::from_node(test)); self.if_statement(B::node(test), no, EMPTY) - } else if last.is_some_and(|last| self.terminal(last)) { + } else if terminal { // Step 4, second form: the else branch follows in place. self.if_statement(test, yes, EMPTY) } else { @@ -723,12 +691,13 @@ impl State<'_, B> { let statement = self.js.exp_rule(&mut self.arena, value); self.statements.push(statement); let run = self.statements.since(mark); - let statement = B::stmt_from_node(self.if_statement(B::node(test), run, EMPTY)); - let empty = self.js.is_empty_block(&self.arena, &statement).then(|| NodeId(self.js.stmt_id(&self.arena, &statement))); + let statement = self.if_statement(B::node(test), run, EMPTY); + let empty = self.js.is_empty_block(&self.arena, &statement) + .then(|| self.js.stmt_id(&self.arena, &statement)); let slot = self.statements.mark().start; self.statements.push(statement); - if let Some(statement) = empty { - self.statements.note_empty_block(slot, statement); + if let Some(id) = empty { + self.statements.note_empty_block(slot, id); } } @@ -763,7 +732,7 @@ impl State<'_, B> { return Probed::Out(self.output(parts.start, None, Finished::True)); } // Step 3, C:5368 compile_test_return_block_with_fallback. - if self.single_return(yes).is_none() && self.single_return(no).is_some() { + if !self.single_return(yes) && self.single_return(no) { // One block of `if (!c) E` and `T`; `T` stays in place. let test = self.js.not(&mut self.arena, B::from_node(test)); let statement = self.if_statement(B::node(test), no, EMPTY); @@ -790,15 +759,15 @@ impl State<'_, B> { fn tail_return_conditional(&mut self, parts: Parts, test: NodeId, yes: Span, no: Span) -> Probed { let conjunction = self.js.kind(&self.arena, B::at(test)) == row::Kind::Bin && self.js.aux(&self.arena, B::at(test)) == BinOp::And as u8; - let continues = self.statements.last(yes).is_some_and(|s| self.js.stmt_kind(&self.arena, &B::stmt_from_node(s)) == row::Kind::Continue); - let terminal = self.statements.last(no).is_some_and(|s| matches!(self.js.stmt_kind(&self.arena, &B::stmt_from_node(s)), row::Kind::Return | row::Kind::Throw | row::Kind::Break)); + let continues = self.statements.last(yes).is_some_and(|s| self.js.stmt_kind(&self.arena, s) == row::Kind::Continue); + let terminal = self.statements.last(no).is_some_and(|s| matches!(self.js.stmt_kind(&self.arena, s), row::Kind::Return | row::Kind::Throw | row::Kind::Break)); if conjunction && continues && terminal { - let then_ = B::stmt_from_node(self.merge_tail_guard(no)); + let then_ = self.merge_tail_guard(no); let empty = self.js.list(&mut self.arena, 0, std::iter::empty()); let else_ = self.js.block_of(&mut self.arena, empty); let condition = self.js.not(&mut self.arena, B::from_node(test)); let statement = self.js.if_blocks(&mut self.arena, condition, then_, else_); - self.place(parts.head, B::stmt_node(statement)); + self.place(parts.head, statement); } else { self.if_in_place(parts.head, test, yes, no); let prelude = Span { start: parts.start, len: parts.head - parts.start }; @@ -810,23 +779,24 @@ impl State<'_, B> { /// R6 guard merge (legacy merge_terminal_guard_fallthrough_else): O(s) /// consumed statements; inspect the leading guard and terminal endpoints. - fn merge_tail_guard(&mut self, run: Span) -> NodeId { + fn merge_tail_guard(&mut self, run: Span) -> B::S { let merge = match self.statements.first_single_slot(run) { - Some((slot, statement)) => { - let guard = B::stmt_from_node(statement); - let merges = self.js.stmt_kind(&self.arena, &guard) == row::Kind::If - && self.js.branch_last_kind(&self.arena, &guard, true).is_none() + Some((slot, guard)) => { + let merges = self.js.stmt_kind(&self.arena, guard) == row::Kind::If + && self.js.branch_last_kind(&self.arena, guard, true).is_none() && matches!( - self.js.branch_last_kind(&self.arena, &guard, false), + self.js.branch_last_kind(&self.arena, guard, false), Some(row::Kind::Return | row::Kind::Throw | row::Kind::Break | row::Kind::Continue) - ) - && self.statements.last(run) != Some(statement); - merges.then_some((slot, guard)) + ); + merges.then_some(slot) } None => None, }; - if let Some((slot, guard)) = merge { - self.statements.clear(slot); + // The guard is not the last statement of the run: its slot holds one + // statement, so the slots tell. + let merge = merge.filter(|&slot| self.statements.last_slot(run) != Some(slot)); + if let Some(slot) = merge { + let guard = self.statements.take(slot); let (condition, then_, empty) = self.js.take_if(&mut self.arena, guard); if let Some(empty) = empty { self.js.release_stmt(&mut self.arena, empty); @@ -835,10 +805,10 @@ impl State<'_, B> { let else_ = self.js.block_of(&mut self.arena, rest); let statement = self.js.if_blocks(&mut self.arena, condition, then_, else_); let list = self.js.list(&mut self.arena, 1, std::iter::once(statement)); - B::stmt_node(self.js.block_of(&mut self.arena, list)) + self.js.block_of(&mut self.arena, list) } else { let list = self.statements.take_list(&mut self.js, &mut self.arena, run); - B::stmt_node(self.js.block_of(&mut self.arena, list)) + self.js.block_of(&mut self.arena, list) } } @@ -848,33 +818,37 @@ impl State<'_, B> { let Some(last) = self.statements.last(run) else { return false; }; - if self.js.stmt_kind(&self.arena, &B::stmt_from_node(last)) != row::Kind::Return { + if self.js.stmt_kind(&self.arena, last) != row::Kind::Return { return false; } let mut steps = 0; let mut folds = true; - for statement in self.statements.statements(run) { + let mut statements = self.statements.statement_refs(run).peekable(); + while let Some(statement) = statements.next() { steps += 1; - if statement == last { + // The statement after which there is none is the `return`. + if statements.peek().is_none() { break; } - if self.js.stmt_kind(&self.arena, &B::stmt_from_node(statement)) != row::Kind::Exp { + if self.js.stmt_kind(&self.arena, statement) != row::Kind::Exp { folds = false; break; } } + drop(statements); self.statements.scanned += steps; folds } /// C:5241: `(e1, (e2, ..., value))` from `e1; e2; ...; return value`. - /// One step per statement of the run, which is consumed. + /// One step per slot of the run, whose statements are moved out last + /// first; the caller kills the run. fn fold_return_tail(&mut self, run: Span) -> NodeId { let mut result = None; - for statement in self.statements.statements_rev(run) { + for statement in self.statements.take_run_rev(run) { // Checked by returns_after_effects: a `return` led by expression // statements. - let value = self.js.take_value(&mut self.arena, B::stmt_from_node(statement)); + let value = self.js.take_value(&mut self.arena, statement); result = Some(match result { None => value, Some(result) => self.js.seq_rule(&mut self.arena, value, result), @@ -991,7 +965,7 @@ impl State<'_, B> { let merged = self.merge_assignments(test, target, yes.stmts, no.stmts); let statement = self.assignment_statement(target, merged); self.statements.trim(Self::from(start)); - self.statements.push(B::stmt_from_node(statement)); + self.statements.push(statement); self.output(start, None, Finished::False) } Cont::Value(_) | Cont::Declare(..) | Cont::Effect(Tail::Return(_)) => { @@ -1006,6 +980,8 @@ impl State<'_, B> { #[cfg(test)] mod tests { + use crate::bir::j::norm; + fn pooled_if(b: &mut crate::bir::Bir, test: crate::bir::NodeId, yes: Vec, no: Vec) -> crate::bir::NodeId { let yes = b.lists.push(&yes); let no = b.lists.push(&no); @@ -1651,7 +1627,7 @@ mod tests { let slot = self.state.statements.mark().start; self.state.statements.push(::stmt_from_node(statement)); if kind == 'o' { - self.state.statements.note_empty_block(slot, statement); + self.state.statements.note_empty_block(slot, statement.0); } } self.state.statements.since(mark) @@ -1730,7 +1706,7 @@ mod tests { let (yes_run, no_run) = (hand.run(0, yes), hand.run(1, no)); let condition = hand.test(test); let statement = hand.state.if_statement(condition, yes_run, no_run); - hand.state.statements.push(::stmt_from_node(statement)); + hand.state.statements.push(statement); assert_eq!( hand.text(None), reference(test, yes, no), diff --git a/compiler-rust/src/bir/lower/exit.rs b/compiler-rust/src/bir/lower/exit.rs index 1d7d43dad8..ecf9df75da 100644 --- a/compiler-rust/src/bir/lower/exit.rs +++ b/compiler-rust/src/bir/lower/exit.rs @@ -420,7 +420,7 @@ impl State<'_, B> { let order = self.js.int_literal(&mut self.arena, plan.first); let test = self.js.bin(&mut self.arena, crate::js_ir::BinOp::EqEqEq, exit, order); let statement = self.if_statement(B::node(test), run, super::cond::EMPTY); - self.statements.push(B::stmt_from_node(statement)); + self.statements.push(statement); return; } let keys = crate::bir::KeySpan { start: self.arena.pools.switch_keys.len() as u32, len: plan.len }; diff --git a/compiler-rust/src/bir/lower/export.rs b/compiler-rust/src/bir/lower/export.rs index 97d8fba69f..2fe53c303f 100644 --- a/compiler-rust/src/bir/lower/export.rs +++ b/compiler-rust/src/bir/lower/export.rs @@ -347,7 +347,7 @@ impl State<'_, B> { work.clear(); let mut nested = std::mem::take(&mut self.scratch); debug_assert!(nested.is_empty()); - for stmt in self.statements.statements(run) { + for stmt in self.statements.statement_refs(run).map(|s| NodeId(self.js.stmt_id(&self.arena, s))) { // `nested` holds the statements of the blocks still open, last // first; a zero marks where a block ends. work.push(stmt); diff --git a/compiler-rust/src/bir/lower/location.rs b/compiler-rust/src/bir/lower/location.rs index 8690f894f9..b06cacc028 100644 --- a/compiler-rust/src/bir/lower/location.rs +++ b/compiler-rust/src/bir/lower/location.rs @@ -131,12 +131,11 @@ impl State<'_, B> { if self.locations.is_none() { return; } - if let Some(first) = self.statements.first(run) - && !self.js.stmt_located(&self.arena, &B::stmt_from_node(first)) + if let Some(slot) = self.statements.first_slot(run) + && !self.js.stmt_located(&self.arena, self.statements.head(slot)) && let Some(loc) = self.switch_location(source) { - let mut first = B::stmt_from_node(first); - self.js.locate_stmt(&mut self.arena, &mut first, loc); + self.js.locate_stmt(&mut self.arena, self.statements.head_mut(slot), loc); } } } diff --git a/compiler-rust/src/bir/lower/loops.rs b/compiler-rust/src/bir/lower/loops.rs index 8b44c5aec3..d57f425a92 100644 --- a/compiler-rust/src/bir/lower/loops.rs +++ b/compiler-rust/src/bir/lower/loops.rs @@ -153,7 +153,7 @@ impl State<'_, B> { let guard = self.if_statement(B::node(negated), yes, no); let mark = self.statements.mark(); self.push_loop_list(blocks[0].unwrap()); - self.statements.push(B::stmt_from_node(guard)); + self.statements.push(guard); self.push_loop_list(blocks[1].unwrap()); let list = self.statements.take_list(&mut self.js, &mut self.arena, self.statements.since(mark)); let condition = self.js.boolean(&mut self.arena, true); diff --git a/compiler-rust/src/bir/lower/mod.rs b/compiler-rust/src/bir/lower/mod.rs index 4cb06e2e13..0aed253a13 100644 --- a/compiler-rust/src/bir/lower/mod.rs +++ b/compiler-rust/src/bir/lower/mod.rs @@ -769,7 +769,7 @@ impl State<'_, B> { /// for a discarded suffix; O(1) otherwise. No temporary statement vector. fn append_outputs(&mut self, first: Out, second: Out, value: Option) -> Out { if first.finished == Finished::True { - for statement in self.statements.statements(second.stmts) { self.arena.release(statement); } + for statement in self.statements.take_run(second.stmts) { self.js.release_stmt(&mut self.arena, statement); } if let Some(value) = second.value { self.arena.release(value); } } self.statements.append(first, second, value) diff --git a/compiler-rust/src/bir/lower/out.rs b/compiler-rust/src/bir/lower/out.rs index 4b4d63b43d..80d64987d9 100644 --- a/compiler-rust/src/bir/lower/out.rs +++ b/compiler-rust/src/bir/lower/out.rs @@ -61,9 +61,10 @@ pub struct StatementStack { pub copied: usize, /// Holes and dead ranges crossed by `first`, `only`, `last` and `trim`. pub scanned: usize, - /// Slots of explicit empty block statements, sorted by slot. An entry is - /// stale once its slot was consumed; `drop_empty_blocks` checks the slot. - empties: Vec<(u32, NodeId)>, + /// Slots of explicit empty block statements with the `JsStmt::stmt_id` + /// of each, sorted by slot. An entry is stale once its slot was + /// consumed; `drop_empty_blocks` checks the slot and the id. + empties: Vec<(u32, u32)>, } impl Default for StatementStack { /// An empty stack without capacity. O(1); allocates nothing. @@ -87,14 +88,6 @@ impl StatementStack { [self.slots.capacity(), self.held.capacity(), self.empties.capacity()] } - /// The row of a statement the stack holds, the handle staying - /// in place. O(1). Transition: deleted by T6x-a3. - fn peek(s: &mut B::S) -> NodeId { - let id = B::stmt_node(std::mem::replace(s, B::stmt_from_node(NodeId(u32::MAX)))); - *s = B::stmt_from_node(id); - id - } - /// K0: C:89 BlockOutput::make, start an empty output at the current top. /// O(1). pub fn mark(&self) -> Span { @@ -138,16 +131,17 @@ impl StatementStack { } }) } - /// A:191 sequence_arguments: the rows of a run, in order, skipping - /// reserved holes without copying statements. Cost: O(slots of the run - /// + held statements), each statement read through `peek`. - pub(super) fn statements(&mut self, span: Span) -> impl Iterator + '_ { + /// The statements of a run moved out of their slots, in order; a slot + /// or a hold that gave its statements is left a hole, and the caller + /// kills the run. Cost: O(slots of the run + held statements), each + /// statement moved once. + pub(super) fn take_run(&mut self, span: Span) -> impl Iterator + '_ { let (mut at, end) = (span.start as usize, span.end()); let (mut inner, mut inner_end) = (0usize, 0usize); std::iter::from_fn(move || loop { if inner < inner_end { inner += 1; - return Some(Self::peek(self.held[inner - 1].as_mut().expect("held statement"))); + return Some(self.held[inner - 1].take().expect("held statement")); } if at >= end { return None; @@ -157,59 +151,34 @@ impl StatementStack { Some(Slot::Dead(len)) => len as usize, _ => 1, }; - match &mut self.slots[current] { - Some(Slot::Statement(s)) => return Some(Self::peek(s)), + match self.slots[current].take() { + Some(Slot::Statement(s)) => return Some(s), Some(Slot::Held(span)) => (inner, inner_end) = (span.start as usize, span.end()), - Some(Slot::Dead(_)) | None => {} + other => self.slots[current] = other, } }) } /// C:5241 try_fold_effect_only_return_tail folds a run from its end: - /// the rows of a run, last first. Cost: O(slots of the run + held - /// statements): one step per slot and per held statement. - pub(super) fn statements_rev(&mut self, span: Span) -> impl Iterator + '_ { + /// the statements of a run moved out, last first; a slot or a hold that + /// gave its statements is left a hole, and the caller kills the run. + /// Cost: O(slots of the run + held statements), one step per slot and + /// per held statement, each statement moved once. + pub(super) fn take_run_rev(&mut self, span: Span) -> impl Iterator + '_ { let (start, mut at) = (span.start as usize, span.end()); let (mut inner_start, mut inner) = (0usize, 0usize); std::iter::from_fn(move || loop { if inner > inner_start { inner -= 1; - return Some(Self::peek(self.held[inner].as_mut().expect("held statement"))); + return Some(self.held[inner].take().expect("held statement")); } if at <= start { return None; } at -= 1; - match &mut self.slots[at] { - Some(Slot::Statement(s)) => return Some(Self::peek(s)), - Some(Slot::Held(span)) => (inner_start, inner) = (span.start as usize, span.end()), - Some(Slot::Dead(_)) | None => {} - } - }) - } - /// The statements of a run moved out of their slots, in order; a slot - /// or a hold that gave its statements is left a hole, and the caller - /// kills the run. Cost: O(slots of the run + held statements), each - /// statement moved once. - fn take_run(&mut self, span: Span) -> impl Iterator + '_ { - let (mut at, end) = (span.start as usize, span.end()); - let (mut inner, mut inner_end) = (0usize, 0usize); - std::iter::from_fn(move || loop { - if inner < inner_end { - inner += 1; - return Some(self.held[inner - 1].take().expect("held statement")); - } - if at >= end { - return None; - } - let current = at; - at += match self.slots[current] { - Some(Slot::Dead(len)) => len as usize, - _ => 1, - }; - match self.slots[current].take() { + match self.slots[at].take() { Some(Slot::Statement(s)) => return Some(s), - Some(Slot::Held(span)) => (inner, inner_end) = (span.start as usize, span.end()), - other => self.slots[current] = other, + Some(Slot::Held(span)) => (inner_start, inner) = (span.start as usize, span.end()), + other => self.slots[at] = other, } }) } @@ -222,14 +191,13 @@ impl StatementStack { Some(Slot::Dead(_)) | None => 0, } } - /// The row of statement `index` of slot `at`, which holds more than - /// `index` statements, read through `peek`. O(1). - fn slot_row(&mut self, at: usize, index: usize) -> NodeId { - match &mut self.slots[at] { - Some(Slot::Statement(s)) => Self::peek(s), + /// Statement `index` of slot `at`, which holds more than `index` + /// statements, read in place. O(1). + fn slot_ref(&self, at: usize, index: usize) -> &B::S { + match &self.slots[at] { + Some(Slot::Statement(s)) => s, Some(Slot::Held(span)) => { - let i = span.start as usize + index; - Self::peek(self.held[i].as_mut().expect("held statement")) + self.held[span.start as usize + index].as_ref().expect("held statement") } Some(Slot::Dead(_)) | None => unreachable!("a slot without a statement"), } @@ -249,48 +217,102 @@ impl StatementStack { } None } - /// B:1575 simplify_if reads the head of a branch. Costs the holes and - /// dead ranges before the first statement, one step each; nothing is - /// copied. - pub(super) fn first(&mut self, span: Span) -> Option { + /// The statement of a slot that holds one, read in place: a statement + /// slot or a hold of exactly one, the slots `only` and + /// `first_single_slot` give. O(1). Panics on any other slot. + pub(super) fn at(&self, slot: u32) -> &B::S { + assert!(self.slot_len(slot as usize) == 1, "slot {slot} holds no single statement"); + self.slot_ref(slot as usize, 0) + } + /// B:1565 unnest_block and C:7612 compiled_from_effect_block replace the + /// only statement of an output: the statement of a slot that holds one, + /// moved out; the slot becomes `Dead(1)`. A hold of exactly one + /// statement is such a slot (`only` and `first_single_slot` give it), + /// and its statement is moved out of the hold. O(1): one write, two + /// for a hold. Panics on any other slot. + pub(super) fn take(&mut self, slot: u32) -> B::S { + match self.slots[slot as usize].replace(Slot::Dead(1)) { + Some(Slot::Statement(s)) => s, + Some(Slot::Held(span)) if span.len == 1 => { + self.held[span.start as usize].take().expect("held statement") + } + _ => panic!("slot {slot} holds no single statement"), + } + } + /// The first statement of a slot that holds one or a hold that is not + /// empty, read in place. O(1). + pub(super) fn head(&self, slot: u32) -> &B::S { + self.slot_ref(slot as usize, 0) + } + /// `head`, to be changed in place (`JsStmt::locate_stmt`). O(1). + pub(super) fn head_mut(&mut self, slot: u32) -> &mut B::S { + match &mut self.slots[slot as usize] { + Some(Slot::Statement(s)) => s, + Some(Slot::Held(span)) => self.held[span.start as usize].as_mut().expect("held statement"), + Some(Slot::Dead(_)) | None => unreachable!("a slot without a statement"), + } + } + /// B:1575 simplify_if reads the head of a branch, in place. Costs the + /// holes and dead ranges before the first statement, one step each; + /// nothing is copied. + pub(super) fn first(&mut self, span: Span) -> Option<&B::S> { let at = self.live(span.start as usize, span.end())?; - Some(self.slot_row(at, 0)) + Some(self.slot_ref(at, 0)) } - /// First slot when it contains one statement. O(leading holes/dead ranges), - /// using the same bounded scan as first; held multi-statement slots stay put. - pub(super) fn first_single_slot(&mut self, span: Span) -> Option<(u32, NodeId)> { + /// The slot of the first statement of a run, as `first` finds it. Costs + /// the holes and dead ranges before it, one step each. + pub(super) fn first_slot(&mut self, span: Span) -> Option { + self.live(span.start as usize, span.end()).map(|at| at as u32) + } + /// First slot when it contains one statement, with that statement read + /// in place. O(leading holes/dead ranges), using the same bounded scan + /// as first; held multi-statement slots stay put. + pub(super) fn first_single_slot(&mut self, span: Span) -> Option<(u32, &B::S)> { let at = self.live(span.start as usize, span.end())?; if self.slot_len(at) != 1 { return None; } - Some((at as u32, self.slot_row(at, 0))) + Some((at as u32, self.slot_ref(at, 0))) } /// B:1575 simplify_if, `[statement]` patterns: the slot and the statement - /// when the run holds exactly one. Costs the holes and dead ranges of the - /// run, one step each, which a trimmed run has only before its first - /// statement. - pub(super) fn only(&mut self, span: Span) -> Option<(u32, NodeId)> { + /// read in place when the run holds exactly one. Costs the holes and + /// dead ranges of the run, one step each, which a trimmed run has only + /// before its first statement. + pub(super) fn only(&mut self, span: Span) -> Option<(u32, &B::S)> { let at = self.live(span.start as usize, span.end())?; if self.slot_len(at) != 1 || self.live(at + 1, span.end()).is_some() { return None; } - Some((at as u32, self.slot_row(at, 0))) + Some((at as u32, self.slot_ref(at, 0))) } - /// B:1575 simplify_if, `block_last_is_return_throw_or_continue`: the last - /// statement. One step on a trimmed run; otherwise one per trailing hole - /// or dead slot. - pub(super) fn last(&mut self, span: Span) -> Option { + /// The slot of the last statement of a run and its index in the slot. + /// One step on a trimmed run; otherwise one per trailing hole or dead + /// slot, counted in `scanned`. + fn last_at(&mut self, span: Span) -> Option<(usize, usize)> { let mut at = span.end(); while at > span.start as usize { at -= 1; let len = self.slot_len(at); if len != 0 { - return Some(self.slot_row(at, len - 1)); + return Some((at, len - 1)); } self.scanned += 1; } None } + /// B:1575 simplify_if, `block_last_is_return_throw_or_continue`: the last + /// statement, read in place. One step on a trimmed run; otherwise one + /// per trailing hole or dead slot. + pub(super) fn last(&mut self, span: Span) -> Option<&B::S> { + let (at, index) = self.last_at(span)?; + Some(self.slot_ref(at, index)) + } + /// The slot of the last statement of a run, as `last` finds it. Cost: + /// O(1) on a trimmed run; otherwise one step per trailing hole or dead + /// slot. + pub(super) fn last_slot(&mut self, span: Span) -> Option { + self.last_at(span).map(|(at, _)| at as u32) + } /// K0: C:119 append. Pop the holes and dead slots at the top, down to /// `floor`, so the run above `floor` ends in a statement or is empty. /// One step per popped slot; each is popped once. @@ -321,24 +343,30 @@ impl StatementStack { self.slots[span.start as usize] = Some(Slot::Dead(span.len)); } } - /// B:1225 block: record that `slot` holds an explicit empty block. - /// Cost: O(log E) for a binary search among the unit's E empty blocks, - /// and an insertion. - pub(super) fn note_empty_block(&mut self, slot: u32, statement: NodeId) { + /// B:1225 block: record that `slot` holds an explicit empty block whose + /// `JsStmt::stmt_id` is `id`, read by the caller before the block was + /// pushed or filled. Cost: O(log E) for a binary search among the + /// unit's E noted empty blocks, and an insertion that moves the entries + /// after its place: O(E) in the worst case, nothing for a block noted + /// above every other (the common case, a block at the top of the stack). + pub(super) fn note_empty_block(&mut self, slot: u32, id: u32) { let at = self.empties.partition_point(|&(other, _)| other < slot); - self.empties.insert(at, (slot, statement)); + self.empties.insert(at, (slot, id)); } /// B:1575 simplify_if drops the empty blocks of both branches first. - /// Cost: two binary searches among the unit's empty blocks, then one - /// step per empty block of the run, each released through - /// `JsStmt::release_stmt`. The run itself is not scanned. + /// Cost: two binary searches among the unit's E noted empty blocks, then + /// one step per empty block of the run, each read through + /// `JsStmt::stmt_id` and released through `JsStmt::release_stmt`, and + /// the drain that moves the entries after the run's: O(E) in the worst + /// case, nothing when the run's are the last. The run itself is not + /// scanned. pub(super) fn drop_empty_blocks(&mut self, js: &mut B, a: &mut Pools, span: Span) { let from = self.empties.partition_point(|&(slot, _)| slot < span.start); let to = self.empties.partition_point(|&(slot, _)| (slot as usize) < span.end()); - for (slot, statement) in self.empties.drain(from..to) { + for (slot, id) in self.empties.drain(from..to) { let slot = &mut self.slots[slot as usize]; let Some(Slot::Statement(held)) = slot else { continue }; - if Self::peek(held) == statement && js.is_empty_block(a, held) { + if js.stmt_id(a, held) == id && js.is_empty_block(a, held) { let Some(Slot::Statement(held)) = slot.replace(Slot::Dead(1)) else { unreachable!("a statement slot") }; @@ -346,11 +374,6 @@ impl StatementStack { } } } - /// B:1565 unnest_block and C:7612 compiled_from_effect_block replace the - /// only statement of an output. One write. - pub(super) fn clear(&mut self, slot: u32) { - self.slots[slot as usize] = Some(Slot::Dead(1)); - } /// A:191 sequence_arguments: a reserved hold is not an argument /// statement. O(holes and dead ranges before the first statement). pub(super) fn has_statements(&self, span: Span) -> bool { @@ -499,6 +522,57 @@ impl StatementStack { } } +#[cfg(test)] +impl StatementStack { + /// The rows of the statements of a run. One step per + /// slot of the run. + pub(super) fn statements(&self, span: Span) -> impl Iterator + '_ { + self.statement_refs(span).map(|s| s.row()) + } +} + +/// The stack's runs for `JsStmt::if_head`: a run is a span of slots, and a +/// branch that is kept stays where it was lowered. +impl super::js_stmt::Runs for StatementStack { + type Run = Span; + /// `drop_empty_blocks`: two binary searches, one step per noted empty + /// block of the run, and the drain that moves the entries noted after + /// the run's: O(E) for the unit's E noted empty blocks in the worst + /// case, nothing when the run's are the last. + fn trim(&mut self, js: &mut B, a: &mut Pools, run: Span) { + self.drop_empty_blocks(js, a, run); + } + /// A span of no slot. O(1). + fn empty(&mut self) -> Span { + Span::default() + } + /// The stack's `last`: O(1) on a run that ends in a statement, else one + /// step per trailing hole or dead slot. + fn last(&mut self, run: Span) -> Option<&B::S> { + StatementStack::last(self, run) + } + /// The statement of the stack's `only`: the holes and dead ranges of the + /// run, one step each. + fn only(&mut self, run: Span) -> Option<&B::S> { + StatementStack::only(self, run).map(|(_, statement)| statement) + } + /// Each statement moved out with `take_run` and released + /// (`JsStmt::release_stmt`), then the run killed: one step per slot and + /// per node released, one write per slot. + fn release(&mut self, js: &mut B, a: &mut Pools, run: Span) { + for statement in self.take_run(run) { + js.release_stmt(a, statement); + } + self.kill(run); + } + /// `take_list`: one step per slot and per statement, each moved once, + /// and the list closed (`close::decide`: one more step per statement) + /// when the arena counts JavaScript occurrences. + fn take(&mut self, js: &mut B, a: &mut Pools, run: Span) -> B::L { + self.take_list(js, a, run) + } +} + /// A linear ownership token: deferred combinations cannot be realized twice. #[derive(Debug, PartialEq, Eq)] pub struct FreeId(u32); @@ -747,6 +821,22 @@ mod tests { assert_eq!(stack.copied, 10_001); assert_eq!(stack.mark(), Span::default()); } + /// A hold of one statement is a slot `only` and `first_single_slot` + /// give, so `at` reads it and `take` moves it out, as a statement slot. + #[test] + fn a_hold_of_one_statement_is_read_and_taken_as_a_slot_of_one() { + let mut stack = StatementStack::::default(); + let mark = stack.mark(); + let slot = stack.reserve(); + stack.fill_held(slot, [Rows::stmt_from_node(NodeId(7))]); + let run = stack.since(mark); + assert_eq!(stack.only(run).map(|(at, s)| (at, s.row())), Some((slot, NodeId(7)))); + assert_eq!(stack.first_single_slot(run).map(|(at, s)| (at, s.row())), Some((slot, NodeId(7)))); + assert_eq!(stack.at(slot).row(), NodeId(7)); + assert_eq!(stack.take(slot).row(), NodeId(7)); + assert!(!stack.has_statements(run)); + assert_eq!(stack.only(run).map(|(at, _)| at), None); + } /// The list of a hold of `n` statements: (i) pushed above a reserved /// slot and moved into it with `hold_run`, or (ii) given to `hold` and /// placed with `fill_held_since`. Each enters its list once. diff --git a/compiler-rust/src/bir/lower/switch.rs b/compiler-rust/src/bir/lower/switch.rs index 1959579c33..a33399592d 100644 --- a/compiler-rust/src/bir/lower/switch.rs +++ b/compiler-rust/src/bir/lower/switch.rs @@ -879,10 +879,11 @@ impl State<'_, B> { } else { None }; - if let Some((statement, _)) = + if let Some(slot) = candidate.and_then(|run| self.single_assignment(run, target)) { - let assignment = self.js.take_value(&mut self.arena, B::stmt_from_node(statement)); + let statement = self.statements.take(slot); + let assignment = self.js.take_value(&mut self.arena, statement); let value = self.js.keep_operand(&mut self.arena, assignment, 1); self.statements.kill(candidate.unwrap()); let statement = self.js.declare(&mut self.arena, kind, target, Some(value)); @@ -1102,11 +1103,10 @@ impl State<'_, B> { Cont::Effect(_) => self.if_statement(test, yes, no), _ => unreachable!("small switch branches assign their result"), }; - let statement = B::stmt_from_node(statement); if matches!(cx.cont, Cont::Effect(Tail::No)) && self.js.is_empty_block(&self.arena, &statement) { self.js.release_stmt(&mut self.arena, statement); } else { - self.place(plan.head, B::stmt_node(statement)); + self.place(plan.head, statement); } } self.switch_result(cx, plan) diff --git a/compiler-rust/src/bir/lower/untagged.rs b/compiler-rust/src/bir/lower/untagged.rs index 0c57654504..cbfc518227 100644 --- a/compiler-rust/src/bir/lower/untagged.rs +++ b/compiler-rust/src/bir/lower/untagged.rs @@ -789,12 +789,13 @@ impl State<'_, B> { start: out.run.start, len: 0, }); - let block = if let Some((_, statement)) = only { + let block = if let Some(slot) = only { + let statement = self.statements.take(slot); self.statements.kill(run); statement } else { let list = self.statements.take_list(&mut self.js, &mut self.arena, run); - B::stmt_node(self.js.explicit_block(&mut self.arena, list)) + self.js.explicit_block(&mut self.arena, list) }; self.place(start.start, block); self.untagged_storage.outputs.push( @@ -841,7 +842,7 @@ impl State<'_, B> { { !self.statements.last(out.run).is_some_and(|s| { matches!( - self.js.stmt_kind(&self.arena, &B::stmt_from_node(s)), + self.js.stmt_kind(&self.arena, s), row::Kind::Return | row::Kind::Throw | row::Kind::Continue | row::Kind::Break ) }) diff --git a/compiler-rust/src/bir/lower/variant.rs b/compiler-rust/src/bir/lower/variant.rs index 188cb3e3d3..2942b21529 100644 --- a/compiler-rust/src/bir/lower/variant.rs +++ b/compiler-rust/src/bir/lower/variant.rs @@ -477,11 +477,11 @@ impl State<'_, B> { } if plan.mixed { let test = self.named_literal_test(&mut plan); - let statement = B::stmt_from_node(self.if_statement(test, plan.sides[0].run, plan.sides[1].run)); + let statement = self.if_statement(test, plan.sides[0].run, plan.sides[1].run); if matches!(cx.cont, Cont::Effect(Tail::No)) && self.js.is_empty_block(&self.arena, &statement) { self.js.release_stmt(&mut self.arena, statement); } else { - self.place(plan.head, B::stmt_node(statement)); + self.place(plan.head, statement); } } if let Some(value) = plan.value {