[cfe-commits] r66117 - in /cfe/trunk: lib/CodeGen/CGBlocks.cpp lib/CodeGen/CGBlocks.h lib/CodeGen/CGDecl.cpp lib/CodeGen/CodeGenFunction.cpp test/CodeGen/blocks-1.c

Mike Stump mrs at apple.com
Wed Mar 4 17:23:13 PST 2009


Author: mrs
Date: Wed Mar  4 19:23:13 2009
New Revision: 66117

URL: http://llvm.org/viewvc/llvm-project?rev=66117&view=rev
Log:
Add codegen support for __block variables to call _Block_object_dispose as necessary.

Modified:
    cfe/trunk/lib/CodeGen/CGBlocks.cpp
    cfe/trunk/lib/CodeGen/CGBlocks.h
    cfe/trunk/lib/CodeGen/CGDecl.cpp
    cfe/trunk/lib/CodeGen/CodeGenFunction.cpp
    cfe/trunk/test/CodeGen/blocks-1.c

Modified: cfe/trunk/lib/CodeGen/CGBlocks.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGBlocks.cpp?rev=66117&r1=66116&r2=66117&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGBlocks.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGBlocks.cpp Wed Mar  4 19:23:13 2009
@@ -34,8 +34,6 @@
 
 
 llvm::Constant *CodeGenFunction::BuildDescriptorBlockDecl(uint64_t Size) {
-  const llvm::PointerType *PtrToInt8Ty
-    = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
   const llvm::Type *UnsignedLongTy
     = CGM.getTypes().ConvertType(getContext().UnsignedLongTy);
   llvm::Constant *C;
@@ -78,8 +76,6 @@
   if (NSConcreteGlobalBlock)
     return NSConcreteGlobalBlock;
 
-  const llvm::PointerType *PtrToInt8Ty
-    = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
   // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the
   // same thing as CreateRuntimeFunction if there's already a variable with the
   // same name.
@@ -96,8 +92,6 @@
   if (NSConcreteStackBlock)
     return NSConcreteStackBlock;
 
-  const llvm::PointerType *PtrToInt8Ty
-    = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
   // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the
   // same thing as CreateRuntimeFunction if there's already a variable with the
   // same name.
@@ -164,8 +158,6 @@
 
     // __isa
     C = CGM.getNSConcreteStackBlock();
-    const llvm::PointerType *PtrToInt8Ty
-      = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
     C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
     Elts.push_back(C);
 
@@ -335,9 +327,6 @@
   if (GenericBlockLiteralType)
     return GenericBlockLiteralType;
 
-  const llvm::Type *Int8PtrTy =
-    llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
-
   const llvm::Type *BlockDescPtrTy =
     llvm::PointerType::getUnqual(getBlockDescriptorType());
 
@@ -351,10 +340,10 @@
   //   void (*__invoke)(void *);
   //   struct __block_descriptor *__descriptor;
   // };
-  GenericBlockLiteralType = llvm::StructType::get(Int8PtrTy,
+  GenericBlockLiteralType = llvm::StructType::get(PtrToInt8Ty,
                                                   IntTy,
                                                   IntTy,
-                                                  Int8PtrTy,
+                                                  PtrToInt8Ty,
                                                   BlockDescPtrTy,
                                                   NULL);
 
@@ -368,9 +357,6 @@
   if (GenericExtendedBlockLiteralType)
     return GenericExtendedBlockLiteralType;
 
-  const llvm::Type *Int8PtrTy =
-    llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
-
   const llvm::Type *BlockDescPtrTy =
     llvm::PointerType::getUnqual(getBlockDescriptorType());
 
@@ -386,13 +372,13 @@
   //   void *__copy_func_helper_decl;
   //   void *__destroy_func_decl;
   // };
-  GenericExtendedBlockLiteralType = llvm::StructType::get(Int8PtrTy,
+  GenericExtendedBlockLiteralType = llvm::StructType::get(PtrToInt8Ty,
                                                           IntTy,
                                                           IntTy,
-                                                          Int8PtrTy,
+                                                          PtrToInt8Ty,
                                                           BlockDescPtrTy,
-                                                          Int8PtrTy,
-                                                          Int8PtrTy,
+                                                          PtrToInt8Ty,
+                                                          PtrToInt8Ty,
                                                           NULL);
 
   getModule().addTypeName("struct.__block_literal_extended_generic",
@@ -678,8 +664,6 @@
 }
 
 llvm::Value *BlockFunction::BuildCopyHelper(int flag) {
-  const llvm::PointerType *PtrToInt8Ty
-    = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
   // FIXME: implement
   llvm::Value *V = llvm::ConstantInt::get(llvm::Type::Int32Ty, 43);
   V = Builder.CreateIntToPtr(V, PtrToInt8Ty, "tmp");
@@ -688,11 +672,33 @@
 }
 
 llvm::Value *BlockFunction::BuildDestroyHelper(int flag) {
-  const llvm::PointerType *PtrToInt8Ty
-    = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
   // FIXME: implement
   llvm::Value *V = llvm::ConstantInt::get(llvm::Type::Int32Ty, 44);
   V = Builder.CreateIntToPtr(V, PtrToInt8Ty, "tmp");
   V = Builder.CreateBitCast(V, PtrToInt8Ty, "tmp");
   return V;
 }
+
+llvm::Value *BlockFunction::getBlockObjectDispose() {
+  if (CGM.BlockObjectDispose == 0) {
+    const llvm::FunctionType *FTy;
+    std::vector<const llvm::Type*> ArgTys;
+    const llvm::Type *ResultType = llvm::Type::VoidTy;
+    ArgTys.push_back(PtrToInt8Ty);
+    ArgTys.push_back(llvm::Type::Int32Ty);
+    FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
+    CGM.BlockObjectDispose  
+      = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose");
+  }
+  return CGM.BlockObjectDispose;
+}
+
+void BlockFunction::BuildBlockRelease(const VarDecl &D, llvm::Value *DeclPtr) {
+  llvm::Value *F = getBlockObjectDispose();
+  llvm::Value *N, *V;
+  V = Builder.CreateStructGEP(DeclPtr, 1, "forwarding");
+  V = Builder.CreateLoad(V, false);
+  V = Builder.CreateBitCast(V, PtrToInt8Ty);
+  N = llvm::ConstantInt::get(llvm::Type::Int32Ty, BLOCK_FIELD_IS_BYREF);
+  Builder.CreateCall2(F, V, N);
+}

Modified: cfe/trunk/lib/CodeGen/CGBlocks.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGBlocks.h?rev=66117&r1=66116&r2=66117&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGBlocks.h (original)
+++ cfe/trunk/lib/CodeGen/CGBlocks.h Wed Mar  4 19:23:13 2009
@@ -94,19 +94,26 @@
     int GlobalUniqueCount;
   } Block;
 
+  llvm::Value *BlockObjectDispose;
+  const llvm::Type *PtrToInt8Ty;
+
   BlockModule(ASTContext &C, llvm::Module &M, const llvm::TargetData &TD,
               CodeGenTypes &T, CodeGenModule &CodeGen)
     : Context(C), TheModule(M), TheTargetData(TD), Types(T),
       CGM(CodeGen),
       NSConcreteGlobalBlock(0), NSConcreteStackBlock(0), BlockDescriptorType(0),
-      GenericBlockLiteralType(0)  {
+      GenericBlockLiteralType(0), BlockObjectDispose(0) {
     Block.GlobalUniqueCount = 0;
+    PtrToInt8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
   }
 };
 
 class BlockFunction : public BlockBase {
+  CodeGenModule &CGM;
+
 public:
-    enum {
+  const llvm::Type *PtrToInt8Ty;
+  enum {
     BLOCK_FIELD_IS_OBJECT   =  3,  /* id, NSObject, __attribute__((NSObject)),
                                       block, ... */
     BLOCK_FIELD_IS_BLOCK    =  7,  /* a block variable */
@@ -140,7 +147,10 @@
 
   CGBuilderTy &Builder;
 
-  BlockFunction(CGBuilderTy &B) : Builder(B) { }
+  BlockFunction(CodeGenModule &cgm, CGBuilderTy &B)
+    : CGM(cgm), Builder(B) {
+    PtrToInt8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
+  }
 
   ImplicitParamDecl *BlockStructDecl;
   ImplicitParamDecl *getBlockStructDecl() { return BlockStructDecl; }
@@ -148,6 +158,8 @@
   llvm::Value *BuildCopyHelper(int flag);
   llvm::Value *BuildDestroyHelper(int flag);
 
+  llvm::Value *getBlockObjectDispose();
+  void BuildBlockRelease(const VarDecl &D, llvm::Value *DeclPtr);
 };
 
 }  // end namespace CodeGen

Modified: cfe/trunk/lib/CodeGen/CGDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDecl.cpp?rev=66117&r1=66116&r2=66117&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGDecl.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGDecl.cpp Wed Mar  4 19:23:13 2009
@@ -227,6 +227,7 @@
 void CodeGenFunction::EmitLocalBlockVarDecl(const VarDecl &D) {
   QualType Ty = D.getType();
   bool isByRef = D.getAttr<BlocksAttr>();
+  bool needsDispose = false;
 
   llvm::Value *DeclPtr;
   if (Ty->isConstantSizeType()) {
@@ -371,6 +372,7 @@
 
       Builder.CreateStore(BuildDestroyHelper(flag), destroy_helper);
     }
+    needsDispose = true;
   }
 
   // Handle the cleanup attribute
@@ -388,6 +390,11 @@
       
     EmitCall(CGM.getTypes().getFunctionInfo(FD), F, Args);
   }
+
+  if (needsDispose) {
+    CleanupScope scope(*this);
+    BuildBlockRelease(D, DeclPtr);
+  }
 }
 
 /// Emit an alloca (or GlobalValue depending on target) 

Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.cpp?rev=66117&r1=66116&r2=66117&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.cpp Wed Mar  4 19:23:13 2009
@@ -24,7 +24,7 @@
 using namespace CodeGen;
 
 CodeGenFunction::CodeGenFunction(CodeGenModule &cgm) 
-  : BlockFunction(Builder), CGM(cgm), Target(CGM.getContext().Target),
+  : BlockFunction(cgm, Builder), CGM(cgm), Target(CGM.getContext().Target),
     DebugInfo(0), SwitchInsn(0), CaseRangeBlock(0), InvokeDest(0) {
   LLVMIntTy = ConvertType(getContext().IntTy);
   LLVMPointerWidth = Target.getPointerWidth(0);

Modified: cfe/trunk/test/CodeGen/blocks-1.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/blocks-1.c?rev=66117&r1=66116&r2=66117&view=diff

==============================================================================
--- cfe/trunk/test/CodeGen/blocks-1.c (original)
+++ cfe/trunk/test/CodeGen/blocks-1.c Wed Mar  4 19:23:13 2009
@@ -1,4 +1,5 @@
-// RUN: clang %s -emit-llvm -o %t -fblocks -f__block
+// RUN: clang %s -emit-llvm -o %t -fblocks -f__block &&
+// RUN: grep "_Block_object_dispose" %t | count 3
 #include <stdio.h>
 
 void test1() {





More information about the cfe-commits mailing list