| @@ -0,0 +1,168 @@ |
| 1 | +//! Drop specialisation, after dup/drop insertion. |
| 2 | +//! |
| 3 | +//! Matching on a value `x` and then dropping it is, after insertion: |
| 4 | +//! |
| 5 | +//! ```text |
| 6 | +//! let a = x.0; dup a; let b = x.1; dup b; drop x |
| 7 | +//! ``` |
| 8 | +//! |
| 9 | +//! **Drop specialisation** turns this into `release x [0: a, 1: b]`: if `x` |
| 10 | +//! is unique, its cell is freed and `a` and `b` take over its references |
| 11 | +//! (no count changes on the fields); otherwise they are dup'd and `x` is |
| 12 | +//! dropped, as before. |
| 13 | +//! |
| 14 | +//! The specialisation only looks at the statements just before the drop |
| 15 | +//! (projections, `dup`s and other `drop`s, which cannot unwind), so it |
| 16 | +//! never moves code across a call. |
| 17 | + |
| 18 | +use std::collections::BTreeSet; |
| 19 | + |
| 20 | +use tilc_core::ir::{Atom, Expr, Value, VarId}; |
| 21 | + |
| 22 | +/// A statement of a straight-line sequence. |
| 23 | +enum Stmt { |
| 24 | + Let(VarId, Value), |
| 25 | + Dup(VarId), |
| 26 | + Drop(VarId), |
| 27 | + Release { |
| 28 | + var: VarId, |
| 29 | + fields: Vec<(u32, VarId)>, |
| 30 | + }, |
| 31 | +} |
| 32 | + |
| 33 | +/// Specialise the drops of `e`. |
| 34 | +pub(crate) fn specialise(e: Expr) -> Expr { |
| 35 | + let (stmts, tail) = peel(e); |
| 36 | + let stmts: Vec<Stmt> = stmts |
| 37 | + .into_iter() |
| 38 | + .map(|s| match s { |
| 39 | + Stmt::Let(v, Value::Block(inner)) => { |
| 40 | + Stmt::Let(v, Value::Block(Box::new(specialise(*inner)))) |
| 41 | + } |
| 42 | + s => s, |
| 43 | + }) |
| 44 | + .collect(); |
| 45 | + let mut e = match tail { |
| 46 | + Expr::Switch { on, cases, default } => Expr::Switch { |
| 47 | + on, |
| 48 | + cases: cases.into_iter().map(|(t, b)| (t, specialise(b))).collect(), |
| 49 | + default: default.map(|d| Box::new(specialise(*d))), |
| 50 | + }, |
| 51 | + Expr::SwitchLit { on, cases, default } => Expr::SwitchLit { |
| 52 | + on, |
| 53 | + cases: cases.into_iter().map(|(l, b)| (l, specialise(b))).collect(), |
| 54 | + default: Box::new(specialise(*default)), |
| 55 | + }, |
| 56 | + tail => tail, |
| 57 | + }; |
| 58 | + for s in release_drops(stmts).into_iter().rev() { |
| 59 | + e = match s { |
| 60 | + Stmt::Let(v, value) => Expr::Let(v, Box::new(value), Box::new(e)), |
| 61 | + Stmt::Dup(v) => Expr::Dup(v, Box::new(e)), |
| 62 | + Stmt::Drop(v) => Expr::Drop(v, Box::new(e)), |
| 63 | + Stmt::Release { var, fields } => Expr::Release { |
| 64 | + var, |
| 65 | + fields, |
| 66 | + rest: Box::new(e), |
| 67 | + }, |
| 68 | + }; |
| 69 | + } |
| 70 | + e |
| 71 | +} |
| 72 | + |
| 73 | +/// Split `e` into its leading statements and what ends them. |
| 74 | +fn peel(mut e: Expr) -> (Vec<Stmt>, Expr) { |
| 75 | + let mut stmts = Vec::new(); |
| 76 | + loop { |
| 77 | + e = match e { |
| 78 | + Expr::Let(v, value, rest) => { |
| 79 | + stmts.push(Stmt::Let(v, *value)); |
| 80 | + *rest |
| 81 | + } |
| 82 | + Expr::Dup(v, rest) => { |
| 83 | + stmts.push(Stmt::Dup(v)); |
| 84 | + *rest |
| 85 | + } |
| 86 | + Expr::Drop(v, rest) => { |
| 87 | + stmts.push(Stmt::Drop(v)); |
| 88 | + *rest |
| 89 | + } |
| 90 | + tail => return (stmts, tail), |
| 91 | + }; |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +/// Turn each `drop x` preceded by projections of `x` into a release. |
| 96 | +fn release_drops(mut stmts: Vec<Stmt>) -> Vec<Stmt> { |
| 97 | + let mut k = 0; |
| 98 | + while k < stmts.len() { |
| 99 | + if let Stmt::Drop(x) = stmts[k] |
| 100 | + && let Some(at) = release(&mut stmts, k, x) |
| 101 | + { |
| 102 | + k = at; |
| 103 | + } |
| 104 | + k += 1; |
| 105 | + } |
| 106 | + stmts |
| 107 | +} |
| 108 | + |
| 109 | +/// Replace `stmts[k]`, `drop x`, by a release of the fields of `x` |
| 110 | +/// projected in the statements just before it, if there are any. Returns |
| 111 | +/// the index of the last statement rewritten. |
| 112 | +fn release(stmts: &mut Vec<Stmt>, k: usize, x: VarId) -> Option<usize> { |
| 113 | + // The statements before the drop that cannot unwind or consume. |
| 114 | + let mut start = k; |
| 115 | + while start > 0 |
| 116 | + && match &stmts[start - 1] { |
| 117 | + Stmt::Let(_, Value::Field(..)) | Stmt::Dup(_) => true, |
| 118 | + Stmt::Drop(w) => *w != x, |
| 119 | + _ => false, |
| 120 | + } |
| 121 | + { |
| 122 | + start -= 1; |
| 123 | + } |
| 124 | + let mut fields: Vec<(u32, VarId)> = Vec::new(); |
| 125 | + // The `dup` making each such projection owned is dropped: the release |
| 126 | + // gives it its reference. |
| 127 | + let mut removed: BTreeSet<usize> = BTreeSet::new(); |
| 128 | + for p in start..k { |
| 129 | + let Stmt::Let(a, Value::Field(Atom::Var(base), i)) = &stmts[p] else { |
| 130 | + continue; |
| 131 | + }; |
| 132 | + if *base != x { |
| 133 | + continue; |
| 134 | + } |
| 135 | + if fields.iter().any(|(j, _)| j == i) { |
| 136 | + continue; // The same field read twice: the other keeps its dup. |
| 137 | + } |
| 138 | + if let Some(q) = (p + 1..k).find(|q| matches!(stmts[*q], Stmt::Dup(d) if d == *a)) { |
| 139 | + fields.push((*i, *a)); |
| 140 | + removed.insert(q); |
| 141 | + } |
| 142 | + } |
| 143 | + if fields.is_empty() { |
| 144 | + return None; |
| 145 | + } |
| 146 | + // Those projections only own a reference after the release, so their |
| 147 | + // drops before it move after it. |
| 148 | + let moved: Vec<usize> = (start..k) |
| 149 | + .filter(|q| matches!(stmts[*q], Stmt::Drop(d) if fields.iter().any(|(_, a)| *a == d))) |
| 150 | + .collect(); |
| 151 | + removed.extend(&moved); |
| 152 | + let after = stmts.split_off(k + 1); |
| 153 | + stmts.pop(); // `drop x` |
| 154 | + let region = stmts.split_off(start); |
| 155 | + let mut drops = Vec::new(); |
| 156 | + for (q, s) in region.into_iter().enumerate() { |
| 157 | + if moved.contains(&(start + q)) { |
| 158 | + drops.push(s); |
| 159 | + } else if !removed.contains(&(start + q)) { |
| 160 | + stmts.push(s); |
| 161 | + } |
| 162 | + } |
| 163 | + stmts.push(Stmt::Release { var: x, fields }); |
| 164 | + stmts.extend(drops); |
| 165 | + let last = stmts.len() - 1; |
| 166 | + stmts.extend(after); |
| 167 | + Some(last) |
| 168 | +} |