Make compiler config even simpler

This commit is contained in:
Syrus
2020-05-05 13:51:11 -07:00
parent fbeedbd3ac
commit c2b74e4c4a
9 changed files with 70 additions and 96 deletions

View File

@@ -20,7 +20,7 @@ publish = false
autoexamples = false autoexamples = false
[dependencies] [dependencies]
wasmer = { version = "0.16.2", path = "lib/api" } wasmer = { version = "0.16.2", path = "lib/api", default-features = false }
wasmer-compiler = { version = "0.16.2", path = "lib/compiler" } wasmer-compiler = { version = "0.16.2", path = "lib/compiler" }
wasmer-compiler-cranelift = { version = "0.16.2", path = "lib/compiler-cranelift", optional = true } wasmer-compiler-cranelift = { version = "0.16.2", path = "lib/compiler-cranelift", optional = true }
wasmer-compiler-singlepass = { version = "0.16.2", path = "lib/compiler-singlepass", optional = true } wasmer-compiler-singlepass = { version = "0.16.2", path = "lib/compiler-singlepass", optional = true }
@@ -53,33 +53,34 @@ anyhow = "1.0.28"
test-utils = { path = "tests/lib/test-utils" } test-utils = { path = "tests/lib/test-utils" }
[features] [features]
# Don't add the backend features in default, please add them on the Makefile # Don't add the compiler features in default, please add them on the Makefile
# since we might want to autoconfigure them depending on the availability on the host. # since we might want to autoconfigure them depending on the availability on the host.
default = ["wast", "wasi", "compiler-cranelift", "cache"] default = ["wat", "wast", "wasi", "cranelift", "cache"]
cache = ["wasmer-cache"] cache = ["wasmer-cache"]
wast = ["wasmer-wast"] wast = ["wasmer-wast"]
wasi = ["wasmer-wasi"] wasi = ["wasmer-wasi"]
wat = ["wasmer/wat"]
compiler = ["wasmer-jit/compiler"] compiler = ["wasmer-jit/compiler"]
experimental-io-devices = [ experimental-io-devices = [
"wasmer-wasi-experimental-io-devices", "wasmer-wasi-experimental-io-devices",
"wasi" "wasi"
] ]
compiler-singlepass = [ singlepass = [
"test-utils/compiler-singlepass",
"wasmer/compiler-singlepass",
"wasmer-compiler-singlepass", "wasmer-compiler-singlepass",
"test-utils/singlepass",
"wasmer/singlepass",
"compiler", "compiler",
] ]
compiler-cranelift = [ cranelift = [
"test-utils/compiler-cranelift",
"wasmer/compiler-cranelift",
"wasmer-compiler-cranelift", "wasmer-compiler-cranelift",
"test-utils/cranelift",
"wasmer/cranelift",
"compiler", "compiler",
] ]
compiler-llvm = [ llvm = [
"test-utils/compiler-llvm",
"wasmer/compiler-llvm",
"wasmer-compiler-llvm", "wasmer-compiler-llvm",
"test-utils/llvm",
"wasmer/llvm",
"compiler", "compiler",
] ]

View File

@@ -10,7 +10,7 @@ use std::path::PathBuf;
use std::process::Command; use std::process::Command;
use test_generator::{ use test_generator::{
build_ignores_from_textfile, test_directory, test_directory_module, wast_processor, build_ignores_from_textfile, test_directory, test_directory_module, wast_processor,
with_backends, with_test_module, Testsuite, with_features, with_test_module, Testsuite,
}; };
fn is_truthy_env(name: &str) -> bool { fn is_truthy_env(name: &str) -> bool {
@@ -34,7 +34,7 @@ fn main() -> anyhow::Result<()> {
}; };
let backends = vec!["singlepass", "cranelift", "llvm"]; let backends = vec!["singlepass", "cranelift", "llvm"];
with_backends(&mut spectests, &backends, |mut spectests| { with_features(&mut spectests, &backends, |mut spectests| {
with_test_module(&mut spectests, "spec", |spectests| { with_test_module(&mut spectests, "spec", |spectests| {
let _spec_tests = test_directory(spectests, "tests/wast/spec", wast_processor)?; let _spec_tests = test_directory(spectests, "tests/wast/spec", wast_processor)?;
test_directory_module( test_directory_module(

View File

@@ -37,26 +37,17 @@ anyhow = "1.0.28"
maintenance = { status = "actively-developed" } maintenance = { status = "actively-developed" }
[features] [features]
default = ["wat", "default-compiler-cranelift"] default = ["wat", "cranelift"]
compiler = ["wasmer-jit/compiler"] compiler = ["wasmer-jit/compiler"]
compiler-singlepass = [ singlepass = [
"wasmer-compiler-singlepass", "wasmer-compiler-singlepass",
"compiler", "compiler",
] ]
compiler-cranelift = [ cranelift = [
"wasmer-compiler-cranelift", "wasmer-compiler-cranelift",
"compiler", "compiler",
] ]
compiler-llvm = [ llvm = [
"wasmer-compiler-llvm", "wasmer-compiler-llvm",
"compiler", "compiler",
] ]
default-compiler-singlepass = [
"compiler-singlepass"
]
default-compiler-cranelift = [
"compiler-cranelift"
]
default-compiler-llvm = [
"compiler-llvm"
]

View File

@@ -35,14 +35,32 @@ pub use wasmer_jit::{
DeserializeError, InstantiationError, LinkError, RuntimeError, SerializeError, DeserializeError, InstantiationError, LinkError, RuntimeError, SerializeError,
}; };
#[cfg(feature = "compiler-singlepass")] // The compilers are mutually exclusive
pub use wasmer_compiler_singlepass::SinglepassConfig; #[cfg(any(
all(feature = "llvm", any(feature = "cranelift", feature = "singlepass")),
all(feature = "cranelift", feature = "singlepass")
))]
compile_error!(
r#"The `singlepass`, `cranelift` and `llvm` features are mutually exclusive.
If you wish to use more than one compiler, you can simply import it from it's own crate. Eg.:
#[cfg(feature = "compiler-cranelift")] ```
pub use wasmer_compiler_cranelift::CraneliftConfig; use wasmer::{Store, Engine};
use wasmer_compiler_singlepass::SinglepassConfig;
#[cfg(feature = "compiler-llvm")] let engine = Engine::new(SinglepassConfig::default());
pub use wasmer_compiler_llvm::LLVMConfig; let store = Store::new_config(&engine);
```"#
);
#[cfg(feature = "singlepass")]
pub use wasmer_compiler_singlepass::SinglepassConfig as DefaultCompilerConfig;
#[cfg(feature = "cranelift")]
pub use wasmer_compiler_cranelift::CraneliftConfig as DefaultCompilerConfig;
#[cfg(feature = "llvm")]
pub use wasmer_compiler_llvm::LLVMConfig as DefaultCompilerConfig;
/// Version number of this crate. /// Version number of this crate.
pub const VERSION: &str = env!("CARGO_PKG_VERSION"); pub const VERSION: &str = env!("CARGO_PKG_VERSION");

View File

@@ -26,37 +26,10 @@ impl Store {
Arc::ptr_eq(&a.engine, &b.engine) Arc::ptr_eq(&a.engine, &b.engine)
} }
#[cfg(any( #[cfg(feature = "compiler")]
feature = "default-compiler-singlepass", fn new_config(config: impl CompilerConfig) -> Self {
feature = "default-compiler-cranelift", let tunables = Tunables::for_target(config.target().triple());
feature = "default-compiler-llvm", Self::new(&Engine::new(&config, tunables))
))]
pub fn default_compiler_config() -> impl CompilerConfig {
#[cfg(any(
all(
feature = "default-compiler-llvm",
any(
feature = "default-compiler-cranelift",
feature = "default-compiler-singlepass"
)
),
all(
feature = "default-compiler-cranelift",
feature = "default-compiler-singlepass"
)
))]
compile_error!(
"The `default-compiler-X` features are mutually exclusive. Please choose just one"
);
#[cfg(feature = "default-compiler-cranelift")]
return wasmer_compiler_cranelift::CraneliftConfig::default();
#[cfg(feature = "default-compiler-llvm")]
return wasmer_compiler_llvm::LLVMConfig::default();
#[cfg(feature = "default-compiler-singlepass")]
return wasmer_compiler_singlepass::SinglepassConfig::default();
} }
} }
@@ -67,16 +40,11 @@ impl PartialEq for Store {
} }
// We only implement default if we have assigned a default compiler // We only implement default if we have assigned a default compiler
#[cfg(any( #[cfg(feature = "compiler")]
feature = "default-compiler-singlepass",
feature = "default-compiler-cranelift",
feature = "default-compiler-llvm",
))]
impl Default for Store { impl Default for Store {
fn default() -> Store { fn default() -> Store {
let config = Self::default_compiler_config(); let config = crate::DefaultCompilerConfig::default();
let tunables = Tunables::for_target(config.target().triple()); Store::new_config(config)
Store::new(&Engine::new(&config, tunables))
} }
} }

View File

@@ -79,11 +79,11 @@ impl StoreOptions {
return Compiler::from_str(&backend); return Compiler::from_str(&backend);
} else { } else {
// Auto mode, we choose the best compiler for that platform // Auto mode, we choose the best compiler for that platform
if cfg!(feature = "compiler-cranelift") && cfg!(target_arch = "x86_64") { if cfg!(feature = "cranelift") && cfg!(target_arch = "x86_64") {
return Ok(Compiler::Cranelift); return Ok(Compiler::Cranelift);
} else if cfg!(feature = "compiler-singlepass") && cfg!(target_arch = "x86_64") { } else if cfg!(feature = "singlepass") && cfg!(target_arch = "x86_64") {
return Ok(Compiler::Singlepass); return Ok(Compiler::Singlepass);
} else if cfg!(feature = "compiler-llvm") { } else if cfg!(feature = "llvm") {
return Ok(Compiler::LLVM); return Ok(Compiler::LLVM);
} else { } else {
bail!("There are no available compilers for your architecture") bail!("There are no available compilers for your architecture")
@@ -95,26 +95,22 @@ impl StoreOptions {
#[allow(unused_variables)] #[allow(unused_variables)]
fn get_config(&self, compiler: Compiler) -> Result<Box<dyn CompilerConfig>> { fn get_config(&self, compiler: Compiler) -> Result<Box<dyn CompilerConfig>> {
let config: Box<dyn CompilerConfig> = match compiler { let config: Box<dyn CompilerConfig> = match compiler {
#[cfg(feature = "compiler-singlepass")] #[cfg(feature = "singlepass")]
Compiler::Singlepass => { Compiler::Singlepass => {
let config = SinglepassConfig::default(); let config = wasmer_compiler_singlepass::SinglepassConfig::default();
Box::new(config) Box::new(config)
} }
#[cfg(feature = "compiler-cranelift")] #[cfg(feature = "cranelift")]
Compiler::Cranelift => { Compiler::Cranelift => {
let config = CraneliftConfig::default(); let config = wasmer_compiler_cranelift::CraneliftConfig::default();
Box::new(config) Box::new(config)
} }
#[cfg(feature = "compiler-llvm")] #[cfg(feature = "llvm")]
Compiler::LLVM => { Compiler::LLVM => {
let config = LLVMConfig::default(); let config = wasmer_compiler_llvm::LLVMConfig::default();
Box::new(config) Box::new(config)
} }
#[cfg(not(all( #[cfg(not(all(feature = "singlepass", feature = "cranelift", feature = "llvm",)))]
feature = "compiler-singlepass",
feature = "compiler-cranelift",
feature = "compiler-llvm",
)))]
compiler => bail!( compiler => bail!(
"The `{}` compiler is not included in this binary.", "The `{}` compiler is not included in this binary.",
compiler.to_string() compiler.to_string()

View File

@@ -167,13 +167,13 @@ pub fn with_test_module<T>(
Ok(result) Ok(result)
} }
pub fn with_backends( pub fn with_features(
mut out: &mut Testsuite, mut out: &mut Testsuite,
backends: &[&str], features: &[&str],
f: impl Fn(&mut Testsuite) -> anyhow::Result<()> + Copy, f: impl Fn(&mut Testsuite) -> anyhow::Result<()> + Copy,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
for compiler in backends.iter() { for compiler in features.iter() {
writeln!(out.buffer, "#[cfg(feature=\"compiler-{}\")]", compiler)?; writeln!(out.buffer, "#[cfg(feature=\"{}\")]", compiler)?;
writeln!(out.buffer, "#[cfg(test)]")?; writeln!(out.buffer, "#[cfg(test)]")?;
writeln!(out.buffer, "#[allow(non_snake_case)]")?; writeln!(out.buffer, "#[allow(non_snake_case)]")?;
with_test_module(&mut out, &compiler, f)?; with_test_module(&mut out, &compiler, f)?;

View File

@@ -16,15 +16,15 @@ wasmer-compiler-llvm = { path = "../../../lib/compiler-llvm", version = "0.16.2"
[features] [features]
compiler = [] compiler = []
compiler-singlepass = [ singlepass = [
"wasmer-compiler-singlepass", "wasmer-compiler-singlepass",
"compiler", "compiler",
] ]
compiler-cranelift = [ cranelift = [
"wasmer-compiler-cranelift", "wasmer-compiler-cranelift",
"compiler", "compiler",
] ]
compiler-llvm = [ llvm = [
"wasmer-compiler-llvm", "wasmer-compiler-llvm",
"compiler", "compiler",
] ]

View File

@@ -11,21 +11,21 @@ pub fn get_compiler_config_from_str(
let target = Target::default(); let target = Target::default();
match compiler_name { match compiler_name {
#[cfg(feature = "compiler-singlepass")] #[cfg(feature = "singlepass")]
"singlepass" => { "singlepass" => {
let mut singlepass_config = let mut singlepass_config =
wasmer_compiler_singlepass::SinglepassConfig::new(features, target); wasmer_compiler_singlepass::SinglepassConfig::new(features, target);
singlepass_config.enable_nan_canonicalization = try_nan_canonicalization; singlepass_config.enable_nan_canonicalization = try_nan_canonicalization;
Box::new(singlepass_config) Box::new(singlepass_config)
} }
#[cfg(feature = "compiler-cranelift")] #[cfg(feature = "cranelift")]
"cranelift" => { "cranelift" => {
let mut cranelift_config = let mut cranelift_config =
wasmer_compiler_cranelift::CraneliftConfig::new(features, target); wasmer_compiler_cranelift::CraneliftConfig::new(features, target);
cranelift_config.enable_nan_canonicalization = try_nan_canonicalization; cranelift_config.enable_nan_canonicalization = try_nan_canonicalization;
Box::new(cranelift_config) Box::new(cranelift_config)
} }
#[cfg(feature = "compiler-llvm")] #[cfg(feature = "llvm")]
"llvm" => { "llvm" => {
let mut llvm_config = wasmer_compiler_llvm::LLVMConfig::new(features, target); let mut llvm_config = wasmer_compiler_llvm::LLVMConfig::new(features, target);
llvm_config.enable_nan_canonicalization = try_nan_canonicalization; llvm_config.enable_nan_canonicalization = try_nan_canonicalization;