Remove usage of CompilationStrategy from Config (#764)

* Remove usage of `CompilationStrategy` from `Config`

This commit removes the public API usage of the internal
`CompilationStrategy` enumeration from the `Config` type in the
`wasmtime` crate. To do this the `enum` was copied locally into the
crate and renamed `Strategy`. The high-level description of this change
is:

* The `Config::strategy` method now takes a locally-defined `Strategy`
  enumeration instead of an internal type.

* The contents of `Strategy` are always the same, not relying on Cargo
  features to indicate which variants are present. This avoids
  unnecessary downstream `#[cfg]`.

* A `lightbeam` feature was added to the `wasmtime` crate itself to
  lightbeam compilation support.

* The `Config::strategy` method is now fallible. It returns a runtime
  error if support for the selected strategy wasn't compiled in.

* The `Strategy` enum is listed as `#[non_exhaustive]` so we can safely
  add variants over time to it.

This reduces the public crate dependencies of the `wasmtime` crate
itself, removing the need to reach into internal crates even more!

cc #708

* Fix fuzz targets

* Update nightly used to build releases

* Run rustfmt
This commit is contained in:
Alex Crichton
2020-01-06 18:08:13 -06:00
committed by GitHub
parent 787f50e107
commit 7474633cca
24 changed files with 173 additions and 153 deletions

View File

@@ -207,7 +207,7 @@ jobs:
submodules: true submodules: true
- uses: ./.github/actions/install-rust - uses: ./.github/actions/install-rust
with: with:
toolchain: nightly-2019-08-15 toolchain: nightly-2020-01-06
- uses: ./.github/actions/binary-compatible-builds - uses: ./.github/actions/binary-compatible-builds
- run: mkdir crates/misc/py/wheelhouse - run: mkdir crates/misc/py/wheelhouse
shell: bash shell: bash

3
Cargo.lock generated
View File

@@ -2016,6 +2016,7 @@ dependencies = [
name = "wasmtime-environ" name = "wasmtime-environ"
version = "0.7.0" version = "0.7.0"
dependencies = [ dependencies = [
"anyhow",
"base64 0.11.0", "base64 0.11.0",
"bincode", "bincode",
"cranelift-codegen", "cranelift-codegen",
@@ -2054,6 +2055,7 @@ dependencies = [
"env_logger 0.7.1", "env_logger 0.7.1",
"libfuzzer-sys", "libfuzzer-sys",
"log", "log",
"wasmtime",
"wasmtime-fuzzing", "wasmtime-fuzzing",
"wasmtime-jit", "wasmtime-jit",
] ]
@@ -2115,6 +2117,7 @@ dependencies = [
name = "wasmtime-obj" name = "wasmtime-obj"
version = "0.7.0" version = "0.7.0"
dependencies = [ dependencies = [
"anyhow",
"faerie", "faerie",
"more-asserts", "more-asserts",
"wasmtime-environ", "wasmtime-environ",

View File

@@ -60,7 +60,8 @@ members = [
lightbeam = [ lightbeam = [
"wasmtime-environ/lightbeam", "wasmtime-environ/lightbeam",
"wasmtime-jit/lightbeam", "wasmtime-jit/lightbeam",
"wasmtime-wast/lightbeam" "wasmtime-wast/lightbeam",
"wasmtime/lightbeam",
] ]
wasi-c = ["wasmtime-wasi-c"] wasi-c = ["wasmtime-wasi-c"]
test_programs = ["test-programs/test_programs"] test_programs = ["test-programs/test_programs"]

View File

@@ -149,7 +149,7 @@ fn write_testsuite_tests(
writeln!(out, "fn r#{}() -> anyhow::Result<()> {{", &testname)?; writeln!(out, "fn r#{}() -> anyhow::Result<()> {{", &testname)?;
writeln!( writeln!(
out, out,
"crate::run_wast(r#\"{}\"#, crate::CompilationStrategy::{})", "crate::run_wast(r#\"{}\"#, crate::Strategy::{})",
path.display(), path.display(),
strategy strategy
)?; )?;

View File

@@ -36,3 +36,9 @@ wat = "1.0"
[badges] [badges]
maintenance = { status = "actively-developed" } maintenance = { status = "actively-developed" }
[features]
# Enables experimental support for the lightbeam codegen backend, an alternative
# to cranelift. Requires Nightly Rust currently, and this is not enabled by
# default.
lightbeam = ["wasmtime-jit/lightbeam"]

View File

@@ -27,7 +27,7 @@ pub use crate::externals::*;
pub use crate::instance::Instance; pub use crate::instance::Instance;
pub use crate::module::Module; pub use crate::module::Module;
pub use crate::r#ref::{AnyRef, HostInfo, HostRef}; pub use crate::r#ref::{AnyRef, HostInfo, HostRef};
pub use crate::runtime::{Config, Engine, Store}; pub use crate::runtime::{Config, Engine, Store, Strategy};
pub use crate::trap::{FrameInfo, Trap, TrapInfo}; pub use crate::trap::{FrameInfo, Trap, TrapInfo};
pub use crate::types::*; pub use crate::types::*;
pub use crate::values::*; pub use crate::values::*;

View File

@@ -1,4 +1,5 @@
use crate::context::Context; use crate::context::Context;
use anyhow::Result;
use std::cell::RefCell; use std::cell::RefCell;
use std::collections::HashMap; use std::collections::HashMap;
use std::rc::Rc; use std::rc::Rc;
@@ -158,15 +159,29 @@ impl Config {
self self
} }
/// Configures the compilation `strategy` provided, indicating which /// Configures which compilation strategy will be used for wasm modules.
/// backend will be used for compiling WebAssembly to native code.
/// ///
/// Currently the primary strategies are with cranelift (an optimizing /// This method can be used to configure which compiler is used for wasm
/// compiler) or lightbeam (a fast single-pass JIT which produces code /// modules, and for more documentation consult the [`Strategy`] enumeration
/// quickly). /// and its documentation.
pub fn strategy(&mut self, strategy: CompilationStrategy) -> &mut Self { ///
self.strategy = strategy; /// # Errors
self ///
/// Some compilation strategies require compile-time options of `wasmtime`
/// itself to be set, but if they're not set and the strategy is specified
/// here then an error will be returned.
pub fn strategy(&mut self, strategy: Strategy) -> Result<&mut Self> {
self.strategy = match strategy {
Strategy::Auto => CompilationStrategy::Auto,
Strategy::Cranelift => CompilationStrategy::Cranelift,
#[cfg(feature = "lightbeam")]
Strategy::Lightbeam => CompilationStrategy::Lightbeam,
#[cfg(not(feature = "lightbeam"))]
Strategy::Lightbeam => {
anyhow::bail!("lightbeam compilation strategy wasn't enabled at compile time");
}
};
Ok(self)
} }
} }
@@ -176,6 +191,30 @@ impl Default for Config {
} }
} }
/// Possible Compilation strategies for a wasm module.
#[non_exhaustive]
#[derive(Clone, Debug)]
pub enum Strategy {
/// An indicator that the compilation strategy should be automatically
/// selected.
///
/// This is generally what you want for most projects and indicates that the
/// `wasmtime` crate itself should make the decision about what the best
/// code generator for a wasm module is.
///
/// Currently this always defaults to Cranelift, but the default value will
/// change over time.
Auto,
/// Currently the default backend, Cranelift aims to be a reasonably fast
/// code generator which generates high quality machine code.
Cranelift,
/// A single-pass code generator that is faster than Cranelift but doesn't
/// produce as high-quality code.
Lightbeam,
}
// Engine // Engine
/// An `Engine` which is a global context for compilation and management of wasm /// An `Engine` which is a global context for compilation and management of wasm

View File

@@ -12,6 +12,7 @@ readme = "README.md"
edition = "2018" edition = "2018"
[dependencies] [dependencies]
anyhow = "1.0"
cranelift-codegen = { version = "0.52.0", features = ["enable-serde"] } cranelift-codegen = { version = "0.52.0", features = ["enable-serde"] }
cranelift-entity = { version = "0.52.0", features = ["enable-serde"] } cranelift-entity = { version = "0.52.0", features = ["enable-serde"] }
cranelift-wasm = { version = "0.52.0", features = ["enable-serde"] } cranelift-wasm = { version = "0.52.0", features = ["enable-serde"] }

View File

@@ -1,6 +1,7 @@
//! Module for configuring the cache system. //! Module for configuring the cache system.
use super::worker; use super::worker;
use anyhow::{anyhow, bail, Context, Result};
use directories::ProjectDirs; use directories::ProjectDirs;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use log::{debug, error, trace, warn}; use log::{debug, error, trace, warn};
@@ -137,32 +138,32 @@ pub fn init<P: AsRef<Path> + Debug>(
/// Creates a new configuration file at specified path, or default path if None is passed. /// Creates a new configuration file at specified path, or default path if None is passed.
/// Fails if file already exists. /// Fails if file already exists.
pub fn create_new_config<P: AsRef<Path> + Debug>( pub fn create_new_config<P: AsRef<Path> + Debug>(config_file: Option<P>) -> Result<PathBuf> {
config_file: Option<P>,
) -> Result<PathBuf, String> {
trace!("Creating new config file, path: {:?}", config_file); trace!("Creating new config file, path: {:?}", config_file);
let config_file = config_file.as_ref().map_or_else( let config_file = config_file
|| DEFAULT_CONFIG_PATH.as_ref().map(|p| p.as_ref()), .as_ref()
|p| Ok(p.as_ref()), .map_or_else(
)?; || DEFAULT_CONFIG_PATH.as_ref().map(|p| p.as_ref()),
|p| Ok(p.as_ref()),
)
.map_err(|s| anyhow!("{}", s))?;
if config_file.exists() { if config_file.exists() {
return Err(format!( bail!(
"Specified config file already exists! Path: {}", "Specified config file already exists! Path: {}",
config_file.display() config_file.display()
)); );
} }
let parent_dir = config_file let parent_dir = config_file
.parent() .parent()
.ok_or_else(|| format!("Invalid cache config path: {}", config_file.display()))?; .ok_or_else(|| anyhow!("Invalid cache config path: {}", config_file.display()))?;
fs::create_dir_all(parent_dir).map_err(|err| { fs::create_dir_all(parent_dir).with_context(|| {
format!( format!(
"Failed to create config directory, config path: {}, error: {}", "Failed to create config directory, config path: {}",
config_file.display(), config_file.display(),
err
) )
})?; })?;
@@ -175,11 +176,10 @@ pub fn create_new_config<P: AsRef<Path> + Debug>(
enabled = true enabled = true
"; ";
fs::write(&config_file, &content).map_err(|err| { fs::write(&config_file, &content).with_context(|| {
format!( format!(
"Failed to flush config to the disk, path: {}, msg: {}", "Failed to flush config to the disk, path: {}",
config_file.display(), config_file.display(),
err
) )
})?; })?;

View File

@@ -32,14 +32,15 @@ fn host_isa() -> Box<dyn isa::TargetIsa> {
/// Performs initial validation, and returns early if the Wasm is invalid. /// Performs initial validation, and returns early if the Wasm is invalid.
/// ///
/// You can control which compiler is used via passing a `CompilationStrategy`. /// You can control which compiler is used via passing a `CompilationStrategy`.
pub fn instantiate(wasm: &[u8], compilation_strategy: CompilationStrategy) { pub fn instantiate(wasm: &[u8], strategy: Strategy) {
if wasmparser::validate(wasm, None).is_err() { if wasmparser::validate(wasm, None).is_err() {
return; return;
} }
let mut config = Config::new(); let mut config = Config::new();
config.strategy(compilation_strategy); config
.strategy(strategy)
.expect("failed to enable lightbeam");
let engine = Engine::new(&config); let engine = Engine::new(&config);
let store = HostRef::new(Store::new(&engine)); let store = HostRef::new(Store::new(&engine));

View File

@@ -5,10 +5,11 @@
//! use the Wasm binary by including it via //! use the Wasm binary by including it via
//! `include_bytes!("./regressions/some-descriptive-name.wasm")`. //! `include_bytes!("./regressions/some-descriptive-name.wasm")`.
use wasmtime::Strategy;
use wasmtime_fuzzing::oracles; use wasmtime_fuzzing::oracles;
#[test] #[test]
fn instantiate_empty_module() { fn instantiate_empty_module() {
let data = wat::parse_str(include_str!("./regressions/empty.wat")).unwrap(); let data = wat::parse_str(include_str!("./regressions/empty.wat")).unwrap();
oracles::instantiate(&data, wasmtime_jit::CompilationStrategy::Auto); oracles::instantiate(&data, Strategy::Auto);
} }

View File

@@ -11,6 +11,7 @@ readme = "README.md"
edition = "2018" edition = "2018"
[dependencies] [dependencies]
anyhow = "1.0"
wasmtime-environ = { path = "../environ" } wasmtime-environ = { path = "../environ" }
faerie = "0.13.0" faerie = "0.13.0"
more-asserts = "0.2.1" more-asserts = "0.2.1"

View File

@@ -1,3 +1,4 @@
use anyhow::Result;
use faerie::{Artifact, Decl}; use faerie::{Artifact, Decl};
use wasmtime_environ::DataInitializer; use wasmtime_environ::DataInitializer;
@@ -6,10 +7,9 @@ pub fn declare_data_segment(
obj: &mut Artifact, obj: &mut Artifact,
_data_initaliazer: &DataInitializer, _data_initaliazer: &DataInitializer,
index: usize, index: usize,
) -> Result<(), String> { ) -> Result<()> {
let name = format!("_memory_{}", index); let name = format!("_memory_{}", index);
obj.declare(name, Decl::data()) obj.declare(name, Decl::data())?;
.map_err(|err| format!("{}", err))?;
Ok(()) Ok(())
} }
@@ -18,9 +18,8 @@ pub fn emit_data_segment(
obj: &mut Artifact, obj: &mut Artifact,
data_initaliazer: &DataInitializer, data_initaliazer: &DataInitializer,
index: usize, index: usize,
) -> Result<(), String> { ) -> Result<()> {
let name = format!("_memory_{}", index); let name = format!("_memory_{}", index);
obj.define(name, Vec::from(data_initaliazer.data)) obj.define(name, Vec::from(data_initaliazer.data))?;
.map_err(|err| format!("{}", err))?;
Ok(()) Ok(())
} }

View File

@@ -1,3 +1,4 @@
use anyhow::Result;
use faerie::{Artifact, Decl, Link}; use faerie::{Artifact, Decl, Link};
use wasmtime_environ::entity::EntityRef; use wasmtime_environ::entity::EntityRef;
use wasmtime_environ::settings; use wasmtime_environ::settings;
@@ -19,26 +20,23 @@ pub fn declare_functions(
obj: &mut Artifact, obj: &mut Artifact,
module: &Module, module: &Module,
relocations: &Relocations, relocations: &Relocations,
) -> Result<(), String> { ) -> Result<()> {
for i in 0..module.imported_funcs.len() { for i in 0..module.imported_funcs.len() {
let string_name = format!("_wasm_function_{}", i); let string_name = format!("_wasm_function_{}", i);
obj.declare(string_name, Decl::function_import()) obj.declare(string_name, Decl::function_import())?;
.map_err(|err| format!("{}", err))?;
} }
for (_, function_relocs) in relocations.iter() { for (_, function_relocs) in relocations.iter() {
for r in function_relocs { for r in function_relocs {
let special_import_name = get_reloc_target_special_import_name(r.reloc_target); let special_import_name = get_reloc_target_special_import_name(r.reloc_target);
if let Some(special_import_name) = special_import_name { if let Some(special_import_name) = special_import_name {
obj.declare(special_import_name, Decl::function_import()) obj.declare(special_import_name, Decl::function_import())?;
.map_err(|err| format!("{}", err))?;
} }
} }
} }
for (i, _function_relocs) in relocations.iter().rev() { for (i, _function_relocs) in relocations.iter().rev() {
let func_index = module.func_index(i); let func_index = module.func_index(i);
let string_name = format!("_wasm_function_{}", func_index.index()); let string_name = format!("_wasm_function_{}", func_index.index());
obj.declare(string_name, Decl::function().global()) obj.declare(string_name, Decl::function().global())?;
.map_err(|err| format!("{}", err))?;
} }
Ok(()) Ok(())
} }
@@ -49,7 +47,7 @@ pub fn emit_functions(
module: &Module, module: &Module,
compilation: &Compilation, compilation: &Compilation,
relocations: &Relocations, relocations: &Relocations,
) -> Result<(), String> { ) -> Result<()> {
debug_assert!( debug_assert!(
module.start_func.is_none() module.start_func.is_none()
|| module.start_func.unwrap().index() >= module.imported_funcs.len(), || module.start_func.unwrap().index() >= module.imported_funcs.len(),
@@ -66,8 +64,7 @@ pub fn emit_functions(
let func_index = module.func_index(i); let func_index = module.func_index(i);
let string_name = format!("_wasm_function_{}", func_index.index()); let string_name = format!("_wasm_function_{}", func_index.index());
obj.define(string_name, body.clone()) obj.define(string_name, body.clone())?;
.map_err(|err| format!("{}", err))?;
} }
for (i, function_relocs) in relocations.iter() { for (i, function_relocs) in relocations.iter() {
@@ -82,8 +79,7 @@ pub fn emit_functions(
from: &string_name, from: &string_name,
to: &target_name, to: &target_name,
at: r.offset as u64, at: r.offset as u64,
}) })?;
.map_err(|err| format!("{}", err))?;
} }
RelocationTarget::Memory32Grow RelocationTarget::Memory32Grow
| RelocationTarget::ImportedMemory32Grow | RelocationTarget::ImportedMemory32Grow
@@ -93,8 +89,7 @@ pub fn emit_functions(
from: &string_name, from: &string_name,
to: get_reloc_target_special_import_name(r.reloc_target).expect("name"), to: get_reloc_target_special_import_name(r.reloc_target).expect("name"),
at: r.offset as u64, at: r.offset as u64,
}) })?;
.map_err(|err| format!("{}", err))?;
} }
RelocationTarget::JumpTable(_, _) => { RelocationTarget::JumpTable(_, _) => {
// ignore relocations for jump tables // ignore relocations for jump tables

View File

@@ -2,6 +2,7 @@ use crate::context::layout_vmcontext;
use crate::data_segment::{declare_data_segment, emit_data_segment}; use crate::data_segment::{declare_data_segment, emit_data_segment};
use crate::function::{declare_functions, emit_functions}; use crate::function::{declare_functions, emit_functions};
use crate::table::{declare_table, emit_table}; use crate::table::{declare_table, emit_table};
use anyhow::Result;
use faerie::{Artifact, Decl, Link}; use faerie::{Artifact, Decl, Link};
use wasmtime_environ::isa::TargetFrontendConfig; use wasmtime_environ::isa::TargetFrontendConfig;
use wasmtime_environ::{Compilation, DataInitializer, Module, Relocations}; use wasmtime_environ::{Compilation, DataInitializer, Module, Relocations};
@@ -10,18 +11,16 @@ fn emit_vmcontext_init(
obj: &mut Artifact, obj: &mut Artifact,
module: &Module, module: &Module,
target_config: &TargetFrontendConfig, target_config: &TargetFrontendConfig,
) -> Result<(), String> { ) -> Result<()> {
let (data, table_relocs) = layout_vmcontext(module, target_config); let (data, table_relocs) = layout_vmcontext(module, target_config);
obj.declare_with("_vmcontext_init", Decl::data().global(), data.to_vec()) obj.declare_with("_vmcontext_init", Decl::data().global(), data.to_vec())?;
.map_err(|err| format!("{}", err))?;
for reloc in table_relocs.iter() { for reloc in table_relocs.iter() {
let target_name = format!("_table_{}", reloc.index); let target_name = format!("_table_{}", reloc.index);
obj.link(Link { obj.link(Link {
from: "_vmcontext_init", from: "_vmcontext_init",
to: &target_name, to: &target_name,
at: reloc.offset as u64, at: reloc.offset as u64,
}) })?;
.map_err(|err| format!("{}", err))?;
} }
Ok(()) Ok(())
} }
@@ -35,7 +34,7 @@ pub fn emit_module(
relocations: &Relocations, relocations: &Relocations,
data_initializers: &[DataInitializer], data_initializers: &[DataInitializer],
target_config: &TargetFrontendConfig, target_config: &TargetFrontendConfig,
) -> Result<(), String> { ) -> Result<()> {
declare_functions(obj, module, relocations)?; declare_functions(obj, module, relocations)?;
for (i, initializer) in data_initializers.iter().enumerate() { for (i, initializer) in data_initializers.iter().enumerate() {

View File

@@ -1,18 +1,17 @@
use anyhow::Result;
use faerie::{Artifact, Decl}; use faerie::{Artifact, Decl};
/// Declares data segment symbol /// Declares data segment symbol
pub fn declare_table(obj: &mut Artifact, index: usize) -> Result<(), String> { pub fn declare_table(obj: &mut Artifact, index: usize) -> Result<()> {
let name = format!("_table_{}", index); let name = format!("_table_{}", index);
obj.declare(name, Decl::data()) obj.declare(name, Decl::data())?;
.map_err(|err| format!("{}", err))?;
Ok(()) Ok(())
} }
/// Emit segment data and initialization location /// Emit segment data and initialization location
pub fn emit_table(obj: &mut Artifact, index: usize) -> Result<(), String> { pub fn emit_table(obj: &mut Artifact, index: usize) -> Result<()> {
let name = format!("_table_{}", index); let name = format!("_table_{}", index);
// FIXME: We need to initialize table using function symbols // FIXME: We need to initialize table using function symbols
obj.define(name, Vec::new()) obj.define(name, Vec::new())?;
.map_err(|err| format!("{}", err))?;
Ok(()) Ok(())
} }

View File

@@ -14,6 +14,7 @@ env_logger = "0.7.1"
log = "0.4.8" log = "0.4.8"
wasmtime-fuzzing = { path = "../crates/fuzzing", features = ["env_logger"] } wasmtime-fuzzing = { path = "../crates/fuzzing", features = ["env_logger"] }
wasmtime-jit = { path = "../crates/jit" } wasmtime-jit = { path = "../crates/jit" }
wasmtime = { path = "../crates/api" }
libfuzzer-sys = { git = "https://github.com/rust-fuzz/libfuzzer-sys.git" } libfuzzer-sys = { git = "https://github.com/rust-fuzz/libfuzzer-sys.git" }
[[bin]] [[bin]]

View File

@@ -1,12 +1,9 @@
#![no_main] #![no_main]
use libfuzzer_sys::fuzz_target; use libfuzzer_sys::fuzz_target;
use wasmtime::Strategy;
use wasmtime_fuzzing::{oracles, with_log_wasm_test_case}; use wasmtime_fuzzing::{oracles, with_log_wasm_test_case};
use wasmtime_jit::CompilationStrategy;
fuzz_target!(|data: &[u8]| { fuzz_target!(|data: &[u8]| {
with_log_wasm_test_case!(data, |data| oracles::instantiate( with_log_wasm_test_case!(data, |data| oracles::instantiate(data, Strategy::Auto,));
data,
CompilationStrategy::Auto
));
}); });

View File

@@ -1,12 +1,12 @@
#![no_main] #![no_main]
use libfuzzer_sys::fuzz_target; use libfuzzer_sys::fuzz_target;
use wasmtime::Strategy;
use wasmtime_fuzzing::{generators, oracles, with_log_wasm_test_case}; use wasmtime_fuzzing::{generators, oracles, with_log_wasm_test_case};
use wasmtime_jit::CompilationStrategy;
fuzz_target!(|data: generators::WasmOptTtf| { fuzz_target!(|data: generators::WasmOptTtf| {
with_log_wasm_test_case!(&data.wasm, |wasm| oracles::instantiate( with_log_wasm_test_case!(&data.wasm, |wasm| oracles::instantiate(
wasm, wasm,
CompilationStrategy::Auto Strategy::Auto,
)); ));
}); });

View File

@@ -26,15 +26,15 @@
) )
)] )]
use anyhow::{anyhow, bail, Result};
use docopt::Docopt; use docopt::Docopt;
use faerie::Artifact; use faerie::Artifact;
use serde::Deserialize; use serde::Deserialize;
use std::error::Error;
use std::fmt::format;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::str::FromStr; use std::str::FromStr;
use std::{process, str}; use std::{process, str};
use target_lexicon::Triple; use target_lexicon::Triple;
use wasmtime::Strategy;
use wasmtime_cli::pick_compilation_strategy; use wasmtime_cli::pick_compilation_strategy;
use wasmtime_debug::{emit_debugsections, read_debuginfo}; use wasmtime_debug::{emit_debugsections, read_debuginfo};
use wasmtime_environ::entity::EntityRef; use wasmtime_environ::entity::EntityRef;
@@ -47,7 +47,7 @@ use wasmtime_environ::{
cache_create_new_config, cache_init, Compiler, Cranelift, ModuleEnvironment, ModuleVmctxInfo, cache_create_new_config, cache_init, Compiler, Cranelift, ModuleEnvironment, ModuleVmctxInfo,
Tunables, VMOffsets, Tunables, VMOffsets,
}; };
use wasmtime_jit::{native, CompilationStrategy}; use wasmtime_jit::native;
use wasmtime_obj::emit_module; use wasmtime_obj::emit_module;
const USAGE: &str = " const USAGE: &str = "
@@ -176,18 +176,14 @@ fn handle_module(
enable_optimize: bool, enable_optimize: bool,
cranelift: bool, cranelift: bool,
lightbeam: bool, lightbeam: bool,
) -> Result<(), String> { ) -> Result<()> {
let data = match wat::parse_file(path) { let data = wat::parse_file(path)?;
Ok(data) => data,
Err(err) => {
return Err(String::from(err.description()));
}
};
let isa_builder = match *target { let isa_builder = match *target {
Some(ref target) => { Some(ref target) => {
let target = Triple::from_str(&target).map_err(|_| "could not parse --target")?; let target =
native::lookup(target).map_err(|err| format!("{:?}", err))? Triple::from_str(&target).map_err(|_| anyhow!("could not parse --target"))?;
native::lookup(target)?
} }
None => native::builder(), None => native::builder(),
}; };
@@ -213,7 +209,7 @@ fn handle_module(
let tunables = Tunables::default(); let tunables = Tunables::default();
// Decide how to compile. // Decide how to compile.
let strategy = pick_compilation_strategy(cranelift, lightbeam); let strategy = pick_compilation_strategy(cranelift, lightbeam)?;
let ( let (
module, module,
@@ -224,10 +220,7 @@ fn handle_module(
) = { ) = {
let environ = ModuleEnvironment::new(isa.frontend_config(), tunables); let environ = ModuleEnvironment::new(isa.frontend_config(), tunables);
let translation = environ let translation = environ.translate(&data)?;
.translate(&data)
.map_err(|error| error.to_string())?;
( (
translation.module, translation.module,
translation.module_translation.unwrap(), translation.module_translation.unwrap(),
@@ -240,25 +233,24 @@ fn handle_module(
// TODO: use the traps information // TODO: use the traps information
let (compilation, relocations, address_transform, value_ranges, stack_slots, _traps) = let (compilation, relocations, address_transform, value_ranges, stack_slots, _traps) =
match strategy { match strategy {
CompilationStrategy::Auto | CompilationStrategy::Cranelift => { Strategy::Auto | Strategy::Cranelift => Cranelift::compile_module(
Cranelift::compile_module(
&module,
&module_translation,
lazy_function_body_inputs,
&*isa,
generate_debug_info,
)
.map_err(|e| e.to_string())?
}
#[cfg(feature = "lightbeam")]
CompilationStrategy::Lightbeam => Lightbeam::compile_module(
&module, &module,
&module_translation, &module_translation,
lazy_function_body_inputs, lazy_function_body_inputs,
&*isa, &*isa,
generate_debug_info, generate_debug_info,
) )?,
.map_err(|e| e.to_string())?, #[cfg(feature = "lightbeam")]
Strategy::Lightbeam => Lightbeam::compile_module(
&module,
&module_translation,
lazy_function_body_inputs,
&*isa,
generate_debug_info,
)?,
#[cfg(not(feature = "lightbeam"))]
Strategy::Lightbeam => bail!("lightbeam support not enabled"),
other => bail!("unsupported compilation strategy {:?}", other),
}; };
let module_vmctx_info = { let module_vmctx_info = {
@@ -288,14 +280,12 @@ fn handle_module(
&debug_data, &debug_data,
&address_transform, &address_transform,
&value_ranges, &value_ranges,
) )?
.map_err(|e| e.to_string())?;
} }
// FIXME: Make the format a parameter. // FIXME: Make the format a parameter.
let file = let file = ::std::fs::File::create(Path::new(output))?;
::std::fs::File::create(Path::new(output)).map_err(|x| format(format_args!("{}", x)))?; obj.write(file)?;
obj.write(file).map_err(|e| e.to_string())?;
Ok(()) Ok(())
} }

View File

@@ -257,11 +257,11 @@ fn main() -> Result<()> {
} }
// Decide how to compile. // Decide how to compile.
let strategy = pick_compilation_strategy(args.flag_cranelift, args.flag_lightbeam); config.strategy(pick_compilation_strategy(
args.flag_cranelift,
config args.flag_lightbeam,
.flags(settings::Flags::new(flag_builder)) )?)?;
.strategy(strategy); config.flags(settings::Flags::new(flag_builder));
let engine = Engine::new(&config); let engine = Engine::new(&config);
let store = HostRef::new(Store::new(&engine)); let store = HostRef::new(Store::new(&engine));

View File

@@ -22,8 +22,8 @@
) )
)] )]
use anyhow::{Context, Result};
use docopt::Docopt; use docopt::Docopt;
use pretty_env_logger;
use serde::Deserialize; use serde::Deserialize;
use std::path::Path; use std::path::Path;
use std::process; use std::process;
@@ -75,7 +75,7 @@ struct Args {
flag_cranelift: bool, flag_cranelift: bool,
} }
fn main() { fn main() -> Result<()> {
let version = env!("CARGO_PKG_VERSION"); let version = env!("CARGO_PKG_VERSION");
let args: Args = Docopt::new(USAGE) let args: Args = Docopt::new(USAGE)
.and_then(|d| { .and_then(|d| {
@@ -95,19 +95,12 @@ fn main() {
}; };
if args.flag_create_cache_config { if args.flag_create_cache_config {
match cache_create_new_config(args.flag_cache_config) { let path = cache_create_new_config(args.flag_cache_config)?;
Ok(path) => { println!(
println!( "Successfully created new configuation file at {}",
"Successfully created new configuation file at {}", path.display()
path.display() );
); return Ok(());
return;
}
Err(err) => {
eprintln!("Error: {}", err);
process::exit(1);
}
}
} }
let errors = cache_init( let errors = cache_init(
@@ -148,22 +141,20 @@ fn main() {
} }
// Decide how to compile. // Decide how to compile.
let strategy = pick_compilation_strategy(args.flag_cranelift, args.flag_lightbeam); cfg.strategy(pick_compilation_strategy(
cfg.strategy(strategy) args.flag_cranelift,
.flags(settings::Flags::new(flag_builder)); args.flag_lightbeam,
)?)?
.flags(settings::Flags::new(flag_builder));
let store = HostRef::new(Store::new(&Engine::new(&cfg))); let store = HostRef::new(Store::new(&Engine::new(&cfg)));
let mut wast_context = WastContext::new(store); let mut wast_context = WastContext::new(store);
wast_context wast_context
.register_spectest() .register_spectest()
.expect("error instantiating \"spectest\""); .context("error instantiating \"spectest\"")?;
for filename in &args.arg_file { for filename in &args.arg_file {
wast_context wast_context.run_file(Path::new(&filename))?;
.run_file(Path::new(&filename))
.unwrap_or_else(|e| {
eprintln!("{:?}", e);
process::exit(1)
});
} }
Ok(())
} }

View File

@@ -1,16 +1,13 @@
use wasmtime_jit::CompilationStrategy; use anyhow::{bail, Result};
use wasmtime::Strategy;
pub fn pick_compilation_strategy(cranelift: bool, lightbeam: bool) -> CompilationStrategy { pub fn pick_compilation_strategy(cranelift: bool, lightbeam: bool) -> Result<Strategy> {
// Decide how to compile. Ok(match (lightbeam, cranelift) {
match (lightbeam, cranelift) { (true, false) => Strategy::Lightbeam,
#[cfg(feature = "lightbeam")] (false, true) => Strategy::Cranelift,
(true, false) => CompilationStrategy::Lightbeam, (false, false) => Strategy::Auto,
#[cfg(not(feature = "lightbeam"))] (true, true) => bail!("Can't enable --cranelift and --lightbeam at the same time"),
(true, false) => panic!("--lightbeam given, but Lightbeam support is not enabled"), })
(false, true) => CompilationStrategy::Cranelift,
(false, false) => CompilationStrategy::Auto,
(true, true) => panic!("Can't enable --cranelift and --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) {

View File

@@ -1,8 +1,7 @@
use std::path::Path; use std::path::Path;
use wasmtime::{Config, Engine, HostRef, Store}; use wasmtime::{Config, Engine, HostRef, Store, Strategy};
use wasmtime_environ::settings; use wasmtime_environ::settings;
use wasmtime_environ::settings::Configurable; use wasmtime_environ::settings::Configurable;
use wasmtime_jit::CompilationStrategy;
use wasmtime_wast::WastContext; use wasmtime_wast::WastContext;
include!(concat!(env!("OUT_DIR"), "/wast_testsuite_tests.rs")); include!(concat!(env!("OUT_DIR"), "/wast_testsuite_tests.rs"));
@@ -10,7 +9,7 @@ include!(concat!(env!("OUT_DIR"), "/wast_testsuite_tests.rs"));
// Each of the tests included from `wast_testsuite_tests` will call this // Each of the tests included from `wast_testsuite_tests` will call this
// function which actually executes the `wast` test suite given the `strategy` // function which actually executes the `wast` test suite given the `strategy`
// to compile it. // to compile it.
fn run_wast(wast: &str, strategy: CompilationStrategy) -> anyhow::Result<()> { fn run_wast(wast: &str, strategy: Strategy) -> anyhow::Result<()> {
let wast = Path::new(wast); let wast = Path::new(wast);
let mut flag_builder = settings::builder(); let mut flag_builder = settings::builder();
@@ -21,7 +20,7 @@ fn run_wast(wast: &str, strategy: CompilationStrategy) -> anyhow::Result<()> {
let mut cfg = Config::new(); let mut cfg = Config::new();
cfg.wasm_simd(wast.iter().any(|s| s == "simd")) cfg.wasm_simd(wast.iter().any(|s| s == "simd"))
.wasm_multi_value(wast.iter().any(|s| s == "multi-value")) .wasm_multi_value(wast.iter().any(|s| s == "multi-value"))
.strategy(strategy) .strategy(strategy)?
.flags(settings::Flags::new(flag_builder)); .flags(settings::Flags::new(flag_builder));
let store = HostRef::new(Store::new(&Engine::new(&cfg))); let store = HostRef::new(Store::new(&Engine::new(&cfg)));
let mut wast_context = WastContext::new(store); let mut wast_context = WastContext::new(store);