Add options to wasmtime and wasm2obj to pick compilation strategy.

This commit is contained in:
Dan Gohman
2019-10-02 13:59:49 -07:00
parent d4353f03cb
commit 8d89c3b479
6 changed files with 72 additions and 24 deletions

View File

@@ -63,7 +63,7 @@ The translation is dependent on the environment chosen.
The default is a dummy environment that produces placeholder values.
Usage:
wasm2obj [--target TARGET] [-Odg] [--disable-cache | --cache-config=<cache_config_file>] [--enable-simd] <file> -o <output>
wasm2obj [--target TARGET] [-Odg] [--disable-cache | --cache-config=<cache_config_file>] [--enable-simd] [--always-lightbeam | --always-cranelift] <file> -o <output>
wasm2obj --create-cache-config [--cache-config=<cache_config_file>]
wasm2obj --help | --version
@@ -80,6 +80,8 @@ Options:
creates default configuration and writes it to the disk,
use with --cache-config to specify custom config file
instead of default one
--always-lightbeam use Lightbeam for all compilation
--always-cranelift use Cranelift for all compilation
--enable-simd enable proposed SIMD instructions
-O, --optimize runs optimization passes on the translated functions
--version print the Cranelift version
@@ -97,6 +99,8 @@ struct Args {
flag_cache_config: Option<String>,
flag_create_cache_config: bool,
flag_enable_simd: bool,
flag_always_lightbeam: bool,
flag_always_cranelift: bool,
flag_optimize: bool,
}

View File

@@ -44,6 +44,7 @@ use std::path::{Path, PathBuf};
use std::process::exit;
use wabt;
use wasi_common::preopen_dir;
use wasmtime::pick_compilation_strategy;
use wasmtime_api::{Config, Engine, HostRef, Instance, Module, Store};
use wasmtime_environ::{cache_create_new_config, cache_init};
use wasmtime_interface_types::ModuleData;
@@ -62,8 +63,8 @@ including calling the start function if one is present. Additional functions
given with --invoke are then called.
Usage:
wasmtime [-odg] [--enable-simd] [--wasi-c] [--disable-cache | --cache-config=<cache_config_file>] [--preload=<wasm>...] [--env=<env>...] [--dir=<dir>...] [--mapdir=<mapping>...] <file> [<arg>...]
wasmtime [-odg] [--enable-simd] [--wasi-c] [--disable-cache | --cache-config=<cache_config_file>] [--env=<env>...] [--dir=<dir>...] [--mapdir=<mapping>...] --invoke=<fn> <file> [<arg>...]
wasmtime [-odg] [--enable-simd] [--wasi-c] [--disable-cache | --cache-config=<cache_config_file>] [--preload=<wasm>...] [--env=<env>...] [--dir=<dir>...] [--mapdir=<mapping>...] [--always-lightbeam | --always-cranelift] <file> [<arg>...]
wasmtime [-odg] [--enable-simd] [--wasi-c] [--disable-cache | --cache-config=<cache_config_file>] [--env=<env>...] [--dir=<dir>...] [--mapdir=<mapping>...] --invoke=<fn> [--always-lightbeam | --always-cranelift] <file> [<arg>...]
wasmtime --create-cache-config [--cache-config=<cache_config_file>]
wasmtime --help | --version
@@ -80,6 +81,8 @@ Options:
instead of default one
-g generate debug information
-d, --debug enable debug output on stderr/stdout
--always-lightbeam use Lightbeam for all compilation
--always-cranelift use Cranelift for all compilation
--enable-simd enable proposed SIMD instructions
--wasi-c enable the wasi-c implementation of WASI
--preload=<wasm> load an additional wasm module before loading the main module
@@ -102,6 +105,8 @@ struct Args {
flag_debug: bool,
flag_g: bool,
flag_enable_simd: bool,
flag_always_lightbeam: bool,
flag_always_cranelift: bool,
flag_invoke: Option<String>,
flag_preload: Vec<String>,
flag_env: Vec<String>,
@@ -281,7 +286,16 @@ fn rmain() -> Result<(), Error> {
flag_builder.set("opt_level", "speed")?;
}
let config = Config::new(settings::Flags::new(flag_builder), features, debug_info);
// Decide how to compile.
let strategy =
pick_compilation_strategy(args.flag_always_cranelift, args.flag_always_lightbeam);
let config = Config::new(
settings::Flags::new(flag_builder),
features,
debug_info,
strategy,
);
let engine = HostRef::new(Engine::new(config));
let store = HostRef::new(Store::new(engine));

View File

@@ -33,8 +33,9 @@ use pretty_env_logger;
use serde::Deserialize;
use std::path::Path;
use std::process;
use wasmtime::pick_compilation_strategy;
use wasmtime_environ::{cache_create_new_config, cache_init};
use wasmtime_jit::{CompilationStrategy, Compiler, Features};
use wasmtime_jit::{Compiler, Features};
use wasmtime_wast::WastContext;
const USAGE: &str = "
@@ -153,17 +154,8 @@ fn main() {
}
// Decide how to compile.
let strategy = match (args.flag_always_lightbeam, args.flag_always_cranelift) {
#[cfg(feature = "lightbeam")]
(true, false) => CompilationStrategy::AlwaysLightbeam,
#[cfg(not(feature = "lightbeam"))]
(true, false) => panic!("--always-lightbeam given, but Lightbeam support is not enabled"),
(false, true) => CompilationStrategy::AlwaysCranelift,
(false, false) => CompilationStrategy::Auto,
(true, true) => {
panic!("Can't enable --always-cranelift and --always-lightbeam at the same time")
}
};
let strategy =
pick_compilation_strategy(args.flag_always_cranelift, args.flag_always_lightbeam);
let isa = isa_builder.finish(settings::Flags::new(flag_builder));
let engine = Compiler::new(isa, strategy);

View File

@@ -1,3 +1,23 @@
use wasmtime_jit::CompilationStrategy;
pub fn pick_compilation_strategy(
always_cranelift: bool,
always_lightbeam: bool,
) -> CompilationStrategy {
// Decide how to compile.
match (always_lightbeam, always_cranelift) {
#[cfg(feature = "lightbeam")]
(true, false) => CompilationStrategy::AlwaysLightbeam,
#[cfg(not(feature = "lightbeam"))]
(true, false) => panic!("--always-lightbeam given, but Lightbeam support is not enabled"),
(false, true) => CompilationStrategy::AlwaysCranelift,
(false, false) => CompilationStrategy::Auto,
(true, true) => {
panic!("Can't enable --always-cranelift and --always-lightbeam at the same time")
}
}
}
pub fn init_file_per_thread_logger(prefix: &'static str) {
file_per_thread_logger::initialize(prefix);

View File

@@ -22,8 +22,13 @@ impl Context {
}
}
pub fn create(flags: settings::Flags, features: Features, debug_info: bool) -> Context {
Context::new(create_compiler(flags), features, debug_info)
pub fn create(
flags: settings::Flags,
features: Features,
debug_info: bool,
strategy: CompilationStrategy,
) -> Context {
Context::new(create_compiler(flags, strategy), features, debug_info)
}
pub(crate) fn debug_info(&self) -> bool {
@@ -52,12 +57,12 @@ impl PartialEq for Context {
}
}
pub(crate) fn create_compiler(flags: settings::Flags) -> Compiler {
pub(crate) fn create_compiler(flags: settings::Flags, strategy: CompilationStrategy) -> Compiler {
let isa = {
let isa_builder =
cranelift_native::builder().expect("host machine is not a supported target");
isa_builder.finish(flags)
};
Compiler::new(isa, CompilationStrategy::Auto)
Compiler::new(isa, strategy)
}

View File

@@ -6,7 +6,7 @@ use crate::context::{create_compiler, Context};
use crate::r#ref::HostRef;
use cranelift_codegen::{ir, settings};
use wasmtime_jit::Features;
use wasmtime_jit::{CompilationStrategy, Features};
// Runtime Environment
@@ -21,6 +21,7 @@ pub struct Config {
flags: settings::Flags,
features: Features,
debug_info: bool,
strategy: CompilationStrategy,
}
impl Config {
@@ -29,14 +30,21 @@ impl Config {
debug_info: false,
features: Default::default(),
flags: default_flags(),
strategy: CompilationStrategy::Auto,
}
}
pub fn new(flags: settings::Flags, features: Features, debug_info: bool) -> Config {
pub fn new(
flags: settings::Flags,
features: Features,
debug_info: bool,
strategy: CompilationStrategy,
) -> Config {
Config {
flags,
features,
debug_info,
strategy,
}
}
@@ -51,6 +59,10 @@ impl Config {
pub(crate) fn features(&self) -> &Features {
&self.features
}
pub(crate) fn strategy(&self) -> CompilationStrategy {
self.strategy
}
}
// Engine
@@ -74,7 +86,7 @@ impl Engine {
pub fn create_wasmtime_context(&self) -> wasmtime_jit::Context {
let flags = self.config.flags().clone();
wasmtime_jit::Context::new(Box::new(create_compiler(flags)))
wasmtime_jit::Context::new(Box::new(create_compiler(flags, self.config.strategy())))
}
}
@@ -92,9 +104,10 @@ impl Store {
let flags = engine.borrow().config().flags().clone();
let features = engine.borrow().config().features().clone();
let debug_info = engine.borrow().config().debug_info();
let strategy = engine.borrow().config().strategy();
Store {
engine,
context: Context::create(flags, features, debug_info),
context: Context::create(flags, features, debug_info, strategy),
global_exports: Rc::new(RefCell::new(HashMap::new())),
signature_cache: HashMap::new(),
}