diff --git a/lib/runtime/src/lib.rs b/lib/runtime/src/lib.rs index 2447f4761..339b29aec 100644 --- a/lib/runtime/src/lib.rs +++ b/lib/runtime/src/lib.rs @@ -164,14 +164,27 @@ pub enum Backend { } impl Backend { + /// Get a list of the currently enabled (via feature flag) backends. + pub fn variants() -> &'static [&'static str] { + &[ + #[cfg(feature = "singlepass")] + "singlepass", + #[cfg(feature = "cranelift")] + "cranelift", + #[cfg(feature = "llvm")] + "llvm", + "auto", + ] + } + /// Stable string representation of the backend. /// It can be used as part of a cache key, for example. pub fn to_string(&self) -> &'static str { match self { - #[cfg(feature = "cranelift")] - Backend::Cranelift => "cranelift", #[cfg(feature = "singlepass")] Backend::Singlepass => "singlepass", + #[cfg(feature = "cranelift")] + Backend::Cranelift => "cranelift", #[cfg(feature = "llvm")] Backend::LLVM => "llvm", Backend::Auto => "auto", @@ -192,6 +205,22 @@ impl Default for Backend { } } +impl std::str::FromStr for Backend { + type Err = String; + fn from_str(s: &str) -> Result { + match s.to_lowercase().as_str() { + #[cfg(feature = "singlepass")] + "singlepass" => Ok(Backend::Singlepass), + #[cfg(feature = "cranelift")] + "cranelift" => Ok(Backend::Cranelift), + #[cfg(feature = "llvm")] + "llvm" => Ok(Backend::LLVM), + "auto" => Ok(Backend::Auto), + _ => Err(format!("The backend {} doesn't exist", s)), + } + } +} + /// Compile WebAssembly binary code into a [`Module`]. /// This function is useful if it is necessary to /// compile a module before it can be instantiated diff --git a/src/bin/wasmer.rs b/src/bin/wasmer.rs index 04ef6d3a2..91538b96b 100644 --- a/src/bin/wasmer.rs +++ b/src/bin/wasmer.rs @@ -30,13 +30,13 @@ use wasmer_llvm_backend::{ }; use wasmer_runtime::{ cache::{Cache as BaseCache, FileSystemCache, WasmHash}, - Value, VERSION, + Backend, Value, VERSION, }; #[cfg(feature = "managed")] use wasmer_runtime_core::tiering::{run_tiering, InteractiveShellContext, ShellExitOperation}; use wasmer_runtime_core::{ self, - backend::{Backend, Compiler, CompilerConfig, Features, MemoryBoundCheckMode}, + backend::{Compiler, CompilerConfig, Features, MemoryBoundCheckMode}, debug, loader::{Instance as LoadedInstance, LocalLoader}, Module, @@ -143,7 +143,7 @@ struct Run { #[structopt(parse(from_os_str))] path: PathBuf, - /// Name of the backend to use. (x86_64) + /// Name of the backend to use (x86_64) #[cfg(target_arch = "x86_64")] #[structopt( long = "backend", @@ -153,7 +153,7 @@ struct Run { )] backend: Backend, - /// Name of the backend to use. (aarch64) + /// Name of the backend to use (aarch64) #[cfg(target_arch = "aarch64")] #[structopt( long = "backend", @@ -486,7 +486,7 @@ fn execute_wasi( baseline: true, msm: msm, base: instance.module.runnable_module.get_code().unwrap().as_ptr() as usize, - backend: options.backend, + backend: options.backend.to_string().to_owned(), runnable_module: instance.module.runnable_module.clone(), }); true @@ -618,8 +618,15 @@ fn execute_wasm(options: &Run) -> Result<(), String> { }; // 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 options.features.simd { + #[cfg(feature = "backend-llvm")] + { + if options.backend != Backend::LLVM { + return Err("SIMD is only supported in the LLVM backend for now".to_string()); + } + } + #[cfg(not(feature = "backend-llvm"))] + return Err("SIMD is not supported in this backend".to_string()); } if !utils::is_wasm_binary(&wasm_binary) { @@ -1031,16 +1038,10 @@ fn get_compiler_by_backend(backend: Backend, _opts: &Run) -> Option return None, #[cfg(feature = "backend-cranelift")] Backend::Cranelift => Box::new(CraneliftCompiler::new()), - #[cfg(not(feature = "backend-cranelift"))] - Backend::Cranelift => return None, #[cfg(feature = "backend-llvm")] Backend::LLVM => Box::new(LLVMCompiler::new()), - #[cfg(not(feature = "backend-llvm"))] - Backend::LLVM => return None, Backend::Auto => return None, }) }