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

Implement re-designed Wasmi call_indirect instructions #1156

Merged
merged 1 commit into from
Aug 30, 2024
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
33 changes: 33 additions & 0 deletions crates/wasmi/src/engine/bytecode/construct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1007,13 +1007,27 @@ impl Instruction {
}
}

/// Creates a new [`Instruction::ReturnCallIndirect0Imm16`] for the given `func`.
pub fn return_call_indirect_0_imm16(func_type: impl Into<SignatureIdx>) -> Self {
Self::ReturnCallIndirect0Imm16 {
func_type: func_type.into(),
}
}

/// Creates a new [`Instruction::ReturnCallIndirect`] for the given `func`.
pub fn return_call_indirect(func_type: impl Into<SignatureIdx>) -> Self {
Self::ReturnCallIndirect {
func_type: func_type.into(),
}
}

/// Creates a new [`Instruction::ReturnCallIndirectImm16`] for the given `func`.
pub fn return_call_indirect_imm16(func_type: impl Into<SignatureIdx>) -> Self {
Self::ReturnCallIndirectImm16 {
func_type: func_type.into(),
}
}

/// Creates a new [`Instruction::CallInternal0`] for the given `func`.
pub fn call_internal_0(results: RegisterSpan, func: EngineFunc) -> Self {
Self::CallInternal0 { results, func }
Expand Down Expand Up @@ -1048,13 +1062,32 @@ impl Instruction {
}
}

/// Creates a new [`Instruction::CallIndirect0Imm16`] for the given `func`.
pub fn call_indirect_0_imm16(
results: RegisterSpan,
func_type: impl Into<SignatureIdx>,
) -> Self {
Self::CallIndirect0Imm16 {
results,
func_type: func_type.into(),
}
}

/// Creates a new [`Instruction::CallIndirect`] for the given `func`.
pub fn call_indirect(results: RegisterSpan, func_type: impl Into<SignatureIdx>) -> Self {
Self::CallIndirect {
results,
func_type: func_type.into(),
}
}

/// Creates a new [`Instruction::CallIndirectImm16`] for the given `func`.
pub fn call_indirect_imm16(results: RegisterSpan, func_type: impl Into<SignatureIdx>) -> Self {
Self::CallIndirectImm16 {
results,
func_type: func_type.into(),
}
}
}

macro_rules! constructor_for_binary_instrs {
Expand Down
90 changes: 73 additions & 17 deletions crates/wasmi/src/engine/bytecode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -757,11 +757,7 @@ pub enum Instruction {
///
/// # Encoding
///
/// Must be followed by
///
/// 1. Either
/// - [`Instruction::CallIndirectParams`]: the `table` and `index`
/// - [`Instruction::CallIndirectParamsImm16`]: the `table` and 16-bit constant `index`
/// Must be followed by [`Instruction::CallIndirectParams`] encoding `table` and `index`.
ReturnCallIndirect0 {
/// The called internal function.
func_type: SignatureIdx,
Expand All @@ -770,15 +766,26 @@ pub enum Instruction {
///
/// # Note
///
/// Used for indirectly calling Wasm functions without parameters.
///
/// # Encoding
///
/// Must be followed by [`Instruction::CallIndirectParamsImm16`] encoding `table` and 16-bit immediate `index`.
ReturnCallIndirect0Imm16 {
/// The called internal function.
func_type: SignatureIdx,
},
/// Wasm `return_call_indirect` equivalent Wasmi instruction.
///
/// # Note
///
/// Used for indirectly calling Wasm functions with parameters.
///
/// # Encoding
///
/// Must be followed by
///
/// 1. Either
/// - [`Instruction::CallIndirectParams`]: the `table` and `index`
/// - [`Instruction::CallIndirectParamsImm16`]: the `table` and 16-bit constant `index`
/// 1. [`Instruction::CallIndirectParams`] encoding `table` and `index`
/// 2. Zero or more [`Instruction::RegisterList`]
/// 3. Followed by one of
/// - [`Instruction::Register`]
Expand All @@ -788,6 +795,26 @@ pub enum Instruction {
/// The called internal function.
func_type: SignatureIdx,
},
/// Wasm `return_call_indirect` equivalent Wasmi instruction.
///
/// # Note
///
/// Used for indirectly calling Wasm functions with parameters.
///
/// # Encoding
///
/// Must be followed by
///
/// 1. [`Instruction::CallIndirectParamsImm16`] encoding `table` and 16-bit immediate `index`
/// 2. Zero or more [`Instruction::RegisterList`]
/// 3. Followed by one of
/// - [`Instruction::Register`]
/// - [`Instruction::Register2`]
/// - [`Instruction::Register3`]
ReturnCallIndirectImm16 {
/// The called internal function.
func_type: SignatureIdx,
},

/// Wasm `call` equivalent Wasmi instruction.
///
Expand Down Expand Up @@ -863,11 +890,7 @@ pub enum Instruction {
///
/// # Encoding
///
/// Must be followed by
///
/// 1. Either
/// - [`Instruction::CallIndirectParams`]: the `table` and `index`
/// - [`Instruction::CallIndirectParamsImm16`]: the `table` and 16-bit constant `index`
/// Must be followed by [`Instruction::CallIndirectParams`] encoding `table` and `index`.
CallIndirect0 {
/// The registers storing the results of the call.
results: RegisterSpan,
Expand All @@ -878,21 +901,54 @@ pub enum Instruction {
///
/// # Note
///
/// Used for indirectly calling Wasm functions without parameters.
///
/// # Encoding
///
/// Must be followed by [`Instruction::CallIndirectParamsImm16`] encoding `table` and 16-bit constant `index`.
CallIndirect0Imm16 {
/// The registers storing the results of the call.
results: RegisterSpan,
/// The called internal function.
func_type: SignatureIdx,
},
/// Wasm `call_indirect` equivalent Wasmi instruction.
///
/// # Note
///
/// Used for indirectly calling Wasm functions with parameters.
///
/// # Encoding
///
/// Must be followed by [`Instruction::CallIndirectParams`] encoding `table` and `index`.
/// 2. Zero or more [`Instruction::RegisterList`]
/// 3. Followed by one of
/// - [`Instruction::Register`]
/// - [`Instruction::Register2`]
/// - [`Instruction::Register3`]
CallIndirect {
/// The registers storing the results of the call.
results: RegisterSpan,
/// The called internal function.
func_type: SignatureIdx,
},
/// Wasm `call_indirect` equivalent Wasmi instruction.
///
/// # Note
///
/// Used for indirectly calling Wasm functions with parameters.
///
/// # Encoding
///
/// Must be followed by
///
/// 1. Either
/// - [`Instruction::CallIndirectParams`]: the `table` and `index`
/// - [`Instruction::CallIndirectParamsImm16`]: the `table` and 16-bit constant `index`
/// 1. [`Instruction::CallIndirectParamsImm16`] encoding `table` and 16-bit immediate `index`
/// 2. Zero or more [`Instruction::RegisterList`]
/// 3. Followed by one of
/// - [`Instruction::Register`]
/// - [`Instruction::Register2`]
/// - [`Instruction::Register3`]
CallIndirect {
CallIndirectImm16 {
/// The registers storing the results of the call.
results: RegisterSpan,
/// The called internal function.
Expand Down
12 changes: 12 additions & 0 deletions crates/wasmi/src/engine/executor/instrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,15 @@ impl<'engine> Executor<'engine> {
Instr::ReturnCallIndirect0 { func_type } => {
self.execute_return_call_indirect_0::<T>(store, func_type)?
}
Instr::ReturnCallIndirect0Imm16 { func_type } => {
self.execute_return_call_indirect_0_imm16::<T>(store, func_type)?
}
Instr::ReturnCallIndirect { func_type } => {
self.execute_return_call_indirect::<T>(store, func_type)?
}
Instr::ReturnCallIndirectImm16 { func_type } => {
self.execute_return_call_indirect_imm16::<T>(store, func_type)?
}
Instr::CallInternal0 { results, func } => {
self.execute_call_internal_0(&mut store.inner, results, func)?
}
Expand All @@ -335,9 +341,15 @@ impl<'engine> Executor<'engine> {
Instr::CallIndirect0 { results, func_type } => {
self.execute_call_indirect_0::<T>(store, results, func_type)?
}
Instr::CallIndirect0Imm16 { results, func_type } => {
self.execute_call_indirect_0_imm16::<T>(store, results, func_type)?
}
Instr::CallIndirect { results, func_type } => {
self.execute_call_indirect::<T>(store, results, func_type)?
}
Instr::CallIndirectImm16 { results, func_type } => {
self.execute_call_indirect_imm16::<T>(store, results, func_type)?
}
Instr::Select {
result,
condition,
Expand Down
76 changes: 76 additions & 0 deletions crates/wasmi/src/engine/executor/instrs/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,26 @@
let table = call_params.table;
(index, table)
}
unexpected => unreachable!(
"expected `Instruction::CallIndirectParams[Imm16]` but found {unexpected:?}"
),
}
}

/// Fetches the [`Instruction::CallIndirectParamsImm16`] parameter for a call [`Instruction`].
///
/// # Note
///
/// - This advances the [`InstructionPtr`] to the next [`Instruction`].
/// - This is done by encoding an [`Instruction::TableGet`] instruction
/// word following the actual instruction where the [`TableIdx`]
/// paremeter belongs to.
/// - This is required for some instructions that do not fit into
/// a single instruction word and store a [`TableIdx`] value in
/// another instruction word.
fn pull_call_indirect_params_imm16(&mut self) -> (u32, TableIdx) {
self.ip.add(1);
match self.ip.get() {
Instruction::CallIndirectParamsImm16(call_params) => {
let index = u32::from(call_params.index);
let table = call_params.table;
Expand Down Expand Up @@ -568,6 +588,20 @@
)
}

/// Executes an [`Instruction::CallIndirect0Imm16`].
#[inline(always)]
pub fn execute_return_call_indirect_0_imm16<T>(

Check warning on line 593 in crates/wasmi/src/engine/executor/instrs/call.rs

View check run for this annotation

Codecov / codecov/patch

crates/wasmi/src/engine/executor/instrs/call.rs#L593

Added line #L593 was not covered by tests
&mut self,
store: &mut Store<T>,
func_type: SignatureIdx,
) -> Result<(), Error> {
let (index, table) = self.pull_call_indirect_params_imm16();
let results = self.caller_results();
self.execute_call_indirect_impl::<marker::ReturnCall0, T>(
store, results, func_type, index, table,

Check warning on line 601 in crates/wasmi/src/engine/executor/instrs/call.rs

View check run for this annotation

Codecov / codecov/patch

crates/wasmi/src/engine/executor/instrs/call.rs#L601

Added line #L601 was not covered by tests
)
}

/// Executes an [`Instruction::CallIndirect0`].
#[inline(always)]
pub fn execute_return_call_indirect<T>(
Expand All @@ -582,6 +616,20 @@
)
}

/// Executes an [`Instruction::CallIndirect0Imm16`].
#[inline(always)]
pub fn execute_return_call_indirect_imm16<T>(

Check warning on line 621 in crates/wasmi/src/engine/executor/instrs/call.rs

View check run for this annotation

Codecov / codecov/patch

crates/wasmi/src/engine/executor/instrs/call.rs#L621

Added line #L621 was not covered by tests
&mut self,
store: &mut Store<T>,
func_type: SignatureIdx,
) -> Result<(), Error> {
let (index, table) = self.pull_call_indirect_params_imm16();
let results = self.caller_results();
self.execute_call_indirect_impl::<marker::ReturnCall, T>(
store, results, func_type, index, table,

Check warning on line 629 in crates/wasmi/src/engine/executor/instrs/call.rs

View check run for this annotation

Codecov / codecov/patch

crates/wasmi/src/engine/executor/instrs/call.rs#L629

Added line #L629 was not covered by tests
)
}

/// Executes an [`Instruction::CallIndirect0`].
#[inline(always)]
pub fn execute_call_indirect_0<T>(
Expand All @@ -596,6 +644,20 @@
)
}

/// Executes an [`Instruction::CallIndirect0Imm16`].
#[inline(always)]
pub fn execute_call_indirect_0_imm16<T>(

Check warning on line 649 in crates/wasmi/src/engine/executor/instrs/call.rs

View check run for this annotation

Codecov / codecov/patch

crates/wasmi/src/engine/executor/instrs/call.rs#L649

Added line #L649 was not covered by tests
&mut self,
store: &mut Store<T>,
results: RegisterSpan,
func_type: SignatureIdx,
) -> Result<(), Error> {
let (index, table) = self.pull_call_indirect_params_imm16();
self.execute_call_indirect_impl::<marker::NestedCall0, T>(
store, results, func_type, index, table,

Check warning on line 657 in crates/wasmi/src/engine/executor/instrs/call.rs

View check run for this annotation

Codecov / codecov/patch

crates/wasmi/src/engine/executor/instrs/call.rs#L657

Added line #L657 was not covered by tests
)
}

/// Executes an [`Instruction::CallIndirect`].
#[inline(always)]
pub fn execute_call_indirect<T>(
Expand All @@ -610,6 +672,20 @@
)
}

/// Executes an [`Instruction::CallIndirectImm16`].
#[inline(always)]
pub fn execute_call_indirect_imm16<T>(

Check warning on line 677 in crates/wasmi/src/engine/executor/instrs/call.rs

View check run for this annotation

Codecov / codecov/patch

crates/wasmi/src/engine/executor/instrs/call.rs#L677

Added line #L677 was not covered by tests
&mut self,
store: &mut Store<T>,
results: RegisterSpan,
func_type: SignatureIdx,
) -> Result<(), Error> {
let (index, table) = self.pull_call_indirect_params_imm16();
self.execute_call_indirect_impl::<marker::NestedCall, T>(
store, results, func_type, index, table,

Check warning on line 685 in crates/wasmi/src/engine/executor/instrs/call.rs

View check run for this annotation

Codecov / codecov/patch

crates/wasmi/src/engine/executor/instrs/call.rs#L685

Added line #L685 was not covered by tests
)
}

/// Executes an [`Instruction::CallIndirect`] and [`Instruction::CallIndirect0`].
fn execute_call_indirect_impl<C: CallContext, T>(
&mut self,
Expand Down
27 changes: 26 additions & 1 deletion crates/wasmi/src/engine/translator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub use self::{
instr_encoder::{Instr, InstrEncoder},
stack::TypedProvider,
};
use super::code_map::CompiledFuncEntity;
use super::{bytecode::Provider, code_map::CompiledFuncEntity};
use crate::{
core::{TrapCode, Typed, TypedVal, UntypedVal, ValType},
engine::{
Expand Down Expand Up @@ -2599,4 +2599,29 @@ impl FuncTranslator {
fuel_info,
)
}

/// Create either [`Instruction::CallIndirectParams`] or [`Instruction::CallIndirectParamsImm16`] depending on the inputs.
fn call_indirect_params(
&mut self,
index: Provider<TypedVal>,
table_index: u32,
) -> Result<Instruction, Error> {
let instr = match index {
TypedProvider::Const(index) => match <Const16<u32>>::try_from(u32::from(index)).ok() {
Some(index) => {
// Case: the index is encodable as 16-bit constant value
// which allows us to use an optimized instruction.
Instruction::call_indirect_params_imm16(index, table_index)
}
None => {
// Case: the index is not encodable as 16-bit constant value
// and we need to allocate it as function local constant.
let index = self.alloc.stack.alloc_const(index)?;
Instruction::call_indirect_params(index, table_index)
}
},
TypedProvider::Register(index) => Instruction::call_indirect_params(index, table_index),
};
Ok(instr)
}
}
Loading
Loading