Skip to content

Commit

Permalink
Remove Engine trait
Browse files Browse the repository at this point in the history
  • Loading branch information
epilys committed Jul 25, 2022
1 parent 187d2e6 commit 987a90c
Show file tree
Hide file tree
Showing 15 changed files with 171 additions and 225 deletions.
20 changes: 10 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ SHELL=/usr/bin/env bash

# The matrix is the product of the following columns:
#
# |------------|-----------|----------|--------------|-------|
# | Compiler ⨯ Engine ⨯ Platform ⨯ Architecture ⨯ libc |
# |------------|-----------|----------|--------------|-------|
# | Cranelift | Universal | Linux | amd64 | glibc |
# | LLVM | | Darwin | aarch64 | musl |
# | Singlepass | | Windows | | |
# |------------|-----------|----------|--------------|-------|
# |------------|----------|--------------|-------|
# | Compiler ⨯ Platform ⨯ Architecture ⨯ libc |
# |------------|----------|--------------|-------|
# | Cranelift | Linux | amd64 | glibc |
# | LLVM | Darwin | aarch64 | musl |
# | Singlepass | Windows | | |
# |------------|----------|--------------|-------|
#
# Here is what works and what doesn't:
#
# * Cranelift with the Universal engine works everywhere,
# * Cranelift works everywhere,
#
# * LLVM with the Universal engine works on Linux+Darwin/`amd64`,
# * LLVM works on Linux+Darwin/`amd64`,
# but it doesn't work on */`aarch64` or Windows/*.
#
# * Singlepass with the Universal engine works on Linux+Darwin/`amd64`, but
# * Singlepass works on Linux+Darwin/`amd64`, but
# it doesn't work on */`aarch64` or Windows/*.
#
# * Windows isn't tested on `aarch64`, that's why we consider it's not
Expand Down
6 changes: 3 additions & 3 deletions fuzz/fuzz_targets/deterministic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use libfuzzer_sys::{arbitrary, arbitrary::Arbitrary, fuzz_target};
use wasm_smith::{Config, ConfiguredModule};
use wasmer::{CompilerConfig, Engine, Module, Store};
use wasmer::{CompilerConfig, Module, Store, UniversalEngine};
use wasmer_compiler::Universal;
use wasmer_compiler_cranelift::Cranelift;
use wasmer_compiler_llvm::LLVM;
Expand All @@ -23,8 +23,8 @@ impl Config for NoImportsConfig {
}
}

fn compile_and_compare(name: &str, engine: impl Engine, wasm: &[u8]) {
let mut store = Store::new_with_engine(&engine);
fn compile_and_compare(name: &str, engine: UniversalEngine, wasm: &[u8]) {
let store = Store::new_with_engine(&engine);

// compile for first time
let module = Module::new(&store, wasm).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion lib/api/src/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub use target_lexicon::{Architecture, CallingConvention, OperatingSystem, Tripl
pub use wasmer_compiler::{
wasmparser, CompilerConfig, FunctionMiddleware, MiddlewareReaderState, ModuleMiddleware,
};
pub use wasmer_compiler::{Engine, Features, FrameInfo, LinkError, RuntimeError, Tunables};
pub use wasmer_compiler::{Features, FrameInfo, LinkError, RuntimeError, Tunables};
pub use wasmer_derive::ValueType;
pub use wasmer_types::is_wasm;
pub use wasmer_types::{
Expand Down
65 changes: 49 additions & 16 deletions lib/api/src/sys/store.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use crate::sys::tunables::BaseTunables;
use std::fmt;
use std::sync::Arc;
use std::sync::{Arc, RwLock};
#[cfg(feature = "compiler")]
use wasmer_compiler::CompilerConfig;
#[cfg(feature = "compiler")]
use wasmer_compiler::Universal;
use wasmer_compiler::{Engine, Tunables};
use wasmer_vm::{init_traps, TrapHandlerFn};
use wasmer_compiler::{Tunables, Universal, UniversalEngine};
use wasmer_vm::{init_traps, TrapHandler, TrapHandlerFn};

use wasmer_vm::StoreObjects;

Expand All @@ -15,7 +14,7 @@ use wasmer_vm::StoreObjects;
/// wrap the actual context in a box.
pub(crate) struct StoreInner {
pub(crate) objects: StoreObjects,
pub(crate) engine: Arc<dyn Engine + Send + Sync>,
pub(crate) engine: Arc<UniversalEngine>,
pub(crate) tunables: Box<dyn Tunables + Send + Sync>,
pub(crate) trap_handler: Option<Box<TrapHandlerFn<'static>>>,
}
Expand All @@ -32,6 +31,8 @@ pub(crate) struct StoreInner {
/// Spec: <https://webassembly.github.io/spec/core/exec/runtime.html#store>
pub struct Store {
pub(crate) inner: Box<StoreInner>,
engine: Arc<UniversalEngine>,
trap_handler: Arc<RwLock<Option<Box<TrapHandlerFn<'static>>>>>,
}

impl Store {
Expand All @@ -43,10 +44,7 @@ impl Store {
}

/// Creates a new `Store` with a specific [`Engine`].
pub fn new_with_engine<E>(engine: &E) -> Self
where
E: Engine + ?Sized,
{
pub fn new_with_engine(engine: &UniversalEngine) -> Self {
Self::new_with_tunables(engine, BaseTunables::for_target(engine.target()))
}

Expand All @@ -56,10 +54,10 @@ impl Store {
}

/// Creates a new `Store` with a specific [`Engine`] and [`Tunables`].
pub fn new_with_tunables<E>(engine: &E, tunables: impl Tunables + Send + Sync + 'static) -> Self
where
E: Engine + ?Sized,
{
pub fn new_with_tunables(
engine: &UniversalEngine,
tunables: impl Tunables + Send + Sync + 'static,
) -> Self {
// Make sure the signal handlers are installed.
// This is required for handling traps.
init_traps();
Expand All @@ -71,6 +69,41 @@ impl Store {
tunables: Box::new(tunables),
trap_handler: None,
}),
engine: engine.cloned(),
trap_handler: Arc::new(RwLock::new(None)),
}
}

/// Returns the [`Tunables`].
pub fn tunables(&self) -> &dyn Tunables {
self.inner.tunables.as_ref()
}

/// Returns the [`Engine`].
pub fn engine(&self) -> &Arc<UniversalEngine> {
&self.engine
}

/// Checks whether two stores are identical. A store is considered
/// equal to another store if both have the same engine. The
/// tunables are excluded from the logic.
pub fn same(a: &Self, b: &Self) -> bool {
a.engine.id() == b.engine.id()
}
}

impl PartialEq for Store {
fn eq(&self, other: &Self) -> bool {
Self::same(self, other)
}
}

unsafe impl TrapHandler for Store {
fn custom_trap_handler(&self, call: &dyn Fn(&TrapHandlerFn) -> bool) -> bool {
if let Some(handler) = self.trap_handler.read().unwrap().as_ref() {
call(handler)
} else {
false
}
}
}
Expand Down Expand Up @@ -109,7 +142,7 @@ impl Default for Store {
}

#[allow(unreachable_code, unused_mut)]
fn get_engine(mut config: impl CompilerConfig + 'static) -> impl Engine + Send + Sync {
fn get_engine(mut config: impl CompilerConfig + 'static) -> UniversalEngine {
cfg_if::cfg_if! {
if #[cfg(feature = "default-universal")] {
wasmer_compiler::Universal::new(config)
Expand Down Expand Up @@ -165,7 +198,7 @@ impl<'a> StoreRef<'a> {
}

/// Returns the [`Engine`].
pub fn engine(&self) -> &Arc<dyn Engine + Send + Sync> {
pub fn engine(&self) -> &Arc<UniversalEngine> {
&self.inner.engine
}

Expand Down Expand Up @@ -198,7 +231,7 @@ impl<'a> StoreMut<'a> {
}

/// Returns the [`Engine`].
pub fn engine(&self) -> &Arc<dyn Engine + Send + Sync> {
pub fn engine(&self) -> &Arc<UniversalEngine> {
&self.inner.engine
}

Expand Down
32 changes: 12 additions & 20 deletions lib/c-api/src/wasm_c_api/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ use super::unstable::target_lexicon::wasmer_target_t;
use crate::error::update_last_error;
use cfg_if::cfg_if;
use std::sync::Arc;
use wasmer_api::Engine;
#[cfg(feature = "universal")]
use wasmer_compiler::Universal;
use wasmer_compiler::{Universal, UniversalEngine};

/// Kind of compilers that can be used by the engines.
///
Expand Down Expand Up @@ -263,7 +262,7 @@ pub extern "C" fn wasm_config_set_engine(config: &mut wasm_config_t, engine: was
/// cbindgen:ignore
#[repr(C)]
pub struct wasm_engine_t {
pub(crate) inner: Arc<dyn Engine + Send + Sync>,
pub(crate) inner: Arc<UniversalEngine>,
}

#[cfg(feature = "compiler")]
Expand Down Expand Up @@ -296,7 +295,7 @@ cfg_if! {
#[no_mangle]
pub extern "C" fn wasm_engine_new() -> Box<wasm_engine_t> {
let compiler_config: Box<dyn CompilerConfig> = get_default_compiler_config();
let engine: Arc<dyn Engine + Send + Sync> = Arc::new(Universal::new(compiler_config).engine());
let engine: Arc<UniversalEngine> = Arc::new(Universal::new(compiler_config).engine());
Box::new(wasm_engine_t { inner: engine })
}
} else if #[cfg(feature = "universal")] {
Expand All @@ -309,7 +308,7 @@ cfg_if! {
/// cbindgen:ignore
#[no_mangle]
pub extern "C" fn wasm_engine_new() -> Box<wasm_engine_t> {
let engine: Arc<dyn Engine + Send + Sync> = Arc::new(Universal::headless().engine());
let engine: Arc<UniversalEngine> = Arc::new(Universal::headless().engine());
Box::new(wasm_engine_t { inner: engine })
}
} else {
Expand Down Expand Up @@ -419,10 +418,11 @@ pub extern "C" fn wasm_engine_new_with_config(
compiler_config.canonicalize_nans(true);
}

let inner: Arc<dyn Engine + Send + Sync> = match config.engine {
wasmer_engine_t::UNIVERSAL => {
cfg_if! {
if #[cfg(feature = "universal")] {
#[cfg(not(feature = "universal"))]
return return_with_error("Wasmer has not been compiled with the `universal` feature.");
#[cfg(feature = "universal")]
let inner: Arc<UniversalEngine> =
{
let mut builder = Universal::new(compiler_config);

if let Some(target) = config.target {
Expand All @@ -434,16 +434,10 @@ pub extern "C" fn wasm_engine_new_with_config(
}

Arc::new(builder.engine())
} else {
return return_with_error("Wasmer has not been compiled with the `universal` feature.");
}
}
},
};
};
Some(Box::new(wasm_engine_t { inner }))
} else {
let inner: Arc<dyn Engine + Send + Sync> = match config.engine {
wasmer_engine_t::UNIVERSAL => {
let inner: Arc<UniversalEngine> =
cfg_if! {
if #[cfg(feature = "universal")] {
let mut builder = Universal::headless();
Expand All @@ -460,9 +454,7 @@ pub extern "C" fn wasm_engine_new_with_config(
} else {
return return_with_error("Wasmer has not been compiled with the `universal` feature.");
}
}
},
};
};
Some(Box::new(wasm_engine_t { inner }))
}
}
Expand Down
19 changes: 5 additions & 14 deletions lib/cli-compiler/src/commands/compile.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::store::{EngineType, StoreOptions};
use crate::store::StoreOptions;
use crate::warning;
use anyhow::{Context, Result};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -39,15 +39,8 @@ impl Compile {
.context(format!("failed to compile `{}`", self.path.display()))
}

pub(crate) fn get_recommend_extension(
engine_type: &EngineType,
target_triple: &Triple,
) -> Result<&'static str> {
Ok(match engine_type {
EngineType::Universal => {
wasmer_compiler::UniversalArtifactBuild::get_default_extension(target_triple)
}
})
pub(crate) fn get_recommend_extension(target_triple: &Triple) -> Result<&'static str> {
Ok(wasmer_compiler::UniversalArtifactBuild::get_default_extension(target_triple))
}

fn inner_execute(&self) -> Result<()> {
Expand All @@ -66,14 +59,13 @@ impl Compile {
Target::new(target_triple.clone(), features)
})
.unwrap_or_default();
let (mut engine, engine_type, compiler_type) =
self.store.get_engine_for_target(target.clone())?;
let (mut engine, compiler_type) = self.store.get_engine_for_target(target.clone())?;
let output_filename = self
.output
.file_stem()
.map(|osstr| osstr.to_string_lossy().to_string())
.unwrap_or_default();
let recommended_extension = Self::get_recommend_extension(&engine_type, target.triple())?;
let recommended_extension = Self::get_recommend_extension(target.triple())?;
match self.output.extension() {
Some(ext) => {
if ext != recommended_extension {
Expand All @@ -86,7 +78,6 @@ impl Compile {
}
let tunables = self.store.get_tunables_for_target(&target)?;

println!("Engine: {}", engine_type.to_string());
println!("Compiler: {}", compiler_type.to_string());
println!("Target: {}", target.triple());

Expand Down
2 changes: 1 addition & 1 deletion lib/cli-compiler/src/commands/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Validate {
Triple::from_str("x86_64-linux-gnu").unwrap(),
CpuFeature::SSE2 | CpuFeature::AVX,
);
let (engine, _engine_type, _compiler_type) = self.store.get_engine_for_target(target)?;
let (engine, _compiler_type) = self.store.get_engine_for_target(target)?;
let module_contents = std::fs::read(&self.path)?;
if !is_wasm(&module_contents) {
bail!("`wasmer validate` only validates WebAssembly files");
Expand Down
Loading

0 comments on commit 987a90c

Please sign in to comment.