2 ms·
If I remember correctly, the main reason why Wasm has structured control flow rather than old-school arbitrary jumps is not because you can optimize the former
by int_19h 3y ago
If I remember correctly, the main reason why Wasm has structured control flow rather than old-school arbitrary jumps is not because you can optimize the former better in general, but specifically because it enables efficient JIT streaming compilation with simultaneous code verification (for sandboxing), which was deemed important for web use cases.
Simply put, wasm is optimized to allow JITs to be simpler, at the expense of making it harder to write compilers for it.
- kevingadd 3y agoThe structured control flow also specifically makes secure compilation much easier. If you want to do something like CFI, it's easy since you know where all your branches and branch targets are and who can reach them.
- int_19h 3y agoYes, that is the "verification" part I mentioned. With VMs like Java or .NET, the compiler has to infer essentially the same graph from unstructured labels and jumps.
- Findecanor 3y agoI find that being guaranteed to have only structured control flow, and having loops and if-else constructs marked as such in advance could also simplify a AOT-compiler a little. If you're doing liveness analysis, you wouldn't have to first deduce which edges are unstructured jumps and which are loop edges. If you're trying to find more opportunities for if-conversion, you wouldn't have to first deduce which edges those are.
- int_19h 3y agoI should add, however, that the unmentioned elephant in the room is V8 JIT (TurboFan), which simply doesn't handle irreducible control flow. While there are some valid theoretical arguments around the current arrangement in Wasm, looking at the history of the associated discussions makes it pretty obvious that having V8 support Wasm and generate fast code similar to what it can do for asm.js was an overriding concern in many cases. And Google straight up said that if Wasm has ICF, they will not bother supporting such cases, so it will be done by a much slower fallback: https://github.com/WebAssembly/design/issues/796#issuecomment-298268883 https://github.com/WebAssembly/design/issues/796#issuecommen... AFAIK no other Wasm implementation has the same constraint - the rest generally tend to desugar everything to (potentially irreducible) CFG and then proceed from there. So this is, at least to some extent, yet another case of a large company effectively forcing an open standard to be more convenient for them specifically.