The incoming branch (now 1.36.1) now has a new optimization when building
to JS with -Oz. It will eliminate C++ global constructor functions
aggressively, removing them from the codebase, removing the need to call
them during startup, and removing code that would otherwise be used only by
them.

This makes -Oz when compiling to JS slower to compile than before, almost
2x slower. To avoid that, you can disable this optimization (-s
EVAL_CTORS=0), or just use -Os. In general, -Oz is kind of the "try at all
costs to reduce code size", so it felt natural to include this optimization
there.

The benefit can be noticeable. For example, this removes the 2 global ctors
that doing any C++ iostream usage would normally bring in, that create the
standard streams. This reduces code size by a few percent, as well as JS
compilation time, and startup is faster also because we can jump right to
executing main(). In general, of course, we can't remove all ctors, as it
might do something with side effects like printf or malloc, which we can't
optimize away. With EMCC_DEBUG=1 in the env, you'll see logging that shows
an error in such a case (which you can use to optimize your codebase, if
you want).

This optimization was inspired by Cheerp's PreExecutor (
http://blog.leaningtech.com/2016/02/cheerp-preexecuter-compile-time.html ).
That made me wonder, doesn't LLVM already do this? Turns out, yes, it does,
but at the IR level, and as a result is not as successful as it could be,
due to the complexity of LLVM IR. But at the asm.js level things are very
simple - in fact, this optimization just literally runs the code in a JS
sandbox, and sees if it ran without using anything nondeterministic. That's
after all the LLVM complexity was lowered out, and is basically guaranteed
to work when it should work. More details at

https://github.com/kripken/emscripten/blob/incoming/src/settings.js#L699

- Alon

-- 
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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to