diff --git a/include/clang/Basic/Attr.td b/include/clang/Basic/Attr.td
index 2e23c64..6b0eb90 100644
--- a/include/clang/Basic/Attr.td
+++ b/include/clang/Basic/Attr.td
@@ -153,6 +153,12 @@ def AlwaysInline : InheritableAttr {
   let Spellings = ["always_inline"];
 }
 
+def TLSModel : InheritableAttr {
+  let Spellings = ["tls_model"];
+  let Subjects = [Var];
+  let Args = [StringArgument<"Model">];
+}
+
 def AnalyzerNoReturn : InheritableAttr {
   let Spellings = ["analyzer_noreturn"];
 }
@@ -846,4 +852,4 @@ def MultipleInheritance : InheritableAttr {
 
 def VirtualInheritance : InheritableAttr {
   let Spellings = ["__virtual_inheritance"];
-}
\ No newline at end of file
+}
diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td
index d3d8238..cf3c50c 100644
--- a/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1587,6 +1587,7 @@ def err_invalid_collection_element : Error<
 def err_box_literal_collection : Error<
   "%select{string|character|boolean|numeric}0 literal must be prefixed by '@' "
   "in a collection">;
+def err_attr_tlsmodel_arg : Error<"tls_model must be %0">;
 
 let CategoryName = "Cocoa API Issue" in {
 def warn_objc_redundant_literal_use : Warning<
@@ -1649,13 +1650,13 @@ def warn_attribute_wrong_decl_type : Warning<
   "functions, methods and blocks|functions, methods, and parameters|"
   "classes|variables|methods|variables, functions and labels|"
   "fields and global variables|structs|"
-  "variables, functions and tag types}1">;
+  "variables, functions and tag types|thread-local variables}1">;
 def err_attribute_wrong_decl_type : Error<
   "%0 attribute only applies to %select{functions|unions|"
   "variables and functions|functions and methods|parameters|"
   "functions, methods and blocks|functions, methods, and parameters|"
   "classes|variables|methods|variables, functions and labels|"
-  "fields and global variables|structs}1">;
+  "fields and global variables|structs|thread-local variables}1">;
 def warn_function_attribute_wrong_type : Warning<
   "'%0' only applies to function types; type here is %1">;
 def warn_pointer_attribute_wrong_type : Warning<
diff --git a/lib/CodeGen/CGDecl.cpp b/lib/CodeGen/CGDecl.cpp
index 4d4b579..39a7789 100644
--- a/lib/CodeGen/CGDecl.cpp
+++ b/lib/CodeGen/CGDecl.cpp
@@ -183,12 +183,32 @@ CodeGenFunction::CreateStaticVarDecl(const VarDecl &D,
   else
     Name = GetStaticDeclName(*this, D, Separator);
 
+  llvm::GlobalVariable::ThreadLocalMode TLM;
+  TLM = D.isThreadSpecified() ? llvm::GlobalVariable::DefaultTLSModel
+                              : llvm::GlobalVariable::NotThreadLocal;
+
+  // Set the TLS mode if it it's explicitly specified.
+  if (D.hasAttr<TLSModelAttr>()) {
+    assert(D.isThreadSpecified() && "Can't have TLS model on non-tls var.");
+    const TLSModelAttr *Attr = D.getAttr<TLSModelAttr>();
+    if (Attr->getModel() == "global-dynamic") {
+       TLM = llvm::GlobalVariable::GlobalDynamicTLSModel;
+    } else if (Attr->getModel() == "local-dynamic") {
+       TLM = llvm::GlobalVariable::LocalDynamicTLSModel;
+    } else if (Attr->getModel() == "initial-exec") {
+       TLM = llvm::GlobalVariable::InitialExecTLSModel;
+    } else if (Attr->getModel() == "local-exec") {
+       TLM = llvm::GlobalVariable::LocalExecTLSModel;
+    } else {
+      llvm_unreachable("unknown TLS model attribute");
+    }
+  }
+
   llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
   llvm::GlobalVariable *GV =
     new llvm::GlobalVariable(CGM.getModule(), LTy,
                              Ty.isConstant(getContext()), Linkage,
-                             CGM.EmitNullConstant(D.getType()), Name, 0,
-                             D.isThreadSpecified(),
+                             CGM.EmitNullConstant(D.getType()), Name, 0, TLM,
                              CGM.getContext().getTargetAddressSpace(Ty));
   GV->setAlignment(getContext().getDeclAlign(&D).getQuantity());
   if (Linkage != llvm::GlobalValue::InternalLinkage)
@@ -239,7 +259,7 @@ CodeGenFunction::AddInitializerToStaticVarDecl(const VarDecl &D,
                                   OldGV->isConstant(),
                                   OldGV->getLinkage(), Init, "",
                                   /*InsertBefore*/ OldGV,
-                                  D.isThreadSpecified(),
+                                  OldGV->getThreadLocalMode(),
                            CGM.getContext().getTargetAddressSpace(D.getType()));
     GV->setVisibility(OldGV->getVisibility());
 
@@ -1058,7 +1078,7 @@ void CodeGenFunction::EmitAutoVarInit(const AutoVarEmission &emission) {
     llvm::GlobalVariable *GV =
       new llvm::GlobalVariable(CGM.getModule(), constant->getType(), true,
                                llvm::GlobalValue::PrivateLinkage,
-                               constant, Name, 0, false, 0);
+                               constant, Name);
     GV->setAlignment(alignment.getQuantity());
     GV->setUnnamedAddr(true);
 
diff --git a/lib/CodeGen/CGExprConstant.cpp b/lib/CodeGen/CGExprConstant.cpp
index 4e308ae..508a036 100644
--- a/lib/CodeGen/CGExprConstant.cpp
+++ b/lib/CodeGen/CGExprConstant.cpp
@@ -932,7 +932,8 @@ public:
         C = new llvm::GlobalVariable(CGM.getModule(), C->getType(),
                                      E->getType().isConstant(CGM.getContext()),
                                      llvm::GlobalValue::InternalLinkage,
-                                     C, ".compoundliteral", 0, false,
+                                     C, ".compoundliteral", 0,
+                                     llvm::GlobalVariable::NotThreadLocal,
                           CGM.getContext().getTargetAddressSpace(E->getType()));
       return C;
     }
diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp
index d1f2fac..fa7bcdb 100644
--- a/lib/CodeGen/CodeGenModule.cpp
+++ b/lib/CodeGen/CodeGenModule.cpp
@@ -1176,7 +1176,7 @@ CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName,
     new llvm::GlobalVariable(getModule(), Ty->getElementType(), false,
                              llvm::GlobalValue::ExternalLinkage,
                              0, MangledName, 0,
-                             false, AddrSpace);
+                             llvm::GlobalVariable::NotThreadLocal, AddrSpace);
 
   // Handle things which are present even on external declarations.
   if (D) {
@@ -1200,6 +1200,22 @@ CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName,
     }
 
     GV->setThreadLocal(D->isThreadSpecified());
+
+    // Set the TLS mode if it it's explicitly specified.
+    if (D->hasAttr<TLSModelAttr>()) {
+      const TLSModelAttr *Attr = D->getAttr<TLSModelAttr>();
+      if (Attr->getModel() == "global-dynamic") {
+        GV->setThreadLocalMode(llvm::GlobalVariable::GlobalDynamicTLSModel);
+      } else if (Attr->getModel() == "local-dynamic") {
+        GV->setThreadLocalMode(llvm::GlobalVariable::LocalDynamicTLSModel);
+      } else if (Attr->getModel() == "initial-exec") {
+        GV->setThreadLocalMode(llvm::GlobalVariable::InitialExecTLSModel);
+      } else if (Attr->getModel() == "local-exec") {
+        GV->setThreadLocalMode(llvm::GlobalVariable::LocalExecTLSModel);
+      } else {
+        llvm_unreachable("unknown TLS model attribute");
+      }
+    }
   }
 
   if (AddrSpace != Ty->getAddressSpace())
diff --git a/lib/Sema/SemaDeclAttr.cpp b/lib/Sema/SemaDeclAttr.cpp
index 0f95747..44531fb 100644
--- a/lib/Sema/SemaDeclAttr.cpp
+++ b/lib/Sema/SemaDeclAttr.cpp
@@ -43,7 +43,8 @@ enum AttributeDeclKind {
   ExpectedMethod,
   ExpectedVariableFunctionOrLabel,
   ExpectedFieldOrGlobalVar,
-  ExpectedStruct
+  ExpectedStruct,
+  ExpectedTLSVar
 };
 
 //===----------------------------------------------------------------------===//
@@ -1440,6 +1441,42 @@ static void handleAlwaysInlineAttr(Sema &S, Decl *D,
   D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getRange(), S.Context));
 }
 
+static void handleTLSModelAttr(Sema &S, Decl *D,
+                               const AttributeList &Attr) {
+  // Check the attribute arguments.
+  if (Attr.getNumArgs() != 1) {
+    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
+    return;
+  }
+
+  Expr *Arg = Attr.getArg(0);
+  Arg = Arg->IgnoreParenCasts();
+  StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
+
+  // Check that it is a string.
+  if (!Str) {
+    S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model";
+    return;
+  }
+
+  if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->isThreadSpecified()) {
+    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
+      << Attr.getName() << ExpectedTLSVar;
+    return;
+  }
+
+  StringRef Model = Str->getString();
+  if (Model == "global-dynamic" || Model == "local-dynamic"
+      || Model == "initial-exec" || Model == "local-exec") {
+    D->addAttr(::new (S.Context) TLSModelAttr(Attr.getRange(), S.Context,
+                                              Model));
+  } else {
+    S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg)
+        << "\"global-dynamic\", \"local-dynamic\", "
+        "\"initial-exec\" or \"local-exec\"";
+  }
+}
+
 static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
   // Check the attribute arguments.
   if (Attr.hasParameterOrArguments()) {
@@ -3930,6 +3967,7 @@ static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
   case AttributeList::AT_alloc_size:  handleAllocSizeAttr   (S, D, Attr); break;
   case AttributeList::AT_always_inline:
     handleAlwaysInlineAttr  (S, D, Attr); break;
+  case AttributeList::AT_tls_model:   handleTLSModelAttr    (S, D, Attr); break;
   case AttributeList::AT_analyzer_noreturn:
     handleAnalyzerNoReturnAttr  (S, D, Attr); break;
   case AttributeList::AT_annotate:    handleAnnotateAttr    (S, D, Attr); break;
diff --git a/test/CodeGen/thread-specifier.c b/test/CodeGen/thread-specifier.c
index a1f3e16..85ac505 100644
--- a/test/CodeGen/thread-specifier.c
+++ b/test/CodeGen/thread-specifier.c
@@ -3,7 +3,13 @@
 // CHECK: @b = external thread_local global
 // CHECK: @d.e = internal thread_local global
 // CHECK: @d.f = internal thread_local global
+// CHECK: @f.a = internal thread_local(initialexec) global
 // CHECK: @a = thread_local global
+// CHECK: @g = thread_local(globaldynamic) global
+// CHECK: @h = thread_local(localdynamic) global
+// CHECK: @i = thread_local(initialexec) global
+// CHECK: @j = thread_local(localexec) global
+
 __thread int a;
 extern __thread int b;
 int c() { return *&b; }
@@ -13,3 +19,12 @@ int d() {
   return 0;
 }
 
+__thread int g __attribute__((tls_model("global-dynamic")));
+__thread int h __attribute__((tls_model("local-dynamic")));
+__thread int i __attribute__((tls_model("initial-exec")));
+__thread int j __attribute__((tls_model("local-exec")));
+
+int f() {
+  __thread static int a __attribute__((tls_model("initial-exec")));
+  return a++;
+}
diff --git a/test/Sema/attr-tls_model.c b/test/Sema/attr-tls_model.c
new file mode 100644
index 0000000..e883e8f
--- /dev/null
+++ b/test/Sema/attr-tls_model.c
@@ -0,0 +1,10 @@
+// RUN: %clang -Xclang -verify -fsyntax-only %s
+
+int f() __attribute((tls_model("global-dynamic"))); // expected-error {{'tls_model' attribute only applies to thread-local variables}}
+
+int x __attribute((tls_model("global-dynamic"))); // expected-error {{'tls_model' attribute only applies to thread-local variables}}
+static __thread int y __attribute((tls_model("global-dynamic"))); // no-warning
+
+static __thread int y __attribute((tls_model("local", "dynamic"))); // expected-error {{attribute takes one argument}}
+static __thread int y __attribute((tls_model(123))); // expected-error {{argument to tls_model attribute was not a string literal}}
+static __thread int y __attribute((tls_model("foobar"))); // expected-error {{tls_model must be "global-dynamic", "local-dynamic", "initial-exec" or "local-exec"}}
