| @@ -17,7 +17,10 @@ |
| 17 | 17 | //! The subset a worker covers is wide: arithmetic and comparisons; |
| 18 | 18 | //! control flow; self-recursion (raw); any other call (a raw argument |
| 19 | 19 | //! is boxed at the call, and the call's boxed result is unboxed where |
| 20 | | −//! it's used as one); values escaping into data (boxed at the escape). |
| 20 | +//! it's used as one — or stays raw end to end when the callee has a |
| 21 | +//! float worker of its own and the result never escapes: the planner |
| 22 | +//! walks callees first and records escapes); values escaping into data |
| 23 | +//! (boxed at the escape). |
| 21 | 24 | //! Refused: a cross-function *tail* call (`musttail` needs the uniform |
| 22 | 25 | //! shape), an early `exit` (v1), a reuse token or `switch` on a raw |
| 23 | 26 | //! value, and non-finite float literals (v1). Closures, the runtime and |
| @@ -117,7 +120,9 @@ pub(crate) fn plan( |
| 117 | 120 | // (computed only when some function could get an int worker). |
| 118 | 121 | let mut pure = None; |
| 119 | 122 | let mut out = BTreeMap::new(); |
| 120 | | − for &f in reachable { |
| 123 | + // Callees first: a caller's walk sees the workers of the functions |
| 124 | + // it calls, so a call's result can stay raw across them. |
| 125 | + for f in callees_first(program, reachable) { |
| 121 | 126 | let decl = &program.fns[f as usize]; |
| 122 | 127 | let Some(sig) = &decl.unbox_sig else { continue }; |
| 123 | 128 | if decl.cpr_arity.is_some() { |
| @@ -161,8 +166,19 @@ pub(crate) fn plan( |
| 161 | 166 | bail: false, |
| 162 | 167 | ranges, |
| 163 | 168 | pure: pure.as_ref(), |
| 169 | + planned: &out, |
| 170 | + escaped: BTreeSet::new(), |
| 171 | + pending: Vec::new(), |
| 164 | 172 | }; |
| 165 | 173 | w.expr(&decl.body, true); |
| 174 | + // A cross-worker call's result stays raw only if the variable |
| 175 | + // never escapes: an escape boxes at the boundary, so a result |
| 176 | + // that only escapes is boxed at the call instead, as before. |
| 177 | + for v in std::mem::take(&mut w.pending) { |
| 178 | + if !w.escaped.contains(&v) { |
| 179 | + w.raw.insert(v); |
| 180 | + } |
| 181 | + } |
| 166 | 182 | if !w.bail { |
| 167 | 183 | out.insert( |
| 168 | 184 | f, |
| @@ -176,6 +192,73 @@ pub(crate) fn plan( |
| 176 | 192 | out |
| 177 | 193 | } |
| 178 | 194 | |
| 195 | +/// `reachable` with direct callees before their callers (a post-order of |
| 196 | +/// the direct calls; a recursive group's members in some deterministic |
| 197 | +/// order), so a function's walk sees the workers of the functions it |
| 198 | +/// calls. |
| 199 | +fn callees_first(program: &Program, reachable: &BTreeSet<FnId>) -> Vec<FnId> { |
| 200 | + let mut order = Vec::new(); |
| 201 | + let mut seen = BTreeSet::new(); |
| 202 | + for &root in reachable { |
| 203 | + let mut stack = vec![(root, false)]; |
| 204 | + while let Some((f, done)) = stack.pop() { |
| 205 | + if done { |
| 206 | + order.push(f); |
| 207 | + continue; |
| 208 | + } |
| 209 | + if !reachable.contains(&f) || !seen.insert(f) { |
| 210 | + continue; |
| 211 | + } |
| 212 | + stack.push((f, true)); |
| 213 | + visit_calls(&program.fns[f as usize].body, &mut |g| { |
| 214 | + if !seen.contains(&g) { |
| 215 | + stack.push((g, false)); |
| 216 | + } |
| 217 | + }); |
| 218 | + } |
| 219 | + } |
| 220 | + order |
| 221 | +} |
| 222 | + |
| 223 | +/// Call `f` on every direct call's callee in `e`, nested blocks included. |
| 224 | +fn visit_calls(e: &Expr, f: &mut impl FnMut(FnId)) { |
| 225 | + match e { |
| 226 | + Expr::Let(_, value, rest, _) => { |
| 227 | + if let Value::CallFn(g, _) = &**value { |
| 228 | + f(*g); |
| 229 | + } |
| 230 | + if let Value::Block(inner) = &**value { |
| 231 | + visit_calls(inner, f); |
| 232 | + } |
| 233 | + visit_calls(rest, f); |
| 234 | + } |
| 235 | + Expr::LetN(_, value, rest, _) => { |
| 236 | + if let Value::CallFn(g, _) = &**value { |
| 237 | + f(*g); |
| 238 | + } |
| 239 | + visit_calls(rest, f); |
| 240 | + } |
| 241 | + Expr::Switch { cases, default, .. } => { |
| 242 | + for (_, b) in cases { |
| 243 | + visit_calls(b, f); |
| 244 | + } |
| 245 | + if let Some(d) = default { |
| 246 | + visit_calls(d, f); |
| 247 | + } |
| 248 | + } |
| 249 | + Expr::SwitchLit { cases, default, .. } => { |
| 250 | + for (_, b) in cases { |
| 251 | + visit_calls(b, f); |
| 252 | + } |
| 253 | + visit_calls(default, f); |
| 254 | + } |
| 255 | + Expr::Dup(_, rest) | Expr::Drop(_, rest) | Expr::Release { rest, .. } => { |
| 256 | + visit_calls(rest, f) |
| 257 | + } |
| 258 | + Expr::Return(_) | Expr::Exit(_) | Expr::Panic(_) | Expr::ReturnN(_) => {} |
| 259 | + } |
| 260 | +} |
| 261 | + |
| 179 | 262 | /// The per-function walk: the set of locals that are raw, or a bail (no |
| 180 | 263 | /// worker for this function). |
| 181 | 264 | struct Walker<'p, 'r> { |
| @@ -191,6 +274,15 @@ struct Walker<'p, 'r> { |
| 191 | 274 | /// The re-runnable functions (the int workers' gate); set only when |
| 192 | 275 | /// walking an int signature. |
| 193 | 276 | pure: Option<&'r std::collections::HashSet<FnId>>, |
| 277 | + /// The workers planned so far (callees first): a call of one can |
| 278 | + /// keep its result raw. |
| 279 | + planned: &'p BTreeMap<FnId, WorkerPlan>, |
| 280 | + /// Variables flowing to a box position somewhere in the body, |
| 281 | + /// recorded by [`Walker::escape`]. |
| 282 | + escaped: BTreeSet<VarId>, |
| 283 | + /// `let v = g(…)` where `g` has a float worker: `v` can stay raw, |
| 284 | + /// decided after the walk, when `escaped` is complete. |
| 285 | + pending: Vec<VarId>, |
| 194 | 286 | } |
| 195 | 287 | |
| 196 | 288 | impl Walker<'_, '_> { |
| @@ -218,6 +310,26 @@ impl Walker<'_, '_> { |
| 218 | 310 | } |
| 219 | 311 | if self.value(value, is_tail) { |
| 220 | 312 | self.raw.insert(*v); |
| 313 | + } else if let Value::CallFn(g, _) = &**value { |
| 314 | + // A float worker's call of another function with |
| 315 | + // a float worker: the result can stay raw (no |
| 316 | + // box at the call, no unbox at the use) when the |
| 317 | + // variable never escapes — decided after the |
| 318 | + // walk. Int workers bail, and a bailed |
| 319 | + // recompute's box is not proven small, so int |
| 320 | + // calls stay boxed (a follow-up could use the |
| 321 | + // callee's range). Tail calls keep their refusal |
| 322 | + // (`musttail` needs the uniform shape). |
| 323 | + if !is_tail |
| 324 | + && *g != self.f |
| 325 | + && self.kind == UnboxedKind::Float |
| 326 | + && self |
| 327 | + .planned |
| 328 | + .get(g) |
| 329 | + .is_some_and(|p| p.kind == UnboxedKind::Float) |
| 330 | + { |
| 331 | + self.pending.push(*v); |
| 332 | + } |
| 221 | 333 | } |
| 222 | 334 | e = rest; |
| 223 | 335 | } |
| @@ -480,9 +592,16 @@ impl Walker<'_, '_> { |
| 480 | 592 | } |
| 481 | 593 | } |
| 482 | 594 | |
| 483 | | − /// An atom flowing somewhere that needs a box: nothing to record — |
| 484 | | − /// the emitter boxes a raw local at the boundary. |
| 485 | | − fn escape(&mut self, _a: &Atom) {} |
| 595 | + /// An atom flowing somewhere that needs a box: record the variable. |
| 596 | + /// The emitter boxes a raw local at the boundary; the record lets the |
| 597 | + /// walk keep a cross-worker call's result raw only when the variable |
| 598 | + /// never escapes (one that does is boxed once, at the call, exactly |
| 599 | + /// as before this analysis). |
| 600 | + fn escape(&mut self, a: &Atom) { |
| 601 | + if let Atom::Var(v) = a { |
| 602 | + self.escaped.insert(*v); |
| 603 | + } |
| 604 | + } |
| 486 | 605 | |
| 487 | 606 | /// Whether the atom is (or becomes) raw: a raw local or a literal of |
| 488 | 607 | /// the worker's kind. |
| @@ -915,4 +1034,103 @@ mod tests { |
| 915 | 1034 | "the worker's own result must be raw or proven small" |
| 916 | 1035 | ); |
| 917 | 1036 | } |
| 1037 | + |
| 1038 | + /// f(v0) = let v1 = g(v0); let v2 = op(v1, …); v2 — a non-tail call |
| 1039 | + /// whose result is used raw. |
| 1040 | + fn f_calls_g(kind: UnboxedKind, op: &str, operand: Atom) -> FnDecl { |
| 1041 | + FnDecl { |
| 1042 | + name: "f".to_owned(), |
| 1043 | + params: vec![0], |
| 1044 | + body: Expr::Let( |
| 1045 | + 1, |
| 1046 | + Box::new(call(1, vec![Atom::Var(0)])), |
| 1047 | + Box::new(Expr::Let( |
| 1048 | + 2, |
| 1049 | + Box::new(Value::CallExtern( |
| 1050 | + op.to_owned(), |
| 1051 | + vec![Atom::Var(1), operand], |
| 1052 | + )), |
| 1053 | + Box::new(Expr::Return(Atom::Var(2))), |
| 1054 | + None, |
| 1055 | + )), |
| 1056 | + None, |
| 1057 | + ), |
| 1058 | + n_vars: 3, |
| 1059 | + borrowed: Default::default(), |
| 1060 | + span: None, |
| 1061 | + module: None, |
| 1062 | + shapes: Default::default(), |
| 1063 | + fip: false, |
| 1064 | + cpr_arity: None, |
| 1065 | + unbox_sig: Some(UnboxedSig { |
| 1066 | + kind, |
| 1067 | + params: vec![true], |
| 1068 | + }), |
| 1069 | + } |
| 1070 | + } |
| 1071 | + |
| 1072 | + #[test] |
| 1073 | + fn a_cross_worker_float_call_keeps_its_result_raw() { |
| 1074 | + // f(v0) = let v1 = g(v0); v1 + 3.0: g is planned first (callees |
| 1075 | + // first — f's id is the smaller), and v1 never escapes, so the |
| 1076 | + // call's result stays raw. |
| 1077 | + let p = program(vec![ |
| 1078 | + f_calls_g(UnboxedKind::Float, "tl_float_add", lit(3.0)), |
| 1079 | + f("g", float_sig(vec![true]), Value::Atom(Atom::Var(0))), |
| 1080 | + ]); |
| 1081 | + let plans = worker_plans(&p, &[0, 1]); |
| 1082 | + let raw = &plans[&0].raw; |
| 1083 | + assert!(raw.contains(&1), "the call's result is raw: {raw:?}"); |
| 1084 | + assert!(raw.contains(&2), "the sum is raw: {raw:?}"); |
| 1085 | + } |
| 1086 | + |
| 1087 | + #[test] |
| 1088 | + fn an_escaping_cross_worker_result_stays_boxed() { |
| 1089 | + // f(v0) = let v1 = g(v0); let t = (v1,); v1 + 1.0: v1 escapes |
| 1090 | + // into the tuple, so it is boxed at the call as before (the sum |
| 1091 | + // still reads it raw in place). |
| 1092 | + let mut decl = f_calls_g(UnboxedKind::Float, "tl_float_add", lit(1.0)); |
| 1093 | + decl.body = Expr::Let( |
| 1094 | + 1, |
| 1095 | + Box::new(call(1, vec![Atom::Var(0)])), |
| 1096 | + Box::new(Expr::Let( |
| 1097 | + 2, |
| 1098 | + Box::new(Value::Tuple(vec![Atom::Var(1)])), |
| 1099 | + Box::new(Expr::Let( |
| 1100 | + 3, |
| 1101 | + Box::new(Value::CallExtern( |
| 1102 | + "tl_float_add".to_owned(), |
| 1103 | + vec![Atom::Var(1), lit(1.0)], |
| 1104 | + )), |
| 1105 | + Box::new(Expr::Return(Atom::Var(3))), |
| 1106 | + None, |
| 1107 | + )), |
| 1108 | + None, |
| 1109 | + )), |
| 1110 | + None, |
| 1111 | + ); |
| 1112 | + decl.n_vars = 4; |
| 1113 | + let p = program(vec![ |
| 1114 | + decl, |
| 1115 | + f("g", float_sig(vec![true]), Value::Atom(Atom::Var(0))), |
| 1116 | + ]); |
| 1117 | + let plans = worker_plans(&p, &[0, 1]); |
| 1118 | + let raw = &plans[&0].raw; |
| 1119 | + assert!(!raw.contains(&1), "escapes: boxed at the call: {raw:?}"); |
| 1120 | + assert!(raw.contains(&3), "the sum is raw: {raw:?}"); |
| 1121 | + } |
| 1122 | + |
| 1123 | + #[test] |
| 1124 | + fn a_cross_worker_int_call_stays_boxed() { |
| 1125 | + // f(v0) = let v1 = g(v0); v1 + 1: g has an int worker, but a |
| 1126 | + // bailed recompute's box is not proven small — v1 stays boxed, |
| 1127 | + // and the unproven operand refuses the worker, as before. |
| 1128 | + let p = program(vec![ |
| 1129 | + f_calls_g(UnboxedKind::Int, "tl_int_add", int(1)), |
| 1130 | + f("g", int_sig(vec![true]), Value::Atom(Atom::Var(0))), |
| 1131 | + ]); |
| 1132 | + let plans = worker_plans(&p, &[0, 1]); |
| 1133 | + assert!(plans.contains_key(&1)); |
| 1134 | + assert!(!plans.contains_key(&0), "unproven boxed operand bails"); |
| 1135 | + } |
| 918 | 1136 | } |