Skip to content

Commit 71b3b32

Browse files
authored
[Clang] [MS] Add /Zc:tlsGuards option to control tls guard emission (#113830)
This adds an option to control whether guards for on-demand TLS initialization in combination with Microsoft's CXX ABI are emitted or not. The behaviour should match with Microsoft: https://learn.microsoft.com/en-us/cpp/build/reference/zc-tlsguards?view=msvc-170 This fixes #103484
1 parent b88e938 commit 71b3b32

File tree

6 files changed

+24
-0
lines changed

6 files changed

+24
-0
lines changed

clang/include/clang/Basic/CodeGenOptions.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ VALUE_CODEGENOPT(PatchableFunctionEntryOffset , 32, 0)
155155
CODEGENOPT(HotPatch, 1, 0) ///< Supports the Microsoft /HOTPATCH flag and
156156
///< generates a 'patchable-function' attribute.
157157

158+
CODEGENOPT(TlsGuards , 1, 1) ///< Controls emission of tls guards via -fms-tls-guards
158159
CODEGENOPT(JMCInstrument, 1, 0) ///< Set when -fjmc is enabled.
159160
CODEGENOPT(InstrumentForProfiling , 1, 0) ///< Set when -pg is enabled.
160161
CODEGENOPT(CallFEntry , 1, 0) ///< Set when -mfentry is enabled.

clang/include/clang/Driver/Options.td

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4072,6 +4072,11 @@ defm threadsafe_statics : BoolFOption<"threadsafe-statics",
40724072
NegFlag<SetFalse, [], [ClangOption, CC1Option],
40734073
"Do not emit code to make initialization of local statics thread safe">,
40744074
PosFlag<SetTrue>>;
4075+
defm ms_tls_guards : BoolFOption<"ms-tls-guards",
4076+
CodeGenOpts<"TlsGuards">, DefaultTrue,
4077+
NegFlag<SetFalse, [], [CC1Option],
4078+
"Do not emit code to perform on-demand initialization of thread-local variables">,
4079+
PosFlag<SetTrue>>;
40754080
def ftime_report : Flag<["-"], "ftime-report">, Group<f_Group>,
40764081
Visibility<[ClangOption, CC1Option]>,
40774082
MarshallingInfoFlag<CodeGenOpts<"TimePasses">>;
@@ -8635,6 +8640,12 @@ def _SLASH_Zc_threadSafeInit : CLFlag<"Zc:threadSafeInit">,
86358640
def _SLASH_Zc_threadSafeInit_ : CLFlag<"Zc:threadSafeInit-">,
86368641
HelpText<"Disable thread-safe initialization of static variables">,
86378642
Alias<fno_threadsafe_statics>;
8643+
def _SLASH_Zc_tlsGuards : CLFlag<"Zc:tlsGuards">,
8644+
HelpText<"Enable on-demand initialization of thread-local variables">,
8645+
Alias<fms_tls_guards>;
8646+
def _SLASH_Zc_tlsGuards_ : CLFlag<"Zc:tlsGuards-">,
8647+
HelpText<"Disable on-demand initialization of thread-local variables">,
8648+
Alias<fno_ms_tls_guards>;
86388649
def _SLASH_Zc_trigraphs : CLFlag<"Zc:trigraphs">,
86398650
HelpText<"Enable trigraphs">, Alias<ftrigraphs>;
86408651
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+
CGM.getCodeGenOpts().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
@@ -7317,6 +7317,10 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
73177317
(!IsWindowsMSVC || IsMSVC2015Compatible)))
73187318
CmdArgs.push_back("-fno-threadsafe-statics");
73197319

7320+
if (!Args.hasFlag(options::OPT_fms_tls_guards, options::OPT_fno_ms_tls_guards,
7321+
true))
7322+
CmdArgs.push_back("-fno-ms-tls-guards");
7323+
73207324
// Add -fno-assumptions, if it was specified.
73217325
if (!Args.hasFlag(options::OPT_fassumptions, options::OPT_fno_assumptions,
73227326
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-ms-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-ms-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)