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

View File

@@ -44,6 +44,7 @@ use std::path::{Path, PathBuf};
use std::process::exit; use std::process::exit;
use wabt; use wabt;
use wasi_common::preopen_dir; use wasi_common::preopen_dir;
use wasmtime::pick_compilation_strategy;
use wasmtime_api::{Config, Engine, HostRef, Instance, Module, Store}; use wasmtime_api::{Config, Engine, HostRef, Instance, Module, Store};
use wasmtime_environ::{cache_create_new_config, cache_init}; use wasmtime_environ::{cache_create_new_config, cache_init};
use wasmtime_interface_types::ModuleData; 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. given with --invoke are then called.
Usage: 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>] [--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> <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 --create-cache-config [--cache-config=<cache_config_file>]
wasmtime --help | --version wasmtime --help | --version
@@ -80,6 +81,8 @@ Options:
instead of default one instead of default one
-g generate debug information -g generate debug information
-d, --debug enable debug output on stderr/stdout -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 --enable-simd enable proposed SIMD instructions
--wasi-c enable the wasi-c implementation of WASI --wasi-c enable the wasi-c implementation of WASI
--preload=<wasm> load an additional wasm module before loading the main module --preload=<wasm> load an additional wasm module before loading the main module
@@ -102,6 +105,8 @@ struct Args {
flag_debug: bool, flag_debug: bool,
flag_g: bool, flag_g: bool,
flag_enable_simd: bool, flag_enable_simd: bool,
flag_always_lightbeam: bool,
flag_always_cranelift: bool,
flag_invoke: Option<String>, flag_invoke: Option<String>,
flag_preload: Vec<String>, flag_preload: Vec<String>,
flag_env: Vec<String>, flag_env: Vec<String>,
@@ -281,7 +286,16 @@ fn rmain() -> Result<(), Error> {
flag_builder.set("opt_level", "speed")?; 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 engine = HostRef::new(Engine::new(config));
let store = HostRef::new(Store::new(engine)); let store = HostRef::new(Store::new(engine));

View File

@@ -33,8 +33,9 @@ use pretty_env_logger;
use serde::Deserialize; use serde::Deserialize;
use std::path::Path; use std::path::Path;
use std::process; use std::process;
use wasmtime::pick_compilation_strategy;
use wasmtime_environ::{cache_create_new_config, cache_init}; use wasmtime_environ::{cache_create_new_config, cache_init};
use wasmtime_jit::{CompilationStrategy, Compiler, Features}; use wasmtime_jit::{Compiler, Features};
use wasmtime_wast::WastContext; use wasmtime_wast::WastContext;
const USAGE: &str = " const USAGE: &str = "
@@ -153,17 +154,8 @@ fn main() {
} }
// Decide how to compile. // Decide how to compile.
let strategy = match (args.flag_always_lightbeam, args.flag_always_cranelift) { let strategy =
#[cfg(feature = "lightbeam")] pick_compilation_strategy(args.flag_always_cranelift, args.flag_always_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 isa = isa_builder.finish(settings::Flags::new(flag_builder)); let isa = isa_builder.finish(settings::Flags::new(flag_builder));
let engine = Compiler::new(isa, strategy); 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) { pub fn init_file_per_thread_logger(prefix: &'static str) {
file_per_thread_logger::initialize(prefix); 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 { pub fn create(
Context::new(create_compiler(flags), features, debug_info) 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 { 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 = {
let isa_builder = let isa_builder =
cranelift_native::builder().expect("host machine is not a supported target"); cranelift_native::builder().expect("host machine is not a supported target");
isa_builder.finish(flags) 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 crate::r#ref::HostRef;
use cranelift_codegen::{ir, settings}; use cranelift_codegen::{ir, settings};
use wasmtime_jit::Features; use wasmtime_jit::{CompilationStrategy, Features};
// Runtime Environment // Runtime Environment
@@ -21,6 +21,7 @@ pub struct Config {
flags: settings::Flags, flags: settings::Flags,
features: Features, features: Features,
debug_info: bool, debug_info: bool,
strategy: CompilationStrategy,
} }
impl Config { impl Config {
@@ -29,14 +30,21 @@ impl Config {
debug_info: false, debug_info: false,
features: Default::default(), features: Default::default(),
flags: default_flags(), 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 { Config {
flags, flags,
features, features,
debug_info, debug_info,
strategy,
} }
} }
@@ -51,6 +59,10 @@ impl Config {
pub(crate) fn features(&self) -> &Features { pub(crate) fn features(&self) -> &Features {
&self.features &self.features
} }
pub(crate) fn strategy(&self) -> CompilationStrategy {
self.strategy
}
} }
// Engine // Engine
@@ -74,7 +86,7 @@ impl Engine {
pub fn create_wasmtime_context(&self) -> wasmtime_jit::Context { pub fn create_wasmtime_context(&self) -> wasmtime_jit::Context {
let flags = self.config.flags().clone(); 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 flags = engine.borrow().config().flags().clone();
let features = engine.borrow().config().features().clone(); let features = engine.borrow().config().features().clone();
let debug_info = engine.borrow().config().debug_info(); let debug_info = engine.borrow().config().debug_info();
let strategy = engine.borrow().config().strategy();
Store { Store {
engine, engine,
context: Context::create(flags, features, debug_info), context: Context::create(flags, features, debug_info, strategy),
global_exports: Rc::new(RefCell::new(HashMap::new())), global_exports: Rc::new(RefCell::new(HashMap::new())),
signature_cache: HashMap::new(), signature_cache: HashMap::new(),
} }