Add a dataflow-based representation of components (#4597)
* Add a dataflow-based representation of components This commit updates the inlining phase of compiling a component to creating a dataflow-based representation of a component instead of creating a final `Component` with a linear list of initializers. This dataflow graph is then linearized in a final step to create the actual final `Component`. The motivation for this commit stems primarily from my work implementing strings in fused adapters. In doing this my plan is to defer most low-level transcoding to the host itself rather than implementing that in the core wasm adapter modules. This means that small cranelift-generated trampolines will be used for adapter modules to call which then call "transcoding libcalls". The cranelift-generated trampolines will get raw pointers into linear memory and pass those to the libcall which core wasm doesn't have access to when passing arguments to an import. Implementing this with the previous representation of a `Component` was becoming too tricky to bear. The initialization of a transcoder needed to happen at just the right time: before the adapter module which needed it was instantiated but after the linear memories referenced had been extracted into the `VMComponentContext`. The difficulty here is further compounded by the current adapter module injection pass already being quite complicated. Adapter modules are already renumbering the index space of runtime instances and shuffling items around in the `GlobalInitializer` list. Perhaps the worst part of this was that memories could already be referenced by host function imports or exports to the host, and if adapters referenced the same memory it shouldn't be referenced twice in the component. This meant that `ExtractMemory` initializers ideally needed to be shuffled around in the initializer list to happen as early as possible instead of wherever they happened to show up during translation. Overall I did my best to implement the transcoders but everything always came up short. I have decided to throw my hands up in the air and try a completely different approach to this, namely the dataflow-based representation in this commit. This makes it much easier to edit the component after initial translation for injection of adapters, injection of transcoders, adding dependencies on possibly-already-existing items, etc. The adapter module partitioning pass in this commit was greatly simplified to something which I believe is functionally equivalent but is probably an order of magnitude easier to understand. The biggest downside of this representation I believe is having a duplicate representation of a component. The `component::info` was largely duplicated into the `component::dfg` module in this commit. Personally though I think this is a more appropriate tradeoff than before because it's very easy to reason about "convert representation A to B" code whereas it was very difficult to reason about shuffling around `GlobalInitializer` items in optimal fashions. This may also have a cost at compile-time in terms of shuffling data around, but my hope is that we have lots of other low-hanging fruit to optimize if it ever comes to that which allows keeping this easier-to-understand representation. Finally, to reiterate, the final representation of components is not changed by this PR. To the runtime internals everything is still the same. * Fix compile of factc
This commit is contained in:
@@ -101,33 +101,27 @@
|
||||
//! algorithm is a one-pass approach to partitioning everything into adapter
|
||||
//! modules.
|
||||
//!
|
||||
//! As the `GlobalInitializer` list is iterated over the last adapter module
|
||||
//! created is recorded. Each adapter module, when created, records the index
|
||||
//! space limits at the time of its creation. If a new adapter is found which
|
||||
//! depends on an item after the original adapter module was created then the
|
||||
//! prior adapter module is finished and a new one is started. Adapters only
|
||||
//! ever attempt to get inserted into the most recent adapter module, no
|
||||
//! searching is currently done to try to fit adapters into a prior adapter
|
||||
//! module.
|
||||
//! Adapters were indentified in-order as part of the inlining phase of
|
||||
//! translation where we're guaranteed that once an adapter is identified
|
||||
//! it can't depend on anything identified later. The pass implemented here is
|
||||
//! to visit all transitive dependencies of an adapter. If one of the
|
||||
//! dependencies of an adapter is an adapter in the current adapter module
|
||||
//! being built then the current module is finished and a new adapter module is
|
||||
//! started. This should quickly parition adapters into contiugous chunks of
|
||||
//! their index space which can be in adapter modules together.
|
||||
//!
|
||||
//! During this remapping process the `RuntimeInstanceIndex` for all instances
|
||||
//! is also updated. Insertion of an adapter module will increase all further
|
||||
//! instance indices by one so this must be accounted for in various
|
||||
//! references.
|
||||
//! There's probably more general algorithms for this but for now this should be
|
||||
//! fast enough as it's "just" a linear pass. As we get more components over
|
||||
//! time this may want to be revisited if too many adapter modules are being
|
||||
//! created.
|
||||
|
||||
use crate::component::translate::*;
|
||||
use crate::fact::Module;
|
||||
use std::collections::HashSet;
|
||||
use wasmparser::WasmFeatures;
|
||||
|
||||
/// Information about fused adapters within a component.
|
||||
#[derive(Default)]
|
||||
pub struct Adapters {
|
||||
/// List of all fused adapters identified which are assigned an index and
|
||||
/// contain various metadata about them as well.
|
||||
pub adapters: PrimaryMap<AdapterIndex, Adapter>,
|
||||
}
|
||||
|
||||
/// Metadata information about a fused adapter.
|
||||
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
|
||||
pub struct Adapter {
|
||||
/// The type used when the original core wasm function was lifted.
|
||||
///
|
||||
@@ -145,12 +139,12 @@ pub struct Adapter {
|
||||
/// Canonical ABI options used when the function was lowered.
|
||||
pub lower_options: AdapterOptions,
|
||||
/// The original core wasm function which was lifted.
|
||||
pub func: CoreDef,
|
||||
pub func: dfg::CoreDef,
|
||||
}
|
||||
|
||||
/// Configuration options which can be specified as part of the canonical ABI
|
||||
/// in the component model.
|
||||
#[derive(Clone)]
|
||||
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
|
||||
pub struct AdapterOptions {
|
||||
/// The Wasmtime-assigned component instance index where the options were
|
||||
/// originally specified.
|
||||
@@ -158,503 +152,230 @@ pub struct AdapterOptions {
|
||||
/// How strings are encoded.
|
||||
pub string_encoding: StringEncoding,
|
||||
/// An optional memory definition supplied.
|
||||
pub memory: Option<CoreExport<MemoryIndex>>,
|
||||
pub memory: Option<dfg::CoreExport<MemoryIndex>>,
|
||||
/// If `memory` is specified, whether it's a 64-bit memory.
|
||||
pub memory64: bool,
|
||||
/// An optional definition of `realloc` to used.
|
||||
pub realloc: Option<CoreDef>,
|
||||
pub realloc: Option<dfg::CoreDef>,
|
||||
/// An optional definition of a `post-return` to use.
|
||||
pub post_return: Option<CoreDef>,
|
||||
pub post_return: Option<dfg::CoreDef>,
|
||||
}
|
||||
|
||||
impl<'data> Translator<'_, 'data> {
|
||||
/// Modifies the list of `GlobalInitializer` entries within a
|
||||
/// `Component`with `InstantiateModule::Adapter` entries where necessary.
|
||||
///
|
||||
/// This is the entrypoint of functionality within this module which
|
||||
/// performs all the work of identifying adapter usages and organizing
|
||||
/// everything into adapter modules.
|
||||
pub(super) fn insert_adapter_module_initializers(
|
||||
&mut self,
|
||||
component: &mut Component,
|
||||
adapters: &mut Adapters,
|
||||
) {
|
||||
let mut state = PartitionAdapterModules {
|
||||
to_process: Vec::new(),
|
||||
cur_idx: 0,
|
||||
adapter_modules: PrimaryMap::new(),
|
||||
items: DefinedItems::default(),
|
||||
instance_map: PrimaryMap::with_capacity(component.num_runtime_instances as usize),
|
||||
};
|
||||
state.run(component, adapters);
|
||||
|
||||
// Next, in reverse, insert all of the adapter modules into the actual
|
||||
// initializer list. Note that the iteration order is important here to
|
||||
// ensure that all the `at_initializer_index` listed is valid for each
|
||||
// entry.
|
||||
let mut adapter_map = PrimaryMap::with_capacity(adapters.adapters.len());
|
||||
for _ in adapters.adapters.iter() {
|
||||
adapter_map.push(None);
|
||||
}
|
||||
for (_, module) in state.adapter_modules.into_iter().rev() {
|
||||
let index = module.at_initializer_index;
|
||||
let instantiate = self.compile_adapter_module(module, adapters, &mut adapter_map);
|
||||
let init = GlobalInitializer::InstantiateModule(instantiate);
|
||||
component.initializers.insert(index, init);
|
||||
///
|
||||
/// This will mutate the provided `component` in-place and fill out the dfg
|
||||
/// metadata for adapter modules.
|
||||
pub(super) fn partition_adapter_modules(&mut self, component: &mut dfg::ComponentDfg) {
|
||||
// Visit each adapter, in order of its original definition, during the
|
||||
// paritioning. This allows for the guarantee that dependencies are
|
||||
// visited in a topological fashion ideally.
|
||||
let mut state = PartitionAdapterModules::default();
|
||||
for (id, adapter) in component.adapters.iter() {
|
||||
state.adapter(component, id, adapter);
|
||||
}
|
||||
state.finish_adapter_module();
|
||||
|
||||
// Finally all references to `CoreDef::Adapter` are rewritten to their
|
||||
// corresponding `CoreDef::Export` as identified within `adapter_map`.
|
||||
for init in component.initializers.iter_mut() {
|
||||
map_adapter_references(init, &adapter_map);
|
||||
}
|
||||
}
|
||||
|
||||
fn compile_adapter_module(
|
||||
&mut self,
|
||||
module_parts: AdapterModuleParts,
|
||||
adapters: &Adapters,
|
||||
adapter_map: &mut PrimaryMap<AdapterIndex, Option<CoreExport<EntityIndex>>>,
|
||||
) -> InstantiateModule {
|
||||
// Use the `fact::Module` builder to create a new wasm module which
|
||||
// represents all of the adapters specified here.
|
||||
let mut module = Module::new(
|
||||
self.types.component_types(),
|
||||
self.tunables.debug_adapter_modules,
|
||||
);
|
||||
let mut names = Vec::with_capacity(module_parts.adapters.len());
|
||||
for adapter in module_parts.adapters.iter() {
|
||||
let name = format!("adapter{}", adapter.as_u32());
|
||||
module.adapt(&name, &adapters.adapters[*adapter]);
|
||||
names.push(name);
|
||||
}
|
||||
let wasm = module.encode();
|
||||
let args = module.imports().to_vec();
|
||||
|
||||
// Extend the lifetime of the owned `wasm: Vec<u8>` on the stack to a
|
||||
// higher scope defined by our original caller. That allows to transform
|
||||
// `wasm` into `&'data [u8]` which is much easier to work with here.
|
||||
let wasm = &*self.scope_vec.push(wasm);
|
||||
if log::log_enabled!(log::Level::Trace) {
|
||||
match wasmprinter::print_bytes(wasm) {
|
||||
Ok(s) => log::trace!("generated adapter module:\n{}", s),
|
||||
Err(e) => log::trace!("failed to print adapter module: {}", e),
|
||||
// Now that all adapters have been partitioned into modules this loop
|
||||
// generates a core wasm module for each adapter module, translates
|
||||
// the module using standard core wasm translation, and then fills out
|
||||
// the dfg metadata for each adapter.
|
||||
for (module_id, adapter_module) in state.adapter_modules.iter() {
|
||||
let mut module = Module::new(
|
||||
self.types.component_types(),
|
||||
self.tunables.debug_adapter_modules,
|
||||
);
|
||||
let mut names = Vec::with_capacity(adapter_module.adapters.len());
|
||||
for adapter in adapter_module.adapters.iter() {
|
||||
let name = format!("adapter{}", adapter.as_u32());
|
||||
module.adapt(&name, &component.adapters[*adapter]);
|
||||
names.push(name);
|
||||
}
|
||||
}
|
||||
let wasm = module.encode();
|
||||
let args = module.imports().to_vec();
|
||||
|
||||
// With the wasm binary this is then pushed through general translation,
|
||||
// validation, etc. Note that multi-memory is specifically enabled here
|
||||
// since the adapter module is highly likely to use that if anything is
|
||||
// actually indirected through memory.
|
||||
let mut validator = Validator::new_with_features(WasmFeatures {
|
||||
multi_memory: true,
|
||||
..*self.validator.features()
|
||||
});
|
||||
let translation = ModuleEnvironment::new(
|
||||
self.tunables,
|
||||
&mut validator,
|
||||
self.types.module_types_builder(),
|
||||
)
|
||||
.translate(Parser::new(0), wasm)
|
||||
.expect("invalid adapter module generated");
|
||||
// Extend the lifetime of the owned `wasm: Vec<u8>` on the stack to
|
||||
// a higher scope defined by our original caller. That allows to
|
||||
// transform `wasm` into `&'data [u8]` which is much easier to work
|
||||
// with here.
|
||||
let wasm = &*self.scope_vec.push(wasm);
|
||||
if log::log_enabled!(log::Level::Trace) {
|
||||
match wasmprinter::print_bytes(wasm) {
|
||||
Ok(s) => log::trace!("generated adapter module:\n{}", s),
|
||||
Err(e) => log::trace!("failed to print adapter module: {}", e),
|
||||
}
|
||||
}
|
||||
|
||||
// And with all metadata available about the generated module a map can
|
||||
// be built from adapter index to the precise export in the module that
|
||||
// was generated.
|
||||
for (adapter, name) in module_parts.adapters.iter().zip(&names) {
|
||||
assert!(adapter_map[*adapter].is_none());
|
||||
let index = translation.module.exports[name];
|
||||
adapter_map[*adapter] = Some(CoreExport {
|
||||
instance: module_parts.index,
|
||||
item: ExportItem::Index(index),
|
||||
// With the wasm binary this is then pushed through general
|
||||
// translation, validation, etc. Note that multi-memory is
|
||||
// specifically enabled here since the adapter module is highly
|
||||
// likely to use that if anything is actually indirected through
|
||||
// memory.
|
||||
let mut validator = Validator::new_with_features(WasmFeatures {
|
||||
multi_memory: true,
|
||||
..*self.validator.features()
|
||||
});
|
||||
}
|
||||
let translation = ModuleEnvironment::new(
|
||||
self.tunables,
|
||||
&mut validator,
|
||||
self.types.module_types_builder(),
|
||||
)
|
||||
.translate(Parser::new(0), wasm)
|
||||
.expect("invalid adapter module generated");
|
||||
|
||||
// Finally the module translation is saved in the list of static
|
||||
// modules to get fully compiled later and the `InstantiateModule`
|
||||
// representation of this adapter module is returned.
|
||||
let static_index = self.static_modules.push(translation);
|
||||
InstantiateModule::Static(static_index, args.into())
|
||||
// Record, for each adapter in this adapter module, the module that
|
||||
// the adapter was placed within as well as the function index of
|
||||
// the adapter in the wasm module generated. Note that adapters are
|
||||
// paritioned in-order so we're guaranteed to push the adapters
|
||||
// in-order here as well. (with an assert to double-check)
|
||||
for (adapter, name) in adapter_module.adapters.iter().zip(&names) {
|
||||
let index = translation.module.exports[name];
|
||||
let i = component.adapter_paritionings.push((module_id, index));
|
||||
assert_eq!(i, *adapter);
|
||||
}
|
||||
|
||||
// Finally the metadata necessary to instantiate this adapter
|
||||
// module is also recorded in the dfg. This metadata will be used
|
||||
// to generate `GlobalInitializer` entries during the linearization
|
||||
// final phase.
|
||||
let static_index = self.static_modules.push(translation);
|
||||
let id = component.adapter_modules.push((static_index, args.into()));
|
||||
assert_eq!(id, module_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct PartitionAdapterModules {
|
||||
/// Stack of remaining elements to process
|
||||
to_process: Vec<ToProcess>,
|
||||
/// The next adapter module that's being created. This may be empty.
|
||||
next_module: AdapterModuleInProgress,
|
||||
|
||||
/// Index of the current `GlobalInitializer` being processed.
|
||||
cur_idx: usize,
|
||||
/// The set of items which are known to be defined which the adapter module
|
||||
/// in progress is allowed to depend on.
|
||||
defined_items: HashSet<Def>,
|
||||
|
||||
/// Information about all fused adapter modules that have been created so
|
||||
/// far.
|
||||
/// Finished adapter modules that won't be added to.
|
||||
///
|
||||
/// This is modified whenever a fused adapter is used.
|
||||
adapter_modules: PrimaryMap<AdapterModuleIndex, AdapterModuleParts>,
|
||||
|
||||
/// Map from "old runtime instance index" to "new runtime instance index".
|
||||
///
|
||||
/// This map is populated when instances are created to account for prior
|
||||
/// adapter modules having been created. This effectively tracks an offset
|
||||
/// for each index.
|
||||
instance_map: PrimaryMap<RuntimeInstanceIndex, RuntimeInstanceIndex>,
|
||||
|
||||
/// Current limits of index spaces.
|
||||
items: DefinedItems,
|
||||
/// In theory items could be added to preexisting modules here but to keep
|
||||
/// this pass linear this is never modified after insertion.
|
||||
adapter_modules: PrimaryMap<dfg::AdapterModuleId, AdapterModuleInProgress>,
|
||||
}
|
||||
|
||||
/// Entries in the `PartitionAdapterModules::to_process` array.
|
||||
enum ToProcess {
|
||||
/// An adapter needs its own dependencies processed. This will map the
|
||||
/// fields of `Adapter` above for the specified index.
|
||||
Adapter(AdapterIndex),
|
||||
/// An adapter has had its dependencies fully processed (transitively) and
|
||||
/// the adapter now needs to be inserted into a module.
|
||||
AddAdapterToModule(AdapterIndex),
|
||||
/// A global initializer needs to be remapped.
|
||||
GlobalInitializer(usize),
|
||||
/// An export needs to be remapped.
|
||||
Export(usize),
|
||||
/// A global initializer which creates an instance has had all of its
|
||||
/// arguments processed and now the instance number needs to be recorded.
|
||||
PushInstance,
|
||||
#[derive(Default)]
|
||||
struct AdapterModuleInProgress {
|
||||
/// The adapters which have been placed into this module.
|
||||
adapters: Vec<dfg::AdapterId>,
|
||||
}
|
||||
|
||||
/// Custom index type used exclusively for the `adapter_modules` map above.
|
||||
#[derive(Copy, Clone, PartialEq, Eq)]
|
||||
struct AdapterModuleIndex(u32);
|
||||
cranelift_entity::entity_impl!(AdapterModuleIndex);
|
||||
|
||||
struct AdapterModuleParts {
|
||||
/// The runtime index that will be assigned to this adapter module when it's
|
||||
/// instantiated.
|
||||
index: RuntimeInstanceIndex,
|
||||
/// The index in the `GlobalInitializer` list that this adapter module will
|
||||
/// get inserted at.
|
||||
at_initializer_index: usize,
|
||||
/// Items that were available when this adapter module was created.
|
||||
items_at_initializer: DefinedItems,
|
||||
/// Adapters that have been inserted into this module, guaranteed to be
|
||||
/// non-empty.
|
||||
adapters: Vec<AdapterIndex>,
|
||||
}
|
||||
|
||||
#[derive(Default, Clone)]
|
||||
struct DefinedItems {
|
||||
/// Number of core wasm instances created so far.
|
||||
///
|
||||
/// Note that this does not count adapter modules created, only the
|
||||
/// instance index space before adapter modules were inserted.
|
||||
instances: u32,
|
||||
/// Number of host-lowered functions seen so far.
|
||||
lowerings: u32,
|
||||
/// Number of "always trap" functions seen so far.
|
||||
always_trap: u32,
|
||||
/// Map of whether adapters have been inserted into an adapter module yet.
|
||||
adapter_to_module: PrimaryMap<AdapterIndex, Option<AdapterModuleIndex>>,
|
||||
/// Items that adapters can depend on.
|
||||
///
|
||||
/// Note that this is somewhat of a flat list and is intended to mostly model
|
||||
/// core wasm instances which are side-effectful unlike other host items like
|
||||
/// lowerings or always-trapping functions.
|
||||
#[derive(Copy, Clone, Hash, Eq, PartialEq)]
|
||||
enum Def {
|
||||
Adapter(dfg::AdapterId),
|
||||
Instance(dfg::InstanceId),
|
||||
}
|
||||
|
||||
impl PartitionAdapterModules {
|
||||
/// Process the list of global `initializers` and partitions adapters into
|
||||
/// adapter modules which will get inserted into the provided list in a
|
||||
/// later pass.
|
||||
fn run(&mut self, component: &mut Component, adapters: &mut Adapters) {
|
||||
// This function is designed to be an iterative loop which models
|
||||
// recursion in the `self.to_process` array instead of on the host call
|
||||
// stack. The reason for this is that adapters need recursive processing
|
||||
// since the argument to an adapter can hypothetically be an adapter
|
||||
// itself (albeit silly but still valid). This recursive nature of
|
||||
// adapters means that a component could be crafted to have an
|
||||
// arbitrarily deep recursive dependeny chain for any one adapter. To
|
||||
// avoid consuming host stack space the storage for this dependency
|
||||
// chain is placed on the heap.
|
||||
fn adapter(&mut self, dfg: &dfg::ComponentDfg, id: dfg::AdapterId, adapter: &Adapter) {
|
||||
// Visit all dependencies of this adapter and if anything depends on
|
||||
// the current adapter module in progress then a new adapter module is
|
||||
// started.
|
||||
self.adapter_options(dfg, &adapter.lift_options);
|
||||
self.adapter_options(dfg, &adapter.lower_options);
|
||||
self.core_def(dfg, &adapter.func);
|
||||
|
||||
// With all dependencies visited this adapter is added to the next
|
||||
// module.
|
||||
//
|
||||
// The `self.to_process` list is a FIFO queue of what to process next.
|
||||
// Initially seeded with all the global initializer indexes this is
|
||||
// pushed to during processing to recursively handle adapters and
|
||||
// similar.
|
||||
assert!(self.to_process.is_empty());
|
||||
assert!(self.items.adapter_to_module.is_empty());
|
||||
// This will either get added the preexisting module if this adapter
|
||||
// didn't depend on anything in that module itself or it will be added
|
||||
// to a fresh module if this adapter depended on something that the
|
||||
// current adapter module created.
|
||||
log::debug!("adding {id:?} to adapter module {adapter:#?}");
|
||||
self.next_module.adapters.push(id);
|
||||
}
|
||||
|
||||
// Initially record all adapters as having no module which will get
|
||||
// filled in over time.
|
||||
for _ in adapters.adapters.iter() {
|
||||
self.items.adapter_to_module.push(None);
|
||||
fn adapter_options(&mut self, dfg: &dfg::ComponentDfg, options: &AdapterOptions) {
|
||||
if let Some(memory) = &options.memory {
|
||||
self.core_export(dfg, memory);
|
||||
}
|
||||
|
||||
// Seed the worklist of what to process with the list of global
|
||||
// initializers and exports, but in reverse order since this is a LIFO
|
||||
// queue. Afterwards all of the items to process are handled in a loop.
|
||||
for i in (0..component.exports.len()).rev() {
|
||||
self.to_process.push(ToProcess::Export(i));
|
||||
if let Some(def) = &options.realloc {
|
||||
self.core_def(dfg, def);
|
||||
}
|
||||
for i in (0..component.initializers.len()).rev() {
|
||||
self.to_process.push(ToProcess::GlobalInitializer(i));
|
||||
}
|
||||
|
||||
while let Some(to_process) = self.to_process.pop() {
|
||||
match to_process {
|
||||
ToProcess::GlobalInitializer(i) => {
|
||||
assert!(i <= self.cur_idx + 1);
|
||||
self.cur_idx = i;
|
||||
self.global_initializer(&mut component.initializers[i]);
|
||||
}
|
||||
|
||||
ToProcess::Export(i) => {
|
||||
self.cur_idx = component.initializers.len();
|
||||
self.export(&mut component.exports[i]);
|
||||
}
|
||||
|
||||
ToProcess::PushInstance => {
|
||||
// A new runtime instance is being created here so insert an
|
||||
// entry into the remapping map for instance indexes. This
|
||||
// instance's index is offset by the number of adapter modules
|
||||
// created prior.
|
||||
self.instance_map
|
||||
.push(RuntimeInstanceIndex::from_u32(self.items.instances));
|
||||
self.items.instances += 1;
|
||||
}
|
||||
|
||||
ToProcess::Adapter(idx) => {
|
||||
let info = &mut adapters.adapters[idx];
|
||||
self.process_core_def(&mut info.func);
|
||||
self.process_options(&mut info.lift_options);
|
||||
self.process_options(&mut info.lower_options);
|
||||
}
|
||||
|
||||
ToProcess::AddAdapterToModule(idx) => {
|
||||
// If this adapter has already been assigned to a module
|
||||
// then there's no need to do anything else here.
|
||||
//
|
||||
// This can happen when a core wasm instance is created with
|
||||
// an adapter as the argument multiple times for example.
|
||||
if self.items.adapter_to_module[idx].is_some() {
|
||||
continue;
|
||||
}
|
||||
|
||||
// If an adapter module is already in progress and
|
||||
// everything this adapter depends on was available at the
|
||||
// time of creation of that adapter module, then this
|
||||
// adapter can go in that module.
|
||||
if let Some((module_idx, module)) = self.adapter_modules.last_mut() {
|
||||
let info = &adapters.adapters[idx];
|
||||
if module.items_at_initializer.contains(info) {
|
||||
self.items.adapter_to_module[idx] = Some(module_idx);
|
||||
module.adapters.push(idx);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// ... otherwise a new adapter module is started. Note that
|
||||
// the instance count is bumped here to model the
|
||||
// instantiation of the adapter module.
|
||||
let module = AdapterModuleParts {
|
||||
index: RuntimeInstanceIndex::from_u32(self.items.instances),
|
||||
at_initializer_index: self.cur_idx,
|
||||
items_at_initializer: self.items.clone(),
|
||||
adapters: vec![idx],
|
||||
};
|
||||
let index = self.adapter_modules.push(module);
|
||||
self.items.adapter_to_module[idx] = Some(index);
|
||||
self.items.instances += 1;
|
||||
}
|
||||
}
|
||||
if let Some(def) = &options.post_return {
|
||||
self.core_def(dfg, def);
|
||||
}
|
||||
}
|
||||
|
||||
fn global_initializer(&mut self, init: &mut GlobalInitializer) {
|
||||
match init {
|
||||
GlobalInitializer::InstantiateModule(module) => {
|
||||
// Enqueue a bump of the instance count, but this only happens
|
||||
// after all the arguments have been processed below. Given the
|
||||
// LIFO nature of `self.to_process` this will be handled after
|
||||
// all arguments are recursively processed.
|
||||
self.to_process.push(ToProcess::PushInstance);
|
||||
|
||||
match module {
|
||||
InstantiateModule::Static(_, args) => {
|
||||
for def in args.iter_mut() {
|
||||
self.process_core_def(def);
|
||||
}
|
||||
}
|
||||
InstantiateModule::Import(_, args) => {
|
||||
for (_, map) in args {
|
||||
for (_, def) in map {
|
||||
self.process_core_def(def);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
GlobalInitializer::ExtractRealloc(e) => self.process_core_def(&mut e.def),
|
||||
GlobalInitializer::ExtractPostReturn(e) => self.process_core_def(&mut e.def),
|
||||
|
||||
// Update items available as they're defined
|
||||
GlobalInitializer::LowerImport(_) => self.items.lowerings += 1,
|
||||
GlobalInitializer::AlwaysTrap(_) => self.items.always_trap += 1,
|
||||
|
||||
// Nothing is defined or referenced by these initializers that we
|
||||
// need to worry about here.
|
||||
GlobalInitializer::ExtractMemory(_) => {}
|
||||
GlobalInitializer::SaveStaticModule(_) => {}
|
||||
GlobalInitializer::SaveModuleImport(_) => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn export(&mut self, export: &mut Export) {
|
||||
match export {
|
||||
Export::LiftedFunction { func, .. } => {
|
||||
self.process_core_def(func);
|
||||
}
|
||||
Export::Instance(exports) => {
|
||||
for (_, export) in exports {
|
||||
self.export(export);
|
||||
}
|
||||
}
|
||||
Export::Module(_) => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn process_options(&mut self, opts: &mut AdapterOptions) {
|
||||
if let Some(memory) = &mut opts.memory {
|
||||
self.process_core_export(memory);
|
||||
}
|
||||
if let Some(def) = &mut opts.realloc {
|
||||
self.process_core_def(def);
|
||||
}
|
||||
if let Some(def) = &mut opts.post_return {
|
||||
self.process_core_def(def);
|
||||
}
|
||||
}
|
||||
|
||||
fn process_core_def(&mut self, def: &mut CoreDef) {
|
||||
fn core_def(&mut self, dfg: &dfg::ComponentDfg, def: &dfg::CoreDef) {
|
||||
match def {
|
||||
CoreDef::Adapter(idx) => {
|
||||
// The `to_process` queue is a LIFO queue so first enqueue the
|
||||
// addition of this adapter into a module followed by the
|
||||
// processing of the adapter itself. This means that the
|
||||
// adapter's own dependencies will be processed before the
|
||||
// adapter is added to a module.
|
||||
self.to_process.push(ToProcess::AddAdapterToModule(*idx));
|
||||
self.to_process.push(ToProcess::Adapter(*idx));
|
||||
dfg::CoreDef::Export(e) => self.core_export(dfg, e),
|
||||
dfg::CoreDef::Adapter(id) => {
|
||||
// If this adapter is already defined then we can safely depend
|
||||
// on it with no consequences.
|
||||
if self.defined_items.contains(&Def::Adapter(*id)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// .. otherwise we found a case of an adapter depending on an
|
||||
// adapter-module-in-progress meaning that the current adapter
|
||||
// module must be completed and then a new one is started.
|
||||
self.finish_adapter_module();
|
||||
assert!(self.defined_items.contains(&Def::Adapter(*id)));
|
||||
}
|
||||
|
||||
CoreDef::Export(e) => self.process_core_export(e),
|
||||
|
||||
// These are ignored since they don't contain a reference to an
|
||||
// adapter which may need to be inserted into a module.
|
||||
CoreDef::Lowered(_) | CoreDef::AlwaysTrap(_) | CoreDef::InstanceFlags(_) => {}
|
||||
// These items can't transitively depend on an adapter
|
||||
dfg::CoreDef::Lowered(_)
|
||||
| dfg::CoreDef::AlwaysTrap(_)
|
||||
| dfg::CoreDef::InstanceFlags(_) => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn process_core_export<T>(&mut self, export: &mut CoreExport<T>) {
|
||||
// Remap the instance index referenced here as necessary to account
|
||||
// for any adapter modules that needed creating in the meantime.
|
||||
export.instance = self.instance_map[export.instance];
|
||||
}
|
||||
}
|
||||
|
||||
impl DefinedItems {
|
||||
fn contains(&self, info: &Adapter) -> bool {
|
||||
self.contains_options(&info.lift_options)
|
||||
&& self.contains_options(&info.lower_options)
|
||||
&& self.contains_def(&info.func)
|
||||
}
|
||||
|
||||
fn contains_options(&self, options: &AdapterOptions) -> bool {
|
||||
let AdapterOptions {
|
||||
instance: _,
|
||||
string_encoding: _,
|
||||
memory64: _,
|
||||
memory,
|
||||
realloc,
|
||||
post_return,
|
||||
} = options;
|
||||
|
||||
if let Some(mem) = memory {
|
||||
if !self.contains_export(mem) {
|
||||
return false;
|
||||
}
|
||||
fn core_export<T>(&mut self, dfg: &dfg::ComponentDfg, export: &dfg::CoreExport<T>) {
|
||||
// If this instance has already been visited that means it can already
|
||||
// be defined for this adapter module, so nothing else needs to be done.
|
||||
if !self.defined_items.insert(Def::Instance(export.instance)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if let Some(def) = realloc {
|
||||
if !self.contains_def(def) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(def) = post_return {
|
||||
if !self.contains_def(def) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
fn contains_def(&self, options: &CoreDef) -> bool {
|
||||
match options {
|
||||
CoreDef::Export(e) => self.contains_export(e),
|
||||
CoreDef::AlwaysTrap(i) => i.as_u32() < self.always_trap,
|
||||
CoreDef::Lowered(i) => i.as_u32() < self.lowerings,
|
||||
CoreDef::Adapter(idx) => self.adapter_to_module[*idx].is_some(),
|
||||
CoreDef::InstanceFlags(_) => true,
|
||||
}
|
||||
}
|
||||
|
||||
fn contains_export<T>(&self, export: &CoreExport<T>) -> bool {
|
||||
// This `DefinedItems` index space will contain `export` if the
|
||||
// instance referenced has already been instantiated. The actual item
|
||||
// that `export` points to doesn't need to be tested since it comes
|
||||
// from the instance regardless.
|
||||
export.instance.as_u32() < self.instances
|
||||
}
|
||||
}
|
||||
|
||||
/// Rewrites all instances of `CoreDef::Adapter` within the `init` initializer
|
||||
/// provided to `CoreExport` according to the `map` provided.
|
||||
///
|
||||
/// This is called after all adapter modules have been constructed and the
|
||||
/// core wasm function for each adapter has been identified.
|
||||
fn map_adapter_references(
|
||||
init: &mut GlobalInitializer,
|
||||
map: &PrimaryMap<AdapterIndex, Option<CoreExport<EntityIndex>>>,
|
||||
) {
|
||||
let map_core_def = |def: &mut CoreDef| {
|
||||
let adapter = match def {
|
||||
CoreDef::Adapter(idx) => *idx,
|
||||
_ => return,
|
||||
};
|
||||
*def = CoreDef::Export(
|
||||
map[adapter]
|
||||
.clone()
|
||||
.expect("adapter should have been instantiated"),
|
||||
);
|
||||
};
|
||||
match init {
|
||||
GlobalInitializer::InstantiateModule(module) => match module {
|
||||
InstantiateModule::Static(_, args) => {
|
||||
for def in args.iter_mut() {
|
||||
map_core_def(def);
|
||||
// ... otherwise if this is the first timet he instance has been seen
|
||||
// then the instances own arguments are recursively visited to find
|
||||
// transitive dependencies on adapters.
|
||||
match &dfg.instances[export.instance] {
|
||||
dfg::Instance::Static(_, args) => {
|
||||
for arg in args.iter() {
|
||||
self.core_def(dfg, arg);
|
||||
}
|
||||
}
|
||||
InstantiateModule::Import(_, args) => {
|
||||
for (_, map) in args {
|
||||
for (_, def) in map {
|
||||
map_core_def(def);
|
||||
dfg::Instance::Import(_, args) => {
|
||||
for (_, values) in args {
|
||||
for (_, def) in values {
|
||||
self.core_def(dfg, def);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
GlobalInitializer::ExtractRealloc(e) => map_core_def(&mut e.def),
|
||||
GlobalInitializer::ExtractPostReturn(e) => map_core_def(&mut e.def),
|
||||
fn finish_adapter_module(&mut self) {
|
||||
if self.next_module.adapters.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
// Nothing to map here
|
||||
GlobalInitializer::LowerImport(_)
|
||||
| GlobalInitializer::AlwaysTrap(_)
|
||||
| GlobalInitializer::ExtractMemory(_) => {}
|
||||
GlobalInitializer::SaveStaticModule(_) => {}
|
||||
GlobalInitializer::SaveModuleImport(_) => {}
|
||||
// Reset the state of the current module-in-progress and then flag all
|
||||
// pending adapters as now defined since the current module is being
|
||||
// committed.
|
||||
let module = mem::take(&mut self.next_module);
|
||||
for adapter in module.adapters.iter() {
|
||||
let inserted = self.defined_items.insert(Def::Adapter(*adapter));
|
||||
assert!(inserted);
|
||||
}
|
||||
let idx = self.adapter_modules.push(module);
|
||||
log::debug!("finishing adapter module {idx:?}");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user