diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp
index a36fcf2..c799161 100644
--- a/lib/AST/Decl.cpp
+++ b/lib/AST/Decl.cpp
@@ -1043,9 +1043,13 @@ static LinkageInfo getLVForLocalDecl(const NamedDecl *D,
 
       return LV;
     }
+
+    if (!Var->isStaticLocal())
+      return LinkageInfo::none();
   }
 
-  if (!isa<TagDecl>(D))
+  ASTContext &Context = D->getASTContext();
+  if (!Context.getLangOpts().CPlusPlus)
     return LinkageInfo::none();
 
   const FunctionDecl *FD = getOutermostFunctionContext(D);
diff --git a/lib/CodeGen/CGDecl.cpp b/lib/CodeGen/CGDecl.cpp
index 4b19b54..020b0d4 100644
--- a/lib/CodeGen/CGDecl.cpp
+++ b/lib/CodeGen/CGDecl.cpp
@@ -122,17 +122,8 @@ void CodeGenFunction::EmitDecl(const Decl &D) {
 void CodeGenFunction::EmitVarDecl(const VarDecl &D) {
   if (D.isStaticLocal()) {
     llvm::GlobalValue::LinkageTypes Linkage =
-      llvm::GlobalValue::InternalLinkage;
-
-    // If the function definition has some sort of weak linkage, its
-    // static variables should also be weak so that they get properly
-    // uniqued.  We can't do this in C, though, because there's no
-    // standard way to agree on which variables are the same (i.e.
-    // there's no mangling).
-    if (getLangOpts().CPlusPlus)
-      if (llvm::GlobalValue::isWeakForLinker(CurFn->getLinkage()))
-        Linkage = CurFn->getLinkage();
-
+        D.isExternallyVisible() ? CurFn->getLinkage()
+                                : llvm::GlobalValue::InternalLinkage;
     return EmitStaticVarDecl(D, Linkage);
   }
 
diff --git a/test/CodeGenCXX/linkage.cpp b/test/CodeGenCXX/linkage.cpp
index 7c67029..ccc4fd6 100644
--- a/test/CodeGenCXX/linkage.cpp
+++ b/test/CodeGenCXX/linkage.cpp
@@ -209,3 +209,13 @@ namespace test16 {
   }
   void *test() { return foo<int>::bar(); }
 }
+
+namespace test17 {
+  // CHECK-DAG: define weak_odr i32* @_ZN6test173fooILi42EEEPiv(
+  template<int I>
+  int *foo() {
+    static int bar;
+    return &bar;
+  }
+  template int *foo<42>();
+}
