Fix wasmer binary

This commit is contained in:
Syrus
2019-12-20 22:57:18 -08:00
parent d36d883528
commit 8cff1adf5c
2 changed files with 45 additions and 15 deletions

View File

@ -164,14 +164,27 @@ pub enum Backend {
} }
impl 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. /// Stable string representation of the backend.
/// It can be used as part of a cache key, for example. /// It can be used as part of a cache key, for example.
pub fn to_string(&self) -> &'static str { pub fn to_string(&self) -> &'static str {
match self { match self {
#[cfg(feature = "cranelift")]
Backend::Cranelift => "cranelift",
#[cfg(feature = "singlepass")] #[cfg(feature = "singlepass")]
Backend::Singlepass => "singlepass", Backend::Singlepass => "singlepass",
#[cfg(feature = "cranelift")]
Backend::Cranelift => "cranelift",
#[cfg(feature = "llvm")] #[cfg(feature = "llvm")]
Backend::LLVM => "llvm", Backend::LLVM => "llvm",
Backend::Auto => "auto", 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<Backend, String> {
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`]. /// Compile WebAssembly binary code into a [`Module`].
/// This function is useful if it is necessary to /// This function is useful if it is necessary to
/// compile a module before it can be instantiated /// compile a module before it can be instantiated

View File

@ -30,13 +30,13 @@ use wasmer_llvm_backend::{
}; };
use wasmer_runtime::{ use wasmer_runtime::{
cache::{Cache as BaseCache, FileSystemCache, WasmHash}, cache::{Cache as BaseCache, FileSystemCache, WasmHash},
Value, VERSION, Backend, Value, VERSION,
}; };
#[cfg(feature = "managed")] #[cfg(feature = "managed")]
use wasmer_runtime_core::tiering::{run_tiering, InteractiveShellContext, ShellExitOperation}; use wasmer_runtime_core::tiering::{run_tiering, InteractiveShellContext, ShellExitOperation};
use wasmer_runtime_core::{ use wasmer_runtime_core::{
self, self,
backend::{Backend, Compiler, CompilerConfig, Features, MemoryBoundCheckMode}, backend::{Compiler, CompilerConfig, Features, MemoryBoundCheckMode},
debug, debug,
loader::{Instance as LoadedInstance, LocalLoader}, loader::{Instance as LoadedInstance, LocalLoader},
Module, Module,
@ -143,7 +143,7 @@ struct Run {
#[structopt(parse(from_os_str))] #[structopt(parse(from_os_str))]
path: PathBuf, path: PathBuf,
/// Name of the backend to use. (x86_64) /// Name of the backend to use (x86_64)
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
#[structopt( #[structopt(
long = "backend", long = "backend",
@ -153,7 +153,7 @@ struct Run {
)] )]
backend: Backend, backend: Backend,
/// Name of the backend to use. (aarch64) /// Name of the backend to use (aarch64)
#[cfg(target_arch = "aarch64")] #[cfg(target_arch = "aarch64")]
#[structopt( #[structopt(
long = "backend", long = "backend",
@ -486,7 +486,7 @@ fn execute_wasi(
baseline: true, baseline: true,
msm: msm, msm: msm,
base: instance.module.runnable_module.get_code().unwrap().as_ptr() as usize, 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(), runnable_module: instance.module.runnable_module.clone(),
}); });
true true
@ -618,8 +618,15 @@ fn execute_wasm(options: &Run) -> Result<(), String> {
}; };
// Don't error on --enable-all for other backends. // Don't error on --enable-all for other backends.
if options.features.simd && options.backend != Backend::LLVM { if options.features.simd {
return Err("SIMD is only supported in the LLVM backend for now".to_string()); #[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) { if !utils::is_wasm_binary(&wasm_binary) {
@ -1031,16 +1038,10 @@ fn get_compiler_by_backend(backend: Backend, _opts: &Run) -> Option<Box<dyn Comp
StreamingCompiler::new(middlewares_gen); StreamingCompiler::new(middlewares_gen);
Box::new(c) Box::new(c)
} }
#[cfg(not(feature = "backend-singlepass"))]
Backend::Singlepass => return None,
#[cfg(feature = "backend-cranelift")] #[cfg(feature = "backend-cranelift")]
Backend::Cranelift => Box::new(CraneliftCompiler::new()), Backend::Cranelift => Box::new(CraneliftCompiler::new()),
#[cfg(not(feature = "backend-cranelift"))]
Backend::Cranelift => return None,
#[cfg(feature = "backend-llvm")] #[cfg(feature = "backend-llvm")]
Backend::LLVM => Box::new(LLVMCompiler::new()), Backend::LLVM => Box::new(LLVMCompiler::new()),
#[cfg(not(feature = "backend-llvm"))]
Backend::LLVM => return None,
Backend::Auto => return None, Backend::Auto => return None,
}) })
} }