Skip to content

Commit

Permalink
Add "auto" backend to change the backend depending on the wasm file size
Browse files Browse the repository at this point in the history
  • Loading branch information
d0iasm committed Nov 27, 2019
1 parent e8ebc4c commit 757017b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 10 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
4 changes: 4 additions & 0 deletions 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,6 +52,7 @@ impl Backend {
Backend::Cranelift => "cranelift",
Backend::Singlepass => "singlepass",
Backend::LLVM => "llvm",
Backend::Auto => "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
5 changes: 0 additions & 5 deletions lib/runtime/src/lib.rs
Original file line number Diff line number Diff line change
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`.
// 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 757017b

Please sign in to comment.