r218503 - Small fix for bug 18635.

Alexander Musman alexander.musman at gmail.com
Thu Sep 25 23:28:25 PDT 2014


Author: amusman
Date: Fri Sep 26 01:28:25 2014
New Revision: 218503

URL: http://llvm.org/viewvc/llvm-project?rev=218503&view=rev
Log:
Small fix for bug 18635.
(clang crashed in CodeGen in llvm::Module::getNamedValue on
 thread_local std::unique_ptr<int>).
Differential Revision: http://reviews.llvm.org/D5353

Added:
    cfe/trunk/test/CodeGenCXX/pr18635.cpp
Modified:
    cfe/trunk/lib/CodeGen/ItaniumCXXABI.cpp

Modified: cfe/trunk/lib/CodeGen/ItaniumCXXABI.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/ItaniumCXXABI.cpp?rev=218503&r1=218502&r2=218503&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/ItaniumCXXABI.cpp (original)
+++ cfe/trunk/lib/CodeGen/ItaniumCXXABI.cpp Fri Sep 26 01:28:25 2014
@@ -235,7 +235,7 @@ public:
                           llvm::Constant *dtor, llvm::Constant *addr) override;
 
   llvm::Function *getOrCreateThreadLocalWrapper(const VarDecl *VD,
-                                                llvm::GlobalVariable *Var);
+                                                llvm::Value *Val);
   void EmitThreadLocalInitFuncs(
       ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *> > Decls,
       llvm::Function *InitFunc) override;
@@ -1870,7 +1870,7 @@ getThreadLocalWrapperLinkage(const VarDe
 
 llvm::Function *
 ItaniumCXXABI::getOrCreateThreadLocalWrapper(const VarDecl *VD,
-                                             llvm::GlobalVariable *Var) {
+                                             llvm::Value *Val) {
   // Mangle the name for the thread_local wrapper function.
   SmallString<256> WrapperName;
   {
@@ -1879,10 +1879,10 @@ ItaniumCXXABI::getOrCreateThreadLocalWra
     Out.flush();
   }
 
-  if (llvm::Value *V = Var->getParent()->getNamedValue(WrapperName))
+  if (llvm::Value *V = CGM.getModule().getNamedValue(WrapperName))
     return cast<llvm::Function>(V);
 
-  llvm::Type *RetTy = Var->getType();
+  llvm::Type *RetTy = Val->getType();
   if (VD->getType()->isReferenceType())
     RetTy = RetTy->getPointerElementType();
 
@@ -1970,7 +1970,9 @@ void ItaniumCXXABI::EmitThreadLocalInitF
       LI->setAlignment(CGM.getContext().getDeclAlign(VD).getQuantity());
       Val = LI;
     }
-
+    if (Val->getType() != Wrapper->getReturnType())
+      Val = Builder.CreatePointerBitCastOrAddrSpaceCast(
+          Val, Wrapper->getReturnType(), "");
     Builder.CreateRet(Val);
   }
 }
@@ -1981,8 +1983,7 @@ LValue ItaniumCXXABI::EmitThreadLocalVar
   QualType T = VD->getType();
   llvm::Type *Ty = CGF.getTypes().ConvertTypeForMem(T);
   llvm::Value *Val = CGF.CGM.GetAddrOfGlobalVar(VD, Ty);
-  llvm::Function *Wrapper =
-      getOrCreateThreadLocalWrapper(VD, cast<llvm::GlobalVariable>(Val));
+  llvm::Function *Wrapper = getOrCreateThreadLocalWrapper(VD, Val);
 
   Val = CGF.Builder.CreateCall(Wrapper);
 

Added: cfe/trunk/test/CodeGenCXX/pr18635.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/pr18635.cpp?rev=218503&view=auto
==============================================================================
--- cfe/trunk/test/CodeGenCXX/pr18635.cpp (added)
+++ cfe/trunk/test/CodeGenCXX/pr18635.cpp Fri Sep 26 01:28:25 2014
@@ -0,0 +1,22 @@
+// RUN: %clang_cc1 -emit-llvm -std=c++11 -triple x86_64-pc-linux-gnu -o- %s | FileCheck %s
+
+// Global @x:
+// CHECK: [[X_GLOBAL:@[^ ]+]]{{.*}}thread_local global
+
+// returned somewhere in TLS wrapper:
+// CHECK: ret{{.*}}[[X_GLOBAL]]
+
+template <typename T> class unique_ptr {
+  template <typename F, typename S> struct pair {
+    F first;
+    S second;
+  };
+  pair<T *, int> data;
+public:
+  constexpr unique_ptr() noexcept : data() {}
+  explicit unique_ptr(T *p) noexcept : data() {}
+};
+
+thread_local unique_ptr<int> x;
+int main() { x = unique_ptr<int>(new int(5)); }
+





More information about the cfe-commits mailing list