diff --git a/CHANGELOG.md b/CHANGELOG.md index 60ff5c652ec..c1de67bf893 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - [#1006](https://github.com/wasmerio/wasmer/pull/1006) Fix minor panic issue when `wasmer::compile_with` called with llvm backend - [#1009](https://github.com/wasmerio/wasmer/pull/1009) Enable LLVM verifier for all tests, add new llvm-backend-tests crate. +- [#1004](https://github.com/wasmerio/wasmer/pull/1004) Add the Auto backend to enable to adapt backend usage depending on wasm file executed. ## 0.11.0 - 2019-11-22 diff --git a/lib/runtime-core/src/backend.rs b/lib/runtime-core/src/backend.rs index df4ef775160..febfdda0e98 100644 --- a/lib/runtime-core/src/backend.rs +++ b/lib/runtime-core/src/backend.rs @@ -28,6 +28,7 @@ pub enum Backend { Cranelift, Singlepass, LLVM, + Auto, } impl Backend { @@ -40,6 +41,7 @@ impl Backend { "singlepass", #[cfg(feature = "backend-llvm")] "llvm", + "auto", ] } @@ -50,6 +52,7 @@ impl Backend { Backend::Cranelift => "cranelift", Backend::Singlepass => "singlepass", Backend::LLVM => "llvm", + Backend::Auto => "auto", } } } @@ -67,6 +70,7 @@ impl std::str::FromStr for Backend { "singlepass" => Ok(Backend::Singlepass), "cranelift" => Ok(Backend::Cranelift), "llvm" => Ok(Backend::LLVM), + "auto" => Ok(Backend::Auto), _ => Err(format!("The backend {} doesn't exist", s)), } } diff --git a/lib/runtime-core/src/codegen.rs b/lib/runtime-core/src/codegen.rs index 7b4533296b9..991e05ed8ad 100644 --- a/lib/runtime-core/src/codegen.rs +++ b/lib/runtime-core/src/codegen.rs @@ -265,6 +265,7 @@ fn requires_pre_validation(backend: Backend) -> bool { Backend::Cranelift => true, Backend::LLVM => true, Backend::Singlepass => false, + Backend::Auto => false, } } diff --git a/lib/runtime/src/lib.rs b/lib/runtime/src/lib.rs index 2ac6c174261..0c832bf3020 100644 --- a/lib/runtime/src/lib.rs +++ b/lib/runtime/src/lib.rs @@ -254,11 +254,6 @@ pub fn compiler_for_backend(backend: Backend) -> Option> { #[cfg(feature = "llvm")] Backend::LLVM => Some(Box::new(wasmer_llvm_backend::LLVMCompiler::new())), - #[cfg(any( - not(feature = "llvm"), - not(feature = "singlepass"), - not(feature = "cranelift") - ))] _ => None, } } diff --git a/src/bin/wasmer.rs b/src/bin/wasmer.rs index a08f7bd532b..457b6f51060 100644 --- a/src/bin/wasmer.rs +++ b/src/bin/wasmer.rs @@ -10,7 +10,7 @@ extern crate structopt; use std::env; -use std::fs::{read_to_string, File}; +use std::fs::{metadata, read_to_string, File}; use std::io; use std::io::Read; use std::path::PathBuf; @@ -130,7 +130,7 @@ struct Run { #[cfg(target_arch = "x86_64")] #[structopt( long = "backend", - default_value = "cranelift", + default_value = "auto", case_insensitive = true, possible_values = Backend::variants(), )] @@ -855,8 +855,30 @@ fn interactive_shell(mut ctx: InteractiveShellContext) -> ShellExitOperation { } } -fn run(options: Run) { - match execute_wasm(&options) { +fn update_backend(options: &mut Run) { + let binary_size = match metadata(&options.path) { + Ok(wasm_binary) => wasm_binary.len(), + Err(_e) => 0, + }; + + // Update backend when a backend flag is `auto`. + // Use the Singlepass backend if it's enabled and the file provided is larger + // than 10MiB (10485760 bytes), or it's enabled and the target architecture + // is AArch64. Otherwise, use the Cranelift backend. + if options.backend == Backend::Auto { + if Backend::variants().contains(&Backend::Singlepass.to_string()) + && (binary_size > 10485760 || cfg!(target_arch = "aarch64")) + { + options.backend = Backend::Singlepass; + } else { + options.backend = Backend::Cranelift; + } + } +} + +fn run(options: &mut Run) { + update_backend(options); + match execute_wasm(options) { Ok(()) => {} Err(message) => { eprintln!("Error: {}", message); @@ -940,6 +962,7 @@ fn get_compiler_by_backend(backend: Backend, _opts: &Run) -> Option Box::new(LLVMCompiler::new()), #[cfg(not(feature = "backend-llvm"))] Backend::LLVM => return None, + Backend::Auto => return None, }) } @@ -958,7 +981,7 @@ fn main() { } }); match options { - CLIOptions::Run(options) => run(options), + CLIOptions::Run(mut options) => run(&mut options), #[cfg(not(target_os = "windows"))] CLIOptions::SelfUpdate => update::self_update(), #[cfg(target_os = "windows")]