Merge pull request #2791 from peterhuene/compile-command
Add a compile command to Wasmtime.
This commit is contained in:
273
src/commands/compile.rs
Normal file
273
src/commands/compile.rs
Normal file
@@ -0,0 +1,273 @@
|
||||
//! The module that implements the `wasmtime compile` command.
|
||||
|
||||
use crate::CommonOptions;
|
||||
use anyhow::{bail, Context, Result};
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use structopt::{clap::AppSettings, StructOpt};
|
||||
use target_lexicon::Triple;
|
||||
use wasmtime::Engine;
|
||||
|
||||
lazy_static::lazy_static! {
|
||||
static ref AFTER_HELP: String = {
|
||||
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::WASM_FEATURES.as_str()
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
/// Compiles a WebAssembly module.
|
||||
#[derive(StructOpt)]
|
||||
#[structopt(
|
||||
name = "compile",
|
||||
version = env!("CARGO_PKG_VERSION"),
|
||||
setting = AppSettings::ColoredHelp,
|
||||
after_help = AFTER_HELP.as_str()
|
||||
)]
|
||||
pub struct CompileCommand {
|
||||
#[structopt(flatten)]
|
||||
common: CommonOptions,
|
||||
|
||||
/// Enable support for interrupting WebAssembly code.
|
||||
#[structopt(long)]
|
||||
interruptable: bool,
|
||||
|
||||
/// The target triple; default is the host triple
|
||||
#[structopt(long, value_name = "TARGET")]
|
||||
target: Option<String>,
|
||||
|
||||
/// The path of the output compiled module; defaults to <MODULE>.cwasm
|
||||
#[structopt(short = "o", long, value_name = "OUTPUT", parse(from_os_str))]
|
||||
output: Option<PathBuf>,
|
||||
|
||||
/// The path of the WebAssembly to compile
|
||||
#[structopt(index = 1, value_name = "MODULE", parse(from_os_str))]
|
||||
module: PathBuf,
|
||||
}
|
||||
|
||||
impl CompileCommand {
|
||||
/// Executes the command.
|
||||
pub fn execute(mut self) -> Result<()> {
|
||||
self.common.init_logging();
|
||||
|
||||
let target = self
|
||||
.target
|
||||
.take()
|
||||
.unwrap_or_else(|| Triple::host().to_string());
|
||||
|
||||
let mut config = self.common.config(Some(&target))?;
|
||||
config.interruptable(self.interruptable);
|
||||
|
||||
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 = fs::read(&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
|
||||
});
|
||||
|
||||
fs::write(output, engine.precompile_module(&input)?)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
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::from_iter_safe(vec![
|
||||
"compile",
|
||||
"--disable-logging",
|
||||
"-o",
|
||||
output_path.to_str().unwrap(),
|
||||
input_path.to_str().unwrap(),
|
||||
])?;
|
||||
|
||||
command.execute()?;
|
||||
|
||||
let engine = Engine::default();
|
||||
let module = Module::from_file(&engine, output_path)?;
|
||||
let store = Store::new(&engine);
|
||||
let instance = Instance::new(&store, &module, &[])?;
|
||||
let f = instance.get_typed_func::<i32, i32>("f")?;
|
||||
assert_eq!(f.call(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();
|
||||
|
||||
// Set all the x64 flags to make sure they work
|
||||
let command = CompileCommand::from_iter_safe(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_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();
|
||||
|
||||
// Set all the aarch64 flags to make sure they work
|
||||
let command = CompileCommand::from_iter_safe(vec![
|
||||
"compile",
|
||||
"--disable-logging",
|
||||
"--cranelift-enable",
|
||||
"has_lse",
|
||||
"-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();
|
||||
|
||||
// aarch64 flags should not be supported
|
||||
let command = CompileCommand::from_iter_safe(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::from_iter_safe(vec![
|
||||
"compile",
|
||||
"--disable-logging",
|
||||
"--cranelift-enable",
|
||||
preset,
|
||||
"-o",
|
||||
output_path.to_str().unwrap(),
|
||||
input_path.to_str().unwrap(),
|
||||
])?;
|
||||
|
||||
command.execute()?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,7 @@ pub enum ConfigCommand {
|
||||
|
||||
impl ConfigCommand {
|
||||
/// Executes the command.
|
||||
pub fn execute(&self) -> Result<()> {
|
||||
pub fn execute(self) -> Result<()> {
|
||||
match self {
|
||||
Self::New(c) => c.execute(),
|
||||
}
|
||||
@@ -35,7 +35,7 @@ pub struct ConfigNewCommand {
|
||||
|
||||
impl ConfigNewCommand {
|
||||
/// Executes the command.
|
||||
pub fn execute(&self) -> Result<()> {
|
||||
pub fn execute(self) -> Result<()> {
|
||||
let path = wasmtime_cache::create_new_config(self.path.as_ref())?;
|
||||
|
||||
println!(
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//! The module that implements the `wasmtime run` command.
|
||||
|
||||
use crate::{init_file_per_thread_logger, CommonOptions};
|
||||
use crate::CommonOptions;
|
||||
use anyhow::{bail, Context as _, Result};
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
@@ -28,9 +28,8 @@ use wasmtime_wasi_crypto::{
|
||||
fn parse_module(s: &OsStr) -> Result<PathBuf, OsString> {
|
||||
// Do not accept wasmtime subcommand names as the module name
|
||||
match s.to_str() {
|
||||
Some("help") | Some("config") | Some("run") | Some("wasm2obj") | Some("wast") => {
|
||||
Err("module name cannot be the same as a subcommand".into())
|
||||
}
|
||||
Some("help") | Some("config") | Some("run") | Some("wasm2obj") | Some("wast")
|
||||
| Some("compile") => Err("module name cannot be the same as a subcommand".into()),
|
||||
_ => Ok(s.into()),
|
||||
}
|
||||
}
|
||||
@@ -69,9 +68,15 @@ fn parse_preloads(s: &str) -> Result<(String, PathBuf)> {
|
||||
Ok((parts[0].into(), parts[1].into()))
|
||||
}
|
||||
|
||||
lazy_static::lazy_static! {
|
||||
static ref AFTER_HELP: String = {
|
||||
crate::WASM_FEATURES.to_string()
|
||||
};
|
||||
}
|
||||
|
||||
/// Runs a WebAssembly module
|
||||
#[derive(StructOpt)]
|
||||
#[structopt(name = "run", setting = AppSettings::TrailingVarArg)]
|
||||
#[structopt(name = "run", setting = AppSettings::TrailingVarArg, after_help = AFTER_HELP.as_str())]
|
||||
pub struct RunCommand {
|
||||
#[structopt(flatten)]
|
||||
common: CommonOptions,
|
||||
@@ -96,7 +101,7 @@ pub struct RunCommand {
|
||||
#[structopt(
|
||||
index = 1,
|
||||
required = true,
|
||||
value_name = "WASM_MODULE",
|
||||
value_name = "MODULE",
|
||||
parse(try_from_os_str = parse_module),
|
||||
)]
|
||||
module: PathBuf,
|
||||
@@ -127,14 +132,9 @@ pub struct RunCommand {
|
||||
impl RunCommand {
|
||||
/// Executes the command.
|
||||
pub fn execute(&self) -> Result<()> {
|
||||
if self.common.log_to_files {
|
||||
let prefix = "wasmtime.dbg.";
|
||||
init_file_per_thread_logger(prefix);
|
||||
} else {
|
||||
pretty_env_logger::init();
|
||||
}
|
||||
self.common.init_logging();
|
||||
|
||||
let mut config = self.common.config()?;
|
||||
let mut config = self.common.config(None)?;
|
||||
if self.wasm_timeout.is_some() {
|
||||
config.interruptable(true);
|
||||
}
|
||||
|
||||
98
src/commands/settings.rs
Normal file
98
src/commands/settings.rs
Normal file
@@ -0,0 +1,98 @@
|
||||
//! The module that implements the `wasmtime settings` command.
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
use std::str::FromStr;
|
||||
use structopt::StructOpt;
|
||||
use wasmtime_environ::settings::{self, Setting, SettingKind};
|
||||
use wasmtime_jit::native;
|
||||
|
||||
/// Displays available Cranelift settings for a target.
|
||||
#[derive(StructOpt)]
|
||||
#[structopt(name = "run")]
|
||||
pub struct SettingsCommand {
|
||||
/// The target triple to get the settings for; defaults to the host triple.
|
||||
#[structopt(long, value_name = "TARGET")]
|
||||
target: Option<String>,
|
||||
}
|
||||
|
||||
impl SettingsCommand {
|
||||
/// Executes the command.
|
||||
pub fn execute(self) -> Result<()> {
|
||||
let settings = match &self.target {
|
||||
Some(target) => {
|
||||
native::lookup(target_lexicon::Triple::from_str(target).map_err(|e| anyhow!(e))?)?
|
||||
}
|
||||
None => native::builder(),
|
||||
};
|
||||
|
||||
let mut enums = (Vec::new(), 0, "Enum settings:");
|
||||
let mut nums = (Vec::new(), 0, "Numerical settings:");
|
||||
let mut bools = (Vec::new(), 0, "Boolean settings:");
|
||||
let mut presets = (Vec::new(), 0, "Presets:");
|
||||
|
||||
for setting in settings.iter() {
|
||||
let (collection, max, _) = match setting.kind {
|
||||
SettingKind::Enum => &mut enums,
|
||||
SettingKind::Num => &mut nums,
|
||||
SettingKind::Bool => &mut bools,
|
||||
SettingKind::Preset => &mut presets,
|
||||
};
|
||||
|
||||
if setting.name.len() > *max {
|
||||
*max = setting.name.len();
|
||||
}
|
||||
|
||||
collection.push(setting);
|
||||
}
|
||||
|
||||
if enums.0.is_empty() && nums.0.is_empty() && bools.0.is_empty() && presets.0.is_empty() {
|
||||
println!("Target '{}' has no settings.", settings.triple());
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
println!("Cranelift settings for target '{}':", settings.triple());
|
||||
|
||||
for (collection, max, header) in &mut [enums, nums, bools, presets] {
|
||||
if collection.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
collection.sort_by_key(|k| k.name);
|
||||
println!();
|
||||
Self::print_settings(header, collection, *max);
|
||||
}
|
||||
|
||||
if self.target.is_none() {
|
||||
let isa = settings.finish(settings::Flags::new(settings::builder()));
|
||||
println!();
|
||||
println!("Settings inferred for the current host:");
|
||||
|
||||
let mut values = isa.isa_flags();
|
||||
values.sort_by_key(|k| k.name);
|
||||
|
||||
for value in values {
|
||||
if value.as_bool().unwrap_or(false) {
|
||||
println!(" {}", value.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn print_settings(header: &str, settings: &[Setting], width: usize) {
|
||||
println!("{}", header);
|
||||
for setting in settings {
|
||||
println!(
|
||||
" {:width$} {}{}",
|
||||
setting.name,
|
||||
setting.description,
|
||||
setting
|
||||
.values
|
||||
.map(|v| format!(" Supported values: {}.", v.join(", ")))
|
||||
.unwrap_or("".to_string()),
|
||||
width = width + 2
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,23 +1,26 @@
|
||||
//! The module that implements the `wasmtime wasm2obj` command.
|
||||
|
||||
use crate::obj::compile_to_obj;
|
||||
use crate::{init_file_per_thread_logger, pick_compilation_strategy, CommonOptions};
|
||||
use anyhow::{anyhow, Context as _, Result};
|
||||
use crate::{parse_target, pick_compilation_strategy, CommonOptions};
|
||||
use anyhow::{Context as _, Result};
|
||||
use std::{
|
||||
fs::File,
|
||||
io::Write,
|
||||
path::{Path, PathBuf},
|
||||
str::FromStr,
|
||||
};
|
||||
use structopt::{clap::AppSettings, StructOpt};
|
||||
use target_lexicon::Triple;
|
||||
|
||||
/// The after help text for the `wasm2obj` command.
|
||||
pub const WASM2OBJ_AFTER_HELP: &str = "The translation is dependent on the environment chosen.\n\
|
||||
The default is a dummy environment that produces placeholder values.";
|
||||
|
||||
fn parse_target(s: &str) -> Result<Triple> {
|
||||
Triple::from_str(&s).map_err(|e| anyhow!(e))
|
||||
lazy_static::lazy_static! {
|
||||
static ref AFTER_HELP: String = {
|
||||
format!(
|
||||
"The translation is dependent on the environment chosen.\n\
|
||||
The default is a dummy environment that produces placeholder values.\n\
|
||||
\n\
|
||||
{}",
|
||||
crate::WASM_FEATURES.as_str()
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
/// Translates a WebAssembly module to native object file
|
||||
@@ -26,7 +29,7 @@ fn parse_target(s: &str) -> Result<Triple> {
|
||||
name = "wasm2obj",
|
||||
version = env!("CARGO_PKG_VERSION"),
|
||||
setting = AppSettings::ColoredHelp,
|
||||
after_help = WASM2OBJ_AFTER_HELP,
|
||||
after_help = AFTER_HELP.as_str(),
|
||||
)]
|
||||
pub struct WasmToObjCommand {
|
||||
#[structopt(flatten)]
|
||||
@@ -47,17 +50,8 @@ pub struct WasmToObjCommand {
|
||||
|
||||
impl WasmToObjCommand {
|
||||
/// Executes the command.
|
||||
pub fn execute(&self) -> Result<()> {
|
||||
self.handle_module()
|
||||
}
|
||||
|
||||
fn handle_module(&self) -> Result<()> {
|
||||
if self.common.log_to_files {
|
||||
let prefix = "wasm2obj.dbg.";
|
||||
init_file_per_thread_logger(prefix);
|
||||
} else {
|
||||
pretty_env_logger::init();
|
||||
}
|
||||
pub fn execute(self) -> Result<()> {
|
||||
self.common.init_logging();
|
||||
|
||||
let strategy = pick_compilation_strategy(self.common.cranelift, self.common.lightbeam)?;
|
||||
|
||||
|
||||
@@ -1,18 +1,25 @@
|
||||
//! The module that implements the `wasmtime wast` command.
|
||||
|
||||
use crate::{init_file_per_thread_logger, CommonOptions};
|
||||
use crate::CommonOptions;
|
||||
use anyhow::{Context as _, Result};
|
||||
use std::path::PathBuf;
|
||||
use structopt::{clap::AppSettings, StructOpt};
|
||||
use wasmtime::{Engine, Store};
|
||||
use wasmtime_wast::WastContext;
|
||||
|
||||
lazy_static::lazy_static! {
|
||||
static ref AFTER_HELP: String = {
|
||||
crate::WASM_FEATURES.to_string()
|
||||
};
|
||||
}
|
||||
|
||||
/// Runs a WebAssembly test script file
|
||||
#[derive(StructOpt)]
|
||||
#[structopt(
|
||||
name = "wast",
|
||||
version = env!("CARGO_PKG_VERSION"),
|
||||
setting = AppSettings::ColoredHelp,
|
||||
after_help = AFTER_HELP.as_str(),
|
||||
)]
|
||||
pub struct WastCommand {
|
||||
#[structopt(flatten)]
|
||||
@@ -25,15 +32,10 @@ pub struct WastCommand {
|
||||
|
||||
impl WastCommand {
|
||||
/// Executes the command.
|
||||
pub fn execute(&self) -> Result<()> {
|
||||
if self.common.log_to_files {
|
||||
let prefix = "wast.dbg.";
|
||||
init_file_per_thread_logger(prefix);
|
||||
} else {
|
||||
pretty_env_logger::init();
|
||||
}
|
||||
pub fn execute(self) -> Result<()> {
|
||||
self.common.init_logging();
|
||||
|
||||
let config = self.common.config()?;
|
||||
let config = self.common.config(None)?;
|
||||
let store = Store::new(&Engine::new(&config)?);
|
||||
let mut wast_context = WastContext::new(store);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user