[PATCH] D53102: Support for the mno-tls-direct-seg-refs flag

2018-10-18 Thread Ruslan Nikolaev via Phabricator via cfe-commits
nruslan added a comment.

@kristina : Thank you very much for taking care of the patchsets!


Repository:
  rC Clang

https://reviews.llvm.org/D53102



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D53102: Support for the mno-tls-direct-seg-refs flag

2018-10-18 Thread Kristina Brooks via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rC344739: Add support for -mno-tls-direct-seg-refs to Clang 
(authored by kristina, committed by ).

Changed prior to commit:
  https://reviews.llvm.org/D53102?vs=169224&id=170082#toc

Repository:
  rC Clang

https://reviews.llvm.org/D53102

Files:
  include/clang/Driver/Options.td
  include/clang/Frontend/CodeGenOptions.def
  lib/CodeGen/CGCall.cpp
  lib/Driver/ToolChains/Clang.cpp
  lib/Frontend/CompilerInvocation.cpp
  test/CodeGen/indirect-tls-seg-refs.c
  test/Driver/indirect-tls-seg-refs.c


Index: include/clang/Frontend/CodeGenOptions.def
===
--- include/clang/Frontend/CodeGenOptions.def
+++ include/clang/Frontend/CodeGenOptions.def
@@ -62,6 +62,8 @@
 CODEGENOPT(DebugPassManager, 1, 0) ///< Prints debug information for the new
///< pass manager.
 CODEGENOPT(DisableRedZone, 1, 0) ///< Set when -mno-red-zone is enabled.
+CODEGENOPT(IndirectTlsSegRefs, 1, 0) ///< Set when -mno-tls-direct-seg-refs
+ ///< is specified.
 CODEGENOPT(DisableTailCalls  , 1, 0) ///< Do not emit tail calls.
 CODEGENOPT(NoEscapingBlockTailCalls, 1, 0) ///< Do not emit tail calls from
///< escaping blocks.
Index: include/clang/Driver/Options.td
===
--- include/clang/Driver/Options.td
+++ include/clang/Driver/Options.td
@@ -2011,6 +2011,8 @@
 def mno_pascal_strings : Flag<["-"], "mno-pascal-strings">,
   Alias;
 def mno_red_zone : Flag<["-"], "mno-red-zone">, Group;
+def mno_tls_direct_seg_refs : Flag<["-"], "mno-tls-direct-seg-refs">, 
Group, Flags<[CC1Option]>,
+  HelpText<"Disable direct TLS access through segment registers">;
 def mno_relax_all : Flag<["-"], "mno-relax-all">, Group;
 def mno_rtd: Flag<["-"], "mno-rtd">, Group;
 def mno_soft_float : Flag<["-"], "mno-soft-float">, Group;
@@ -2171,6 +2173,8 @@
 def moslib_EQ : Joined<["-"], "moslib=">, Group;
 def mpascal_strings : Flag<["-"], "mpascal-strings">, Alias;
 def mred_zone : Flag<["-"], "mred-zone">, Group;
+def mtls_direct_seg_refs : Flag<["-"], "mtls-direct-seg-refs">, Group,
+  HelpText<"Enable direct TLS access through segment registers (default)">;
 def mregparm_EQ : Joined<["-"], "mregparm=">, Group;
 def mrelax_all : Flag<["-"], "mrelax-all">, Group, 
Flags<[CC1Option,CC1AsOption]>,
   HelpText<"(integrated-as) Relax all machine instructions">;
Index: test/CodeGen/indirect-tls-seg-refs.c
===
--- test/CodeGen/indirect-tls-seg-refs.c
+++ test/CodeGen/indirect-tls-seg-refs.c
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 %s -emit-llvm -o - -mno-tls-direct-seg-refs | FileCheck %s 
-check-prefix=NO-TLSDIRECT
+// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s -check-prefix=TLSDIRECT
+
+// NO-TLSDIRECT: attributes #{{[0-9]+}} = {{{.*}} "indirect-tls-seg-refs"
+// TLSDIRECT-NOT: attributes #{{[0-9]+}} = {{{.*}} "indirect-tls-seg-refs"
+
+void test1() {
+}
Index: test/Driver/indirect-tls-seg-refs.c
===
--- test/Driver/indirect-tls-seg-refs.c
+++ test/Driver/indirect-tls-seg-refs.c
@@ -0,0 +1,7 @@
+// RUN: %clang -### %s 2>&1 | FileCheck %s -check-prefix=TLSDIRECT
+// RUN: %clang -### -mno-tls-direct-seg-refs -mtls-direct-seg-refs %s 2>&1 | 
FileCheck %s -check-prefix=TLSDIRECT
+// RUN: %clang -### -mtls-direct-seg-refs -mno-tls-direct-seg-refs %s 2>&1 | 
FileCheck %s -check-prefix=NO-TLSDIRECT
+// REQUIRES: clang-driver
+
+// NO-TLSDIRECT: -mno-tls-direct-seg-refs
+// TLSDIRECT-NOT: -mno-tls-direct-seg-refs
Index: lib/CodeGen/CGCall.cpp
===
--- lib/CodeGen/CGCall.cpp
+++ lib/CodeGen/CGCall.cpp
@@ -1709,6 +1709,8 @@
 
   if (CodeGenOpts.DisableRedZone)
 FuncAttrs.addAttribute(llvm::Attribute::NoRedZone);
+  if (CodeGenOpts.IndirectTlsSegRefs)
+FuncAttrs.addAttribute("indirect-tls-seg-refs");
   if (CodeGenOpts.NoImplicitFloat)
 FuncAttrs.addAttribute(llvm::Attribute::NoImplicitFloat);
 
Index: lib/Driver/ToolChains/Clang.cpp
===
--- lib/Driver/ToolChains/Clang.cpp
+++ lib/Driver/ToolChains/Clang.cpp
@@ -1739,6 +1739,10 @@
   Args.hasArg(options::OPT_fapple_kext))
 CmdArgs.push_back("-disable-red-zone");
 
+  if (!Args.hasFlag(options::OPT_mtls_direct_seg_refs,
+options::OPT_mno_tls_direct_seg_refs, true))
+CmdArgs.push_back("-mno-tls-direct-seg-refs");
+
   // Default to avoid implicit floating-point for kernel/kext code, but allow
   // that to be overridden with -mno-soft-float.
   bool NoImplicitFloat = (Args.hasArg(options::OPT_mkernel) ||
Index: lib/Frontend/CompilerInvocation.cpp
==

[PATCH] D53102: Support for the mno-tls-direct-seg-refs flag

2018-10-18 Thread Ruslan Nikolaev via Phabricator via cfe-commits
nruslan added a comment.

In https://reviews.llvm.org/D53102#1268364, @kristina wrote:

> By the way, out of curiosity is this for anything specific (alternative libc 
> or some user-mode-scheduling implementation)? Not nitpicking, just curious 
> since it's an interesting topic in general and it's frustrating that the 
> architecture is so limited in terms of registers that can be used for 
> TLS/related data unlike newer ARM/ARM64 architectures.
>
> Also FWIW `%gs` is generally free to use under x86_64 Linux which is where I 
> usually place my thread control blocks which doesn't interfere with libc 
> which uses `%fs`. (Just started build/test job, waiting on that for now)


@kristina : Yes, there are some recent use cases mentioned in the bug report 
(see the link above). Also, I know that it was used for x86 (32-bit only) Xen 
guests where it would be recommended to compile an OS with this flag. I also 
used it in my fork of the NPTL pthread library in VirtuOS to support M:N 
threading model: http://sigops.org/sosp/sosp13/papers/p116-nikolaev.pdf


https://reviews.llvm.org/D53102



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D53102: Support for the mno-tls-direct-seg-refs flag

2018-10-18 Thread Kristina Brooks via Phabricator via cfe-commits
kristina added a comment.

By the way, out of curiosity is this for anything specific (alternative libc or 
some user-mode-scheduling implementation)? Not nitpicking, just curious since 
it's an interesting topic in general and it's frustrating that the architecture 
is so limited in terms of registers that can be used for TLS/related data 
unlike newer ARM/ARM64 architectures.

Also FWIW `%gs` is generally free to use under x86_64 Linux which is where I 
usually place my thread control blocks which doesn't interfere with libc which 
uses `%fs`. (Just started build/test job, waiting on that for now)


https://reviews.llvm.org/D53102



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D53102: Support for the mno-tls-direct-seg-refs flag

2018-10-18 Thread Ruslan Nikolaev via Phabricator via cfe-commits
nruslan added a comment.

In https://reviews.llvm.org/D53102#1268272, @kristina wrote:

> If the author doesn't mind I can just apply the style fix after patching and 
> then rebuild and run all the relevant tests (or would you prefer to do 
> that?). Seems easier than a new revision for an indentation change on one 
> line.


@kristina : Thank you! Sure, that is fine. Go ahead.


https://reviews.llvm.org/D53102



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D53102: Support for the mno-tls-direct-seg-refs flag

2018-10-18 Thread Kristina Brooks via Phabricator via cfe-commits
kristina added a comment.

If the author doesn't mind I can just apply the style fix after patching and 
then rebuild and run all the relevant tests (or would you prefer to do that?). 
Seems easier than a new revision for an indentation change on one line.


https://reviews.llvm.org/D53102



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D53102: Support for the mno-tls-direct-seg-refs flag

2018-10-17 Thread Craig Topper via Phabricator via cfe-commits
craig.topper accepted this revision.
craig.topper added a comment.
This revision is now accepted and ready to land.

LGTM with that one style comment fixed.




Comment at: lib/Driver/ToolChains/Clang.cpp:1743
+  if (!Args.hasFlag(options::OPT_mtls_direct_seg_refs,
+  options::OPT_mno_tls_direct_seg_refs, true))
+CmdArgs.push_back("-mno-tls-direct-seg-refs");

minor - I think this line should be lined up with the start of the arguments to 
hasFlags on the previous line.


https://reviews.llvm.org/D53102



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D53102: Support for the mno-tls-direct-seg-refs flag

2018-10-17 Thread Kristina Brooks via Phabricator via cfe-commits
kristina added a comment.

@nruslan This patchset is still pending review so be patient, I landed 
https://reviews.llvm.org/D53103 as it was reviewed and accepted by the code 
owner, on which this patch depends on. That said it LGTM, it's simple enough as 
it just forwards the argument to the backend, but still would rather have the 
X86 code owner sign off on this as well.


https://reviews.llvm.org/D53102



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D53102: Support for the mno-tls-direct-seg-refs flag

2018-10-11 Thread Ruslan Nikolaev via Phabricator via cfe-commits
nruslan added a comment.

@hans, @craig.topper : If everything is fine, can someone check in the clang 
and llvm patches on my behalf? I do not have commit access.


https://reviews.llvm.org/D53102



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D53102: Support for the mno-tls-direct-seg-refs flag

2018-10-11 Thread Ruslan Nikolaev via Phabricator via cfe-commits
nruslan updated this revision to Diff 169224.
nruslan marked 3 inline comments as done.
nruslan added a comment.

@hans , @craig.topper : Updated the patch with all requested changes


https://reviews.llvm.org/D53102

Files:
  include/clang/Driver/Options.td
  include/clang/Frontend/CodeGenOptions.def
  lib/CodeGen/CGCall.cpp
  lib/Driver/ToolChains/Clang.cpp
  lib/Frontend/CompilerInvocation.cpp
  test/CodeGen/indirect-tls-seg-refs.c
  test/Driver/indirect-tls-seg-refs.c


Index: test/Driver/indirect-tls-seg-refs.c
===
--- /dev/null
+++ test/Driver/indirect-tls-seg-refs.c
@@ -0,0 +1,7 @@
+// RUN: %clang -### %s 2>&1 | FileCheck %s -check-prefix=TLSDIRECT
+// RUN: %clang -### -mno-tls-direct-seg-refs -mtls-direct-seg-refs %s 2>&1 | 
FileCheck %s -check-prefix=TLSDIRECT
+// RUN: %clang -### -mtls-direct-seg-refs -mno-tls-direct-seg-refs %s 2>&1 | 
FileCheck %s -check-prefix=NO-TLSDIRECT
+// REQUIRES: clang-driver
+
+// NO-TLSDIRECT: -mno-tls-direct-seg-refs
+// TLSDIRECT-NOT: -mno-tls-direct-seg-refs
Index: test/CodeGen/indirect-tls-seg-refs.c
===
--- /dev/null
+++ test/CodeGen/indirect-tls-seg-refs.c
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 %s -emit-llvm -o - -mno-tls-direct-seg-refs | FileCheck %s 
-check-prefix=NO-TLSDIRECT
+// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s -check-prefix=TLSDIRECT
+
+// NO-TLSDIRECT: attributes #{{[0-9]+}} = {{{.*}} "indirect-tls-seg-refs"
+// TLSDIRECT-NOT: attributes #{{[0-9]+}} = {{{.*}} "indirect-tls-seg-refs"
+
+void test1() {
+}
Index: lib/Frontend/CompilerInvocation.cpp
===
--- lib/Frontend/CompilerInvocation.cpp
+++ lib/Frontend/CompilerInvocation.cpp
@@ -614,6 +614,7 @@
   Opts.DisableLifetimeMarkers = Args.hasArg(OPT_disable_lifetimemarkers);
   Opts.DisableO0ImplyOptNone = Args.hasArg(OPT_disable_O0_optnone);
   Opts.DisableRedZone = Args.hasArg(OPT_disable_red_zone);
+  Opts.IndirectTlsSegRefs = Args.hasArg(OPT_mno_tls_direct_seg_refs);
   Opts.ForbidGuardVariables = Args.hasArg(OPT_fforbid_guard_variables);
   Opts.UseRegisterSizedBitfieldAccess = Args.hasArg(
 OPT_fuse_register_sized_bitfield_access);
Index: lib/Driver/ToolChains/Clang.cpp
===
--- lib/Driver/ToolChains/Clang.cpp
+++ lib/Driver/ToolChains/Clang.cpp
@@ -1739,6 +1739,10 @@
   Args.hasArg(options::OPT_fapple_kext))
 CmdArgs.push_back("-disable-red-zone");
 
+  if (!Args.hasFlag(options::OPT_mtls_direct_seg_refs,
+  options::OPT_mno_tls_direct_seg_refs, true))
+CmdArgs.push_back("-mno-tls-direct-seg-refs");
+
   // Default to avoid implicit floating-point for kernel/kext code, but allow
   // that to be overridden with -mno-soft-float.
   bool NoImplicitFloat = (Args.hasArg(options::OPT_mkernel) ||
Index: lib/CodeGen/CGCall.cpp
===
--- lib/CodeGen/CGCall.cpp
+++ lib/CodeGen/CGCall.cpp
@@ -1709,6 +1709,8 @@
 
   if (CodeGenOpts.DisableRedZone)
 FuncAttrs.addAttribute(llvm::Attribute::NoRedZone);
+  if (CodeGenOpts.IndirectTlsSegRefs)
+FuncAttrs.addAttribute("indirect-tls-seg-refs");
   if (CodeGenOpts.NoImplicitFloat)
 FuncAttrs.addAttribute(llvm::Attribute::NoImplicitFloat);
 
Index: include/clang/Frontend/CodeGenOptions.def
===
--- include/clang/Frontend/CodeGenOptions.def
+++ include/clang/Frontend/CodeGenOptions.def
@@ -62,6 +62,8 @@
 CODEGENOPT(DebugPassManager, 1, 0) ///< Prints debug information for the new
///< pass manager.
 CODEGENOPT(DisableRedZone, 1, 0) ///< Set when -mno-red-zone is enabled.
+CODEGENOPT(IndirectTlsSegRefs, 1, 0) ///< Set when -mno-tls-direct-seg-refs
+ ///< is specified.
 CODEGENOPT(DisableTailCalls  , 1, 0) ///< Do not emit tail calls.
 CODEGENOPT(NoEscapingBlockTailCalls, 1, 0) ///< Do not emit tail calls from
///< escaping blocks.
Index: include/clang/Driver/Options.td
===
--- include/clang/Driver/Options.td
+++ include/clang/Driver/Options.td
@@ -2006,6 +2006,8 @@
 def mno_pascal_strings : Flag<["-"], "mno-pascal-strings">,
   Alias;
 def mno_red_zone : Flag<["-"], "mno-red-zone">, Group;
+def mno_tls_direct_seg_refs : Flag<["-"], "mno-tls-direct-seg-refs">, 
Group, Flags<[CC1Option]>,
+  HelpText<"Disable direct TLS access through segment registers">;
 def mno_relax_all : Flag<["-"], "mno-relax-all">, Group;
 def mno_rtd: Flag<["-"], "mno-rtd">, Group;
 def mno_soft_float : Flag<["-"], "mno-soft-float">, Group;
@@ -2161,6 +2163,8 @@
 def moslib_EQ : Joined<["-"], "moslib=">, Group;
 def mpascal_strings : Flag<["-"], "mpascal-strings">, Alias;
 def mred_zone : Flag<["-"], "mr

[PATCH] D53102: Support for the mno-tls-direct-seg-refs flag

2018-10-11 Thread Ruslan Nikolaev via Phabricator via cfe-commits
nruslan added inline comments.



Comment at: docs/ClangCommandLineReference.rst:2241
+
+Enable or disable direct TLS access through segment registers
+

hans wrote:
> This file is automatically generated based on the options .td files, so no 
> need to update it here.
ok



Comment at: include/clang/Driver/CC1Options.td:195
   HelpText<"Do not emit code that uses the red zone.">;
+def indirect_tls_seg_refs : Flag<["-"], "indirect-tls-seg-refs">,
+  HelpText<"Do not emit code that uses direct TLS segment access.">;

hans wrote:
> Could mno_tls_direct_seg_refs be used as a cc1 flag instead?
done



Comment at: include/clang/Driver/Options.td:2167
+def mtls_direct_seg_refs : Flag<["-"], "mtls-direct-seg-refs">, Group,
+  HelpText<"Enable direct TLS access through segment registers">;
 def mregparm_EQ : Joined<["-"], "mregparm=">, Group;

hans wrote:
> Maybe add (default) to the help text to indicate this is the default?
done


https://reviews.llvm.org/D53102



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D53102: Support for the mno-tls-direct-seg-refs flag

2018-10-11 Thread Hans Wennborg via Phabricator via cfe-commits
hans added a comment.

Looking good, just a few minor comments.




Comment at: docs/ClangCommandLineReference.rst:2241
+
+Enable or disable direct TLS access through segment registers
+

This file is automatically generated based on the options .td files, so no need 
to update it here.



Comment at: include/clang/Driver/CC1Options.td:195
   HelpText<"Do not emit code that uses the red zone.">;
+def indirect_tls_seg_refs : Flag<["-"], "indirect-tls-seg-refs">,
+  HelpText<"Do not emit code that uses direct TLS segment access.">;

Could mno_tls_direct_seg_refs be used as a cc1 flag instead?



Comment at: include/clang/Driver/Options.td:2167
+def mtls_direct_seg_refs : Flag<["-"], "mtls-direct-seg-refs">, Group,
+  HelpText<"Enable direct TLS access through segment registers">;
 def mregparm_EQ : Joined<["-"], "mregparm=">, Group;

Maybe add (default) to the help text to indicate this is the default?


Repository:
  rC Clang

https://reviews.llvm.org/D53102



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D53102: Support for the mno-tls-direct-seg-refs flag

2018-10-10 Thread Ruslan Nikolaev via Phabricator via cfe-commits
nruslan created this revision.
nruslan added reviewers: hans, craig.topper.
Herald added a subscriber: cfe-commits.

Allows to disable direct TLS segment access (%fs or %gs). GCC supports a 
similar flag, it can be useful in some circumstances, e.g. when a thread 
context block needs to be updated directly from user space. More info and 
specific use cases: https://bugs.llvm.org/show_bug.cgi?id=16145

There is another revision for LLVM as well.


Repository:
  rC Clang

https://reviews.llvm.org/D53102

Files:
  docs/ClangCommandLineReference.rst
  include/clang/Driver/CC1Options.td
  include/clang/Driver/Options.td
  include/clang/Frontend/CodeGenOptions.def
  lib/CodeGen/CGCall.cpp
  lib/Driver/ToolChains/Clang.cpp
  lib/Frontend/CompilerInvocation.cpp
  test/CodeGen/indirect-tls-seg-refs.c
  test/Driver/indirect-tls-seg-refs.c

Index: test/Driver/indirect-tls-seg-refs.c
===
--- /dev/null
+++ test/Driver/indirect-tls-seg-refs.c
@@ -0,0 +1,7 @@
+// RUN: %clang -### %s 2>&1 | FileCheck %s -check-prefix=TLSDIRECT
+// RUN: %clang -### -mno-tls-direct-seg-refs -mtls-direct-seg-refs %s 2>&1 | FileCheck %s -check-prefix=TLSDIRECT
+// RUN: %clang -### -mtls-direct-seg-refs -mno-tls-direct-seg-refs %s 2>&1 | FileCheck %s -check-prefix=NO-TLSDIRECT
+// REQUIRES: clang-driver
+
+// NO-TLSDIRECT: -indirect-tls-seg-refs
+// TLSDIRECT-NOT: -indirect-tls-seg-refs
Index: test/CodeGen/indirect-tls-seg-refs.c
===
--- /dev/null
+++ test/CodeGen/indirect-tls-seg-refs.c
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 %s -emit-llvm -o - -indirect-tls-seg-refs | FileCheck %s -check-prefix=NO-TLSDIRECT
+// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s -check-prefix=TLSDIRECT
+
+// NO-TLSDIRECT: attributes #{{[0-9]+}} = {{{.*}} "indirect-tls-seg-refs"
+// TLSDIRECT-NOT: attributes #{{[0-9]+}} = {{{.*}} "indirect-tls-seg-refs"
+
+void test1() {
+}
Index: lib/Frontend/CompilerInvocation.cpp
===
--- lib/Frontend/CompilerInvocation.cpp
+++ lib/Frontend/CompilerInvocation.cpp
@@ -614,6 +614,7 @@
   Opts.DisableLifetimeMarkers = Args.hasArg(OPT_disable_lifetimemarkers);
   Opts.DisableO0ImplyOptNone = Args.hasArg(OPT_disable_O0_optnone);
   Opts.DisableRedZone = Args.hasArg(OPT_disable_red_zone);
+  Opts.IndirectTlsSegRefs = Args.hasArg(OPT_indirect_tls_seg_refs);
   Opts.ForbidGuardVariables = Args.hasArg(OPT_fforbid_guard_variables);
   Opts.UseRegisterSizedBitfieldAccess = Args.hasArg(
 OPT_fuse_register_sized_bitfield_access);
Index: lib/Driver/ToolChains/Clang.cpp
===
--- lib/Driver/ToolChains/Clang.cpp
+++ lib/Driver/ToolChains/Clang.cpp
@@ -1739,6 +1739,10 @@
   Args.hasArg(options::OPT_fapple_kext))
 CmdArgs.push_back("-disable-red-zone");
 
+  if (!Args.hasFlag(options::OPT_mtls_direct_seg_refs,
+  options::OPT_mno_tls_direct_seg_refs, true))
+CmdArgs.push_back("-indirect-tls-seg-refs");
+
   // Default to avoid implicit floating-point for kernel/kext code, but allow
   // that to be overridden with -mno-soft-float.
   bool NoImplicitFloat = (Args.hasArg(options::OPT_mkernel) ||
Index: lib/CodeGen/CGCall.cpp
===
--- lib/CodeGen/CGCall.cpp
+++ lib/CodeGen/CGCall.cpp
@@ -1709,6 +1709,8 @@
 
   if (CodeGenOpts.DisableRedZone)
 FuncAttrs.addAttribute(llvm::Attribute::NoRedZone);
+  if (CodeGenOpts.IndirectTlsSegRefs)
+FuncAttrs.addAttribute("indirect-tls-seg-refs");
   if (CodeGenOpts.NoImplicitFloat)
 FuncAttrs.addAttribute(llvm::Attribute::NoImplicitFloat);
 
Index: include/clang/Frontend/CodeGenOptions.def
===
--- include/clang/Frontend/CodeGenOptions.def
+++ include/clang/Frontend/CodeGenOptions.def
@@ -62,6 +62,8 @@
 CODEGENOPT(DebugPassManager, 1, 0) ///< Prints debug information for the new
///< pass manager.
 CODEGENOPT(DisableRedZone, 1, 0) ///< Set when -mno-red-zone is enabled.
+CODEGENOPT(IndirectTlsSegRefs, 1, 0) ///< Set when -mno-tls-direct-seg-refs
+ ///< is specified.
 CODEGENOPT(DisableTailCalls  , 1, 0) ///< Do not emit tail calls.
 CODEGENOPT(NoEscapingBlockTailCalls, 1, 0) ///< Do not emit tail calls from
///< escaping blocks.
Index: include/clang/Driver/Options.td
===
--- include/clang/Driver/Options.td
+++ include/clang/Driver/Options.td
@@ -2006,6 +2006,8 @@
 def mno_pascal_strings : Flag<["-"], "mno-pascal-strings">,
   Alias;
 def mno_red_zone : Flag<["-"], "mno-red-zone">, Group;
+def mno_tls_direct_seg_refs : Flag<["-"], "mno-tls-direct-seg-refs">, Group,
+  HelpText<"Disable direct TLS access through segment registers">;