Conversation
Member
|
Couldn't it benefit js as well ? Allowing to optimize the loop .. |
Member
Author
That could benefit JS as well, indeed. This seems harder to implement, though. |
Wasm engines tier up (compile to optimised native code) functions that contain loops. For the toplevel/init function, which is large and called only once, this is wasteful. Extract contained loops into small helper functions so only the helpers get tiered up. The pass runs on the toplevel function after code generation. It extracts loops whose branches all stay within the loop body (no escaping Br, Return, or Rethrow). Variables are split into parameters (whose pre-loop values are needed) and locals (written before first read). Non-nullable ref locals are made nullable with RefCast on reads. Modified variables are returned.
Array bounds checks and division-by-zero checks use Br to jump to handler blocks set up by wrap_with_handlers. When those handler blocks are outside the loop, the Br escapes the loop and is_contained returns false. Wrapping the loop body with its own handlers keeps those Br targets inside the loop. Only done for the toplevel function (where loop hoisting applies).
…alues. Have the helper functions return a struct rather than multiple values, since this is better optimized by Binaryen.
needed_handlers used CFG-reachability from the start pc, which spills past the wrap's structural region: starting at a try body entry, the traversal followed Poptrap edges into post-try blocks; starting at a loop header, it could reach blocks not dominated by the loop header. Those checks live outside the wrap, so counting them caused dead handler blocks to be emitted inside the wrap. Walk the dominator subtree of the start pc instead, still skipping the try body when a Pushtrap is encountered (it has its own wrap). The function-level wrap is unaffected since the function entry dominates every block in the function.
Member
Author
|
Closed in favor of #2245 which works both for JavaScript and Wasm. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Wasm engines tier up (compile to optimised native code) functions that contain loops. For the toplevel/init function, which is large and called only once, this is wasteful. Extract contained loops into small helper functions so only the helpers get tiered up.