Skip to content

Commit 853811f

Browse files
Ong-art IntelOng-art Intel
authored andcommitted
try use cranelift
1 parent d5c4b5b commit 853811f

File tree

2 files changed

+73
-7
lines changed

2 files changed

+73
-7
lines changed

packages/vm/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ assert_matches = "1.3.0"
1313
hex = "0.4"
1414
parity-wasm = "0.41"
1515
pwasm-utils = "0.12"
16-
wasmer = { version = "2.3.0", default-features = false, features = ["jit", "singlepass", "compiler", "universal"] }
16+
wasmer = { version = "2.3.0", default-features = false, features = ["jit", "singlepass", "compiler", "universal", "cranelift"] }
1717
wasmer-middlewares = "2.3.0"
1818
owasm-crypto = { path = "../crypto", version = "0.1.13" }
1919
tempfile = "3.1.0"

packages/vm/src/lib.rs

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ use wasmer_middlewares::metering::{get_remaining_points, MeteringPoints};
1515

1616
use pwasm_utils::{self, rules};
1717

18-
use wasmer::{
19-
imports, wasmparser, wasmparser::Operator, CompilerConfig, Function, Singlepass, Store, JIT,
20-
};
18+
#[cfg(feature = "cranelift")]
19+
use wasmer::Cranelift;
20+
21+
#[cfg(not(feature = "cranelift"))]
22+
use wasmer::Singlepass;
23+
24+
use wasmer::{imports, wasmparser, wasmparser::Operator, CompilerConfig, Function, Store, JIT};
2125
use wasmer_middlewares::Metering;
2226

2327
use owasm_crypto::ecvrf;
@@ -134,6 +138,21 @@ pub fn compile(code: &[u8]) -> Result<Vec<u8>, Error> {
134138
elements::serialize(module).map_err(|_| Error::SerializationError)
135139
}
136140

141+
pub fn new_compile(code: &[u8]) -> Result<Vec<u8>, Error> {
142+
// Check that the given Wasm code is indeed a valid Wasm.
143+
wasmparser::validate(code).map_err(|_| Error::ValidationError)?;
144+
145+
// Start the compiling chains.
146+
let module = elements::deserialize_buffer(code).map_err(|_| Error::DeserializationError)?;
147+
check_wasm_exports(&module)?;
148+
check_wasm_imports(&module)?;
149+
let module = inject_memory(module)?;
150+
let module = inject_stack_height(module)?;
151+
152+
// Serialize the final Wasm code back to bytes.
153+
elements::serialize(module).map_err(|_| Error::SerializationError)
154+
}
155+
137156
fn require_mem_range(max_range: usize, require_range: usize) -> Result<(), Error> {
138157
if max_range < require_range {
139158
return Err(Error::MemoryOutOfBoundError);
@@ -164,7 +183,12 @@ where
164183
{
165184
let owasm_env = Environment::new(env, gas);
166185

186+
#[cfg(feature = "cranelift")]
187+
let mut compiler = Cranelift::default();
188+
189+
#[cfg(not(feature = "cranelift"))]
167190
let mut compiler = Singlepass::default();
191+
168192
let metering = Arc::new(Metering::new(4294967290, cost));
169193
compiler.push_middleware(metering);
170194
let engine = Universal::new(compiler).engine();
@@ -173,9 +197,9 @@ where
173197
let import_object = imports! {
174198
"env" => {
175199
"gas" => Function::new_native_with_env(&store, owasm_env.clone(), |env: &Environment<E>, gas: u32| {
176-
env.with_mut_vm(|vm| {
177-
vm.consume_gas(gas)
178-
})
200+
// env.with_mut_vm(|vm| {
201+
// vm.consume_gas(gas)
202+
// })
179203
}),
180204
"get_span_size" => Function::new_native_with_env(&store, owasm_env.clone(), |env: &Environment<E>| {
181205
env.with_vm(|vm| {
@@ -338,6 +362,7 @@ mod test {
338362
use parity_wasm::elements;
339363
use std::io::{Read, Write};
340364
use std::process::Command;
365+
use std::time::Instant;
341366
use tempfile::NamedTempFile;
342367

343368
pub struct MockEnv {}
@@ -517,6 +542,47 @@ mod test {
517542
assert_eq!(gas_used, 1000015 as u32);
518543
}
519544

545+
#[test]
546+
fn test_run_time() {
547+
let wasm = wat2wasm(
548+
r#"(module
549+
(type (func (param i64 i64 i64 i64) (result)))
550+
(func
551+
(local $idx i32)
552+
(local.set $idx (i32.const 0))
553+
(block
554+
(loop
555+
(local.set $idx (local.get $idx) (i32.const 1) (i32.add) )
556+
(br_if 0 (i32.lt_u (local.get $idx) (i32.const 1000000)))
557+
)
558+
)
559+
)
560+
(func (;"execute": Resolves with result "beeb";)
561+
)
562+
(memory 17)
563+
(data (i32.const 1048576) "beeb") (;str = "beeb";)
564+
(export "prepare" (func 0))
565+
(export "execute" (func 1)))
566+
"#,
567+
);
568+
let code = compile(&wasm).unwrap();
569+
let new_code = new_compile(&wasm).unwrap();
570+
let mut cache = Cache::new(CacheOptions { cache_size: 10000 });
571+
let env = MockEnv {};
572+
573+
let now = Instant::now();
574+
let _gas_used = run(&mut cache, &code, 4294967290, true, env).unwrap();
575+
let elapsed_time = now.elapsed();
576+
577+
let env = MockEnv {};
578+
579+
let now = Instant::now();
580+
let _gas_used = run(&mut cache, &new_code, 4294967290, true, env).unwrap();
581+
let elapsed_time_2 = now.elapsed();
582+
583+
assert_eq!(elapsed_time.as_micros(), elapsed_time_2.as_micros());
584+
}
585+
520586
#[test]
521587
fn test_inject_memory_no_memory() {
522588
let wasm = wat2wasm("(module)");

0 commit comments

Comments
 (0)