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

USE_PTHREADS=1 fails with LLVM ERROR: Cannot select ... #5956

Closed
chrisber opened this issue Dec 20, 2017 · 4 comments
Closed

USE_PTHREADS=1 fails with LLVM ERROR: Cannot select ... #5956

chrisber opened this issue Dec 20, 2017 · 4 comments

Comments

@chrisber
Copy link

chrisber commented Dec 20, 2017

After the latest merge of the pull request Initial support for pthreads with wasm #5710.
I was curious about thread support in webassembly and tried to compile the the following example cppreference storage duration , but compiling and executing with thread support only succeeded with asm as target.
Is thread support with the native webassembly backend currently limited or did I make a mistake?

emsdk sdk-incoming-64bit compiled from source (emscripten-core/emsdk@d3a5609)
emscripten/incoming (1754e32)
clang/fastcomp/src (emscripten-core/emscripten-fastcomp@7153769)
Firefox nightly Version 59.0a1

Example.cc:

#include <iostream>
#include <string>
#include <thread>
#include <mutex>
 
thread_local unsigned int rage = 1; 
std::mutex cout_mutex;
 
void increase_rage(const std::string& thread_name)
{
    ++rage; // modifying outside a lock is okay; this is a thread-local variable
    std::lock_guard<std::mutex> lock(cout_mutex);
    std::cout << "Rage counter for " << thread_name << ": " << rage << '\n';
}
 
int main()
{
    std::thread a(increase_rage, "a"), b(increase_rage, "b");
 
    {
        std::lock_guard<std::mutex> lock(cout_mutex);
        std::cout << "Rage counter for main: " << rage << '\n';
    }
 
    a.join();
    b.join();
}

Build from source for asm :

 git clone https://github.com/juj/emsdk.git
 cd emsdk
./emsdk install sdk-incoming-64bit binaryen-master-64bit
./emsdk activate sdk-incoming-64bit binaryen-master-64bit
. ./emsdk_env.sh
emcc -v -std=c++11  -01 \
-s USE_PTHREADS=1 \
-s PROXY_TO_PTHREAD=1 \
-s VERBOSE=1 \
example.cc -o example.html

Compiles and runs successfull when optimization is set to-01 or above (fails with -O0).

Program output:

Preallocating 1 workers for a pthread spawn pool.
Preallocating 1 workers for a pthread spawn pool.
Preallocating 1 workers for a pthread spawn pool.
Rage counter for main: 1
Rage counter for a: 2
Rage counter for b: 3

Build from source with asm2wasm backend:

 git clone https://github.com/juj/emsdk.git
 cd emsdk
 ./emsdk install sdk-incoming-64bit binaryen-master-64bit
 ./emsdk activate sdk-incoming-64bit binaryen-master-64bit
 . ./emsdk_env.sh
 DEBUG=1 emcc -v -std=c++11   \
-s USE_PTHREADS=1 \
-s PROXY_TO_PTHREAD=1 \
-s WASM=1 \
-s VERBOSE=1 \
example.cc -o example.html

Compiles but the program is not executable.

Build from source with native wasm backend:

 git clone https://github.com/juj/emsdk.git
 cd emsdk
./emsdk install --enable-wasm sdk-incoming-64bit binaryen-master-64bit
./emsdk activate --enable-wasm sdk-incoming-64bit binaryen-master-64bit
. ./emsdk_env.sh
DEBUG=1 EMCC_WASM_BACKEND=1 emcc -v -std=c++11   \
-s USE_PTHREADS=1 \
-s PROXY_TO_PTHREAD=1 \
-s WASM=1 \
-s VERBOSE=1 \
example.cc -o example.html

Compilation fails with error:

LLVM ERROR: Cannot select: 0x55d8f3285d00: i32 = GlobalTLSAddress<i32* @rage> 0 In function: _ZTW4rage

It seems like an issue with the statement thread_local unsigned int rage = 1;

@juj
Copy link
Collaborator

juj commented Dec 20, 2017

The missing piece here is the thread_local keyword, which is not yet supported in our multithreading work. This is on the TODO list though, since it's quite an important one. Thanks for the test case!

@trailcode
Copy link

I am having the same problem. If I compile with -s WASM=1 it does not work.

@stale
Copy link

stale bot commented Jan 13, 2020

This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 7 days. Feel free to re-open at any time if this issue is still relevant.

@stale stale bot added the wontfix label Jan 13, 2020
@tlively tlively removed the wontfix label Jan 13, 2020
@tlively
Copy link
Member

tlively commented Jan 13, 2020

Actually, this should be fixed now that we support thread_locals.

@tlively tlively closed this as completed Jan 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants