[PATCH] D118571: [clang][WebAssembly] Imply -fno-threadsafe-static when threading is disabled

Sam Clegg via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Jan 30 11:08:14 PST 2022


sbc100 created this revision.
Herald added subscribers: wingo, ecnelises, sunfish, jgravelle-google, dschuff.
sbc100 requested review of this revision.
Herald added subscribers: cfe-commits, aheejin.
Herald added a project: clang.

When we don't enable atomics we completely disabled threading in
which case there is no point in generating thread safe code for
static initialization.

This should always be safe because, in WebAssembly, it is not
possible to link object compiled without the atomics feature into a
mutli-threaded program.

See https://github.com/emscripten-core/emscripten/pull/16152


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D118571

Files:
  clang/lib/Basic/Targets/WebAssembly.cpp
  clang/test/CodeGenCXX/static-init-wasm.cpp


Index: clang/test/CodeGenCXX/static-init-wasm.cpp
===================================================================
--- clang/test/CodeGenCXX/static-init-wasm.cpp
+++ clang/test/CodeGenCXX/static-init-wasm.cpp
@@ -1,6 +1,6 @@
-// RUN: %clang_cc1 -emit-llvm -triple=wasm32-unknown-unknown -o - %s \
+// RUN: %clang_cc1 -emit-llvm -triple=wasm32-unknown-unknown -target-feature +atomics -o - %s \
 // RUN:   | FileCheck %s -check-prefix=WEBASSEMBLY32
-// RUN: %clang_cc1 -emit-llvm -triple=wasm64-unknown-unknown -o - %s \
+// RUN: %clang_cc1 -emit-llvm -triple=wasm64-unknown-unknown -target-feature +atomics -o - %s \
 // RUN:   | FileCheck %s -check-prefix=WEBASSEMBLY64
 
 // Test that we don't create common blocks.
@@ -52,3 +52,17 @@
 // WEBASSEMBLY64: call noundef %struct.A* @_ZN1AC1Ev(%struct.A* {{[^,]*}} @theA)
 // WEBASSEMBLY64: define internal void @_GLOBAL__sub_I_static_init_wasm.cpp() #3 {
 // WEBASSEMBLY64: call void @__cxx_global_var_init()
+
+// RUN: %clang_cc1 -emit-llvm -triple=wasm32-unknown-unknown -o - %s \
+// RUN:   | FileCheck %s -check-prefix=NOATOMICS
+// RUN: %clang_cc1 -emit-llvm -triple=wasm64-unknown-unknown -o - %s \
+// RUN:   | FileCheck %s -check-prefix=NOATOMICS
+
+// NOATOMICS-LABEL: @_Z1gv()
+// NOATOMICS:       %[[R0:.+]] = load i8, i8* @_ZGVZ1gvE1a, align 1
+// NOATOMICS-NEXT:  %guard.uninitialized = icmp eq i8 %[[R0]], 0
+// NOATOMICS-NEXT:  br i1 %guard.uninitialized, label %[[CHECK:.+]], label %[[END:.+]],
+// NOATOMICS:       [[CHECK]]:
+// NOATOMICS-NOT:   __cxa_guard_acquire
+// NOATOMICS:       [[END]]:
+// NOATOMICS-NEXT:  ret void
Index: clang/lib/Basic/Targets/WebAssembly.cpp
===================================================================
--- clang/lib/Basic/Targets/WebAssembly.cpp
+++ clang/lib/Basic/Targets/WebAssembly.cpp
@@ -255,11 +255,13 @@
 
 void WebAssemblyTargetInfo::adjust(DiagnosticsEngine &Diags,
                                    LangOptions &Opts) {
+  TargetInfo::adjust(Diags, Opts);
   // If the Atomics feature isn't available, turn off POSIXThreads and
   // ThreadModel, so that we don't predefine _REENTRANT or __STDCPP_THREADS__.
   if (!HasAtomics) {
     Opts.POSIXThreads = false;
     Opts.setThreadModel(LangOptions::ThreadModelKind::Single);
+    Opts.ThreadsafeStatics = false;
   }
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D118571.404401.patch
Type: text/x-patch
Size: 2294 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220130/b6ce57f0/attachment.bin>


More information about the cfe-commits mailing list