Skip to content

Conversation

@xdoardo
Copy link
Contributor

@xdoardo xdoardo commented Apr 9, 2025

(closes #5493 #5494)

This PR adds the --enable-pass-params-opt flag to the CLI and the enable_pass_params_opt() function to the LLVM compiler.

This optimization is turned off by default. It assumes that the Wasm module to compile has both a global (g0) and a memory (m0) defined in it. The objective of the optimization is that of passing the value of g0 and a pointer to the base of m0 as parameters between guest functions, in order to reduce the overhead of loading these values through (multiple) pointers (i.e., loads in the generated code).

Pitfalls

In some cases this optimization works very well, speeding up the execution times of modules such as php:

$ wasmer-dev run --registry=wasmer.io --llvm php/[email protected] --mapdir=./app:. -- app/bench.php
-------------------------------------------------------
|       PHP BENCHMARK SCRIPT v.2.0 by @SergiX44       |
-------------------------------------------------------
     ...
-------------------------------------------------------
Total time.................................... 5.0362 s
Peak memory usage................................ 0 MiB

vs

$ wasmer-dev run --registry=wasmer.io --llvm --enable-pass-params-opt php/[email protected] --mapdir=./app:. -- app/bench.php
-------------------------------------------------------
|       PHP BENCHMARK SCRIPT v.2.0 by @SergiX44       |
-------------------------------------------------------
    ...
Total time.................................... 2.9245 s
Peak memory usage................................ 0 MiB

In some other cases, passing g0 and m0 as parameters can be actually an hindrance, making the execution slower. In my opinion, this is due to the fact that g0 and m0 are passed as second and third parameters to generated guest functions, which might cause other parameters to be spilled onto the stack. Thus, depending on which of g0, m0 or the other spilled parameters is used more, this optimization might actually slow things down.

@promptless
Copy link

promptless bot commented Apr 22, 2025

📝 Documentation updates detected! A separate PR for documentation updates has been made here: wasmerio/docs.wasmer.io#126

@xdoardo xdoardo enabled auto-merge April 23, 2025 11:12
@xdoardo xdoardo linked an issue Apr 23, 2025 that may be closed by this pull request
@xdoardo xdoardo merged commit d07093b into main Apr 23, 2025
83 of 84 checks passed
@xdoardo xdoardo deleted the llvm-pass-params branch April 23, 2025 12:23
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.

Pass memory 0 by parameter Pass global 0 by parameter

3 participants