Add a compile command to Wasmtime.
This commit adds a `compile` command to the Wasmtime CLI. The command can be used to Ahead-Of-Time (AOT) compile WebAssembly modules. With the `all-arch` feature enabled, AOT compilation can be performed for non-native architectures (i.e. cross-compilation). The `Module::compile` method has been added to perform AOT compilation. A few of the CLI flags relating to "on by default" Wasm features have been changed to be "--disable-XYZ" flags. A simple example of using the `wasmtime compile` command: ```text $ wasmtime compile input.wasm $ wasmtime input.cwasm ```
This commit is contained in:
@@ -1,57 +1,61 @@
|
||||
use anyhow::Result;
|
||||
use std::io::BufWriter;
|
||||
use wasmtime::*;
|
||||
|
||||
#[test]
|
||||
fn caches_across_engines() {
|
||||
let mut c = Config::new();
|
||||
c.cranelift_clear_cpu_flags();
|
||||
let c = Config::new();
|
||||
|
||||
let bytes = Module::new(&Engine::new(&c).unwrap(), "(module)")
|
||||
.unwrap()
|
||||
.serialize()
|
||||
.unwrap();
|
||||
|
||||
let res = Module::deserialize(
|
||||
&Engine::new(&Config::new().cranelift_clear_cpu_flags()).unwrap(),
|
||||
&bytes,
|
||||
);
|
||||
let res = Module::deserialize(&Engine::new(&Config::new()).unwrap(), &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),
|
||||
)
|
||||
.unwrap(),
|
||||
&Engine::new(&Config::new().cranelift_nan_canonicalization(true)).unwrap(),
|
||||
&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),
|
||||
)
|
||||
.unwrap(),
|
||||
&Engine::new(&Config::new().cranelift_opt_level(OptLevel::None)).unwrap(),
|
||||
&bytes,
|
||||
);
|
||||
assert!(res.is_err());
|
||||
|
||||
// differ in cpu-specific flags
|
||||
// Missing required cpu 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()
|
||||
})
|
||||
.unwrap(),
|
||||
&Engine::new(&Config::new().cranelift_clear_cpu_flags()).unwrap(),
|
||||
&bytes,
|
||||
);
|
||||
assert!(res.is_err());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn aot_compiles() -> Result<()> {
|
||||
let engine = Engine::default();
|
||||
let mut writer = BufWriter::new(Vec::new());
|
||||
Module::compile(
|
||||
&engine,
|
||||
"(module (func (export \"f\") (param i32) (result i32) local.get 0))".as_bytes(),
|
||||
&mut writer,
|
||||
)?;
|
||||
|
||||
let bytes = writer.into_inner()?;
|
||||
let module = Module::from_binary(&engine, &bytes)?;
|
||||
|
||||
let store = Store::new(&engine);
|
||||
let instance = Instance::new(&store, &module, &[])?;
|
||||
|
||||
let f = instance.get_typed_func::<i32, i32>("f")?;
|
||||
assert_eq!(f.call(101).unwrap(), 101);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user