Refactor where results of compilation are stored (#2086)

* Refactor where results of compilation are stored

This commit refactors the internals of compilation in Wasmtime to change
where results of individual function compilation are stored. Previously
compilation resulted in many maps being returned, and compilation
results generally held all these maps together. This commit instead
switches this to have all metadata stored in a `CompiledFunction`
instead of having a separate map for each item that can be stored.

The motivation for this is primarily to help out with future
module-linking-related PRs. What exactly "module level" is depends on
how we interpret modules and how many modules are in play, so it's a bit
easier for operations in wasmtime to work at the function level where
possible. This means that we don't have to pass around multiple
different maps and a function index, but instead just one map or just
one entry representing a compiled function.

Additionally this change updates where the parallelism of compilation
happens, pushing it into `wasmtime-jit` instead of `wasmtime-environ`.
This is another goal where `wasmtime-jit` will have more knowledge about
module-level pieces with module linking in play. User-facing-wise this
should be the same in terms of parallel compilation, though.

The ultimate goal of this refactoring is to make it easier for the
results of compilation to actually be a set of wasm modules. This means
we won't be able to have a map-per-metadata where the primary key is the
function index, because there will be many modules within one "object
file".

* Don't clear out fields, just don't store them

Persist a smaller set of fields in `CompilationArtifacts` instead of
trying to clear fields out and dynamically not accessing them.
This commit is contained in:
Alex Crichton
2020-08-03 12:20:51 -05:00
committed by GitHub
parent 026fb8d388
commit 65eaca35dd
31 changed files with 466 additions and 757 deletions

View File

@@ -20,8 +20,8 @@ use wasmtime_environ::entity::{BoxedSlice, PrimaryMap};
use wasmtime_environ::isa::TargetIsa;
use wasmtime_environ::wasm::{DefinedFuncIndex, SignatureIndex};
use wasmtime_environ::{
CompileError, DataInitializer, DataInitializerLocation, Module, ModuleAddressMap,
ModuleEnvironment, ModuleTranslation, StackMaps, Traps,
CompileError, DataInitializer, DataInitializerLocation, FunctionAddressMap, Module,
ModuleEnvironment, ModuleTranslation, StackMapInformation, TrapInformation,
};
use wasmtime_profiling::ProfilingAgent;
use wasmtime_runtime::VMInterrupts;
@@ -67,19 +67,20 @@ pub struct CompilationArtifacts {
/// Data initiailizers.
data_initializers: Box<[OwnedDataInitializer]>,
/// Traps descriptors.
traps: Traps,
/// Stack map descriptors.
stack_maps: StackMaps,
/// Wasm to function code address map.
address_transform: ModuleAddressMap,
/// Descriptions of compiled functions
funcs: PrimaryMap<DefinedFuncIndex, FunctionInfo>,
/// Debug info presence flags.
debug_info: bool,
}
#[derive(Serialize, Deserialize, Clone)]
struct FunctionInfo {
traps: Vec<TrapInformation>,
address_map: FunctionAddressMap,
stack_maps: Vec<StackMapInformation>,
}
impl CompilationArtifacts {
/// Builds compilation artifacts.
pub fn build(compiler: &Compiler, data: &[u8]) -> Result<Self, SetupError> {
@@ -92,9 +93,7 @@ impl CompilationArtifacts {
let Compilation {
obj,
unwind_info,
traps,
stack_maps,
address_transform,
funcs,
} = compiler.compile(&translation)?;
let ModuleTranslation {
@@ -120,9 +119,14 @@ impl CompilationArtifacts {
obj: obj.into_boxed_slice(),
unwind_info: unwind_info.into_boxed_slice(),
data_initializers,
traps,
stack_maps,
address_transform,
funcs: funcs
.into_iter()
.map(|(_, func)| FunctionInfo {
stack_maps: func.stack_maps,
traps: func.traps,
address_map: func.address_map,
})
.collect(),
debug_info: compiler.tunables().debug_info,
})
}
@@ -147,9 +151,7 @@ pub struct CompiledModule {
finished_functions: FinishedFunctions,
trampolines: PrimaryMap<SignatureIndex, VMTrampoline>,
data_initializers: Box<[OwnedDataInitializer]>,
traps: Traps,
stack_maps: StackMaps,
address_transform: ModuleAddressMap,
funcs: PrimaryMap<DefinedFuncIndex, FunctionInfo>,
obj: Box<[u8]>,
unwind_info: Box<[ObjectUnwindInfo]>,
}
@@ -176,9 +178,7 @@ impl CompiledModule {
obj,
unwind_info,
data_initializers,
traps,
stack_maps,
address_transform,
funcs,
debug_info,
} = artifacts;
@@ -216,9 +216,7 @@ impl CompiledModule {
finished_functions,
trampolines,
data_initializers,
traps,
stack_maps,
address_transform,
funcs,
obj,
unwind_info,
})
@@ -231,9 +229,7 @@ impl CompiledModule {
obj: self.obj.clone(),
unwind_info: self.unwind_info.clone(),
data_initializers: self.data_initializers.clone(),
traps: self.traps.clone(),
stack_maps: self.stack_maps.clone(),
address_transform: self.address_transform.clone(),
funcs: self.funcs.clone(),
debug_info: self.code.dbg_jit_registration.is_some(),
}
}
@@ -318,19 +314,36 @@ impl CompiledModule {
&self.finished_functions.0
}
/// Returns the map for all traps in this module.
pub fn traps(&self) -> &Traps {
&self.traps
/// Returns the stack map information for all functions defined in this
/// module.
///
/// The iterator returned iterates over the span of the compiled function in
/// memory with the stack maps associated with those bytes.
pub fn stack_maps(
&self,
) -> impl Iterator<Item = (*mut [VMFunctionBody], &[StackMapInformation])> {
self.finished_functions()
.values()
.copied()
.zip(self.funcs.values().map(|f| f.stack_maps.as_slice()))
}
/// Returns the map for each of this module's stack maps.
pub fn stack_maps(&self) -> &StackMaps {
&self.stack_maps
}
/// Returns a map of compiled addresses back to original bytecode offsets.
pub fn address_transform(&self) -> &ModuleAddressMap {
&self.address_transform
/// Iterates over all functions in this module, returning information about
/// how to decode traps which happen in the function.
pub fn trap_information(
&self,
) -> impl Iterator<
Item = (
DefinedFuncIndex,
*mut [VMFunctionBody],
&[TrapInformation],
&FunctionAddressMap,
),
> {
self.finished_functions()
.iter()
.zip(self.funcs.values())
.map(|((i, alloc), func)| (i, *alloc, func.traps.as_slice(), &func.address_map))
}
/// Returns all ranges convered by JIT code.