emscripten-core / emscripten-core/emscripten

Missed juicy code size optimization with -sEXIT_RUNTIME=0

Offen
#17,872 9 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
code size
Vorherrschende Sprache
C++
Sterne
27.6k
Forks
3.6k
Ø Merge
1 T. 14 Std.
Gemergte PRs (30 T.)
125

Beschreibung

Example code:
`a.cpp`
```c++
#include
#include

std::vector d;

int main()
{
// just some random stuff to prevent dead code elimination
for(int i = (int)emscripten_get_now(); i >= 0; --i)
d.push_back(i);
return d[d.size()/2];
}
```
Build with `em++ a.cpp -o a.js -Oz -g3 -sEXIT_RUNTIME=0`.

The generated code will emit the following global initializer for the std::vector instance in wasm:
```wast
(func $__cxx_global_var_init
(drop
(call $std::__2::vector>::vector\28\29
(i32.const 1684)
)
)
(drop
(call $__cxa_atexit
(i32.const 1)
(i32.const 0)
(i32.const 1024)
)
)
)
```
Note the `__cxa_atexit` call. This should not be present in the generated code, since the code is being built with `-sEXIT_RUNTIME=0`. This atexit directive is registering a destructor function pointer, which is present in the function pointer table as:

```
(elem (i32.const 1) $__cxx_global_array_dtor
```
which is
```wast
(func $__cxx_global_array_dtor (param $0 i32)
(drop
(call $std::__2::vector>::~vector\28\29
(i32.const 1684)
)
)
)
```
This dtor is likewise never called, since EXIT_RUNTIME=0.

I see that in Unity codebase and other codebases, there are quite a lot of these global dtors and `__cxa_atexit` directives piling up even when the codebase is built with `-sEXIT_RUNTIME=0`.

Would there be a good way to get rid of those when the runtime will never exit?

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Beginne damit, das Beispiel in a.cpp mit `em++ a.cpp -o a.js -Oz -g3 -sEXIT_RUNTIME=0` zu reproduzieren, und untersuche anschließend das generierte wasm auf `__cxa_atexit` und `__cxx_global_array_dtor`. Als abgeschlossen gilt die Aufgabe, wenn die ungenutzte Destruktorregistrierung und der Destruktorcode bei `EXIT_RUNTIME=0` weggelassen werden, während das Laufzeitverhalten korrekt bleibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
cpp, wasm
Bereich
compilers, performance
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.