Skip to content

Commit

Permalink
wasmi_v1: improve instruction scheduler (#376)
Browse files Browse the repository at this point in the history
* rename inst_ptr to pc (program counter)

* rename FunctionExecutionOutcome to CallOutcome

* rename inst_context -> exec_ctx

* add ResolvedFuncBody::get_release_unchecked

* implement new instruction scheduler for engine v1

* clean-up codebase and remove no longer used code

* some minor clean-ups

* use core::hint::unreachable instead of unreachable!() in --release mode

This is used in the interpreter hot path and results in roughly 5-15% performance improvement across the board for nearly all execution benchmarks.

* fix docs

* inline pc into execution context

This shows performance improvements for most benchmarks in the range of 10-20%!

* remove commented out code
  • Loading branch information
Robbepop authored Jul 13, 2022
1 parent 38a0712 commit f5d8f7c
Show file tree
Hide file tree
Showing 7 changed files with 523 additions and 775 deletions.
6 changes: 1 addition & 5 deletions wasmi_v1/src/engine/bytecode/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
//! The instruction architecture of the `wasmi` interpreter.

mod utils;
mod visitor;

#[cfg(test)]
mod tests;

pub use self::{
utils::{BrTable, DropKeep, FuncIdx, GlobalIdx, LocalIdx, Offset, SignatureIdx, Target},
visitor::VisitInstruction,
};
pub use self::utils::{DropKeep, FuncIdx, GlobalIdx, LocalIdx, Offset, SignatureIdx, Target};
use wasmi_core::UntypedValue;

/// The internal `wasmi` bytecode that is stored for Wasm functions.
Expand Down
50 changes: 1 addition & 49 deletions wasmi_v1/src/engine/bytecode/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::{super::super::engine::InstructionIdx, Instruction};
use core::cmp;
use super::super::super::engine::InstructionIdx;

/// Defines how many stack values are going to be dropped and kept after branching.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -191,50 +190,3 @@ impl Offset {
self.0
}
}

/// A reference to a `wasmi` bytecode `br_table`.
#[derive(Debug)]
pub struct BrTable<'a> {
/// The branches of the `wasmi` bytecode `br_table` including the default target.
///
/// # Note
///
/// All elements of this slice are of variant [`Instruction::Br`] or [`Instruction::Return`].
branches: &'a [Instruction],
}

impl<'a> BrTable<'a> {
/// Creates a new reference to a `wasmi` bytecode `br_table`.
///
/// # Note
///
/// The `targets` slice must contain the default target at its end.
///
/// # Panics (Debug Mode)
///
/// If the `targets` slice does not represent a `wasmi` bytecode `br_table`.
pub fn new(branches: &'a [Instruction]) -> Self {
assert!(
!branches.is_empty(),
"the targets slice must not be empty since the \
default target must be included at least",
);
debug_assert!(
branches
.iter()
.all(|inst| matches!(inst, Instruction::Br(_) | Instruction::Return(_))),
"the branches slice contains non `br` or `return` instructions: {:?}",
branches,
);
Self { branches }
}

/// Returns the branch at the given `index` if any or the default target.
pub fn branch_or_default(&self, index: usize) -> &Instruction {
// The index of the default target which is the last target of the slice.
let max_index = self.branches.len() - 1;
// A normalized index will always yield a target without panicking.
let normalized_index = cmp::min(index, max_index);
&self.branches[normalized_index]
}
}
185 changes: 0 additions & 185 deletions wasmi_v1/src/engine/bytecode/visitor.rs

This file was deleted.

18 changes: 14 additions & 4 deletions wasmi_v1/src/engine/call_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ pub struct FunctionFrame {
/// calling functions using the default table and avoids one indirection
/// to look-up the table in the `Instance`.
default_table: Option<Table>,
/// The current value of the instruction pointer.
/// The current value of the program counter.
///
/// # Note
///
/// The instruction pointer always points to the instruction
/// The program counter always points to the instruction
/// that is going to executed next.
pub inst_ptr: usize,
pc: usize,
}

impl FunctionFrame {
Expand All @@ -89,6 +89,16 @@ impl FunctionFrame {
}
}

/// Returns the program counter.
pub(super) fn pc(&self) -> usize {
self.pc
}

/// Updates the program counter.
pub(super) fn update_pc(&mut self, new_pc: usize) {
self.pc = new_pc;
}

/// Creates a new [`FunctionFrame`] from the given Wasm function entity.
pub(super) fn new_wasm(func: Func, wasm_func: &WasmFuncEntity) -> Self {
let instance = wasm_func.instance();
Expand All @@ -100,7 +110,7 @@ impl FunctionFrame {
instance,
default_memory: None,
default_table: None,
inst_ptr: 0,
pc: 0,
}
}

Expand Down
Loading

0 comments on commit f5d8f7c

Please sign in to comment.