Add "auto" backend to change the backend depending on the wasm file size

This commit is contained in:
Asami Doi
2019-11-22 19:02:57 +09:00
parent dfc7163b71
commit fd0df9946b
5 changed files with 34 additions and 10 deletions

View File

@ -4,6 +4,7 @@
- [#1006](https://github.com/wasmerio/wasmer/pull/1006) Fix minor panic issue when `wasmer::compile_with` called with llvm backend - [#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. - [#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 ## 0.11.0 - 2019-11-22

View File

@ -28,6 +28,7 @@ pub enum Backend {
Cranelift, Cranelift,
Singlepass, Singlepass,
LLVM, LLVM,
Auto,
} }
impl Backend { impl Backend {
@ -40,6 +41,7 @@ impl Backend {
"singlepass", "singlepass",
#[cfg(feature = "backend-llvm")] #[cfg(feature = "backend-llvm")]
"llvm", "llvm",
"auto",
] ]
} }
@ -50,6 +52,7 @@ impl Backend {
Backend::Cranelift => "cranelift", Backend::Cranelift => "cranelift",
Backend::Singlepass => "singlepass", Backend::Singlepass => "singlepass",
Backend::LLVM => "llvm", Backend::LLVM => "llvm",
Backend::Auto => "auto",
} }
} }
} }
@ -67,6 +70,7 @@ impl std::str::FromStr for Backend {
"singlepass" => Ok(Backend::Singlepass), "singlepass" => Ok(Backend::Singlepass),
"cranelift" => Ok(Backend::Cranelift), "cranelift" => Ok(Backend::Cranelift),
"llvm" => Ok(Backend::LLVM), "llvm" => Ok(Backend::LLVM),
"auto" => Ok(Backend::Auto),
_ => Err(format!("The backend {} doesn't exist", s)), _ => Err(format!("The backend {} doesn't exist", s)),
} }
} }

View File

@ -265,6 +265,7 @@ fn requires_pre_validation(backend: Backend) -> bool {
Backend::Cranelift => true, Backend::Cranelift => true,
Backend::LLVM => true, Backend::LLVM => true,
Backend::Singlepass => false, Backend::Singlepass => false,
Backend::Auto => false,
} }
} }

View File

@ -254,11 +254,6 @@ pub fn compiler_for_backend(backend: Backend) -> Option<Box<dyn Compiler>> {
#[cfg(feature = "llvm")] #[cfg(feature = "llvm")]
Backend::LLVM => Some(Box::new(wasmer_llvm_backend::LLVMCompiler::new())), Backend::LLVM => Some(Box::new(wasmer_llvm_backend::LLVMCompiler::new())),
#[cfg(any(
not(feature = "llvm"),
not(feature = "singlepass"),
not(feature = "cranelift")
))]
_ => None, _ => None,
} }
} }

View File

@ -10,7 +10,7 @@
extern crate structopt; extern crate structopt;
use std::env; use std::env;
use std::fs::{read_to_string, File}; use std::fs::{metadata, read_to_string, File};
use std::io; use std::io;
use std::io::Read; use std::io::Read;
use std::path::PathBuf; use std::path::PathBuf;
@ -130,7 +130,7 @@ struct Run {
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
#[structopt( #[structopt(
long = "backend", long = "backend",
default_value = "cranelift", default_value = "auto",
case_insensitive = true, case_insensitive = true,
possible_values = Backend::variants(), possible_values = Backend::variants(),
)] )]
@ -855,8 +855,30 @@ fn interactive_shell(mut ctx: InteractiveShellContext) -> ShellExitOperation {
} }
} }
fn run(options: Run) { fn update_backend(options: &mut Run) {
match execute_wasm(&options) { 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(()) => {} Ok(()) => {}
Err(message) => { Err(message) => {
eprintln!("Error: {}", message); eprintln!("Error: {}", message);
@ -940,6 +962,7 @@ fn get_compiler_by_backend(backend: Backend, _opts: &Run) -> Option<Box<dyn Comp
Backend::LLVM => Box::new(LLVMCompiler::new()), Backend::LLVM => Box::new(LLVMCompiler::new()),
#[cfg(not(feature = "backend-llvm"))] #[cfg(not(feature = "backend-llvm"))]
Backend::LLVM => return None, Backend::LLVM => return None,
Backend::Auto => return None,
}) })
} }
@ -958,7 +981,7 @@ fn main() {
} }
}); });
match options { match options {
CLIOptions::Run(options) => run(options), CLIOptions::Run(mut options) => run(&mut options),
#[cfg(not(target_os = "windows"))] #[cfg(not(target_os = "windows"))]
CLIOptions::SelfUpdate => update::self_update(), CLIOptions::SelfUpdate => update::self_update(),
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]