Add --enable-simd flag to wasmer run and wasmer validate.

This commit is contained in:
Nick Lewycky 2019-07-25 23:33:30 -07:00
parent ab5f28851a
commit 8f417f3d59
4 changed files with 63 additions and 6 deletions

View File

@ -110,6 +110,11 @@ impl Default for MemoryBoundCheckMode {
} }
} }
#[derive(Default)]
pub struct Features {
pub simd: bool,
}
/// Configuration data for the compiler /// Configuration data for the compiler
#[derive(Default)] #[derive(Default)]
pub struct CompilerConfig { pub struct CompilerConfig {
@ -118,6 +123,7 @@ pub struct CompilerConfig {
pub memory_bound_check_mode: MemoryBoundCheckMode, pub memory_bound_check_mode: MemoryBoundCheckMode,
pub enforce_stack_check: bool, pub enforce_stack_check: bool,
pub track_state: bool, pub track_state: bool,
pub features: Features,
} }
pub trait Compiler { pub trait Compiler {

View File

@ -117,8 +117,26 @@ pub fn validate(wasm: &[u8]) -> bool {
/// The same as `validate` but with an Error message on failure /// The same as `validate` but with an Error message on failure
pub fn validate_and_report_errors(wasm: &[u8]) -> ::std::result::Result<(), String> { pub fn validate_and_report_errors(wasm: &[u8]) -> ::std::result::Result<(), String> {
validate_and_report_errors_with_features(wasm, Default::default())
}
/// The same as `validate_and_report_errors` but with a Features.
pub fn validate_and_report_errors_with_features(
wasm: &[u8],
features: backend::Features,
) -> ::std::result::Result<(), String> {
use wasmparser::WasmDecoder; use wasmparser::WasmDecoder;
let mut parser = wasmparser::ValidatingParser::new(wasm, None); let config = wasmparser::ValidatingParserConfig {
operator_config: wasmparser::OperatorValidatorConfig {
enable_simd: features.simd,
enable_bulk_memory: false,
enable_multi_value: false,
enable_reference_types: false,
enable_threads: false,
},
mutable_global_imports: false,
};
let mut parser = wasmparser::ValidatingParser::new(wasm, Some(config));
loop { loop {
let state = parser.read(); let state = parser.read();
match *state { match *state {

View File

@ -55,6 +55,7 @@ fn handle_client(mut stream: UnixStream) {
memory_bound_check_mode: MemoryBoundCheckMode::Disable, memory_bound_check_mode: MemoryBoundCheckMode::Disable,
enforce_stack_check: true, enforce_stack_check: true,
track_state: false, track_state: false,
features: Default::default(),
}, },
&SinglePassCompiler::new(), &SinglePassCompiler::new(),
) )

View File

@ -23,7 +23,7 @@ use wasmer_runtime::{
}; };
use wasmer_runtime_core::{ use wasmer_runtime_core::{
self, self,
backend::{Backend, Compiler, CompilerConfig, MemoryBoundCheckMode}, backend::{Backend, Compiler, CompilerConfig, Features, MemoryBoundCheckMode},
debug, debug,
loader::{Instance as LoadedInstance, LocalLoader}, loader::{Instance as LoadedInstance, LocalLoader},
}; };
@ -67,6 +67,17 @@ enum CLIOptions {
SelfUpdate, SelfUpdate,
} }
#[derive(Debug, StructOpt)]
struct PrestandardFeatures {
/// Enable support for the SIMD proposal.
#[structopt(long = "enable-simd")]
simd: bool,
/// Enable support for all pre-standard proposals.
#[structopt(long = "enable-all")]
all: bool,
}
#[derive(Debug, StructOpt)] #[derive(Debug, StructOpt)]
struct Run { struct Run {
// Disable the cache // Disable the cache
@ -134,6 +145,9 @@ struct Run {
#[structopt(long = "cache-key", hidden = true)] #[structopt(long = "cache-key", hidden = true)]
cache_key: Option<String>, cache_key: Option<String>,
#[structopt(flatten)]
features: PrestandardFeatures,
/// Application arguments /// Application arguments
#[structopt(name = "--", raw(multiple = "true"))] #[structopt(name = "--", raw(multiple = "true"))]
args: Vec<String>, args: Vec<String>,
@ -185,6 +199,9 @@ struct Validate {
/// Input file /// Input file
#[structopt(parse(from_os_str))] #[structopt(parse(from_os_str))]
path: PathBuf, path: PathBuf,
#[structopt(flatten)]
features: PrestandardFeatures,
} }
/// Read the contents of a file /// Read the contents of a file
@ -315,10 +332,14 @@ fn execute_wasm(options: &Run) -> Result<(), String> {
None None
}; };
// Don't error on --enable-all for other backends.
if options.features.simd && options.backend != Backend::LLVM {
return Err("SIMD is only supported in the LLVM backend for now".to_string());
}
if !utils::is_wasm_binary(&wasm_binary) { if !utils::is_wasm_binary(&wasm_binary) {
let mut features = wabt::Features::new(); let mut features = wabt::Features::new();
if options.backend == Backend::LLVM { if options.features.simd || options.features.all {
// SIMD is only supported in the LLVM backend for now
features.enable_simd(); features.enable_simd();
} }
wasm_binary = wabt::wat2wasm_with_features(wasm_binary, features) wasm_binary = wabt::wat2wasm_with_features(wasm_binary, features)
@ -357,6 +378,9 @@ fn execute_wasm(options: &Run) -> Result<(), String> {
memory_bound_check_mode: MemoryBoundCheckMode::Disable, memory_bound_check_mode: MemoryBoundCheckMode::Disable,
enforce_stack_check: true, enforce_stack_check: true,
track_state, track_state,
features: Features {
simd: options.features.simd || options.features.all,
},
}, },
&*compiler, &*compiler,
) )
@ -367,6 +391,9 @@ fn execute_wasm(options: &Run) -> Result<(), String> {
CompilerConfig { CompilerConfig {
symbol_map: em_symbol_map, symbol_map: em_symbol_map,
track_state, track_state,
features: Features {
simd: options.features.simd || options.features.all,
},
..Default::default() ..Default::default()
}, },
&*compiler, &*compiler,
@ -737,7 +764,12 @@ fn validate_wasm(validate: Validate) -> Result<(), String> {
)); ));
} }
wasmer_runtime_core::validate_and_report_errors(&wasm_binary) wasmer_runtime_core::validate_and_report_errors_with_features(
&wasm_binary,
Features {
simd: validate.features.simd || validate.features.all,
},
)
.map_err(|err| format!("Validation failed: {}", err))?; .map_err(|err| format!("Validation failed: {}", err))?;
Ok(()) Ok(())