Remove ExplicitEmulatedTLS and simplify -femulated-tls handling
authorFangrui Song <i@maskray.me>
Sun, 23 Apr 2023 18:55:12 +0000 (11:55 -0700)
committerFangrui Song <i@maskray.me>
Sun, 23 Apr 2023 18:55:12 +0000 (11:55 -0700)
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.

13 files changed:
clang/include/clang/Driver/Options.td
clang/lib/CodeGen/BackendUtil.cpp
clang/lib/Driver/ToolChains/Clang.cpp
clang/lib/Driver/ToolChains/CommonArgs.cpp
clang/lib/Frontend/CompilerInvocation.cpp
clang/test/Driver/emulated-tls.cpp
llvm/include/llvm/CodeGen/CommandFlags.h
llvm/include/llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h
llvm/include/llvm/Target/TargetOptions.h
llvm/lib/CodeGen/CommandFlags.cpp
llvm/lib/ExecutionEngine/Orc/JITTargetMachineBuilder.cpp
llvm/lib/ExecutionEngine/TargetSelect.cpp
llvm/lib/Target/TargetMachine.cpp

index eed0d51..2abec84 100644 (file)
@@ -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",
index e6e058b..ea38cc4 100644 (file)
@@ -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;
index 56250fc..edea4ac 100644 (file)
@@ -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
index 2f34504..315cf63 100644 (file)
@@ -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,
index 1ecf116..cb29049 100644 (file)
@@ -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);
@@ -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();
index edc68e0..2044bc8 100644 (file)
@@ -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
 
 // 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"
index 19b4666..ff7f1ba 100644 (file)
@@ -111,6 +111,7 @@ std::string getBBSections();
 unsigned getTLSSize();
 
 bool getEmulatedTLS();
+std::optional<bool> getExplicitEmulatedTLS();
 
 bool getUniqueSectionNames();
 
index 39e34e5..8575114 100644 (file)
@@ -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.
@@ -125,9 +123,9 @@ public:
   /// 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;
index 76e4248..5a04a7d 100644 (file)
@@ -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) {}
 
@@ -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;
 
index c58547d..6dafe23 100644 (file)
@@ -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)
@@ -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();
index 27299aa..d0fc364 100644 (file)
@@ -18,7 +18,6 @@ namespace orc {
 JITTargetMachineBuilder::JITTargetMachineBuilder(Triple TT)
     : TT(std::move(TT)) {
   Options.EmulatedTLS = true;
-  Options.ExplicitEmulatedTLS = true;
   Options.UseInitArray = true;
 }
 
index 5ba02d6..b739837 100644 (file)
@@ -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;
index 8d1ad61..2fbd140 100644 (file)
@@ -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;