use std::collections::HashMap;
use std::sync::{Arc, PoisonError};
use dive_ir::{Arg, DeclId, DefId, DefScope, Expr, ExprId};
use dive_value::Value;
use crate::ConstBinding;
use crate::core::Core;
use crate::env::{Env, Ev, St, Sv, control};
use crate::error::Result;
use crate::options::Comparisons;
const MAX_ENTRIES: usize = 4096;
const MAX_UNREPEATED: u32 = 512;
const MAX_SETTLE_DEPTH: usize = 8;
struct Entry {
def: DefId,
control: u8,
inputs: Vec<Value>,
value: Value,
}
#[derive(Default)]
pub(crate) struct StartupCalls {
entries: HashMap<u64, Vec<Entry>>,
len: usize,
yields: HashMap<DefId, (u32, u32)>,
pub(crate) hits: u64,
}
impl StartupCalls {
fn declined(&self, d: DefId) -> bool {
self.yields
.get(&d)
.is_some_and(|&(kept, hit)| kept > hit.saturating_add(MAX_UNREPEATED))
}
fn get(&mut self, d: DefId, control: u8, inputs: &[Value]) -> Option<Value> {
let found = self
.entries
.get(&identity_hash(d, control, inputs))?
.iter()
.find(|e| {
e.def == d
&& e.control == control
&& e.inputs.len() == inputs.len()
&& e.inputs.iter().zip(inputs).all(|(x, y)| same(x, y))
})?;
self.hits += 1;
let y = self.yields.entry(d).or_default();
y.1 = y.1.saturating_add(1);
Some(found.value.clone())
}
fn insert(&mut self, d: DefId, control: u8, inputs: Vec<Value>, value: Value) {
if self.len >= MAX_ENTRIES {
self.entries.clear();
self.len = 0;
}
let y = self.yields.entry(d).or_default();
y.0 = y.0.saturating_add(1);
self.entries
.entry(identity_hash(d, control, &inputs))
.or_default()
.push(Entry {
def: d,
control,
inputs,
value,
});
self.len += 1;
}
}
fn identity_hash(d: DefId, control: u8, inputs: &[Value]) -> u64 {
let mix = |h: u64, x: u64| (h.rotate_left(5) ^ x).wrapping_mul(0x517c_c1b7_2722_0a95);
let mut h = mix(u64::from(d.0), u64::from(control));
for v in inputs {
let x = match v {
Value::Set(s) => s.addr() as u64,
Value::Fcn(f) => f.addr() as u64,
scalar => scalar.hash64(),
};
h = mix(h, x);
}
h
}
fn same(a: &Value, b: &Value) -> bool {
match (a, b) {
(Value::Set(x), Value::Set(y)) => x.ptr_eq(y),
(Value::Fcn(x), Value::Fcn(y)) => x.ptr_eq(y),
(Value::Bool(x), Value::Bool(y)) => x == y,
(Value::Int(x), Value::Int(y)) => x == y,
(Value::BigInt(x), Value::BigInt(y)) => x == y,
(Value::Str(x), Value::Str(y)) | (Value::Model(x), Value::Model(y)) => x == y,
_ => false,
}
}
impl Core {
pub(crate) fn startup_call(
self: &Arc<Self>,
caller: &Env,
st: &St,
d: DefId,
args: &[Arg],
e: ExprId,
) -> Option<Result<Ev>> {
if args.is_empty()
|| !st.is(control::PRECOMPUTE)
|| st.is(control::DIAGNOSTIC)
|| self.opts.comparisons != Comparisons::Tla
|| self.expression_cache() != 0
{
return None;
}
let substs = self.call_cacheable(d)?;
if self
.startup_calls
.lock()
.unwrap_or_else(PoisonError::into_inner)
.as_ref()
.is_none_or(|calls| calls.declined(d))
{
return None;
}
let mut values = Vec::with_capacity(args.len() + substs.len());
for a in args {
let Arg::Expr { expr, .. } = a else {
return None;
};
values.push(self.settled_value(caller, *expr, 0)?);
}
for &decl in substs.iter() {
values.push(self.settled_subst(caller, decl)?);
}
{
let mut calls = self
.startup_calls
.lock()
.unwrap_or_else(PoisonError::into_inner);
let calls = calls.as_mut()?;
if let Some(v) = calls.get(d, st.control, &values) {
return Some(Ok(Ev::V(v)));
}
}
let definition = self.ir.def(d);
let mut body_env = Env::new(
d,
definition.depth,
definition.frame.len(),
None,
caller.dynamic.clone(),
);
for (i, v) in values.iter().take(args.len()).enumerate() {
body_env.bind(i, Sv::Val(v.clone()));
}
let r = match self.enter(e) {
Ok(_g) => self.eval(&mut body_env, st, definition.body),
Err(error) => Err(error),
};
if let Ok(Ev::V(v)) = &r
&& let Some(calls) = self
.startup_calls
.lock()
.unwrap_or_else(PoisonError::into_inner)
.as_mut()
{
calls.insert(d, st.control, values, v.clone());
}
Some(r)
}
fn settled_value(&self, env: &Env, expr: ExprId, depth: usize) -> Option<Value> {
if let Some(v) = self.settled(env, expr) {
return Some(v);
}
match &self.ir.node(expr).expr {
Expr::Const(c) => self.const_value(*c),
Expr::SubstRef {
decl,
primed: false,
} => self.settled_subst_at(env, *decl, depth),
_ => None,
}
}
fn settled_subst(&self, env: &Env, decl: DeclId) -> Option<Value> {
self.settled_subst_at(env, decl, 0)
}
fn settled_subst_at(&self, env: &Env, decl: DeclId, depth: usize) -> Option<Value> {
let info = self.ir.decl(decl);
if info.variable || info.var.is_some() {
return None;
}
match env.dynamic.lookup_cut(decl, false) {
Some(Sv::Val(v)) => Some(v.clone()),
Some(Sv::Thunk(t)) if depth < MAX_SETTLE_DEPTH => {
self.settled_value(&t.env, t.expr, depth + 1)
}
Some(_) => None,
None => info.constant.and_then(|c| self.const_value(c)),
}
}
fn const_value(&self, c: dive_ir::ConstId) -> Option<Value> {
match self.consts.get(c.index()).and_then(Option::as_ref) {
Some(ConstBinding::Value(v)) => Some(v.clone()),
_ => None,
}
}
fn call_cacheable(&self, d: DefId) -> Option<Arc<[DeclId]>> {
let def = self.ir.def(d);
let qualifies = !def.params.is_empty()
&& def.std.is_none()
&& !def.recursive
&& def.level == dive_ir::Level::Constant
&& def.scope == DefScope::TopLevel
&& matches!(def.kind, dive_ir::DefKind::Operator)
&& !def.effects.intersects(dive_ir::Effects::IMPURE);
if !qualifies {
return None;
}
self.call_cache_safe[d.index()]
.get_or_init(|| {
let mut substs = Vec::new();
self.state_free(d, true, Some(&mut substs))
.then(|| Arc::from(substs))
})
.clone()
}
}
#[cfg(test)]
#[path = "startup_calls_tests.rs"]
mod tests;