mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-21 20:51:32 +00:00
Add backend selection to cli
This commit is contained in:
@ -40,7 +40,7 @@ wabt = "0.7.2"
|
|||||||
glob = "0.2.11"
|
glob = "0.2.11"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["fast-tests", "wasi"]
|
default = ["fast-tests", "wasi", "singlepass"]
|
||||||
debug = ["wasmer-clif-backend/debug", "wasmer-runtime-core/debug"]
|
debug = ["wasmer-clif-backend/debug", "wasmer-runtime-core/debug"]
|
||||||
# This feature will allow cargo test to run much faster
|
# This feature will allow cargo test to run much faster
|
||||||
fast-tests = []
|
fast-tests = []
|
||||||
|
@ -141,6 +141,16 @@ pub fn compile_with_config(
|
|||||||
wasmer_runtime_core::compile_with_config(&wasm[..], default_compiler(), compiler_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
|
/// Compile and instantiate WebAssembly code without
|
||||||
/// creating a [`Module`].
|
/// creating a [`Module`].
|
||||||
///
|
///
|
||||||
|
@ -13,8 +13,13 @@ use structopt::StructOpt;
|
|||||||
|
|
||||||
use wasmer::webassembly::InstanceABI;
|
use wasmer::webassembly::InstanceABI;
|
||||||
use wasmer::*;
|
use wasmer::*;
|
||||||
|
use wasmer_clif_backend::CraneliftCompiler;
|
||||||
use wasmer_runtime::cache::{Cache as BaseCache, FileSystemCache, WasmHash, WASMER_VERSION_HASH};
|
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")]
|
#[cfg(feature = "wasi")]
|
||||||
use wasmer_wasi;
|
use wasmer_wasi;
|
||||||
|
|
||||||
@ -64,7 +69,11 @@ struct Run {
|
|||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
|
|
||||||
// Disable the cache
|
// 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,
|
backend: Backend,
|
||||||
|
|
||||||
/// Emscripten symbol map
|
/// Emscripten symbol map
|
||||||
@ -76,6 +85,7 @@ struct Run {
|
|||||||
args: Vec<String>,
|
args: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum Backend {
|
enum Backend {
|
||||||
Cranelift,
|
Cranelift,
|
||||||
@ -83,13 +93,23 @@ enum Backend {
|
|||||||
LLVM,
|
LLVM,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Backend {
|
||||||
|
pub fn variants() -> &'static [&'static str] {
|
||||||
|
&["singlepass", "cranelift", "llvm"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl FromStr for Backend {
|
impl FromStr for Backend {
|
||||||
type Err = String;
|
type Err = String;
|
||||||
fn from_str(s: &str) -> Result<Backend, String> {
|
fn from_str(s: &str) -> Result<Backend, String> {
|
||||||
match s {
|
match s.to_lowercase().as_str() {
|
||||||
"singlepass" => Ok(Backend::Singlepass),
|
"singlepass" => Ok(Backend::Singlepass),
|
||||||
"cranelift" => Ok(Backend::Cranelift),
|
"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)),
|
_ => 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))?;
|
.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 {
|
let module = if !disable_cache {
|
||||||
// If we have cache enabled
|
// If we have cache enabled
|
||||||
|
|
||||||
@ -226,11 +252,12 @@ fn execute_wasm(options: &Run) -> Result<(), String> {
|
|||||||
module
|
module
|
||||||
}
|
}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
let module = webassembly::compile_with_config(
|
let module = webassembly::compile_with_config_with(
|
||||||
&wasm_binary[..],
|
&wasm_binary[..],
|
||||||
CompilerConfig {
|
CompilerConfig {
|
||||||
symbol_map: em_symbol_map,
|
symbol_map: em_symbol_map,
|
||||||
},
|
},
|
||||||
|
&*compiler,
|
||||||
)
|
)
|
||||||
.map_err(|e| format!("Can't compile module: {:?}", e))?;
|
.map_err(|e| format!("Can't compile module: {:?}", e))?;
|
||||||
// We try to save the module into a cache file
|
// We try to save the module into a cache file
|
||||||
@ -241,11 +268,12 @@ fn execute_wasm(options: &Run) -> Result<(), String> {
|
|||||||
};
|
};
|
||||||
module
|
module
|
||||||
} else {
|
} else {
|
||||||
webassembly::compile_with_config(
|
webassembly::compile_with_config_with(
|
||||||
&wasm_binary[..],
|
&wasm_binary[..],
|
||||||
CompilerConfig {
|
CompilerConfig {
|
||||||
symbol_map: em_symbol_map,
|
symbol_map: em_symbol_map,
|
||||||
},
|
},
|
||||||
|
&*compiler,
|
||||||
)
|
)
|
||||||
.map_err(|e| format!("Can't compile module: {:?}", e))?
|
.map_err(|e| format!("Can't compile module: {:?}", e))?
|
||||||
};
|
};
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
use std::panic;
|
use std::panic;
|
||||||
|
pub use wasmer_runtime::compile_with_config_with;
|
||||||
use wasmer_runtime::{
|
use wasmer_runtime::{
|
||||||
self as runtime,
|
self as runtime,
|
||||||
error::{CallResult, Result},
|
error::{CallResult, Result},
|
||||||
@ -78,15 +79,15 @@ pub fn compile(buffer_source: &[u8]) -> Result<Module> {
|
|||||||
Ok(module)
|
Ok(module)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The same as `compile` but takes a `CompilerConfig` for the purpose of
|
// /// The same as `compile` but takes a `CompilerConfig` for the purpose of
|
||||||
/// changing the compiler's behavior
|
// /// changing the compiler's behavior
|
||||||
pub fn compile_with_config(
|
// pub fn compile_with_config_with(
|
||||||
buffer_source: &[u8],
|
// buffer_source: &[u8],
|
||||||
compiler_config: CompilerConfig,
|
// compiler_config: CompilerConfig,
|
||||||
) -> Result<Module> {
|
// ) -> Result<Module> {
|
||||||
let module = runtime::compile_with_config(buffer_source, compiler_config)?;
|
// let module = runtime::compile_with_config(buffer_source, compiler_config)?;
|
||||||
Ok(module)
|
// Ok(module)
|
||||||
}
|
// }
|
||||||
|
|
||||||
/// Performs common instance operations needed when an instance is first run
|
/// Performs common instance operations needed when an instance is first run
|
||||||
/// including data setup, handling arguments and calling a main function
|
/// including data setup, handling arguments and calling a main function
|
||||||
|
Reference in New Issue
Block a user