* Reimplement how unwind information is stored This commit is a major refactoring of how unwind information is stored after compilation of a function has finished. Previously we would store the raw `UnwindInfo` as a result of compilation and this would get serialized/deserialized alongside the rest of the ELF object that compilation creates. Whenever functions were registered with `CodeMemory` this would also result in registering unwinding information dynamically at runtime, which in the case of Unix, for example, would dynamically created FDE/CIE entries on-the-fly. Eventually I'd like to support compiling Wasmtime without Cranelift, but this means that `UnwindInfo` wouldn't be easily available to decode into and create unwinding information from. To solve this I've changed the ELF object created to have the unwinding information encoded into it ahead-of-time so loading code into memory no longer needs to create unwinding tables. This change has two different implementations for Windows/Unix: * On Windows the implementation was much easier. The unwinding information on Windows is already stored after the function itself in the text section. This was actually slightly duplicated in object building and in code memory allocation. Now the object building continues to do the same, recording unwinding information after functions, and code memory no longer manually tracks this. Additionally Wasmtime will emit a special custom section in the object file with unwinding information which is the list of `RUNTIME_FUNCTION` structures that `RtlAddFunctionTable` expects. This means that the object file has all the information precompiled into it and registration at runtime is simply passing a few pointers around to the runtime. * Unix was a little bit more difficult than Windows. Today a `.eh_frame` section is created on-the-fly with offsets in FDEs specified as the absolute address that functions are loaded at. This absolute address hindered the ability to precompile the FDE into the object file itself. I've switched how addresses are encoded, though, to using `DW_EH_PE_pcrel` which means that FDE addresses are now specified relative to the FDE itself. This means that we can maintain a fixed offset between the `.eh_frame` loaded in memory and the beginning of code memory. When doing so this enables precompiling the `.eh_frame` section into the object file and at runtime when loading an object no further construction of unwinding information is needed. The overall result of this commit is that unwinding information is no longer stored in its cranelift-data-structure form on disk. This means that this unwinding information format is only present during compilation, which will make it that much easier to compile out cranelift in the future. This commit also significantly refactors `CodeMemory` since the way unwinding information is handled is not much different from before. Previously `CodeMemory` was suitable for incrementally adding more and more functions to it, but nowadays a `CodeMemory` either lives per module (in which case all functions are known up front) or it's created once-per-`Func::new` with two trampolines. In both cases we know all functions up front so the functionality of incrementally adding more and more segments is no longer needed. This commit removes the ability to add a function-at-a-time in `CodeMemory` and instead it can now only load objects in their entirety. A small helper function is added to build a small object file for trampolines in `Func::new` to handle allocation there. Finally, this commit also folds the `wasmtime-obj` crate directly into the `wasmtime-cranelift` crate and its builder structure to be more amenable to this strategy of managing unwinding tables. It is not intentional to have any real functional change as a result of this commit. This might accelerate loading a module from cache slightly since less work is needed to manage the unwinding information, but that's just a side benefit from the main goal of this commit which is to remove the dependence on cranelift unwinding information being available at runtime. * Remove isa reexport from wasmtime-environ * Trim down reexports of `cranelift-codegen` Remove everything non-essential so that only the bits which will need to be refactored out of cranelift remain. * Fix debug tests * Review comments
181 lines
4.9 KiB
Rust
181 lines
4.9 KiB
Rust
//! JIT compilation.
|
|
|
|
use crate::instantiate::SetupError;
|
|
#[cfg(feature = "parallel-compilation")]
|
|
use rayon::prelude::*;
|
|
use serde::{Deserialize, Serialize};
|
|
use std::collections::BTreeMap;
|
|
use std::hash::{Hash, Hasher};
|
|
use std::mem;
|
|
use wasmparser::WasmFeatures;
|
|
use wasmtime_environ::{
|
|
CompiledFunctions, Compiler as EnvCompiler, CompilerBuilder, ModuleTranslation, Tunables,
|
|
TypeTables,
|
|
};
|
|
|
|
/// Select which kind of compilation to use.
|
|
#[derive(Copy, Clone, Debug, Hash, Serialize, Deserialize, Eq, PartialEq)]
|
|
pub enum CompilationStrategy {
|
|
/// Let Wasmtime pick the strategy.
|
|
Auto,
|
|
|
|
/// Compile all functions with Cranelift.
|
|
Cranelift,
|
|
|
|
/// Compile all functions with Lightbeam.
|
|
#[cfg(feature = "lightbeam")]
|
|
Lightbeam,
|
|
}
|
|
|
|
/// A WebAssembly code JIT compiler.
|
|
pub struct Compiler {
|
|
compiler: Box<dyn EnvCompiler>,
|
|
tunables: Tunables,
|
|
features: WasmFeatures,
|
|
parallel_compilation: bool,
|
|
}
|
|
|
|
impl Compiler {
|
|
/// Creates a new compiler builder for the provided compilation strategy.
|
|
pub fn builder(strategy: CompilationStrategy) -> Box<dyn CompilerBuilder> {
|
|
match strategy {
|
|
CompilationStrategy::Auto | CompilationStrategy::Cranelift => {
|
|
wasmtime_cranelift::builder()
|
|
}
|
|
#[cfg(feature = "lightbeam")]
|
|
CompilationStrategy::Lightbeam => unimplemented!(),
|
|
}
|
|
}
|
|
|
|
/// Creates a new instance of a `Compiler` from the provided compiler
|
|
/// builder.
|
|
pub fn new(
|
|
builder: &dyn CompilerBuilder,
|
|
tunables: Tunables,
|
|
features: WasmFeatures,
|
|
parallel_compilation: bool,
|
|
) -> Compiler {
|
|
Compiler {
|
|
compiler: builder.build(),
|
|
tunables,
|
|
features,
|
|
parallel_compilation,
|
|
}
|
|
}
|
|
}
|
|
|
|
fn _assert_compiler_send_sync() {
|
|
fn _assert<T: Send + Sync>() {}
|
|
_assert::<Compiler>();
|
|
}
|
|
|
|
#[allow(missing_docs)]
|
|
pub struct Compilation {
|
|
pub obj: Vec<u8>,
|
|
pub funcs: CompiledFunctions,
|
|
}
|
|
|
|
impl Compiler {
|
|
/// Return the tunables in use by this engine.
|
|
pub fn tunables(&self) -> &Tunables {
|
|
&self.tunables
|
|
}
|
|
|
|
/// Return the enabled wasm features.
|
|
pub fn features(&self) -> &WasmFeatures {
|
|
&self.features
|
|
}
|
|
|
|
/// Return the underlying compiler in use
|
|
pub fn compiler(&self) -> &dyn EnvCompiler {
|
|
&*self.compiler
|
|
}
|
|
|
|
/// Returns the target this compiler is compiling for.
|
|
pub fn triple(&self) -> &target_lexicon::Triple {
|
|
self.compiler.triple()
|
|
}
|
|
|
|
/// Compile the given function bodies.
|
|
pub fn compile<'data>(
|
|
&self,
|
|
translation: &mut ModuleTranslation,
|
|
types: &TypeTables,
|
|
) -> Result<Compilation, SetupError> {
|
|
let functions = mem::take(&mut translation.function_body_inputs);
|
|
let functions = functions.into_iter().collect::<Vec<_>>();
|
|
|
|
let funcs = self
|
|
.run_maybe_parallel(functions, |(index, func)| {
|
|
self.compiler
|
|
.compile_function(translation, index, func, &self.tunables, types)
|
|
})?
|
|
.into_iter()
|
|
.collect::<CompiledFunctions>();
|
|
|
|
let obj = self.compiler.emit_obj(
|
|
&translation,
|
|
types,
|
|
&funcs,
|
|
self.tunables.generate_native_debuginfo,
|
|
)?;
|
|
|
|
Ok(Compilation { obj, funcs })
|
|
}
|
|
|
|
/// Run the given closure in parallel if the compiler is configured to do so.
|
|
pub(crate) fn run_maybe_parallel<
|
|
A: Send,
|
|
B: Send,
|
|
E: Send,
|
|
F: Fn(A) -> Result<B, E> + Send + Sync,
|
|
>(
|
|
&self,
|
|
input: Vec<A>,
|
|
f: F,
|
|
) -> Result<Vec<B>, E> {
|
|
if self.parallel_compilation {
|
|
#[cfg(feature = "parallel-compilation")]
|
|
return input
|
|
.into_par_iter()
|
|
.map(|a| f(a))
|
|
.collect::<Result<Vec<B>, E>>();
|
|
}
|
|
|
|
// In case the parallel-compilation feature is disabled or the parallel_compilation config
|
|
// was turned off dynamically fallback to the non-parallel version.
|
|
input
|
|
.into_iter()
|
|
.map(|a| f(a))
|
|
.collect::<Result<Vec<B>, E>>()
|
|
}
|
|
}
|
|
|
|
impl Hash for Compiler {
|
|
fn hash<H: Hasher>(&self, hasher: &mut H) {
|
|
let Compiler {
|
|
compiler,
|
|
tunables,
|
|
features,
|
|
parallel_compilation: _,
|
|
} = self;
|
|
|
|
compiler.triple().hash(hasher);
|
|
compiler
|
|
.flags()
|
|
.into_iter()
|
|
.collect::<BTreeMap<_, _>>()
|
|
.hash(hasher);
|
|
compiler
|
|
.isa_flags()
|
|
.into_iter()
|
|
.collect::<BTreeMap<_, _>>()
|
|
.hash(hasher);
|
|
tunables.hash(hasher);
|
|
features.hash(hasher);
|
|
|
|
// Catch accidental bugs of reusing across crate versions.
|
|
env!("CARGO_PKG_VERSION").hash(hasher);
|
|
}
|
|
}
|