Skip to content

Commit

Permalink
Moved tunables into Store
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed Jun 18, 2020
1 parent 83203a8 commit 5273fa0
Show file tree
Hide file tree
Showing 18 changed files with 118 additions and 167 deletions.
15 changes: 7 additions & 8 deletions benches/static_and_dynamic_functions.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use test_utils::get_compiler_config_from_str;

use wasmer::*;
use wasmer_engine_jit::JITEngine;
use wasmer_engine_jit::JIT;

static BASIC_WAT: &str = r#"(module
(func $multiply (import "env" "multiply") (param i32 i32) (result i32))
Expand Down Expand Up @@ -151,39 +150,39 @@ pub fn run_basic_dynamic_function(store: &Store, compiler_name: &str, c: &mut Cr
fn run_static_benchmarks(c: &mut Criterion) {
#[cfg(feature = "llvm")]
{
let store = test_utils::get_default_llvm_store();
let store = Store::new(&JIT::new(&wasmer_compiler_llvm::LLVM::new()).engine());
run_basic_static_function(&store, "llvm", c);
}

#[cfg(feature = "cranelift")]
{
let store = test_utils::get_default_cranelift_store();
let store = Store::new(&JIT::new(&wasmer_compiler_cranelift::Cranelift::new()).engine());
run_basic_static_function(&store, "cranelift", c);
}

#[cfg(feature = "singlepass")]
{
let store = test_utils::get_default_singlepass_store();
let store = Store::new(&JIT::new(&wasmer_compiler_singlepass::Singlepass::new()).engine());
run_basic_static_function(&store, "singlepass", c);
}
}

fn run_dynamic_benchmarks(c: &mut Criterion) {
#[cfg(feature = "llvm")]
{
let store = test_utils::get_default_llvm_store();
let store = Store::new(&JIT::new(&wasmer_compiler_llvm::LLVM::new()).engine());
run_basic_dynamic_function(&store, "llvm", c);
}

#[cfg(feature = "cranelift")]
{
let store = test_utils::get_default_cranelift_store();
let store = Store::new(&JIT::new(&wasmer_compiler_cranelift::Cranelift::new()).engine());
run_basic_dynamic_function(&store, "cranelift", c);
}

#[cfg(feature = "singlepass")]
{
let store = test_utils::get_default_singlepass_store();
let store = Store::new(&JIT::new(&wasmer_compiler_singlepass::Singlepass::new()).engine());
run_basic_dynamic_function(&store, "singlepass", c);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/api/src/externals/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct Memory {

impl Memory {
pub fn new(store: &Store, ty: MemoryType) -> Result<Memory, MemoryError> {
let tunables = store.engine().tunables();
let tunables = store.tunables();
let memory_plan = tunables.memory_plan(ty);
let memory = tunables.create_memory(memory_plan)?;

Expand Down
2 changes: 1 addition & 1 deletion lib/api/src/externals/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl Table {
/// All the elements in the table will be set to the `init` value.
pub fn new(store: &Store, ty: TableType, init: Val) -> Result<Table, RuntimeError> {
let item = init.into_checked_anyfunc(store)?;
let tunables = store.engine().tunables();
let tunables = store.tunables();
let table_plan = tunables.table_plan(ty);
let table = tunables
.create_table(table_plan)
Expand Down
12 changes: 5 additions & 7 deletions lib/api/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use thiserror::Error;
use wasmer_compiler::CompileError;
#[cfg(feature = "wat")]
use wasmer_compiler::WasmError;
use wasmer_engine::{Artifact, DeserializeError, Resolver, SerializeError};
use wasmer_engine::{Artifact, DeserializeError, Resolver, SerializeError, Tunables as _};
use wasmer_runtime::{ExportsIterator, ImportsIterator, InstanceHandle, ModuleInfo};

#[derive(Error, Debug)]
Expand Down Expand Up @@ -156,7 +156,7 @@ impl Module {
}

fn compile(store: &Store, binary: &[u8]) -> Result<Self, CompileError> {
let artifact = store.engine().compile(binary)?;
let artifact = store.engine().compile(binary, store.tunables())?;
Ok(Self::from_artifact(store, artifact))
}

Expand Down Expand Up @@ -261,11 +261,9 @@ impl Module {
resolver: &dyn Resolver,
) -> Result<InstanceHandle, InstantiationError> {
unsafe {
let instance_handle = self.artifact.instantiate(
self.store.engine().tunables(),
resolver,
Box::new(()),
)?;
let instance_handle =
self.artifact
.instantiate(self.store.tunables(), resolver, Box::new(()))?;

// After the instance handle is created, we need to initialize
// the data, call the start function and so. However, if any
Expand Down
25 changes: 22 additions & 3 deletions lib/api/src/store.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#[cfg(all(feature = "compiler", feature = "engine"))]
use crate::tunables::Tunables;
#[cfg(all(feature = "compiler", feature = "engine"))]
use wasmer_compiler::CompilerConfig;
use wasmer_engine::Tunables as BaseTunables;

use std::sync::Arc;
use wasmer_engine::Engine;

#[derive(Clone)]
pub struct Store {
engine: Arc<dyn Engine + Send + Sync>,
tunables: Arc<dyn BaseTunables + Send + Sync>,
}

impl Store {
Expand All @@ -18,9 +19,27 @@ impl Store {
{
Store {
engine: engine.cloned(),
tunables: Arc::new(Tunables::for_target(engine.target())),
}
}

pub fn new_with_tunables<E>(
engine: &E,
tunables: impl BaseTunables + Send + Sync + 'static,
) -> Store
where
E: Engine + ?Sized,
{
Store {
engine: engine.cloned(),
tunables: Arc::new(tunables),
}
}

pub fn tunables(&self) -> &dyn BaseTunables {
self.tunables.as_ref()
}

pub fn engine(&self) -> &Arc<dyn Engine + Send + Sync> {
&self.engine
}
Expand Down Expand Up @@ -63,11 +82,9 @@ impl Default for Store {
cfg_if::cfg_if! {
if #[cfg(feature = "default-jit")] {
wasmer_engine_jit::JIT::new(&config)
.tunables(Tunables::for_target)
.engine()
} else if #[cfg(feature = "default-native")] {
wasmer_engine_native::Native::new(&config)
.tunables(Tunables::for_target)
.engine()
} else {
compile_error!("No default engine chosen")
Expand All @@ -77,8 +94,10 @@ impl Default for Store {

let config = get_config();
let engine = get_engine(config);
let tunables = Tunables::for_target(engine.target());
Store {
engine: Arc::new(engine),
tunables: Arc::new(tunables),
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions lib/engine-jit/src/artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use wasmer_compiler::{CompileError, Features, Triple};
use wasmer_compiler::{CompileModuleInfo, ModuleEnvironment};
use wasmer_engine::{
register_frame_info, Artifact, DeserializeError, GlobalFrameInfoRegistration, SerializeError,
Tunables,
};
#[cfg(feature = "compiler")]
use wasmer_engine::{Engine, SerializableFunctionFrameInfo};
Expand All @@ -43,10 +44,13 @@ impl JITArtifact {

/// Compile a data buffer into a `JITArtifact`, which may then be instantiated.
#[cfg(feature = "compiler")]
pub fn new(jit: &JITEngine, data: &[u8]) -> Result<Self, CompileError> {
pub fn new(
jit: &JITEngine,
data: &[u8],
tunables: &dyn Tunables,
) -> Result<Self, CompileError> {
let environ = ModuleEnvironment::new();
let mut inner_jit = jit.inner_mut();
let tunables = jit.tunables();
let features = inner_jit.features();

let translation = environ.translate(data).map_err(CompileError::Wasm)?;
Expand Down Expand Up @@ -75,7 +79,7 @@ impl JITArtifact {

// Compile the Module
let compilation = compiler.compile_module(
&inner_jit.target(),
&jit.target(),
&compile_info,
translation.module_translation.as_ref().unwrap(),
translation.function_body_inputs,
Expand Down
26 changes: 2 additions & 24 deletions lib/engine-jit/src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use crate::JITEngine;
use std::sync::Arc;
use wasmer_compiler::{Compiler, CompilerConfig, Features, Target};
use wasmer_engine::Tunables;

/// The JIT builder
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>,
}
Expand All @@ -17,7 +15,6 @@ impl<'a> JIT<'a> {
Self {
compiler_config: Some(compiler_config),
target: None,
tunables_fn: None,
features: None,
}
}
Expand All @@ -27,7 +24,6 @@ impl<'a> JIT<'a> {
Self {
compiler_config: None,
target: None,
tunables_fn: None,
features: None,
}
}
Expand All @@ -38,20 +34,6 @@ impl<'a> JIT<'a> {
self
}

/// Set the tunables constructor function.
///
/// It should receive a [`Target`] and return a
pub fn tunables<F, T>(mut self, tunables_fn: F) -> Self
where
F: Fn(&Target) -> T + 'static,
T: Tunables + Send + Sync + 'static
{
self.tunables_fn = Some(Box::new(move |target: &Target| {
Box::new(tunables_fn(target))
}));
self
}

/// Set the features
pub fn features(mut self, features: Features) -> Self {
self.features = Some(features);
Expand All @@ -61,18 +43,14 @@ impl<'a> JIT<'a> {
/// Build the `JITEngine` for this configuration
pub fn engine(self) -> JITEngine {
let target = self.target.unwrap_or_default();
let tunables_fn = self
.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_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)
JITEngine::new(compiler, target, features)
} else {
JITEngine::headless(tunables)
JITEngine::headless()
}
}
}
39 changes: 15 additions & 24 deletions lib/engine-jit/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,24 @@ use wasmer_runtime::{
#[derive(Clone)]
pub struct JITEngine {
inner: Arc<Mutex<JITEngineInner>>,
tunables: Arc<dyn Tunables + Send + Sync>,
/// The target for the compiler
target: Arc<Target>,
engine_id: EngineId,
}

impl JITEngine {
/// Create a new `JITEngine` with the given config
#[cfg(feature = "compiler")]
pub fn new(
compiler: Box<dyn Compiler + Send>,
target: Target,
tunables: Arc<dyn Tunables + 'static + Send + Sync>,
features: Features,
) -> Self {
pub fn new(compiler: Box<dyn Compiler + Send>, target: Target, features: Features) -> Self {
Self {
inner: Arc::new(Mutex::new(JITEngineInner {
compiler: Some(compiler),
function_call_trampolines: HashMap::new(),
code_memory: CodeMemory::new(),
signatures: SignatureRegistry::new(),
features,
target: Some(target),
})),
tunables: tunables,
target: Arc::new(target),
engine_id: EngineId::default(),
}
}
Expand All @@ -61,7 +56,7 @@ impl JITEngine {
///
/// Headless engines can't compile or validate any modules,
/// they just take already processed Modules (via `Module::serialize`).
pub fn headless(tunables: Arc<dyn Tunables + 'static + Send + Sync>) -> Self {
pub fn headless() -> Self {
Self {
inner: Arc::new(Mutex::new(JITEngineInner {
#[cfg(feature = "compiler")]
Expand All @@ -70,9 +65,8 @@ impl JITEngine {
code_memory: CodeMemory::new(),
signatures: SignatureRegistry::new(),
features: Features::default(),
target: None,
})),
tunables,
target: Arc::new(Target::default()),
engine_id: EngineId::default(),
}
}
Expand All @@ -87,9 +81,9 @@ impl JITEngine {
}

impl Engine for JITEngine {
/// Get the tunables
fn tunables(&self) -> &dyn Tunables {
&*self.tunables
/// The target
fn target(&self) -> &Target {
&self.target
}

/// Register a signature
Expand All @@ -115,8 +109,12 @@ impl Engine for JITEngine {
}

/// Compile a WebAssembly binary
fn compile(&self, binary: &[u8]) -> Result<Arc<dyn Artifact>, CompileError> {
Ok(Arc::new(JITArtifact::new(&self, binary)?))
fn compile(
&self,
binary: &[u8],
tunables: &dyn Tunables,
) -> Result<Arc<dyn Artifact>, CompileError> {
Ok(Arc::new(JITArtifact::new(&self, binary, tunables)?))
}

/// Deserializes a WebAssembly module
Expand All @@ -142,8 +140,6 @@ pub struct JITEngineInner {
function_call_trampolines: HashMap<VMSharedSignatureIndex, VMTrampoline>,
/// The features to compile the Wasm module with
features: Features,
/// The target for the compiler
target: Option<Target>,
/// The code memory is responsible of publishing the compiled
/// functions to memory.
code_memory: CodeMemory,
Expand Down Expand Up @@ -177,11 +173,6 @@ impl JITEngineInner {
))
}

/// The target
pub fn target(&self) -> &Target {
&self.target.as_ref().unwrap()
}

/// The Wasm features
pub fn features(&self) -> &Features {
&self.features
Expand Down
Loading

0 comments on commit 5273fa0

Please sign in to comment.