Skip to content

Commit

Permalink
Try #1099:
Browse files Browse the repository at this point in the history
  • Loading branch information
bors[bot] authored Dec 21, 2019
2 parents 7390372 + 52975a0 commit 51c3c93
Show file tree
Hide file tree
Showing 20 changed files with 136 additions and 195 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## **[Unreleased]**

- [#1099](https://github.com/wasmerio/wasmer/pull/1099) Remove `backend::Backend` from `wasmer_runtime_core`
- [#1097](https://github.com/wasmerio/wasmer/pull/1097) Move inline breakpoint outside of runtime backend
- [#1095](https://github.com/wasmerio/wasmer/pull/1095) Update to cranelift 0.52.
- [#1092](https://github.com/wasmerio/wasmer/pull/1092) Add `get_utf8_string_with_nul` to `WasmPtr` to read nul-terminated strings from memory.
Expand Down
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,16 @@ extra-debug = ["wasmer-clif-backend/debug", "wasmer-runtime-core/debug"]
fast-tests = []
backend-cranelift = [
"wasmer-clif-backend",
"wasmer-runtime-core/backend-cranelift",
"wasmer-runtime/cranelift",
"wasmer-middleware-common-tests/clif",
]
backend-llvm = [
"wasmer-llvm-backend",
"wasmer-runtime-core/backend-llvm",
"wasmer-runtime/llvm",
"wasmer-middleware-common-tests/llvm",
]
backend-singlepass = [
"wasmer-singlepass-backend",
"wasmer-runtime-core/backend-singlepass",
"wasmer-runtime/singlepass",
"wasmer-middleware-common-tests/singlepass",
]
Expand Down
31 changes: 1 addition & 30 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -222,38 +222,9 @@ check: check-bench
# as default, and test a minimal set of features with only one backend
# at a time.
cargo check --manifest-path lib/runtime/Cargo.toml
# Check some of the cases where deterministic execution could matter
cargo check --manifest-path lib/runtime/Cargo.toml --features "deterministic-execution"
cargo check --manifest-path lib/runtime/Cargo.toml --no-default-features \
--features=default-backend-singlepass,deterministic-execution
cargo check --manifest-path lib/runtime/Cargo.toml --no-default-features \
--features=default-backend-llvm,deterministic-execution
cargo check --release --manifest-path lib/runtime/Cargo.toml

$(RUNTIME_CHECK) \
--features=cranelift,cache,debug,llvm,singlepass,default-backend-singlepass
$(RUNTIME_CHECK) --release \
--features=cranelift,cache,llvm,singlepass,default-backend-singlepass
$(RUNTIME_CHECK) \
--features=cranelift,cache,debug,llvm,singlepass,default-backend-cranelift
$(RUNTIME_CHECK) --release \
--features=cranelift,cache,llvm,singlepass,default-backend-cranelift
$(RUNTIME_CHECK) \
--features=cranelift,cache,debug,llvm,singlepass,default-backend-llvm
$(RUNTIME_CHECK) --release \
--features=cranelift,cache,llvm,singlepass,default-backend-llvm
$(RUNTIME_CHECK) \
--features=singlepass,default-backend-singlepass,debug
$(RUNTIME_CHECK) --release \
--features=singlepass,default-backend-singlepass
$(RUNTIME_CHECK) \
--features=cranelift,default-backend-cranelift,debug
$(RUNTIME_CHECK) --release \
--features=cranelift,default-backend-cranelift
$(RUNTIME_CHECK) \
--features=llvm,default-backend-llvm,debug
$(RUNTIME_CHECK) --release \
--features=llvm,default-backend-llvm
--features=singlepass,cranelift,llvm,cache,debug,deterministic-execution

# Release
release:
Expand Down
6 changes: 3 additions & 3 deletions lib/clif-backend/src/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::mem;
use std::sync::{Arc, RwLock};
use wasmer_runtime_core::error::CompileError;
use wasmer_runtime_core::{
backend::{Backend, CacheGen, Token},
backend::{CacheGen, Token},
cache::{Artifact, Error as CacheError},
codegen::*,
memory::MemoryType,
Expand Down Expand Up @@ -58,8 +58,8 @@ impl ModuleCodeGenerator<CraneliftFunctionCodeGenerator, Caller, CodegenError>
unimplemented!("cross compilation is not available for clif backend")
}

fn backend_id() -> Backend {
Backend::Cranelift
fn backend_id() -> String {
"cranelift".to_string()
}

fn check_precondition(&mut self, _module_info: &ModuleInfo) -> Result<(), CodegenError> {
Expand Down
6 changes: 3 additions & 3 deletions lib/llvm-backend/src/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use std::{
};

use wasmer_runtime_core::{
backend::{Backend, CacheGen, CompilerConfig, Token},
backend::{CacheGen, CompilerConfig, Token},
cache::{Artifact, Error as CacheError},
codegen::*,
memory::MemoryType,
Expand Down Expand Up @@ -8721,8 +8721,8 @@ impl<'ctx> ModuleCodeGenerator<LLVMFunctionCodeGenerator<'ctx>, LLVMBackend, Cod
}
}

fn backend_id() -> Backend {
Backend::LLVM
fn backend_id() -> String {
"llvm".to_string()
}

fn check_precondition(&mut self, _module_info: &ModuleInfo) -> Result<(), CodegenError> {
Expand Down
17 changes: 7 additions & 10 deletions lib/middleware-common-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,38 @@ mod tests {
use wasmer_runtime_core::codegen::{MiddlewareChain, StreamingCompiler};
use wasmer_runtime_core::fault::{pop_code_version, push_code_version};
use wasmer_runtime_core::state::CodeVersion;
use wasmer_runtime_core::{
backend::{Backend, Compiler},
compile_with, imports, Func,
};
use wasmer_runtime_core::{backend::Compiler, compile_with, imports, Func};

#[cfg(feature = "llvm")]
fn get_compiler(limit: u64) -> (impl Compiler, Backend) {
fn get_compiler(limit: u64) -> impl Compiler {
use wasmer_llvm_backend::ModuleCodeGenerator as LLVMMCG;
let c: StreamingCompiler<LLVMMCG, _, _, _, _> = StreamingCompiler::new(move || {
let mut chain = MiddlewareChain::new();
chain.push(Metering::new(limit));
chain
});
(c, Backend::LLVM)
c
}

#[cfg(feature = "singlepass")]
fn get_compiler(limit: u64) -> (impl Compiler, Backend) {
fn get_compiler(limit: u64) -> impl Compiler {
use wasmer_singlepass_backend::ModuleCodeGenerator as SinglePassMCG;
let c: StreamingCompiler<SinglePassMCG, _, _, _, _> = StreamingCompiler::new(move || {
let mut chain = MiddlewareChain::new();
chain.push(Metering::new(limit));
chain
});
(c, Backend::Singlepass)
c
}

#[cfg(not(any(feature = "llvm", feature = "clif", feature = "singlepass")))]
compile_error!("compiler not specified, activate a compiler via features");

#[cfg(feature = "clif")]
fn get_compiler(_limit: u64) -> (impl Compiler, Backend) {
fn get_compiler(_limit: u64) -> impl Compiler {
compile_error!("cranelift does not implement metering");
use wasmer_clif_backend::CraneliftCompiler;
(CraneliftCompiler::new(), Backend::Cranelift)
CraneliftCompiler::new()
}

// Assemblyscript
Expand Down
2 changes: 1 addition & 1 deletion lib/runtime-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ cc = "1.0"
[features]
debug = []
trace = ["debug"]
# backend flags used in conditional compilation of Backend::variants
# backend flags no longer used
"backend-cranelift" = []
"backend-singlepass" = []
"backend-llvm" = []
Expand Down
70 changes: 0 additions & 70 deletions lib/runtime-core/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,60 +22,6 @@ pub mod sys {
}
pub use crate::sig_registry::SigRegistry;

/// Enum used to select which compiler should be used to generate code.
#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq)]
pub enum Backend {
Cranelift,
Singlepass,
LLVM,
Auto,
}

impl Backend {
/// Get a list of the currently enabled (via feature flag) backends.
pub fn variants() -> &'static [&'static str] {
&[
#[cfg(feature = "backend-cranelift")]
"cranelift",
#[cfg(feature = "backend-singlepass")]
"singlepass",
#[cfg(feature = "backend-llvm")]
"llvm",
"auto",
]
}

/// Stable string representation of the backend.
/// It can be used as part of a cache key, for example.
pub fn to_string(&self) -> &'static str {
match self {
Backend::Cranelift => "cranelift",
Backend::Singlepass => "singlepass",
Backend::LLVM => "llvm",
Backend::Auto => "auto",
}
}
}

impl Default for Backend {
fn default() -> Self {
Backend::Cranelift
}
}

impl std::str::FromStr for Backend {
type Err = String;
fn from_str(s: &str) -> Result<Backend, String> {
match s.to_lowercase().as_str() {
"singlepass" => Ok(Backend::Singlepass),
"cranelift" => Ok(Backend::Cranelift),
"llvm" => Ok(Backend::LLVM),
"auto" => Ok(Backend::Auto),
_ => Err(format!("The backend {} doesn't exist", s)),
}
}
}

/// The target architecture for code generation.
#[derive(Copy, Clone, Debug)]
pub enum Architecture {
Expand Down Expand Up @@ -104,22 +50,6 @@ pub struct InlineBreakpoint {
pub ty: InlineBreakpointType,
}

#[cfg(test)]
mod backend_test {
use super::*;
use std::str::FromStr;

#[test]
fn str_repr_matches() {
// if this test breaks, think hard about why it's breaking
// can we avoid having these be different?

for &backend in &[Backend::Cranelift, Backend::LLVM, Backend::Singlepass] {
assert_eq!(backend, Backend::from_str(backend.to_string()).unwrap());
}
}
}

/// This type cannot be constructed from
/// outside the runtime crate.
pub struct Token {
Expand Down
28 changes: 3 additions & 25 deletions lib/runtime-core/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@
//! serializing compiled wasm code to a binary format. The binary format can be persisted,
//! and loaded to allow skipping compilation and fast startup.
use crate::{
backend::Backend,
module::{Module, ModuleInfo},
sys::Memory,
};
use crate::{module::ModuleInfo, sys::Memory};
use blake2b_simd::blake2bp;
use std::{fmt, io, mem, slice};
use std::{io, mem, slice};

/// Indicates the invalid type of invalid cache file
#[derive(Debug)]
Expand All @@ -35,7 +31,7 @@ pub enum Error {
/// The cached binary has been invalidated.
InvalidatedCache,
/// The current backend does not support caching.
UnsupportedBackend(Backend),
UnsupportedBackend(String),
}

impl From<io::Error> for Error {
Expand Down Expand Up @@ -246,24 +242,6 @@ impl Artifact {
}
}

/// A generic cache for storing and loading compiled wasm modules.
///
/// The `wasmer-runtime` supplies a naive `FileSystemCache` api.
pub trait Cache {
/// Error type to return when load error occurs
type LoadError: fmt::Debug;
/// Error type to return when store error occurs
type StoreError: fmt::Debug;

/// loads a module using the default `Backend`
fn load(&self, key: WasmHash) -> Result<Module, Self::LoadError>;
/// loads a cached module using a specific `Backend`
fn load_with_backend(&self, key: WasmHash, backend: Backend)
-> Result<Module, Self::LoadError>;
/// Store a module into the cache with the given key
fn store(&mut self, key: WasmHash, module: Module) -> Result<(), Self::StoreError>;
}

/// A unique ID generated from the version of Wasmer for use with cache versioning
pub const WASMER_VERSION_HASH: &'static str =
include_str!(concat!(env!("OUT_DIR"), "/wasmer_version_hash.txt"));
32 changes: 11 additions & 21 deletions lib/runtime-core/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use crate::fault::FaultInfo;
use crate::{
backend::RunnableModule,
backend::{Backend, CacheGen, Compiler, CompilerConfig, Features, Token},
backend::{CacheGen, Compiler, CompilerConfig, Features, Token},
cache::{Artifact, Error as CacheError},
error::{CompileError, CompileResult},
module::{ModuleInfo, ModuleInner},
Expand Down Expand Up @@ -92,7 +92,12 @@ pub trait ModuleCodeGenerator<FCG: FunctionCodeGenerator<E>, RM: RunnableModule,
) -> Self;

/// Returns the backend id associated with this MCG.
fn backend_id() -> Backend;
fn backend_id() -> String;

/// It sets if the current compiler requires validation before compilation
fn requires_pre_validation() -> bool {
true
}

/// Feeds the compiler config.
fn feed_compiler_config(&mut self, _config: &CompilerConfig) -> Result<(), E> {
Expand Down Expand Up @@ -222,26 +227,20 @@ impl<
compiler_config: CompilerConfig,
_: Token,
) -> CompileResult<ModuleInner> {
if requires_pre_validation(MCG::backend_id()) {
if MCG::requires_pre_validation() {
validate_with_features(wasm, &compiler_config.features)?;
}

let mut mcg = match MCG::backend_id() {
Backend::LLVM => MCG::new_with_target(
let mut mcg = match MCG::backend_id().as_ref() {
"llvm" => MCG::new_with_target(
compiler_config.triple.clone(),
compiler_config.cpu_name.clone(),
compiler_config.cpu_features.clone(),
),
_ => MCG::new(),
};
let mut chain = (self.middleware_chain_generator)();
let info = crate::parse::read_module(
wasm,
MCG::backend_id(),
&mut mcg,
&mut chain,
&compiler_config,
)?;
let info = crate::parse::read_module(wasm, &mut mcg, &mut chain, &compiler_config)?;
let (exec_context, cache_gen) =
mcg.finalize(&info.read().unwrap())
.map_err(|x| CompileError::InternalError {
Expand All @@ -263,15 +262,6 @@ impl<
}
}

fn requires_pre_validation(backend: Backend) -> bool {
match backend {
Backend::Cranelift => true,
Backend::LLVM => true,
Backend::Singlepass => false,
Backend::Auto => false,
}
}

/// A sink for parse events.
pub struct EventSink<'a, 'b> {
buffer: SmallVec<[Event<'a, 'b>; 2]>,
Expand Down
4 changes: 2 additions & 2 deletions lib/runtime-core/src/module.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! The module module contains the implementation data structures and helper functions used to
//! manipulate and access wasm modules.
use crate::{
backend::{Backend, RunnableModule},
backend::RunnableModule,
cache::{Artifact, Error as CacheError},
error,
import::ImportObject,
Expand Down Expand Up @@ -65,7 +65,7 @@ pub struct ModuleInfo {
/// Map signature index to function signature.
pub signatures: Map<SigIndex, FuncSig>,
/// Backend.
pub backend: Backend,
pub backend: String,

/// Table of namespace indexes.
pub namespace_table: StringTable<NamespaceIndex>,
Expand Down
Loading

0 comments on commit 51c3c93

Please sign in to comment.