[cfe-commits] r103880 - in /cfe/trunk: lib/CodeGen/CGDeclCXX.cpp lib/CodeGen/CGException.cpp lib/CodeGen/CodeGenFunction.h test/CodeGenCXX/threadsafe-statics-exceptions.cpp

Douglas Gregor dgregor at apple.com
Sat May 15 10:55:51 PDT 2010


Author: dgregor
Date: Sat May 15 12:55:51 2010
New Revision: 103880

URL: http://llvm.org/viewvc/llvm-project?rev=103880&view=rev
Log:
When initializing thread-safe statics, put the call to
__cxa_guard_abort along the exceptional edge into (in effect) a nested
"try" that rethrows after aborting. Fixes PR7144 and the remaining
Boost.ProgramOptions failures.

Added:
    cfe/trunk/test/CodeGenCXX/threadsafe-statics-exceptions.cpp   (with props)
Modified:
    cfe/trunk/lib/CodeGen/CGDeclCXX.cpp
    cfe/trunk/lib/CodeGen/CGException.cpp
    cfe/trunk/lib/CodeGen/CodeGenFunction.h

Modified: cfe/trunk/lib/CodeGen/CGDeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDeclCXX.cpp?rev=103880&r1=103879&r2=103880&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGDeclCXX.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGDeclCXX.cpp Sat May 15 12:55:51 2010
@@ -321,7 +321,11 @@
 
   EmitBlock(InitCheckBlock);
 
-  if (ThreadsafeStatics) {
+  // Variables used when coping with thread-safe statics and exceptions.
+  llvm::BasicBlock *SavedLandingPad = 0;
+  llvm::BasicBlock *Abort = 0;
+  
+  if (ThreadsafeStatics) {    
     // Call __cxa_guard_acquire.
     V = Builder.CreateCall(getGuardAcquireFn(*this), GuardVariable);
                
@@ -330,14 +334,13 @@
     Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"),
                          InitBlock, EndBlock);
   
-    EmitBlock(InitBlock);
-
     if (Exceptions) {
-      EHCleanupBlock Cleanup(*this);
-    
-      // Call __cxa_guard_abort.
-      Builder.CreateCall(getGuardAbortFn(*this), GuardVariable);
+      SavedLandingPad = getInvokeDest();
+      Abort = createBasicBlock("guard.abort");
+      setInvokeDest(Abort);
     }
+    
+    EmitBlock(InitBlock);
   }
 
   if (D.getType()->isReferenceType()) {
@@ -353,7 +356,7 @@
 
   if (ThreadsafeStatics) {
     // Call __cxa_guard_release.
-    Builder.CreateCall(getGuardReleaseFn(*this), GuardVariable);
+    Builder.CreateCall(getGuardReleaseFn(*this), GuardVariable);    
   } else {
     llvm::Value *One = 
       llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), 1);
@@ -364,5 +367,20 @@
   if (!D.getType()->isReferenceType())
     EmitDeclDestroy(*this, D, GV);
   
+  if (ThreadsafeStatics && Exceptions) {
+    // If an exception is thrown during initialization, call __cxa_guard_abort
+    // along the exceptional edge.
+    EmitBranch(EndBlock);
+    
+    EmitBlock(Abort);
+    
+    // Call __cxa_guard_abort along the exceptional edge.
+    Builder.CreateCall(getGuardAbortFn(*this), GuardVariable);
+    setInvokeDest(SavedLandingPad);
+    
+    // Rethrow the current exception.
+    EmitRethrow();
+  }    
+  
   EmitBlock(EndBlock);
 }

Modified: cfe/trunk/lib/CodeGen/CGException.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGException.cpp?rev=103880&r1=103879&r2=103880&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGException.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGException.cpp Sat May 15 12:55:51 2010
@@ -239,19 +239,23 @@
   }
 }
 
+void CodeGenFunction::EmitRethrow() {
+  if (getInvokeDest()) {
+    llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
+    Builder.CreateInvoke(getReThrowFn(*this), Cont, getInvokeDest())
+    ->setDoesNotReturn();
+    EmitBlock(Cont);
+  } else
+    Builder.CreateCall(getReThrowFn(*this))->setDoesNotReturn();
+  Builder.CreateUnreachable();
+  
+  // Clear the insertion point to indicate we are in unreachable code.
+  Builder.ClearInsertionPoint();
+}
+
 void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E) {
   if (!E->getSubExpr()) {
-    if (getInvokeDest()) {
-      llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
-      Builder.CreateInvoke(getReThrowFn(*this), Cont, getInvokeDest())
-        ->setDoesNotReturn();
-      EmitBlock(Cont);
-    } else
-      Builder.CreateCall(getReThrowFn(*this))->setDoesNotReturn();
-    Builder.CreateUnreachable();
-
-    // Clear the insertion point to indicate we are in unreachable code.
-    Builder.ClearInsertionPoint();
+    EmitRethrow();
     return;
   }
 
@@ -562,6 +566,7 @@
         // FIXME: we need to do this sooner so that the EH region for the
         // cleanup doesn't start until after the ctor completes, use a decl
         // init?
+        // FIXME: Alternatively, can we just elide this copy entirely?
         CopyObject(*this, CatchParam->getType().getNonReferenceType(),
                    WasPointer, WasPointerReference, ExcObject,
                    GetAddrOfLocalVar(CatchParam));

Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.h?rev=103880&r1=103879&r2=103880&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.h Sat May 15 12:55:51 2010
@@ -1258,7 +1258,8 @@
                                     bool IsInitializer = false);
 
   void EmitCXXThrowExpr(const CXXThrowExpr *E);
-
+  void EmitRethrow();
+  
   //===--------------------------------------------------------------------===//
   //                             Internal Helpers
   //===--------------------------------------------------------------------===//

Added: cfe/trunk/test/CodeGenCXX/threadsafe-statics-exceptions.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/threadsafe-statics-exceptions.cpp?rev=103880&view=auto
==============================================================================
--- cfe/trunk/test/CodeGenCXX/threadsafe-statics-exceptions.cpp (added)
+++ cfe/trunk/test/CodeGenCXX/threadsafe-statics-exceptions.cpp Sat May 15 12:55:51 2010
@@ -0,0 +1,24 @@
+// RUN: %clang_cc1 -emit-llvm -o - -fexceptions -triple x86_64-apple-darwin10 %s | FileCheck %s
+
+struct X {
+  X();
+  ~X();
+};
+
+struct Y { };
+
+// CHECK: define void @_Z1fv
+void f() {
+  // CHECK: call i32 @__cxa_guard_acquire(i64* @_ZGVZ1fvE1x)
+  // CHECK: invoke void @_ZN1XC1Ev
+  // CHECK: call void @__cxa_guard_release(i64* @_ZGVZ1fvE1x)
+  // CHECK: call i32 @__cxa_atexit
+  // CHECK: br
+  static X x;
+  // CHECK: call void @__cxa_guard_abort(i64* @_ZGVZ1fvE1x)
+  // CHECK: call void @__cxa_rethrow() noreturn
+  // CHECK: unreachable
+
+  // CHECK: call i8* @__cxa_allocate_exception
+  throw Y();
+}

Propchange: cfe/trunk/test/CodeGenCXX/threadsafe-statics-exceptions.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/trunk/test/CodeGenCXX/threadsafe-statics-exceptions.cpp
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cfe/trunk/test/CodeGenCXX/threadsafe-statics-exceptions.cpp
------------------------------------------------------------------------------
    svn:mime-type = text/plain





More information about the cfe-commits mailing list