Add a method to share Config across machines (#2608)

With `Module::{serialize,deserialize}` it should be possible to share
wasmtime modules across machines or CPUs. Serialization, however, embeds
a hash of all configuration values, including cranelift compilation
settings. By default wasmtime's selection of the native ISA would enable
ISA flags according to CPU features available on the host, but the same
CPU features may not be available across two machines.

This commit adds a `Config::cranelift_clear_cpu_flags` method which
allows clearing the target-specific ISA flags that are automatically
inferred by default for the native CPU. Options can then be
incrementally built back up as-desired with teh `cranelift_other_flag`
method.
This commit is contained in:
Alex Crichton
2021-01-26 15:59:12 -06:00
committed by GitHub
parent e594c43d50
commit 503129ad91
16 changed files with 125 additions and 11 deletions

View File

@@ -12,6 +12,7 @@ mod instance;
mod invoke_func_via_table;
mod linker;
mod memory_creator;
mod module;
mod module_linking;
mod module_serialize;
mod name;

54
tests/all/module.rs Normal file
View File

@@ -0,0 +1,54 @@
use wasmtime::*;
#[test]
fn caches_across_engines() {
let mut c = Config::new();
c.cranelift_clear_cpu_flags();
let bytes = Module::new(&Engine::new(&c), "(module)")
.unwrap()
.serialize()
.unwrap();
let res = Module::deserialize(
&Engine::new(&Config::new().cranelift_clear_cpu_flags()),
&bytes,
);
assert!(res.is_ok());
// differ in shared cranelift flags
let res = Module::deserialize(
&Engine::new(
&Config::new()
.cranelift_clear_cpu_flags()
.cranelift_nan_canonicalization(true),
),
&bytes,
);
assert!(res.is_err());
// differ in cranelift settings
let res = Module::deserialize(
&Engine::new(
&Config::new()
.cranelift_clear_cpu_flags()
.cranelift_opt_level(OptLevel::None),
),
&bytes,
);
assert!(res.is_err());
// differ in cpu-specific flags
if cfg!(target_arch = "x86_64") {
let res = Module::deserialize(
&Engine::new(unsafe {
&Config::new()
.cranelift_clear_cpu_flags()
.cranelift_other_flag("has_sse3", "true")
.unwrap()
}),
&bytes,
);
assert!(res.is_err());
}
}