add specialize_host_specializations

This commit is contained in:
Folkert 2021-11-19 22:15:02 +01:00
parent 6baffdf6fb
commit 5d9d2b7fea

View file

@ -225,7 +225,7 @@ impl<'a> Default for CapturedSymbols<'a> {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct PendingSpecialization<'a> { pub struct PendingSpecialization<'a> {
solved_type: SolvedType, solved_type: SolvedType,
host_exposed_aliases: BumpMap<Symbol, SolvedType>, host_exposed_aliases: MutMap<Symbol, SolvedType>,
_lifetime: std::marker::PhantomData<&'a u8>, _lifetime: std::marker::PhantomData<&'a u8>,
} }
@ -234,7 +234,8 @@ impl<'a> PendingSpecialization<'a> {
let solved_type = SolvedType::from_var(subs, var); let solved_type = SolvedType::from_var(subs, var);
PendingSpecialization { PendingSpecialization {
solved_type, solved_type,
host_exposed_aliases: BumpMap::new_in(arena), host_exposed_aliases: MutMap::default(),
_lifetime: std::marker::PhantomData, _lifetime: std::marker::PhantomData,
} }
} }
@ -247,7 +248,7 @@ impl<'a> PendingSpecialization<'a> {
) -> Self { ) -> Self {
let solved_type = SolvedType::from_var(subs, var); let solved_type = SolvedType::from_var(subs, var);
let mut host_exposed_aliases = BumpMap::with_capacity_in(exposed.len(), arena); let mut host_exposed_aliases = MutMap::default();
host_exposed_aliases.extend( host_exposed_aliases.extend(
exposed exposed
@ -886,7 +887,7 @@ impl<'a> Procs<'a> {
symbol, symbol,
layout_cache, layout_cache,
annotation, annotation,
BumpMap::new_in(env.arena), MutMap::default(),
partial_proc_id, partial_proc_id,
) { ) {
Ok((proc, layout)) => { Ok((proc, layout)) => {
@ -2020,7 +2021,7 @@ fn specialize_suspended<'a>(
name, name,
layout_cache, layout_cache,
var, var,
BumpMap::new_in(env.arena), MutMap::default(),
partial_proc, partial_proc,
) { ) {
Ok((proc, layout)) => { Ok((proc, layout)) => {
@ -2136,6 +2137,70 @@ pub fn specialize_all<'a>(
procs procs
} }
fn specialize_host_specializations<'a>(
env: &mut Env<'a, '_>,
procs: &mut Procs<'a>,
layout_cache: &mut LayoutCache<'a>,
host_specializations: HostSpecializations,
) {
let (store, it) = host_specializations.decompose();
let offset_variable = StorageSubs::merge_into(store, env.subs);
for (symbol, solved_types, host_exposed_aliases) in it {
let it = solved_types
.into_iter()
.zip(host_exposed_aliases.into_iter());
for (store_variable, host_exposed_aliases) in it {
let variable = offset_variable(store_variable);
// historical note: we used to deduplicate with a hash here,
// but the cost of that hash is very high. So for now we make
// duplicate specializations, and the insertion into a hash map
// below will deduplicate them.
let name = symbol;
let partial_proc_id = match procs.partial_procs.symbol_to_id(name) {
Some(v) => v,
None => {
panic!("Cannot find a partial proc for {:?}", name);
}
};
// TODO I believe this is also duplicated
match specialize_variable(
env,
procs,
name,
layout_cache,
variable,
host_exposed_aliases,
partial_proc_id,
) {
Ok((proc, layout)) => {
let top_level = ProcLayout::from_raw(env.arena, layout);
if procs.is_module_thunk(name) {
debug_assert!(top_level.arguments.is_empty());
}
procs.specialized.insert_specialized(name, top_level, proc);
}
Err(SpecializeFailure {
problem: _,
attempted_layout,
}) => {
let proc = generate_runtime_error_function(env, name, attempted_layout);
let top_level = ProcLayout::from_raw(env.arena, attempted_layout);
procs.specialized.insert_specialized(name, top_level, proc);
}
}
}
}
}
fn specialize_externals_others_need<'a>( fn specialize_externals_others_need<'a>(
env: &mut Env<'a, '_>, env: &mut Env<'a, '_>,
procs: &mut Procs<'a>, procs: &mut Procs<'a>,
@ -2171,7 +2236,7 @@ fn specialize_externals_others_need<'a>(
name, name,
layout_cache, layout_cache,
variable, variable,
BumpMap::new_in(env.arena), MutMap::default(),
partial_proc_id, partial_proc_id,
) { ) {
Ok((proc, layout)) => { Ok((proc, layout)) => {
@ -2814,7 +2879,7 @@ fn specialize_solved_type<'a>(
proc_name: Symbol, proc_name: Symbol,
layout_cache: &mut LayoutCache<'a>, layout_cache: &mut LayoutCache<'a>,
solved_type: &SolvedType, solved_type: &SolvedType,
host_exposed_aliases: BumpMap<Symbol, SolvedType>, host_exposed_aliases: MutMap<Symbol, SolvedType>,
partial_proc_id: PartialProcId, partial_proc_id: PartialProcId,
) -> Result<SpecializeSuccess<'a>, SpecializeFailure<'a>> { ) -> Result<SpecializeSuccess<'a>, SpecializeFailure<'a>> {
specialize_variable_help( specialize_variable_help(
@ -2834,7 +2899,7 @@ fn specialize_variable<'a>(
proc_name: Symbol, proc_name: Symbol,
layout_cache: &mut LayoutCache<'a>, layout_cache: &mut LayoutCache<'a>,
fn_var: Variable, fn_var: Variable,
host_exposed_aliases: BumpMap<Symbol, SolvedType>, host_exposed_aliases: MutMap<Symbol, SolvedType>,
partial_proc_id: PartialProcId, partial_proc_id: PartialProcId,
) -> Result<SpecializeSuccess<'a>, SpecializeFailure<'a>> { ) -> Result<SpecializeSuccess<'a>, SpecializeFailure<'a>> {
specialize_variable_help( specialize_variable_help(
@ -2854,7 +2919,7 @@ fn specialize_variable_help<'a, F>(
proc_name: Symbol, proc_name: Symbol,
layout_cache: &mut LayoutCache<'a>, layout_cache: &mut LayoutCache<'a>,
fn_var_thunk: F, fn_var_thunk: F,
host_exposed_aliases: BumpMap<Symbol, SolvedType>, host_exposed_aliases: MutMap<Symbol, SolvedType>,
partial_proc_id: PartialProcId, partial_proc_id: PartialProcId,
) -> Result<SpecializeSuccess<'a>, SpecializeFailure<'a>> ) -> Result<SpecializeSuccess<'a>, SpecializeFailure<'a>>
where where
@ -7014,7 +7079,7 @@ fn call_by_name_help<'a>(
proc_name, proc_name,
layout_cache, layout_cache,
fn_var, fn_var,
BumpMap::new_in(env.arena), MutMap::default(),
partial_proc, partial_proc,
) { ) {
Ok((proc, layout)) => { Ok((proc, layout)) => {
@ -7136,7 +7201,7 @@ fn call_by_name_module_thunk<'a>(
proc_name, proc_name,
layout_cache, layout_cache,
fn_var, fn_var,
BumpMap::new_in(env.arena), MutMap::default(),
partial_proc, partial_proc,
) { ) {
Ok((proc, raw_layout)) => { Ok((proc, raw_layout)) => {