From 720fffff5f8d648b2d17811f20a03b438a708569 Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Sat, 25 Nov 2023 11:33:20 +0100 Subject: [PATCH 1/3] minor optimization in executor --- crates/wasmi/src/engine/regmach/executor/instrs.rs | 12 +++++++++++- .../src/engine/regmach/executor/instrs/branch.rs | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/crates/wasmi/src/engine/regmach/executor/instrs.rs b/crates/wasmi/src/engine/regmach/executor/instrs.rs index b851abee36..26a409fa1b 100644 --- a/crates/wasmi/src/engine/regmach/executor/instrs.rs +++ b/crates/wasmi/src/engine/regmach/executor/instrs.rs @@ -15,7 +15,7 @@ use crate::{ Instruction, Register, RegisterSpan, - UnaryInstr, + UnaryInstr, BranchOffset16, }, code_map::{CodeMap, InstructionPtr}, stack::{CallFrame, CallStack, ValueStack, ValueStackPtr}, @@ -926,6 +926,16 @@ impl<'ctx, 'engine> Executor<'ctx, 'engine> { self.ip.offset(offset.to_i32() as isize) } + /// Branches and adjusts the value stack. + /// + /// # Note + /// + /// Offsets the instruction pointer using the given [`BranchOffset`]. + #[inline(always)] + fn branch_to16(&mut self, offset: BranchOffset16) { + self.ip.offset(offset.to_i16() as isize) + } + /// Returns the [`ValueStackPtr`] of the [`CallFrame`]. fn frame_stack_ptr(&mut self, frame: &CallFrame) -> ValueStackPtr { Self::frame_stack_ptr_impl(self.value_stack, frame) diff --git a/crates/wasmi/src/engine/regmach/executor/instrs/branch.rs b/crates/wasmi/src/engine/regmach/executor/instrs/branch.rs index 55644ffe2f..ad67d266bb 100644 --- a/crates/wasmi/src/engine/regmach/executor/instrs/branch.rs +++ b/crates/wasmi/src/engine/regmach/executor/instrs/branch.rs @@ -47,7 +47,7 @@ impl<'ctx, 'engine> Executor<'ctx, 'engine> { let lhs: T = self.get_register_as(instr.lhs); let rhs = T::from(instr.rhs); if f(lhs, rhs) { - return self.branch_to(instr.offset.into()); + return self.branch_to16(instr.offset); } self.next_instr() } From 276ca42f387e92ece6365b4053a56b04345c3b7d Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Sat, 25 Nov 2023 11:38:34 +0100 Subject: [PATCH 2/3] apply rustfmt --- crates/wasmi/src/engine/regmach/executor/instrs.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/wasmi/src/engine/regmach/executor/instrs.rs b/crates/wasmi/src/engine/regmach/executor/instrs.rs index 26a409fa1b..98a0fce405 100644 --- a/crates/wasmi/src/engine/regmach/executor/instrs.rs +++ b/crates/wasmi/src/engine/regmach/executor/instrs.rs @@ -11,11 +11,12 @@ use crate::{ AnyConst32, BinInstr, BinInstrImm16, + BranchOffset16, Const16, Instruction, Register, RegisterSpan, - UnaryInstr, BranchOffset16, + UnaryInstr, }, code_map::{CodeMap, InstructionPtr}, stack::{CallFrame, CallStack, ValueStack, ValueStackPtr}, From bab9588d0490e0b607f89d472c2e46ce5bfd242c Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Sat, 25 Nov 2023 11:49:36 +0100 Subject: [PATCH 3/3] move utility methods into submodule --- .../src/engine/regmach/executor/instrs.rs | 23 +-------------- .../engine/regmach/executor/instrs/branch.rs | 29 ++++++++++++++++++- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/crates/wasmi/src/engine/regmach/executor/instrs.rs b/crates/wasmi/src/engine/regmach/executor/instrs.rs index 98a0fce405..d7a5a4f5de 100644 --- a/crates/wasmi/src/engine/regmach/executor/instrs.rs +++ b/crates/wasmi/src/engine/regmach/executor/instrs.rs @@ -2,7 +2,7 @@ use self::{call::CallOutcome, return_::ReturnOutcome}; use crate::{ core::{TrapCode, UntypedValue}, engine::{ - bytecode::{BlockFuel, BranchOffset, FuncIdx}, + bytecode::{BlockFuel, FuncIdx}, cache::InstanceCache, config::FuelCosts, func_types::FuncTypeRegistry, @@ -11,7 +11,6 @@ use crate::{ AnyConst32, BinInstr, BinInstrImm16, - BranchOffset16, Const16, Instruction, Register, @@ -917,26 +916,6 @@ impl<'ctx, 'engine> Executor<'ctx, 'engine> { Ok(()) } - /// Branches and adjusts the value stack. - /// - /// # Note - /// - /// Offsets the instruction pointer using the given [`BranchOffset`]. - #[inline(always)] - fn branch_to(&mut self, offset: BranchOffset) { - self.ip.offset(offset.to_i32() as isize) - } - - /// Branches and adjusts the value stack. - /// - /// # Note - /// - /// Offsets the instruction pointer using the given [`BranchOffset`]. - #[inline(always)] - fn branch_to16(&mut self, offset: BranchOffset16) { - self.ip.offset(offset.to_i16() as isize) - } - /// Returns the [`ValueStackPtr`] of the [`CallFrame`]. fn frame_stack_ptr(&mut self, frame: &CallFrame) -> ValueStackPtr { Self::frame_stack_ptr_impl(self.value_stack, frame) diff --git a/crates/wasmi/src/engine/regmach/executor/instrs/branch.rs b/crates/wasmi/src/engine/regmach/executor/instrs/branch.rs index ad67d266bb..66c66d716b 100644 --- a/crates/wasmi/src/engine/regmach/executor/instrs/branch.rs +++ b/crates/wasmi/src/engine/regmach/executor/instrs/branch.rs @@ -1,7 +1,14 @@ use super::Executor; use crate::engine::{ bytecode::BranchOffset, - regmach::bytecode::{BranchBinOpInstr, BranchBinOpInstrImm16, Const16, Const32, Register}, + regmach::bytecode::{ + BranchBinOpInstr, + BranchBinOpInstrImm16, + BranchOffset16, + Const16, + Const32, + Register, + }, }; use core::cmp; use wasmi_core::UntypedValue; @@ -10,6 +17,26 @@ use wasmi_core::UntypedValue; use crate::engine::regmach::bytecode::Instruction; impl<'ctx, 'engine> Executor<'ctx, 'engine> { + /// Branches and adjusts the value stack. + /// + /// # Note + /// + /// Offsets the instruction pointer using the given [`BranchOffset`]. + #[inline(always)] + fn branch_to(&mut self, offset: BranchOffset) { + self.ip.offset(offset.to_i32() as isize) + } + + /// Branches and adjusts the value stack. + /// + /// # Note + /// + /// Offsets the instruction pointer using the given [`BranchOffset`]. + #[inline(always)] + fn branch_to16(&mut self, offset: BranchOffset16) { + self.ip.offset(offset.to_i16() as isize) + } + #[inline(always)] pub fn execute_branch(&mut self, offset: BranchOffset) { self.branch_to(offset)