Skip to content

Add CompareResult extension trait for Instruction #1537

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 13, 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
88 changes: 88 additions & 0 deletions crates/wasmi/src/engine/translator/comparator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,94 @@ pub trait AllocConst {
fn alloc_const<T: Into<UntypedVal>>(&mut self, value: T) -> Result<Reg, Error>;
}

/// Extension trait to return [`Reg`] result of compare [`Instruction`]s.
pub trait CompareResult {
/// Returns the result [`Reg`] of the compare [`Instruction`].
///
/// Returns `None` if the [`Instruction`] is not a compare instruction.
fn compare_result(&self) -> Option<Reg>;
}

impl CompareResult for Instruction {
fn compare_result(&self) -> Option<Reg> {
use crate::ir::Instruction as I;
let result = match *self {
| I::I32BitAnd { result, .. }
| I::I32BitAndImm16 { result, .. }
| I::I32BitOr { result, .. }
| I::I32BitOrImm16 { result, .. }
| I::I32BitXor { result, .. }
| I::I32BitXorImm16 { result, .. }
| I::I32And { result, .. }
| I::I32AndImm16 { result, .. }
| I::I32Or { result, .. }
| I::I32OrImm16 { result, .. }
| I::I32Xor { result, .. }
| I::I32XorImm16 { result, .. }
| I::I32Nand { result, .. }
| I::I32NandImm16 { result, .. }
| I::I32Nor { result, .. }
| I::I32NorImm16 { result, .. }
| I::I32Xnor { result, .. }
| I::I32XnorImm16 { result, .. }
| I::I32Eq { result, .. }
| I::I32EqImm16 { result, .. }
| I::I32Ne { result, .. }
| I::I32NeImm16 { result, .. }
| I::I32LtS { result, .. }
| I::I32LtSImm16Lhs { result, .. }
| I::I32LtSImm16Rhs { result, .. }
| I::I32LtU { result, .. }
| I::I32LtUImm16Lhs { result, .. }
| I::I32LtUImm16Rhs { result, .. }
| I::I32LeS { result, .. }
| I::I32LeSImm16Lhs { result, .. }
| I::I32LeSImm16Rhs { result, .. }
| I::I32LeU { result, .. }
| I::I32LeUImm16Lhs { result, .. }
| I::I32LeUImm16Rhs { result, .. }
| I::I64And { result, .. }
| I::I64AndImm16 { result, .. }
| I::I64Or { result, .. }
| I::I64OrImm16 { result, .. }
| I::I64Xor { result, .. }
| I::I64XorImm16 { result, .. }
| I::I64Nand { result, .. }
| I::I64NandImm16 { result, .. }
| I::I64Nor { result, .. }
| I::I64NorImm16 { result, .. }
| I::I64Xnor { result, .. }
| I::I64XnorImm16 { result, .. }
| I::I64Eq { result, .. }
| I::I64EqImm16 { result, .. }
| I::I64Ne { result, .. }
| I::I64NeImm16 { result, .. }
| I::I64LtS { result, .. }
| I::I64LtSImm16Lhs { result, .. }
| I::I64LtSImm16Rhs { result, .. }
| I::I64LtU { result, .. }
| I::I64LtUImm16Lhs { result, .. }
| I::I64LtUImm16Rhs { result, .. }
| I::I64LeS { result, .. }
| I::I64LeSImm16Lhs { result, .. }
| I::I64LeSImm16Rhs { result, .. }
| I::I64LeU { result, .. }
| I::I64LeUImm16Lhs { result, .. }
| I::I64LeUImm16Rhs { result, .. }
| I::F32Eq { result, .. }
| I::F32Ne { result, .. }
| I::F32Lt { result, .. }
| I::F32Le { result, .. }
| I::F64Eq { result, .. }
| I::F64Ne { result, .. }
| I::F64Lt { result, .. }
| I::F64Le { result, .. } => result,
_ => return None,
};
Some(result)
}
}

pub trait NegateCmpInstr: Sized {
/// Negates the compare (`cmp`) [`Instruction`].
fn negate_cmp_instr(&self) -> Option<Self>;
Expand Down
42 changes: 3 additions & 39 deletions crates/wasmi/src/engine/translator/func/instr_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{
core::{FuelCostsProvider, UntypedVal, ValType},
engine::translator::{
comparator::{
CompareResult,
LogicalizeCmpInstr,
NegateCmpInstr,
TryIntoCmpBranchFallbackInstr,
Expand Down Expand Up @@ -1051,7 +1052,6 @@ impl InstrEncoder {
/// Try to fuse [`Instruction`] at `instr` into a branch+cmp instruction.
///
/// Returns `Ok(Some)` if successful.
#[rustfmt::skip]
fn try_fuse_branch_cmp_for_instr(
&mut self,
stack: &mut ValueStack,
Expand All @@ -1060,45 +1060,9 @@ impl InstrEncoder {
label: LabelRef,
negate: bool,
) -> Result<Option<Instruction>, Error> {
use Instruction as I;
let last_instruction = *self.instrs.get(last_instr);
let result = match last_instruction {
| I::I32BitAnd { result, .. } | I::I32BitAndImm16 { result, .. }
| I::I32BitOr { result, .. } | I::I32BitOrImm16 { result, .. }
| I::I32BitXor { result, .. } | I::I32BitXorImm16 { result, .. }
| I::I32And { result, .. } | I::I32AndImm16 { result, .. }
| I::I32Or { result, .. } | I::I32OrImm16 { result, .. }
| I::I32Xor { result, .. } | I::I32XorImm16 { result, .. }
| I::I32Nand { result, .. } | I::I32NandImm16 { result, .. }
| I::I32Nor { result, .. } | I::I32NorImm16 { result, .. }
| I::I32Xnor { result, .. } | I::I32XnorImm16 { result, .. }
| I::I32Eq { result, .. } | I::I32EqImm16 { result, .. }
| I::I32Ne { result, .. } | I::I32NeImm16 { result, .. }
| I::I32LtS { result, .. } | I::I32LtSImm16Lhs { result, .. } | I::I32LtSImm16Rhs { result, .. }
| I::I32LtU { result, .. } | I::I32LtUImm16Lhs { result, .. } | I::I32LtUImm16Rhs { result, .. }
| I::I32LeS { result, .. } | I::I32LeSImm16Lhs { result, .. } | I::I32LeSImm16Rhs { result, .. }
| I::I32LeU { result, .. } | I::I32LeUImm16Lhs { result, .. } | I::I32LeUImm16Rhs { result, .. }
| I::I64And { result, .. } | I::I64AndImm16 { result, .. }
| I::I64Or { result, .. } | I::I64OrImm16 { result, .. }
| I::I64Xor { result, .. } | I::I64XorImm16 { result, .. }
| I::I64Nand { result, .. } | I::I64NandImm16 { result, .. }
| I::I64Nor { result, .. } | I::I64NorImm16 { result, .. }
| I::I64Xnor { result, .. } | I::I64XnorImm16 { result, .. }
| I::I64Eq { result, .. } | I::I64EqImm16 { result, .. }
| I::I64Ne { result, .. } | I::I64NeImm16 { result, .. }
| I::I64LtS { result, .. } | I::I64LtSImm16Lhs { result, .. } | I::I64LtSImm16Rhs { result, .. }
| I::I64LtU { result, .. } | I::I64LtUImm16Lhs { result, .. } | I::I64LtUImm16Rhs { result, .. }
| I::I64LeS { result, .. } | I::I64LeSImm16Lhs { result, .. } | I::I64LeSImm16Rhs { result, .. }
| I::I64LeU { result, .. } | I::I64LeUImm16Lhs { result, .. } | I::I64LeUImm16Rhs { result, .. }
| I::F32Eq { result, .. }
| I::F32Ne { result, .. }
| I::F32Lt { result, .. }
| I::F32Le { result, .. }
| I::F64Eq { result, .. }
| I::F64Ne { result, .. }
| I::F64Lt { result, .. }
| I::F64Le { result, .. } => result,
_ => return Ok(None),
let Some(result) = last_instruction.compare_result() else {
return Ok(None);
};
if matches!(stack.get_register_space(result), RegisterSpace::Local) {
// We need to filter out instructions that store their result
Expand Down