Skip to content

Commit 23d4562

Browse files
committed
[MS] Add /Zc:tlsGuards option to control tls guard emission
1 parent 60d2fed commit 23d4562

File tree

6 files changed

+24
-0
lines changed

6 files changed

+24
-0
lines changed

clang/include/clang/Basic/LangOptions.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ COMPATIBLE_LANGOPT(RecoveryAST, 1, 1, "Preserve expressions in AST when encounte
186186
COMPATIBLE_LANGOPT(RecoveryASTType, 1, 1, "Preserve the type in recovery expressions")
187187

188188
BENIGN_LANGOPT(ThreadsafeStatics , 1, 1, "thread-safe static initializers")
189+
BENIGN_LANGOPT(TlsGuards , 1, 1, "on-demand TLS initialization")
189190
LANGOPT(POSIXThreads , 1, 0, "POSIX thread support")
190191
LANGOPT(Blocks , 1, 0, "blocks extension to C")
191192
BENIGN_LANGOPT(EmitAllDecls , 1, 0, "emitting all declarations")

clang/include/clang/Driver/Options.td

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4063,6 +4063,11 @@ defm threadsafe_statics : BoolFOption<"threadsafe-statics",
40634063
NegFlag<SetFalse, [], [ClangOption, CC1Option],
40644064
"Do not emit code to make initialization of local statics thread safe">,
40654065
PosFlag<SetTrue>>;
4066+
defm tls_guards : BoolFOption<"tls-guards",
4067+
LangOpts<"TlsGuards">, DefaultTrue,
4068+
NegFlag<SetFalse, [], [ClangOption, CC1Option],
4069+
"Do not emit code to perform on-demand initialization of thread-local variables">,
4070+
PosFlag<SetTrue>>;
40664071
def ftime_report : Flag<["-"], "ftime-report">, Group<f_Group>,
40674072
Visibility<[ClangOption, CC1Option]>,
40684073
MarshallingInfoFlag<CodeGenOpts<"TimePasses">>;
@@ -8610,6 +8615,12 @@ def _SLASH_Zc_threadSafeInit : CLFlag<"Zc:threadSafeInit">,
86108615
def _SLASH_Zc_threadSafeInit_ : CLFlag<"Zc:threadSafeInit-">,
86118616
HelpText<"Disable thread-safe initialization of static variables">,
86128617
Alias<fno_threadsafe_statics>;
8618+
def _SLASH_Zc_tlsGuards : CLFlag<"Zc:tlsGuards">,
8619+
HelpText<"Enable on-demand initialization of thread-local variables">,
8620+
Alias<ftls_guards>;
8621+
def _SLASH_Zc_tlsGuards_ : CLFlag<"Zc:tlsGuards-">,
8622+
HelpText<"Disable on-demand initialization of thread-local variables">,
8623+
Alias<fno_tls_guards>;
86138624
def _SLASH_Zc_trigraphs : CLFlag<"Zc:trigraphs">,
86148625
HelpText<"Enable trigraphs">, Alias<ftrigraphs>;
86158626
def _SLASH_Zc_trigraphs_off : CLFlag<"Zc:trigraphs-">,

clang/lib/CodeGen/MicrosoftCXXABI.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ class MicrosoftCXXABI : public CGCXXABI {
431431
bool usesThreadWrapperFunction(const VarDecl *VD) const override {
432432
return getContext().getLangOpts().isCompatibleWithMSVC(
433433
LangOptions::MSVC2019_5) &&
434+
getContext().getLangOpts().TlsGuards &&
434435
(!isEmittedWithConstantInitializer(VD) || mayNeedDestruction(VD));
435436
}
436437
LValue EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF, const VarDecl *VD,

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7291,6 +7291,10 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
72917291
(!IsWindowsMSVC || IsMSVC2015Compatible)))
72927292
CmdArgs.push_back("-fno-threadsafe-statics");
72937293

7294+
if (!Args.hasFlag(options::OPT_ftls_guards, options::OPT_fno_tls_guards,
7295+
true))
7296+
CmdArgs.push_back("-fno-tls-guards");
7297+
72947298
// Add -fno-assumptions, if it was specified.
72957299
if (!Args.hasFlag(options::OPT_fassumptions, options::OPT_fno_assumptions,
72967300
true))

clang/test/CodeGenCXX/ms-thread_local.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// RUN: %clang_cc1 %s -std=c++1y -triple=i686-pc-win32 -fms-compatibility-version=19.25 -emit-llvm -o - | FileCheck %s
22
// RUN: %clang_cc1 %s -std=c++1y -triple=i686-pc-win32 -fms-compatibility-version=19.20 -emit-llvm -o - | FileCheck %s -check-prefix=CHECK-LEGACY
3+
// RUN: %clang_cc1 %s -std=c++1y -triple=i686-pc-win32 -fms-compatibility-version=19.25 -fno-tls-guards -emit-llvm -o - | FileCheck %s -check-prefix=CHECK-NO-GUARDS
34
// RUN: %clang_cc1 %s -std=c++1y -triple=i686-pc-win32 -fms-compatibility-version=19.25 -ftls-model=local-dynamic -emit-llvm -o - | FileCheck %s -check-prefix=CHECK-LD
45

56
struct A {
@@ -23,17 +24,20 @@ thread_local A a = A();
2324
// CHECK-LD-DAG: @"__tls_init$initializer$" = internal constant ptr @__tls_init, section ".CRT$XDU"
2425
// CHECK-LD-DAG: @__tls_guard = external dso_local thread_local global i8
2526
// CHECK-LEGACY-NOT: @__tls_guard = external dso_local thread_local global i8
27+
// CHECK-NO-GUARDS-NOT: @__tls_guard = external dso_local thread_local global i8
2628
thread_local A b;
2729

2830
// CHECK-LABEL: declare dso_local void @__dyn_tls_on_demand_init()
2931
// CHECK-LD-LABEL: declare dso_local void @__dyn_tls_on_demand_init()
3032
// CHECK-LEGACY-NOT: declare dso_local void @__dyn_tls_on_demand_init()
33+
// CHECK-NO-GUARDS-NOT: declare dso_local void @__dyn_tls_on_demand_init()
3134

3235
// CHECK-LABEL: define dso_local void @"?f@@YA?AUA@@XZ"(ptr dead_on_unwind noalias writable sret(%struct.A) align 1 %agg.result)
3336
// CHECK: call void @__dyn_tls_on_demand_init()
3437
// CHECK-LD-LABEL: define dso_local void @"?f@@YA?AUA@@XZ"(ptr dead_on_unwind noalias writable sret(%struct.A) align 1 %agg.result)
3538
// CHECK-LD: call void @__dyn_tls_on_demand_init()
3639
// CHECK-LEGACY-NOT: call void @__dyn_tls_on_demand_init()
40+
// CHECK-NO-GUARDS-NOT: call void @__dyn_tls_on_demand_init()
3741

3842
thread_local A &c = b;
3943
thread_local A &d = c;

clang/test/Driver/cl-zc.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@
9999
// RUN: %clang_cl /Zc:threadSafeInit /c -### -- %s 2>&1 | FileCheck -check-prefix=ThreadSafeStatics %s
100100
// ThreadSafeStatics-NOT: "-fno-threadsafe-statics"
101101

102+
// RUN: %clang_cl /Zc:tlsGuards- /c -### -- %s 2>&1 | FileCheck -check-prefix=NoTlsGuards %s
103+
// NoTlsGuards: "-fno-tls-guards"
104+
102105
// RUN: %clang_cl /Zc:dllexportInlines- /c -### -- %s 2>&1 | FileCheck -check-prefix=NoDllExportInlines %s
103106
// NoDllExportInlines: "-fno-dllexport-inlines"
104107
// RUN: %clang_cl /Zc:dllexportInlines /c -### -- %s 2>&1 | FileCheck -check-prefix=DllExportInlines %s

0 commit comments

Comments
 (0)