Improved feature-generation to be compiler-based

This commit is contained in:
Syrus
2020-06-18 02:11:54 -07:00
parent 422051ebe5
commit 769ffebb61
7 changed files with 44 additions and 26 deletions

View File

@@ -207,6 +207,11 @@ impl CompilerConfig for Cranelift {
fn push_middleware(&mut self, middleware: Arc<dyn FunctionMiddlewareGenerator>) {
self.middlewares.push(middleware);
}
/// Gets the default features for this compiler in the given target
fn default_features_for_target(&self, _target: &Target) -> Features {
Features::default()
}
}
impl Default for Cranelift {

View File

@@ -7,6 +7,7 @@ use inkwell::OptimizationLevel;
use itertools::Itertools;
use std::sync::Arc;
use target_lexicon::Architecture;
use wasm_common::Features;
use wasm_common::{FunctionType, LocalFunctionIndex};
use wasmer_compiler::{Compiler, CompilerConfig, FunctionMiddlewareGenerator, Target, Triple};
@@ -189,6 +190,11 @@ impl CompilerConfig for LLVM {
Box::new(LLVMCompiler::new(&self))
}
/// Gets the default features for this compiler in the given target
fn default_features_for_target(&self, _target: &Target) -> Features {
Features::default()
}
/// Pushes a middleware onto the back of the middleware chain.
fn push_middleware(&mut self, middleware: Arc<dyn FunctionMiddlewareGenerator>) {
self.middlewares.push(middleware);

View File

@@ -3,6 +3,7 @@
use crate::compiler::SinglepassCompiler;
use std::sync::Arc;
use wasm_common::Features;
use wasmer_compiler::{Compiler, CompilerConfig, CpuFeature, FunctionMiddlewareGenerator, Target};
#[derive(Clone)]
@@ -57,6 +58,13 @@ impl CompilerConfig for Singlepass {
Box::new(SinglepassCompiler::new(&self))
}
/// Gets the default features for this compiler in the given target
fn default_features_for_target(&self, target: &Target) -> Features {
let mut features = Features::default();
features.multi_value(false);
features
}
/// Pushes a middleware onto the back of the middleware chain.
fn push_middleware(&mut self, middleware: Arc<dyn FunctionMiddlewareGenerator>) {
self.middlewares.push(middleware);

View File

@@ -28,6 +28,9 @@ pub trait CompilerConfig {
/// Gets the custom compiler config
fn compiler(&self) -> Box<dyn Compiler + Send>;
/// Gets the default features for this compiler in the given target
fn default_features_for_target(&self, target: &Target) -> Features;
/// Pushes a middleware onto the back of the middleware chain.
fn push_middleware(&mut self, middleware: Arc<dyn FunctionMiddlewareGenerator>);
}

View File

@@ -4,18 +4,18 @@ use wasmer_compiler::{Compiler, CompilerConfig, Features, Target};
use wasmer_engine::Tunables;
/// The JIT builder
pub struct JIT {
compiler: Option<Box<dyn Compiler + Send>>,
pub struct JIT<'a> {
compiler_config: Option<&'a dyn CompilerConfig>,
tunables_fn: Option<Box<Fn(&Target) -> Box<dyn Tunables + Send + Sync>>>,
target: Option<Target>,
features: Option<Features>,
}
impl JIT {
impl<'a> JIT<'a> {
/// Create a new JIT
pub fn new(compiler_config: &dyn CompilerConfig) -> Self {
pub fn new(compiler_config: &'a dyn CompilerConfig) -> Self {
Self {
compiler: Some(compiler_config.compiler()),
compiler_config: Some(compiler_config),
target: None,
tunables_fn: None,
features: None,
@@ -25,7 +25,7 @@ impl JIT {
/// Create a new headless JIT
pub fn headless() -> Self {
Self {
compiler: None,
compiler_config: None,
target: None,
tunables_fn: None,
features: None,
@@ -62,8 +62,11 @@ impl JIT {
.tunables_fn
.expect("You need to specify tunables for the JIT");
let tunables: Arc<dyn Tunables + Send + Sync> = tunables_fn(&target).into();
if let Some(compiler) = self.compiler {
let features = self.features.unwrap_or_default();
if let Some(compiler_config) = self.compiler_config {
let features = self
.features
.unwrap_or_else(|| compiler_config.default_features_for_target(&target));
let compiler = compiler_config.compiler();
JITEngine::new(compiler, target, tunables, features)
} else {
JITEngine::headless(tunables)

View File

@@ -4,19 +4,19 @@ use wasmer_compiler::{Compiler, CompilerConfig, Features, Target};
use wasmer_engine::Tunables;
/// The Native builder
pub struct Native {
compiler: Option<Box<dyn Compiler + Send>>,
pub struct Native<'a> {
compiler_config: Option<&'a CompilerConfig>,
tunables_fn: Option<Box<Fn(&Target) -> Box<dyn Tunables + Send + Sync>>>,
target: Option<Target>,
features: Option<Features>,
}
impl Native {
impl<'a> Native<'a> {
/// Create a new Native
pub fn new(compiler_config: &mut dyn CompilerConfig) -> Self {
pub fn new(compiler_config: &'a mut dyn CompilerConfig) -> Self {
compiler_config.enable_pic();
Self {
compiler: Some(compiler_config.compiler()),
compiler_config: Some(compiler_config),
target: None,
tunables_fn: None,
features: None,
@@ -26,7 +26,7 @@ impl Native {
/// Create a new headless Native
pub fn headless() -> Self {
Self {
compiler: None,
compiler_config: None,
target: None,
tunables_fn: None,
features: None,
@@ -63,8 +63,11 @@ impl Native {
.tunables_fn
.expect("You need to specify tunables for the JIT");
let tunables: Arc<dyn Tunables + Send + Sync> = tunables_fn(&target).into();
if let Some(compiler) = self.compiler {
let features = self.features.unwrap_or_default();
if let Some(compiler_config) = self.compiler_config {
let features = self
.features
.unwrap_or_else(|| compiler_config.default_features_for_target(&target));
let compiler = compiler_config.compiler();
NativeEngine::new(compiler, target, tunables, features)
} else {
NativeEngine::headless(tunables)

View File

@@ -29,14 +29,9 @@ pub fn get_compiler(canonicalize_nans: bool) -> impl CompilerConfig {
}
pub fn get_engine() -> impl Engine {
let mut features = Features::default();
#[cfg(feature = "test-singlepass")]
features.multi_value(false);
let compiler_config = get_compiler(false);
JIT::new(&compiler_config)
.tunables(Tunables::for_target)
.features(features)
.engine()
}
@@ -47,17 +42,12 @@ pub fn get_store() -> Store {
pub fn get_store_with_middlewares<I: Iterator<Item = Arc<dyn FunctionMiddlewareGenerator>>>(
middlewares: I,
) -> Store {
let mut features = Features::default();
#[cfg(feature = "test-singlepass")]
features.multi_value(false);
let mut compiler_config = get_compiler(false);
for x in middlewares {
compiler_config.push_middleware(x);
}
let engine = JIT::new(&compiler_config)
.tunables(Tunables::for_target)
.features(features)
.engine();
Store::new(&engine)
}