Skip to content

Generalize comparator submodule logic #1536

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

Merged
merged 3 commits into from
Jun 12, 2025
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
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
use crate::{
engine::translator::func::ValueStack,
core::UntypedVal,
ir::{BranchOffset, BranchOffset16, Comparator, ComparatorAndOffset, Instruction, Reg},
Error,
};

/// Types able to allocate function local constant values.
///
/// # Note
///
/// This allows to cheaply convert immediate values to [`Reg`]s.
///
/// # Errors
///
/// If the function local constant allocation from immediate value to [`Reg`] failed.
pub trait AllocConst {
/// Allocates a new function local constant value and returns its [`Reg`].
///
/// # Note
///
/// Constant values allocated this way are deduplicated and return shared [`Reg`].
fn alloc_const<T: Into<UntypedVal>>(&mut self, value: T) -> Result<Reg, Error>;
}

pub trait NegateCmpInstr: Sized {
/// Negates the compare (`cmp`) [`Instruction`].
fn negate_cmp_instr(&self) -> Option<Self>;
Expand Down Expand Up @@ -346,15 +364,15 @@ pub trait TryIntoCmpBranchInstr: Sized {
fn try_into_cmp_branch_instr(
&self,
offset: BranchOffset,
stack: &mut ValueStack,
stack: &mut impl AllocConst,
) -> Result<Option<Self>, Error>;
}

impl TryIntoCmpBranchInstr for Instruction {
fn try_into_cmp_branch_instr(
&self,
offset: BranchOffset,
stack: &mut ValueStack,
stack: &mut impl AllocConst,
) -> Result<Option<Self>, Error> {
use Instruction as I;
let Ok(offset) = BranchOffset16::try_from(offset) else {
Expand Down Expand Up @@ -458,15 +476,15 @@ pub trait TryIntoCmpBranchFallbackInstr {
fn try_into_cmp_branch_fallback_instr(
&self,
offset: BranchOffset,
stack: &mut ValueStack,
stack: &mut impl AllocConst,
) -> Result<Option<Instruction>, Error>;
}

impl TryIntoCmpBranchFallbackInstr for Instruction {
fn try_into_cmp_branch_fallback_instr(
&self,
offset: BranchOffset,
stack: &mut ValueStack,
stack: &mut impl AllocConst,
) -> Result<Option<Instruction>, Error> {
use Instruction as I;
debug_assert!(BranchOffset16::try_from(offset).is_err());
Expand Down
12 changes: 7 additions & 5 deletions crates/wasmi/src/engine/translator/func/instr_encoder.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
use crate::{
core::{FuelCostsProvider, UntypedVal, ValType},
engine::translator::{
func::{
stack::RegisterSpace,
utils::FromProviders as _,
LabelRef,
LabelRegistry,
comparator::{
LogicalizeCmpInstr,
NegateCmpInstr,
TryIntoCmpBranchFallbackInstr,
TryIntoCmpBranchInstr,
TryIntoCmpSelectInstr,
},
func::{
stack::RegisterSpace,
utils::FromProviders as _,
LabelRef,
LabelRegistry,
TypedProvider,
ValueStack,
},
Expand Down
20 changes: 6 additions & 14 deletions crates/wasmi/src/engine/translator/func/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Function translation for the register-machine bytecode based Wasmi engine.

mod comparator;
mod control_frame;
mod control_stack;
mod instr_encoder;
Expand All @@ -13,14 +12,13 @@ mod visit;
#[cfg(feature = "simd")]
mod simd;

pub use self::{
control_frame::ControlFrame,
control_stack::ControlStack,
instr_encoder::{Instr, InstrEncoder},
stack::TypedProvider,
};
use self::{
comparator::{
LogicalizeCmpInstr,
NegateCmpInstr,
TryIntoCmpBranchFallbackInstr,
TryIntoCmpBranchInstr,
TryIntoCmpSelectInstr,
},
control_frame::{
BlockControlFrame,
BlockHeight,
Expand All @@ -34,12 +32,6 @@ use self::{
stack::ValueStack,
utils::FromProviders as _,
};
pub use self::{
control_frame::ControlFrame,
control_stack::ControlStack,
instr_encoder::{Instr, InstrEncoder},
stack::TypedProvider,
};
use crate::{
core::{FuelCostsProvider, TrapCode, TypedVal, UntypedVal, ValType},
engine::{
Expand Down
11 changes: 10 additions & 1 deletion crates/wasmi/src/engine/translator/func/stack/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ use super::{PreservedLocal, TypedVal};
use crate::{
core::UntypedVal,
engine::{
translator::func::{Provider, UntypedProvider},
translator::{
comparator::AllocConst,
func::{Provider, UntypedProvider},
},
TranslationError,
},
ir::{Reg, RegSpan},
Expand Down Expand Up @@ -420,3 +423,9 @@ impl ValueStack {
self.reg_alloc.dec_register_usage(register)
}
}

impl AllocConst for ValueStack {
fn alloc_const<T: Into<UntypedVal>>(&mut self, value: T) -> Result<Reg, Error> {
self.consts.alloc(value.into())
}
}
1 change: 1 addition & 0 deletions crates/wasmi/src/engine/translator/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Function translation for the register-machine bytecode based Wasmi engine.

mod comparator;
mod driver;
mod error;
mod func;
Expand Down
Loading