Remove global state for trap registration (#909)

* Remove global state for trap registration

There's a number of changes brought about in this commit, motivated by a
few things. One motivation was to remove an instance of using
`lazy_static!` in an effort to remove global state and encapsulate it
wherever possible. A second motivation came when investigating a
slowly-compiling wasm module (a bit too slowly) where a good chunk of
time was spent in managing trap registrations.

The specific change made here is that `TrapRegistry` is now stored
inside of a `Compiler` instead of inside a global. Additionally traps
are "bulk registered" for a module rather than one-by-one. This form of
bulk-registration allows optimizing the locks used here, where a lock is
only held for a module at-a-time instead of once-per-function.

With these changes the "unregister" logic has also been tweaked a bit
here and there to continue to work. As a nice side effect the `Compiler`
type now has one fewer field that requires actual mutability and has
been updated for multi-threaded compilation, nudging us closer to a
world where we can support multi-threaded compilation. Yay!

In terms of performance improvements, a local wasm test file that
previously took 3 seconds to compile is now 10% faster to compile,
taking ~2.7 seconds now.

* Perform trap resolution after unwinding

This avoids taking locks in signal handlers which feels a bit iffy...

* Remove `TrapRegistration::dummy()`

Avoid an case where you're trying to lookup trap information from a
dummy module for something that happened in a different module.

* Tweak some comments
This commit is contained in:
Alex Crichton
2020-02-06 12:40:50 -06:00
committed by GitHub
parent 9dffaf9d57
commit 348c597a8e
14 changed files with 233 additions and 131 deletions

View File

@@ -18,7 +18,7 @@ use wasmtime_environ::{
CompileError, DataInitializer, DataInitializerLocation, Module, ModuleEnvironment,
};
use wasmtime_runtime::{
GdbJitImageRegistration, InstanceHandle, InstantiationError, VMFunctionBody,
GdbJitImageRegistration, InstanceHandle, InstantiationError, TrapRegistration, VMFunctionBody,
VMSharedSignatureIndex,
};
@@ -52,6 +52,7 @@ struct RawCompiledModule<'data> {
data_initializers: Box<[DataInitializer<'data>]>,
signatures: BoxedSlice<SignatureIndex, VMSharedSignatureIndex>,
dbg_jit_registration: Option<GdbJitImageRegistration>,
trap_registration: TrapRegistration,
}
impl<'data> RawCompiledModule<'data> {
@@ -73,12 +74,13 @@ impl<'data> RawCompiledModule<'data> {
None
};
let (allocated_functions, jt_offsets, relocations, dbg_image) = compiler.compile(
&translation.module,
translation.module_translation.as_ref().unwrap(),
translation.function_body_inputs,
debug_data,
)?;
let (allocated_functions, jt_offsets, relocations, dbg_image, trap_registration) = compiler
.compile(
&translation.module,
translation.module_translation.as_ref().unwrap(),
translation.function_body_inputs,
debug_data,
)?;
link_module(
&translation.module,
@@ -127,6 +129,7 @@ impl<'data> RawCompiledModule<'data> {
data_initializers: translation.data_initializers.into_boxed_slice(),
signatures: signatures.into_boxed_slice(),
dbg_jit_registration,
trap_registration,
})
}
}
@@ -138,6 +141,7 @@ pub struct CompiledModule {
data_initializers: Box<[OwnedDataInitializer]>,
signatures: BoxedSlice<SignatureIndex, VMSharedSignatureIndex>,
dbg_jit_registration: Option<Rc<GdbJitImageRegistration>>,
trap_registration: TrapRegistration,
}
impl CompiledModule {
@@ -159,6 +163,7 @@ impl CompiledModule {
.into_boxed_slice(),
raw.signatures.clone(),
raw.dbg_jit_registration,
raw.trap_registration,
))
}
@@ -169,6 +174,7 @@ impl CompiledModule {
data_initializers: Box<[OwnedDataInitializer]>,
signatures: BoxedSlice<SignatureIndex, VMSharedSignatureIndex>,
dbg_jit_registration: Option<GdbJitImageRegistration>,
trap_registration: TrapRegistration,
) -> Self {
Self {
module: Arc::new(module),
@@ -176,6 +182,7 @@ impl CompiledModule {
data_initializers,
signatures,
dbg_jit_registration: dbg_jit_registration.map(Rc::new),
trap_registration,
}
}
@@ -203,6 +210,7 @@ impl CompiledModule {
let imports = resolve_imports(&self.module, resolver)?;
InstanceHandle::new(
Arc::clone(&self.module),
self.trap_registration.clone(),
self.finished_functions.clone(),
imports,
&data_initializers,