* 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>
76 lines
3.0 KiB
Rust
76 lines
3.0 KiB
Rust
//! Support for compiling with Lightbeam.
|
|
|
|
use crate::cache::ModuleCacheDataTupleType;
|
|
use crate::compilation::{Compilation, CompileError, Traps};
|
|
use crate::func_environ::FuncEnvironment;
|
|
use crate::module::Module;
|
|
use crate::module_environ::FunctionBodyData;
|
|
// TODO: Put this in `compilation`
|
|
use crate::address_map::{ModuleAddressMap, ValueLabelsRanges};
|
|
use crate::cranelift::RelocSink;
|
|
use crate::CacheConfig;
|
|
use cranelift_codegen::isa;
|
|
use cranelift_entity::{PrimaryMap, SecondaryMap};
|
|
use cranelift_wasm::{DefinedFuncIndex, ModuleTranslationState};
|
|
|
|
/// A compiler that compiles a WebAssembly module with Lightbeam, directly translating the Wasm file.
|
|
pub struct Lightbeam;
|
|
|
|
impl crate::compilation::Compiler for Lightbeam {
|
|
/// Compile the module using Lightbeam, producing a compilation result with
|
|
/// associated relocations.
|
|
fn compile_module<'data, 'module>(
|
|
module: &'module Module,
|
|
_module_translation: &ModuleTranslationState,
|
|
function_body_inputs: PrimaryMap<DefinedFuncIndex, FunctionBodyData<'data>>,
|
|
isa: &dyn isa::TargetIsa,
|
|
// TODO
|
|
generate_debug_info: bool,
|
|
_cache_config: &CacheConfig,
|
|
) -> Result<ModuleCacheDataTupleType, CompileError> {
|
|
if generate_debug_info {
|
|
return Err(CompileError::DebugInfoNotSupported);
|
|
}
|
|
|
|
let env = FuncEnvironment::new(isa.frontend_config(), &module.local);
|
|
let mut relocations = PrimaryMap::new();
|
|
let mut codegen_session: lightbeam::CodeGenSession<_> =
|
|
lightbeam::CodeGenSession::new(function_body_inputs.len() as u32, &env);
|
|
|
|
for (i, function_body) in &function_body_inputs {
|
|
let func_index = module.local.func_index(i);
|
|
let mut reloc_sink = RelocSink::new(func_index);
|
|
|
|
lightbeam::translate_function(
|
|
&mut codegen_session,
|
|
&mut reloc_sink,
|
|
i.as_u32(),
|
|
&wasmparser::FunctionBody::new(0, function_body.data),
|
|
)
|
|
.map_err(|e| CompileError::Codegen(format!("Failed to translate function: {}", e)))?;
|
|
relocations.push(reloc_sink.func_relocs);
|
|
}
|
|
|
|
let code_section = codegen_session
|
|
.into_translated_code_section()
|
|
.map_err(|e| CompileError::Codegen(format!("Failed to generate output code: {}", e)))?;
|
|
|
|
// TODO pass jump table offsets to Compilation::from_buffer() when they
|
|
// are implemented in lightbeam -- using empty set of offsets for now.
|
|
// TODO: pass an empty range for the unwind information until lightbeam emits it
|
|
let code_section_ranges_and_jt = code_section
|
|
.funcs()
|
|
.into_iter()
|
|
.map(|r| (r, SecondaryMap::new(), 0..0));
|
|
|
|
Ok((
|
|
Compilation::from_buffer(code_section.buffer().to_vec(), code_section_ranges_and_jt),
|
|
relocations,
|
|
ModuleAddressMap::new(),
|
|
ValueLabelsRanges::new(),
|
|
PrimaryMap::new(),
|
|
Traps::new(),
|
|
))
|
|
}
|
|
}
|