emscripten-core / emscripten-core/emscripten

Missed juicy code size optimization with -sEXIT_RUNTIME=0

Aperta
#17,872 9 commenti 0 reazioni 0 assegnatari Vedi su GitHub
code size
Lingua principale
C++
Stelle
27.6k
Fork
3.6k
Merge medio
1g 14h
PR unite (30g)
125

Descrizione

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?

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Inizia riproducendo l'esempio in a.cpp con `em++ a.cpp -o a.js -Oz -g3 -sEXIT_RUNTIME=0`, quindi ispeziona il wasm generato cercando `__cxa_atexit` e `__cxx_global_array_dtor`. Il lavoro è completato quando la registrazione del distruttore inutilizzato e il codice del distruttore vengono omessi con `EXIT_RUNTIME=0`, mentre il comportamento a runtime rimane corretto.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
cpp, wasm
Ambito
compilers, performance
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.