emscripten-core / emscripten-core/emscripten
A bug report: possible under-optimization issue
- Dominant language
- C++
- Stars
- 27.6k
- Forks
- 3.6k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 105
Description
The minimal code example:
```C
#include
#include
static volatile int32_t g_3 = 0x443EB606L;
static int32_t g_5 = 1L;
static uint32_t foo() {
for (g_5 = 8; (g_5 >= 0); g_5 -= 1)
for (g_3 = 0; g_3 < 9; g_3 += 1)
;
}
int main(int argc) {
foo();
printf("%d\n", g_3);
printf("%d\n", g_5);
}
```
**Version of emscripten/emsdk:**
```
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.14 (4343cbec72b7db283ea3bda1adc6cb1811ae9a73)
clang version 15.0.0 (https://github.com/llvm/llvm-project 7effcbda49ba32991b8955821b8fdbd4f8f303e2)
Target: wasm32-unknown-emscripten
Thread model: posix
```
**Compilation option:**
`emcc -O3 -g -w test.c -o test.wasm -o test.js`
**Compiled WASM code (in text format):**
Extracted with [wabt](https://github.com/WebAssembly/wabt)
[test.txt](https://github.com/emscripten-core/emscripten/files/9275220/test.txt)
**Bug Description:**
The output of the WASM code is as expected (`9 -1`). However, even with the `O3` option, emcc seems failed to reach the best-optimized code. If compile the above C code with `gcc -O3` (version 11.2.0), all code in function `foo` is optimized away. But `emcc -O3` only inlines the `foo` function, most calculation and assignment in `foo` is still kept.
As WASM emphasizes the performance on the web browsers, and as emcc is the most mature WASM compiler, although such an under-optimization issue does not incur severe problems (no functionality error), I think it is still worth a fixing?
Contributor guide
Assessment
This issue has not been assessed yet.