-
Notifications
You must be signed in to change notification settings - Fork 58
Investigate hidden binaryen workarounds #31
Comments
Could you bring this to the attention of the Binaryen devs? |
@mboes Other compilers based on binaryen may already be aware of some workarounds and even relying on them, so the switch shall still be inevitable. Still, yes, I'll factor out a short experience summary and "what to fix" list as soon as getting out of this minefield. Some should be doc enhancements, other in code. |
Related discussion: WebAssembly/binaryen#903 The only remaining obstacle is sorting out the mess of binaryen vs wasm type system. |
One interesting observation: when we re-enable binaryen's type inference for blocks, the |
Findings after some more late night binaryen hacking: The code implementing the Still, the current type-related mess in migration to new backend reflects some flaws in expression IR:
So here is a mini roadmap for the way out:
|
Progress on this front: A naive port of binaryen's
And when marshalling a This makes V8 validates the module happily, but traps at runtime, so we're emitting more Meanwhile, it seems after masking the function body of |
Type error is somewhere near https://github.com/tweag/asterius/blob/wip-wasm-3/asterius/src/Asterius/Builtins.hs#L976. We're pretty close. |
After disabling It's still an amazement how binaryen managed to defer the type errors to runtime, and since the scheduler didn't need to handle |
Sounds reasonable. |
@mchakravarty @mboes There are several possible options regarding how to handle the binaryen backend:
Any thoughts on this? |
Let's scratch option (1): it will hardly be discoverable by others who want to revive the binaryen bindings and not start from zero. We can either move these to separate repo or keep it here. Either way, don't you think it will be useful to keep this code path for debugging purposes? If so that could even justify continuing to maintain it, at least while the alternative code path matures. |
@mboes Makes sense. I've already enabled the new backend as default while providing a switch to fall back to the binaryen codegen. Will soon land in |
When comparing binary code emitted by binaryen/new backend, I discovered that binaryen doesn't simply do a post-order traversal on the expression IR as expected; there are quite some hidden transformations/workarounds here and there, which are mostly undocumented and there's no way to opt out even when optimize/shrink level is set to 0. A non-comprehensive list including workarounds I discovered so far:
locals
buckets appear in a function definition. Non-parameter local indices don't correspond to input expression IR (although there does exist a bijection)unreachable
instructions not present in the original IR.I've patched binaryen and shrank the list above to only contain the last entry. As long as the last piece is fit into the jigsaw puzzle, we'll base everything on the new backend right away.
We're not accusing binaryen's deficiencies here (though the caveats probably should be mentioned in docs); however, this debugging experience does reveal some additional mismatches between what binaryen offers and what we expect:
unreachable
instruction in quite some places. Hand it a mal-formed expression IR, it uses this trick to make the IR automagically pass validation...The text was updated successfully, but these errors were encountered: