Lines
100 %
Functions
68.75 %
Branches
//! Per-`(defun_name, signature)` monomorph cache.
//!
//! Tier 1.5 closes Gap B (recursive defun called with a runtime arg) by
//! lowering through a real wasm fn rather than the inline const-fold
//! walk. Each distinct call-site signature gets its own emitted fn:
//! `(defun fact (n) ...)` called with a `Ratio` arg emits one wasm fn,
//! the same defun called with (hypothetically) a `Commodity` arg emits
//! another. The cache lookup is keyed by `(name, params)` so call sites
//! with identical signatures share one fn — including the recursive
//! self-call inside the body.
//! The cache is mutated only by [`CompileContext::lookup_or_emit_monomorph`]
//! (added in `compiler/expr/call.rs`) and snapshot-restored along with
//! the rest of the wasm-section state via [`super::snapshot`].
use std::collections::HashMap;
use crate::ast::{Expr, LambdaParams, WasmType};
use crate::error::{Error, Result};
use super::CompileContext;
/// A free runtime name the helper reads from its caller: passed after the
/// arguments, as the shared cell itself or as a plain value.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub(crate) struct Slot {
pub name: String,
pub ty: WasmType,
pub cell: bool,
}
/// One monomorph entry: the wasm fn idx at which the body was emitted
/// plus the body-derived return type and the slots every call passes.
/// Cloneable + Eq because it lives in the snapshot/restore graph.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct MonomorphEntry {
pub func_idx: u32,
pub ret_ty: WasmType,
pub slots: Vec<Slot>,
/// Cache key: defun name, the table-assigned id of the definition it
/// resolved to, the redefinition epoch, the monomorph parameter signature and the free
/// runtime names it takes. The body AST stays
/// stored on the `Symbol::function` slot and is re-fetched at emit time;
/// a redefinition or a shadowing LABELS gets a fresh id, so it misses.
pub(crate) struct MonomorphKey {
pub definition: u32,
pub epoch: u32,
pub params: Vec<WasmType>,
/// Per parameter, the interned source of a capture-free closure argument:
/// the helper inlines it per call, as the call site would, so two lambdas
/// of one signature never share a helper.
pub closures: Vec<Option<u32>>,
#[derive(Debug, Default, Clone)]
pub(crate) struct MonomorphCache {
entries: HashMap<MonomorphKey, MonomorphEntry>,
sources: Vec<(LambdaParams, Expr)>,
impl MonomorphCache {
pub(crate) fn intern_source(&mut self, params: LambdaParams, body: Expr) -> Result<u32> {
let source = (params, body);
let at = match self.sources.iter().position(|known| *known == source) {
Some(at) => at,
None => {
self.sources.push(source);
self.sources.len() - 1
};
u32::try_from(at).map_err(|_| Error::Compile("closure source id space exhausted".into()))
pub(crate) fn source(&self, id: u32) -> Option<&(LambdaParams, Expr)> {
self.sources.get(usize::try_from(id).ok()?)
pub(crate) fn get(&self, key: &MonomorphKey) -> Option<&MonomorphEntry> {
self.entries.get(key)
pub(crate) fn insert(&mut self, key: MonomorphKey, entry: MonomorphEntry) {
self.entries.insert(key, entry);
impl CompileContext {
pub(crate) fn monomorph_cache(&self) -> &MonomorphCache {
&self.monomorphs
pub(crate) fn monomorph_cache_mut(&mut self) -> &mut MonomorphCache {
&mut self.monomorphs
#[cfg(test)]
#[path = "monomorph_tests.rs"]
mod tests;