use anyhow::{bail, Context, Result};
use clap::Parser;
use once_cell::sync::Lazy;
use std::fs;
use std::path::PathBuf;
use wasmtime::Engine;
use wasmtime_cli_flags::CommonOptions;
static AFTER_HELP: Lazy<String> = Lazy::new(|| {
format!(
"By default, no CPU features or presets will be enabled for the compilation.\n\
\n\
{}\
\n\
Usage examples:\n\
\n\
Compiling a WebAssembly module for the current platform:\n\
\n \
wasmtime compile example.wasm
\n\
Specifying the output file:\n\
\n \
wasmtime compile -o output.cwasm input.wasm\n\
\n\
Compiling for a specific platform (Linux) and CPU preset (Skylake):\n\
\n \
wasmtime compile --target x86_64-unknown-linux --cranelift-enable skylake foo.wasm\n",
crate::FLAG_EXPLANATIONS.as_str()
)
});
#[derive(Parser)]
#[structopt(
name = "compile",
version,
after_help = AFTER_HELP.as_str()
)]
pub struct CompileCommand {
#[clap(flatten)]
common: CommonOptions,
#[clap(long, value_name = "TARGET")]
target: Option<String>,
#[clap(short = 'o', long, value_name = "OUTPUT", parse(from_os_str))]
output: Option<PathBuf>,
#[clap(long = "emit-clif", value_name = "PATH", parse(from_os_str))]
emit_clif: Option<PathBuf>,
#[clap(index = 1, value_name = "MODULE", parse(from_os_str))]
module: PathBuf,
}
impl CompileCommand {
pub fn execute(mut self) -> Result<()> {
self.common.init_logging();
let mut config = self.common.config(self.target.as_deref())?;
if let Some(path) = self.emit_clif {
if !path.exists() {
std::fs::create_dir(&path)?;
}
if !path.is_dir() {
bail!(
"the path passed for '--emit-clif' ({}) must be a directory",
path.display()
);
}
config.emit_clif(&path);
}
let engine = Engine::new(&config)?;
if self.module.file_name().is_none() {
bail!(
"'{}' is not a valid input module path",
self.module.display()
);
}
let input = wat::parse_file(&self.module).with_context(|| "failed to read input file")?;
let output = self.output.take().unwrap_or_else(|| {
let mut output: PathBuf = self.module.file_name().unwrap().into();
output.set_extension("cwasm");
output
});
#[cfg(feature = "component-model")]
{
if let Ok(wasmparser::Chunk::Parsed {
payload:
wasmparser::Payload::Version {
encoding: wasmparser::Encoding::Component,
..
},
..
}) = wasmparser::Parser::new(0).parse(&input, true)
{
fs::write(output, engine.precompile_component(&input)?)?;
return Ok(());
}
}
fs::write(output, engine.precompile_module(&input)?)?;
Ok(())
}
}
#[cfg(all(test, not(miri)))]
mod test {
use super::*;
use std::io::Write;
use tempfile::NamedTempFile;
use wasmtime::{Instance, Module, Store};
#[test]
fn test_successful_compile() -> Result<()> {
let (mut input, input_path) = NamedTempFile::new()?.into_parts();
input.write_all(
"(module (func (export \"f\") (param i32) (result i32) local.get 0))".as_bytes(),
)?;
drop(input);
let output_path = NamedTempFile::new()?.into_temp_path();
let command = CompileCommand::try_parse_from(vec![
"compile",
"--disable-logging",
"-o",
output_path.to_str().unwrap(),
input_path.to_str().unwrap(),
])?;
command.execute()?;
let engine = Engine::default();
let contents = std::fs::read(output_path)?;
let module = unsafe { Module::deserialize(&engine, contents)? };
let mut store = Store::new(&engine, ());
let instance = Instance::new(&mut store, &module, &[])?;
let f = instance.get_typed_func::<i32, i32>(&mut store, "f")?;
assert_eq!(f.call(&mut store, 1234).unwrap(), 1234);
Ok(())
}
#[cfg(target_arch = "x86_64")]
#[test]
fn test_x64_flags_compile() -> Result<()> {
let (mut input, input_path) = NamedTempFile::new()?.into_parts();
input.write_all("(module)".as_bytes())?;
drop(input);
let output_path = NamedTempFile::new()?.into_temp_path();
let command = CompileCommand::try_parse_from(vec![
"compile",
"--disable-logging",
"--cranelift-enable",
"has_sse3",
"--cranelift-enable",
"has_ssse3",
"--cranelift-enable",
"has_sse41",
"--cranelift-enable",
"has_sse42",
"--cranelift-enable",
"has_avx",
"--cranelift-enable",
"has_avx2",
"--cranelift-enable",
"has_fma",
"--cranelift-enable",
"has_avx512dq",
"--cranelift-enable",
"has_avx512vl",
"--cranelift-enable",
"has_avx512f",
"--cranelift-enable",
"has_popcnt",
"--cranelift-enable",
"has_bmi1",
"--cranelift-enable",
"has_bmi2",
"--cranelift-enable",
"has_lzcnt",
"-o",
output_path.to_str().unwrap(),
input_path.to_str().unwrap(),
])?;
command.execute()?;
Ok(())
}
#[cfg(target_arch = "aarch64")]
#[test]
fn test_aarch64_flags_compile() -> Result<()> {
let (mut input, input_path) = NamedTempFile::new()?.into_parts();
input.write_all("(module)".as_bytes())?;
drop(input);
let output_path = NamedTempFile::new()?.into_temp_path();
let command = CompileCommand::try_parse_from(vec![
"compile",
"--disable-logging",
"--cranelift-enable",
"has_lse",
"--cranelift-enable",
"has_pauth",
"--cranelift-enable",
"sign_return_address",
"--cranelift-enable",
"sign_return_address_all",
"--cranelift-enable",
"sign_return_address_with_bkey",
"-o",
output_path.to_str().unwrap(),
input_path.to_str().unwrap(),
])?;
command.execute()?;
Ok(())
}
#[cfg(target_arch = "x86_64")]
#[test]
fn test_unsupported_flags_compile() -> Result<()> {
let (mut input, input_path) = NamedTempFile::new()?.into_parts();
input.write_all("(module)".as_bytes())?;
drop(input);
let output_path = NamedTempFile::new()?.into_temp_path();
let command = CompileCommand::try_parse_from(vec![
"compile",
"--disable-logging",
"--cranelift-enable",
"has_lse",
"-o",
output_path.to_str().unwrap(),
input_path.to_str().unwrap(),
])?;
assert_eq!(
command.execute().unwrap_err().to_string(),
"No existing setting named 'has_lse'"
);
Ok(())
}
#[cfg(target_arch = "x86_64")]
#[test]
fn test_x64_presets_compile() -> Result<()> {
let (mut input, input_path) = NamedTempFile::new()?.into_parts();
input.write_all("(module)".as_bytes())?;
drop(input);
let output_path = NamedTempFile::new()?.into_temp_path();
for preset in &[
"nehalem",
"haswell",
"broadwell",
"skylake",
"cannonlake",
"icelake",
"znver1",
] {
let command = CompileCommand::try_parse_from(vec![
"compile",
"--disable-logging",
"--cranelift-enable",
preset,
"-o",
output_path.to_str().unwrap(),
input_path.to_str().unwrap(),
])?;
command.execute()?;
}
Ok(())
}
}