Skip to content

Commit 9090bb3

Browse files
authored
Merge pull request #78591 from finagolfin/tls
[android] Use emulated thread-local storage for API 28 and earlier
2 parents ce29eca + 062803a commit 9090bb3

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

lib/IRGen/IRGen.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ swift::getIRTargetOptions(const IRGenOptions &Opts, ASTContext &Ctx) {
140140
// Set UseInitArray appropriately.
141141
TargetOpts.UseInitArray = Clang->getCodeGenOpts().UseInitArray;
142142

143+
// Set emulated TLS in inlined C/C++ functions based on what clang is doing,
144+
// ie either setting the default based on the OS or -Xcc -f{no-,}emulated-tls
145+
// command-line flags.
146+
TargetOpts.EmulatedTLS = Clang->getCodeGenOpts().EmulatedTLS;
147+
143148
// WebAssembly doesn't support atomics yet, see
144149
// https://github.com/apple/swift/issues/54533 for more details.
145150
if (Clang->getTargetInfo().getTriple().isOSBinFormatWasm())

test/IRGen/Inputs/tls.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "shims/SwiftStdint.h"
2+
3+
static inline __swift_uint32_t _swift_stdlib_gettid() {
4+
static __thread __swift_uint32_t tid = 0;
5+
6+
return tid;
7+
}

test/IRGen/emulated-tls.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %target-swift-frontend -Xcc -femulated-tls %s -S -import-objc-header %S/Inputs/tls.h | %FileCheck %s --check-prefix=EMUTLS --check-prefix=EMUTLS-%target-os
2+
// RUN: %target-swift-frontend -Xcc -fno-emulated-tls %s -S -import-objc-header %S/Inputs/tls.h | %FileCheck %s --check-prefix=NOEMUTLS
3+
4+
_swift_stdlib_gettid()
5+
6+
// EMUTLS: __emutls_v._swift_stdlib_gettid.tid
7+
// EMUTLS-linux-android: __emutls_get_address
8+
// EMUTLS-linux-gnu: __emutls_get_address
9+
// EMUTLS-macosx: __emutls_get_address
10+
// EMUTLS-openbsd: __emutls_get_address
11+
// EMUTLS-windows-msvc: __emutls_get_address
12+
// EMUTLS-wasi-NOT: __emutls_get_address
13+
14+
// NOEMUTLS-NOT: __emutls_v._swift_stdlib_gettid.tid
15+
// NOEMUTLS-NOT: __emutls_get_address

0 commit comments

Comments
 (0)