Page MenuHomePhabricator

[WebAssembly] Emit br_table for most switch instructions

Authored by aheejin on Apr 22 2019, 7:47 AM.



Always convert switches to br_tables unless there is only one case,
which is equivalent to a simple branch. This reduces code size for wasm,
and we defer possible jump table optimizations to the VM.
Addresses PR41502.

Diff Detail


Event Timeline

aheejin created this revision.Apr 22 2019, 7:47 AM
aheejin marked an inline comment as done.Apr 22 2019, 7:51 AM
aheejin added inline comments.
649 ↗(On Diff #196073)

@sunfish There are some CHECK-NOTs that I don't know how to preserve here, given that the structure has completely changed. Any ideas? Or do you think it's OK not preserving these?

The theory here is that br_table represents the performance characteristics of a jump table, while br_if represents the performance characteristics of a branch. In hardware, a small switch with 3 cases is often more efficient with a couple of conditional branches than a jump table.

aheejin updated this revision to Diff 196101.Apr 22 2019, 11:11 AM
  • v8 -> VM
aheejin edited the summary of this revision. (Show Details)Apr 22 2019, 11:11 AM
kripken accepted this revision.Apr 22 2019, 11:57 AM

I'm in favor of this. There are a bunch of tradeoffs that VMs can make, and this lets them make their own choices, by emitting the higher-level construct, which is also smaller in size in the wasm.

This revision is now accepted and ready to land.Apr 22 2019, 11:57 AM
This revision was automatically updated to reflect the committed changes.
dschuff added inline comments.Apr 24 2019, 10:29 AM

Might be worth putting a TODO in here or somewhere to investigate using if/else instead once we have support for that?

aheejin marked an inline comment as done.Apr 24 2019, 12:30 PM
aheejin added inline comments.

In PR41502, @kripken said using if/else generates bigger code too. If we are doing this for code size, do we need that TODO?