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:
@@ -6,7 +6,7 @@
|
||||
use anyhow::Result;
|
||||
use structopt::{clap::AppSettings, clap::ErrorKind, StructOpt};
|
||||
use wasmtime_cli::commands::{
|
||||
ConfigCommand, RunCommand, WasmToObjCommand, WastCommand, WASM2OBJ_AFTER_HELP,
|
||||
CompileCommand, ConfigCommand, RunCommand, WasmToObjCommand, WastCommand, WASM2OBJ_AFTER_HELP,
|
||||
};
|
||||
|
||||
/// Wasmtime WebAssembly Runtime
|
||||
@@ -38,6 +38,8 @@ enum WasmtimeApp {
|
||||
// !!! IMPORTANT: if subcommands are added or removed, update `parse_module` in `src/commands/run.rs`. !!!
|
||||
/// Controls Wasmtime configuration settings
|
||||
Config(ConfigCommand),
|
||||
/// Compiles a WebAssembly module.
|
||||
Compile(CompileCommand),
|
||||
/// Runs a WebAssembly module
|
||||
Run(RunCommand),
|
||||
/// Translates a WebAssembly module to native object file
|
||||
@@ -49,9 +51,10 @@ enum WasmtimeApp {
|
||||
|
||||
impl WasmtimeApp {
|
||||
/// Executes the command.
|
||||
pub fn execute(&self) -> Result<()> {
|
||||
pub fn execute(self) -> Result<()> {
|
||||
match self {
|
||||
Self::Config(c) => c.execute(),
|
||||
Self::Compile(c) => c.execute(),
|
||||
Self::Run(c) => c.execute(),
|
||||
Self::WasmToObj(c) => c.execute(),
|
||||
Self::Wast(c) => c.execute(),
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
//! The module for the Wasmtime CLI commands.
|
||||
|
||||
mod compile;
|
||||
mod config;
|
||||
mod run;
|
||||
mod wasm2obj;
|
||||
mod wast;
|
||||
|
||||
pub use self::{config::*, run::*, wasm2obj::*, wast::*};
|
||||
pub use self::{compile::*, config::*, run::*, wasm2obj::*, wast::*};
|
||||
|
||||
413
src/commands/compile.rs
Normal file
413
src/commands/compile.rs
Normal file
@@ -0,0 +1,413 @@
|
||||
//! The module that implements the `wasmtime wast` command.
|
||||
|
||||
use crate::{init_file_per_thread_logger, CommonOptions};
|
||||
use anyhow::{anyhow, bail, Context, Result};
|
||||
use std::fs::{self, File};
|
||||
use std::io::BufWriter;
|
||||
use std::path::PathBuf;
|
||||
use structopt::{
|
||||
clap::{AppSettings, ArgGroup},
|
||||
StructOpt,
|
||||
};
|
||||
use target_lexicon::Triple;
|
||||
use wasmtime::{Config, Engine, Module};
|
||||
|
||||
/// Compiles a WebAssembly module.
|
||||
#[derive(StructOpt)]
|
||||
#[structopt(
|
||||
name = "compile",
|
||||
version = env!("CARGO_PKG_VERSION"),
|
||||
setting = AppSettings::ColoredHelp,
|
||||
group = ArgGroup::with_name("x64").multiple(true),
|
||||
group = ArgGroup::with_name("preset-x64"),
|
||||
group = ArgGroup::with_name("aarch64").multiple(true).conflicts_with_all(&["x64", "preset-x64"]),
|
||||
group = ArgGroup::with_name("preset-aarch64").conflicts_with_all(&["x64", "preset-x64"]),
|
||||
after_help = "By default, no CPU flags will be enabled for the compilation.\n\
|
||||
\n\
|
||||
Use the various preset and CPU flag options for the environment being targeted.\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 --skylake foo.wasm\n"
|
||||
)]
|
||||
pub struct CompileCommand {
|
||||
#[structopt(flatten)]
|
||||
common: CommonOptions,
|
||||
|
||||
/// Enable support for interrupting WebAssembly code.
|
||||
#[structopt(long)]
|
||||
interruptable: bool,
|
||||
|
||||
/// Enable SSE3 support (for x86-64 targets).
|
||||
#[structopt(long, group = "x64")]
|
||||
sse3: bool,
|
||||
|
||||
/// Enable SSSE3 support (for x86-64 targets).
|
||||
#[structopt(long, group = "x64")]
|
||||
ssse3: bool,
|
||||
|
||||
/// Enable SSE41 support (for x86-64 targets).
|
||||
#[structopt(long, group = "x64")]
|
||||
sse41: bool,
|
||||
|
||||
/// Enable SSE42 support (for x86-64 targets).
|
||||
#[structopt(long, group = "x64")]
|
||||
sse42: bool,
|
||||
|
||||
/// Enable AVX support (for x86-64 targets).
|
||||
#[structopt(long, group = "x64")]
|
||||
avx: bool,
|
||||
|
||||
/// Enable AVX2 support (for x86-64 targets).
|
||||
#[structopt(long, group = "x64")]
|
||||
avx2: bool,
|
||||
|
||||
/// Enable AVX512DQ support (for x86-64 targets).
|
||||
#[structopt(long, group = "x64")]
|
||||
avx512dq: bool,
|
||||
|
||||
/// Enable AVX512VL support (for x86-64 targets).
|
||||
#[structopt(long, group = "x64")]
|
||||
avx512vl: bool,
|
||||
|
||||
/// Enable AVX512F support (for x86-64 targets).
|
||||
#[structopt(long, group = "x64")]
|
||||
avx512f: bool,
|
||||
|
||||
/// Enable POPCNT support (for x86-64 targets).
|
||||
#[structopt(long, group = "x64")]
|
||||
popcnt: bool,
|
||||
|
||||
/// Enable BMI1 support (for x86-64 targets).
|
||||
#[structopt(long, group = "x64")]
|
||||
bmi1: bool,
|
||||
|
||||
/// Enable BMI2 support (for x86-64 targets).
|
||||
#[structopt(long, group = "x64")]
|
||||
bmi2: bool,
|
||||
|
||||
/// Enable LZCNT support (for x86-64 targets).
|
||||
#[structopt(long, group = "x64")]
|
||||
lzcnt: bool,
|
||||
|
||||
/// Enable LSE support (for aarch64 targets).
|
||||
#[structopt(long, group = "aarch64")]
|
||||
lse: bool,
|
||||
|
||||
/// Enable Nehalem preset (for x86-64 targets).
|
||||
#[structopt(long, group = "x64", group = "preset-x64")]
|
||||
nehalem: bool,
|
||||
|
||||
/// Enable Haswell preset (for x86-64 targets).
|
||||
#[structopt(long, group = "x64", group = "preset-x64")]
|
||||
haswell: bool,
|
||||
|
||||
/// Enable Broadwell preset (for x86-64 targets).
|
||||
#[structopt(long, group = "x64", group = "preset-x64")]
|
||||
broadwell: bool,
|
||||
|
||||
/// Enable Skylake preset (for x86-64 targets).
|
||||
#[structopt(long, group = "x64", group = "preset-x64")]
|
||||
skylake: bool,
|
||||
|
||||
/// Enable Cannonlake preset (for x86-64 targets).
|
||||
#[structopt(long, group = "x64", group = "preset-x64")]
|
||||
cannonlake: bool,
|
||||
|
||||
/// Enable Icelake preset (for x86-64 targets).
|
||||
#[structopt(long, group = "x64", group = "preset-x64")]
|
||||
icelake: bool,
|
||||
|
||||
/// Enable Zen preset (for x86-64 targets).
|
||||
#[structopt(long, group = "x64", group = "preset-x64")]
|
||||
znver1: 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<()> {
|
||||
if !self.common.disable_logging {
|
||||
if self.common.log_to_files {
|
||||
let prefix = "wasmtime.dbg.";
|
||||
init_file_per_thread_logger(prefix);
|
||||
} else {
|
||||
pretty_env_logger::init();
|
||||
}
|
||||
}
|
||||
|
||||
let target = self
|
||||
.target
|
||||
.take()
|
||||
.unwrap_or_else(|| Triple::host().to_string());
|
||||
|
||||
let mut config = self.common.config(Some(&target))?;
|
||||
config.interruptable(self.interruptable);
|
||||
|
||||
self.set_flags(&mut config, &target)?;
|
||||
|
||||
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
|
||||
});
|
||||
|
||||
let mut writer = BufWriter::new(File::create(&output)?);
|
||||
Module::compile(&engine, &input, &mut writer)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn set_flags(&self, c: &mut Config, target: &str) -> Result<()> {
|
||||
use std::str::FromStr;
|
||||
|
||||
macro_rules! set_flag {
|
||||
($config:expr, $arch:expr, $flag:expr, $name:literal, $display:literal) => {
|
||||
if $flag {
|
||||
unsafe {
|
||||
$config.cranelift_flag_enable($name).map_err(|_| {
|
||||
anyhow!("{} is not supported for architecture '{}'", $display, $arch)
|
||||
})?;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
let arch = Triple::from_str(target).unwrap().architecture;
|
||||
|
||||
set_flag!(c, arch, self.sse3, "has_sse3", "SSE3");
|
||||
set_flag!(c, arch, self.ssse3, "has_ssse3", "SSSE3");
|
||||
set_flag!(c, arch, self.sse41, "has_sse41", "SSE41");
|
||||
set_flag!(c, arch, self.sse42, "has_sse42", "SSE42");
|
||||
set_flag!(c, arch, self.avx, "has_avx", "AVX");
|
||||
set_flag!(c, arch, self.avx2, "has_avx2", "AVX2");
|
||||
set_flag!(c, arch, self.avx512dq, "has_avx512dq", "AVX512DQ");
|
||||
set_flag!(c, arch, self.avx512vl, "has_avx512vl", "AVX512VL");
|
||||
set_flag!(c, arch, self.avx512f, "has_avx512f", "AVX512F");
|
||||
set_flag!(c, arch, self.popcnt, "has_popcnt", "POPCNT");
|
||||
set_flag!(c, arch, self.bmi1, "has_bmi1", "BMI1");
|
||||
set_flag!(c, arch, self.bmi2, "has_bmi2", "BMI2");
|
||||
set_flag!(c, arch, self.lzcnt, "has_lzcnt", "LZCNT");
|
||||
set_flag!(c, arch, self.lse, "has_lse", "LSE");
|
||||
set_flag!(c, arch, self.nehalem, "nehalem", "Nehalem preset");
|
||||
set_flag!(c, arch, self.haswell, "haswell", "Haswell preset");
|
||||
set_flag!(c, arch, self.broadwell, "broadwell", "Broadwell preset");
|
||||
set_flag!(c, arch, self.skylake, "skylake", "Skylake preset");
|
||||
set_flag!(c, arch, self.cannonlake, "cannonlake", "Cannonlake preset");
|
||||
set_flag!(c, arch, self.icelake, "icelake", "Icelake preset");
|
||||
set_flag!(c, arch, self.znver1, "znver1", "Zen preset");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use std::io::Write;
|
||||
use tempfile::NamedTempFile;
|
||||
use wasmtime::{Instance, 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",
|
||||
"--sse3",
|
||||
"--ssse3",
|
||||
"--sse41",
|
||||
"--sse42",
|
||||
"--avx",
|
||||
"--avx2",
|
||||
"--avx512dq",
|
||||
"--avx512vl",
|
||||
"--avx512f",
|
||||
"--popcnt",
|
||||
"--bmi1",
|
||||
"--bmi2",
|
||||
"--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",
|
||||
"--lse",
|
||||
"-o",
|
||||
output_path.to_str().unwrap(),
|
||||
input_path.to_str().unwrap(),
|
||||
])?;
|
||||
|
||||
command.execute()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
#[test]
|
||||
fn test_incompatible_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();
|
||||
|
||||
// x64 and aarch64 flags should conflict
|
||||
match CompileCommand::from_iter_safe(vec![
|
||||
"compile",
|
||||
"--disable-logging",
|
||||
"--sse3",
|
||||
"--lse",
|
||||
"-o",
|
||||
output_path.to_str().unwrap(),
|
||||
input_path.to_str().unwrap(),
|
||||
]) {
|
||||
Ok(_) => unreachable!(),
|
||||
Err(e) => {
|
||||
assert!(e
|
||||
.to_string()
|
||||
.contains("cannot be used with one or more of the other specified arguments"));
|
||||
}
|
||||
}
|
||||
|
||||
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",
|
||||
preset,
|
||||
"-o",
|
||||
output_path.to_str().unwrap(),
|
||||
input_path.to_str().unwrap(),
|
||||
])?;
|
||||
|
||||
command.execute()?;
|
||||
}
|
||||
|
||||
// Two presets should conflict
|
||||
match CompileCommand::from_iter_safe(vec![
|
||||
"compile",
|
||||
"--disable-logging",
|
||||
"--broadwell",
|
||||
"--cannonlake",
|
||||
"-o",
|
||||
output_path.to_str().unwrap(),
|
||||
input_path.to_str().unwrap(),
|
||||
]) {
|
||||
Ok(_) => unreachable!(),
|
||||
Err(e) => {
|
||||
assert!(e
|
||||
.to_string()
|
||||
.contains("cannot be used with one or more of the other specified arguments"));
|
||||
}
|
||||
}
|
||||
|
||||
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!(
|
||||
|
||||
@@ -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()),
|
||||
}
|
||||
}
|
||||
@@ -96,7 +95,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 +126,16 @@ 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();
|
||||
if !self.common.disable_logging {
|
||||
if self.common.log_to_files {
|
||||
let prefix = "wasmtime.dbg.";
|
||||
init_file_per_thread_logger(prefix);
|
||||
} else {
|
||||
pretty_env_logger::init();
|
||||
}
|
||||
}
|
||||
|
||||
let mut config = self.common.config()?;
|
||||
let mut config = self.common.config(None)?;
|
||||
if self.wasm_timeout.is_some() {
|
||||
config.interruptable(true);
|
||||
}
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
//! 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::{init_file_per_thread_logger, 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;
|
||||
@@ -16,10 +15,6 @@ use target_lexicon::Triple;
|
||||
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))
|
||||
}
|
||||
|
||||
/// Translates a WebAssembly module to native object file
|
||||
#[derive(StructOpt)]
|
||||
#[structopt(
|
||||
@@ -47,16 +42,14 @@ 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<()> {
|
||||
if !self.common.disable_logging {
|
||||
if self.common.log_to_files {
|
||||
let prefix = "wasm2obj.dbg.";
|
||||
init_file_per_thread_logger(prefix);
|
||||
} else {
|
||||
pretty_env_logger::init();
|
||||
}
|
||||
}
|
||||
|
||||
let strategy = pick_compilation_strategy(self.common.cranelift, self.common.lightbeam)?;
|
||||
|
||||
@@ -25,15 +25,17 @@ 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<()> {
|
||||
if !self.common.disable_logging {
|
||||
if self.common.log_to_files {
|
||||
let prefix = "wast.dbg.";
|
||||
init_file_per_thread_logger(prefix);
|
||||
} else {
|
||||
pretty_env_logger::init();
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
|
||||
72
src/lib.rs
72
src/lib.rs
@@ -29,6 +29,7 @@ mod obj;
|
||||
use anyhow::{bail, Result};
|
||||
use std::path::PathBuf;
|
||||
use structopt::StructOpt;
|
||||
use target_lexicon::Triple;
|
||||
use wasmtime::{Config, ProfilingStrategy, Strategy};
|
||||
|
||||
pub use obj::compile_to_obj;
|
||||
@@ -91,6 +92,10 @@ struct CommonOptions {
|
||||
#[structopt(long, conflicts_with = "lightbeam")]
|
||||
cranelift: bool,
|
||||
|
||||
/// Disable logging.
|
||||
#[structopt(long, conflicts_with = "log_to_files")]
|
||||
disable_logging: bool,
|
||||
|
||||
/// Log to per-thread log files instead of stderr.
|
||||
#[structopt(long)]
|
||||
log_to_files: bool,
|
||||
@@ -107,21 +112,21 @@ struct CommonOptions {
|
||||
#[structopt(long)]
|
||||
enable_simd: bool,
|
||||
|
||||
/// Enable support for reference types
|
||||
/// Disable support for reference types
|
||||
#[structopt(long)]
|
||||
enable_reference_types: Option<bool>,
|
||||
disable_reference_types: bool,
|
||||
|
||||
/// Enable support for multi-value functions
|
||||
/// Disable support for multi-value functions
|
||||
#[structopt(long)]
|
||||
enable_multi_value: Option<bool>,
|
||||
disable_multi_value: bool,
|
||||
|
||||
/// Enable support for Wasm threads
|
||||
#[structopt(long)]
|
||||
enable_threads: bool,
|
||||
|
||||
/// Enable support for bulk memory instructions
|
||||
/// Disable support for bulk memory instructions
|
||||
#[structopt(long)]
|
||||
enable_bulk_memory: Option<bool>,
|
||||
disable_bulk_memory: bool,
|
||||
|
||||
/// Enable support for the multi-memory proposal
|
||||
#[structopt(long)]
|
||||
@@ -151,30 +156,34 @@ struct CommonOptions {
|
||||
#[structopt(short = "O", long)]
|
||||
optimize: bool,
|
||||
|
||||
/// Optimization level for generated functions (0 (none), 1, 2 (most), or s
|
||||
/// (size))
|
||||
/// Optimization level for generated functions: 0 (none), 1, 2 (most), or s
|
||||
/// (size); defaults to "most"
|
||||
#[structopt(
|
||||
long,
|
||||
value_name = "LEVEL",
|
||||
parse(try_from_str = parse_opt_level),
|
||||
default_value = "2",
|
||||
)]
|
||||
opt_level: wasmtime::OptLevel,
|
||||
opt_level: Option<wasmtime::OptLevel>,
|
||||
|
||||
/// Other Cranelift flags to be passed down to Cranelift.
|
||||
#[structopt(long, parse(try_from_str = parse_cranelift_flag))]
|
||||
/// Cranelift common flags to set.
|
||||
#[structopt(long = "cranelift-flag", value_name = "NAME=VALUE", parse(try_from_str = parse_cranelift_flag))]
|
||||
cranelift_flags: Vec<CraneliftFlag>,
|
||||
|
||||
/// The Cranelift ISA preset to use.
|
||||
#[structopt(long, value_name = "PRESET")]
|
||||
cranelift_preset: Option<String>,
|
||||
|
||||
/// Maximum size in bytes of wasm memory before it becomes dynamically
|
||||
/// relocatable instead of up-front-reserved.
|
||||
#[structopt(long)]
|
||||
#[structopt(long, value_name = "MAXIMUM")]
|
||||
static_memory_maximum_size: Option<u64>,
|
||||
|
||||
/// Byte size of the guard region after static memories are allocated.
|
||||
#[structopt(long)]
|
||||
#[structopt(long, value_name = "SIZE")]
|
||||
static_memory_guard_size: Option<u64>,
|
||||
|
||||
/// Byte size of the guard region after dynamic memories are allocated.
|
||||
#[structopt(long)]
|
||||
#[structopt(long, value_name = "SIZE")]
|
||||
dynamic_memory_guard_size: Option<u64>,
|
||||
|
||||
/// Enable Cranelift's internal debug verifier (expensive)
|
||||
@@ -187,19 +196,22 @@ struct CommonOptions {
|
||||
}
|
||||
|
||||
impl CommonOptions {
|
||||
fn config(&self) -> Result<Config> {
|
||||
let mut config = Config::new();
|
||||
fn config(&self, target: Option<&str>) -> Result<Config> {
|
||||
let mut config = if let Some(target) = target {
|
||||
Config::for_target(target)?
|
||||
} else {
|
||||
Config::new()
|
||||
};
|
||||
|
||||
config
|
||||
.cranelift_debug_verifier(self.enable_cranelift_debug_verifier)
|
||||
.debug_info(self.debug_info)
|
||||
.wasm_simd(self.enable_simd || self.enable_all)
|
||||
.wasm_bulk_memory(self.enable_bulk_memory.unwrap_or(true) || self.enable_all)
|
||||
.wasm_bulk_memory(!self.disable_bulk_memory || self.enable_all)
|
||||
.wasm_reference_types(
|
||||
self.enable_reference_types
|
||||
.unwrap_or(cfg!(target_arch = "x86_64"))
|
||||
|| self.enable_all,
|
||||
(!self.disable_reference_types || cfg!(target_arch = "x86_64")) || self.enable_all,
|
||||
)
|
||||
.wasm_multi_value(self.enable_multi_value.unwrap_or(true) || self.enable_all)
|
||||
.wasm_multi_value(!self.disable_multi_value || self.enable_all)
|
||||
.wasm_threads(self.enable_threads || self.enable_all)
|
||||
.wasm_multi_memory(self.enable_multi_memory || self.enable_all)
|
||||
.wasm_module_linking(self.enable_module_linking || self.enable_all)
|
||||
@@ -207,11 +219,19 @@ impl CommonOptions {
|
||||
.strategy(pick_compilation_strategy(self.cranelift, self.lightbeam)?)?
|
||||
.profiler(pick_profiling_strategy(self.jitdump, self.vtune)?)?
|
||||
.cranelift_nan_canonicalization(self.enable_cranelift_nan_canonicalization);
|
||||
|
||||
if let Some(preset) = &self.cranelift_preset {
|
||||
unsafe {
|
||||
config.cranelift_flag_enable(preset)?;
|
||||
}
|
||||
}
|
||||
|
||||
for CraneliftFlag { name, value } in &self.cranelift_flags {
|
||||
unsafe {
|
||||
config.cranelift_other_flag(name, value)?;
|
||||
}
|
||||
}
|
||||
|
||||
if !self.disable_cache {
|
||||
match &self.config {
|
||||
Some(path) => {
|
||||
@@ -237,7 +257,7 @@ impl CommonOptions {
|
||||
fn opt_level(&self) -> wasmtime::OptLevel {
|
||||
match (self.optimize, self.opt_level.clone()) {
|
||||
(true, _) => wasmtime::OptLevel::Speed,
|
||||
(false, other) => other,
|
||||
(false, other) => other.unwrap_or(wasmtime::OptLevel::Speed),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -274,3 +294,9 @@ fn parse_cranelift_flag(name_and_value: &str) -> Result<CraneliftFlag> {
|
||||
};
|
||||
Ok(CraneliftFlag { name, value })
|
||||
}
|
||||
|
||||
fn parse_target(s: &str) -> Result<Triple> {
|
||||
use std::str::FromStr;
|
||||
|
||||
Triple::from_str(&s).map_err(|e| anyhow::anyhow!(e))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user