Skip to content

Commit e788e75

Browse files
committed
[clang][WebAssembly] Always have -pthread imply --shared-memory linker flag
Unlike `-lpthread` this flag should not be suppressed by `-nostdlib`.
1 parent 86f0e6d commit e788e75

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

clang/lib/Driver/ToolChains/WebAssembly.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,15 @@ void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA,
150150

151151
AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
152152

153+
if (Args.hasArg(options::OPT_pthread))
154+
CmdArgs.push_back("--shared-memory");
155+
153156
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
154157
if (ToolChain.ShouldLinkCXXStdlib(Args))
155158
ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
156159

157-
if (Args.hasArg(options::OPT_pthread)) {
160+
if (Args.hasArg(options::OPT_pthread))
158161
CmdArgs.push_back("-lpthread");
159-
CmdArgs.push_back("--shared-memory");
160-
}
161162

162163
CmdArgs.push_back("-lc");
163164
AddRunTimeLibs(ToolChain, ToolChain.getDriver(), CmdArgs, Args);

clang/test/Driver/wasm-toolchain.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,16 @@
7575
// Thread-related command line tests.
7676

7777
// '-pthread' sets +atomics, +bulk-memory, +mutable-globals, +sign-ext, and --shared-memory
78-
// RUN: %clang -### --target=wasm32-unknown-unknown \
79-
// RUN: --sysroot=/foo %s -pthread 2>&1 \
78+
// RUN: %clang -### --target=wasm32-unknown-unknown --sysroot=/foo %s -pthread 2>&1 \
8079
// RUN: | FileCheck -check-prefix=PTHREAD %s
8180
// PTHREAD: "-cc1" {{.*}} "-target-feature" "+atomics" "-target-feature" "+bulk-memory" "-target-feature" "+mutable-globals" "-target-feature" "+sign-ext"
82-
// PTHREAD: wasm-ld{{.*}}" "-lpthread" "--shared-memory"
81+
// PTHREAD: wasm-ld{{.*}}" "--shared-memory" "-lpthread"
82+
//
83+
// '-pthread' with '-nostdlib' should still set '--shared-memory' but not include '-lpthread'
84+
// RUN: %clang -### --target=wasm32-unknown-unknown --sysroot=/foo %s -pthread -nostdlib 2>&1 \
85+
// RUN: | FileCheck -check-prefix=PTHREAD-NOSTDLIB %s
86+
// PTHREAD-NOSTDLIB: "-cc1" {{.*}} "-target-feature" "+atomics" "-target-feature" "+bulk-memory" "-target-feature" "+mutable-globals" "-target-feature" "+sign-ext"
87+
// PTHREAD-NOSTDLIB: wasm-ld{{.*}}" "--shared-memory" "-o" "a.out"
8388

8489
// '-pthread' not allowed with '-mno-atomics'
8590
// RUN: not %clang -### --target=wasm32-unknown-unknown \

0 commit comments

Comments
 (0)