Add a wasmtime settings command to print Cranelift settings.

This commit adds the `wasmtime settings` command to print out available
Cranelift settings for a target (defaults to the host).

The compile command has been updated to remove the Cranelift ISA options in
favor of encouraging users to use `wasmtime settings` to discover what settings
are available.  This will reduce the maintenance cost for syncing the compile
command with Cranelift ISA flags.
This commit is contained in:
Peter Huene
2021-03-31 22:44:13 -07:00
parent a474524d3b
commit abf3bf29f9
16 changed files with 478 additions and 312 deletions

View File

@@ -6,7 +6,7 @@
use anyhow::Result;
use structopt::{clap::AppSettings, clap::ErrorKind, StructOpt};
use wasmtime_cli::commands::{
CompileCommand, ConfigCommand, RunCommand, WasmToObjCommand, WastCommand,
CompileCommand, ConfigCommand, RunCommand, SettingsCommand, WasmToObjCommand, WastCommand,
};
/// Wasmtime WebAssembly Runtime
@@ -42,6 +42,8 @@ enum WasmtimeApp {
Compile(CompileCommand),
/// Runs a WebAssembly module
Run(RunCommand),
/// Displays available Cranelift settings for a target.
Settings(SettingsCommand),
/// Translates a WebAssembly module to native object file
#[structopt(name = "wasm2obj")]
WasmToObj(WasmToObjCommand),
@@ -56,6 +58,7 @@ impl WasmtimeApp {
Self::Config(c) => c.execute(),
Self::Compile(c) => c.execute(),
Self::Run(c) => c.execute(),
Self::Settings(c) => c.execute(),
Self::WasmToObj(c) => c.execute(),
Self::Wast(c) => c.execute(),
}

View File

@@ -3,7 +3,8 @@
mod compile;
mod config;
mod run;
mod settings;
mod wasm2obj;
mod wast;
pub use self::{compile::*, config::*, run::*, wasm2obj::*, wast::*};
pub use self::{compile::*, config::*, run::*, settings::*, wasm2obj::*, wast::*};

View File

@@ -1,7 +1,7 @@
//! The module that implements the `wasmtime wast` command.
//! The module that implements the `wasmtime compile` command.
use crate::CommonOptions;
use anyhow::{anyhow, bail, Context, Result};
use anyhow::{bail, Context, Result};
use std::fs::{self, File};
use std::io::BufWriter;
use std::path::PathBuf;
@@ -10,7 +10,7 @@ use structopt::{
StructOpt,
};
use target_lexicon::Triple;
use wasmtime::{Config, Engine, Module};
use wasmtime::{Engine, Module};
lazy_static::lazy_static! {
static ref AFTER_HELP: String = {
@@ -31,7 +31,7 @@ lazy_static::lazy_static! {
\n\
Compiling for a specific platform (Linux) and CPU preset (Skylake):\n\
\n \
wasmtime compile --target x86_64-unknown-linux --skylake foo.wasm\n",
wasmtime compile --target x86_64-unknown-linux --cranelift-enable skylake foo.wasm\n",
crate::WASM_FEATURES.as_str()
)
};
@@ -57,90 +57,6 @@ pub struct CompileCommand {
#[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>,
@@ -167,8 +83,6 @@ impl CompileCommand {
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() {
@@ -191,48 +105,6 @@ impl CompileCommand {
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)]
@@ -285,19 +157,32 @@ mod test {
let command = CompileCommand::from_iter_safe(vec![
"compile",
"--disable-logging",
"--sse3",
"--ssse3",
"--sse41",
"--sse42",
"--avx",
"--avx2",
"--avx512dq",
"--avx512vl",
"--avx512f",
"--popcnt",
"--bmi1",
"--bmi2",
"--lzcnt",
"--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(),
@@ -321,7 +206,8 @@ mod test {
let command = CompileCommand::from_iter_safe(vec![
"compile",
"--disable-logging",
"--lse",
"--cranelift-enable",
"has_lse",
"-o",
output_path.to_str().unwrap(),
input_path.to_str().unwrap(),
@@ -334,30 +220,28 @@ mod test {
#[cfg(target_arch = "x86_64")]
#[test]
fn test_incompatible_flags_compile() -> Result<()> {
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();
// x64 and aarch64 flags should conflict
match CompileCommand::from_iter_safe(vec![
// aarch64 flags should not be supported
let command = CompileCommand::from_iter_safe(vec![
"compile",
"--disable-logging",
"--sse3",
"--lse",
"--cranelift-enable",
"has_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"));
}
}
])?;
assert_eq!(
command.execute().unwrap_err().to_string(),
"No existing setting named 'has_lse'"
);
Ok(())
}
@@ -372,17 +256,18 @@ mod test {
let output_path = NamedTempFile::new()?.into_temp_path();
for preset in &[
"--nehalem",
"--haswell",
"--broadwell",
"--skylake",
"--cannonlake",
"--icelake",
"--znver1",
"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(),
@@ -392,24 +277,6 @@ mod test {
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(())
}
}

103
src/commands/settings.rs Normal file
View File

@@ -0,0 +1,103 @@
//! 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);
let mut nums = (Vec::new(), 0);
let mut bools = (Vec::new(), 0);
let mut presets = (Vec::new(), 0);
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());
if !enums.0.is_empty() {
println!();
Self::print_settings("Enum settings:", enums.0, enums.1);
}
if !nums.0.is_empty() {
println!();
Self::print_settings("Numerical settings:", nums.0, nums.1);
}
if !bools.0.is_empty() {
println!();
Self::print_settings("Boolean settings:", bools.0, bools.1);
}
if !presets.0.is_empty() {
println!();
Self::print_settings("Presets:", presets.0, presets.1);
}
if self.target.is_none() {
let isa = settings.finish(settings::Flags::new(settings::builder()));
println!();
println!("Settings enabled for this host:");
for flag in isa.enabled_isa_flags() {
println!(" - {}", flag);
}
}
Ok(())
}
fn print_settings(header: &str, settings: impl IntoIterator<Item = 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
);
}
}
}

View File

@@ -212,12 +212,19 @@ struct CommonOptions {
)]
opt_level: Option<wasmtime::OptLevel>,
/// Cranelift common flags to set.
#[structopt(long = "cranelift-set", value_name = "NAME=VALUE", parse(try_from_str = parse_cranelift_flag))]
/// Set a Cranelift setting to a given value.
/// Use `wasmtime settings` to list Cranelift settings for a target.
#[structopt(long = "cranelift-set", value_name = "NAME=VALUE", number_of_values = 1, verbatim_doc_comment, parse(try_from_str = parse_cranelift_flag))]
cranelift_set: Vec<(String, String)>,
/// The Cranelift boolean setting or preset to enable.
#[structopt(long, value_name = "SETTING")]
/// Enable a Cranelift boolean setting or preset.
/// Use `wasmtime settings` to list Cranelift settings for a target.
#[structopt(
long,
value_name = "SETTING",
number_of_values = 1,
verbatim_doc_comment
)]
cranelift_enable: Vec<String>,
/// Maximum size in bytes of wasm memory before it becomes dynamically