Skip to content

Commit 0d333bf

Browse files
committed
Remove ExplicitEmulatedTLS and simplify -femulated-tls handling
Currently clangDriver passes -femulated-tls and -fno-emulated-tls to cc1. cc1 forwards the option to LLVMCodeGen and ExplicitEmulatedTLS is used to decide the value. Simplify this by moving the Clang decision to clangDriver and moving the LLVM decision to InitTargetOptionsFromCodeGenFlags.
1 parent 8d163e5 commit 0d333bf

File tree

13 files changed

+49
-70
lines changed

13 files changed

+49
-70
lines changed

clang/include/clang/Driver/Options.td

+1-1
Original file line numberDiff line numberDiff line change
@@ -1590,7 +1590,7 @@ def femit_all_decls : Flag<["-"], "femit-all-decls">, Group<f_Group>, Flags<[CC1
15901590
defm emulated_tls : BoolFOption<"emulated-tls",
15911591
CodeGenOpts<"EmulatedTLS">, DefaultFalse,
15921592
PosFlag<SetTrue, [CC1Option], "Use emutls functions to access thread_local variables">,
1593-
NegFlag<SetFalse>, BothFlags<[CC1Option]>>;
1593+
NegFlag<SetFalse>>;
15941594
def fencoding_EQ : Joined<["-"], "fencoding=">, Group<f_Group>;
15951595
def ferror_limit_EQ : Joined<["-"], "ferror-limit=">, Group<f_Group>, Flags<[CoreOption]>;
15961596
defm exceptions : BoolFOption<"exceptions",

clang/lib/CodeGen/BackendUtil.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,6 @@ static bool initTargetOptions(DiagnosticsEngine &Diags,
422422
CodeGenOpts.UniqueBasicBlockSectionNames;
423423
Options.TLSSize = CodeGenOpts.TLSSize;
424424
Options.EmulatedTLS = CodeGenOpts.EmulatedTLS;
425-
Options.ExplicitEmulatedTLS = true;
426425
Options.DebuggerTuning = CodeGenOpts.getDebuggerTuning();
427426
Options.EmitStackSizeSection = CodeGenOpts.StackSizeSection;
428427
Options.StackUsageOutput = CodeGenOpts.StackUsageOutput;

clang/lib/Driver/ToolChains/Clang.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -6125,10 +6125,12 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
61256125
Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls);
61266126
Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions);
61276127
Args.AddLastArg(CmdArgs, options::OPT_fdigraphs, options::OPT_fno_digraphs);
6128-
Args.AddLastArg(CmdArgs, options::OPT_femulated_tls,
6129-
options::OPT_fno_emulated_tls);
61306128
Args.AddLastArg(CmdArgs, options::OPT_fzero_call_used_regs_EQ);
61316129

6130+
if (Args.hasFlag(options::OPT_femulated_tls, options::OPT_fno_emulated_tls,
6131+
Triple.hasDefaultEmulatedTLS()))
6132+
CmdArgs.push_back("-femulated-tls");
6133+
61326134
if (Arg *A = Args.getLastArg(options::OPT_fzero_call_used_regs_EQ)) {
61336135
// FIXME: There's no reason for this to be restricted to X86. The backend
61346136
// code needs to be changed to include the appropriate function calls

clang/lib/Driver/ToolChains/CommonArgs.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -764,11 +764,10 @@ void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
764764
D.Diag(clang::diag::warn_drv_fjmc_for_elf_only);
765765
}
766766

767-
if (Arg *A = Args.getLastArg(options::OPT_femulated_tls,
768-
options::OPT_fno_emulated_tls)) {
769-
bool Enable = A->getOption().getID() == options::OPT_femulated_tls;
770-
CmdArgs.push_back(Args.MakeArgString(
771-
Twine(PluginOptPrefix) + "-emulated-tls=" + (Enable ? "1" : "0")));
767+
if (Args.hasFlag(options::OPT_femulated_tls, options::OPT_fno_emulated_tls,
768+
ToolChain.getTriple().hasDefaultEmulatedTLS())) {
769+
CmdArgs.push_back(
770+
Args.MakeArgString(Twine(PluginOptPrefix) + "-emulated-tls"));
772771
}
773772

774773
if (Args.hasFlag(options::OPT_fstack_size_section,

clang/lib/Frontend/CompilerInvocation.cpp

+2-7
Original file line numberDiff line numberDiff line change
@@ -1536,8 +1536,8 @@ void CompilerInvocation::GenerateCodeGenArgs(
15361536
F.Filename, SA);
15371537
}
15381538

1539-
GenerateArg(
1540-
Args, Opts.EmulatedTLS ? OPT_femulated_tls : OPT_fno_emulated_tls, SA);
1539+
if (Opts.EmulatedTLS)
1540+
GenerateArg(Args, OPT_femulated_tls, SA);
15411541

15421542
if (Opts.FPDenormalMode != llvm::DenormalMode::getIEEE())
15431543
GenerateArg(Args, OPT_fdenormal_fp_math_EQ, Opts.FPDenormalMode.str(), SA);
@@ -1890,11 +1890,6 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args,
18901890
Opts.LinkBitcodeFiles.push_back(F);
18911891
}
18921892

1893-
if (!Args.getLastArg(OPT_femulated_tls) &&
1894-
!Args.getLastArg(OPT_fno_emulated_tls)) {
1895-
Opts.EmulatedTLS = T.hasDefaultEmulatedTLS();
1896-
}
1897-
18981893
if (Arg *A = Args.getLastArg(OPT_ftlsmodel_EQ)) {
18991894
if (T.isOSAIX()) {
19001895
StringRef Name = A->getValue();

clang/test/Driver/emulated-tls.cpp

+16-20
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
// Android, Cygwin and OpenBSD use emutls by default.
2-
// Clang should pass -femulated-tls or -fno-emulated-tls to cc1 if they are used,
3-
// and cc1 should set up EmulatedTLS and ExplicitEmulatedTLS to LLVM CodeGen.
2+
// Clang should pass -femulated-tls to cc1 if they are used,
3+
// and cc1 should set up EmulatedTLS to LLVM CodeGen.
44
//
5-
// RUN: %clang -### -target arm-linux-androideabi %s 2>&1 \
6-
// RUN: | FileCheck -check-prefix=DEFAULT %s
7-
// RUN: %clang -### -target arm-linux-gnu %s 2>&1 \
8-
// RUN: | FileCheck -check-prefix=DEFAULT %s
9-
// RUN: %clang -### -target i686-pc-cygwin %s 2>&1 \
10-
// RUN: | FileCheck -check-prefix=DEFAULT %s
11-
// RUN: %clang -### -target i686-pc-openbsd %s 2>&1 \
12-
// RUN: | FileCheck -check-prefix=DEFAULT %s
5+
// RUN: %clang -### --target=arm-linux-androideabi %s 2>&1 \
6+
// RUN: | FileCheck -check-prefix=EMU %s
7+
// RUN: %clang -### --target=arm-linux-gnu %s 2>&1 \
8+
// RUN: | FileCheck -check-prefix=NOEMU %s
9+
// RUN: %clang -### --target=i686-pc-cygwin %s 2>&1 \
10+
// RUN: | FileCheck -check-prefix=EMU %s
11+
// RUN: %clang -### --target=i686-pc-openbsd %s 2>&1 \
12+
// RUN: | FileCheck -check-prefix=EMU %s
1313

1414
// RUN: %clang -### -target arm-linux-androideabi -fno-emulated-tls -femulated-tls %s 2>&1 \
1515
// RUN: | FileCheck -check-prefix=EMU %s
@@ -42,18 +42,14 @@
4242

4343
// Default without -f[no-]emulated-tls, will be decided by the target triple.
4444
// DEFAULT-NOT: "-cc1" {{.*}}"-femulated-tls"
45-
// DEFAULT-NOT: "-cc1" {{.*}}"-fno-emulated-tls"
4645

47-
// Explicit and last -f[no-]emulated-tls flag will be passed to cc1.
48-
// EMU: "-cc1" {{.*}}"-femulated-tls"
49-
// EMU-NOT: "-cc1" {{.*}}"-fno-emulated-tls"
46+
// EMU: "-cc1"
47+
// EMU-SAME: "-femulated-tls"
5048

51-
// NOEMU: "-cc1" {{.*}}"-fno-emulated-tls"
52-
// NOEMU-NOT: "-cc1" {{.*}}"-femulated-tls"
49+
// NOEMU: "-cc1"
50+
// NOEMU-NOT: "-femulated-tls"
5351

5452
// LTO related checks
55-
// LTO_NOEMUTLS: plugin-opt=-emulated-tls=0
56-
// LTO_NOEMUTLS-NOT: plugin-opt=-emulated-tls=1
53+
// LTO_NOEMUTLS-NOT: "-plugin-opt=-emulated-tls"
5754

58-
// LTO_EMUTLS: plugin-opt=-emulated-tls=1
59-
// LTO_EMUTLS-NOT: plugin-opt=-emulated-tls=0
55+
// LTO_EMUTLS: "-plugin-opt=-emulated-tls"

llvm/include/llvm/CodeGen/CommandFlags.h

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ std::string getBBSections();
111111
unsigned getTLSSize();
112112

113113
bool getEmulatedTLS();
114+
std::optional<bool> getExplicitEmulatedTLS();
114115

115116
bool getUniqueSectionNames();
116117

llvm/include/llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h

+9-11
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,16 @@ class JITTargetMachineBuilder {
3838
public:
3939
/// Create a JITTargetMachineBuilder based on the given triple.
4040
///
41-
/// Note: TargetOptions is default-constructed, then EmulatedTLS and
42-
/// ExplicitEmulatedTLS are set to true. If EmulatedTLS is not
43-
/// required, these values should be reset before calling
44-
/// createTargetMachine.
41+
/// Note: TargetOptions is default-constructed, then EmulatedTLS is set to
42+
/// true. If EmulatedTLS is not required, these values should be reset before
43+
/// calling createTargetMachine.
4544
JITTargetMachineBuilder(Triple TT);
4645

4746
/// Create a JITTargetMachineBuilder for the host system.
4847
///
49-
/// Note: TargetOptions is default-constructed, then EmulatedTLS and
50-
/// ExplicitEmulatedTLS are set to true. If EmulatedTLS is not
51-
/// required, these values should be reset before calling
52-
/// createTargetMachine.
48+
/// Note: TargetOptions is default-constructed, then EmulatedTLS is set to
49+
/// true. If EmulatedTLS is not required, these values should be reset before
50+
/// calling createTargetMachine.
5351
static Expected<JITTargetMachineBuilder> detectHost();
5452

5553
/// Create a TargetMachine.
@@ -125,9 +123,9 @@ class JITTargetMachineBuilder {
125123
/// Set TargetOptions.
126124
///
127125
/// Note: This operation will overwrite any previously configured options,
128-
/// including EmulatedTLS, ExplicitEmulatedTLS, and UseInitArray which
129-
/// the JITTargetMachineBuilder sets by default. Clients are responsible
130-
/// for re-enabling these overwritten options.
126+
/// including EmulatedTLS and UseInitArray which the JITTargetMachineBuilder
127+
/// sets by default. Clients are responsible for re-enabling these overwritten
128+
/// options.
131129
JITTargetMachineBuilder &setOptions(TargetOptions Options) {
132130
this->Options = std::move(Options);
133131
return *this;

llvm/include/llvm/Target/TargetOptions.h

+8-11
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,14 @@ namespace llvm {
135135
IgnoreXCOFFVisibility(false), XCOFFTracebackTable(true),
136136
UniqueSectionNames(true), UniqueBasicBlockSectionNames(false),
137137
TrapUnreachable(false), NoTrapAfterNoreturn(false), TLSSize(0),
138-
EmulatedTLS(false), ExplicitEmulatedTLS(false), EnableIPRA(false),
139-
EmitStackSizeSection(false), EnableMachineOutliner(false),
140-
EnableMachineFunctionSplitter(false), SupportsDefaultOutlining(false),
141-
EmitAddrsig(false), EmitCallSiteInfo(false),
142-
SupportsDebugEntryValues(false), EnableDebugEntryValues(false),
143-
ValueTrackingVariableLocations(false), ForceDwarfFrameSection(false),
144-
XRayOmitFunctionIndex(false), DebugStrictDwarf(false),
145-
Hotpatch(false), PPCGenScalarMASSEntries(false), JMCInstrument(false),
138+
EmulatedTLS(false), EnableIPRA(false), EmitStackSizeSection(false),
139+
EnableMachineOutliner(false), EnableMachineFunctionSplitter(false),
140+
SupportsDefaultOutlining(false), EmitAddrsig(false),
141+
EmitCallSiteInfo(false), SupportsDebugEntryValues(false),
142+
EnableDebugEntryValues(false), ValueTrackingVariableLocations(false),
143+
ForceDwarfFrameSection(false), XRayOmitFunctionIndex(false),
144+
DebugStrictDwarf(false), Hotpatch(false),
145+
PPCGenScalarMASSEntries(false), JMCInstrument(false),
146146
EnableCFIFixup(false), MisExpect(false), XCOFFReadOnlyPointers(false),
147147
FPDenormalMode(DenormalMode::IEEE, DenormalMode::IEEE) {}
148148

@@ -285,9 +285,6 @@ namespace llvm {
285285
/// function in the runtime library..
286286
unsigned EmulatedTLS : 1;
287287

288-
/// Whether -emulated-tls or -no-emulated-tls is set.
289-
unsigned ExplicitEmulatedTLS : 1;
290-
291288
/// This flag enables InterProcedural Register Allocation (IPRA).
292289
unsigned EnableIPRA : 1;
293290

llvm/lib/CodeGen/CommandFlags.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ CGOPT(bool, IgnoreXCOFFVisibility)
8888
CGOPT(bool, XCOFFTracebackTable)
8989
CGOPT(std::string, BBSections)
9090
CGOPT(unsigned, TLSSize)
91-
CGOPT(bool, EmulatedTLS)
91+
CGOPT_EXP(bool, EmulatedTLS)
9292
CGOPT(bool, UniqueSectionNames)
9393
CGOPT(bool, UniqueBasicBlockSectionNames)
9494
CGOPT(EABI, EABIVersion)
@@ -549,8 +549,8 @@ codegen::InitTargetOptionsFromCodeGenFlags(const Triple &TheTriple) {
549549
Options.UniqueSectionNames = getUniqueSectionNames();
550550
Options.UniqueBasicBlockSectionNames = getUniqueBasicBlockSectionNames();
551551
Options.TLSSize = getTLSSize();
552-
Options.EmulatedTLS = getEmulatedTLS();
553-
Options.ExplicitEmulatedTLS = EmulatedTLSView->getNumOccurrences() > 0;
552+
Options.EmulatedTLS =
553+
getExplicitEmulatedTLS().value_or(TheTriple.hasDefaultEmulatedTLS());
554554
Options.ExceptionModel = getExceptionModel();
555555
Options.EmitStackSizeSection = getEnableStackSizeSection();
556556
Options.EnableMachineFunctionSplitter = getEnableMachineFunctionSplitter();

llvm/lib/ExecutionEngine/Orc/JITTargetMachineBuilder.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ namespace orc {
1818
JITTargetMachineBuilder::JITTargetMachineBuilder(Triple TT)
1919
: TT(std::move(TT)) {
2020
Options.EmulatedTLS = true;
21-
Options.ExplicitEmulatedTLS = true;
2221
Options.UseInitArray = true;
2322
}
2423

llvm/lib/ExecutionEngine/TargetSelect.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ TargetMachine *EngineBuilder::selectTarget(const Triple &TargetTriple,
8989
Options, RelocModel, CMModel, OptLevel,
9090
/*JIT*/ true);
9191
Target->Options.EmulatedTLS = EmulatedTLS;
92-
Target->Options.ExplicitEmulatedTLS = true;
9392

9493
assert(Target && "Could not allocate target machine!");
9594
return Target;

llvm/lib/Target/TargetMachine.cpp

+1-7
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,7 @@ bool TargetMachine::shouldAssumeDSOLocal(const Module &M,
143143
return false;
144144
}
145145

146-
bool TargetMachine::useEmulatedTLS() const {
147-
// Returns Options.EmulatedTLS if the -emulated-tls or -no-emulated-tls
148-
// was specified explicitly; otherwise uses target triple to decide default.
149-
if (Options.ExplicitEmulatedTLS)
150-
return Options.EmulatedTLS;
151-
return getTargetTriple().hasDefaultEmulatedTLS();
152-
}
146+
bool TargetMachine::useEmulatedTLS() const { return Options.EmulatedTLS; }
153147

154148
TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
155149
bool IsPIE = GV->getParent()->getPIELevel() != PIELevel::Default;

0 commit comments

Comments
 (0)