Skip to content
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

Store long-lived toplevel values into globals #30

Merged
merged 2 commits into from
Mar 22, 2024
Merged

Store long-lived toplevel values into globals #30

merged 2 commits into from
Mar 22, 2024

Conversation

vouillon
Copy link
Collaborator

@vouillon vouillon commented Mar 12, 2024

Any variable which is used a number of instructions after being defined is stored into a global
instead of a local. The goals are the following:

  • Turn a large number of closures into constant closures, which has a significant impact on performance
  • Reduce the compilation time of the toplevel function in case the Wasm engine decide to optimize it: reduce the register pressure by avoiding long-lived registers in the toplevel function, and make load elimination less expensive by reducing the number of constant structures defined in this function.
    This also improves binaryen's compilation time.

We could generate invalid code for `Obj.set_field x 0 (Obj.repr f)` in
`Lazy.from_fun` (OCaml 4.14) when `f` is a known function.
number of instructions after being defined is stored into a global
instead of a local. The goals are the following:
- Turn a large number of closures into constant closures, which has a
  significant impact on performance
- Reduce the compilation time of the toplevel function in case the
  Wasm engine decide to optimize it: reduce the register pressure by
  avoiding long-lived registers in the toplevel function, and make
  load elimination less expensive by reducing the number of constant
  structures defined in this function.
@OlivierNicole
Copy link
Collaborator

So if I understand correctly, after this pass no function will use more than 1,000 locals?

@vouillon
Copy link
Collaborator Author

This is only for the toplevel function. And we are counting 1000 instructions in the intermediate code, not in the generated code. So this does not put an exact limit on the number of locals. And wasm-opt might not be able to coalesce some variables since it has refined their types.
When compiling the partial-render-table the number of local variables goes from about 1000 to about 100 in the optimized code (from 10000 to 4500 in the wat file produced by wasm_of_ocaml). There are certainly a lot less that are simultaneously live.

@OlivierNicole
Copy link
Collaborator

I see.

@vouillon vouillon merged commit 5c60fb8 into main Mar 22, 2024
4 checks passed
@vouillon vouillon deleted the globalize branch March 22, 2024 14:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants