wasm-split to multiple modules

435 views
Skip to first unread message

Александр Гурьянов

unread,
Apr 21, 2021, 6:56:48 AM4/21/21
to emscripte...@googlegroups.com
Hi.I have a custom optimization layer that helps me to decrease wasm binary size. It is a sort of PGO optimization, I have a statistic about each function in wasm - how many times it called. Then I remove functions with zero invocations from wasm and generate js version of same function to invoke if it suddenly needed (1 js file - 1 removed function). This optimization works very good except it uses js which is slow and big.

Now I want to generate small wasm modules (<4Kb) instead of js versions. I thought that wasm-split can help, but I realized that it generates a big module that can't be load in synchronous way. Moreover there is another problem, it replaces all placeholders with actual implementation on instantiation. But, I don't want to replace placeholders because they continue to track call stats (this is minor problem, I know workaround for it).

Is there some way to force wasm-split to generate multiple modules (1 per function). Something like this:

  std::unique_ptr<Module> fn1 =
    ModuleSplitting::splitFunctions(wasm, <ALL FUNCTIONS - fn1>);
  std::unique_ptr<Module> fn2 =
    ModuleSplitting::splitFunctions(wasm, <ALL FUNCTIONS - fn1 - fn2>);

...

  ModuleWriter writer;
  writer.setBinary(options.emitBinary);
  writer.setDebugInfo(options.passOptions.debugInfo);
  writer.write(wasm, options.primaryOutput);
  writer.write(*fn1, wasmFn1);
  writer.write(*fn2, wasmFn2);

Or if it's not possible, what do you think is it doable and how hard is it?

Thomas Lively

unread,
Apr 21, 2021, 1:33:08 PM4/21/21
to emscripte...@googlegroups.com
Yes, the code pattern you suggest should be able to split off each function into a separate module without any problems. I would recommend configuring a different `placeholderNamespace` for each module you split off to make it easy to tell which module you will need to instantiate to get the proper replacement. In principle you could also do this by invoking wasm-split multiple times instead of writing a custom tool. If you want to continue tracking calls after the replacement module has been instantiated, you should do that using a separate mechanism rather than using wasm-split's placeholder functions, since it's a core assumption of wasm-split that the placeholder functions will be entirely replaced.

Do note that Emscripten has no support for this kind of custom splitting, so you'll be on your own for making the JS load and instantiate the individual modules at runtime.


--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/CAKOm%3DVGzr8Wr_MR4733AkKnkkyoq2ainU6kzsS9dOkVuXki7NQ%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages