Skip to content

Commit ee40970

Browse files
committed
[android] Use emulated thread-local storage for API 28 and earlier
Android before API 29 and a few other platforms don't support native TLS, so fall back to LLVM's emulated TLS there, just like clang does. Also, make sure `-Xcc -f{no-,}emulated-tls` flags passed in are applied to control what the Swift compiler does.
1 parent 55189ba commit ee40970

File tree

5 files changed

+32
-0
lines changed

5 files changed

+32
-0
lines changed

include/swift/AST/IRGenOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,10 @@ class IRGenOptions {
551551
/// Emit a .casid file next to the object file if CAS Backend is used.
552552
bool EmitCASIDFile = false;
553553

554+
/// Tell the LLVM codegen backend to use emulated Thread-Local Storage instead
555+
/// of native TLS
556+
bool UseEmulatedTLS = false;
557+
554558
/// Paths to the pass plugins registered via -load-pass-plugin.
555559
std::vector<std::string> LLVMPassPlugins;
556560

lib/Frontend/CompilerInvocation.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3143,10 +3143,14 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
31433143
Opts.CoveragePrefixMap.addMapping(SplitMap.first, SplitMap.second);
31443144
}
31453145

3146+
Opts.UseEmulatedTLS = Triple.hasDefaultEmulatedTLS();
31463147
for (const Arg *A : Args.filtered(OPT_Xcc)) {
31473148
StringRef Opt = A->getValue();
31483149
if (Opt.starts_with("-D") || Opt.starts_with("-U"))
31493150
Opts.ClangDefines.push_back(Opt.str());
3151+
3152+
if (Opt == "-femulated-tls" || Opt == "-fno-emulated-tls")
3153+
Opts.UseEmulatedTLS = Opt == "-femulated-tls";
31503154
}
31513155

31523156
for (const Arg *A : Args.filtered(OPT_l, OPT_framework)) {

lib/IRGen/IRGen.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ swift::getIRTargetOptions(const IRGenOptions &Opts, ASTContext &Ctx) {
148148
if (Clang->getTargetInfo().getTriple().isOSBinFormatWasm())
149149
TargetOpts.ThreadModel = llvm::ThreadModel::Single;
150150

151+
TargetOpts.EmulatedTLS = Opts.UseEmulatedTLS;
152+
151153
if (Opts.EnableGlobalISel) {
152154
TargetOpts.EnableGlobalISel = true;
153155
TargetOpts.GlobalISelAbort = GlobalISelAbortMode::DisableWithDiag;

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)