use crate::component::*;
use crate::prelude::*;
use crate::{EntityIndex, EntityRef, ModuleInternedTypeIndex, PrimaryMap, WasmValType};
use anyhow::Result;
use indexmap::IndexMap;
use std::collections::HashMap;
use std::hash::Hash;
use std::ops::Index;
use wasmparser::component_types::ComponentCoreModuleTypeId;
#[derive(Default)]
pub struct ComponentDfg {
pub import_types: PrimaryMap<ImportIndex, (String, TypeDef)>,
pub imports: PrimaryMap<RuntimeImportIndex, (ImportIndex, Vec<String>)>,
pub exports: IndexMap<String, Export>,
pub trampolines: Intern<TrampolineIndex, (ModuleInternedTypeIndex, Trampoline)>,
pub reallocs: Intern<ReallocId, CoreDef>,
pub callbacks: Intern<CallbackId, CoreDef>,
pub post_returns: Intern<PostReturnId, CoreDef>,
pub memories: Intern<MemoryId, CoreExport<MemoryIndex>>,
pub adapters: Intern<AdapterId, Adapter>,
pub instances: PrimaryMap<InstanceId, Instance>,
pub num_runtime_component_instances: u32,
pub adapter_modules: PrimaryMap<AdapterModuleId, (StaticModuleIndex, Vec<CoreDef>)>,
pub adapter_partitionings: PrimaryMap<AdapterId, (AdapterModuleId, EntityIndex)>,
pub resources: PrimaryMap<DefinedResourceIndex, Resource>,
pub imported_resources: PrimaryMap<ResourceIndex, RuntimeImportIndex>,
pub num_resource_tables: usize,
pub num_future_tables: usize,
pub num_stream_tables: usize,
pub num_error_context_tables: usize,
pub side_effects: Vec<SideEffect>,
}
pub enum SideEffect {
Instance(InstanceId),
Resource(DefinedResourceIndex),
}
macro_rules! id {
($(pub struct $name:ident(u32);)*) => ($(
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)]
#[allow(missing_docs, reason = "tedious to document")]
pub struct $name(u32);
cranelift_entity::entity_impl!($name);
)*)
}
id! {
pub struct InstanceId(u32);
pub struct MemoryId(u32);
pub struct ReallocId(u32);
pub struct CallbackId(u32);
pub struct AdapterId(u32);
pub struct PostReturnId(u32);
pub struct AdapterModuleId(u32);
}
#[allow(missing_docs, reason = "tedious to document variants")]
pub enum Instance {
Static(StaticModuleIndex, Box<[CoreDef]>),
Import(
RuntimeImportIndex,
IndexMap<String, IndexMap<String, CoreDef>>,
),
}
#[allow(missing_docs, reason = "tedious to document variants")]
pub enum Export {
LiftedFunction {
ty: TypeFuncIndex,
func: CoreDef,
options: CanonicalOptions,
},
ModuleStatic {
ty: ComponentCoreModuleTypeId,
index: StaticModuleIndex,
},
ModuleImport {
ty: TypeModuleIndex,
import: RuntimeImportIndex,
},
Instance {
ty: TypeComponentInstanceIndex,
exports: IndexMap<String, Export>,
},
Type(TypeDef),
}
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
#[allow(missing_docs, reason = "tedious to document variants")]
pub enum CoreDef {
Export(CoreExport<EntityIndex>),
InstanceFlags(RuntimeComponentInstanceIndex),
Trampoline(TrampolineIndex),
Adapter(AdapterId),
}
impl<T> From<CoreExport<T>> for CoreDef
where
EntityIndex: From<T>,
{
fn from(export: CoreExport<T>) -> CoreDef {
CoreDef::Export(export.map_index(|i| i.into()))
}
}
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
#[allow(missing_docs, reason = "self-describing fields")]
pub struct CoreExport<T> {
pub instance: InstanceId,
pub item: ExportItem<T>,
}
impl<T> CoreExport<T> {
#[allow(missing_docs, reason = "self-describing function")]
pub fn map_index<U>(self, f: impl FnOnce(T) -> U) -> CoreExport<U> {
CoreExport {
instance: self.instance,
item: match self.item {
ExportItem::Index(i) => ExportItem::Index(f(i)),
ExportItem::Name(s) => ExportItem::Name(s),
},
}
}
}
#[derive(Clone, PartialEq, Eq, Hash)]
#[allow(missing_docs, reason = "self-describing fields")]
pub enum Trampoline {
LowerImport {
import: RuntimeImportIndex,
options: CanonicalOptions,
lower_ty: TypeFuncIndex,
},
Transcoder {
op: Transcode,
from: MemoryId,
from64: bool,
to: MemoryId,
to64: bool,
},
AlwaysTrap,
ResourceNew(TypeResourceTableIndex),
ResourceRep(TypeResourceTableIndex),
ResourceDrop(TypeResourceTableIndex),
TaskBackpressure {
instance: RuntimeComponentInstanceIndex,
},
TaskReturn,
TaskWait {
instance: RuntimeComponentInstanceIndex,
async_: bool,
memory: MemoryId,
},
TaskPoll {
instance: RuntimeComponentInstanceIndex,
async_: bool,
memory: MemoryId,
},
TaskYield {
async_: bool,
},
SubtaskDrop {
instance: RuntimeComponentInstanceIndex,
},
StreamNew {
ty: TypeStreamTableIndex,
},
StreamRead {
ty: TypeStreamTableIndex,
options: CanonicalOptions,
},
StreamWrite {
ty: TypeStreamTableIndex,
options: CanonicalOptions,
},
StreamCancelRead {
ty: TypeStreamTableIndex,
async_: bool,
},
StreamCancelWrite {
ty: TypeStreamTableIndex,
async_: bool,
},
StreamCloseReadable {
ty: TypeStreamTableIndex,
},
StreamCloseWritable {
ty: TypeStreamTableIndex,
},
FutureNew {
ty: TypeFutureTableIndex,
},
FutureRead {
ty: TypeFutureTableIndex,
options: CanonicalOptions,
},
FutureWrite {
ty: TypeFutureTableIndex,
options: CanonicalOptions,
},
FutureCancelRead {
ty: TypeFutureTableIndex,
async_: bool,
},
FutureCancelWrite {
ty: TypeFutureTableIndex,
async_: bool,
},
FutureCloseReadable {
ty: TypeFutureTableIndex,
},
FutureCloseWritable {
ty: TypeFutureTableIndex,
},
ErrorContextNew {
ty: TypeComponentLocalErrorContextTableIndex,
options: CanonicalOptions,
},
ErrorContextDebugMessage {
ty: TypeComponentLocalErrorContextTableIndex,
options: CanonicalOptions,
},
ErrorContextDrop {
ty: TypeComponentLocalErrorContextTableIndex,
},
ResourceTransferOwn,
ResourceTransferBorrow,
ResourceEnterCall,
ResourceExitCall,
AsyncEnterCall,
AsyncExitCall {
callback: Option<CallbackId>,
post_return: Option<PostReturnId>,
},
FutureTransfer,
StreamTransfer,
ErrorContextTransfer,
}
#[derive(Copy, Clone, Hash, Eq, PartialEq)]
#[allow(missing_docs, reason = "self-describing fields")]
pub struct FutureInfo {
pub instance: RuntimeComponentInstanceIndex,
pub payload_type: Option<InterfaceType>,
}
#[derive(Copy, Clone, Hash, Eq, PartialEq)]
#[allow(missing_docs, reason = "self-describing fields")]
pub struct StreamInfo {
pub instance: RuntimeComponentInstanceIndex,
pub payload_type: InterfaceType,
}
#[derive(Clone, Hash, Eq, PartialEq)]
#[allow(missing_docs, reason = "self-describing fields")]
pub struct CanonicalOptions {
pub instance: RuntimeComponentInstanceIndex,
pub string_encoding: StringEncoding,
pub memory: Option<MemoryId>,
pub realloc: Option<ReallocId>,
pub callback: Option<CallbackId>,
pub post_return: Option<PostReturnId>,
pub async_: bool,
}
#[allow(missing_docs, reason = "self-describing fields")]
pub struct Resource {
pub rep: WasmValType,
pub dtor: Option<CoreDef>,
pub instance: RuntimeComponentInstanceIndex,
}
pub struct Intern<K: EntityRef, V> {
intern_map: HashMap<V, K>,
key_map: PrimaryMap<K, V>,
}
impl<K, V> Intern<K, V>
where
K: EntityRef,
{
pub fn push(&mut self, value: V) -> K
where
V: Hash + Eq + Clone,
{
*self
.intern_map
.entry(value.clone())
.or_insert_with(|| self.key_map.push(value))
}
pub fn iter(&self) -> impl Iterator<Item = (K, &V)> {
self.key_map.iter()
}
}
impl<K: EntityRef, V> Index<K> for Intern<K, V> {
type Output = V;
fn index(&self, key: K) -> &V {
&self.key_map[key]
}
}
impl<K: EntityRef, V> Default for Intern<K, V> {
fn default() -> Intern<K, V> {
Intern {
intern_map: HashMap::new(),
key_map: PrimaryMap::new(),
}
}
}
impl ComponentDfg {
pub fn finish(
self,
wasmtime_types: &mut ComponentTypesBuilder,
wasmparser_types: wasmparser::types::TypesRef<'_>,
) -> Result<ComponentTranslation> {
let mut linearize = LinearizeDfg {
dfg: &self,
initializers: Vec::new(),
runtime_memories: Default::default(),
runtime_post_return: Default::default(),
runtime_reallocs: Default::default(),
runtime_callbacks: Default::default(),
runtime_instances: Default::default(),
num_lowerings: 0,
trampolines: Default::default(),
trampoline_defs: Default::default(),
trampoline_map: Default::default(),
};
for item in linearize.dfg.side_effects.iter() {
linearize.side_effect(item);
}
let mut export_items = PrimaryMap::new();
let mut exports = NameMap::default();
for (name, export) in self.exports.iter() {
let export =
linearize.export(export, &mut export_items, wasmtime_types, wasmparser_types)?;
exports.insert(name, &mut NameMapNoIntern, false, export)?;
}
Ok(ComponentTranslation {
trampolines: linearize.trampoline_defs,
component: Component {
exports,
export_items,
initializers: linearize.initializers,
trampolines: linearize.trampolines,
num_lowerings: linearize.num_lowerings,
num_runtime_memories: linearize.runtime_memories.len() as u32,
num_runtime_post_returns: linearize.runtime_post_return.len() as u32,
num_runtime_reallocs: linearize.runtime_reallocs.len() as u32,
num_runtime_callbacks: linearize.runtime_callbacks.len() as u32,
num_runtime_instances: linearize.runtime_instances.len() as u32,
imports: self.imports,
import_types: self.import_types,
num_runtime_component_instances: self.num_runtime_component_instances,
num_resource_tables: self.num_resource_tables,
num_future_tables: self.num_future_tables,
num_stream_tables: self.num_stream_tables,
num_error_context_tables: self.num_error_context_tables,
num_resources: (self.resources.len() + self.imported_resources.len()) as u32,
imported_resources: self.imported_resources,
defined_resource_instances: self
.resources
.iter()
.map(|(_, r)| r.instance)
.collect(),
},
})
}
pub fn resource_index(&self, defined: DefinedResourceIndex) -> ResourceIndex {
ResourceIndex::from_u32(defined.as_u32() + (self.imported_resources.len() as u32))
}
}
struct LinearizeDfg<'a> {
dfg: &'a ComponentDfg,
initializers: Vec<GlobalInitializer>,
trampolines: PrimaryMap<TrampolineIndex, ModuleInternedTypeIndex>,
trampoline_defs: PrimaryMap<TrampolineIndex, info::Trampoline>,
trampoline_map: HashMap<TrampolineIndex, TrampolineIndex>,
runtime_memories: HashMap<MemoryId, RuntimeMemoryIndex>,
runtime_reallocs: HashMap<ReallocId, RuntimeReallocIndex>,
runtime_callbacks: HashMap<CallbackId, RuntimeCallbackIndex>,
runtime_post_return: HashMap<PostReturnId, RuntimePostReturnIndex>,
runtime_instances: HashMap<RuntimeInstance, RuntimeInstanceIndex>,
num_lowerings: u32,
}
#[derive(Copy, Clone, Hash, Eq, PartialEq)]
enum RuntimeInstance {
Normal(InstanceId),
Adapter(AdapterModuleId),
}
impl LinearizeDfg<'_> {
fn side_effect(&mut self, effect: &SideEffect) {
match effect {
SideEffect::Instance(i) => {
self.instantiate(*i, &self.dfg.instances[*i]);
}
SideEffect::Resource(i) => {
self.resource(*i, &self.dfg.resources[*i]);
}
}
}
fn instantiate(&mut self, instance: InstanceId, args: &Instance) {
log::trace!("creating instance {instance:?}");
let instantiation = match args {
Instance::Static(index, args) => InstantiateModule::Static(
*index,
args.iter().map(|def| self.core_def(def)).collect(),
),
Instance::Import(index, args) => InstantiateModule::Import(
*index,
args.iter()
.map(|(module, values)| {
let values = values
.iter()
.map(|(name, def)| (name.clone(), self.core_def(def)))
.collect();
(module.clone(), values)
})
.collect(),
),
};
let index = RuntimeInstanceIndex::new(self.runtime_instances.len());
self.initializers
.push(GlobalInitializer::InstantiateModule(instantiation));
let prev = self
.runtime_instances
.insert(RuntimeInstance::Normal(instance), index);
assert!(prev.is_none());
}
fn resource(&mut self, index: DefinedResourceIndex, resource: &Resource) {
let dtor = resource.dtor.as_ref().map(|dtor| self.core_def(dtor));
self.initializers
.push(GlobalInitializer::Resource(info::Resource {
dtor,
index,
rep: resource.rep,
instance: resource.instance,
}));
}
fn export(
&mut self,
export: &Export,
items: &mut PrimaryMap<ExportIndex, info::Export>,
wasmtime_types: &mut ComponentTypesBuilder,
wasmparser_types: wasmparser::types::TypesRef<'_>,
) -> Result<ExportIndex> {
let item = match export {
Export::LiftedFunction { ty, func, options } => {
let func = self.core_def(func);
let options = self.options(options);
info::Export::LiftedFunction {
ty: *ty,
func,
options,
}
}
Export::ModuleStatic { ty, index } => info::Export::ModuleStatic {
ty: wasmtime_types.convert_module(wasmparser_types, *ty)?,
index: *index,
},
Export::ModuleImport { ty, import } => info::Export::ModuleImport {
ty: *ty,
import: *import,
},
Export::Instance { ty, exports } => info::Export::Instance {
ty: *ty,
exports: {
let mut map = NameMap::default();
for (name, export) in exports {
let export =
self.export(export, items, wasmtime_types, wasmparser_types)?;
map.insert(name, &mut NameMapNoIntern, false, export)?;
}
map
},
},
Export::Type(def) => info::Export::Type(*def),
};
Ok(items.push(item))
}
fn options(&mut self, options: &CanonicalOptions) -> info::CanonicalOptions {
let memory = options.memory.map(|mem| self.runtime_memory(mem));
let realloc = options.realloc.map(|mem| self.runtime_realloc(mem));
let callback = options.callback.map(|mem| self.runtime_callback(mem));
let post_return = options.post_return.map(|mem| self.runtime_post_return(mem));
info::CanonicalOptions {
instance: options.instance,
string_encoding: options.string_encoding,
memory,
realloc,
callback,
post_return,
async_: options.async_,
}
}
fn runtime_memory(&mut self, mem: MemoryId) -> RuntimeMemoryIndex {
self.intern(
mem,
|me| &mut me.runtime_memories,
|me, mem| me.core_export(&me.dfg.memories[mem]),
|index, export| GlobalInitializer::ExtractMemory(ExtractMemory { index, export }),
)
}
fn runtime_realloc(&mut self, realloc: ReallocId) -> RuntimeReallocIndex {
self.intern(
realloc,
|me| &mut me.runtime_reallocs,
|me, realloc| me.core_def(&me.dfg.reallocs[realloc]),
|index, def| GlobalInitializer::ExtractRealloc(ExtractRealloc { index, def }),
)
}
fn runtime_callback(&mut self, callback: CallbackId) -> RuntimeCallbackIndex {
self.intern(
callback,
|me| &mut me.runtime_callbacks,
|me, callback| me.core_def(&me.dfg.callbacks[callback]),
|index, def| GlobalInitializer::ExtractCallback(ExtractCallback { index, def }),
)
}
fn runtime_post_return(&mut self, post_return: PostReturnId) -> RuntimePostReturnIndex {
self.intern(
post_return,
|me| &mut me.runtime_post_return,
|me, post_return| me.core_def(&me.dfg.post_returns[post_return]),
|index, def| GlobalInitializer::ExtractPostReturn(ExtractPostReturn { index, def }),
)
}
fn core_def(&mut self, def: &CoreDef) -> info::CoreDef {
match def {
CoreDef::Export(e) => info::CoreDef::Export(self.core_export(e)),
CoreDef::InstanceFlags(i) => info::CoreDef::InstanceFlags(*i),
CoreDef::Adapter(id) => info::CoreDef::Export(self.adapter(*id)),
CoreDef::Trampoline(index) => info::CoreDef::Trampoline(self.trampoline(*index)),
}
}
fn trampoline(&mut self, index: TrampolineIndex) -> TrampolineIndex {
if let Some(idx) = self.trampoline_map.get(&index) {
return *idx;
}
let (signature, trampoline) = &self.dfg.trampolines[index];
let trampoline = match trampoline {
Trampoline::LowerImport {
import,
options,
lower_ty,
} => {
let index = LoweredIndex::from_u32(self.num_lowerings);
self.num_lowerings += 1;
self.initializers.push(GlobalInitializer::LowerImport {
index,
import: *import,
});
info::Trampoline::LowerImport {
index,
options: self.options(options),
lower_ty: *lower_ty,
}
}
Trampoline::Transcoder {
op,
from,
from64,
to,
to64,
} => info::Trampoline::Transcoder {
op: *op,
from: self.runtime_memory(*from),
from64: *from64,
to: self.runtime_memory(*to),
to64: *to64,
},
Trampoline::AlwaysTrap => info::Trampoline::AlwaysTrap,
Trampoline::ResourceNew(ty) => info::Trampoline::ResourceNew(*ty),
Trampoline::ResourceDrop(ty) => info::Trampoline::ResourceDrop(*ty),
Trampoline::ResourceRep(ty) => info::Trampoline::ResourceRep(*ty),
Trampoline::TaskBackpressure { instance } => info::Trampoline::TaskBackpressure {
instance: *instance,
},
Trampoline::TaskReturn => info::Trampoline::TaskReturn,
Trampoline::TaskWait {
instance,
async_,
memory,
} => info::Trampoline::TaskWait {
instance: *instance,
async_: *async_,
memory: self.runtime_memory(*memory),
},
Trampoline::TaskPoll {
instance,
async_,
memory,
} => info::Trampoline::TaskPoll {
instance: *instance,
async_: *async_,
memory: self.runtime_memory(*memory),
},
Trampoline::TaskYield { async_ } => info::Trampoline::TaskYield { async_: *async_ },
Trampoline::SubtaskDrop { instance } => info::Trampoline::SubtaskDrop {
instance: *instance,
},
Trampoline::StreamNew { ty } => info::Trampoline::StreamNew { ty: *ty },
Trampoline::StreamRead { ty, options } => info::Trampoline::StreamRead {
ty: *ty,
options: self.options(options),
},
Trampoline::StreamWrite { ty, options } => info::Trampoline::StreamWrite {
ty: *ty,
options: self.options(options),
},
Trampoline::StreamCancelRead { ty, async_ } => info::Trampoline::StreamCancelRead {
ty: *ty,
async_: *async_,
},
Trampoline::StreamCancelWrite { ty, async_ } => info::Trampoline::StreamCancelWrite {
ty: *ty,
async_: *async_,
},
Trampoline::StreamCloseReadable { ty } => {
info::Trampoline::StreamCloseReadable { ty: *ty }
}
Trampoline::StreamCloseWritable { ty } => {
info::Trampoline::StreamCloseWritable { ty: *ty }
}
Trampoline::FutureNew { ty } => info::Trampoline::FutureNew { ty: *ty },
Trampoline::FutureRead { ty, options } => info::Trampoline::FutureRead {
ty: *ty,
options: self.options(options),
},
Trampoline::FutureWrite { ty, options } => info::Trampoline::FutureWrite {
ty: *ty,
options: self.options(options),
},
Trampoline::FutureCancelRead { ty, async_ } => info::Trampoline::FutureCancelRead {
ty: *ty,
async_: *async_,
},
Trampoline::FutureCancelWrite { ty, async_ } => info::Trampoline::FutureCancelWrite {
ty: *ty,
async_: *async_,
},
Trampoline::FutureCloseReadable { ty } => {
info::Trampoline::FutureCloseReadable { ty: *ty }
}
Trampoline::FutureCloseWritable { ty } => {
info::Trampoline::FutureCloseWritable { ty: *ty }
}
Trampoline::ErrorContextNew { ty, options } => info::Trampoline::ErrorContextNew {
ty: *ty,
options: self.options(options),
},
Trampoline::ErrorContextDebugMessage { ty, options } => {
info::Trampoline::ErrorContextDebugMessage {
ty: *ty,
options: self.options(options),
}
}
Trampoline::ErrorContextDrop { ty } => info::Trampoline::ErrorContextDrop { ty: *ty },
Trampoline::ResourceTransferOwn => info::Trampoline::ResourceTransferOwn,
Trampoline::ResourceTransferBorrow => info::Trampoline::ResourceTransferBorrow,
Trampoline::ResourceEnterCall => info::Trampoline::ResourceEnterCall,
Trampoline::ResourceExitCall => info::Trampoline::ResourceExitCall,
Trampoline::AsyncEnterCall => info::Trampoline::AsyncEnterCall,
Trampoline::AsyncExitCall {
callback,
post_return,
} => info::Trampoline::AsyncExitCall {
callback: callback.map(|v| self.runtime_callback(v)),
post_return: post_return.map(|v| self.runtime_post_return(v)),
},
Trampoline::FutureTransfer => info::Trampoline::FutureTransfer,
Trampoline::StreamTransfer => info::Trampoline::StreamTransfer,
Trampoline::ErrorContextTransfer => info::Trampoline::ErrorContextTransfer,
};
let i1 = self.trampolines.push(*signature);
let i2 = self.trampoline_defs.push(trampoline);
assert_eq!(i1, i2);
self.trampoline_map.insert(index, i1);
i1
}
fn core_export<T>(&mut self, export: &CoreExport<T>) -> info::CoreExport<T>
where
T: Clone,
{
let instance = export.instance;
log::trace!("referencing export of {instance:?}");
info::CoreExport {
instance: self.runtime_instances[&RuntimeInstance::Normal(instance)],
item: export.item.clone(),
}
}
fn adapter(&mut self, adapter: AdapterId) -> info::CoreExport<EntityIndex> {
let (adapter_module, entity_index) = self.dfg.adapter_partitionings[adapter];
let instance = self.adapter_module(adapter_module);
info::CoreExport {
instance,
item: ExportItem::Index(entity_index),
}
}
fn adapter_module(&mut self, adapter_module: AdapterModuleId) -> RuntimeInstanceIndex {
self.intern(
RuntimeInstance::Adapter(adapter_module),
|me| &mut me.runtime_instances,
|me, _| {
log::debug!("instantiating {adapter_module:?}");
let (module_index, args) = &me.dfg.adapter_modules[adapter_module];
let args = args.iter().map(|arg| me.core_def(arg)).collect();
let instantiate = InstantiateModule::Static(*module_index, args);
GlobalInitializer::InstantiateModule(instantiate)
},
|_, init| init,
)
}
fn intern<K, V, T>(
&mut self,
key: K,
map: impl Fn(&mut Self) -> &mut HashMap<K, V>,
generate: impl FnOnce(&mut Self, K) -> T,
init: impl FnOnce(V, T) -> GlobalInitializer,
) -> V
where
K: Hash + Eq + Copy,
V: EntityRef,
{
if let Some(val) = map(self).get(&key) {
return *val;
}
let tmp = generate(self, key);
let index = V::new(map(self).len());
self.initializers.push(init(index, tmp));
let prev = map(self).insert(key, index);
assert!(prev.is_none());
index
}
}