-
Notifications
You must be signed in to change notification settings - Fork 695
Branch hinting #1363
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
Comments
FWIW, I think adding new instructions would be the cleanest approach here, but a downside is that we have a growing number of branch instructions (e.g. br_on_exn, casting instructions) in addition to the I would also be curious to see data on the potential performance gains this extension would unlock; I don't have a good intuition for it. |
What about an |
So it's to be x86-style prefixes then? 😁 |
I agree with @titzer's approach, and I think we'll likely see more examples of these sorts of "modifying" instructions. The |
Similarly, there was earlier discussion on defining exception-throwing versions of trapping numeric operations (like division by 0). Are we ready to open the can of worms? |
Similarly, many of the atomic operations could have been expressed by using an |
The no-op |
I started drafting a repo for this proposal here: https://github.com/yuri91/branch-hinting There is a very brief overview, and a folder with a minimal benchmark. The benchmark uses the Switching from a normal Any feedback is appreciated. |
I think hinting should be more general and use probability similar to (module
(func $foo (param $x i32) (result i32)
(if (result i32)
(expect value=1 probability=70
(i32.eqz
(local.get $x)
)
)
i32.const 1
i32.const 2
)
)
) |
branch-hinting became an official proposal and is now a standardized, so we can now close this issue! |
As you may be aware, Leaning Technologies, is working on a Wasm powered x86 virtualization product named CheerpX, which is in an advanced stage of development. I have added further reading below if anybody is interested in more context.
We generate optimized Wasm bytecode Just-In-Time, and it is fairly common to have code like the following:
Due to the condition involved, the if body is executed very rarely (e.g. <1% probability). It would be advantageous to provide information to the Wasm VM to let it make better decisions in these cases, in particular with regard to:
It also seems to us that branch hinting could be useful not only in our scenario, but also for other Wasm based VMs for dynamic languages or even PGO compiled C++ sources.
Before trying to develop a full proposal we wanted to gather feedback on the few approaches we have though about
if_hinted
), with an immediate parameter (ether 0/1) to indicate the direction of the hint. Possibly 2/-1 could be used to signal "no hint" or other semantics.if
opcode to also encode the hint. For example we could decide that hinting can only be used withvoid/0x40
block type, and encode the hints as0x41/0x42
. Apologies if those specific values are already reserved for other purposes, this is just intended as an example.assume
orexpect
which works as a pass-trough, consuming one value and pushing the same one afterwards. This could be used to effectively "annotate" or add metadata to a specific value on the stack. This metadata can then be used by a branch instruction to deduce the hint direction.From the implementation point of view this concept fit very well in V8, and we have hacked a prototype already. In particular V8 already internally support branch hints and "deferred" blocks which can be naturally used to implement this feature. We are willing to share a refined prototype after the first round of feedback, if there is interest from the community. We have not investigated what would be the implementation complexity in other engines, on the upside a "stub" implementation that correctly parses the information without acting on it seems to be not difficult in general.
============
Further reading about our technology (for context, should not be required for the discussion):
Presentation at Wasm SF Meetup: https://www.youtube.com/watch?v=7JUs4c99-mo (first 30sec of audio missing, apologies)
Python3 Demo: https://www.leaningtech.com/pages/pythondemo.html
https://medium.com/leaningtech/extreme-webassembly-1-pushing-browsers-to-their-absolute-limits-56a393435323
https://medium.com/leaningtech/preserving-flash-content-with-webassembly-done-right-eb6838b7e36f
https://medium.com/leaningtech/running-flash-in-webassembly-using-cheerpx-an-update-d500b6fbc44e
The text was updated successfully, but these errors were encountered: