Skip to content

Commit 9eb24f5

Browse files
MaskRayilovepi
authored andcommitted
[Driver,CodeGen] Support -mtls-dialect= (llvm#79256)
GCC supports -mtls-dialect= for several architectures to select TLSDESC. This patch supports the following values * x86: "gnu". "gnu2" (TLSDESC) is not supported yet. * RISC-V: "trad" (general dynamic), "desc" (TLSDESC, see llvm#66915) AArch64 toolchains seem to support TLSDESC from the beginning, and the general dynamic model has poor support. Nobody seems to use the option -mtls-dialect= at all, so we don't bother with it. There also seems very little interest in AArch32's TLSDESC support. TLSDESC does not change IR, but affects object file generation. Without a backend option the option is a no-op for in-process ThinLTO. There seems no motivation to have fine-grained control mixing trad/desc for TLS, so we just pass -mllvm, and don't bother with a modules flag metadata or function attribute. Co-authored-by: Paul Kirth <[email protected]> (cherry picked from commit 36b4a9c)
1 parent 78a0a47 commit 9eb24f5

File tree

9 files changed

+87
-3
lines changed

9 files changed

+87
-3
lines changed

clang/include/clang/Basic/CodeGenOptions.def

+3
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,9 @@ ENUM_CODEGENOPT(VecLib, llvm::driver::VectorLibrary, 3, llvm::driver::VectorLibr
369369
/// The default TLS model to use.
370370
ENUM_CODEGENOPT(DefaultTLSModel, TLSModel, 2, GeneralDynamicTLSModel)
371371

372+
/// Whether to enable TLSDESC. AArch64 enables TLSDESC regardless of this value.
373+
CODEGENOPT(EnableTLSDESC, 1, 0)
374+
372375
/// Bit size of immediate TLS offsets (0 == use the default).
373376
VALUE_CODEGENOPT(TLSSize, 8, 0)
374377

clang/include/clang/Driver/Options.td

+5
Original file line numberDiff line numberDiff line change
@@ -4419,6 +4419,8 @@ def mtls_size_EQ : Joined<["-"], "mtls-size=">, Group<m_Group>,
44194419
HelpText<"Specify bit size of immediate TLS offsets (AArch64 ELF only): "
44204420
"12 (for 4KB) | 24 (for 16MB, default) | 32 (for 4GB) | 48 (for 256TB, needs -mcmodel=large)">,
44214421
MarshallingInfoInt<CodeGenOpts<"TLSSize">>;
4422+
def mtls_dialect_EQ : Joined<["-"], "mtls-dialect=">, Group<m_Group>,
4423+
Flags<[TargetSpecific]>, HelpText<"Which thread-local storage dialect to use for dynamic accesses of TLS variables">;
44224424
def mimplicit_it_EQ : Joined<["-"], "mimplicit-it=">, Group<m_Group>;
44234425
def mdefault_build_attributes : Joined<["-"], "mdefault-build-attributes">, Group<m_Group>;
44244426
def mno_default_build_attributes : Joined<["-"], "mno-default-build-attributes">, Group<m_Group>;
@@ -7066,6 +7068,9 @@ def fexperimental_assignment_tracking_EQ : Joined<["-"], "fexperimental-assignme
70667068
Values<"disabled,enabled,forced">, NormalizedValues<["Disabled","Enabled","Forced"]>,
70677069
MarshallingInfoEnum<CodeGenOpts<"AssignmentTrackingMode">, "Enabled">;
70687070

7071+
def enable_tlsdesc : Flag<["-"], "enable-tlsdesc">,
7072+
MarshallingInfoFlag<CodeGenOpts<"EnableTLSDESC">>;
7073+
70697074
} // let Visibility = [CC1Option]
70707075

70717076
//===----------------------------------------------------------------------===//

clang/lib/CodeGen/BackendUtil.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ static bool initTargetOptions(DiagnosticsEngine &Diags,
401401
Options.UniqueBasicBlockSectionNames =
402402
CodeGenOpts.UniqueBasicBlockSectionNames;
403403
Options.TLSSize = CodeGenOpts.TLSSize;
404+
Options.EnableTLSDESC = CodeGenOpts.EnableTLSDESC;
404405
Options.EmulatedTLS = CodeGenOpts.EmulatedTLS;
405406
Options.DebuggerTuning = CodeGenOpts.getDebuggerTuning();
406407
Options.EmitStackSizeSection = CodeGenOpts.StackSizeSection;

clang/lib/Driver/ToolChains/Clang.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -5822,6 +5822,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
58225822
Args.AddLastArg(CmdArgs, options::OPT_mtls_size_EQ);
58235823
}
58245824

5825+
if (isTLSDESCEnabled(TC, Args))
5826+
CmdArgs.push_back("-enable-tlsdesc");
5827+
58255828
// Add the target cpu
58265829
std::string CPU = getCPUName(D, Args, Triple, /*FromAs*/ false);
58275830
if (!CPU.empty()) {

clang/lib/Driver/ToolChains/CommonArgs.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,33 @@ bool tools::isUseSeparateSections(const llvm::Triple &Triple) {
729729
return Triple.isPS();
730730
}
731731

732+
bool tools::isTLSDESCEnabled(const ToolChain &TC,
733+
const llvm::opt::ArgList &Args) {
734+
const llvm::Triple &Triple = TC.getEffectiveTriple();
735+
Arg *A = Args.getLastArg(options::OPT_mtls_dialect_EQ);
736+
if (!A)
737+
return Triple.hasDefaultTLSDESC();
738+
StringRef V = A->getValue();
739+
bool SupportedArgument = false, EnableTLSDESC = false;
740+
bool Unsupported = !Triple.isOSBinFormatELF();
741+
if (Triple.isRISCV()) {
742+
SupportedArgument = V == "desc" || V == "trad";
743+
EnableTLSDESC = V == "desc";
744+
} else if (Triple.isX86()) {
745+
SupportedArgument = V == "gnu";
746+
} else {
747+
Unsupported = true;
748+
}
749+
if (Unsupported) {
750+
TC.getDriver().Diag(diag::err_drv_unsupported_opt_for_target)
751+
<< A->getSpelling() << Triple.getTriple();
752+
} else if (!SupportedArgument) {
753+
TC.getDriver().Diag(diag::err_drv_unsupported_option_argument_for_target)
754+
<< A->getSpelling() << V << Triple.getTriple();
755+
}
756+
return EnableTLSDESC;
757+
}
758+
732759
void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
733760
ArgStringList &CmdArgs, const InputInfo &Output,
734761
const InputInfo &Input, bool IsThinLTO) {
@@ -988,6 +1015,9 @@ void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
9881015
CmdArgs.push_back(
9891016
Args.MakeArgString(Twine(PluginOptPrefix) + "-emulated-tls"));
9901017
}
1018+
if (isTLSDESCEnabled(ToolChain, Args))
1019+
CmdArgs.push_back(
1020+
Args.MakeArgString(Twine(PluginOptPrefix) + "-enable-tlsdesc"));
9911021

9921022
if (Args.hasFlag(options::OPT_fstack_size_section,
9931023
options::OPT_fno_stack_size_section, false))

clang/lib/Driver/ToolChains/CommonArgs.h

+3
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ llvm::StringRef getLTOParallelism(const llvm::opt::ArgList &Args,
144144
bool areOptimizationsEnabled(const llvm::opt::ArgList &Args);
145145

146146
bool isUseSeparateSections(const llvm::Triple &Triple);
147+
// Parse -mtls-dialect=. Return true if the target supports both general-dynamic
148+
// and TLSDESC, and TLSDESC is requested.
149+
bool isTLSDESCEnabled(const ToolChain &TC, const llvm::opt::ArgList &Args);
147150

148151
/// \p EnvVar is split by system delimiter for environment variables.
149152
/// If \p ArgName is "-I", "-L", or an empty string, each entry from \p EnvVar
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// REQUIRES: riscv-registered-target
2+
/// cc1 -enable-tlsdesc (due to -mtls-dialect=desc) enables TLSDESC.
3+
// RUN: %clang_cc1 -triple riscv64 -S -mrelocation-model pic -pic-level 1 -enable-tlsdesc %s -o - | FileCheck %s --check-prefix=DESC
4+
// RUN: %clang_cc1 -triple riscv64 -S -mrelocation-model pic -pic-level 1 %s -o - | FileCheck %s --check-prefix=NODESC
5+
6+
__thread int x;
7+
8+
// DESC: %tlsdesc_hi
9+
// DESC-NOT: %tls_gd_pcrel_hi
10+
// NODESC: %tls_gd_pcrel_hi
11+
// NODESC-NOT: %tlsdesc_hi
12+
int use() {
13+
return x;
14+
}

clang/test/Driver/tls-dialect.c

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: %clang -### --target=riscv64-freebsd -mtls-dialect=desc %s 2>&1 | FileCheck --check-prefix=DESC %s
2+
// RUN: %clang -### --target=riscv64-linux -mtls-dialect=trad %s 2>&1 | FileCheck --check-prefix=NODESC %s
3+
// RUN: %clang -### --target=riscv64-linux %s 2>&1 | FileCheck --check-prefix=NODESC %s
4+
// RUN: %clang -### --target=x86_64-linux -mtls-dialect=gnu %s 2>&1 | FileCheck --check-prefix=NODESC %s
5+
6+
/// LTO
7+
// RUN: %clang -### --target=riscv64-linux -flto -mtls-dialect=desc %s 2>&1 | FileCheck --check-prefix=LTO-DESC %s
8+
// RUN: %clang -### --target=riscv64-linux -flto %s 2>&1 | FileCheck --check-prefix=LTO-NODESC %s
9+
10+
/// Unsupported target
11+
/// GCC supports -mtls-dialect= for AArch64, but we just unsupport it for AArch64 as it is very rarely used.
12+
// RUN: not %clang --target=aarch64-linux -mtls-dialect=desc %s 2>&1 | FileCheck --check-prefix=UNSUPPORTED-TARGET %s
13+
// RUN: not %clang --target=x86_64-apple-macos -mtls-dialect=desc -flto %s 2>&1 | FileCheck -check-prefix=UNSUPPORTED-TARGET %s
14+
15+
/// Unsupported argument
16+
// RUN: not %clang -### --target=riscv64-linux -mtls-dialect=gnu2 %s 2>&1 | FileCheck --check-prefix=UNSUPPORTED-ARG %s
17+
// RUN: not %clang -### --target=x86_64-linux -mtls-dialect=gnu2 %s 2>&1 | FileCheck --check-prefix=UNSUPPORTED-ARG %s
18+
19+
// DESC: "-cc1" {{.*}}"-enable-tlsdesc"
20+
// NODESC-NOT: "-enable-tlsdesc"
21+
// LTO-DESC: "-plugin-opt=-enable-tlsdesc"
22+
// LTO-NODESC-NOT: "-plugin-opt=-enable-tlsdesc"
23+
24+
// UNSUPPORTED-TARGET: error: unsupported option '-mtls-dialect=' for target
25+
// UNSUPPORTED-ARG: error: unsupported argument 'gnu2' to option '-mtls-dialect=' for target

llvm/include/llvm/TargetParser/Triple.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -1033,11 +1033,11 @@ class Triple {
10331033
isWindowsCygwinEnvironment() || isOHOSFamily();
10341034
}
10351035

1036-
/// Tests whether the target uses TLS Descriptor by default.
1036+
/// True if the target supports both general-dynamic and TLSDESC, and TLSDESC
1037+
/// is enabled by default.
10371038
bool hasDefaultTLSDESC() const {
10381039
// TODO: Improve check for other platforms, like Android, and RISC-V
1039-
// Note: This is currently only used on RISC-V.
1040-
return isOSBinFormatELF() && isAArch64();
1040+
return false;
10411041
}
10421042

10431043
/// Tests whether the target uses -data-sections as default.

0 commit comments

Comments
 (0)