From 8d89c3b4796fd6ca4b1e3d2661772f8111ac63b2 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Wed, 2 Oct 2019 13:59:49 -0700 Subject: [PATCH] Add options to wasmtime and wasm2obj to pick compilation strategy. --- src/bin/wasm2obj.rs | 6 +++++- src/bin/wasmtime.rs | 20 +++++++++++++++++--- src/bin/wast.rs | 16 ++++------------ src/lib.rs | 20 ++++++++++++++++++++ wasmtime-api/src/context.rs | 13 +++++++++---- wasmtime-api/src/runtime.rs | 21 +++++++++++++++++---- 6 files changed, 72 insertions(+), 24 deletions(-) diff --git a/src/bin/wasm2obj.rs b/src/bin/wasm2obj.rs index 768101a2c3..c796636750 100644 --- a/src/bin/wasm2obj.rs +++ b/src/bin/wasm2obj.rs @@ -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=] [--enable-simd] -o + wasm2obj [--target TARGET] [-Odg] [--disable-cache | --cache-config=] [--enable-simd] [--always-lightbeam | --always-cranelift] -o wasm2obj --create-cache-config [--cache-config=] 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, flag_create_cache_config: bool, flag_enable_simd: bool, + flag_always_lightbeam: bool, + flag_always_cranelift: bool, flag_optimize: bool, } diff --git a/src/bin/wasmtime.rs b/src/bin/wasmtime.rs index 10e362ea95..f3a6e4d812 100644 --- a/src/bin/wasmtime.rs +++ b/src/bin/wasmtime.rs @@ -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=] [--preload=...] [--env=...] [--dir=...] [--mapdir=...] [...] - wasmtime [-odg] [--enable-simd] [--wasi-c] [--disable-cache | --cache-config=] [--env=...] [--dir=...] [--mapdir=...] --invoke= [...] + wasmtime [-odg] [--enable-simd] [--wasi-c] [--disable-cache | --cache-config=] [--preload=...] [--env=...] [--dir=...] [--mapdir=...] [--always-lightbeam | --always-cranelift] [...] + wasmtime [-odg] [--enable-simd] [--wasi-c] [--disable-cache | --cache-config=] [--env=...] [--dir=...] [--mapdir=...] --invoke= [--always-lightbeam | --always-cranelift] [...] wasmtime --create-cache-config [--cache-config=] 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= 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, flag_preload: Vec, flag_env: Vec, @@ -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)); diff --git a/src/bin/wast.rs b/src/bin/wast.rs index 857ef392d5..4820d248b0 100644 --- a/src/bin/wast.rs +++ b/src/bin/wast.rs @@ -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); diff --git a/src/lib.rs b/src/lib.rs index aa6546c1c9..c2d5ce31a6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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); diff --git a/wasmtime-api/src/context.rs b/wasmtime-api/src/context.rs index 3eb40e7db4..3016288c59 100644 --- a/wasmtime-api/src/context.rs +++ b/wasmtime-api/src/context.rs @@ -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) } diff --git a/wasmtime-api/src/runtime.rs b/wasmtime-api/src/runtime.rs index 61de46a3b6..535120091e 100644 --- a/wasmtime-api/src/runtime.rs +++ b/wasmtime-api/src/runtime.rs @@ -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(), }