Skip to content

Commit 303abc7

Browse files
chore: refactor interpreter loop
Signed-off-by: Henry <mail@henrygressmann.de>
1 parent 3ebed33 commit 303abc7

File tree

7 files changed

+650
-690
lines changed

7 files changed

+650
-690
lines changed

crates/tinywasm/benches/tinywasm_modes.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,6 @@ fn criterion_benchmark(c: &mut Criterion) {
5858
let mut group = c.benchmark_group("tinywasm_modes");
5959
group.measurement_time(BENCH_MEASUREMENT_TIME);
6060

61-
group.bench_function("call", |b| {
62-
b.iter_batched_ref(
63-
|| setup_typed_func(module.clone(), None).expect("setup call"),
64-
|(store, func)| run_call(store, func).expect("run call"),
65-
BatchSize::LargeInput,
66-
)
67-
});
68-
6961
let per_instruction_engine = Engine::new(Config::new().fuel_policy(FuelPolicy::PerInstruction));
7062
group.bench_function("resume_fuel_per_instruction", |b| {
7163
b.iter_batched_ref(
@@ -95,6 +87,14 @@ fn criterion_benchmark(c: &mut Criterion) {
9587
)
9688
});
9789

90+
group.bench_function("call", |b| {
91+
b.iter_batched_ref(
92+
|| setup_typed_func(module.clone(), None).expect("setup call"),
93+
|(store, func)| run_call(store, func).expect("run call"),
94+
BatchSize::LargeInput,
95+
)
96+
});
97+
9898
group.finish();
9999
}
100100

crates/tinywasm/src/error.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use alloc::string::{String, ToString};
22
use alloc::vec::Vec;
33
use core::fmt::Debug;
4-
use core::{fmt::Display, ops::ControlFlow};
4+
use core::fmt::Display;
55
use tinywasm_types::FuncType;
66
use tinywasm_types::archive::TwasmError;
77

@@ -274,17 +274,3 @@ impl From<tinywasm_parser::ParseError> for Error {
274274

275275
/// A wrapper around [`core::result::Result`] for tinywasm operations
276276
pub type Result<T, E = Error> = crate::std::result::Result<T, E>;
277-
278-
pub(crate) trait Controlify<T> {
279-
fn to_cf(self) -> ControlFlow<Option<Error>, T>;
280-
}
281-
282-
impl<T> Controlify<T> for Result<T, Error> {
283-
#[inline(always)]
284-
fn to_cf(self) -> ControlFlow<Option<Error>, T> {
285-
match self {
286-
Ok(value) => ControlFlow::Continue(value),
287-
Err(err) => ControlFlow::Break(Some(err)),
288-
}
289-
}
290-
}

crates/tinywasm/src/interpreter/executor.rs

Lines changed: 627 additions & 649 deletions
Large diffs are not rendered by default.

crates/tinywasm/src/interpreter/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl InterpreterRuntime {
3838
pub(crate) fn exec_with_time_budget(
3939
store: &mut Store,
4040
cf: CallFrame,
41-
time_budget: crate::std::time::Duration,
41+
time_budget: core::time::Duration,
4242
) -> Result<ExecState> {
4343
executor::Executor::<false>::new(store, cf)?.run_with_time_budget(time_budget)
4444
}

crates/tinywasm/src/interpreter/num_helpers.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,16 @@ macro_rules! checked_conv_float {
3131
};
3232
// Conversion with an intermediate unsigned type and error checking (three types)
3333
($from:tt, $intermediate:tt, $to:tt, $self:expr) => {
34-
$self
35-
.store
36-
.stack
37-
.values
38-
.unary_into::<$from, $to>(|v| {
39-
let (min, max) = float_min_max!($from, $intermediate);
40-
if unlikely(v.is_nan()) {
41-
return Err(Error::Trap(crate::Trap::InvalidConversionToInt));
42-
}
43-
if unlikely(v <= min || v >= max) {
44-
return Err(Error::Trap(crate::Trap::IntegerOverflow));
45-
}
46-
Ok((v as $intermediate as $to).into())
47-
})
48-
.to_cf()?
34+
$self.store.stack.values.unary_into::<$from, $to>(|v| {
35+
let (min, max) = float_min_max!($from, $intermediate);
36+
if unlikely(v.is_nan()) {
37+
return Err(Error::Trap(crate::Trap::InvalidConversionToInt));
38+
}
39+
if unlikely(v <= min || v >= max) {
40+
return Err(Error::Trap(crate::Trap::IntegerOverflow));
41+
}
42+
Ok((v as $intermediate as $to).into())
43+
})?
4944
};
5045
}
5146

crates/tinywasm/tests/host_func_signature_check.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ fn test_linking_invalid_typed_func() -> Result<()> {
100100
let mut store = Store::default();
101101
let mut imports = Imports::new();
102102
imports.define("host", "hfn", typed_fn).unwrap();
103-
module.clone().instantiate(&mut store, Some(imports))?;
103+
let link_failure = module.clone().instantiate(&mut store, Some(imports));
104+
assert!(link_failure.is_err(), "Expected linking to fail for mismatched typed func, but it succeeded");
104105
}
105106
}
106107

examples/rust/build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ out_dir="./target/wasm32-unknown-unknown/wasm"
77
dest_dir="out"
88

99
rust_features="+simd128,+reference-types,+bulk-memory,+mutable-globals,+multivalue,+sign-ext,+nontrapping-fptoint"
10-
wasmopt_features="--enable-simd --enable-reference-types --enable-bulk-memory --enable-mutable-globals --enable-multivalue --enable-sign-ext --enable-nontrapping-float-to-int --duplicate-function-elimination"
10+
wasmopt_features="--enable-simd --enable-tail-call --enable-extended-const --enable-reference-types --enable-bulk-memory --enable-mutable-globals --enable-multivalue --enable-sign-ext --enable-nontrapping-float-to-int --duplicate-function-elimination"
1111

1212
# ensure out dir exists
1313
mkdir -p "$dest_dir"

0 commit comments

Comments
 (0)