Skip to content

Commit

Permalink
Remove ExplicitEmulatedTLS and simplify -femulated-tls handling
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MaskRay committed Apr 23, 2023
1 parent 8d163e5 commit 0d333bf
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 70 deletions.
2 changes: 1 addition & 1 deletion clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -1590,7 +1590,7 @@ def femit_all_decls : Flag<["-"], "femit-all-decls">, Group<f_Group>, Flags<[CC1
defm emulated_tls : BoolFOption<"emulated-tls",
CodeGenOpts<"EmulatedTLS">, DefaultFalse,
PosFlag<SetTrue, [CC1Option], "Use emutls functions to access thread_local variables">,
NegFlag<SetFalse>, BothFlags<[CC1Option]>>;
NegFlag<SetFalse>>;
def fencoding_EQ : Joined<["-"], "fencoding=">, Group<f_Group>;
def ferror_limit_EQ : Joined<["-"], "ferror-limit=">, Group<f_Group>, Flags<[CoreOption]>;
defm exceptions : BoolFOption<"exceptions",
Expand Down
1 change: 0 additions & 1 deletion clang/lib/CodeGen/BackendUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,6 @@ static bool initTargetOptions(DiagnosticsEngine &Diags,
CodeGenOpts.UniqueBasicBlockSectionNames;
Options.TLSSize = CodeGenOpts.TLSSize;
Options.EmulatedTLS = CodeGenOpts.EmulatedTLS;
Options.ExplicitEmulatedTLS = true;
Options.DebuggerTuning = CodeGenOpts.getDebuggerTuning();
Options.EmitStackSizeSection = CodeGenOpts.StackSizeSection;
Options.StackUsageOutput = CodeGenOpts.StackUsageOutput;
Expand Down
6 changes: 4 additions & 2 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6125,10 +6125,12 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls);
Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions);
Args.AddLastArg(CmdArgs, options::OPT_fdigraphs, options::OPT_fno_digraphs);
Args.AddLastArg(CmdArgs, options::OPT_femulated_tls,
options::OPT_fno_emulated_tls);
Args.AddLastArg(CmdArgs, options::OPT_fzero_call_used_regs_EQ);

if (Args.hasFlag(options::OPT_femulated_tls, options::OPT_fno_emulated_tls,
Triple.hasDefaultEmulatedTLS()))
CmdArgs.push_back("-femulated-tls");

if (Arg *A = Args.getLastArg(options::OPT_fzero_call_used_regs_EQ)) {
// FIXME: There's no reason for this to be restricted to X86. The backend
// code needs to be changed to include the appropriate function calls
Expand Down
9 changes: 4 additions & 5 deletions clang/lib/Driver/ToolChains/CommonArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -764,11 +764,10 @@ void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
D.Diag(clang::diag::warn_drv_fjmc_for_elf_only);
}

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

if (Args.hasFlag(options::OPT_fstack_size_section,
Expand Down
9 changes: 2 additions & 7 deletions clang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1536,8 +1536,8 @@ void CompilerInvocation::GenerateCodeGenArgs(
F.Filename, SA);
}

GenerateArg(
Args, Opts.EmulatedTLS ? OPT_femulated_tls : OPT_fno_emulated_tls, SA);
if (Opts.EmulatedTLS)
GenerateArg(Args, OPT_femulated_tls, SA);

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

if (!Args.getLastArg(OPT_femulated_tls) &&
!Args.getLastArg(OPT_fno_emulated_tls)) {
Opts.EmulatedTLS = T.hasDefaultEmulatedTLS();
}

if (Arg *A = Args.getLastArg(OPT_ftlsmodel_EQ)) {
if (T.isOSAIX()) {
StringRef Name = A->getValue();
Expand Down
36 changes: 16 additions & 20 deletions clang/test/Driver/emulated-tls.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// Android, Cygwin and OpenBSD use emutls by default.
// Clang should pass -femulated-tls or -fno-emulated-tls to cc1 if they are used,
// and cc1 should set up EmulatedTLS and ExplicitEmulatedTLS to LLVM CodeGen.
// Clang should pass -femulated-tls to cc1 if they are used,
// and cc1 should set up EmulatedTLS to LLVM CodeGen.
//
// RUN: %clang -### -target arm-linux-androideabi %s 2>&1 \
// RUN: | FileCheck -check-prefix=DEFAULT %s
// RUN: %clang -### -target arm-linux-gnu %s 2>&1 \
// RUN: | FileCheck -check-prefix=DEFAULT %s
// RUN: %clang -### -target i686-pc-cygwin %s 2>&1 \
// RUN: | FileCheck -check-prefix=DEFAULT %s
// RUN: %clang -### -target i686-pc-openbsd %s 2>&1 \
// RUN: | FileCheck -check-prefix=DEFAULT %s
// RUN: %clang -### --target=arm-linux-androideabi %s 2>&1 \
// RUN: | FileCheck -check-prefix=EMU %s
// RUN: %clang -### --target=arm-linux-gnu %s 2>&1 \
// RUN: | FileCheck -check-prefix=NOEMU %s
// RUN: %clang -### --target=i686-pc-cygwin %s 2>&1 \
// RUN: | FileCheck -check-prefix=EMU %s
// RUN: %clang -### --target=i686-pc-openbsd %s 2>&1 \
// RUN: | FileCheck -check-prefix=EMU %s

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

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

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

// NOEMU: "-cc1" {{.*}}"-fno-emulated-tls"
// NOEMU-NOT: "-cc1" {{.*}}"-femulated-tls"
// NOEMU: "-cc1"
// NOEMU-NOT: "-femulated-tls"

// LTO related checks
// LTO_NOEMUTLS: plugin-opt=-emulated-tls=0
// LTO_NOEMUTLS-NOT: plugin-opt=-emulated-tls=1
// LTO_NOEMUTLS-NOT: "-plugin-opt=-emulated-tls"

// LTO_EMUTLS: plugin-opt=-emulated-tls=1
// LTO_EMUTLS-NOT: plugin-opt=-emulated-tls=0
// LTO_EMUTLS: "-plugin-opt=-emulated-tls"
1 change: 1 addition & 0 deletions llvm/include/llvm/CodeGen/CommandFlags.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ std::string getBBSections();
unsigned getTLSSize();

bool getEmulatedTLS();
std::optional<bool> getExplicitEmulatedTLS();

bool getUniqueSectionNames();

Expand Down
20 changes: 9 additions & 11 deletions llvm/include/llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,16 @@ class JITTargetMachineBuilder {
public:
/// Create a JITTargetMachineBuilder based on the given triple.
///
/// Note: TargetOptions is default-constructed, then EmulatedTLS and
/// ExplicitEmulatedTLS are set to true. If EmulatedTLS is not
/// required, these values should be reset before calling
/// createTargetMachine.
/// Note: TargetOptions is default-constructed, then EmulatedTLS is set to
/// true. If EmulatedTLS is not required, these values should be reset before
/// calling createTargetMachine.
JITTargetMachineBuilder(Triple TT);

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

/// Create a TargetMachine.
Expand Down Expand Up @@ -125,9 +123,9 @@ class JITTargetMachineBuilder {
/// Set TargetOptions.
///
/// Note: This operation will overwrite any previously configured options,
/// including EmulatedTLS, ExplicitEmulatedTLS, and UseInitArray which
/// the JITTargetMachineBuilder sets by default. Clients are responsible
/// for re-enabling these overwritten options.
/// including EmulatedTLS and UseInitArray which the JITTargetMachineBuilder
/// sets by default. Clients are responsible for re-enabling these overwritten
/// options.
JITTargetMachineBuilder &setOptions(TargetOptions Options) {
this->Options = std::move(Options);
return *this;
Expand Down
19 changes: 8 additions & 11 deletions llvm/include/llvm/Target/TargetOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,14 @@ namespace llvm {
IgnoreXCOFFVisibility(false), XCOFFTracebackTable(true),
UniqueSectionNames(true), UniqueBasicBlockSectionNames(false),
TrapUnreachable(false), NoTrapAfterNoreturn(false), TLSSize(0),
EmulatedTLS(false), ExplicitEmulatedTLS(false), EnableIPRA(false),
EmitStackSizeSection(false), EnableMachineOutliner(false),
EnableMachineFunctionSplitter(false), SupportsDefaultOutlining(false),
EmitAddrsig(false), EmitCallSiteInfo(false),
SupportsDebugEntryValues(false), EnableDebugEntryValues(false),
ValueTrackingVariableLocations(false), ForceDwarfFrameSection(false),
XRayOmitFunctionIndex(false), DebugStrictDwarf(false),
Hotpatch(false), PPCGenScalarMASSEntries(false), JMCInstrument(false),
EmulatedTLS(false), EnableIPRA(false), EmitStackSizeSection(false),
EnableMachineOutliner(false), EnableMachineFunctionSplitter(false),
SupportsDefaultOutlining(false), EmitAddrsig(false),
EmitCallSiteInfo(false), SupportsDebugEntryValues(false),
EnableDebugEntryValues(false), ValueTrackingVariableLocations(false),
ForceDwarfFrameSection(false), XRayOmitFunctionIndex(false),
DebugStrictDwarf(false), Hotpatch(false),
PPCGenScalarMASSEntries(false), JMCInstrument(false),
EnableCFIFixup(false), MisExpect(false), XCOFFReadOnlyPointers(false),
FPDenormalMode(DenormalMode::IEEE, DenormalMode::IEEE) {}

Expand Down Expand Up @@ -285,9 +285,6 @@ namespace llvm {
/// function in the runtime library..
unsigned EmulatedTLS : 1;

/// Whether -emulated-tls or -no-emulated-tls is set.
unsigned ExplicitEmulatedTLS : 1;

/// This flag enables InterProcedural Register Allocation (IPRA).
unsigned EnableIPRA : 1;

Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/CodeGen/CommandFlags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ CGOPT(bool, IgnoreXCOFFVisibility)
CGOPT(bool, XCOFFTracebackTable)
CGOPT(std::string, BBSections)
CGOPT(unsigned, TLSSize)
CGOPT(bool, EmulatedTLS)
CGOPT_EXP(bool, EmulatedTLS)
CGOPT(bool, UniqueSectionNames)
CGOPT(bool, UniqueBasicBlockSectionNames)
CGOPT(EABI, EABIVersion)
Expand Down Expand Up @@ -549,8 +549,8 @@ codegen::InitTargetOptionsFromCodeGenFlags(const Triple &TheTriple) {
Options.UniqueSectionNames = getUniqueSectionNames();
Options.UniqueBasicBlockSectionNames = getUniqueBasicBlockSectionNames();
Options.TLSSize = getTLSSize();
Options.EmulatedTLS = getEmulatedTLS();
Options.ExplicitEmulatedTLS = EmulatedTLSView->getNumOccurrences() > 0;
Options.EmulatedTLS =
getExplicitEmulatedTLS().value_or(TheTriple.hasDefaultEmulatedTLS());
Options.ExceptionModel = getExceptionModel();
Options.EmitStackSizeSection = getEnableStackSizeSection();
Options.EnableMachineFunctionSplitter = getEnableMachineFunctionSplitter();
Expand Down
1 change: 0 additions & 1 deletion llvm/lib/ExecutionEngine/Orc/JITTargetMachineBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ namespace orc {
JITTargetMachineBuilder::JITTargetMachineBuilder(Triple TT)
: TT(std::move(TT)) {
Options.EmulatedTLS = true;
Options.ExplicitEmulatedTLS = true;
Options.UseInitArray = true;
}

Expand Down
1 change: 0 additions & 1 deletion llvm/lib/ExecutionEngine/TargetSelect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ TargetMachine *EngineBuilder::selectTarget(const Triple &TargetTriple,
Options, RelocModel, CMModel, OptLevel,
/*JIT*/ true);
Target->Options.EmulatedTLS = EmulatedTLS;
Target->Options.ExplicitEmulatedTLS = true;

assert(Target && "Could not allocate target machine!");
return Target;
Expand Down
8 changes: 1 addition & 7 deletions llvm/lib/Target/TargetMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,7 @@ bool TargetMachine::shouldAssumeDSOLocal(const Module &M,
return false;
}

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

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

0 comments on commit 0d333bf

Please sign in to comment.