Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize float-to-int Wasm trunc instructions #439

Merged
merged 4 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wasmi_core"
version = "0.3.0"
version = "0.4.0"
edition = "2021"
authors = ["Parity Technologies <admin@parity.io>"]
license = "MIT/Apache-2.0"
Expand All @@ -13,7 +13,6 @@ keywords = ["wasm", "webassembly", "bytecode", "interpreter"]
[dependencies]
memory_units = "0.4.0"
libm = "0.2.1"
num-rational = { version = "0.4", default-features = false, features = ["num-bigint"] }
num-traits = { version = "0.2.8", default-features = false }
downcast-rs = { version = "1.2", default-features = false }

Expand All @@ -24,8 +23,6 @@ rand = "0.8.2"
default = ["std"]
# Use `no-default-features` for a `no_std` build.
std = [
"num-rational/std",
"num-rational/num-bigint-std",
"num-traits/std",
"downcast-rs/std",
]
27 changes: 13 additions & 14 deletions core/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,18 +500,17 @@ impl WrapInto<F32> for F64 {
}

macro_rules! impl_try_truncate_into {
(@primitive $from: ident, $into: ident, $to_primitive:path) => {
(@primitive $from: ident, $into: ident, $to_primitive:path, $rmin:literal, $rmax:literal) => {
impl TryTruncateInto<$into, TrapCode> for $from {
#[inline]
fn try_truncate_into(self) -> Result<$into, TrapCode> {
// Casting from a float to an integer will round the float towards zero
if self.is_nan() {
return Err(TrapCode::InvalidConversionToInt);
}
num_rational::BigRational::from_float(self)
.map(|val| val.to_integer())
.and_then(|val| $to_primitive(&val))
.ok_or(TrapCode::IntegerOverflow)
if self <= $rmin || self >= $rmax {
return Err(TrapCode::IntegerOverflow);
}
Ok(self as _)
}
}

Expand Down Expand Up @@ -548,14 +547,14 @@ macro_rules! impl_try_truncate_into {
};
}

impl_try_truncate_into!(@primitive f32, i32, num_traits::cast::ToPrimitive::to_i32);
impl_try_truncate_into!(@primitive f32, i64, num_traits::cast::ToPrimitive::to_i64);
impl_try_truncate_into!(@primitive f64, i32, num_traits::cast::ToPrimitive::to_i32);
impl_try_truncate_into!(@primitive f64, i64, num_traits::cast::ToPrimitive::to_i64);
impl_try_truncate_into!(@primitive f32, u32, num_traits::cast::ToPrimitive::to_u32);
impl_try_truncate_into!(@primitive f32, u64, num_traits::cast::ToPrimitive::to_u64);
impl_try_truncate_into!(@primitive f64, u32, num_traits::cast::ToPrimitive::to_u32);
impl_try_truncate_into!(@primitive f64, u64, num_traits::cast::ToPrimitive::to_u64);
impl_try_truncate_into!(@primitive f32, i32, num_traits::cast::ToPrimitive::to_i32, -2147483904.0_f32, 2147483648.0_f32);
impl_try_truncate_into!(@primitive f32, u32, num_traits::cast::ToPrimitive::to_u32, -1.0_f32, 4294967296.0_f32);
impl_try_truncate_into!(@primitive f64, i32, num_traits::cast::ToPrimitive::to_i32, -2147483649.0_f64, 2147483648.0_f64);
impl_try_truncate_into!(@primitive f64, u32, num_traits::cast::ToPrimitive::to_u32, -1.0_f64, 4294967296.0_f64);
impl_try_truncate_into!(@primitive f32, i64, num_traits::cast::ToPrimitive::to_i64, -9223373136366403584.0_f32, 9223372036854775808.0_f32);
impl_try_truncate_into!(@primitive f32, u64, num_traits::cast::ToPrimitive::to_u64, -1.0_f32, 18446744073709551616.0_f32);
impl_try_truncate_into!(@primitive f64, i64, num_traits::cast::ToPrimitive::to_i64, -9223372036854777856.0_f64, 9223372036854775808.0_f64);
impl_try_truncate_into!(@primitive f64, u64, num_traits::cast::ToPrimitive::to_u64, -1.0_f64, 18446744073709551616.0_f64);
impl_try_truncate_into!(@wrapped F32, f32, i32);
impl_try_truncate_into!(@wrapped F32, f32, i64);
impl_try_truncate_into!(@wrapped F64, f64, i32);
Expand Down
2 changes: 1 addition & 1 deletion wasmi_v1/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ exclude = [ "tests/*", "benches/*" ]

[dependencies]
wasmparser = { version = "0.90", package = "wasmparser-nostd", default-features = false }
wasmi_core = { version = "0.3", path = "../core", default-features = false }
wasmi_core = { version = "0.4", path = "../core", default-features = false }
spin = { version = "0.9", default-features = false, features = ["mutex", "spin_mutex"] }

[dev-dependencies]
Expand Down
20 changes: 20 additions & 0 deletions wasmi_v1/benches/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use criterion::{criterion_group, criterion_main, Bencher, Criterion};
use std::{slice, time::Duration};
use wasmi as v1;
use wasmi::core::Value;
use wasmi_core::{F32, F64};

const WASM_KERNEL: &str =
"benches/wasm/wasm_kernel/target/wasm32-unknown-unknown/release/wasm_kernel.wasm";
Expand Down Expand Up @@ -46,6 +47,7 @@ criterion_group! {
bench_execute_rev_comp_v1,
bench_execute_regex_redux_v1,
bench_execute_count_until_v1,
bench_execute_trunc_f2i,
bench_execute_global_bump,
bench_execute_fac_recursive_v1,
bench_execute_fac_opt_v1,
Expand Down Expand Up @@ -254,6 +256,24 @@ fn bench_execute_count_until_v1(c: &mut Criterion) {
});
}

fn bench_execute_trunc_f2i(c: &mut Criterion) {
const ITERATIONS: i32 = 25_000;
c.bench_function("execute/trunc_f2i/v1", |b| {
let (mut store, instance) = load_instance_from_wat_v1(include_bytes!("wat/trunc_f2i.wat"));
let count_until = instance
.get_export(&store, "trunc_f2i")
.and_then(v1::Extern::into_func)
.unwrap();
let count_until = count_until.typed::<(i32, F32, F64), (), _>(&store).unwrap();

b.iter(|| {
count_until
.call(&mut store, (ITERATIONS, F32::from(42.0), F64::from(69.0)))
.unwrap();
})
});
}

fn bench_execute_global_bump(c: &mut Criterion) {
const BUMP_AMOUNT: i32 = 100_000;
c.bench_function("execute/global_bump/v1", |b| {
Expand Down
45 changes: 45 additions & 0 deletions wasmi_v1/benches/wat/trunc_f2i.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
(module
(func (export "trunc_f2i") (param $n i32) (param $input32 f32) (param $input64 f64) (result)
(local $i i32)
(block $exit
(if
(i32.le_u
(local.get $n)
(i32.const 0)
)
(unreachable) ;; trap if $n <= 0
)
(local.set $i (local.get $n)) ;; i = n
(loop $continue
(drop
(i32.trunc_f32_s (local.get $input32)) ;; <- under test
)
(drop
(i32.trunc_f32_u (local.get $input32)) ;; <- under test
)
(drop
(i64.trunc_f32_s (local.get $input32)) ;; <- under test
)
(drop
(i64.trunc_f64_u (local.get $input64)) ;; <- under test
)
(drop
(i32.trunc_f64_s (local.get $input64)) ;; <- under test
)
(drop
(i32.trunc_f64_u (local.get $input64)) ;; <- under test
)
(drop
(i64.trunc_f64_s (local.get $input64)) ;; <- under test
)
(drop
(i64.trunc_f64_u (local.get $input64)) ;; <- under test
)
(local.set $i ;; i -= 1
(i32.sub (local.get $i) (i32.const 1))
)
(br_if $continue (local.get $i)) ;; continue if i != 0
)
)
)
)