* Improve robustness of cache loading/storing
Today wasmtime incorrectly loads compiled compiled modules from the
global cache when toggling settings such as optimizations. For example
if you execute `wasmtime foo.wasm` that will cache globally an
unoptimized version of the wasm module. If you then execute `wasmtime -O
foo.wasm` it would then reload the unoptimized version from cache, not
realizing the compilation settings were different, and use that instead.
This can lead to very surprising behavior naturally!
This commit updates how the cache is managed in an attempt to make it
much more robust against these sorts of issues. This takes a leaf out of
rustc's playbook and models the cache with a function that looks like:
fn load<T: Hash>(
&self,
data: T,
compute: fn(T) -> CacheEntry,
) -> CacheEntry;
The goal here is that it guarantees that all the `data` necessary to
`compute` the result of the cache entry is hashable and stored into the
hash key entry. This was previously open-coded and manually managed
where items were hashed explicitly, but this construction guarantees
that everything reasonable `compute` could use to compile the module is
stored in `data`, which is itself hashable.
This refactoring then resulted in a few workarounds and a few fixes,
including the original issue:
* The `Module` type was split into `Module` and `ModuleLocal` where only
the latter is hashed. The previous hash function for a `Module` left
out items like the `start_func` and didn't hash items like the imports
of the module. Omitting the `start_func` was fine since compilation
didn't actually use it, but omitting imports seemed uncomfortable
because while compilation didn't use the import values it did use the
*number* of imports, which seems like it should then be put into the
cache key. The `ModuleLocal` type now derives `Hash` to guarantee that
all of its contents affect the hash key.
* The `ModuleTranslationState` from `cranelift-wasm` doesn't implement
`Hash` which means that we have a manual wrapper to work around that.
This will be fixed with an upstream implementation, since this state
affects the generated wasm code. Currently this is just a map of
signatures, which is present in `Module` anyway, so we should be good
for the time being.
* Hashing `dyn TargetIsa` was also added, where previously it was not
fully hashed. Previously only the target name was used as part of the
cache key, but crucially the flags of compilation were omitted (for
example the optimization flags). Unfortunately the trait object itself
is not hashable so we still have to manually write a wrapper to hash
it, but we likely want to add upstream some utilities to hash isa
objects into cranelift itself. For now though we can continue to add
hashed fields as necessary.
Overall the goal here was to use the compiler to expose what we're not
hashing, and then make sure we organize data and write the right code to
ensure everything is hashed, and nothing more.
* Update crates/environ/src/module.rs
Co-Authored-By: Peter Huene <peterhuene@protonmail.com>
* Fix lightbeam
* Fix compilation of tests
* Update the expected structure of the cache
* Revert "Update the expected structure of the cache"
This reverts commit 2b53fee426a4e411c313d8c1e424841ba304a9cd.
* Separate the cache dir a bit
* Add a test the cache is busted with opt levels
* rustfmt
Co-authored-by: Peter Huene <peterhuene@protonmail.com>
152 lines
4.6 KiB
Rust
152 lines
4.6 KiB
Rust
use anyhow::{anyhow, bail, Context as _, Result};
|
|
use faerie::Artifact;
|
|
use target_lexicon::Triple;
|
|
use wasmtime::Strategy;
|
|
use wasmtime_debug::{emit_debugsections, read_debuginfo};
|
|
#[cfg(feature = "lightbeam")]
|
|
use wasmtime_environ::Lightbeam;
|
|
use wasmtime_environ::{
|
|
entity::EntityRef, settings, settings::Configurable, wasm::DefinedMemoryIndex,
|
|
wasm::MemoryIndex, CacheConfig, Compiler, Cranelift, ModuleEnvironment, ModuleMemoryOffset,
|
|
ModuleVmctxInfo, Tunables, VMOffsets,
|
|
};
|
|
use wasmtime_jit::native;
|
|
use wasmtime_obj::emit_module;
|
|
|
|
/// Creates object file from binary wasm data.
|
|
pub fn compile_to_obj(
|
|
wasm: &[u8],
|
|
target: Option<&Triple>,
|
|
strategy: Strategy,
|
|
enable_simd: bool,
|
|
opt_level: wasmtime::OptLevel,
|
|
debug_info: bool,
|
|
artifact_name: String,
|
|
cache_config: &CacheConfig,
|
|
) -> Result<Artifact> {
|
|
let isa_builder = match target {
|
|
Some(target) => native::lookup(target.clone())?,
|
|
None => native::builder(),
|
|
};
|
|
let mut flag_builder = settings::builder();
|
|
|
|
// There are two possible traps for division, and this way
|
|
// we get the proper one if code traps.
|
|
flag_builder.enable("avoid_div_traps").unwrap();
|
|
|
|
if enable_simd {
|
|
flag_builder.enable("enable_simd").unwrap();
|
|
}
|
|
|
|
match opt_level {
|
|
wasmtime::OptLevel::None => {}
|
|
wasmtime::OptLevel::Speed => {
|
|
flag_builder.set("opt_level", "speed").unwrap();
|
|
}
|
|
wasmtime::OptLevel::SpeedAndSize => {
|
|
flag_builder.set("opt_level", "speed_and_size").unwrap();
|
|
}
|
|
other => bail!("unknown optimization level {:?}", other),
|
|
}
|
|
|
|
let isa = isa_builder.finish(settings::Flags::new(flag_builder));
|
|
|
|
let mut obj = Artifact::new(isa.triple().clone(), artifact_name);
|
|
|
|
// TODO: Expose the tunables as command-line flags.
|
|
let tunables = Tunables::default();
|
|
|
|
let (
|
|
module,
|
|
module_translation,
|
|
lazy_function_body_inputs,
|
|
lazy_data_initializers,
|
|
target_config,
|
|
) = {
|
|
let environ = ModuleEnvironment::new(isa.frontend_config(), tunables);
|
|
|
|
let translation = environ
|
|
.translate(wasm)
|
|
.context("failed to translate module")?;
|
|
|
|
(
|
|
translation.module,
|
|
translation.module_translation.unwrap(),
|
|
translation.function_body_inputs,
|
|
translation.data_initializers,
|
|
translation.target_config,
|
|
)
|
|
};
|
|
|
|
// TODO: use the traps information
|
|
let (compilation, relocations, address_transform, value_ranges, stack_slots, _traps) =
|
|
match strategy {
|
|
Strategy::Auto | Strategy::Cranelift => Cranelift::compile_module(
|
|
&module,
|
|
&module_translation,
|
|
lazy_function_body_inputs,
|
|
&*isa,
|
|
debug_info,
|
|
cache_config,
|
|
),
|
|
#[cfg(feature = "lightbeam")]
|
|
Strategy::Lightbeam => Lightbeam::compile_module(
|
|
&module,
|
|
&module_translation,
|
|
lazy_function_body_inputs,
|
|
&*isa,
|
|
debug_info,
|
|
cache_config,
|
|
),
|
|
#[cfg(not(feature = "lightbeam"))]
|
|
Strategy::Lightbeam => bail!("lightbeam support not enabled"),
|
|
other => bail!("unsupported compilation strategy {:?}", other),
|
|
}
|
|
.context("failed to compile module")?;
|
|
|
|
if compilation.is_empty() {
|
|
bail!("no functions were found/compiled");
|
|
}
|
|
|
|
let module_vmctx_info = {
|
|
let ofs = VMOffsets::new(target_config.pointer_bytes(), &module.local);
|
|
ModuleVmctxInfo {
|
|
memory_offset: if ofs.num_imported_memories > 0 {
|
|
ModuleMemoryOffset::Imported(ofs.vmctx_vmmemory_import(MemoryIndex::new(0)))
|
|
} else if ofs.num_defined_memories > 0 {
|
|
ModuleMemoryOffset::Defined(
|
|
ofs.vmctx_vmmemory_definition_base(DefinedMemoryIndex::new(0)),
|
|
)
|
|
} else {
|
|
ModuleMemoryOffset::None
|
|
},
|
|
stack_slots,
|
|
}
|
|
};
|
|
|
|
emit_module(
|
|
&mut obj,
|
|
&module,
|
|
&compilation,
|
|
&relocations,
|
|
&lazy_data_initializers,
|
|
&target_config,
|
|
)
|
|
.map_err(|e| anyhow!(e))
|
|
.context("failed to emit module")?;
|
|
|
|
if debug_info {
|
|
let debug_data = read_debuginfo(wasm);
|
|
emit_debugsections(
|
|
&mut obj,
|
|
&module_vmctx_info,
|
|
target_config,
|
|
&debug_data,
|
|
&address_transform,
|
|
&value_ranges,
|
|
)
|
|
.context("failed to emit debug sections")?;
|
|
}
|
|
Ok(obj)
|
|
}
|