From 620e0f3a5a71511ca318d8f8a396bd5434977876 Mon Sep 17 00:00:00 2001 From: Asami Doi Date: Fri, 22 Nov 2019 19:02:57 +0900 Subject: [PATCH 1/8] Set backend as a default and select backend depending on a wasm file --- lib/runtime-core/src/backend.rs | 4 ++++ lib/runtime-core/src/codegen.rs | 1 + src/bin/wasmer.rs | 22 +++++++++++++++++----- 3 files changed, 22 insertions(+), 5 deletions(-) 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 99158fb1501..7085b7c7e2b 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 => false, Backend::Singlepass => false, + Backend::Auto => false, } } diff --git a/src/bin/wasmer.rs b/src/bin/wasmer.rs index a08f7bd532b..c850bb6509c 100644 --- a/src/bin/wasmer.rs +++ b/src/bin/wasmer.rs @@ -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(), )] @@ -487,7 +487,7 @@ fn execute_wasi( } /// Execute a wasm/wat file -fn execute_wasm(options: &Run) -> Result<(), String> { +fn execute_wasm(options: &mut Run) -> Result<(), String> { let disable_cache = options.disable_cache; let mapped_dirs = get_mapped_dirs(&options.mapped_dirs[..])?; @@ -547,6 +547,17 @@ fn execute_wasm(options: &Run) -> Result<(), String> { None }; + // Update backend when a backend flag is `auto` or no specific value. + // Use a singlepass if it's enable and the file provided is larger + // than 10MiB (10485760 bytes). Otherwise, use a cranelift as a backend. + if options.backend == Backend::Auto { + options.backend = Backend::Cranelift; + if Backend::variants().contains(&Backend::Singlepass.to_string()) && + wasm_binary.len() > 10485760 { + options.backend = Backend::Singlepass; + } + } + // Don't error on --enable-all for other backends. if options.features.simd && options.backend != Backend::LLVM { return Err("SIMD is only supported in the LLVM backend for now".to_string()); @@ -855,8 +866,8 @@ fn interactive_shell(mut ctx: InteractiveShellContext) -> ShellExitOperation { } } -fn run(options: Run) { - match execute_wasm(&options) { +fn run(options: &mut Run) { + match execute_wasm(options) { Ok(()) => {} Err(message) => { eprintln!("Error: {}", message); @@ -940,6 +951,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 +970,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")] From dcc1f92f62c1995cd055d6aa8746e246fc0cb8d2 Mon Sep 17 00:00:00 2001 From: Asami Doi Date: Fri, 22 Nov 2019 19:51:58 +0900 Subject: [PATCH 2/8] Fix a lint error --- src/bin/wasmer.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/bin/wasmer.rs b/src/bin/wasmer.rs index c850bb6509c..da65d5e231f 100644 --- a/src/bin/wasmer.rs +++ b/src/bin/wasmer.rs @@ -552,8 +552,9 @@ fn execute_wasm(options: &mut Run) -> Result<(), String> { // than 10MiB (10485760 bytes). Otherwise, use a cranelift as a backend. if options.backend == Backend::Auto { options.backend = Backend::Cranelift; - if Backend::variants().contains(&Backend::Singlepass.to_string()) && - wasm_binary.len() > 10485760 { + if Backend::variants().contains(&Backend::Singlepass.to_string()) + && wasm_binary.len() > 10485760 + { options.backend = Backend::Singlepass; } } From 0a290b32d2036a9aafd2e2b10db402d395c1e302 Mon Sep 17 00:00:00 2001 From: Asami Doi Date: Fri, 22 Nov 2019 20:14:39 +0900 Subject: [PATCH 3/8] Use singlepass backend when the target architecture is aarch64 --- src/bin/wasmer.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/bin/wasmer.rs b/src/bin/wasmer.rs index da65d5e231f..9cd80e0621c 100644 --- a/src/bin/wasmer.rs +++ b/src/bin/wasmer.rs @@ -551,11 +551,12 @@ fn execute_wasm(options: &mut Run) -> Result<(), String> { // Use a singlepass if it's enable and the file provided is larger // than 10MiB (10485760 bytes). Otherwise, use a cranelift as a backend. if options.backend == Backend::Auto { - options.backend = Backend::Cranelift; if Backend::variants().contains(&Backend::Singlepass.to_string()) - && wasm_binary.len() > 10485760 + && (wasm_binary.len() > 10485760 || cfg!(target_arch = "aarch64")) { options.backend = Backend::Singlepass; + } else { + options.backend = Backend::Cranelift; } } From f79cbcd7693ce47c42eaee238201f4f8580a884e Mon Sep 17 00:00:00 2001 From: Asami Doi Date: Fri, 22 Nov 2019 20:20:25 +0900 Subject: [PATCH 4/8] Update comments --- src/bin/wasmer.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/bin/wasmer.rs b/src/bin/wasmer.rs index 9cd80e0621c..87cf8997b88 100644 --- a/src/bin/wasmer.rs +++ b/src/bin/wasmer.rs @@ -547,9 +547,10 @@ fn execute_wasm(options: &mut Run) -> Result<(), String> { None }; - // Update backend when a backend flag is `auto` or no specific value. + // Update backend when a backend flag is `auto` or a default value. // Use a singlepass if it's enable and the file provided is larger - // than 10MiB (10485760 bytes). Otherwise, use a cranelift as a backend. + // than 10MiB (10485760 bytes), or it's enable and the target architecture + // is aarch64. Otherwise, use a cranelift as a backend. if options.backend == Backend::Auto { if Backend::variants().contains(&Backend::Singlepass.to_string()) && (wasm_binary.len() > 10485760 || cfg!(target_arch = "aarch64")) From 5f2854c9c0a9c806e6b963ae3e005d059f6e3357 Mon Sep 17 00:00:00 2001 From: Asami Doi Date: Mon, 25 Nov 2019 11:22:33 +0900 Subject: [PATCH 5/8] Update a comment and a changelog --- CHANGELOG.md | 1 + lib/runtime-core/src/backend.rs | 2 +- src/bin/wasmer.rs | 6 +++--- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5df0b8ae3ab..82b055db5a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## **[Unreleased]** - [#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 Auto backend to enable change 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 febfdda0e98..c64f99c1855 100644 --- a/lib/runtime-core/src/backend.rs +++ b/lib/runtime-core/src/backend.rs @@ -59,7 +59,7 @@ impl Backend { impl Default for Backend { fn default() -> Self { - Backend::Cranelift + Backend::Auto } } diff --git a/src/bin/wasmer.rs b/src/bin/wasmer.rs index 87cf8997b88..dc991d95e24 100644 --- a/src/bin/wasmer.rs +++ b/src/bin/wasmer.rs @@ -548,9 +548,9 @@ fn execute_wasm(options: &mut Run) -> Result<(), String> { }; // Update backend when a backend flag is `auto` or a default value. - // Use a singlepass if it's enable and the file provided is larger - // than 10MiB (10485760 bytes), or it's enable and the target architecture - // is aarch64. Otherwise, use a cranelift as a backend. + // 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()) && (wasm_binary.len() > 10485760 || cfg!(target_arch = "aarch64")) From 958bc23d36c7636529956ed016828c86f71e8eb6 Mon Sep 17 00:00:00 2001 From: Asami Doi Date: Mon, 25 Nov 2019 13:37:53 +0900 Subject: [PATCH 6/8] Add a helper function to update the backend --- CHANGELOG.md | 2 +- src/bin/wasmer.rs | 43 +++++++++++++++++++++++++++---------------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 82b055db5a4..8c71fd4073d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ## **[Unreleased]** - [#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 Auto backend to enable change backend usage depending on wasm file executed. +- [#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/src/bin/wasmer.rs b/src/bin/wasmer.rs index dc991d95e24..fc36feb90a7 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; @@ -487,7 +487,7 @@ fn execute_wasi( } /// Execute a wasm/wat file -fn execute_wasm(options: &mut Run) -> Result<(), String> { +fn execute_wasm(options: &Run) -> Result<(), String> { let disable_cache = options.disable_cache; let mapped_dirs = get_mapped_dirs(&options.mapped_dirs[..])?; @@ -547,20 +547,6 @@ fn execute_wasm(options: &mut Run) -> Result<(), String> { None }; - // Update backend when a backend flag is `auto` or a default value. - // 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()) - && (wasm_binary.len() > 10485760 || cfg!(target_arch = "aarch64")) - { - options.backend = Backend::Singlepass; - } else { - options.backend = Backend::Cranelift; - } - } - // Don't error on --enable-all for other backends. if options.features.simd && options.backend != Backend::LLVM { return Err("SIMD is only supported in the LLVM backend for now".to_string()); @@ -572,6 +558,9 @@ fn execute_wasm(options: &mut Run) -> Result<(), String> { .map_err(|e| format!("Can't convert from wast to wasm: {:?}", e))?; } + println!("execute_wasm() {:#?}", options); + println!("execute_wasm() {:#?}", options.backend); + let compiler: Box = match get_compiler_by_backend(options.backend, options) { Some(x) => x, None => return Err("the requested backend is not enabled".into()), @@ -869,7 +858,29 @@ fn interactive_shell(mut ctx: InteractiveShellContext) -> ShellExitOperation { } } +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` or a default value. + // 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) => { From a3abf93dde6516cb0117bcb0a55e6041480c4d04 Mon Sep 17 00:00:00 2001 From: Asami Doi Date: Mon, 25 Nov 2019 14:04:10 +0900 Subject: [PATCH 7/8] Fix lint errors and remove the flag for a conditional compilation --- lib/runtime/src/lib.rs | 7 +------ src/bin/wasmer.rs | 36 ++++++++++++++++++------------------ 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/lib/runtime/src/lib.rs b/lib/runtime/src/lib.rs index 2ac6c174261..37e3f5695c1 100644 --- a/lib/runtime/src/lib.rs +++ b/lib/runtime/src/lib.rs @@ -71,7 +71,7 @@ //! let value = add_one.call(42)?; //! //! assert_eq!(value, 43); -//! +//! //! Ok(()) //! } //! ``` @@ -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 fc36feb90a7..23722836e13 100644 --- a/src/bin/wasmer.rs +++ b/src/bin/wasmer.rs @@ -859,24 +859,24 @@ fn interactive_shell(mut ctx: InteractiveShellContext) -> ShellExitOperation { } 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` or a default value. - // 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; - } - } + let binary_size = match metadata(&options.path) { + Ok(wasm_binary) => wasm_binary.len(), + Err(_e) => 0, + }; + + // Update backend when a backend flag is `auto` or a default value. + // 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) { From 160f36eccb219d9e653042da3b12a1d5a221d0e4 Mon Sep 17 00:00:00 2001 From: Asami Doi Date: Mon, 25 Nov 2019 14:16:42 +0900 Subject: [PATCH 8/8] Remove unused println --- src/bin/wasmer.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/bin/wasmer.rs b/src/bin/wasmer.rs index 23722836e13..2e44cbfe247 100644 --- a/src/bin/wasmer.rs +++ b/src/bin/wasmer.rs @@ -558,9 +558,6 @@ fn execute_wasm(options: &Run) -> Result<(), String> { .map_err(|e| format!("Can't convert from wast to wasm: {:?}", e))?; } - println!("execute_wasm() {:#?}", options); - println!("execute_wasm() {:#?}", options.backend); - let compiler: Box = match get_compiler_by_backend(options.backend, options) { Some(x) => x, None => return Err("the requested backend is not enabled".into()),