Skip to content

Commit

Permalink
remove superseeded branch+cmp instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
Robbepop committed Nov 25, 2023
1 parent a5432b0 commit 2b9ff82
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 171 deletions.
28 changes: 14 additions & 14 deletions crates/wasmi/src/engine/regmach/bytecode/construct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ macro_rules! constructor_for_branch_binop_imm {
impl Instruction {
$(
#[doc = concat!("Creates a new [`Instruction::", stringify!($op_code), "`].")]
pub fn $name(lhs: Register, rhs: Const16<$ty>, offset: BranchOffset16) -> Self {
Self::$op_code(BranchBinOpInstrImm16::new(lhs, rhs, offset))
pub fn $name(lhs: Register, rhs: impl Into<Const16<$ty>>, offset: BranchOffset16) -> Self {
Self::$op_code(BranchBinOpInstrImm16::new(lhs, rhs.into(), offset))
}
)*
}
Expand Down Expand Up @@ -412,24 +412,24 @@ impl Instruction {
Self::Branch { offset }
}

/// Creates a new [`Instruction::BranchI32Eqz`] for the given `condition` and `offset`.
pub fn branch_i32_eqz(condition: Register, offset: BranchOffset) -> Self {
Self::BranchI32Eqz { condition, offset }
/// Convenience constructor to create a new [`Instruction::BranchI32Eqz`] with a zero immediate value.

Check failure on line 415 in crates/wasmi/src/engine/regmach/bytecode/construct.rs

View workflow job for this annotation

GitHub Actions / Documentation

unresolved link to `Instruction::BranchI32Eqz`
pub fn branch_i32_eqz(condition: Register, offset: BranchOffset16) -> Self {
Self::branch_i32_eq_imm(condition, 0_i16, offset)
}

/// Creates a new [`Instruction::BranchI32Nez`] for the given `condition` and `offset`.
pub fn branch_i32_nez(condition: Register, offset: BranchOffset) -> Self {
Self::BranchI32Nez { condition, offset }
/// Convenience constructor to create a new [`Instruction::BranchI32Nez`] with a zero immediate value.

Check failure on line 420 in crates/wasmi/src/engine/regmach/bytecode/construct.rs

View workflow job for this annotation

GitHub Actions / Documentation

unresolved link to `Instruction::BranchI32Nez`
pub fn branch_i32_nez(condition: Register, offset: BranchOffset16) -> Self {
Self::branch_i32_ne_imm(condition, 0_i16, offset)
}

/// Creates a new [`Instruction::BranchI64Eqz`] for the given `condition` and `offset`.
pub fn branch_i64_eqz(condition: Register, offset: BranchOffset) -> Self {
Self::BranchI64Eqz { condition, offset }
/// Convenience constructor to create a new [`Instruction::BranchI64Eqz`] with a zero immediate value.

Check failure on line 425 in crates/wasmi/src/engine/regmach/bytecode/construct.rs

View workflow job for this annotation

GitHub Actions / Documentation

unresolved link to `Instruction::BranchI64Eqz`
pub fn branch_i64_eqz(condition: Register, offset: BranchOffset16) -> Self {
Self::branch_i64_eq_imm(condition, 0_i16, offset)
}

/// Creates a new [`Instruction::BranchI64Nez`] for the given `condition` and `offset`.
pub fn branch_i64_nez(condition: Register, offset: BranchOffset) -> Self {
Self::BranchI64Nez { condition, offset }
/// Convenience constructor to create a new [`Instruction::BranchI64Nez`] with a zero immediate value.

Check failure on line 430 in crates/wasmi/src/engine/regmach/bytecode/construct.rs

View workflow job for this annotation

GitHub Actions / Documentation

unresolved link to `Instruction::BranchI64Nez`
pub fn branch_i64_nez(condition: Register, offset: BranchOffset16) -> Self {
Self::branch_i64_ne_imm(condition, 0_i16, offset)
}

/// Creates a new [`Instruction::BranchTable`] for the given `index` and `len_targets`.
Expand Down
45 changes: 0 additions & 45 deletions crates/wasmi/src/engine/regmach/bytecode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,51 +367,6 @@ pub enum Instruction {
/// The branching offset for the instruction pointer.
offset: BranchOffset,
},
/// A conditional branch instruction.
///
/// # Note
///
/// - The branch is taken if `condition` evaluates to zero.
/// - Partially translated from negated Wasm `br_if` instructions.
BranchI32Eqz {
/// The register holding the condition to evaluate against zero.
condition: Register,
/// The branching offset for the instruction pointer.
offset: BranchOffset,
},
/// A Wasm `br_if` instruction.
///
/// # Note
///
/// The branch is taken if `condition` evaluates to zero.
BranchI32Nez {
/// The register holding the condition to evaluate against zero.
condition: Register,
/// The branching offset for the instruction pointer.
offset: BranchOffset,
},
/// A fused Wasm `i64.eqz` + `if` instruction.
///
/// # Note
///
/// - The branch is taken if `condition` evaluates to zero.
BranchI64Eqz {
/// The register holding the condition to evaluate against zero.
condition: Register,
/// The branching offset for the instruction pointer.
offset: BranchOffset,
},
/// A fused Wasm `i64.eqz` + `br_if` instruction.
///
/// # Note
///
/// The branch is taken if `condition` evaluates to zero.
BranchI64Nez {
/// The register holding the condition to evaluate against zero.
condition: Register,
/// The branching offset for the instruction pointer.
offset: BranchOffset,
},

/// A fused [`Instruction::I32And`] and [`Instruction::BranchI32Nez`] instruction.

Check failure on line 371 in crates/wasmi/src/engine/regmach/bytecode/mod.rs

View workflow job for this annotation

GitHub Actions / Documentation

unresolved link to `Instruction::BranchI32Nez`
BranchI32And(BranchBinOpInstr),
Expand Down
4 changes: 3 additions & 1 deletion crates/wasmi/src/engine/regmach/bytecode/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,9 @@ impl TryFrom<BranchOffset> for BranchOffset16 {

fn try_from(offset: BranchOffset) -> Result<Self, Self::Error> {
let Ok(offset16) = i16::try_from(offset.to_i32()) else {
return Err(TranslationError::new(TranslationErrorInner::BranchOffsetOutOfBounds))
return Err(TranslationError::new(
TranslationErrorInner::BranchOffsetOutOfBounds,
));
};
Ok(Self(offset16))
}
Expand Down
12 changes: 0 additions & 12 deletions crates/wasmi/src/engine/regmach/executor/instrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,18 +245,6 @@ impl<'ctx, 'engine> Executor<'ctx, 'engine> {
forward_return!(self.execute_return_nez_many(condition, values))
}
Instr::Branch { offset } => self.execute_branch(offset),
Instr::BranchI32Eqz { condition, offset } => {
self.execute_branch_i32_eqz(condition, offset)
}
Instr::BranchI32Nez { condition, offset } => {
self.execute_branch_i32_nez(condition, offset)
}
Instr::BranchI64Eqz { condition, offset } => {
self.execute_branch_i64_eqz(condition, offset)
}
Instr::BranchI64Nez { condition, offset } => {
self.execute_branch_i64_nez(condition, offset)
}
Instr::BranchTable { index, len_targets } => {
self.execute_branch_table(index, len_targets)
}
Expand Down
36 changes: 0 additions & 36 deletions crates/wasmi/src/engine/regmach/executor/instrs/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,6 @@ impl<'ctx, 'engine> Executor<'ctx, 'engine> {
self.branch_to(offset)
}

#[inline(always)]
pub fn execute_branch_i32_nez(&mut self, condition: Register, offset: BranchOffset) {
let value: i32 = self.get_register_as(condition);
if value != 0 {
return self.branch_to(offset);
}
self.next_instr();
}

#[inline(always)]
pub fn execute_branch_i32_eqz(&mut self, condition: Register, offset: BranchOffset) {
let value: i32 = self.get_register_as(condition);
if value == 0 {
return self.branch_to(offset);
}
self.next_instr();
}

#[inline(always)]
pub fn execute_branch_i64_nez(&mut self, condition: Register, offset: BranchOffset) {
let value: i64 = self.get_register_as(condition);
if value != 0 {
return self.branch_to(offset);
}
self.next_instr();
}

#[inline(always)]
pub fn execute_branch_i64_eqz(&mut self, condition: Register, offset: BranchOffset) {
let value: i64 = self.get_register_as(condition);
if value == 0 {
return self.branch_to(offset);
}
self.next_instr();
}

#[inline(always)]
pub fn execute_branch_table(&mut self, index: Register, len_targets: Const32<u32>) {
let index: u32 = self.get_register_as(index);
Expand Down
6 changes: 3 additions & 3 deletions crates/wasmi/src/engine/regmach/tests/op/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::*;
use crate::engine::{
bytecode::BranchOffset,
regmach::{
bytecode::RegisterSpan,
bytecode::{BranchOffset16, RegisterSpan},
tests::{display_wasm::DisplayValueType, wasm_type::WasmType},
},
};
Expand Down Expand Up @@ -346,7 +346,7 @@ fn branch_if_block_0() {
);
TranslationTest::new(wasm)
.expect_func_instrs([
Instruction::branch_i32_nez(Register::from_i16(0), BranchOffset::from(1)),
Instruction::branch_i32_nez(Register::from_i16(0), BranchOffset16::from(1)),
Instruction::Return,
])
.run()
Expand All @@ -369,7 +369,7 @@ fn branch_if_block_1() {
);
TranslationTest::new(wasm)
.expect_func_instrs([
Instruction::branch_i32_eqz(Register::from_i16(1), BranchOffset::from(3)),
Instruction::branch_i32_eqz(Register::from_i16(1), BranchOffset16::from(3)),
Instruction::copy(Register::from_i16(2), Register::from_i16(0)),
Instruction::branch(BranchOffset::from(2)),
Instruction::copy(Register::from_i16(2), Register::from_i16(0)),
Expand Down
16 changes: 8 additions & 8 deletions crates/wasmi/src/engine/regmach/tests/op/br_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::*;
use crate::engine::{
bytecode::BranchOffset,
regmach::{
bytecode::RegisterSpan,
bytecode::{BranchOffset16, RegisterSpan},
tests::{display_wasm::DisplayValueType, driver::ExpectedFunc, wasm_type::WasmType},
},
};
Expand Down Expand Up @@ -844,7 +844,7 @@ fn branch_if_results_0() {
);
TranslationTest::new(wasm)
.expect_func_instrs([
Instruction::branch_i32_nez(Register::from_i16(0), BranchOffset::from(1)),
Instruction::branch_i32_nez(Register::from_i16(0), BranchOffset16::from(1)),
Instruction::Return,
])
.run()
Expand All @@ -867,7 +867,7 @@ fn branch_if_results_1() {
);
TranslationTest::new(wasm)
.expect_func_instrs([
Instruction::branch_i32_eqz(Register::from_i16(1), BranchOffset::from(3)),
Instruction::branch_i32_eqz(Register::from_i16(1), BranchOffset16::from(3)),
Instruction::copy(Register::from_i16(2), Register::from_i16(0)),
Instruction::branch(BranchOffset::from(2)),
Instruction::copy(Register::from_i16(2), Register::from_i16(0)),
Expand Down Expand Up @@ -902,7 +902,7 @@ fn branch_if_results_1_avoid_copy() {
.expect_func_instrs([
Instruction::i32_clz(Register::from_i16(2), Register::from_i16(0)),
Instruction::i32_ctz(Register::from_i16(3), Register::from_i16(1)),
Instruction::branch_i32_nez(Register::from_i16(3), BranchOffset::from(1)),
Instruction::branch_i32_nez(Register::from_i16(3), BranchOffset16::from(1)),
Instruction::return_reg(Register::from_i16(2)),
])
.run()
Expand All @@ -927,7 +927,7 @@ fn branch_if_results_2() {
);
TranslationTest::new(wasm)
.expect_func_instrs([
Instruction::branch_i32_eqz(Register::from_i16(2), BranchOffset::from(3)),
Instruction::branch_i32_eqz(Register::from_i16(2), BranchOffset16::from(3)),
Instruction::copy2(RegisterSpan::new(Register::from_i16(3)), 0, 1),
Instruction::branch(BranchOffset::from(2)),
Instruction::copy2(RegisterSpan::new(Register::from_i16(3)), 0, 1),
Expand Down Expand Up @@ -968,7 +968,7 @@ fn branch_if_results_2_avoid_copy() {
.expect_func_instrs([
Instruction::i32_clz(Register::from_i16(3), Register::from_i16(0)),
Instruction::i32_ctz(Register::from_i16(4), Register::from_i16(1)),
Instruction::branch_i32_nez(Register::from_i16(2), BranchOffset::from(1)),
Instruction::branch_i32_nez(Register::from_i16(2), BranchOffset16::from(1)),
Instruction::i32_add(
Register::from_i16(3),
Register::from_i16(3),
Expand Down Expand Up @@ -1002,7 +1002,7 @@ fn branch_if_results_4_mixed_1() {
TranslationTest::new(wasm)
.expect_func(
ExpectedFunc::new([
Instruction::branch_i32_eqz(Register::from_i16(2), BranchOffset::from(4)),
Instruction::branch_i32_eqz(Register::from_i16(2), BranchOffset16::from(4)),
Instruction::copy_many_non_overlapping(
RegisterSpan::new(Register::from_i16(3)),
-1,
Expand Down Expand Up @@ -1045,7 +1045,7 @@ fn branch_if_results_4_mixed_2() {
);
TranslationTest::new(wasm)
.expect_func_instrs([
Instruction::branch_i32_eqz(Register::from_i16(2), BranchOffset::from(4)),
Instruction::branch_i32_eqz(Register::from_i16(2), BranchOffset16::from(4)),
Instruction::copy_many_non_overlapping(RegisterSpan::new(Register::from_i16(3)), 0, 0),
Instruction::register2(1, 1),
Instruction::branch(BranchOffset::from(3)),
Expand Down
8 changes: 4 additions & 4 deletions crates/wasmi/src/engine/regmach/tests/op/cmp_br.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ fn loop_backward_imm() {
#[test]
#[cfg_attr(miri, ignore)]
fn loop_backward_imm_eqz() {
fn test_for(op: &str, expect_instr: fn(Register, BranchOffset) -> Instruction) {
fn test_for(op: &str, expect_instr: fn(Register, BranchOffset16) -> Instruction) {
let wasm = wat2wasm(&format!(
r"
(module
Expand All @@ -166,7 +166,7 @@ fn loop_backward_imm_eqz() {
));
TranslationTest::new(wasm)
.expect_func_instrs([
expect_instr(Register::from_i16(0), BranchOffset::from(0_i32)),
expect_instr(Register::from_i16(0), BranchOffset16::from(0_i16)),
Instruction::Return,
])
.run()
Expand Down Expand Up @@ -539,7 +539,7 @@ fn block_i64_eqz_fuse() {
);
TranslationTest::new(wasm)
.expect_func_instrs([
Instruction::branch_i64_eqz(Register::from_i16(0), BranchOffset::from(1)),
Instruction::branch_i64_eqz(Register::from_i16(0), BranchOffset16::from(1)),
Instruction::Return,
])
.run()
Expand All @@ -561,7 +561,7 @@ fn if_i64_eqz_fuse() {
);
TranslationTest::new(wasm)
.expect_func_instrs([
Instruction::branch_i64_nez(Register::from_i16(0), BranchOffset::from(1)),
Instruction::branch_i64_nez(Register::from_i16(0), BranchOffset16::from(1)),
Instruction::Return,
])
.run()
Expand Down
Loading

0 comments on commit 2b9ff82

Please sign in to comment.