Improve regify

This commit is contained in:
2026-09-09 19:20:23 +00:00
parent 75bd52d8ec
commit f99ebcc722
2 changed files with 135 additions and 140 deletions
+91
View File
@@ -0,0 +1,91 @@
//! A block is a run of instructions control enters at the top and leaves at the
//! bottom. Blocks are all regify needs: they say what stack a block is entered
//! with, and where two paths meet -- the only thing a linear walk gets wrong.
use crate::parse::{ImmOperand, Instr, opcodes::OpCode};
#[derive(Debug)]
pub struct Block {
/// The blocks control can reach from this one.
pub succ: Vec<usize>,
/// `(slot value, incoming value)` for every path in AFTER the first. The
/// slot keeps the id the first path gave it, so a later path's value has to
/// be joined into it. That is the phi, and it stays data because nothing
/// here has to run it -- infer folds the types together instead.
pub phi: Vec<(usize, usize)>,
}
pub fn find_basic_blocks(code: &[Instr], pcs: &[usize]) -> (Vec<usize>, Vec<Block>) {
let mut starts = vec![0];
for (at, instr) in code.iter().enumerate() {
if let Some(target) = jump_target(instr.imm_operands, pcs) {
starts.push(target);
}
if ends_block(instr, pcs) && at + 1 < code.len() {
starts.push(at + 1);
}
}
starts.sort_unstable();
starts.dedup();
let ends = |i: usize| starts.get(i + 1).copied().unwrap_or(code.len());
// A block reaches wherever its last instruction goes: the jump's target,
// and the block below whenever control can run off the end.
let blocks = (0..starts.len())
.map(|i| {
let last = &code[ends(i) - 1];
let mut succ = vec![];
if let Some(t) = jump_target(last.imm_operands, pcs) {
succ.extend(starts.iter().position(|&s| s == t));
}
if falls_through(last.op) && i + 1 < starts.len() {
succ.push(i + 1);
}
Block { succ, phi: vec![] }
})
.collect();
(starts, blocks)
}
/// True when a block cannot carry on past this instruction, because it jumps
/// or because it leaves the function.
fn ends_block(instr: &Instr, pcs: &[usize]) -> bool {
!falls_through(instr.op) || jump_target(instr.imm_operands, pcs).is_some()
}
/// The instruction a jump lands on. A target arrives as a byte offset, so it is
/// looked up in the pcs; one past the end lands nowhere and is dropped.
fn jump_target(imm: ImmOperand, pcs: &[usize]) -> Option<usize> {
let target = match imm {
ImmOperand::Label { target }
| ImmOperand::LabelU16 { target, .. }
| ImmOperand::AtomLabelU8 { target, .. }
| ImmOperand::AtomLabelU16 { target, .. } => target,
_ => return None,
};
pcs.iter().position(|&pc| pc == target)
}
/// True when control can leave a block by running off the end of it.
fn falls_through(op: OpCode) -> bool {
!matches!(
op,
OpCode::Goto
| OpCode::Goto8
| OpCode::Goto16
| OpCode::Return
| OpCode::ReturnUndef
| OpCode::ReturnAsync
| OpCode::Throw
)
}
@@ -1,10 +1,15 @@
mod basic_block;
use std::fmt::Debug; use std::fmt::Debug;
use crate::{ use crate::{
flatten::{Capture, FlatFn}, flatten::{Capture, FlatFn},
parse::{ImmOperand, Instr, JsValue, opcodes::OpCode}, parse::{ImmOperand, JsValue, opcodes::OpCode},
}; };
pub use basic_block::Block;
use basic_block::find_basic_blocks;
#[derive(Clone)] #[derive(Clone)]
pub struct RegInstr { pub struct RegInstr {
/// True on the first instruction of a basic block. It rides with the /// True on the first instruction of a basic block. It rides with the
@@ -51,45 +56,6 @@ impl Debug for RegInstr {
} }
} }
// ─────────────────────────────── Basic Blocks ───────────────────────────────
// A block is a run of instructions control enters at the top and leaves at the
// bottom. Blocks are all regify needs: they say what stack a block is entered
// with, and where two paths meet -- the only thing a linear walk gets wrong.
#[derive(Debug)]
pub struct Block {
/// The blocks control can reach from this one.
pub succ: Vec<usize>,
/// The value ids this block's stack slots hold when it is entered. The
/// first path in fixes them. None until one arrives.
entry: Option<Vec<usize>>,
/// `(slot value, incoming value)` for every path in AFTER the first. The
/// slot keeps the id the first path gave it, so a later path's value has to
/// be joined into it. That is the phi, and it stays data because nothing
/// here has to run it -- infer folds the types together instead.
pub phi: Vec<(usize, usize)>,
}
impl Block {
/// Arrive along one edge, with `stack` in hand. The first arrival names the
/// slots; every later one is reconciled against those names.
fn arrive(&mut self, stack: &[usize]) {
match &self.entry {
None => self.entry = Some(stack.to_vec()),
Some(slots) => self
.phi
.extend(slots.iter().copied().zip(stack.iter().copied())),
}
}
/// The stack this block starts with. Empty when nothing reaches it.
fn entry_stack(&self) -> Vec<usize> {
self.entry.clone().unwrap_or_default()
}
}
#[derive(Debug)] #[derive(Debug)]
pub struct RegFn { pub struct RegFn {
pub name: String, pub name: String,
@@ -111,22 +77,29 @@ impl RegFn {
let mut next_val = flat_fn.var_count; // Reserve values for locals let mut next_val = flat_fn.var_count; // Reserve values for locals
// Emit per block, so blocks can be WALKED in the order control reaches // One instruction in, one out, so a block's output lands exactly where
// them and still be READ in listing order. Listing order alone is not // its input was and blocks can be walked in any order. A block nothing
// enough: a for-of puts its body before the test, and the body is // reaches keeps its nops, which is what unreachable code should leave.
// reached only by the back edge, so walking it first finds no stack. let mut output =
let mut emitted = vec![Vec::new(); blocks.len()]; vec![RegInstr::new(OpCode::Nop, ImmOperand::None, vec![], vec![]); flat_fn.code.len()];
let mut seen = vec![false; blocks.len()];
let mut todo = vec![0];
seen[0] = true;
while let Some(b) = todo.pop() { // A worklist, because listing order does not reach every block: a
// for-of puts its body BEFORE the test, so the body is entered only by
// the back edge, from a block further down. A block joins the list the
// once, when the first path arrives with a stack for it. The function
// itself is entered at block 0, holding nothing.
let mut entry_stack = vec![None; blocks.len()];
let mut worklist = vec![];
entry_stack[0] = Some(vec![]);
worklist.push(0);
while let Some(b) = worklist.pop() {
// A block starts with the stack it is ENTERED with, not with // A block starts with the stack it is ENTERED with, not with
// whatever the block above it in the listing left behind. This one // whatever the block above it in the listing left behind. This one
// line is what stops the walk running off a `return` into a // line is what stops the walk running off a `return` into a
// jump-only path, or through a `goto` into the arm it jumps over. // jump-only path, or through a `goto` into the arm it jumps over.
let mut stack = blocks[b].entry_stack(); let mut stack = entry_stack[b].clone().unwrap_or_default();
let mut first = true;
let mut fresh_val = || { let mut fresh_val = || {
let v = next_val; let v = next_val;
@@ -136,7 +109,9 @@ impl RegFn {
let end = starts.get(b + 1).copied().unwrap_or(flat_fn.code.len()); let end = starts.get(b + 1).copied().unwrap_or(flat_fn.code.len());
for instr in &flat_fn.code[starts[b]..end] { for at in starts[b]..end {
let instr = &flat_fn.code[at];
let mut reg_instr = RegInstr { let mut reg_instr = RegInstr {
block_start: false, block_start: false,
dsts: vec![], dsts: vec![],
@@ -219,26 +194,33 @@ impl RegFn {
} }
} }
reg_instr.block_start = std::mem::take(&mut first); output[at] = reg_instr;
emitted[b].push(reg_instr);
} }
// Every edge out of here arrives at its target with this stack. // Every edge out of here arrives at its target with this stack.
// Walking a block only after something has arrived at it is what
// makes the entry stack right, and it is why the order is the CFG's
// and not the listing's.
for i in 0..blocks[b].succ.len() { for i in 0..blocks[b].succ.len() {
let to = blocks[b].succ[i]; let to = blocks[b].succ[i];
blocks[to].arrive(&stack);
if !seen[to] { // A later path is reconciled against the names the first one
seen[to] = true; // left, and that pairing is the phi.
todo.push(to); if let Some(slots) = &entry_stack[to] {
blocks[to]
.phi
.extend(slots.iter().copied().zip(stack.iter().copied()));
}
// The first path names the block's slots, and is the one that
// puts it on the worklist.
else {
entry_stack[to] = Some(stack.clone());
worklist.push(to);
} }
} }
} }
let output = emitted.into_iter().flatten().collect::<Vec<RegInstr>>(); // Every block begins where its first instruction sits, walked or not.
for &start in &starts {
output[start].block_start = true;
}
Self { Self {
name: flat_fn.name, name: flat_fn.name,
@@ -266,84 +248,6 @@ impl RegFn {
/// jump target, and after anything a block cannot continue past. /// jump target, and after anything a block cannot continue past.
/// ///
/// Two things come back: where each block starts, which is only good while the /// Two things come back: where each block starts, which is only good while the
/// code stands still, and the block itself, which outlives every later pass.
fn find_basic_blocks(code: &[Instr], pcs: &[usize]) -> (Vec<usize>, Vec<Block>) {
let mut starts = vec![0];
for (at, instr) in code.iter().enumerate() {
if let Some(target) = jump_target(instr.imm_operands, pcs) {
starts.push(target);
}
if ends_block(instr, pcs) && at + 1 < code.len() {
starts.push(at + 1);
}
}
starts.sort_unstable();
starts.dedup();
let ends = |i: usize| starts.get(i + 1).copied().unwrap_or(code.len());
// A block reaches wherever its last instruction goes: the jump's target,
// and the block below whenever control can run off the end.
let blocks = (0..starts.len())
.map(|i| {
let last = &code[ends(i) - 1];
let mut succ = vec![];
if let Some(t) = jump_target(last.imm_operands, pcs) {
succ.extend(starts.iter().position(|&s| s == t));
}
if falls_through(last.op) && i + 1 < starts.len() {
succ.push(i + 1);
}
Block {
succ,
entry: None,
phi: vec![],
}
})
.collect();
(starts, blocks)
}
/// True when a block cannot carry on past this instruction, because it jumps
/// or because it leaves the function.
fn ends_block(instr: &Instr, pcs: &[usize]) -> bool {
!falls_through(instr.op) || jump_target(instr.imm_operands, pcs).is_some()
}
/// The instruction a jump lands on. A target arrives as a byte offset, so it is
/// looked up in the pcs; one past the end lands nowhere and is dropped.
fn jump_target(imm: ImmOperand, pcs: &[usize]) -> Option<usize> {
let target = match imm {
ImmOperand::Label { target }
| ImmOperand::LabelU16 { target, .. }
| ImmOperand::AtomLabelU8 { target, .. }
| ImmOperand::AtomLabelU16 { target, .. } => target,
_ => return None,
};
pcs.iter().position(|&pc| pc == target)
}
/// True when control can leave a block by running off the end of it.
fn falls_through(op: OpCode) -> bool {
!matches!(
op,
OpCode::Goto
| OpCode::Goto8
| OpCode::Goto16
| OpCode::Return
| OpCode::ReturnUndef
| OpCode::ReturnAsync
| OpCode::Throw
)
}
pub fn regify(flat_fns: Vec<FlatFn>) -> Vec<RegFn> { pub fn regify(flat_fns: Vec<FlatFn>) -> Vec<RegFn> {
flat_fns.into_iter().map(RegFn::from_flat).collect() flat_fns.into_iter().map(RegFn::from_flat).collect()