| @@ -20,7 +20,7 @@ |
| 20 | 20 | use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque}; |
| 21 | 21 | use std::rc::Rc; |
| 22 | 22 | |
| 23 | | −use crate::ir::{Atom, Expr, FnDecl, FnId, Program, Span, Value, VarId}; |
| 23 | +use crate::ir::{Atom, Expr, FnId, Program, Span, Value, VarId}; |
| 24 | 24 | |
| 25 | 25 | /// A function of at most this many nodes is inlinable at any site. (The |
| 26 | 26 | /// `std/iter` combinators measure 15–30: the point of them is that they |
| @@ -38,26 +38,51 @@ const MAX_ROUNDS: u32 = 4; |
| 38 | 38 | const MAX_NODES: u32 = 512; |
| 39 | 39 | const MAX_GROWTH: u32 = 4; |
| 40 | 40 | |
| 41 | | −/// Simplify the functions `live` of `program`, to the fixed point. |
| 41 | +/// What inlining may still do to each function, over a whole |
| 42 | +/// optimisation: `opt::run_with` runs the simplifier once per round of |
| 43 | +/// its fixed point and once after, and they share one budget (issue |
| 44 | +/// #225). Each run starting afresh would let a function grow 4× per run. |
| 45 | +#[derive(Default)] |
| 46 | +pub struct Budget { |
| 47 | + /// Nodes inlined into each function so far. |
| 48 | + grown: Vec<u32>, |
| 49 | + /// Each function's cap, set the first time it is simplified (0: not |
| 50 | + /// yet). |
| 51 | + caps: Vec<u32>, |
| 52 | + /// The functions whose body was already inlined as a move (a closure |
| 53 | + /// used once, which may exceed the cap): a second such inlining would |
| 54 | + /// be a copy, so it takes the cap like any other. |
| 55 | + moved: HashSet<FnId>, |
| 56 | +} |
| 57 | + |
| 58 | +/// Simplify the functions `live` of `program`, to the fixed point, with a |
| 59 | +/// budget of their own. |
| 42 | 60 | pub fn run_in(program: &mut Program, live: &[FnId]) { |
| 43 | | − // Growth is bounded over the whole pass, not per round: two functions |
| 44 | | − // inlining into each other would otherwise grow on every round. |
| 45 | | − let mut grown = vec![0u32; program.fns.len()]; |
| 46 | | − let mut caps = vec![0u32; program.fns.len()]; |
| 61 | + run_with_budget(program, live, &mut Budget::default()); |
| 62 | +} |
| 63 | + |
| 64 | +/// [`run_in`], spending `budget`. |
| 65 | +pub fn run_with_budget(program: &mut Program, live: &[FnId], budget: &mut Budget) { |
| 66 | + // Growth is bounded over the whole optimisation, not per round: two |
| 67 | + // functions inlining into each other would otherwise grow on every |
| 68 | + // round. The passes between runs add functions: they start at 0. |
| 69 | + budget.grown.resize(program.fns.len(), 0); |
| 70 | + budget.caps.resize(program.fns.len(), 0); |
| 47 | 71 | for _ in 0..MAX_ROUNDS { |
| 48 | 72 | let candidates = candidates(program); |
| 49 | 73 | let mut changed = false; |
| 50 | 74 | for &id in live { |
| 51 | | − if caps[id as usize] == 0 { |
| 75 | + if budget.caps[id as usize] == 0 { |
| 52 | 76 | let size = size(&program.fns[id as usize].body); |
| 53 | | − caps[id as usize] = MAX_NODES.max(MAX_GROWTH * size); |
| 77 | + budget.caps[id as usize] = MAX_NODES.max(MAX_GROWTH * size); |
| 54 | 78 | } |
| 55 | 79 | changed |= function( |
| 56 | 80 | program, |
| 57 | 81 | id, |
| 58 | 82 | &candidates, |
| 59 | | − &mut grown[id as usize], |
| 60 | | − caps[id as usize], |
| 83 | + &mut budget.grown[id as usize], |
| 84 | + budget.caps[id as usize], |
| 85 | + &mut budget.moved, |
| 61 | 86 | ); |
| 62 | 87 | } |
| 63 | 88 | if !changed { |
| @@ -73,6 +98,7 @@ fn function( |
| 73 | 98 | candidates: &HashMap<FnId, Candidate>, |
| 74 | 99 | grown: &mut u32, |
| 75 | 100 | cap: u32, |
| 101 | + moved: &mut HashSet<FnId>, |
| 76 | 102 | ) -> bool { |
| 77 | 103 | let decl = &mut program.fns[id as usize]; |
| 78 | 104 | let body = std::mem::replace(&mut decl.body, Expr::Return(Atom::Unit)); |
| @@ -88,6 +114,7 @@ fn function( |
| 88 | 114 | inlined: HashSet::from([id]), |
| 89 | 115 | grown, |
| 90 | 116 | cap, |
| 117 | + moved, |
| 91 | 118 | changed: false, |
| 92 | 119 | }; |
| 93 | 120 | let uses = uses(&body); |
| @@ -163,7 +190,7 @@ fn candidates(program: &Program) -> HashMap<FnId, Candidate> { |
| 163 | 190 | .fns |
| 164 | 191 | .iter() |
| 165 | 192 | .enumerate() |
| 166 | | − .filter(|(id, f)| !recursive(&f.body, *id as FnId) && !is_constant_dictionary(f)) |
| 193 | + .filter(|(id, f)| !recursive(&f.body, *id as FnId) && !f.is_constant_dictionary()) |
| 167 | 194 | .map(|(id, f)| { |
| 168 | 195 | ( |
| 169 | 196 | id as FnId, |
| @@ -182,33 +209,6 @@ fn candidates(program: &Program) -> HashMap<FnId, Candidate> { |
| 182 | 209 | .collect() |
| 183 | 210 | } |
| 184 | 211 | |
| 185 | | −/// Whether `f` is a constant dictionary (ADR 0007): a nullary function |
| 186 | | −/// whose body is no-capture closures and then a tuple of them. The C |
| 187 | | −/// backend folds one to an immortal static (see `constant` in |
| 188 | | −/// `louise_codegen_c`), so a call to it is a reference; inlining would |
| 189 | | −/// instead rebuild the closures and the tuple at every call site — a |
| 190 | | −/// 27× regression on a generic call in a loop (issue #195's review). |
| 191 | | −fn is_constant_dictionary(f: &FnDecl) -> bool { |
| 192 | | − if !f.params.is_empty() { |
| 193 | | − return false; |
| 194 | | − } |
| 195 | | − let mut e = &f.body; |
| 196 | | − loop { |
| 197 | | − match e { |
| 198 | | − Expr::Let(_, value, rest, _) => { |
| 199 | | − match &**value { |
| 200 | | − Value::Closure(_, caps) if caps.is_empty() => {} |
| 201 | | − Value::Tuple(args) if !args.is_empty() => {} |
| 202 | | − _ => return false, |
| 203 | | − } |
| 204 | | − e = rest; |
| 205 | | − } |
| 206 | | − Expr::Return(Atom::Var(_)) => return true, |
| 207 | | − _ => return false, |
| 208 | | − } |
| 209 | | − } |
| 210 | | −} |
| 211 | | − |
| 212 | 212 | struct Simplify<'a> { |
| 213 | 213 | candidates: &'a HashMap<FnId, Candidate>, |
| 214 | 214 | /// The function's own module: only same-module callees are inlined |
| @@ -233,9 +233,11 @@ struct Simplify<'a> { |
| 233 | 233 | /// enclosing point of this walk: never inlined again within it, so |
| 234 | 234 | /// mutually recursive pairs terminate. |
| 235 | 235 | inlined: HashSet<FnId>, |
| 236 | | − /// Nodes inlined so far (over the whole pass), and the cap. |
| 236 | + /// Nodes inlined so far (over the whole optimisation), and the cap. |
| 237 | 237 | grown: &'a mut u32, |
| 238 | 238 | cap: u32, |
| 239 | + /// Functions already inlined as a move ([`Budget::moved`]). |
| 240 | + moved: &'a mut HashSet<FnId>, |
| 239 | 241 | changed: bool, |
| 240 | 242 | } |
| 241 | 243 | |
| @@ -285,8 +287,10 @@ impl Simplify<'_> { |
| 285 | 287 | { |
| 286 | 288 | // A closure used exactly once: its function's size no |
| 287 | 289 | // longer matters, inlining moves code instead of copying |
| 288 | | − // it. |
| 289 | | − force = uses.get(f) == Some(&1); |
| 290 | + // it — the first time. The function may be referenced |
| 291 | + // elsewhere too (an inlined caller copied the closure), |
| 292 | + // so a second time is a copy, under the cap (issue #225). |
| 293 | + force = uses.get(f) == Some(&1) && !self.moved.contains(&g); |
| 290 | 294 | let mut all = caps; |
| 291 | 295 | all.extend(args.iter().cloned()); |
| 292 | 296 | value = Value::CallFn(g, all); |
| @@ -319,6 +323,9 @@ impl Simplify<'_> { |
| 319 | 323 | (chain && cand.nodes <= SMALL_NODES && within) || tail |
| 320 | 324 | }; |
| 321 | 325 | if ok { |
| 326 | + if force { |
| 327 | + self.moved.insert(*g); |
| 328 | + } |
| 322 | 329 | *self.grown += cand.nodes; |
| 323 | 330 | self.inlined.insert(*g); |
| 324 | 331 | let (mut pasted, pend) = self.inline(&cand, args); |