emscripten-core / emscripten

Emscripten: An LLVM-to-WebAssembly Compiler
Other
25.63k stars 3.29k forks source link

pthread undeclared function atomic_load #20932

Open uvguy opened 9 months ago

uvguy commented 9 months ago

Please include the following in your bug report:

Version of emscripten/emsdk: emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.42-git Debian clang version 17.0.6 (++20231208085813+6009708b4367-1~exp1~20231208085906.81) Target: wasm32-unknown-emscripten Thread model: posix InstalledDir: /usr/bin

this was using arm64 proot debian termux I could not use unmaintained emsdk arm64 port.

Full link command and output with -v appended: emcc: error: '/usr/bin/clang -target wasm32-unknown-emscripten -fignore-exceptions -fvisibility=default -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -D__EMSCRIPTEN_SHARED_MEMORY__=1 -D__EMSCRIPTEN_WASM_WORKERS__=1 -Werror=implicit-function-declaration --sysroot=/opt/tools/emscripten-3.1.42/cache/sysroot -Xclang -iwithsysroot/include/fakesdl -Xclang -iwithsysroot/include/compat -O2 -Wall -Werror -fno-unroll-loops -std=c99 -D_XOPEN_SOURCE=700 -Wno-unused-result -Os -fno-inline-functions -fno-builtin -Wno-ignored-attributes -Wno-macro-redefined -Wno-shift-op-parentheses -Wno-string-plus-int -Wno-missing-braces -Wno-logical-op-parentheses -Wno-bitwise-op-parentheses -Wno-unused-but-set-variable -Wno-unused-variable -Wno-unused-label -Wno-pointer-sign -g3 -I/opt/tools/emscripten-3.1.42/system/lib/libc/musl/src/internal -I/opt/tools/emscripten-3.1.42/system/lib/libc/musl/src/include -I/opt/tools/emscripten-3.1.42/system/lib/libc -I/opt/tools/emscripten-3.1.42/system/lib/pthread -pthread -DNDEBUG -c -matomics -mbulk-memory /opt/tools/emscripten-3.1.42/system/lib/pthread/library_pthread.c -o /opt/tools/emscripten-3.1.42/cache/build/libc-mt-tmp/library_pthread.o' failed (returned 1) ninja: build stopped: subcommand failed

compile this someone test or even minimzed repro emcc test/test_system.c -s USE_PTHREADS=1 -o test.html which yields following error

emcc: error: Subprocess 4/1108 failed (returned 1)! (cmdline: /opt/tools/emscripten-3.1.42/emcc -O2 -Wall -Werror -fno-unroll-loops -std=c99 -D_XOPEN_SOURCE=700 -Wno-unused-result -Os -fno-inline-functions -fno-builtin -Wno-ignored-attributes -Wno-macro-redefined -Wno-shift-op-parentheses -Wno-string-plus-int -Wno-missing-braces -Wno-logical-op-parentheses -Wno-bitwise-op-parentheses -Wno-unused-but-set-variable -Wno-unused-variable -Wno-unused-label -Wno-pointer-sign -g -sSTRICT -I/opt/tools/emscripten-3.1.42/system/lib/libc/musl/src/internal -I/opt/tools/emscripten-3.1.42/system/lib/libc/musl/src/include -I/opt/tools/emscripten-3.1.42/system/lib/libc -I/opt/tools/emscripten-3.1.42/system/lib/pthread -pthread -sWASM_WORKERS -c /opt/tools/emscripten-3.1.42/system/lib/pthread/thread_mailbox.c -o /opt/tools/emscripten-3.1.42/cache/build/libc-mt-debug-tmp/thread_mailbox.o) root@localhost:/opt/tools/emscripten-3.1.42# /opt/tools/emscripten-3.1.42/system/lib/pthread/em_task_queue.c:220:3: error: call to undeclared function 'atomic_compare_exchange_strong'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration] 220 | atomic_compare_exchange_strong( | ^ /opt/tools/emscripten-3.1.42/system/lib/pthread/em_task_queue.c:247:5: error: call to undeclared function 'atomic_exchange'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration] 247 | atomic_exchange(&queue->notification, NOTIFICATION_PENDING); | ^ 2 errors generated.

sbc100 commented 9 months ago

Are you trying to use the emscripten debian package here? If so, then this is probably a bug that should be filed against the debian package.

In general, emscripten doesn't work with stable versions of clang/llvm such as the one in /usr/bin/clang. You would need to build llvm from source and use that custom version of clang.

Why is that you we not able to use emsdk install latest-arm64-linux BTW? Its not that this is unmaintained, just the it doesn't get updated as often as the other builds. The current version there is 3.1.47 which looks more recent than the version you are trying use (3.1.42).

sbc100 commented 9 months ago

The reason em_task_queue.c does not compile here is that it depends on stdatomic.h which is a header that is provided by the compiler and not by libc. It seems that whatever stdatomic.h is shipped with the version of clang that you are using is not recent enough to include atomic_exchange.

sbc100 commented 9 months ago

Thats odd though since on my system even /usr/lib/llvm-16/lib/clang/16/include/stdatomic.h from clang 16 includes #define atomic_exchange(object, desired) __c11_atomic_exchange(object, desired, __ATOMIC_SEQ_CST).

If you want to figure it you would could take the failing command run it with -E to find out which version of stdatomic.h is being pulled in and why it is not defining atomic_exchange?

uvguy commented 9 months ago

Are you trying to use the emscripten debian package here?

No Im using apt.llvm.org repository build.

I can compile following test.c to the wasm obj file.

#include <stdio.h>
#include <stdatomic.h>

_Atomic struct A { int a[100]; } a;
_Atomic struct B { int x, y; } b;
int main(void)
{
    printf("_Atomic struct A is lock free? %s\n>
            atomic_is_lock_free(&a) ? "true" : >
    printf("_Atomic struct B is lock free? %s\n>
            atomic_is_lock_free(&b) ? "true" : >
}
clang -target wasm32-unknown-emscripten -fignore-exceptions -fvisibility=default -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -D__EMSCRIPTEN_SHARED_MEMORY__=1 -DEMSCRIPTEN -Wno-error=implicit-function-declaration --sysroot=/opt/tools/emscripten-3.1.42/cache/sysroot -Xclang -iwithsysroot/include/fakesdl -Xclang -iwithsysroot/include/compat -std=c89 -Wno-error -pthread test.c -c
root@localhost:/opt/proj/atom1# ls              out  test.c  test.o
root@localhost:/opt/proj/atom1# file test.o     test.o: WebAssembly (wasm) binary module version 0x1 (MVP)

how to adjust emscripten system pthread cflags to include c89 and adding -Wno-error=implicit-function-declaration ? I think this is about emscripten build config override issue.

here is output -E flags cmp.txt

sbc100 commented 9 months ago

We don't any any support for building emscripten libraries with anything but the latest LLVM version. You could edit tools/system_libs.py to modify the cflags used when building libc if you like? But be aware that its possible you could run into more issues down that line because we don't build or test using llvm 17.

Building with -Wno-error=implicit-function-declaration might work, but you really want to find out why those function are not being defined in your case.. they really should be defined. From the looks of the preprocessor output you attached it looks like /opt/tools/emscripten-3.1.42/cache/sysroot/include/c++/v1/ is being added to the include path somehow/somwhere, which is really shouldn't be when compiling C code. When I build locally I don't see this path on the include path. Can you confirm this by adding -v so that clang will print its internal include path?

If you take a look at /usr/lib/llvm-17/lib/clang/17/include/stdatomic.h it looks like it include the C++ version of stdatomic.h when __cplusplus is correctly defined.. but that should not be the case when building musl.