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

Fuse i64.{and,or,xor} + i64.eqz + i32.eqz + br_if #803

Open
Robbepop opened this issue Nov 24, 2023 · 3 comments
Open

Fuse i64.{and,or,xor} + i64.eqz + i32.eqz + br_if #803

Robbepop opened this issue Nov 24, 2023 · 3 comments
Labels
good first issue Good for newcomers optimization An performance optimization issue. register-machine A work item for the register-machine engine.

Comments

@Robbepop
Copy link
Member

Robbepop commented Nov 24, 2023

The instruction sequence

i64.{and,or,xor}
i64.eqz
i32.eqz
{if, br_if}

Is very common. The i64.eqz, i32.eqz sequence acts as a double negative, thus nullifying itself. It exists for the purpose of casting the i64 into a i32 which the Wasm validation expects by if and br_if to form something like if and(a, b) where a and b are both i64 typed values.

This requires addition of the following wasmi IR instructions:

New compute instructions for the intermediate fuse steps:

  • i64.and_eqz
  • i64.and_eqz_imm16
  • i64.or_eqz
  • i64.or_eqz_imm16
  • i64.xor_eqz
  • i64.xor_eqz_imm16

New branch instructions:

  • i64.branch_and_eqz
  • i64.branch_and_eqz_imm16
  • i64.branch_or_eqz
  • i64.branch_or_eqz_imm16
  • i64.branch_xor_eqz
  • i64.branch_xor_eqz_imm16

This should be benchmarks thoroughly since it might well be that the addition of this optimization might degrade wasmi performance instead of acting as an optimization.

@Robbepop Robbepop added optimization An performance optimization issue. register-machine A work item for the register-machine engine. good first issue Good for newcomers labels Nov 24, 2023
@OLUWAMUYIWA
Copy link
Contributor

Would love to take this up

@Robbepop
Copy link
Member Author

Robbepop commented May 10, 2024

I already did this experimentally and didn't see great results so I concluded that this optimization is either not worth it or my implementation was doing something wrong. So please if you want to take this be prepared that it won't get merged in case you see similar results.

In constrast to the i32 version of this optimization we see far fewer occurrences in compiled Wasm binaries. One reason for this is that all conditionals return a i32 value and thus chaining is simply not a thing for i64 instructions.

But even if your PR does not yield great results either it is probably a good exercise.

@OLUWAMUYIWA
Copy link
Contributor

Hmm. Thanks for the heads up!
I'm still going to try it though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers optimization An performance optimization issue. register-machine A work item for the register-machine engine.
Projects
None yet
Development

No branches or pull requests

2 participants