| @@ -7,8 +7,11 @@ |
| 7 | 7 | //! pure functions; calls of unknown closures never (after evidence |
| 8 | 8 | //! specialisation, the dictionary calls are direct calls or externs); |
| 9 | 9 | //! externs only if the extern table marks them effect-free |
| 10 | | −//! (`Extern::effect_free`). Panics and divergence are imprecise (ADR |
| 11 | | −//! 0043): they do not make a function impure. Allocation doesn't either: |
| 10 | +//! (`Extern::effect_free`). And **no panics**: a crash is observable in |
| 11 | +//! Louise — a monitor sees `Crashed(message)` (ADR 0004) — so a panicking |
| 12 | +//! stage is never fused, not even one whose calls would all still run in |
| 13 | +//! the same order. (Divergence stays imprecise, as in GHC: a pure stage |
| 14 | +//! that loops hangs fused or not.) Allocation is not observable either: |
| 12 | 15 | //! reference counting has no hooks. |
| 13 | 16 | //! |
| 14 | 17 | //! A greatest fixed point, computed as the least fixed point of impurity: |
| @@ -17,9 +20,9 @@ |
| 17 | 20 | |
| 18 | 21 | use std::collections::{HashMap, HashSet}; |
| 19 | 22 | |
| 20 | | −use super::simplify::visit_values; |
| 23 | +use super::simplify::visit_exprs; |
| 21 | 24 | use crate::externs::runtime_extern; |
| 22 | | −use crate::ir::{FnId, Program, Value}; |
| 25 | +use crate::ir::{Atom, Expr, FnId, Lit, Program, Value}; |
| 23 | 26 | |
| 24 | 27 | /// The pure functions of `program`. |
| 25 | 28 | pub fn functions(program: &Program) -> HashSet<FnId> { |
| @@ -28,22 +31,27 @@ pub fn functions(program: &Program) -> HashSet<FnId> { |
| 28 | 31 | let mut impure: HashSet<FnId> = HashSet::new(); |
| 29 | 32 | for (id, f) in program.fns.iter().enumerate() { |
| 30 | 33 | let mut is_impure = false; |
| 31 | | − visit_values(&f.body, &mut |v| match v { |
| 32 | | − Value::Perform { .. } |
| 33 | | − | Value::Handle { .. } |
| 34 | | − | Value::Region { .. } |
| 35 | | − | Value::Defer { .. } => is_impure = true, |
| 36 | | − Value::Call(..) => is_impure = true, // an unknown closure |
| 37 | | − Value::CallExtern(symbol, _) => { |
| 38 | | − // A binding's extern (not in the table) may do anything. |
| 39 | | − if !runtime_extern(symbol).is_some_and(|e| e.effect_free) { |
| 40 | | − is_impure = true; |
| 41 | | − } |
| 34 | + visit_exprs(&f.body, &mut |e| { |
| 35 | + match e { |
| 36 | + Expr::Panic(_) => is_impure = true, |
| 37 | + Expr::Let(_, value, _, _) => match value.as_ref() { |
| 38 | + Value::Perform { .. } |
| 39 | + | Value::Handle { .. } |
| 40 | + | Value::Region { .. } |
| 41 | + | Value::Defer { .. } => is_impure = true, |
| 42 | + Value::Call(..) => is_impure = true, // an unknown closure |
| 43 | + Value::CallExtern(symbol, args) => { |
| 44 | + if !extern_pure(symbol, args) { |
| 45 | + is_impure = true; |
| 46 | + } |
| 47 | + } |
| 48 | + Value::CallFn(g, _) => { |
| 49 | + callers.entry(*g).or_default().push(id as FnId); |
| 50 | + } |
| 51 | + _ => {} |
| 52 | + }, |
| 53 | + _ => {} |
| 42 | 54 | } |
| 43 | | − Value::CallFn(g, _) => { |
| 44 | | − callers.entry(*g).or_default().push(id as FnId); |
| 45 | | − } |
| 46 | | − _ => {} |
| 47 | 55 | }); |
| 48 | 56 | if is_impure { |
| 49 | 57 | impure.insert(id as FnId); |
| @@ -62,3 +70,29 @@ pub fn functions(program: &Program) -> HashSet<FnId> { |
| 62 | 70 | .filter(|id| !impure.contains(id)) |
| 63 | 71 | .collect() |
| 64 | 72 | } |
| 73 | + |
| 74 | +/// Whether a call of the extern is effect-free and cannot panic. The |
| 75 | +/// table's `effect_free` says the first; the second needs the arguments |
| 76 | +/// where a panic depends on one — division and the shifts panic on a |
| 77 | +/// zero divisor or a negative amount, so only a literal that rules it |
| 78 | +/// out keeps them pure (`x % 3` fuses, `x % n` doesn't). |
| 79 | +fn extern_pure(symbol: &str, args: &[Atom]) -> bool { |
| 80 | + let Some(e) = runtime_extern(symbol) else { |
| 81 | + return false; // a binding's extern may do anything |
| 82 | + }; |
| 83 | + if e.effect_free { |
| 84 | + return true; |
| 85 | + } |
| 86 | + if !e.may_unwind { |
| 87 | + return false; |
| 88 | + } |
| 89 | + let nonzero = matches!(symbol, "tl_int_div" | "tl_int_rem" | "tl_u64_div" | "tl_u64_rem"); |
| 90 | + let nonnegative = matches!( |
| 91 | + symbol, |
| 92 | + "tl_int_shift_left" | "tl_int_shift_right" | "tl_u64_shift_left" | "tl_u64_shift_right" |
| 93 | + ); |
| 94 | + let Some(Atom::Lit(Lit::Int(n))) = args.get(1) else { |
| 95 | + return false; |
| 96 | + }; |
| 97 | + (nonzero && *n != 0) || (nonnegative && *n >= 0) |
| 98 | +} |