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

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

View File

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

View File

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