Skip to content

Commit a6adf52

Browse files
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>
1 parent 344bf2d commit a6adf52

File tree

4 files changed

+60
-46
lines changed

4 files changed

+60
-46
lines changed

crates/api/src/runtime.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,18 @@ impl Config {
7070
/// be enabled through this method for appropriate wasm modules.
7171
///
7272
/// This feature gates items such as shared memories and atomic
73-
/// instructions.
73+
/// instructions. Note that enabling the threads feature will
74+
/// also enable the bulk memory feature.
7475
///
7576
/// This is `false` by default.
7677
///
7778
/// [threads]: https://github.com/webassembly/threads
7879
pub fn wasm_threads(&mut self, enable: bool) -> &mut Self {
7980
self.features.threads = enable;
81+
// The threads proposal depends on the bulk memory proposal
82+
if enable {
83+
self.features.bulk_memory = true;
84+
}
8085
self
8186
}
8287

@@ -90,13 +95,18 @@ impl Config {
9095
/// modules.
9196
///
9297
/// This feature gates items such as the `anyref` type and multiple tables
93-
/// being in a module.
98+
/// being in a module. Note that enabling the reference types feature will
99+
/// also enable the bulk memory feature.
94100
///
95101
/// This is `false` by default.
96102
///
97103
/// [proposal]: https://github.com/webassembly/reference-types
98104
pub fn wasm_reference_types(&mut self, enable: bool) -> &mut Self {
99105
self.features.reference_types = enable;
106+
// The reference types proposal depends on the bulk memory proposal
107+
if enable {
108+
self.features.bulk_memory = true;
109+
}
100110
self
101111
}
102112

src/commands/run.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! The module that implements the `wasmtime run` command.
22
3-
use crate::{init_file_per_thread_logger, pick_compilation_strategy, CommonOptions};
3+
use crate::{init_file_per_thread_logger, CommonOptions};
44
use anyhow::{bail, Context as _, Result};
55
use std::{
66
ffi::{OsStr, OsString},
@@ -9,7 +9,7 @@ use std::{
99
};
1010
use structopt::{clap::AppSettings, StructOpt};
1111
use wasi_common::preopen_dir;
12-
use wasmtime::{Config, Engine, Instance, Module, Store};
12+
use wasmtime::{Engine, Instance, Module, Store};
1313
use wasmtime_interface_types::ModuleData;
1414
use wasmtime_wasi::{old::snapshot_0::Wasi as WasiSnapshot0, Wasi};
1515

@@ -96,21 +96,7 @@ impl RunCommand {
9696
init_file_per_thread_logger(prefix);
9797
}
9898

99-
let mut config = Config::new();
100-
config
101-
.cranelift_debug_verifier(cfg!(debug_assertions))
102-
.debug_info(self.common.debug_info)
103-
.wasm_simd(self.common.enable_simd)
104-
.strategy(pick_compilation_strategy(
105-
self.common.cranelift,
106-
self.common.lightbeam,
107-
)?)?;
108-
self.common.configure_cache(&mut config)?;
109-
110-
if self.common.optimize {
111-
config.cranelift_opt_level(wasmtime::OptLevel::Speed);
112-
}
113-
99+
let config = self.common.config()?;
114100
let engine = Engine::new(&config);
115101
let store = Store::new(&engine);
116102

src/commands/wast.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//! The module that implements the `wasmtime wast` command.
22
3-
use crate::{init_file_per_thread_logger, pick_compilation_strategy, CommonOptions};
3+
use crate::{init_file_per_thread_logger, CommonOptions};
44
use anyhow::{Context as _, Result};
55
use std::path::PathBuf;
66
use structopt::{clap::AppSettings, StructOpt};
7-
use wasmtime::{Config, Engine, Store};
7+
use wasmtime::{Engine, Store};
88
use wasmtime_wast::WastContext;
99

1010
/// Runs a WebAssembly test script file
@@ -33,21 +33,7 @@ impl WastCommand {
3333
init_file_per_thread_logger(prefix);
3434
}
3535

36-
let mut config = Config::new();
37-
config
38-
.cranelift_debug_verifier(cfg!(debug_assertions))
39-
.debug_info(self.common.debug_info)
40-
.wasm_simd(self.common.enable_simd)
41-
.strategy(pick_compilation_strategy(
42-
self.common.cranelift,
43-
self.common.lightbeam,
44-
)?)?;
45-
self.common.configure_cache(&mut config)?;
46-
47-
if self.common.optimize {
48-
config.cranelift_opt_level(wasmtime::OptLevel::Speed);
49-
}
50-
36+
let config = self.common.config()?;
5137
let store = Store::new(&Engine::new(&config));
5238
let mut wast_context = WastContext::new(store);
5339

src/lib.rs

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,26 @@ struct CommonOptions {
9393
#[structopt(long)]
9494
enable_simd: bool,
9595

96+
/// Enable support for reference types
97+
#[structopt(long)]
98+
enable_reference_types: bool,
99+
100+
/// Enable support for multi-value functions
101+
#[structopt(long)]
102+
enable_multi_value: bool,
103+
104+
/// Enable support for Wasm threads
105+
#[structopt(long)]
106+
enable_threads: bool,
107+
108+
/// Enable support for bulk memory instructions
109+
#[structopt(long)]
110+
enable_bulk_memory: bool,
111+
112+
/// Enable all experimental Wasm features
113+
#[structopt(long)]
114+
enable_all: bool,
115+
96116
/// Use Lightbeam for all compilation
97117
#[structopt(long, conflicts_with = "cranelift")]
98118
lightbeam: bool,
@@ -103,18 +123,30 @@ struct CommonOptions {
103123
}
104124

105125
impl CommonOptions {
106-
fn configure_cache(&self, config: &mut Config) -> Result<()> {
107-
if self.disable_cache {
108-
return Ok(());
126+
fn config(&self) -> Result<Config> {
127+
let mut config = Config::new();
128+
config
129+
.cranelift_debug_verifier(cfg!(debug_assertions))
130+
.debug_info(self.debug_info)
131+
.wasm_bulk_memory(self.enable_bulk_memory || self.enable_all)
132+
.wasm_simd(self.enable_simd || self.enable_all)
133+
.wasm_reference_types(self.enable_reference_types || self.enable_all)
134+
.wasm_multi_value(self.enable_multi_value || self.enable_all)
135+
.wasm_threads(self.enable_threads || self.enable_all)
136+
.strategy(pick_compilation_strategy(self.cranelift, self.lightbeam)?)?;
137+
if self.optimize {
138+
config.cranelift_opt_level(wasmtime::OptLevel::Speed);
109139
}
110-
match &self.config {
111-
Some(path) => {
112-
config.cache_config_load(path)?;
113-
}
114-
None => {
115-
config.cache_config_load_default()?;
140+
if !self.disable_cache {
141+
match &self.config {
142+
Some(path) => {
143+
config.cache_config_load(path)?;
144+
}
145+
None => {
146+
config.cache_config_load_default()?;
147+
}
116148
}
117149
}
118-
Ok(())
150+
Ok(config)
119151
}
120152
}

0 commit comments

Comments
 (0)