Index: docs/UsersManual.rst =================================================================== --- docs/UsersManual.rst +++ docs/UsersManual.rst @@ -1157,6 +1157,13 @@ efficient model can be used. The TLS model can be overridden per variable using the ``tls_model`` attribute. +.. option:: -femulated-tls + + Select emulated TLS model, which overrides all -ftls-model choices. + + In emulated TLS mode, all access to TLS variables are converted to + calls to __emutls_get_address in the runtime library. + .. option:: -mhwdiv=[values] Select the ARM modes (arm or thumb) that support hardware division Index: include/clang/Driver/Options.td =================================================================== --- include/clang/Driver/Options.td +++ include/clang/Driver/Options.td @@ -510,6 +510,9 @@ def feliminate_unused_debug_symbols : Flag<["-"], "feliminate-unused-debug-symbols">, Group; def femit_all_decls : Flag<["-"], "femit-all-decls">, Group, Flags<[CC1Option]>, HelpText<"Emit all declarations, even if unused">; +def femulated_tls : Flag<["-"], "femulated-tls">, Group, Flags<[CC1Option]>, + HelpText<"Use emutls functions to access thread_local variables">; +def fno_emulated_tls : Flag<["-"], "fno-emulated-tls">, Group, Flags<[CC1Option]>; def fencoding_EQ : Joined<["-"], "fencoding=">, Group; def ferror_limit_EQ : Joined<["-"], "ferror-limit=">, Group, Flags<[CoreOption]>; def fexceptions : Flag<["-"], "fexceptions">, Group, Flags<[CC1Option, CoreOption]>, Index: include/clang/Frontend/CodeGenOptions.def =================================================================== --- include/clang/Frontend/CodeGenOptions.def +++ include/clang/Frontend/CodeGenOptions.def @@ -57,6 +57,7 @@ CODEGENOPT(EmitGcovArcs , 1, 0) ///< Emit coverage data files, aka. GCDA. CODEGENOPT(EmitGcovNotes , 1, 0) ///< Emit coverage "notes" files, aka GCNO. CODEGENOPT(EmitOpenCLArgMetadata , 1, 0) ///< Emit OpenCL kernel arg metadata. +CODEGENOPT(EmulatedTLS , 1, 0) ///< Set when -femulated-tls is enabled. /// \brief FP_CONTRACT mode (on/off/fast). ENUM_CODEGENOPT(FPContractMode, FPContractModeKind, 2, FPC_On) CODEGENOPT(ForbidGuardVariables , 1, 0) ///< Issue errors if C++ guard variables Index: lib/CodeGen/BackendUtil.cpp =================================================================== --- lib/CodeGen/BackendUtil.cpp +++ lib/CodeGen/BackendUtil.cpp @@ -541,6 +541,7 @@ Options.FunctionSections = CodeGenOpts.FunctionSections; Options.DataSections = CodeGenOpts.DataSections; Options.UniqueSectionNames = CodeGenOpts.UniqueSectionNames; + Options.EmulatedTLS = CodeGenOpts.EmulatedTLS; Options.MCOptions.MCRelaxAll = CodeGenOpts.RelaxAll; Options.MCOptions.MCSaveTempLabels = CodeGenOpts.SaveTempLabels; Index: lib/Driver/Tools.cpp =================================================================== --- lib/Driver/Tools.cpp +++ lib/Driver/Tools.cpp @@ -3952,6 +3952,8 @@ Args.AddLastArg(CmdArgs, options::OPT_fstandalone_debug); Args.AddLastArg(CmdArgs, options::OPT_fno_standalone_debug); Args.AddLastArg(CmdArgs, options::OPT_fno_operator_names); + Args.AddLastArg(CmdArgs, options::OPT_femulated_tls); + Args.AddLastArg(CmdArgs, options::OPT_fno_emulated_tls); // AltiVec language extensions aren't relevant for assembling. if (!isa(JA) || Output.getType() != types::TY_PP_Asm) Args.AddLastArg(CmdArgs, options::OPT_faltivec); Index: lib/Frontend/CompilerInvocation.cpp =================================================================== --- lib/Frontend/CompilerInvocation.cpp +++ lib/Frontend/CompilerInvocation.cpp @@ -592,6 +592,9 @@ } } + Opts.EmulatedTLS = + Args.hasFlag(OPT_femulated_tls, OPT_fno_emulated_tls, false); + if (Arg *A = Args.getLastArg(OPT_ftlsmodel_EQ)) { StringRef Name = A->getValue(); unsigned Model = llvm::StringSwitch(Name) Index: test/CodeGen/tls-model.c =================================================================== --- test/CodeGen/tls-model.c +++ test/CodeGen/tls-model.c @@ -3,7 +3,21 @@ // RUN: %clang_cc1 %s -triple x86_64-pc-linux-gnu -ftls-model=local-dynamic -emit-llvm -o - | FileCheck %s -check-prefix=CHECK-LD // RUN: %clang_cc1 %s -triple x86_64-pc-linux-gnu -ftls-model=initial-exec -emit-llvm -o - | FileCheck %s -check-prefix=CHECK-IE // RUN: %clang_cc1 %s -triple x86_64-pc-linux-gnu -ftls-model=local-exec -emit-llvm -o - | FileCheck %s -check-prefix=CHECK-LE +// +// RUN: %clang %s -target x86_64-pc-linux-gnu -v -femulated-tls -S -emit-llvm -o - 2>&1 | \ +// RUN: FileCheck %s -check-prefix=EMU -check-prefix=CHECK-GD +// RUN: %clang %s -target x86_64-pc-linux-gnu -v -fno-emulated-tls -S -emit-llvm -o - 2>&1 | \ +// RUN: FileCheck %s -check-prefix=NOEMU -check-prefix=CHECK-GD +// RUN: %clang %s -target x86_64-linux-android -v -S -emit-llvm -o - 2>&1 | \ +// RUN: FileCheck %s -check-prefix=DEFAULT -check-prefix=CHECK-GD +// EMU: -cc1 {{.*}} -femulated-tls +// NOEMU: -cc1 {{.*}} -fno-emulated-tls +// DEFAULT-NOT: -cc1 {{.*}} -femulated-tls +// DEFAULT-NOT: -cc1 {{.*}} -fno-emulated-tls + +int z1 = 0; +int z2; int __thread x; int f() { static int __thread y; @@ -11,18 +25,29 @@ } int __thread __attribute__((tls_model("initial-exec"))) z; +// Note that unlike normal C uninitialized global variables, +// uninitialized TLS variables do NOT have COMMON linkage. + +// CHECK-GD: @z1 = global i32 0 // CHECK-GD: @f.y = internal thread_local global i32 0 +// CHECK-GD: @z2 = common global i32 0 // CHECK-GD: @x = thread_local global i32 0 // CHECK-GD: @z = thread_local(initialexec) global i32 0 +// CHECK-LD: @z1 = global i32 0 // CHECK-LD: @f.y = internal thread_local(localdynamic) global i32 0 +// CHECK-LD: @z2 = common global i32 0 // CHECK-LD: @x = thread_local(localdynamic) global i32 0 // CHECK-LD: @z = thread_local(initialexec) global i32 0 +// CHECK-IE: @z1 = global i32 0 // CHECK-IE: @f.y = internal thread_local(initialexec) global i32 0 +// CHECK-IE: @z2 = common global i32 0 // CHECK-IE: @x = thread_local(initialexec) global i32 0 // CHECK-IE: @z = thread_local(initialexec) global i32 0 +// CHECK-LE: @z1 = global i32 0 // CHECK-LE: @f.y = internal thread_local(localexec) global i32 0 +// CHECK-LE: @z2 = common global i32 0 // CHECK-LE: @x = thread_local(localexec) global i32 0 // CHECK-LE: @z = thread_local(initialexec) global i32 0 Index: test/CodeGenCXX/cxx11-thread-local.cpp =================================================================== --- test/CodeGenCXX/cxx11-thread-local.cpp +++ test/CodeGenCXX/cxx11-thread-local.cpp @@ -1,4 +1,9 @@ // RUN: %clang_cc1 -std=c++11 -emit-llvm %s -o - -triple x86_64-linux-gnu | FileCheck %s +// RUN: %clang -std=c++11 -v -femulated-tls -S -emit-llvm %s -o - \ +// RUN: -target x86_64-linux-gnu 2>&1 | FileCheck --check-prefix=CHECK --check-prefix=EMU %s + +// Check if the -femulated-tls option is passed to cc1. +// EMU: -cc1 {{.*}} -femulated-tls int f(); int g();