Skip to content

Commit

Permalink
use explicit targets and target cache
Browse files Browse the repository at this point in the history
  • Loading branch information
tsahee committed Jun 24, 2024
1 parent 4f52b17 commit 9e99245
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 10 deletions.
12 changes: 10 additions & 2 deletions arbitrator/jit/src/stylus_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use std::{
thread::JoinHandle,
};
use stylus::{native::NativeInstance, run::RunProgram};
use wasmer::Target;

struct MessageToCothread {
result: Vec<u8>,
Expand Down Expand Up @@ -143,8 +144,15 @@ pub fn exec_wasm(

let evm_api = EvmApiRequestor::new(cothread);

let mut instance =
unsafe { NativeInstance::deserialize(&module, compile.clone(), evm_api, evm_data) }?;
let mut instance = unsafe {
NativeInstance::deserialize(
&module,
compile.clone(),
evm_api,
evm_data,
Target::default(),
)
}?;

let thread = thread::spawn(move || {
let outcome = instance.run_main(&calldata, config, ink);
Expand Down
13 changes: 12 additions & 1 deletion arbitrator/stylus/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ macro_rules! cache {
pub struct InitCache {
long_term: HashMap<CacheKey, CacheItem>,
lru: LruCache<CacheKey, CacheItem>,
target: Target,
}

#[derive(Clone, Copy, Hash, PartialEq, Eq)]
Expand Down Expand Up @@ -68,6 +69,7 @@ impl InitCache {
Self {
long_term: HashMap::new(),
lru: LruCache::new(NonZeroUsize::new(size).unwrap()),
target: Target::default(),
}
}

Expand All @@ -77,6 +79,14 @@ impl InitCache {
.resize(NonZeroUsize::new(size.try_into().unwrap()).unwrap())
}

pub fn set_target(target: Target) {
cache!().target = target;
}

pub fn target() -> Target {
cache!().target.clone()
}

/// Retrieves a cached value, updating items as necessary.
pub fn get(module_hash: Bytes32, version: u16, debug: bool) -> Option<(Module, Store)> {
let mut cache = cache!();
Expand Down Expand Up @@ -118,9 +128,10 @@ impl InitCache {
}
return Ok(item.data());
}
let target = cache.target.clone();
drop(cache);

let engine = CompileConfig::version(version, debug).engine(Target::default());
let engine = CompileConfig::version(version, debug).engine(target);
let module = unsafe { Module::deserialize_unchecked(&engine, module)? };

let item = CacheItem::new(module, engine);
Expand Down
42 changes: 41 additions & 1 deletion arbitrator/stylus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use native::NativeInstance;
use prover::programs::{prelude::*, StylusData};
use run::RunProgram;
use std::{marker::PhantomData, mem, ptr};
use target_cache::{target_cache_get, target_cache_set};

pub use brotli;
pub use prover;
Expand All @@ -29,6 +30,7 @@ pub mod run;

mod cache;
mod evm_api;
mod target_cache;
mod util;

#[cfg(test)]
Expand Down Expand Up @@ -176,12 +178,18 @@ pub unsafe extern "C" fn stylus_compile(
wasm: GoSliceData,
version: u16,
debug: bool,
name: GoSliceData,
output: *mut RustBytes,
) -> UserOutcomeKind {
let wasm = wasm.slice();
let output = &mut *output;
let name = String::from_utf8_unchecked(name.slice().to_vec());
let target = match target_cache_get(&name) {
Ok(val) => val,
Err(err) => return output.write_err(err),
};

let asm = match native::compile(wasm, version, debug) {
let asm = match native::compile(wasm, version, debug, target) {
Ok(val) => val,
Err(err) => return output.write_err(err),
};
Expand All @@ -190,6 +198,38 @@ pub unsafe extern "C" fn stylus_compile(
UserOutcomeKind::Success
}

/// sets target index to a string
///
/// String format is: Triple+CpuFeature+CpuFeature..
///
/// # Safety
///
/// `output` must not be null.
#[no_mangle]
pub unsafe extern "C" fn stylus_target_set(
name: GoSliceData,
description: GoSliceData,
output: *mut RustBytes,
native: bool,
) -> UserOutcomeKind {
let output = &mut *output;
let name = match String::from_utf8(name.slice().to_vec()) {
Ok(val) => val,
Err(err) => return output.write_err(err.into()),
};

let desc_str = match String::from_utf8(description.slice().to_vec()) {
Ok(val) => val,
Err(err) => return output.write_err(err.into()),
};

if let Err(err) = target_cache_set(name, desc_str, native) {
return output.write_err(err);
};

UserOutcomeKind::Success
}

/// Calls an activated user program.
///
/// # Safety
Expand Down
10 changes: 6 additions & 4 deletions arbitrator/stylus/src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ impl<D: DataReader, E: EvmApi<D>> NativeInstance<D, E> {
compile: CompileConfig,
evm: E,
evm_data: EvmData,
target: Target,
) -> Result<Self> {
let env = WasmEnv::new(compile, None, evm, evm_data);
let store = env.compile.store(Target::default());
let store = env.compile.store(target);
let module = unsafe { Module::deserialize_unchecked(&store, module)? };
Self::from_module(module, store, env)
}
Expand Down Expand Up @@ -137,9 +138,10 @@ impl<D: DataReader, E: EvmApi<D>> NativeInstance<D, E> {
evm_data: EvmData,
compile: &CompileConfig,
config: StylusConfig,
target: Target,
) -> Result<Self> {
let env = WasmEnv::new(compile.clone(), Some(config), evm_api, evm_data);
let store = env.compile.store(Target::default());
let store = env.compile.store(target);
let wat_or_wasm = std::fs::read(path)?;
let module = Module::new(&store, wat_or_wasm)?;
Self::from_module(module, store, env)
Expand Down Expand Up @@ -448,9 +450,9 @@ pub fn activate(
Ok((module, stylus_data))
}

pub fn compile(wasm: &[u8], version: u16, debug: bool) -> Result<Vec<u8>> {
pub fn compile(wasm: &[u8], version: u16, debug: bool, target: Target) -> Result<Vec<u8>> {
let compile = CompileConfig::version(version, debug);
let asm = match self::module(wasm, compile, Target::default()) {
let asm = match self::module(wasm, compile, target) {
Ok(asm) => asm,
Err(err) => util::panic_with_wasm(wasm, err),
};
Expand Down
11 changes: 9 additions & 2 deletions arbitrator/stylus/src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use rand::prelude::*;
use std::{collections::HashMap, path::Path, sync::Arc};
use wasmer::{
imports, wasmparser::Operator, CompilerConfig, Function, FunctionEnv, Imports, Instance,
Module, Store,
Module, Store, Target,
};
use wasmer_compiler_singlepass::Singlepass;

Expand Down Expand Up @@ -86,7 +86,14 @@ impl TestInstance {
config: StylusConfig,
) -> Result<(Self, TestEvmApi)> {
let (mut evm, evm_data) = TestEvmApi::new(compile.clone());
let native = Self::from_path(path, evm.clone(), evm_data, compile, config)?;
let native = Self::from_path(
path,
evm.clone(),
evm_data,
compile,
config,
Target::default(),
)?;
let footprint = native.memory().ty(&native.store).minimum.0 as u16;
evm.set_pages(footprint);
Ok((native, evm))
Expand Down
1 change: 1 addition & 0 deletions arbos/programs/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func activateProgramInternal(
goSlice(wasm),
u16(version),
cbool(debug),
goSlice([]byte{}),
output,
))

Expand Down

0 comments on commit 9e99245

Please sign in to comment.