Skip to content

Commit

Permalink
Merge #1004
Browse files Browse the repository at this point in the history
1004: Adapt backend usage depending on wasm file executed r=Hywan a=d0iasm

Adapt backend usage depending on wasm file executed in issue #998.
Close #998 

# Description
Add `auto` backend into a runtime-core and use it as a default backend.
The `auto` backend is equivalent to: 
* singlepass if singlepass is enabled and the wasm file size is larger than 10MiB, or singlepass is enable and the target architecture is aarch64.
* cranelift otherwise.


Co-authored-by: Asami Doi <[email protected]>
Co-authored-by: Ivan Enderlin <[email protected]>
  • Loading branch information
3 people authored Nov 25, 2019
2 parents 95ccd99 + 9d958d7 commit 5b47d0a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 the Auto backend to enable to adapt backend usage depending on wasm file executed.

## 0.11.0 - 2019-11-22

Expand Down
6 changes: 5 additions & 1 deletion lib/runtime-core/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub enum Backend {
Cranelift,
Singlepass,
LLVM,
Auto,
}

impl Backend {
Expand All @@ -40,6 +41,7 @@ impl Backend {
"singlepass",
#[cfg(feature = "backend-llvm")]
"llvm",
"auto",
]
}

Expand All @@ -50,13 +52,14 @@ impl Backend {
Backend::Cranelift => "cranelift",
Backend::Singlepass => "singlepass",
Backend::LLVM => "llvm",
Backend::Auto => "auto",
}
}
}

impl Default for Backend {
fn default() -> Self {
Backend::Cranelift
Backend::Auto
}
}

Expand All @@ -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)),
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/runtime-core/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ fn requires_pre_validation(backend: Backend) -> bool {
Backend::Cranelift => true,
Backend::LLVM => true,
Backend::Singlepass => false,
Backend::Auto => false,
}
}

Expand Down
7 changes: 1 addition & 6 deletions lib/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
//! let value = add_one.call(42)?;
//!
//! assert_eq!(value, 43);
//!
//!
//! Ok(())
//! }
//! ```
Expand Down Expand Up @@ -254,11 +254,6 @@ pub fn compiler_for_backend(backend: Backend) -> Option<Box<dyn Compiler>> {
#[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,
}
}
Expand Down
33 changes: 28 additions & 5 deletions src/bin/wasmer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
)]
Expand Down Expand Up @@ -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` 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) => {
eprintln!("Error: {}", message);
Expand Down Expand Up @@ -940,6 +962,7 @@ fn get_compiler_by_backend(backend: Backend, _opts: &Run) -> Option<Box<dyn Comp
Backend::LLVM => Box::new(LLVMCompiler::new()),
#[cfg(not(feature = "backend-llvm"))]
Backend::LLVM => return None,
Backend::Auto => return None,
})
}

Expand All @@ -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")]
Expand Down

0 comments on commit 5b47d0a

Please sign in to comment.