Add backend selection to cli

This commit is contained in:
Lachlan Sneff
2019-04-11 14:34:54 -07:00
parent b724dd0a9b
commit 4740b2467f
4 changed files with 55 additions and 16 deletions

View File

@ -40,7 +40,7 @@ wabt = "0.7.2"
glob = "0.2.11"
[features]
default = ["fast-tests", "wasi"]
default = ["fast-tests", "wasi", "singlepass"]
debug = ["wasmer-clif-backend/debug", "wasmer-runtime-core/debug"]
# This feature will allow cargo test to run much faster
fast-tests = []

View File

@ -141,6 +141,16 @@ pub fn compile_with_config(
wasmer_runtime_core::compile_with_config(&wasm[..], default_compiler(), compiler_config)
}
/// The same as `compile_with_config` but takes a `Compiler` for the purpose of
/// changing the backend.
pub fn compile_with_config_with(
wasm: &[u8],
compiler_config: CompilerConfig,
compiler: &dyn Compiler,
) -> error::CompileResult<Module> {
wasmer_runtime_core::compile_with_config(&wasm[..], compiler, compiler_config)
}
/// Compile and instantiate WebAssembly code without
/// creating a [`Module`].
///

View File

@ -13,8 +13,13 @@ use structopt::StructOpt;
use wasmer::webassembly::InstanceABI;
use wasmer::*;
use wasmer_clif_backend::CraneliftCompiler;
use wasmer_runtime::cache::{Cache as BaseCache, FileSystemCache, WasmHash, WASMER_VERSION_HASH};
use wasmer_runtime_core::{self, backend::CompilerConfig};
use wasmer_runtime_core::{
self,
backend::{Compiler, CompilerConfig},
};
use wasmer_singlepass_backend::SinglePassCompiler;
#[cfg(feature = "wasi")]
use wasmer_wasi;
@ -64,7 +69,11 @@ struct Run {
path: PathBuf,
// Disable the cache
#[structopt(long = "backend", default_value = "cranelift")]
#[structopt(
long = "backend",
default_value = "cranelift",
raw(possible_values = "Backend::variants()", case_insensitive = "true")
)]
backend: Backend,
/// Emscripten symbol map
@ -76,6 +85,7 @@ struct Run {
args: Vec<String>,
}
#[allow(dead_code)]
#[derive(Debug)]
enum Backend {
Cranelift,
@ -83,13 +93,23 @@ enum Backend {
LLVM,
}
impl Backend {
pub fn variants() -> &'static [&'static str] {
&["singlepass", "cranelift", "llvm"]
}
}
impl FromStr for Backend {
type Err = String;
fn from_str(s: &str) -> Result<Backend, String> {
match s {
match s.to_lowercase().as_str() {
"singlepass" => Ok(Backend::Singlepass),
"cranelift" => Ok(Backend::Cranelift),
"llvm" => Ok(Backend::LLVM),
// "llvm" => Ok(Backend::LLVM),
"llvm" => Err(
"The LLVM backend option is not enabled by default due to binary size constraints"
.to_string(),
),
_ => Err(format!("The backend {} doesn't exist", s)),
}
}
@ -201,6 +221,12 @@ fn execute_wasm(options: &Run) -> Result<(), String> {
.map_err(|e| format!("Can't convert from wast to wasm: {:?}", e))?;
}
let compiler: Box<dyn Compiler> = match options.backend {
Backend::Singlepass => Box::new(SinglePassCompiler::new()),
Backend::Cranelift => Box::new(CraneliftCompiler::new()),
Backend::LLVM => unimplemented!(),
};
let module = if !disable_cache {
// If we have cache enabled
@ -226,11 +252,12 @@ fn execute_wasm(options: &Run) -> Result<(), String> {
module
}
Err(_) => {
let module = webassembly::compile_with_config(
let module = webassembly::compile_with_config_with(
&wasm_binary[..],
CompilerConfig {
symbol_map: em_symbol_map,
},
&*compiler,
)
.map_err(|e| format!("Can't compile module: {:?}", e))?;
// We try to save the module into a cache file
@ -241,11 +268,12 @@ fn execute_wasm(options: &Run) -> Result<(), String> {
};
module
} else {
webassembly::compile_with_config(
webassembly::compile_with_config_with(
&wasm_binary[..],
CompilerConfig {
symbol_map: em_symbol_map,
},
&*compiler,
)
.map_err(|e| format!("Can't compile module: {:?}", e))?
};

View File

@ -1,4 +1,5 @@
use std::panic;
pub use wasmer_runtime::compile_with_config_with;
use wasmer_runtime::{
self as runtime,
error::{CallResult, Result},
@ -78,15 +79,15 @@ pub fn compile(buffer_source: &[u8]) -> Result<Module> {
Ok(module)
}
/// The same as `compile` but takes a `CompilerConfig` for the purpose of
/// changing the compiler's behavior
pub fn compile_with_config(
buffer_source: &[u8],
compiler_config: CompilerConfig,
) -> Result<Module> {
let module = runtime::compile_with_config(buffer_source, compiler_config)?;
Ok(module)
}
// /// The same as `compile` but takes a `CompilerConfig` for the purpose of
// /// changing the compiler's behavior
// pub fn compile_with_config_with(
// buffer_source: &[u8],
// compiler_config: CompilerConfig,
// ) -> Result<Module> {
// let module = runtime::compile_with_config(buffer_source, compiler_config)?;
// Ok(module)
// }
/// Performs common instance operations needed when an instance is first run
/// including data setup, handling arguments and calling a main function