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

Optimize instruction dispatch #314

Closed
4 tasks done
Robbepop opened this issue Jan 8, 2022 · 5 comments
Closed
4 tasks done

Optimize instruction dispatch #314

Robbepop opened this issue Jan 8, 2022 · 5 comments
Assignees
Labels
enhancement New feature or request

Comments

@Robbepop
Copy link
Member

Robbepop commented Jan 8, 2022

The majority of the overhead of interpreters and in particular wasmi interpreter is the overhead of the instruction dispatch.
Therefore there are 3 main ways to improve efficiency of efficient interpreters:

  • Improve the performance of the dispatch routines, i.e. reduce their overhead.
  • Reduce the amount of executed instructions, e.g. by combining instructions into super instructions.
  • Help the CPU branch predictor to correctly predict the next branch. This is due to the fact that instruction dispatch usually consists of at least one indirect branch. It is possible to help the CPU utilize better branch prediction by providing it with more information. For example having only a single branch when using a single match statement for the dispatch routine is less efficient than having a branch per instruction (match arm) since the branch predictor can include the position of the branch into account for its prediction. Some benchmark indicate 50%-100% performance gains.

Work Items

  • Fuse common instruction sequences into super instructions for wasmi bytecode during Wasm module compilation.
  • LLVM is able to optimize switch based dispatch into one where branch predictors will benefit more at the cost of increased binary size. LLVM usually opts out of this to our despair. It might be possible to find ways to make LLVM optimize into that form from within Rust.
  • LLVM already supports guaranteed tail calls. As soon as Rust provides them too we should definitely experiment with dispatch based on tail calls similar to the Wasm3 interpreter.
    • As stated both Rust and WebAssembly currently do not have tail call support. We can reopen this issue or create a new issue once this has changed.
@Robbepop Robbepop added wasmi-v1 enhancement New feature or request labels Jan 8, 2022
@Robbepop Robbepop self-assigned this Jan 8, 2022
@Robbepop
Copy link
Member Author

This architecture could be used to speed up instruction dispatch in wasmi with safe Rust code:
https://github.com/Neopallium/s1vm

@Robbepop
Copy link
Member Author

This article well describes different instruction dispatch techniques and their expected performance:
https://www.complang.tuwien.ac.at/forth/threaded-code.html

@Robbepop
Copy link
Member Author

Robbepop commented Feb 17, 2022

Research into different instruction dispatch techniques implementable in Rust:
https://github.com/Robbepop/interpreter-dispatch-research

@Robbepop
Copy link
Member Author

PR merged to refactor the instruction dispatch for great wins: #376

@Robbepop
Copy link
Member Author

Closed since all TODO items have been answered or resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant