Skip to content

Commit eae20fe

Browse files
authored
Pass -fno-threadsafe-statics when not building for pthreads (#16152)
This means the compiler won't generate calls `__cxa_guard_acquire`/`__cxa_guard_release` around static intitializers. Even through those funtions don't really do much in non-threads builds (since locks and atomics don't do much) it seems that neither llvm nor binaryen can completely remove them. For example: ``` int foo() { return 42; } int main() { static int a = foo(); return 0; } ``` When compiled, without threading normally generates this code: ``` 0000ef func[2] <main>: 0000f0: 02 40 | block 0000f2: 41 b8 08 | i32.const 1080 0000f5: 2d 00 00 | i32.load8_u 0 0 0000f8: 41 01 | i32.const 1 0000fa: 71 | i32.and 0000fb: 0d 00 | br_if 0 0000fd: 41 b8 08 | i32.const 1080 000100: 10 05 | call 5 <__cxa_guard_acquire> 000102: 45 | i32.eqz 000103: 0d 00 | br_if 0 000105: 41 b8 08 | i32.const 1080 000108: 10 0d | call 13 <__cxa_guard_release> 00010a: 0b | end 00010b: 41 00 | i32.const 0 00010d: 0b | end ``` With `-fno-threadsafe-statics` we get just: ``` 0000cb func[1] <main>: 0000cc: 41 80 08 | i32.const 1024 0000cf: 2d 00 00 | i32.load8_u 0 0 0000d2: 45 | i32.eqz 0000d3: 04 40 | if 0000d5: 41 80 08 | i32.const 1024 0000d8: 41 01 | i32.const 1 0000da: 3a 00 00 | i32.store8 0 0 0000dd: 0b | end 0000de: 41 00 | i32.const 0 0000e0: 0b | end ```
1 parent 7517158 commit eae20fe

7 files changed

+21
-7
lines changed

emcc.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -873,12 +873,18 @@ def get_cflags(user_args):
873873

874874
# Set the LIBCPP ABI version to at least 2 so that we get nicely aligned string
875875
# data and other nice fixes.
876-
cflags += [# '-fno-threadsafe-statics', # disabled due to issue 1289
877-
'-D__EMSCRIPTEN_major__=' + str(shared.EMSCRIPTEN_VERSION_MAJOR),
876+
cflags += ['-D__EMSCRIPTEN_major__=' + str(shared.EMSCRIPTEN_VERSION_MAJOR),
878877
'-D__EMSCRIPTEN_minor__=' + str(shared.EMSCRIPTEN_VERSION_MINOR),
879878
'-D__EMSCRIPTEN_tiny__=' + str(shared.EMSCRIPTEN_VERSION_TINY),
880879
'-D_LIBCPP_ABI_VERSION=2']
881880

881+
if not settings.USE_PTHREADS:
882+
# There is no point in using thread safe static inits in code that cannot
883+
# be part of a multi-threaded program.
884+
# TODO(sbc): Remove this if/when upstream clang takes care of this
885+
# automatically: https://reviews.llvm.org/D118571.
886+
cflags += ['-fno-threadsafe-statics']
887+
882888
# Changes to default clang behavior
883889

884890
# Implicit functions can cause horribly confusing function pointer type errors, see #2175

tests/hello_cxx11.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
#error This file should be compiled with -std=c++11!
1010
#endif
1111

12+
#ifdef _REENTRANT
13+
#error Expected to be compiled single-threaded.
14+
#endif
15+
16+
#ifdef __cpp_threadsafe_static_init
17+
#error single-threaded builds should not define __cpp_threadsafe_static_init
18+
#endif
19+
1220
int main( int argc, const char *argv[] ) {
1321
printf("Hello world!\\n");
1422
return 0;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
124608
1+
124053
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
124589
1+
124034
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
121944
1+
121408
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
166546
1+
164706
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
226456
1+
224609

0 commit comments

Comments
 (0)