emscripten-core / emscripten-core/emscripten

emcc produces wrong code at -O

Open
#18,767 7 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

Hi,
In this case, emcc produces wrong code at -O1(also at -Os), but both gcc(Ubuntu 9.4.0-1ubuntu1~20.04.1) and clang(version 10.0.0-4ubuntu1) produce correct target program at the same optimization level.
Does emcc supports a pointer to a 64-bit integer?
If this program contains undefined behaviour, I earnestly ask for your forgiveness.
Hope to get your reply.

**Version of emscripten/emsdk:**
```shell
$ emcc -v
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.31 (e88336121cfe6da4a96c88e46f314552f07dfed0)
clang version 17.0.0 (https://github.com/llvm/llvm-project 1142e6c7c795de7f80774325a07ed49bc95a48c9)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /home/x/emsdk/upstream/bin
```

**Program**
```c
#include
#include

union {
uint64_t c;
int32_t a;
} d;
int32_t a = 0;
int32_t e = 0;
uint64_t *f = &d.c;
static int32_t *g = &d.a;

int main() {
for (; e <= 0; e++) {
*f = 0x3b47c02bffe;
for (; a; a++) {
*g = 2;
}
}
printf("%llx\n", d.c);
}
```

**Failing command line in full:**
```shell
$ emcc -Os small.c -o small.wasm; wasmtime small.wasm
3b400000000
$ emcc -Os small.c -o small.wasm; wasmedge small.wasm
3b400000000
$ emcc -O1 small.c -o small.wasm; wasmtime small.wasm
3b400000000
$ emcc -O0 small.c -o small.wasm; wasmtime small.wasm
3b47c02bffe
```

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.