Skip to content

[WebAssembly] Make WASI -threads environment behave as -pthread #129164

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

Merged
merged 1 commit into from
Mar 1, 2025

Conversation

ArcaneNibble
Copy link
Member

If the user specifies a target triple of wasm32-wasi-threads, then enable all of the same flags as if -pthread were passed. This helps prevent user error, as the whole point of selecting this target is to gain pthread support.

The reverse does not happen (passing -pthread does not alter the target triple) so as to not interfere with custom environments and/or custom multilib setups.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' labels Feb 28, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 28, 2025

@llvm/pr-subscribers-clang

Author: R (ArcaneNibble)

Changes

If the user specifies a target triple of wasm32-wasi-threads, then enable all of the same flags as if -pthread were passed. This helps prevent user error, as the whole point of selecting this target is to gain pthread support.

The reverse does not happen (passing -pthread does not alter the target triple) so as to not interfere with custom environments and/or custom multilib setups.


Full diff: https://github.com/llvm/llvm-project/pull/129164.diff

2 Files Affected:

  • (modified) clang/lib/Driver/ToolChains/WebAssembly.cpp (+16-4)
  • (modified) clang/test/Driver/wasm-toolchain.c (+6)
diff --git a/clang/lib/Driver/ToolChains/WebAssembly.cpp b/clang/lib/Driver/ToolChains/WebAssembly.cpp
index 93f17a03c580f..78cf8a1f6b343 100644
--- a/clang/lib/Driver/ToolChains/WebAssembly.cpp
+++ b/clang/lib/Driver/ToolChains/WebAssembly.cpp
@@ -69,6 +69,19 @@ static bool TargetBuildsComponents(const llvm::Triple &TargetTriple) {
          TargetTriple.getOSName() != "wasi";
 }
 
+static bool WantsPthread(const llvm::Triple &Triple, const ArgList &Args) {
+  bool WantsPthread = Args.hasFlag(options::OPT_pthread,
+                                   options::OPT_no_pthread,
+                                   false);
+
+  // If the WASI environment is "threads" then enable pthreads support
+  // without requiring -pthread, in order to prevent user error
+  if (Triple.isOSWASI() && Triple.getEnvironmentName() == "threads")
+    WantsPthread = true;
+
+  return WantsPthread;
+}
+
 void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA,
                                 const InputInfo &Output,
                                 const InputInfoList &Inputs,
@@ -150,14 +163,14 @@ void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA,
 
   AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
 
-  if (Args.hasArg(options::OPT_pthread))
+  if (WantsPthread(ToolChain.getTriple(), Args))
     CmdArgs.push_back("--shared-memory");
 
   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
     if (ToolChain.ShouldLinkCXXStdlib(Args))
       ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
 
-    if (Args.hasArg(options::OPT_pthread))
+    if (WantsPthread(ToolChain.getTriple(), Args))
       CmdArgs.push_back("-lpthread");
 
     CmdArgs.push_back("-lc");
@@ -292,8 +305,7 @@ void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs,
     CC1Args.push_back("-fno-use-init-array");
 
   // '-pthread' implies atomics, bulk-memory, mutable-globals, and sign-ext
-  if (DriverArgs.hasFlag(options::OPT_pthread, options::OPT_no_pthread,
-                         false)) {
+  if (WantsPthread(getTriple(), DriverArgs)) {
     if (DriverArgs.hasFlag(options::OPT_mno_atomics, options::OPT_matomics,
                            false))
       getDriver().Diag(diag::err_drv_argument_not_allowed_with)
diff --git a/clang/test/Driver/wasm-toolchain.c b/clang/test/Driver/wasm-toolchain.c
index 1ad219338ac28..91803fe6bc1f2 100644
--- a/clang/test/Driver/wasm-toolchain.c
+++ b/clang/test/Driver/wasm-toolchain.c
@@ -110,6 +110,12 @@
 // RUN:   | FileCheck -check-prefix=PTHREAD_NO_SIGN_EXT %s
 // PTHREAD_NO_SIGN_EXT: invalid argument '-pthread' not allowed with '-mno-sign-ext'
 
+// 'wasm32-wasi-threads' does the same thing as '-pthread'
+// RUN: %clang -### --target=wasm32-wasi-threads --sysroot=/foo %s 2>&1 \
+// RUN:  | FileCheck -check-prefix=WASI_THREADS %s
+// WASI_THREADS: "-cc1" {{.*}} "-target-feature" "+atomics" "-target-feature" "+bulk-memory" "-target-feature" "+mutable-globals" "-target-feature" "+sign-ext"
+// WASI_THREADS: wasm-ld{{.*}}" "--shared-memory" "-lpthread"
+
 // '-mllvm -emscripten-cxx-exceptions-allowed=foo,bar' sets
 // '-mllvm --force-attribute=foo:noinline -mllvm --force-attribute=bar:noinline'
 // RUN: %clang -### --target=wasm32-unknown-unknown \

@llvmbot
Copy link
Member

llvmbot commented Feb 28, 2025

@llvm/pr-subscribers-clang-driver

Author: R (ArcaneNibble)

Changes

If the user specifies a target triple of wasm32-wasi-threads, then enable all of the same flags as if -pthread were passed. This helps prevent user error, as the whole point of selecting this target is to gain pthread support.

The reverse does not happen (passing -pthread does not alter the target triple) so as to not interfere with custom environments and/or custom multilib setups.


Full diff: https://github.com/llvm/llvm-project/pull/129164.diff

2 Files Affected:

  • (modified) clang/lib/Driver/ToolChains/WebAssembly.cpp (+16-4)
  • (modified) clang/test/Driver/wasm-toolchain.c (+6)
diff --git a/clang/lib/Driver/ToolChains/WebAssembly.cpp b/clang/lib/Driver/ToolChains/WebAssembly.cpp
index 93f17a03c580f..78cf8a1f6b343 100644
--- a/clang/lib/Driver/ToolChains/WebAssembly.cpp
+++ b/clang/lib/Driver/ToolChains/WebAssembly.cpp
@@ -69,6 +69,19 @@ static bool TargetBuildsComponents(const llvm::Triple &TargetTriple) {
          TargetTriple.getOSName() != "wasi";
 }
 
+static bool WantsPthread(const llvm::Triple &Triple, const ArgList &Args) {
+  bool WantsPthread = Args.hasFlag(options::OPT_pthread,
+                                   options::OPT_no_pthread,
+                                   false);
+
+  // If the WASI environment is "threads" then enable pthreads support
+  // without requiring -pthread, in order to prevent user error
+  if (Triple.isOSWASI() && Triple.getEnvironmentName() == "threads")
+    WantsPthread = true;
+
+  return WantsPthread;
+}
+
 void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA,
                                 const InputInfo &Output,
                                 const InputInfoList &Inputs,
@@ -150,14 +163,14 @@ void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA,
 
   AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
 
-  if (Args.hasArg(options::OPT_pthread))
+  if (WantsPthread(ToolChain.getTriple(), Args))
     CmdArgs.push_back("--shared-memory");
 
   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
     if (ToolChain.ShouldLinkCXXStdlib(Args))
       ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
 
-    if (Args.hasArg(options::OPT_pthread))
+    if (WantsPthread(ToolChain.getTriple(), Args))
       CmdArgs.push_back("-lpthread");
 
     CmdArgs.push_back("-lc");
@@ -292,8 +305,7 @@ void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs,
     CC1Args.push_back("-fno-use-init-array");
 
   // '-pthread' implies atomics, bulk-memory, mutable-globals, and sign-ext
-  if (DriverArgs.hasFlag(options::OPT_pthread, options::OPT_no_pthread,
-                         false)) {
+  if (WantsPthread(getTriple(), DriverArgs)) {
     if (DriverArgs.hasFlag(options::OPT_mno_atomics, options::OPT_matomics,
                            false))
       getDriver().Diag(diag::err_drv_argument_not_allowed_with)
diff --git a/clang/test/Driver/wasm-toolchain.c b/clang/test/Driver/wasm-toolchain.c
index 1ad219338ac28..91803fe6bc1f2 100644
--- a/clang/test/Driver/wasm-toolchain.c
+++ b/clang/test/Driver/wasm-toolchain.c
@@ -110,6 +110,12 @@
 // RUN:   | FileCheck -check-prefix=PTHREAD_NO_SIGN_EXT %s
 // PTHREAD_NO_SIGN_EXT: invalid argument '-pthread' not allowed with '-mno-sign-ext'
 
+// 'wasm32-wasi-threads' does the same thing as '-pthread'
+// RUN: %clang -### --target=wasm32-wasi-threads --sysroot=/foo %s 2>&1 \
+// RUN:  | FileCheck -check-prefix=WASI_THREADS %s
+// WASI_THREADS: "-cc1" {{.*}} "-target-feature" "+atomics" "-target-feature" "+bulk-memory" "-target-feature" "+mutable-globals" "-target-feature" "+sign-ext"
+// WASI_THREADS: wasm-ld{{.*}}" "--shared-memory" "-lpthread"
+
 // '-mllvm -emscripten-cxx-exceptions-allowed=foo,bar' sets
 // '-mllvm --force-attribute=foo:noinline -mllvm --force-attribute=bar:noinline'
 // RUN: %clang -### --target=wasm32-unknown-unknown \

Copy link

github-actions bot commented Feb 28, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

If the user specifies a target triple of wasm32-wasi-threads,
then enable all of the same flags as if -pthread were passed.
This helps prevent user error, as the whole point of selecting
this target is to gain pthread support.

The reverse does not happen (passing -pthread does not alter
the target triple) so as to not interfere with custom environments
and/or custom multilib setups.
Copy link
Member

@carlocab carlocab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@ArcaneNibble ArcaneNibble merged commit 39edcf9 into llvm:main Mar 1, 2025
11 checks passed
jph-13 pushed a commit to jph-13/llvm-project that referenced this pull request Mar 21, 2025
…#129164)

If the user specifies a target triple of wasm32-wasi-threads, then
enable all of the same flags as if `-pthread` were passed. This helps
prevent user error, as the whole point of selecting this target is to
gain pthread support.

The reverse does not happen (passing `-pthread` does not alter the
target triple) so as to not interfere with custom environments and/or
custom multilib setups.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants