[cfe-commits] r139359 - in /cfe/trunk: lib/CodeGen/CodeGenModule.cpp lib/CodeGen/CodeGenModule.h test/PCH/block-decl-merging.c test/PCH/block-decl-merging.cpp

Argyrios Kyrtzidis akyrtzi at gmail.com
Thu Sep 8 23:44:22 PDT 2011


Author: akirtzidis
Date: Fri Sep  9 01:44:21 2011
New Revision: 139359

URL: http://llvm.org/viewvc/llvm-project?rev=139359&view=rev
Log:
Do a lookup for the blocks runtime globals to see if they were declared,
instead of codegen waiting to consume such a declaration, which won't
happen if that decls are coming from a PCH.

Fixes rdar://10028656.

Added:
    cfe/trunk/test/PCH/block-decl-merging.c
    cfe/trunk/test/PCH/block-decl-merging.cpp
Modified:
    cfe/trunk/lib/CodeGen/CodeGenModule.cpp
    cfe/trunk/lib/CodeGen/CodeGenModule.h

Modified: cfe/trunk/lib/CodeGen/CodeGenModule.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.cpp?rev=139359&r1=139358&r2=139359&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Fri Sep  9 01:44:21 2011
@@ -68,9 +68,7 @@
     CFConstantStringClassRef(0), ConstantStringClassRef(0),
     NSConstantStringType(0),
     VMContext(M.getContext()),
-    NSConcreteGlobalBlockDecl(0), NSConcreteStackBlockDecl(0),
     NSConcreteGlobalBlock(0), NSConcreteStackBlock(0),
-    BlockObjectAssignDecl(0), BlockObjectDisposeDecl(0),
     BlockObjectAssign(0), BlockObjectDispose(0),
     BlockDescriptorType(0), GenericBlockLiteralType(0) {
   if (Features.ObjC1)
@@ -739,15 +737,6 @@
 
   // Ignore declarations, they will be emitted on their first use.
   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) {
-    if (FD->getIdentifier()) {
-      StringRef Name = FD->getName();
-      if (Name == "_Block_object_assign") {
-        BlockObjectAssignDecl = FD;
-      } else if (Name == "_Block_object_dispose") {
-        BlockObjectDisposeDecl = FD;
-      }
-    }
-
     // Forward declarations are emitted lazily on first use.
     if (!FD->doesThisDeclarationHaveABody()) {
       if (!FD->doesDeclarationForceExternallyVisibleDefinition())
@@ -768,16 +757,6 @@
     const VarDecl *VD = cast<VarDecl>(Global);
     assert(VD->isFileVarDecl() && "Cannot emit local var decl as global.");
 
-    if (VD->getIdentifier()) {
-      StringRef Name = VD->getName();
-      if (Name == "_NSConcreteGlobalBlock") {
-        NSConcreteGlobalBlockDecl = VD;
-      } else if (Name == "_NSConcreteStackBlock") {
-        NSConcreteStackBlockDecl = VD;
-      }
-    }
-
-
     if (VD->isThisDeclarationADefinition() != VarDecl::Definition)
       return;
   }
@@ -2428,12 +2407,15 @@
   if (BlockObjectDispose)
     return BlockObjectDispose;
 
-  // If we saw an explicit decl, use that.
-  if (BlockObjectDisposeDecl) {
-    return BlockObjectDispose = GetAddrOfFunction(
-      BlockObjectDisposeDecl,
-      getTypes().GetFunctionType(BlockObjectDisposeDecl));
-  }
+  DeclarationName DName(&Context.Idents.get("_Block_object_dispose"));
+  DeclContext::lookup_result
+    Lookup = Context.getTranslationUnitDecl()->lookup(DName);
+
+  // If there is an explicit decl, use that.
+  if (Lookup.first != Lookup.second)
+    if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*Lookup.first))
+      return BlockObjectDispose =
+          GetAddrOfFunction(FD, getTypes().GetFunctionType(FD));
 
   // Otherwise construct the function by hand.
   llvm::Type *args[] = { Int8PtrTy, Int32Ty };
@@ -2447,12 +2429,15 @@
   if (BlockObjectAssign)
     return BlockObjectAssign;
 
-  // If we saw an explicit decl, use that.
-  if (BlockObjectAssignDecl) {
-    return BlockObjectAssign = GetAddrOfFunction(
-      BlockObjectAssignDecl,
-      getTypes().GetFunctionType(BlockObjectAssignDecl));
-  }
+  DeclarationName DName(&Context.Idents.get("_Block_object_assign"));
+  DeclContext::lookup_result
+    Lookup = Context.getTranslationUnitDecl()->lookup(DName);
+
+  // If there is an explicit decl, use that.
+  if (Lookup.first != Lookup.second)
+    if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*Lookup.first))
+      return BlockObjectAssign =
+          GetAddrOfFunction(FD, getTypes().GetFunctionType(FD));
 
   // Otherwise construct the function by hand.
   llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, Int32Ty };
@@ -2466,12 +2451,15 @@
   if (NSConcreteGlobalBlock)
     return NSConcreteGlobalBlock;
 
-  // If we saw an explicit decl, use that.
-  if (NSConcreteGlobalBlockDecl) {
-    return NSConcreteGlobalBlock = GetAddrOfGlobalVar(
-      NSConcreteGlobalBlockDecl,
-      getTypes().ConvertType(NSConcreteGlobalBlockDecl->getType()));
-  }
+  DeclarationName DName(&Context.Idents.get("_NSConcreteGlobalBlock"));
+  DeclContext::lookup_result
+    Lookup = Context.getTranslationUnitDecl()->lookup(DName);
+
+  // If there is an explicit decl, use that.
+  if (Lookup.first != Lookup.second)
+    if (const VarDecl *VD = dyn_cast<VarDecl>(*Lookup.first))
+      return NSConcreteGlobalBlock =
+          GetAddrOfGlobalVar(VD, getTypes().ConvertType(VD->getType()));
 
   // Otherwise construct the variable by hand.
   return NSConcreteGlobalBlock =
@@ -2482,12 +2470,15 @@
   if (NSConcreteStackBlock)
     return NSConcreteStackBlock;
 
-  // If we saw an explicit decl, use that.
-  if (NSConcreteStackBlockDecl) {
-    return NSConcreteStackBlock = GetAddrOfGlobalVar(
-      NSConcreteStackBlockDecl,
-      getTypes().ConvertType(NSConcreteStackBlockDecl->getType()));
-  }
+  DeclarationName DName(&Context.Idents.get("_NSConcreteStackBlock"));
+  DeclContext::lookup_result
+    Lookup = Context.getTranslationUnitDecl()->lookup(DName);
+
+  // If there is an explicit decl, use that.
+  if (Lookup.first != Lookup.second)
+    if (const VarDecl *VD = dyn_cast<VarDecl>(*Lookup.first))
+      return NSConcreteStackBlock =
+          GetAddrOfGlobalVar(VD, getTypes().ConvertType(VD->getType()));
 
   // Otherwise construct the variable by hand.
   return NSConcreteStackBlock =

Modified: cfe/trunk/lib/CodeGen/CodeGenModule.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.h?rev=139359&r1=139358&r2=139359&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.h Fri Sep  9 01:44:21 2011
@@ -314,13 +314,9 @@
   /// @name Cache for Blocks Runtime Globals
   /// @{
 
-  const VarDecl *NSConcreteGlobalBlockDecl;
-  const VarDecl *NSConcreteStackBlockDecl;
   llvm::Constant *NSConcreteGlobalBlock;
   llvm::Constant *NSConcreteStackBlock;
 
-  const FunctionDecl *BlockObjectAssignDecl;
-  const FunctionDecl *BlockObjectDisposeDecl;
   llvm::Constant *BlockObjectAssign;
   llvm::Constant *BlockObjectDispose;
 

Added: cfe/trunk/test/PCH/block-decl-merging.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/PCH/block-decl-merging.c?rev=139359&view=auto
==============================================================================
--- cfe/trunk/test/PCH/block-decl-merging.c (added)
+++ cfe/trunk/test/PCH/block-decl-merging.c Fri Sep  9 01:44:21 2011
@@ -0,0 +1,28 @@
+// RUN: %clang_cc1 -triple i386-apple-darwin10 -fblocks %s -emit-pch -o %t
+// RUN: %clang_cc1 -triple i386-apple-darwin10 -fblocks %s -include-pch %t -emit-llvm -o - | \
+// RUN:   FileCheck %s
+
+#ifndef HEADER
+#define HEADER
+
+// CHECK: @_NSConcreteGlobalBlock = extern_weak global
+extern void * _NSConcreteStackBlock[32] __attribute__((weak_import));
+// CHECK: @_NSConcreteStackBlock = extern_weak global
+extern void * _NSConcreteGlobalBlock[32] __attribute__((weak_import));
+extern void _Block_object_dispose(const void *, const int) __attribute__((weak_import));
+// CHECK: declare extern_weak void @_Block_object_assign
+extern void _Block_object_assign(void *, const void *, const int) __attribute__((weak_import));
+// CHECK: declare extern_weak void @_Block_object_dispose
+
+#else
+
+void *x = ^(){};
+
+void f1(void (^a0)(void));
+
+void f0() {
+  __block int x;
+  f1(^(void){ x = 1; });
+}
+
+#endif

Added: cfe/trunk/test/PCH/block-decl-merging.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/PCH/block-decl-merging.cpp?rev=139359&view=auto
==============================================================================
--- cfe/trunk/test/PCH/block-decl-merging.cpp (added)
+++ cfe/trunk/test/PCH/block-decl-merging.cpp Fri Sep  9 01:44:21 2011
@@ -0,0 +1,30 @@
+// RUN: %clang_cc1 -triple i386-apple-darwin10 -fblocks %s -emit-pch -o %t
+// RUN: %clang_cc1 -triple i386-apple-darwin10 -fblocks %s -include-pch %t -emit-llvm -o - | \
+// RUN:   FileCheck %s
+
+#ifndef HEADER
+#define HEADER
+
+extern "C" {
+// CHECK: @_NSConcreteGlobalBlock = extern_weak global
+extern void * _NSConcreteStackBlock[32] __attribute__((weak_import));
+// CHECK: @_NSConcreteStackBlock = extern_weak global
+extern void * _NSConcreteGlobalBlock[32] __attribute__((weak_import));
+extern void _Block_object_dispose(const void *, const int) __attribute__((weak_import));
+// CHECK: declare extern_weak void @_Block_object_assign
+extern void _Block_object_assign(void *, const void *, const int) __attribute__((weak_import));
+// CHECK: declare extern_weak void @_Block_object_dispose
+}
+
+#else
+
+void *x = ^(){};
+
+void f1(void (^a0)(void));
+
+void f0() {
+  __block int x;
+  f1(^(void){ x = 1; });
+}
+
+#endif





More information about the cfe-commits mailing list