emscripten-core / emscripten-core/emscripten

__wasm_call_ctors is also emitted into C programs, and not optimized out in C++ programs if there are no ctors to call.

Open
#12,777 2 comments 0 reactions 1 assignee Claimed by @kripken View on GitHub
wontfix
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

STR:

```
emcc tests\minimal_hello.c -o a.html -s MINIMAL_RUNTIME=1 -s ENVIRONMENT=web -s SUPPORT_ERRNO=0 -s MINIFY_HTML=0 -s TEXTDECODER=2 -O3 -g1 --closure 1 --profiling-funcs
```

Will generate

```wast
(module
(type $none_=>_none (func))
(type $i32_=>_none (func (param i32)))
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
(import "a" "a" (memory $1 256 256))
(data (i32.const 1024) "(char* str)<::>{ console.log(UTF8ToString(str)); }\00minimal hello!")
(import "a" "b" (func $console_log (param i32)))
(table $0 1 1 funcref)
(export "c" (table $0))
(export "d" (func $__wasm_call_ctors))
(export "e" (func $main))
(func $main (; 1 ;) (param $0 i32) (param $1 i32) (result i32)
(call $console_log
(i32.const 1075)
)
(i32.const 0)
)
(func $__wasm_call_ctors (; 2 ;)
(nop)
)
)
```
and
```js
var c;
c || (c = Module);
var d = new TextDecoder("utf8");
function e(a) {
if (!a) {
return "";
}
for (var k = a + NaN, b = a; !(b >= k) && f[b];) {
++b;
}
return d.decode(f.subarray(a, b));
}
var g = new WebAssembly.Memory({initial:256, maximum:256}), f = new Uint8Array(g.buffer), h;
WebAssembly.instantiate(c.wasm, {a:{b:function(a) {
console.log(e(a));
}, a:g}}).then(function(a) {
a = a.instance.exports;
h = a.e;
a.d();
h();
});
```
There is a call to `a.d();` in the generated JS file at it comes from the preamble without understanding of the number of wasm ctors, but that is a no-op, so could be optimized away.

This is a regression compared to fastcomp btw, there we dynamically emitted the ctor invocations (or grouped them into one call), which ended up not emitting a dummy function if there were no ctors.

Also the program is a C program, so there should not be any ctors to begin with? It would be nice for this to optimize out automatically.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.