Skip to content

Commit 39edcf9

Browse files
authored
[WebAssembly] Make WASI -threads environment behave as -pthread (#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.
1 parent 2709366 commit 39edcf9

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

clang/lib/Driver/ToolChains/WebAssembly.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@ static bool TargetBuildsComponents(const llvm::Triple &TargetTriple) {
6969
TargetTriple.getOSName() != "wasi";
7070
}
7171

72+
static bool WantsPthread(const llvm::Triple &Triple, const ArgList &Args) {
73+
bool WantsPthread =
74+
Args.hasFlag(options::OPT_pthread, options::OPT_no_pthread, false);
75+
76+
// If the WASI environment is "threads" then enable pthreads support
77+
// without requiring -pthread, in order to prevent user error
78+
if (Triple.isOSWASI() && Triple.getEnvironmentName() == "threads")
79+
WantsPthread = true;
80+
81+
return WantsPthread;
82+
}
83+
7284
void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA,
7385
const InputInfo &Output,
7486
const InputInfoList &Inputs,
@@ -150,14 +162,14 @@ void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA,
150162

151163
AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
152164

153-
if (Args.hasArg(options::OPT_pthread))
165+
if (WantsPthread(ToolChain.getTriple(), Args))
154166
CmdArgs.push_back("--shared-memory");
155167

156168
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
157169
if (ToolChain.ShouldLinkCXXStdlib(Args))
158170
ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
159171

160-
if (Args.hasArg(options::OPT_pthread))
172+
if (WantsPthread(ToolChain.getTriple(), Args))
161173
CmdArgs.push_back("-lpthread");
162174

163175
CmdArgs.push_back("-lc");
@@ -292,8 +304,7 @@ void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs,
292304
CC1Args.push_back("-fno-use-init-array");
293305

294306
// '-pthread' implies atomics, bulk-memory, mutable-globals, and sign-ext
295-
if (DriverArgs.hasFlag(options::OPT_pthread, options::OPT_no_pthread,
296-
false)) {
307+
if (WantsPthread(getTriple(), DriverArgs)) {
297308
if (DriverArgs.hasFlag(options::OPT_mno_atomics, options::OPT_matomics,
298309
false))
299310
getDriver().Diag(diag::err_drv_argument_not_allowed_with)

clang/test/Driver/wasm-toolchain.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@
110110
// RUN: | FileCheck -check-prefix=PTHREAD_NO_SIGN_EXT %s
111111
// PTHREAD_NO_SIGN_EXT: invalid argument '-pthread' not allowed with '-mno-sign-ext'
112112

113+
// 'wasm32-wasi-threads' does the same thing as '-pthread'
114+
// RUN: %clang -### --target=wasm32-wasi-threads --sysroot=/foo %s 2>&1 \
115+
// RUN: | FileCheck -check-prefix=WASI_THREADS %s
116+
// WASI_THREADS: "-cc1" {{.*}} "-target-feature" "+atomics" "-target-feature" "+bulk-memory" "-target-feature" "+mutable-globals" "-target-feature" "+sign-ext"
117+
// WASI_THREADS: wasm-ld{{.*}}" "--shared-memory" "-lpthread"
118+
113119
// '-mllvm -emscripten-cxx-exceptions-allowed=foo,bar' sets
114120
// '-mllvm --force-attribute=foo:noinline -mllvm --force-attribute=bar:noinline'
115121
// RUN: %clang -### --target=wasm32-unknown-unknown \

0 commit comments

Comments
 (0)