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

Add Instruction::branch_i64_{eqz,nez} instructions #797

Merged
merged 4 commits into from
Nov 24, 2023
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
22 changes: 16 additions & 6 deletions crates/wasmi/src/engine/regmach/bytecode/construct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,14 +411,24 @@ impl Instruction {
Self::Branch { offset }
}

/// Creates a new [`Instruction::BranchEqz`] for the given `condition` and `offset`.
pub fn branch_eqz(condition: Register, offset: BranchOffset) -> Self {
Self::BranchEqz { condition, 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 }
}

/// Creates a new [`Instruction::BranchNez`] for the given `condition` and `offset`.
pub fn branch_nez(condition: Register, offset: BranchOffset) -> Self {
Self::BranchNez { condition, 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 }
}

/// 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 }
}

/// 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 }
}

/// Creates a new [`Instruction::BranchTable`] for the given `index` and `len_targets`.
Expand Down
154 changes: 88 additions & 66 deletions crates/wasmi/src/engine/regmach/bytecode/mod.rs

Large diffs are not rendered by default.

14 changes: 10 additions & 4 deletions crates/wasmi/src/engine/regmach/executor/instrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,17 @@ impl<'ctx, 'engine> Executor<'ctx, 'engine> {
forward_return!(self.execute_return_nez_many(condition, values))
}
Instr::Branch { offset } => self.execute_branch(offset),
Instr::BranchEqz { condition, offset } => {
self.execute_branch_eqz(condition, offset)
Instr::BranchI32Eqz { condition, offset } => {
self.execute_branch_i32_eqz(condition, offset)
}
Instr::BranchNez { condition, offset } => {
self.execute_branch_nez(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
46 changes: 28 additions & 18 deletions crates/wasmi/src/engine/regmach/executor/instrs/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,39 @@ impl<'ctx, 'engine> Executor<'ctx, 'engine> {
}

#[inline(always)]
pub fn execute_branch_nez(&mut self, condition: Register, offset: BranchOffset) {
let condition: bool = self.get_register_as(condition);
match condition {
true => {
self.branch_to(offset);
}
false => {
self.next_instr();
}
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_eqz(&mut self, condition: Register, offset: BranchOffset) {
let condition: bool = self.get_register_as(condition);
match condition {
true => {
self.next_instr();
}
false => {
self.branch_to(offset);
}
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)]
Expand Down
4 changes: 2 additions & 2 deletions crates/wasmi/src/engine/regmach/tests/op/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ fn branch_if_block_0() {
);
TranslationTest::new(wasm)
.expect_func_instrs([
Instruction::branch_nez(Register::from_i16(0), BranchOffset::from(1)),
Instruction::branch_i32_nez(Register::from_i16(0), BranchOffset::from(1)),
Instruction::Return,
])
.run()
Expand All @@ -369,7 +369,7 @@ fn branch_if_block_1() {
);
TranslationTest::new(wasm)
.expect_func_instrs([
Instruction::branch_eqz(Register::from_i16(1), BranchOffset::from(3)),
Instruction::branch_i32_eqz(Register::from_i16(1), BranchOffset::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
14 changes: 7 additions & 7 deletions crates/wasmi/src/engine/regmach/tests/op/br_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ fn branch_if_results_0() {
);
TranslationTest::new(wasm)
.expect_func_instrs([
Instruction::branch_nez(Register::from_i16(0), BranchOffset::from(1)),
Instruction::branch_i32_nez(Register::from_i16(0), BranchOffset::from(1)),
Instruction::Return,
])
.run()
Expand All @@ -867,7 +867,7 @@ fn branch_if_results_1() {
);
TranslationTest::new(wasm)
.expect_func_instrs([
Instruction::branch_eqz(Register::from_i16(1), BranchOffset::from(3)),
Instruction::branch_i32_eqz(Register::from_i16(1), BranchOffset::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_nez(Register::from_i16(3), BranchOffset::from(1)),
Instruction::branch_i32_nez(Register::from_i16(3), BranchOffset::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_eqz(Register::from_i16(2), BranchOffset::from(3)),
Instruction::branch_i32_eqz(Register::from_i16(2), BranchOffset::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_nez(Register::from_i16(2), BranchOffset::from(1)),
Instruction::branch_i32_nez(Register::from_i16(2), BranchOffset::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_eqz(Register::from_i16(2), BranchOffset::from(4)),
Instruction::branch_i32_eqz(Register::from_i16(2), BranchOffset::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_eqz(Register::from_i16(2), BranchOffset::from(4)),
Instruction::branch_i32_eqz(Register::from_i16(2), BranchOffset::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
48 changes: 46 additions & 2 deletions crates/wasmi/src/engine/regmach/tests/op/cmp_br.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ fn loop_backward_imm_eqz() {
])
.run()
}
test_for("eq", Instruction::branch_eqz);
test_for("ne", Instruction::branch_nez);
test_for("eq", Instruction::branch_i32_eqz);
test_for("ne", Instruction::branch_i32_nez);
}

#[test]
Expand Down Expand Up @@ -522,3 +522,47 @@ fn if_i32_eqz_fuse() {
test_for("or", Instruction::branch_i32_or);
test_for("xor", Instruction::branch_i32_xor);
}

#[test]
#[cfg_attr(miri, ignore)]
fn block_i64_eqz_fuse() {
let wasm = wat2wasm(
r"
(module
(func (param i64)
(block
(i64.eqz (local.get 0))
(br_if 0)
)
)
)",
);
TranslationTest::new(wasm)
.expect_func_instrs([
Instruction::branch_i64_eqz(Register::from_i16(0), BranchOffset::from(1)),
Instruction::Return,
])
.run()
}

#[test]
#[cfg_attr(miri, ignore)]
fn if_i64_eqz_fuse() {
let wasm = wat2wasm(
r"
(module
(func (param i64)
(if
(i64.eqz (local.get 0))
(then)
)
)
)",
);
TranslationTest::new(wasm)
.expect_func_instrs([
Instruction::branch_i64_nez(Register::from_i16(0), BranchOffset::from(1)),
Instruction::Return,
])
.run()
}
32 changes: 16 additions & 16 deletions crates/wasmi/src/engine/regmach/tests/op/if_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn simple_if_then() {
);
TranslationTest::new(wasm)
.expect_func_instrs([
Instruction::branch_eqz(Register::from_i16(0), BranchOffset::from(1)),
Instruction::branch_i32_eqz(Register::from_i16(0), BranchOffset::from(1)),
Instruction::Return,
])
.run()
Expand All @@ -46,8 +46,8 @@ fn simple_if_then_nested() {
);
TranslationTest::new(wasm)
.expect_func_instrs([
Instruction::branch_eqz(Register::from_i16(0), BranchOffset::from(2)),
Instruction::branch_eqz(Register::from_i16(1), BranchOffset::from(1)),
Instruction::branch_i32_eqz(Register::from_i16(0), BranchOffset::from(2)),
Instruction::branch_i32_eqz(Register::from_i16(1), BranchOffset::from(1)),
Instruction::Return,
])
.run()
Expand All @@ -72,7 +72,7 @@ fn if_then_global_set() {
);
TranslationTest::new(wasm)
.expect_func_instrs([
Instruction::branch_eqz(Register::from_i16(0), BranchOffset::from(2)),
Instruction::branch_i32_eqz(Register::from_i16(0), BranchOffset::from(2)),
Instruction::global_set(GlobalIdx::from(0), Register::from_i16(1)),
Instruction::return_imm32(AnyConst32::from(10_i32)),
])
Expand Down Expand Up @@ -102,7 +102,7 @@ fn if_then_return() {
);
TranslationTest::new(wasm)
.expect_func_instrs([
Instruction::branch_eqz(Register::from_i16(0), BranchOffset::from(3)),
Instruction::branch_i32_eqz(Register::from_i16(0), BranchOffset::from(3)),
Instruction::i32_add(
Register::from_i16(3),
Register::from_i16(1),
Expand Down Expand Up @@ -135,7 +135,7 @@ fn if_then_else_return() {
);
TranslationTest::new(wasm)
.expect_func_instrs([
Instruction::branch_eqz(Register::from_i16(0), BranchOffset::from(2)),
Instruction::branch_i32_eqz(Register::from_i16(0), BranchOffset::from(2)),
Instruction::return_imm32(AnyConst32::from(10_i32)),
Instruction::return_imm32(AnyConst32::from(20_i32)),
])
Expand Down Expand Up @@ -163,7 +163,7 @@ fn if_then_br_else() {
);
TranslationTest::new(wasm)
.expect_func_instrs([
Instruction::branch_eqz(Register::from_i16(0), BranchOffset::from(2)),
Instruction::branch_i32_eqz(Register::from_i16(0), BranchOffset::from(2)),
Instruction::branch(BranchOffset::from(2)),
Instruction::return_imm32(AnyConst32::from(10_i32)),
Instruction::return_imm32(AnyConst32::from(20_i32)),
Expand Down Expand Up @@ -192,7 +192,7 @@ fn if_then_else_br() {
);
TranslationTest::new(wasm)
.expect_func_instrs([
Instruction::branch_eqz(Register::from_i16(0), BranchOffset::from(2)),
Instruction::branch_i32_eqz(Register::from_i16(0), BranchOffset::from(2)),
Instruction::return_imm32(AnyConst32::from(10_i32)),
Instruction::branch(BranchOffset::from(1)),
Instruction::return_imm32(AnyConst32::from(20_i32)),
Expand All @@ -218,7 +218,7 @@ fn simple_if_then_else() {
);
TranslationTest::new(wasm)
.expect_func_instrs([
Instruction::branch_eqz(Register::from_i16(0), BranchOffset::from(2)),
Instruction::branch_i32_eqz(Register::from_i16(0), BranchOffset::from(2)),
Instruction::branch(BranchOffset::from(1)),
Instruction::Return,
])
Expand Down Expand Up @@ -251,11 +251,11 @@ fn simple_if_then_else_nested() {
);
TranslationTest::new(wasm)
.expect_func_instrs([
Instruction::branch_eqz(Register::from_i16(0), BranchOffset::from(4)),
Instruction::branch_eqz(Register::from_i16(1), BranchOffset::from(2)),
Instruction::branch_i32_eqz(Register::from_i16(0), BranchOffset::from(4)),
Instruction::branch_i32_eqz(Register::from_i16(1), BranchOffset::from(2)),
Instruction::branch(BranchOffset::from(1)),
Instruction::branch(BranchOffset::from(3)),
Instruction::branch_eqz(Register::from_i16(1), BranchOffset::from(2)),
Instruction::branch_i32_eqz(Register::from_i16(1), BranchOffset::from(2)),
Instruction::branch(BranchOffset::from(1)),
Instruction::Return,
])
Expand All @@ -280,7 +280,7 @@ fn if_then_else_with_params() {
);
TranslationTest::new(wasm)
.expect_func_instrs([
Instruction::branch_eqz(Register::from_i16(0), BranchOffset::from(3)),
Instruction::branch_i32_eqz(Register::from_i16(0), BranchOffset::from(3)),
Instruction::i32_add(
Register::from_i16(3),
Register::from_i16(1),
Expand Down Expand Up @@ -445,7 +445,7 @@ fn const_condition_br_if_then() {
test_for(
false,
[
Instruction::branch_nez(Register::from_i16(0), BranchOffset::from(2)),
Instruction::branch_i32_nez(Register::from_i16(0), BranchOffset::from(2)),
Instruction::Trap(TrapCode::UnreachableCodeReached),
Instruction::return_imm32(AnyConst32::from(1_i32)),
],
Expand Down Expand Up @@ -484,7 +484,7 @@ fn const_condition_br_if_else() {
test_for(
true,
[
Instruction::branch_nez(Register::from_i16(0), BranchOffset::from(2)),
Instruction::branch_i32_nez(Register::from_i16(0), BranchOffset::from(2)),
Instruction::Trap(TrapCode::UnreachableCodeReached),
Instruction::return_imm32(AnyConst32::from(1_i32)),
],
Expand Down Expand Up @@ -569,7 +569,7 @@ fn test_if_without_else_has_result() {
RegisterSpan::new(Register::from_i16(0)),
CompiledFunc::from_u32(0),
),
Instruction::branch_eqz(Register::from_i16(1), BranchOffset::from(3)),
Instruction::branch_i32_eqz(Register::from_i16(1), BranchOffset::from(3)),
Instruction::copy_i64imm32(Register::from_i16(0), -1),
Instruction::branch(BranchOffset::from(1)),
Instruction::return_reg(Register::from_i16(0)),
Expand Down
Loading
Loading