Skip to main content

aver/ir/
alloc_info.rs

1//! Per-fn heap-allocation analysis.
2//!
3//! Detects which user functions never allocate on the heap when executed
4//! under a backend-specific [`AllocPolicy`]. Backends use the resulting
5//! flag to skip GC-related framing (boundary heap_ptr save / rt_truncate
6//! call on the WASM side, GC pressure tracking on the VM side) for
7//! functions that are guaranteed not to produce garbage.
8//!
9//! "Allocates" is policy-driven: a `Result.Ok(Int)` value, for example,
10//! allocates a heap wrapper under the WASM ABI but stays inline in the
11//! VM's NaN-boxed representation. Each backend supplies its own
12//! [`AllocPolicy`]; the shared analysis below walks expression trees and
13//! reaches a fixpoint over the call graph against that policy.
14//!
15//! A function is marked allocating if it
16//! - has a non-empty `effects` declaration (effects routinely heap-marshal
17//!   their arguments — conservative default),
18//! - directly allocates via a literal shape (`List`, `Tuple`, `MapLiteral`,
19//!   `RecordCreate`, `RecordUpdate`, `InterpolatedStr`, `IndependentProduct`),
20//! - calls a built-in or constructor whose policy says it allocates, or
21//! - calls a user fn already classified as allocating.
22//!
23//! Pure-no-alloc fns end up with `false`.
24
25use std::collections::HashMap;
26
27use crate::ast::{Expr, FnBody, FnDef, Stmt, StrPart};
28
29use super::calls::{expr_to_dotted_name, is_builtin_namespace};
30
31/// Backend-specific allocation policy.
32///
33/// Different backends treat builtins and ADT constructors differently
34/// (NaN-boxing vs heap wrappers vs Rust enum variants), so the per-fn
35/// classification is parameterised by a policy object.
36pub trait AllocPolicy {
37    /// Whether a call to a builtin (e.g. `List.prepend`, `Int.toString`)
38    /// allocates a heap object on this backend.
39    fn builtin_allocates(&self, name: &str) -> bool;
40
41    /// Whether a constructor application (e.g. `Result.Ok(x)`,
42    /// `Shape.Circle(r)`) allocates. `has_payload` is true when the
43    /// constructor was applied to at least one argument; nullary
44    /// constructors (`Option.None`) never allocate.
45    fn constructor_allocates(&self, name: &str, has_payload: bool) -> bool;
46}
47
48/// Walks an expression. Returns true if any sub-expression allocates
49/// directly (via shape) or transitively (via a call to a fn already
50/// classified as allocating in `user_allocates`).
51fn expr_allocates<P: AllocPolicy>(
52    expr: &Expr,
53    user_allocates: &HashMap<String, bool>,
54    policy: &P,
55) -> bool {
56    match expr {
57        Expr::Literal(_) | Expr::Ident(_) | Expr::Resolved { .. } => false,
58        Expr::Constructor(_, None) => false,
59
60        // Direct-allocation shapes — the syntactic shape itself constructs
61        // a heap object regardless of children.
62        Expr::List(_)
63        | Expr::Tuple(_)
64        | Expr::MapLiteral(_)
65        | Expr::RecordCreate { .. }
66        | Expr::RecordUpdate { .. }
67        | Expr::IndependentProduct(_, _) => true,
68        Expr::InterpolatedStr(parts) => {
69            // An interpolated string with no `Parsed` parts is a static
70            // string literal — same as `Literal(Str)`, no allocation.
71            // Anything else builds a fresh string at runtime.
72            parts.iter().any(|p| matches!(p, StrPart::Parsed(_)))
73                || expr_children_allocate(expr, user_allocates, policy)
74        }
75        Expr::Constructor(name, Some(payload)) => {
76            policy.constructor_allocates(name, true)
77                || expr_allocates(&payload.node, user_allocates, policy)
78        }
79
80        // Calls — dispatch on builtin vs user.
81        Expr::FnCall(callee, args) => {
82            if let Some(name) = expr_to_dotted_name(&callee.node) {
83                let ns = name.split('.').next().unwrap_or("");
84                if is_builtin_namespace(ns) {
85                    if policy.builtin_allocates(&name) {
86                        return true;
87                    }
88                } else if let Some(&true) = user_allocates.get(&name) {
89                    return true;
90                }
91            }
92            args.iter()
93                .any(|a| expr_allocates(&a.node, user_allocates, policy))
94        }
95        Expr::TailCall(data) => {
96            if let Some(&true) = user_allocates.get(&data.target) {
97                return true;
98            }
99            data.args
100                .iter()
101                .any(|a| expr_allocates(&a.node, user_allocates, policy))
102        }
103
104        // Recurse-only nodes.
105        Expr::Attr(base, _) | Expr::ErrorProp(base) => {
106            expr_allocates(&base.node, user_allocates, policy)
107        }
108        Expr::BinOp(_, l, r) => {
109            expr_allocates(&l.node, user_allocates, policy)
110                || expr_allocates(&r.node, user_allocates, policy)
111        }
112        Expr::Match { subject, arms } => {
113            expr_allocates(&subject.node, user_allocates, policy)
114                || arms
115                    .iter()
116                    .any(|a| expr_allocates(&a.body.node, user_allocates, policy))
117        }
118    }
119}
120
121/// Helper for `InterpolatedStr` recursion into parsed parts.
122fn expr_children_allocate<P: AllocPolicy>(
123    expr: &Expr,
124    user_allocates: &HashMap<String, bool>,
125    policy: &P,
126) -> bool {
127    if let Expr::InterpolatedStr(parts) = expr {
128        return parts.iter().any(|p| match p {
129            StrPart::Literal(_) => false,
130            StrPart::Parsed(e) => expr_allocates(&e.node, user_allocates, policy),
131        });
132    }
133    false
134}
135
136/// Walks a function body.
137fn body_allocates<P: AllocPolicy>(
138    body: &FnBody,
139    user_allocates: &HashMap<String, bool>,
140    policy: &P,
141) -> bool {
142    body.stmts().iter().any(|s| match s {
143        Stmt::Binding(_, _, e) | Stmt::Expr(e) => expr_allocates(&e.node, user_allocates, policy),
144    })
145}
146
147/// Count the number of *directly* allocating expression nodes in a fn
148/// body under `policy`. Distinct from `compute_alloc_info` (which is a
149/// transitive yes/no via the call graph): this is a per-fn IR-level
150/// metric. Children of an allocating node are recursed into, so a
151/// `List([List([])])` counts as 2 sites (outer + inner). Calls to user
152/// fns don't count — that user fn carries its own count.
153///
154/// Drives `aver bench`'s `compiler_visible_allocs` field — a backend-
155/// stable regression metric ("did this hot fn get more alloc sites than
156/// last release?").
157pub fn count_alloc_sites_in_fn<P: AllocPolicy>(fd: &FnDef, policy: &P) -> usize {
158    let FnBody::Block(stmts) = fd.body.as_ref();
159    let mut acc = 0;
160    for stmt in stmts {
161        match stmt {
162            Stmt::Binding(_, _, e) | Stmt::Expr(e) => {
163                count_expr_alloc_sites(&e.node, policy, &mut acc)
164            }
165        }
166    }
167    acc
168}
169
170fn count_expr_alloc_sites<P: AllocPolicy>(expr: &Expr, policy: &P, acc: &mut usize) {
171    match expr {
172        Expr::Literal(_) | Expr::Ident(_) | Expr::Resolved { .. } => {}
173        Expr::Constructor(_, None) => {}
174
175        Expr::List(items) | Expr::Tuple(items) | Expr::IndependentProduct(items, _) => {
176            *acc += 1;
177            for item in items {
178                count_expr_alloc_sites(&item.node, policy, acc);
179            }
180        }
181        Expr::MapLiteral(entries) => {
182            *acc += 1;
183            for (k, v) in entries {
184                count_expr_alloc_sites(&k.node, policy, acc);
185                count_expr_alloc_sites(&v.node, policy, acc);
186            }
187        }
188        Expr::RecordCreate { fields, .. } => {
189            *acc += 1;
190            for (_, v) in fields {
191                count_expr_alloc_sites(&v.node, policy, acc);
192            }
193        }
194        Expr::RecordUpdate { base, updates, .. } => {
195            *acc += 1;
196            count_expr_alloc_sites(&base.node, policy, acc);
197            for (_, v) in updates {
198                count_expr_alloc_sites(&v.node, policy, acc);
199            }
200        }
201        Expr::InterpolatedStr(parts) => {
202            // Treated as a single allocation site when it has any parsed
203            // parts (the lowering pass should have rewritten it before
204            // this is called, but the count stays well-defined either way).
205            if parts.iter().any(|p| matches!(p, StrPart::Parsed(_))) {
206                *acc += 1;
207            }
208            for part in parts {
209                if let StrPart::Parsed(e) = part {
210                    count_expr_alloc_sites(&e.node, policy, acc);
211                }
212            }
213        }
214        Expr::Constructor(name, Some(payload)) => {
215            if policy.constructor_allocates(name, true) {
216                *acc += 1;
217            }
218            count_expr_alloc_sites(&payload.node, policy, acc);
219        }
220        Expr::FnCall(callee, args) => {
221            if let Some(name) = expr_to_dotted_name(&callee.node) {
222                let ns = name.split('.').next().unwrap_or("");
223                if is_builtin_namespace(ns) && policy.builtin_allocates(&name) {
224                    *acc += 1;
225                }
226            }
227            count_expr_alloc_sites(&callee.node, policy, acc);
228            for a in args {
229                count_expr_alloc_sites(&a.node, policy, acc);
230            }
231        }
232        Expr::TailCall(data) => {
233            for a in &data.args {
234                count_expr_alloc_sites(&a.node, policy, acc);
235            }
236        }
237        Expr::Attr(base, _) | Expr::ErrorProp(base) => {
238            count_expr_alloc_sites(&base.node, policy, acc);
239        }
240        Expr::BinOp(_, l, r) => {
241            count_expr_alloc_sites(&l.node, policy, acc);
242            count_expr_alloc_sites(&r.node, policy, acc);
243        }
244        Expr::Match { subject, arms } => {
245            count_expr_alloc_sites(&subject.node, policy, acc);
246            for arm in arms {
247                count_expr_alloc_sites(&arm.body.node, policy, acc);
248            }
249        }
250    }
251}
252
253/// Sum of [`count_alloc_sites_in_fn`] across every FnDef in `items` under
254/// `policy`. A scenario-level rollup for bench reporting.
255pub fn count_alloc_sites_in_program<P: AllocPolicy>(
256    items: &[crate::ast::TopLevel],
257    policy: &P,
258) -> usize {
259    items
260        .iter()
261        .filter_map(|it| match it {
262            crate::ast::TopLevel::FnDef(fd) => Some(count_alloc_sites_in_fn(fd, policy)),
263            _ => None,
264        })
265        .sum()
266}
267
268/// Compute per-fn allocation status for every fn in `fns` under `policy`.
269///
270/// Iterates to fixpoint: a fn is marked allocating if it has effects, if
271/// its body contains a direct allocation, or if it calls (transitively,
272/// via the existing `info` map) a user fn already classified as
273/// allocating. Mutual recursion is handled by repeating the pass until
274/// nothing changes.
275///
276/// Once a fn flips to `true` it never reverts, so the loop is monotone
277/// and converges in at most `fns.len()` iterations.
278pub fn compute_alloc_info<P: AllocPolicy>(fns: &[&FnDef], policy: &P) -> HashMap<String, bool> {
279    let mut info: HashMap<String, bool> = fns
280        .iter()
281        .map(|fd| {
282            // Effects declaration → conservative "allocates".
283            (fd.name.clone(), !fd.effects.is_empty())
284        })
285        .collect();
286
287    loop {
288        let mut changed = false;
289        for fd in fns {
290            if *info.get(&fd.name).unwrap_or(&false) {
291                continue;
292            }
293            if body_allocates(&fd.body, &info, policy) {
294                info.insert(fd.name.clone(), true);
295                changed = true;
296            }
297        }
298        if !changed {
299            break;
300        }
301    }
302
303    info
304}
305
306#[cfg(test)]
307mod tests {
308    use super::*;
309    use crate::ast::{BinOp, FnDef, Literal, Spanned};
310    use std::sync::Arc;
311
312    /// Test policy: every builtin in the `Map.*` namespace allocates,
313    /// `Int.toString` allocates, everything else doesn't. No constructors
314    /// allocate.
315    struct TestPolicy;
316
317    impl AllocPolicy for TestPolicy {
318        fn builtin_allocates(&self, name: &str) -> bool {
319            name.starts_with("Map.") || name == "Int.toString"
320        }
321        fn constructor_allocates(&self, _name: &str, _has_payload: bool) -> bool {
322            false
323        }
324    }
325
326    fn sp<T>(value: T) -> Spanned<T> {
327        Spanned {
328            node: value,
329            line: 1,
330        }
331    }
332
333    fn lit_int(n: i64) -> Spanned<Expr> {
334        sp(Expr::Literal(Literal::Int(n)))
335    }
336
337    fn fn_def_pure(name: &str, body: Expr) -> FnDef {
338        FnDef {
339            name: name.to_string(),
340            line: 1,
341            params: vec![],
342            return_type: "Int".into(),
343            effects: vec![],
344            desc: None,
345            body: Arc::new(FnBody::from_expr(sp(body))),
346            resolution: None,
347        }
348    }
349
350    #[test]
351    fn pure_arithmetic_does_not_allocate() {
352        let fd = fn_def_pure(
353            "addOne",
354            Expr::BinOp(BinOp::Add, Box::new(lit_int(1)), Box::new(lit_int(2))),
355        );
356        let info = compute_alloc_info(&[&fd], &TestPolicy);
357        assert_eq!(info.get("addOne"), Some(&false));
358    }
359
360    #[test]
361    fn list_literal_allocates() {
362        let fd = fn_def_pure("makeList", Expr::List(vec![lit_int(1), lit_int(2)]));
363        let info = compute_alloc_info(&[&fd], &TestPolicy);
364        assert_eq!(info.get("makeList"), Some(&true));
365    }
366
367    #[test]
368    fn allocating_builtin_call_allocates() {
369        // Int.toString(42)
370        let call = Expr::FnCall(
371            Box::new(sp(Expr::Attr(
372                Box::new(sp(Expr::Ident("Int".into()))),
373                "toString".into(),
374            ))),
375            vec![lit_int(42)],
376        );
377        let fd = fn_def_pure("stringify", call);
378        let info = compute_alloc_info(&[&fd], &TestPolicy);
379        assert_eq!(info.get("stringify"), Some(&true));
380    }
381
382    #[test]
383    fn pure_builtin_call_does_not_allocate() {
384        // Int.abs(-5) — Int.abs is not in the test policy's alloc list.
385        let call = Expr::FnCall(
386            Box::new(sp(Expr::Attr(
387                Box::new(sp(Expr::Ident("Int".into()))),
388                "abs".into(),
389            ))),
390            vec![lit_int(-5)],
391        );
392        let fd = fn_def_pure("absVal", call);
393        let info = compute_alloc_info(&[&fd], &TestPolicy);
394        assert_eq!(info.get("absVal"), Some(&false));
395    }
396
397    #[test]
398    fn effects_force_allocating() {
399        let mut fd = fn_def_pure("logIt", Expr::Literal(Literal::Int(0)));
400        fd.effects = vec![sp("Console.print".into())];
401        let info = compute_alloc_info(&[&fd], &TestPolicy);
402        assert_eq!(info.get("logIt"), Some(&true));
403    }
404
405    #[test]
406    fn transitive_user_call_propagates() {
407        // makeListInner allocates (list literal).
408        // wrapperFn calls makeListInner — also allocates by transitivity.
409        let inner = fn_def_pure("makeListInner", Expr::List(vec![lit_int(1)]));
410
411        let call = Expr::FnCall(Box::new(sp(Expr::Ident("makeListInner".into()))), vec![]);
412        let wrapper = fn_def_pure("wrapperFn", call);
413
414        let info = compute_alloc_info(&[&inner, &wrapper], &TestPolicy);
415        assert_eq!(info.get("makeListInner"), Some(&true));
416        assert_eq!(info.get("wrapperFn"), Some(&true));
417    }
418
419    #[test]
420    fn mutual_recursion_pure_stays_pure() {
421        // f calls g, g calls f, both pure (no allocation anywhere).
422        let f = fn_def_pure(
423            "f",
424            Expr::FnCall(Box::new(sp(Expr::Ident("g".into()))), vec![lit_int(1)]),
425        );
426        let g = fn_def_pure(
427            "g",
428            Expr::FnCall(Box::new(sp(Expr::Ident("f".into()))), vec![lit_int(2)]),
429        );
430        let info = compute_alloc_info(&[&f, &g], &TestPolicy);
431        assert_eq!(info.get("f"), Some(&false));
432        assert_eq!(info.get("g"), Some(&false));
433    }
434
435    #[test]
436    fn mutual_recursion_one_allocates_taints_the_group() {
437        // f calls g, g allocates. Both end up allocating.
438        let f = fn_def_pure(
439            "f",
440            Expr::FnCall(Box::new(sp(Expr::Ident("g".into()))), vec![lit_int(1)]),
441        );
442        let g = fn_def_pure("g", Expr::List(vec![lit_int(0)]));
443        let info = compute_alloc_info(&[&f, &g], &TestPolicy);
444        assert_eq!(info.get("f"), Some(&true));
445        assert_eq!(info.get("g"), Some(&true));
446    }
447}