Implement the module linking alias section (#2451)
This commit is intended to do almost everything necessary for processing the alias section of module linking. Most of this is internal refactoring, the highlights being: * Type contents are now stored separately from a `wasmtime_env::Module`. Given that modules can freely alias types and have them used all over the place, it seemed best to have one canonical location to type storage which everywhere else points to (with indices). A new `TypeTables` structure is produced during compilation which is shared amongst all member modules in a wasm blob. * Instantiation is heavily refactored to account for module linking. The main gotcha here is that imports are now listed as "initializers". We have a sort of pseudo-bytecode-interpreter which interprets the initialization of a module. This is more complicated than just matching imports at this point because in the module linking proposal the module, alias, import, and instance sections may all be interleaved. This means that imports aren't guaranteed to show up at the beginning of the address space for modules/instances. Otherwise most of the changes here largely fell out from these two design points. Aliases are recorded as initializers in this scheme. Copying around type information and/or just knowing type information during compilation is also pretty easy since everything is just a pointer into a `TypeTables` and we don't have to actually copy any types themselves. Lots of various refactorings were necessary to accomodate these changes. Tests are hoped to cover a breadth of functionality here, but not necessarily a depth. There's still one more piece of the module linking proposal missing which is exporting instances/modules, which will come in a future PR. It's also worth nothing that there's one large TODO which isn't implemented in this change that I plan on opening an issue for. With module linking when a set of modules comes back from compilation each modules has all the trampolines for the entire set of modules. This is quite a lot of duplicate trampolines across module-linking modules. We'll want to refactor this at some point to instead have only one set of trampolines per set of module linking modules and have them shared from there. I figured it was best to separate out this change, however, since it's purely related to resource usage, and doesn't impact non-module-linking modules at all. cc #2094
This commit is contained in:
@@ -10,7 +10,8 @@ use std::mem;
|
||||
use std::panic::{self, AssertUnwindSafe};
|
||||
use wasmtime_environ::entity::PrimaryMap;
|
||||
use wasmtime_environ::isa::TargetIsa;
|
||||
use wasmtime_environ::{ir, wasm, CompiledFunction, Module};
|
||||
use wasmtime_environ::wasm::SignatureIndex;
|
||||
use wasmtime_environ::{ir, wasm, CompiledFunction, Module, ModuleType};
|
||||
use wasmtime_jit::trampoline::ir::{
|
||||
ExternalName, Function, InstBuilder, MemFlags, StackSlotData, StackSlotKind,
|
||||
};
|
||||
@@ -223,7 +224,8 @@ pub fn create_handle_with_function(
|
||||
|
||||
// First up we manufacture a trampoline which has the ABI specified by `ft`
|
||||
// and calls into `stub_fn`...
|
||||
let sig_id = module.signatures.push(wft.clone());
|
||||
let sig_id = SignatureIndex::from_u32(u32::max_value() - 1);
|
||||
module.types.push(ModuleType::Function(sig_id));
|
||||
let func_id = module.functions.push(sig_id);
|
||||
module
|
||||
.exports
|
||||
@@ -241,7 +243,7 @@ pub fn create_handle_with_function(
|
||||
&sig,
|
||||
mem::size_of::<u128>(),
|
||||
)?;
|
||||
store.signatures().borrow_mut().register(wft, trampoline);
|
||||
let shared_signature_id = store.signatures().borrow_mut().register(wft, trampoline);
|
||||
|
||||
// Next up we wrap everything up into an `InstanceHandle` by publishing our
|
||||
// code memory (makes it executable) and ensuring all our various bits of
|
||||
@@ -254,6 +256,7 @@ pub fn create_handle_with_function(
|
||||
finished_functions,
|
||||
Box::new(trampoline_state),
|
||||
&[],
|
||||
Some(shared_signature_id),
|
||||
)
|
||||
.map(|instance| (instance, trampoline))
|
||||
}
|
||||
@@ -270,13 +273,21 @@ pub unsafe fn create_handle_with_raw_function(
|
||||
let mut module = Module::new();
|
||||
let mut finished_functions = PrimaryMap::new();
|
||||
|
||||
let sig_id = module.signatures.push(wft.clone());
|
||||
let sig_id = SignatureIndex::from_u32(u32::max_value() - 1);
|
||||
module.types.push(ModuleType::Function(sig_id));
|
||||
let func_id = module.functions.push(sig_id);
|
||||
module
|
||||
.exports
|
||||
.insert(String::new(), wasm::EntityIndex::Function(func_id));
|
||||
finished_functions.push(func);
|
||||
store.signatures().borrow_mut().register(wft, trampoline);
|
||||
let shared_signature_id = store.signatures().borrow_mut().register(wft, trampoline);
|
||||
|
||||
create_handle(module, store, finished_functions, state, &[])
|
||||
create_handle(
|
||||
module,
|
||||
store,
|
||||
finished_functions,
|
||||
state,
|
||||
&[],
|
||||
Some(shared_signature_id),
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user