Browse Source

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 <peterhuene@protonmail.com>

* Apply suggestions from code review

Co-Authored-By: Peter Huene <peterhuene@protonmail.com>

Co-authored-by: Peter Huene <peterhuene@protonmail.com>
pull/922/head
Alex Crichton 5 years ago
committed by GitHub
parent
commit
a6adf52429
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 14
      crates/api/src/runtime.rs
  2. 20
      src/commands/run.rs
  3. 20
      src/commands/wast.rs
  4. 52
      src/lib.rs

14
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
}

20
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);

20
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);

52
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<Config> {
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)
}
}

Loading…
Cancel
Save