diff --git a/lib/IRGen/IRGen.cpp b/lib/IRGen/IRGen.cpp index a301167f64bbb..c7e973b7a1022 100644 --- a/lib/IRGen/IRGen.cpp +++ b/lib/IRGen/IRGen.cpp @@ -140,6 +140,11 @@ swift::getIRTargetOptions(const IRGenOptions &Opts, ASTContext &Ctx) { // Set UseInitArray appropriately. TargetOpts.UseInitArray = Clang->getCodeGenOpts().UseInitArray; + // Set emulated TLS in inlined C/C++ functions based on what clang is doing, + // ie either setting the default based on the OS or -Xcc -f{no-,}emulated-tls + // command-line flags. + TargetOpts.EmulatedTLS = Clang->getCodeGenOpts().EmulatedTLS; + // WebAssembly doesn't support atomics yet, see // https://github.com/apple/swift/issues/54533 for more details. if (Clang->getTargetInfo().getTriple().isOSBinFormatWasm()) diff --git a/test/IRGen/Inputs/tls.h b/test/IRGen/Inputs/tls.h new file mode 100644 index 0000000000000..55a25b51b096a --- /dev/null +++ b/test/IRGen/Inputs/tls.h @@ -0,0 +1,7 @@ +#include "shims/SwiftStdint.h" + +static inline __swift_uint32_t _swift_stdlib_gettid() { + static __thread __swift_uint32_t tid = 0; + + return tid; +} diff --git a/test/IRGen/emulated-tls.swift b/test/IRGen/emulated-tls.swift new file mode 100644 index 0000000000000..140189e6b2771 --- /dev/null +++ b/test/IRGen/emulated-tls.swift @@ -0,0 +1,15 @@ +// 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 +// RUN: %target-swift-frontend -Xcc -fno-emulated-tls %s -S -import-objc-header %S/Inputs/tls.h | %FileCheck %s --check-prefix=NOEMUTLS + +_swift_stdlib_gettid() + +// EMUTLS: __emutls_v._swift_stdlib_gettid.tid +// EMUTLS-linux-android: __emutls_get_address +// EMUTLS-linux-gnu: __emutls_get_address +// EMUTLS-macosx: __emutls_get_address +// EMUTLS-openbsd: __emutls_get_address +// EMUTLS-windows-msvc: __emutls_get_address +// EMUTLS-wasi-NOT: __emutls_get_address + +// NOEMUTLS-NOT: __emutls_v._swift_stdlib_gettid.tid +// NOEMUTLS-NOT: __emutls_get_address