From a6adf524298f0658aa6561a83b1f9bc1a4e12d68 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 7 Feb 2020 04:06:59 -0600 Subject: [PATCH] Add more CLI flags for wasm features (#917) * Add more CLI flags for wasm features This commit adds a few more flags to enable wasm features via the CLI, mirroring the existing `--enable-simd` flag: * `--enable-reference-types` * `--enable-multi-value` * `--enable-threads` * `--enable-bulk-memory` Additionally the bulk memory feature is now automatically enabled if `reference-types` or `threads` are enabled since those two proposals largely depend on `bulk-memory`. * Add --enable-all to enable all wasm features * Update src/lib.rs Co-Authored-By: Peter Huene * Apply suggestions from code review Co-Authored-By: Peter Huene Co-authored-by: Peter Huene --- crates/api/src/runtime.rs | 14 +++++++++-- src/commands/run.rs | 20 +++------------ src/commands/wast.rs | 20 +++------------ src/lib.rs | 52 +++++++++++++++++++++++++++++++-------- 4 files changed, 60 insertions(+), 46 deletions(-) diff --git a/crates/api/src/runtime.rs b/crates/api/src/runtime.rs index 199cf3245041..89a4b2bf2b8c 100644 --- a/crates/api/src/runtime.rs +++ b/crates/api/src/runtime.rs @@ -70,13 +70,18 @@ impl Config { /// be enabled through this method for appropriate wasm modules. /// /// This feature gates items such as shared memories and atomic - /// instructions. + /// instructions. Note that enabling the threads feature will + /// also enable the bulk memory feature. /// /// This is `false` by default. /// /// [threads]: https://github.com/webassembly/threads pub fn wasm_threads(&mut self, enable: bool) -> &mut Self { self.features.threads = enable; + // The threads proposal depends on the bulk memory proposal + if enable { + self.features.bulk_memory = true; + } self } @@ -90,13 +95,18 @@ impl Config { /// modules. /// /// This feature gates items such as the `anyref` type and multiple tables - /// being in a module. + /// being in a module. Note that enabling the reference types feature will + /// also enable the bulk memory feature. /// /// This is `false` by default. /// /// [proposal]: https://github.com/webassembly/reference-types pub fn wasm_reference_types(&mut self, enable: bool) -> &mut Self { self.features.reference_types = enable; + // The reference types proposal depends on the bulk memory proposal + if enable { + self.features.bulk_memory = true; + } self } diff --git a/src/commands/run.rs b/src/commands/run.rs index 031e10a0b8dd..3e1cb2c5caa6 100644 --- a/src/commands/run.rs +++ b/src/commands/run.rs @@ -1,6 +1,6 @@ //! The module that implements the `wasmtime run` command. -use crate::{init_file_per_thread_logger, pick_compilation_strategy, CommonOptions}; +use crate::{init_file_per_thread_logger, CommonOptions}; use anyhow::{bail, Context as _, Result}; use std::{ ffi::{OsStr, OsString}, @@ -9,7 +9,7 @@ use std::{ }; use structopt::{clap::AppSettings, StructOpt}; use wasi_common::preopen_dir; -use wasmtime::{Config, Engine, Instance, Module, Store}; +use wasmtime::{Engine, Instance, Module, Store}; use wasmtime_interface_types::ModuleData; use wasmtime_wasi::{old::snapshot_0::Wasi as WasiSnapshot0, Wasi}; @@ -96,21 +96,7 @@ impl RunCommand { init_file_per_thread_logger(prefix); } - let mut config = Config::new(); - config - .cranelift_debug_verifier(cfg!(debug_assertions)) - .debug_info(self.common.debug_info) - .wasm_simd(self.common.enable_simd) - .strategy(pick_compilation_strategy( - self.common.cranelift, - self.common.lightbeam, - )?)?; - self.common.configure_cache(&mut config)?; - - if self.common.optimize { - config.cranelift_opt_level(wasmtime::OptLevel::Speed); - } - + let config = self.common.config()?; let engine = Engine::new(&config); let store = Store::new(&engine); diff --git a/src/commands/wast.rs b/src/commands/wast.rs index 156812e6ed60..4ac459319aea 100644 --- a/src/commands/wast.rs +++ b/src/commands/wast.rs @@ -1,10 +1,10 @@ //! The module that implements the `wasmtime wast` command. -use crate::{init_file_per_thread_logger, pick_compilation_strategy, CommonOptions}; +use crate::{init_file_per_thread_logger, CommonOptions}; use anyhow::{Context as _, Result}; use std::path::PathBuf; use structopt::{clap::AppSettings, StructOpt}; -use wasmtime::{Config, Engine, Store}; +use wasmtime::{Engine, Store}; use wasmtime_wast::WastContext; /// Runs a WebAssembly test script file @@ -33,21 +33,7 @@ impl WastCommand { init_file_per_thread_logger(prefix); } - let mut config = Config::new(); - config - .cranelift_debug_verifier(cfg!(debug_assertions)) - .debug_info(self.common.debug_info) - .wasm_simd(self.common.enable_simd) - .strategy(pick_compilation_strategy( - self.common.cranelift, - self.common.lightbeam, - )?)?; - self.common.configure_cache(&mut config)?; - - if self.common.optimize { - config.cranelift_opt_level(wasmtime::OptLevel::Speed); - } - + let config = self.common.config()?; let store = Store::new(&Engine::new(&config)); let mut wast_context = WastContext::new(store); diff --git a/src/lib.rs b/src/lib.rs index 9b639512f7ba..60ea36230bb5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -93,6 +93,26 @@ struct CommonOptions { #[structopt(long)] enable_simd: bool, + /// Enable support for reference types + #[structopt(long)] + enable_reference_types: bool, + + /// Enable support for multi-value functions + #[structopt(long)] + enable_multi_value: bool, + + /// Enable support for Wasm threads + #[structopt(long)] + enable_threads: bool, + + /// Enable support for bulk memory instructions + #[structopt(long)] + enable_bulk_memory: bool, + + /// Enable all experimental Wasm features + #[structopt(long)] + enable_all: bool, + /// Use Lightbeam for all compilation #[structopt(long, conflicts_with = "cranelift")] lightbeam: bool, @@ -103,18 +123,30 @@ struct CommonOptions { } impl CommonOptions { - fn configure_cache(&self, config: &mut Config) -> Result<()> { - if self.disable_cache { - return Ok(()); + fn config(&self) -> Result { + let mut config = Config::new(); + config + .cranelift_debug_verifier(cfg!(debug_assertions)) + .debug_info(self.debug_info) + .wasm_bulk_memory(self.enable_bulk_memory || self.enable_all) + .wasm_simd(self.enable_simd || self.enable_all) + .wasm_reference_types(self.enable_reference_types || self.enable_all) + .wasm_multi_value(self.enable_multi_value || self.enable_all) + .wasm_threads(self.enable_threads || self.enable_all) + .strategy(pick_compilation_strategy(self.cranelift, self.lightbeam)?)?; + if self.optimize { + config.cranelift_opt_level(wasmtime::OptLevel::Speed); } - match &self.config { - Some(path) => { - config.cache_config_load(path)?; - } - None => { - config.cache_config_load_default()?; + if !self.disable_cache { + match &self.config { + Some(path) => { + config.cache_config_load(path)?; + } + None => { + config.cache_config_load_default()?; + } } } - Ok(()) + Ok(config) } }