Skip to content
Merged
4 changes: 2 additions & 2 deletions lib/api/src/backend/js/entities/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use wasmer_types::{target::Target, Features};
pub struct Engine;

impl Engine {
pub(crate) fn deterministic_id(&self) -> &str {
pub(crate) fn deterministic_id(&self) -> String {
// All js engines have the same id
"js-generic"
String::from("js-generic")
}

/// Returns the WebAssembly features supported by the JS engine.
Expand Down
4 changes: 2 additions & 2 deletions lib/api/src/backend/jsc/entities/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,9 @@ impl JSCEngine {
}

impl Engine {
pub(crate) fn deterministic_id(&self) -> &str {
pub(crate) fn deterministic_id(&self) -> String {
// All js engines have the same id
"javascriptcore"
String::from("javascriptcore")
}

/// Returns the WebAssembly features supported by the JSC engine for the given target.
Expand Down
4 changes: 2 additions & 2 deletions lib/api/src/backend/v8/entities/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ impl Engine {
Self::default()
}

pub(crate) fn deterministic_id(&self) -> &str {
"v8"
pub(crate) fn deterministic_id(&self) -> String {
String::from("v8")
}

/// Returns the WebAssembly features supported by the V8 engine.
Expand Down
4 changes: 2 additions & 2 deletions lib/api/src/backend/wamr/entities/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ impl Engine {
Self::default()
}

pub(crate) fn deterministic_id(&self) -> &str {
"wamr"
pub(crate) fn deterministic_id(&self) -> String {
String::from("wamr")
}

/// Returns the WebAssembly features supported by the WAMR engine.
Expand Down
4 changes: 2 additions & 2 deletions lib/api/src/backend/wasmi/entities/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ impl Engine {
Self::default()
}

pub(crate) fn deterministic_id(&self) -> &str {
"wasmi"
pub(crate) fn deterministic_id(&self) -> String {
String::from("wasmi")
}

/// Returns the WebAssembly features supported by the WASMI engine.
Expand Down
2 changes: 1 addition & 1 deletion lib/api/src/entities/engine/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ gen_rt_ty!(Engine @derives Debug, Clone);
impl BackendEngine {
/// Returns the deterministic id of this engine.
#[inline]
pub fn deterministic_id(&self) -> &str {
pub fn deterministic_id(&self) -> String {
match_rt!(on self => s {
s.deterministic_id()
})
Expand Down
25 changes: 23 additions & 2 deletions lib/api/src/entities/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

use bytes::Bytes;
use std::{path::Path, sync::Arc};
use wasmer_types::{target::Target, DeserializeError, Features};
use wasmer_types::{
target::{Target, UserCompilerOptimizations},
CompileError, DeserializeError, Features,
};

#[cfg(feature = "sys")]
use wasmer_compiler::Artifact;
Expand Down Expand Up @@ -51,7 +54,7 @@ impl Engine {
}

/// Returns the deterministic id of this engine.
pub fn deterministic_id(&self) -> &str {
pub fn deterministic_id(&self) -> String {
self.be.deterministic_id()
}

Expand Down Expand Up @@ -223,4 +226,22 @@ impl Engine {
) -> Result<Arc<Artifact>, DeserializeError> {
self.be.deserialize_from_file_unchecked(file_ref)
}

/// Add suggested optimizations to this engine.
///
/// # Note
///
/// Not every backend supports every optimization. This function may fail (i.e. not set the
/// suggested optimizations) silently if the underlying engine backend does not support one or
/// more optimizations.
pub fn with_opts(
&mut self,
suggested_opts: &UserCompilerOptimizations,
) -> Result<(), CompileError> {
match self.be {
#[cfg(feature = "sys")]
BackendEngine::Sys(ref mut e) => e.with_opts(suggested_opts),
_ => Ok(()),
}
}
}
1 change: 1 addition & 0 deletions lib/cli/src/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ async fn construct_manifest(
map.insert("wasi".to_string(), "0.1.0-unstable".to_string());
map
}),
annotations: None,
}];

let mut pkg = wasmer_config::package::Package::builder(
Expand Down
5 changes: 5 additions & 0 deletions lib/compiler-cranelift/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ use wasmer_types::{

/// A compiler that compiles a WebAssembly module with Cranelift, translating the Wasm to Cranelift IR,
/// optimizing it and then translating to assembly.
#[derive(Debug)]
pub struct CraneliftCompiler {
config: Cranelift,
}
Expand All @@ -70,6 +71,10 @@ impl Compiler for CraneliftCompiler {
"cranelift"
}

fn deterministic_id(&self) -> String {
String::from("cranelift")
}

/// Get the middlewares for this compiler
fn get_middlewares(&self) -> &[Arc<dyn ModuleMiddleware>] {
&self.config.middlewares
Expand Down
29 changes: 29 additions & 0 deletions lib/compiler-llvm/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use wasmer_vm::LibCall;

/// A compiler that compiles a WebAssembly module with LLVM, translating the Wasm to LLVM IR,
/// optimizing it and then translating to assembly.
#[derive(Debug)]
pub struct LLVMCompiler {
config: LLVM,
}
Expand Down Expand Up @@ -261,6 +262,24 @@ impl Compiler for LLVMCompiler {
"llvm"
}

fn deterministic_id(&self) -> String {
let mut ret = format!(
"llvm-{}",
match self.config.opt_level {
inkwell::OptimizationLevel::None => "opt0",
inkwell::OptimizationLevel::Less => "optl",
inkwell::OptimizationLevel::Default => "optd",
inkwell::OptimizationLevel::Aggressive => "opta",
}
);

if self.config.enable_g0m0_opt {
ret.push_str("-g0m0");
}

ret
}

/// Get the middlewares for this compiler
fn get_middlewares(&self) -> &[Arc<dyn ModuleMiddleware>] {
&self.config.middlewares
Expand Down Expand Up @@ -538,4 +557,14 @@ impl Compiler for LLVMCompiler {
got,
})
}

fn with_opts(
&mut self,
suggested_compiler_opts: &wasmer_types::target::UserCompilerOptimizations,
) -> Result<(), CompileError> {
if suggested_compiler_opts.pass_params.is_some_and(|v| v) {
self.config.enable_g0m0_opt = true;
}
Ok(())
}
}
5 changes: 5 additions & 0 deletions lib/compiler-singlepass/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use wasmer_types::{

/// A compiler that compiles a WebAssembly module with Singlepass.
/// It does the compilation in one pass
#[derive(Debug)]
pub struct SinglepassCompiler {
config: Singlepass,
}
Expand All @@ -59,6 +60,10 @@ impl Compiler for SinglepassCompiler {
"singlepass"
}

fn deterministic_id(&self) -> String {
String::from("singlepass")
}

/// Get the middlewares for this compiler
fn get_middlewares(&self) -> &[Arc<dyn ModuleMiddleware>] {
&self.config.middlewares
Expand Down
23 changes: 21 additions & 2 deletions lib/compiler/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use enumset::EnumSet;
use wasmer_types::{
entity::PrimaryMap,
error::CompileError,
target::{CpuFeature, Target},
target::{CpuFeature, Target, UserCompilerOptimizations},
Features, LocalFunctionIndex,
};
#[cfg(feature = "translator")]
Expand Down Expand Up @@ -75,12 +75,31 @@ where
}

/// An implementation of a Compiler from parsed WebAssembly module to Compiled native code.
pub trait Compiler: Send {
pub trait Compiler: Send + std::fmt::Debug {
/// Returns a descriptive name for this compiler.
///
/// Note that this is an API breaking change since 3.0
fn name(&self) -> &str;

/// Returns the deterministic id of this compiler. Same compilers with different
/// optimizations map to different deterministic IDs.
fn deterministic_id(&self) -> String;

/// Add suggested optimizations to this compiler.
///
/// # Note
///
/// Not every compiler supports every optimization. This function may fail (i.e. not set the
/// suggested optimizations) silently if the underlying compiler does not support one or
/// more optimizations.
fn with_opts(
&mut self,
suggested_compiler_opts: &UserCompilerOptimizations,
) -> Result<(), CompileError> {
_ = suggested_compiler_opts;
Ok(())
}

/// Validates a module.
///
/// It returns the a succesful Result in case is valid, `CompileError` in case is not.
Expand Down
62 changes: 56 additions & 6 deletions lib/compiler/src/engine/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,21 @@ impl Engine {
}

/// Returns the deterministic id of this engine
pub fn deterministic_id(&self) -> &str {
// TODO: add a `deterministic_id` to the Compiler, so two
// compilers can actually serialize into a different deterministic_id
// if their configuration is different (eg. LLVM with optimizations vs LLVM
// without optimizations)
self.name.as_str()
pub fn deterministic_id(&self) -> String {
let i = self.inner();
#[cfg(feature = "compiler")]
{
if let Some(ref c) = i.compiler {
return c.deterministic_id();
} else {
return self.name.clone();
}
}

#[allow(unreachable_code)]
{
self.name.to_string()
}
}

/// Create a headless `Engine`
Expand Down Expand Up @@ -283,11 +292,34 @@ impl Engine {
pub fn tunables(&self) -> &dyn Tunables {
self.tunables.as_ref()
}

/// Add suggested optimizations to this engine.
///
/// # Note
///
/// Not every backend supports every optimization. This function may fail (i.e. not set the
/// suggested optimizations) silently if the underlying engine backend does not support one or
/// more optimizations.
pub fn with_opts(
&mut self,
suggested_opts: &wasmer_types::target::UserCompilerOptimizations,
) -> Result<(), CompileError> {
#[cfg(feature = "compiler")]
{
let mut i = self.inner_mut();
if let Some(ref mut c) = i.compiler {
c.with_opts(suggested_opts)?;
}
}

Ok(())
}
}

impl std::fmt::Debug for Engine {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Engine")
.field("inner", &self.inner)
.field("target", &self.target)
.field("engine_id", &self.engine_id)
.field("name", &self.name)
Expand All @@ -313,6 +345,24 @@ pub struct EngineInner {
signatures: SignatureRegistry,
}

impl std::fmt::Debug for EngineInner {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let mut formatter = f.debug_struct("EngineInner");
#[cfg(feature = "compiler")]
{
formatter.field("compiler", &self.compiler);
formatter.field("features", &self.features);
}

#[cfg(not(target_arch = "wasm32"))]
{
formatter.field("signatures", &self.signatures);
}

formatter.finish()
}
}

impl EngineInner {
/// Gets the compiler associated to this engine.
#[cfg(feature = "compiler")]
Expand Down
22 changes: 22 additions & 0 deletions lib/config/src/package/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,26 @@ pub struct Module {
/// Interface definitions that can be used to generate bindings to this
/// module.
pub bindings: Option<Bindings>,
/// Miscellaneous annotations from the user.
#[serde(skip_serializing_if = "Option::is_none")]
pub annotations: Option<UserAnnotations>,
}

/// Miscellaneous annotations specified by the user.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize, Default)]
pub struct UserAnnotations {
pub suggested_compiler_optimizations: SuggestedCompilerOptimizations,
}

/// Suggested optimization that might be operated on the module when (and if) compiled.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize, Default)]
pub struct SuggestedCompilerOptimizations {
pub pass_params: Option<bool>,
}

impl SuggestedCompilerOptimizations {
pub const KEY: &'static str = "suggested_compiler_optimizations";
pub const PASS_PARAMS_KEY: &'static str = "pass_params";
}

/// The interface exposed by a [`Module`].
Expand Down Expand Up @@ -1007,6 +1027,7 @@ mod tests {
interfaces: None,
kind: Some("https://webc.org/kind/wasi".to_string()),
source: Path::new("test.wasm").to_path_buf(),
annotations: None,
}],
commands: Vec::new(),
fs: vec![
Expand Down Expand Up @@ -1062,6 +1083,7 @@ module = "mod"
wit_exports: PathBuf::from("exports.wit"),
wit_bindgen: "0.0.0".parse().unwrap()
})),
annotations: None
},
);
}
Expand Down
Loading
Loading