| @@ -1,4 +1,4 @@ |
| 1 | | −//! Drop specialisation, after dup/drop insertion. |
| 1 | +//! Drop specialisation and reuse (FBIP), after dup/drop insertion. |
| 2 | 2 | //! |
| 3 | 3 | //! Matching on a value `x` and then dropping it is, after insertion: |
| 4 | 4 | //! |
| @@ -11,6 +11,15 @@ |
| 11 | 11 | //! (no count changes on the fields); otherwise they are dup'd and `x` is |
| 12 | 12 | //! dropped, as before. |
| 13 | 13 | //! |
| 14 | +//! **Reuse:** if `x` was matched by a `switch` (it is a constructor cell) |
| 15 | +//! and a constructor or tuple at least as large as the fields read is |
| 16 | +//! built after the release, the release keeps a unique cell as a *token* |
| 17 | +//! (`release ... reuse as t`) and that construction becomes `reuse t in |
| 18 | +//! C(...)`: it writes the cell in place when the sizes match. Every path |
| 19 | +//! after the release consumes the token once: the first suitable |
| 20 | +//! construction on the path reuses it, and a branch without one drops it |
| 21 | +//! on entry. |
| 22 | +//! |
| 14 | 23 | //! The specialisation only looks at the statements just before the drop |
| 15 | 24 | //! (projections, `dup`s and other `drop`s, which cannot unwind), so it |
| 16 | 25 | //! never moves code across a call. |
| @@ -27,44 +36,94 @@ enum Stmt { |
| 27 | 36 | Release { |
| 28 | 37 | var: VarId, |
| 29 | 38 | fields: Vec<(u32, VarId)>, |
| 39 | + /// Every cell `var` can be has at least this many fields. |
| 40 | + min_fields: usize, |
| 30 | 41 | }, |
| 31 | 42 | } |
| 32 | 43 | |
| 33 | | −/// Specialise the drops of `e`. |
| 34 | | −pub(crate) fn specialise(e: Expr) -> Expr { |
| 44 | +type Set = BTreeSet<VarId>; |
| 45 | + |
| 46 | +/// Specialise the drops of `e` and reuse cells; new variables (tokens) |
| 47 | +/// are numbered from `n_vars`, which is updated. |
| 48 | +pub(crate) fn specialise(e: Expr, n_vars: &mut u32) -> Expr { |
| 49 | + specialise_in(e, n_vars, &Set::new()) |
| 50 | +} |
| 51 | + |
| 52 | +/// [`specialise`], where `matched` are the variables a `switch` around `e` |
| 53 | +/// inspects: constructor cells, whose cells can be reused. (A tuple, or a |
| 54 | +/// dictionary, is projected without a `switch`; a constant dictionary is |
| 55 | +/// immortal, never unique.) |
| 56 | +fn specialise_in(e: Expr, n_vars: &mut u32, matched: &Set) -> Expr { |
| 35 | 57 | let (stmts, tail) = peel(e); |
| 36 | 58 | let stmts: Vec<Stmt> = stmts |
| 37 | 59 | .into_iter() |
| 38 | 60 | .map(|s| match s { |
| 39 | | − Stmt::Let(v, Value::Block(inner)) => { |
| 40 | | − Stmt::Let(v, Value::Block(Box::new(specialise(*inner)))) |
| 41 | | − } |
| 61 | + Stmt::Let(v, Value::Block(inner)) => Stmt::Let( |
| 62 | + v, |
| 63 | + Value::Block(Box::new(specialise_in(*inner, n_vars, matched))), |
| 64 | + ), |
| 42 | 65 | s => s, |
| 43 | 66 | }) |
| 44 | 67 | .collect(); |
| 45 | 68 | 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 | | − }, |
| 69 | + Expr::Switch { on, cases, default } => { |
| 70 | + let mut matched = matched.clone(); |
| 71 | + if let Atom::Var(x) = on { |
| 72 | + matched.insert(x); |
| 73 | + } |
| 74 | + Expr::Switch { |
| 75 | + on, |
| 76 | + cases: cases |
| 77 | + .into_iter() |
| 78 | + .map(|(t, b)| (t, specialise_in(b, n_vars, &matched))) |
| 79 | + .collect(), |
| 80 | + default: default.map(|d| Box::new(specialise_in(*d, n_vars, &matched))), |
| 81 | + } |
| 82 | + } |
| 51 | 83 | Expr::SwitchLit { on, cases, default } => Expr::SwitchLit { |
| 52 | 84 | on, |
| 53 | | − cases: cases.into_iter().map(|(l, b)| (l, specialise(b))).collect(), |
| 54 | | − default: Box::new(specialise(*default)), |
| 85 | + cases: cases |
| 86 | + .into_iter() |
| 87 | + .map(|(l, b)| (l, specialise_in(b, n_vars, matched))) |
| 88 | + .collect(), |
| 89 | + default: Box::new(specialise_in(*default, n_vars, matched)), |
| 55 | 90 | }, |
| 56 | 91 | tail => tail, |
| 57 | 92 | }; |
| 93 | + // Rebuild from the end, so the rest of a release is final when it |
| 94 | + // looks for a construction to reuse its cell. |
| 58 | 95 | for s in release_drops(stmts).into_iter().rev() { |
| 59 | 96 | e = match s { |
| 60 | 97 | Stmt::Let(v, value) => Expr::Let(v, Box::new(value), Box::new(e)), |
| 61 | 98 | Stmt::Dup(v) => Expr::Dup(v, Box::new(e)), |
| 62 | 99 | Stmt::Drop(v) => Expr::Drop(v, Box::new(e)), |
| 63 | | − Stmt::Release { var, fields } => Expr::Release { |
| 100 | + Stmt::Release { |
| 64 | 101 | var, |
| 65 | 102 | fields, |
| 66 | | − rest: Box::new(e), |
| 67 | | − }, |
| 103 | + min_fields, |
| 104 | + } => { |
| 105 | + let reusable = matched.contains(&var) && has_candidate(&e, min_fields); |
| 106 | + let token = reusable.then(|| { |
| 107 | + let t = *n_vars; |
| 108 | + *n_vars += 1; |
| 109 | + e = place( |
| 110 | + std::mem::replace(&mut e, Expr::Return(Atom::Unit)), |
| 111 | + t, |
| 112 | + min_fields, |
| 113 | + ); |
| 114 | + t |
| 115 | + }); |
| 116 | + if fields.is_empty() && token.is_none() { |
| 117 | + Expr::Drop(var, Box::new(e)) |
| 118 | + } else { |
| 119 | + Expr::Release { |
| 120 | + var, |
| 121 | + fields, |
| 122 | + token, |
| 123 | + rest: Box::new(e), |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 68 | 127 | }; |
| 69 | 128 | } |
| 70 | 129 | e |
| @@ -121,6 +180,7 @@ fn release(stmts: &mut Vec<Stmt>, k: usize, x: VarId) -> Option<usize> { |
| 121 | 180 | { |
| 122 | 181 | start -= 1; |
| 123 | 182 | } |
| 183 | + let mut min_fields = 0; |
| 124 | 184 | let mut fields: Vec<(u32, VarId)> = Vec::new(); |
| 125 | 185 | // The `dup` making each such projection owned is dropped: the release |
| 126 | 186 | // gives it its reference. |
| @@ -132,6 +192,7 @@ fn release(stmts: &mut Vec<Stmt>, k: usize, x: VarId) -> Option<usize> { |
| 132 | 192 | if *base != x { |
| 133 | 193 | continue; |
| 134 | 194 | } |
| 195 | + min_fields = min_fields.max(*i as usize + 1); |
| 135 | 196 | if fields.iter().any(|(j, _)| j == i) { |
| 136 | 197 | continue; // The same field read twice: the other keeps its dup. |
| 137 | 198 | } |
| @@ -140,8 +201,8 @@ fn release(stmts: &mut Vec<Stmt>, k: usize, x: VarId) -> Option<usize> { |
| 140 | 201 | removed.insert(q); |
| 141 | 202 | } |
| 142 | 203 | } |
| 143 | | − if fields.is_empty() { |
| 144 | | − return None; |
| 204 | + if min_fields == 0 { |
| 205 | + return None; // Not a matched value: nothing to specialise. |
| 145 | 206 | } |
| 146 | 207 | // Those projections only own a reference after the release, so their |
| 147 | 208 | // drops before it move after it. |
| @@ -160,9 +221,99 @@ fn release(stmts: &mut Vec<Stmt>, k: usize, x: VarId) -> Option<usize> { |
| 160 | 221 | stmts.push(s); |
| 161 | 222 | } |
| 162 | 223 | } |
| 163 | | − stmts.push(Stmt::Release { var: x, fields }); |
| 224 | + stmts.push(Stmt::Release { |
| 225 | + var: x, |
| 226 | + fields, |
| 227 | + min_fields, |
| 228 | + }); |
| 164 | 229 | stmts.extend(drops); |
| 165 | 230 | let last = stmts.len() - 1; |
| 166 | 231 | stmts.extend(after); |
| 167 | 232 | Some(last) |
| 168 | 233 | } |
| 234 | + |
| 235 | +/// Whether a construction with at least `min_fields` fields is built |
| 236 | +/// somewhere in `e`. |
| 237 | +fn has_candidate(e: &Expr, min_fields: usize) -> bool { |
| 238 | + match e { |
| 239 | + Expr::Let(_, value, rest) => { |
| 240 | + fits(value, min_fields) |
| 241 | + || matches!(&**value, Value::Block(inner) if has_candidate(inner, min_fields)) |
| 242 | + || has_candidate(rest, min_fields) |
| 243 | + } |
| 244 | + Expr::Dup(_, rest) | Expr::Drop(_, rest) | Expr::Release { rest, .. } => { |
| 245 | + has_candidate(rest, min_fields) |
| 246 | + } |
| 247 | + Expr::Switch { cases, default, .. } => { |
| 248 | + cases.iter().any(|(_, b)| has_candidate(b, min_fields)) |
| 249 | + || default |
| 250 | + .as_ref() |
| 251 | + .is_some_and(|d| has_candidate(d, min_fields)) |
| 252 | + } |
| 253 | + Expr::SwitchLit { cases, default, .. } => { |
| 254 | + cases.iter().any(|(_, b)| has_candidate(b, min_fields)) |
| 255 | + || has_candidate(default, min_fields) |
| 256 | + } |
| 257 | + Expr::Return(_) | Expr::Panic(_) => false, |
| 258 | + } |
| 259 | +} |
| 260 | + |
| 261 | +fn fits(value: &Value, min_fields: usize) -> bool { |
| 262 | + matches!(value, Value::Construct { args, .. } | Value::Tuple(args) |
| 263 | + if !args.is_empty() && args.len() >= min_fields) |
| 264 | +} |
| 265 | + |
| 266 | +/// Make the first suitable construction on each path of `e` reuse the |
| 267 | +/// token `t`, and drop `t` on entry to the branches without one. `e` must |
| 268 | +/// have one ([`has_candidate`]). |
| 269 | +fn place(e: Expr, t: VarId, min_fields: usize) -> Expr { |
| 270 | + let branch = |b: Expr| { |
| 271 | + if has_candidate(&b, min_fields) { |
| 272 | + place(b, t, min_fields) |
| 273 | + } else { |
| 274 | + Expr::Drop(t, Box::new(b)) |
| 275 | + } |
| 276 | + }; |
| 277 | + match e { |
| 278 | + Expr::Let(v, value, rest) => match *value { |
| 279 | + value if fits(&value, min_fields) => Expr::Let( |
| 280 | + v, |
| 281 | + Box::new(Value::Reuse { |
| 282 | + token: t, |
| 283 | + value: Box::new(value), |
| 284 | + }), |
| 285 | + rest, |
| 286 | + ), |
| 287 | + Value::Block(inner) if has_candidate(&inner, min_fields) => Expr::Let( |
| 288 | + v, |
| 289 | + Box::new(Value::Block(Box::new(place(*inner, t, min_fields)))), |
| 290 | + rest, |
| 291 | + ), |
| 292 | + value => Expr::Let(v, Box::new(value), Box::new(place(*rest, t, min_fields))), |
| 293 | + }, |
| 294 | + Expr::Dup(v, rest) => Expr::Dup(v, Box::new(place(*rest, t, min_fields))), |
| 295 | + Expr::Drop(v, rest) => Expr::Drop(v, Box::new(place(*rest, t, min_fields))), |
| 296 | + Expr::Release { |
| 297 | + var, |
| 298 | + fields, |
| 299 | + token, |
| 300 | + rest, |
| 301 | + } => Expr::Release { |
| 302 | + var, |
| 303 | + fields, |
| 304 | + token, |
| 305 | + rest: Box::new(place(*rest, t, min_fields)), |
| 306 | + }, |
| 307 | + Expr::Switch { on, cases, default } => Expr::Switch { |
| 308 | + on, |
| 309 | + cases: cases.into_iter().map(|(c, b)| (c, branch(b))).collect(), |
| 310 | + default: default.map(|d| Box::new(branch(*d))), |
| 311 | + }, |
| 312 | + Expr::SwitchLit { on, cases, default } => Expr::SwitchLit { |
| 313 | + on, |
| 314 | + cases: cases.into_iter().map(|(l, b)| (l, branch(b))).collect(), |
| 315 | + default: Box::new(branch(*default)), |
| 316 | + }, |
| 317 | + Expr::Return(_) | Expr::Panic(_) => unreachable!("no construction to reuse"), |
| 318 | + } |
| 319 | +} |