Skip to content

Commit

Permalink
Implement minor branch optimization in executor (#806)
Browse files Browse the repository at this point in the history
* minor optimization in executor

* apply rustfmt

* move utility methods into submodule
  • Loading branch information
Robbepop authored Nov 25, 2023
1 parent 834c3fb commit 80e1d21
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
12 changes: 1 addition & 11 deletions crates/wasmi/src/engine/regmach/executor/instrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -916,16 +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)
}

/// Returns the [`ValueStackPtr`] of the [`CallFrame`].
fn frame_stack_ptr(&mut self, frame: &CallFrame) -> ValueStackPtr {
Self::frame_stack_ptr_impl(self.value_stack, frame)
Expand Down
31 changes: 29 additions & 2 deletions crates/wasmi/src/engine/regmach/executor/instrs/branch.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -47,7 +74,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()
}
Expand Down

0 comments on commit 80e1d21

Please sign in to comment.