Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable threading in libcxx in all cases #498

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ArcaneNibble
Copy link

This has not been tested locally! I don't have enough disk space to store yet another LLVM tree, and I couldn't quite figure out how to test only the sysroot changes, so I'm hoping the CI pipeline can test this

This enables threads in libcxx for all builds, building on top of the stubs for libpthread

@alexcrichton
Copy link
Collaborator

alexcrichton commented Oct 30, 2024

I kicked off the CI builds and it looks like they're failing with:

2024-10-30T15:12:55.2446007Z [1249/1301] Linking CXX shared library lib/libc++.so�[K
2024-10-30T15:12:55.2447078Z [1250/1301] Linking CXX shared library lib/libc++.so�[K
2024-10-30T15:12:55.2447920Z �[31mFAILED: �[0mlib/libc++.so 
2024-10-30T15:12:55.2485184Z : && /src/build/install/bin/clang++ --sysroot=/src/build/sysroot/install/share/wasi-sysroot -fPIC -D_WASI_EMULATED_PTHREAD --target=wasm32-wasi -fdebug-prefix-map=/src=wasisdk://v24.13g1e06ec8b881e --sysroot /src/build/sysroot/install/share/wasi-sysroot -resource-dir /src/build/sysroot/install/wasi-resource-dir -fvisibility-inlines-hidden -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -Wimplicit-fallthrough -Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wsuggest-override -Wstring-conversion -Wmisleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -ffunction-sections -fdata-sections -O2 -g -DNDEBUG  -Wl,-z,defs -Wl,--color-diagnostics -shared  -o lib/libc++.so libcxx/src/CMakeFiles/cxx_shared.dir/algorithm.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/any.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/bind.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/call_once.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/charconv.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/chrono.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/error_category.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/exception.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/expected.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/filesystem/filesystem_clock.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/filesystem/filesystem_error.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/filesystem/path.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/functional.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/hash.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/legacy_pointer_safety.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/memory.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/memory_resource.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/new_handler.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/new_helpers.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/optional.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/print.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/random_shuffle.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/ryu/d2fixed.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/ryu/d2s.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/ryu/f2s.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/stdexcept.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/string.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/system_error.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/typeinfo.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/valarray.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/variant.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/vector.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/verbose_abort.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/atomic.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/barrier.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/condition_variable_destructor.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/condition_variable.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/future.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/mutex_destructor.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/mutex.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/shared_mutex.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/thread.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/random.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/fstream.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/ios.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/ios.instantiations.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/iostream.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/locale.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/ostream.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/regex.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/strstream.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/filesystem/directory_entry.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/filesystem/directory_iterator.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/filesystem/operations.cpp.o libcxx/src/CMakeFiles/cxx_shared.dir/filesystem/int128_builtins.cpp.o  -nostdlib++  lib/libc++abi.so  -lpthread  -lc && :
2024-10-30T15:12:55.2518305Z wasm-ld: �[0;35mwarning: �[0munknown -z value: defs
2024-10-30T15:12:55.2519505Z wasm-ld: �[0;35mwarning: �[0mcreating shared libraries, with -shared, is not yet stable
2024-10-30T15:12:55.2521231Z wasm-ld: �[0;31merror: �[0mlibcxx/src/CMakeFiles/cxx_shared.dir/thread.cpp.o: undefined symbol: pthread_join
2024-10-30T15:12:55.2523110Z wasm-ld: �[0;31merror: �[0mlibcxx/src/CMakeFiles/cxx_shared.dir/thread.cpp.o: undefined symbol: pthread_detach
2024-10-30T15:12:55.2524881Z clang++: �[0;1;31merror: �[0m�[1mlinker command failed with exit code 1 (use -v to see invocation)�[0m
2024-10-30T15:12:55.3378068Z 

I think the failure is due to the fact that -lwasi-emulated-pthread isn't passed. With the -D flag passed the -l flag also needs to be passed to link in the dummy libraries. I'm not sure how best to manage that in the build here since if libcxx is built with the -D flag then it will always require -lwasi-emulated-pthread which probably isn't what users of libcxx or C++ in general expect.

@sunfishcode
Copy link
Member

Could you do some measurements of the code size impact of this change? For a basic C++ program that doesn't use threads, how much code size difference is there between threading disabled in the libcxx build vs. threading stubbed out in libc?

@ArcaneNibble
Copy link
Author

As far as I understand it, wasi-emulated-pthread should only be needed if the program actually ends up referencing pthread_{create|detach|join}. However, this wouldn't work if you're building a .so (TIL that that was even possible on wasm), and I'm also not sure how to deal with it either.

@ArcaneNibble
Copy link
Author

I just compared the size difference between a C++ std::cout hello world using <some random wasi sysroot I happened to have lying around on my computer, that should've hopefully come from an official release> against <a different random sysroot that definitely contains both the threading stubs in wasi-libc as well as a manually-recompiled version of libc++ with threading enabled> (these are not built from the same codebase, and they definitely weren't built by the same version of llvm either) and

the version with stubs and threading in libc++ is actually smaller (223K --> 183K)

this is not at all a fair comparison, but the real different is probably minor?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants