Skip to content

[MS] Add /Zc:tlsGuards option to control tls guard emission #113830

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clang/include/clang/Basic/CodeGenOptions.def
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ VALUE_CODEGENOPT(PatchableFunctionEntryOffset , 32, 0)
CODEGENOPT(HotPatch, 1, 0) ///< Supports the Microsoft /HOTPATCH flag and
///< generates a 'patchable-function' attribute.

CODEGENOPT(TlsGuards , 1, 1) ///< Controls emission of tls guards via -fms-tls-guards
CODEGENOPT(JMCInstrument, 1, 0) ///< Set when -fjmc is enabled.
CODEGENOPT(InstrumentForProfiling , 1, 0) ///< Set when -pg is enabled.
CODEGENOPT(CallFEntry , 1, 0) ///< Set when -mfentry is enabled.
Expand Down
11 changes: 11 additions & 0 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -4072,6 +4072,11 @@ defm threadsafe_statics : BoolFOption<"threadsafe-statics",
NegFlag<SetFalse, [], [ClangOption, CC1Option],
"Do not emit code to make initialization of local statics thread safe">,
PosFlag<SetTrue>>;
defm ms_tls_guards : BoolFOption<"ms-tls-guards",
CodeGenOpts<"TlsGuards">, DefaultTrue,
NegFlag<SetFalse, [], [CC1Option],
"Do not emit code to perform on-demand initialization of thread-local variables">,
PosFlag<SetTrue>>;
def ftime_report : Flag<["-"], "ftime-report">, Group<f_Group>,
Visibility<[ClangOption, CC1Option]>,
MarshallingInfoFlag<CodeGenOpts<"TimePasses">>;
Expand Down Expand Up @@ -8635,6 +8640,12 @@ def _SLASH_Zc_threadSafeInit : CLFlag<"Zc:threadSafeInit">,
def _SLASH_Zc_threadSafeInit_ : CLFlag<"Zc:threadSafeInit-">,
HelpText<"Disable thread-safe initialization of static variables">,
Alias<fno_threadsafe_statics>;
def _SLASH_Zc_tlsGuards : CLFlag<"Zc:tlsGuards">,
HelpText<"Enable on-demand initialization of thread-local variables">,
Alias<fms_tls_guards>;
def _SLASH_Zc_tlsGuards_ : CLFlag<"Zc:tlsGuards-">,
HelpText<"Disable on-demand initialization of thread-local variables">,
Alias<fno_ms_tls_guards>;
def _SLASH_Zc_trigraphs : CLFlag<"Zc:trigraphs">,
HelpText<"Enable trigraphs">, Alias<ftrigraphs>;
def _SLASH_Zc_trigraphs_off : CLFlag<"Zc:trigraphs-">,
Expand Down
1 change: 1 addition & 0 deletions clang/lib/CodeGen/MicrosoftCXXABI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ class MicrosoftCXXABI : public CGCXXABI {
bool usesThreadWrapperFunction(const VarDecl *VD) const override {
return getContext().getLangOpts().isCompatibleWithMSVC(
LangOptions::MSVC2019_5) &&
CGM.getCodeGenOpts().TlsGuards &&
(!isEmittedWithConstantInitializer(VD) || mayNeedDestruction(VD));
}
LValue EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF, const VarDecl *VD,
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7317,6 +7317,10 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
(!IsWindowsMSVC || IsMSVC2015Compatible)))
CmdArgs.push_back("-fno-threadsafe-statics");

if (!Args.hasFlag(options::OPT_fms_tls_guards, options::OPT_fno_ms_tls_guards,
true))
CmdArgs.push_back("-fno-ms-tls-guards");

// Add -fno-assumptions, if it was specified.
if (!Args.hasFlag(options::OPT_fassumptions, options::OPT_fno_assumptions,
true))
Expand Down
4 changes: 4 additions & 0 deletions clang/test/CodeGenCXX/ms-thread_local.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// RUN: %clang_cc1 %s -std=c++1y -triple=i686-pc-win32 -fms-compatibility-version=19.25 -emit-llvm -o - | FileCheck %s
// 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
// 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
// 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

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

// CHECK-LABEL: declare dso_local void @__dyn_tls_on_demand_init()
// CHECK-LD-LABEL: declare dso_local void @__dyn_tls_on_demand_init()
// CHECK-LEGACY-NOT: declare dso_local void @__dyn_tls_on_demand_init()
// CHECK-NO-GUARDS-NOT: declare dso_local void @__dyn_tls_on_demand_init()

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

thread_local A &c = b;
thread_local A &d = c;
Expand Down
3 changes: 3 additions & 0 deletions clang/test/Driver/cl-zc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@
// RUN: %clang_cl /Zc:threadSafeInit /c -### -- %s 2>&1 | FileCheck -check-prefix=ThreadSafeStatics %s
// ThreadSafeStatics-NOT: "-fno-threadsafe-statics"

// RUN: %clang_cl /Zc:tlsGuards- /c -### -- %s 2>&1 | FileCheck -check-prefix=NoTlsGuards %s
// NoTlsGuards: "-fno-ms-tls-guards"

// RUN: %clang_cl /Zc:dllexportInlines- /c -### -- %s 2>&1 | FileCheck -check-prefix=NoDllExportInlines %s
// NoDllExportInlines: "-fno-dllexport-inlines"
// RUN: %clang_cl /Zc:dllexportInlines /c -### -- %s 2>&1 | FileCheck -check-prefix=DllExportInlines %s
Expand Down
Loading