r351487 - Fix cleanup registration for lambda captures.

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 17 14:05:51 PST 2019


Author: rsmith
Date: Thu Jan 17 14:05:50 2019
New Revision: 351487

URL: http://llvm.org/viewvc/llvm-project?rev=351487&view=rev
Log:
Fix cleanup registration for lambda captures.

Lambda captures should be destroyed if an exception is thrown only if
the construction of the complete lambda-expression has not completed.
(If the lambda-expression has been fully constructed, any exception will
invoke its destructor, which will destroy the captures.)

This is directly modeled after how we handle the equivalent situation in
InitListExprs.

Note that EmitLambdaLValue was unreachable because in C++11 onwards the
frontend never creates the awkward situation where a prvalue expression
(such as a lambda) is used in an lvalue context (such as the left-hand
side of a class member access).

Added:
    cfe/trunk/test/CodeGenCXX/cxx1y-init-captures-eh.cpp
Modified:
    cfe/trunk/lib/CodeGen/CGExpr.cpp
    cfe/trunk/lib/CodeGen/CGExprAgg.cpp
    cfe/trunk/lib/CodeGen/CGExprCXX.cpp
    cfe/trunk/lib/CodeGen/CodeGenFunction.h

Modified: cfe/trunk/lib/CodeGen/CGExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExpr.cpp?rev=351487&r1=351486&r2=351487&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExpr.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExpr.cpp Thu Jan 17 14:05:50 2019
@@ -1287,8 +1287,6 @@ LValue CodeGenFunction::EmitLValue(const
     return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E));
   case Expr::CXXUuidofExprClass:
     return EmitCXXUuidofLValue(cast<CXXUuidofExpr>(E));
-  case Expr::LambdaExprClass:
-    return EmitLambdaLValue(cast<LambdaExpr>(E));
 
   case Expr::ExprWithCleanupsClass: {
     const auto *cleanups = cast<ExprWithCleanups>(E);
@@ -4548,13 +4546,6 @@ CodeGenFunction::EmitCXXBindTemporaryLVa
   return MakeAddrLValue(Slot.getAddress(), E->getType(), AlignmentSource::Decl);
 }
 
-LValue
-CodeGenFunction::EmitLambdaLValue(const LambdaExpr *E) {
-  AggValueSlot Slot = CreateAggTemp(E->getType(), "temp.lvalue");
-  EmitLambdaExpr(E, Slot);
-  return MakeAddrLValue(Slot.getAddress(), E->getType(), AlignmentSource::Decl);
-}
-
 LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) {
   RValue RV = EmitObjCMessageExpr(E);
 

Modified: cfe/trunk/lib/CodeGen/CGExprAgg.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprAgg.cpp?rev=351487&r1=351486&r2=351487&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprAgg.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprAgg.cpp Thu Jan 17 14:05:50 2019
@@ -1264,7 +1264,52 @@ void AggExprEmitter::VisitCXXInheritedCt
 void
 AggExprEmitter::VisitLambdaExpr(LambdaExpr *E) {
   AggValueSlot Slot = EnsureSlot(E->getType());
-  CGF.EmitLambdaExpr(E, Slot);
+  LValue SlotLV = CGF.MakeAddrLValue(Slot.getAddress(), E->getType());
+
+  // We'll need to enter cleanup scopes in case any of the element
+  // initializers throws an exception.
+  SmallVector<EHScopeStack::stable_iterator, 16> Cleanups;
+  llvm::Instruction *CleanupDominator = nullptr;
+
+  CXXRecordDecl::field_iterator CurField = E->getLambdaClass()->field_begin();
+  for (LambdaExpr::const_capture_init_iterator i = E->capture_init_begin(),
+                                               e = E->capture_init_end();
+       i != e; ++i, ++CurField) {
+    // Emit initialization
+    LValue LV = CGF.EmitLValueForFieldInitialization(SlotLV, *CurField);
+    if (CurField->hasCapturedVLAType()) {
+      CGF.EmitLambdaVLACapture(CurField->getCapturedVLAType(), LV);
+      continue;
+    }
+
+    EmitInitializationToLValue(*i, LV);
+
+    // Push a destructor if necessary.
+    if (QualType::DestructionKind DtorKind =
+            CurField->getType().isDestructedType()) {
+      assert(LV.isSimple());
+      if (CGF.needsEHCleanup(DtorKind)) {
+        if (!CleanupDominator)
+          CleanupDominator = CGF.Builder.CreateAlignedLoad(
+              CGF.Int8Ty,
+              llvm::Constant::getNullValue(CGF.Int8PtrTy),
+              CharUnits::One()); // placeholder
+
+        CGF.pushDestroy(EHCleanup, LV.getAddress(), CurField->getType(),
+                        CGF.getDestroyer(DtorKind), false);
+        Cleanups.push_back(CGF.EHStack.stable_begin());
+      }
+    }
+  }
+
+  // Deactivate all the partial cleanups in reverse order, which
+  // generally means popping them.
+  for (unsigned i = Cleanups.size(); i != 0; --i)
+    CGF.DeactivateCleanupBlock(Cleanups[i-1], CleanupDominator);
+
+  // Destroy the placeholder if we made one.
+  if (CleanupDominator)
+    CleanupDominator->eraseFromParent();
 }
 
 void AggExprEmitter::VisitExprWithCleanups(ExprWithCleanups *E) {

Modified: cfe/trunk/lib/CodeGen/CGExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprCXX.cpp?rev=351487&r1=351486&r2=351487&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprCXX.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprCXX.cpp Thu Jan 17 14:05:50 2019
@@ -2253,21 +2253,3 @@ llvm::Value *CodeGenFunction::EmitDynami
 
   return Value;
 }
-
-void CodeGenFunction::EmitLambdaExpr(const LambdaExpr *E, AggValueSlot Slot) {
-  LValue SlotLV = MakeAddrLValue(Slot.getAddress(), E->getType());
-
-  CXXRecordDecl::field_iterator CurField = E->getLambdaClass()->field_begin();
-  for (LambdaExpr::const_capture_init_iterator i = E->capture_init_begin(),
-                                               e = E->capture_init_end();
-       i != e; ++i, ++CurField) {
-    // Emit initialization
-    LValue LV = EmitLValueForFieldInitialization(SlotLV, *CurField);
-    if (CurField->hasCapturedVLAType()) {
-      auto VAT = CurField->getCapturedVLAType();
-      EmitStoreThroughLValue(RValue::get(VLASizeMap[VAT->getSizeExpr()]), LV);
-    } else {
-      EmitInitializerForField(*CurField, LV, *i);
-    }
-  }
-}

Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.h?rev=351487&r1=351486&r2=351487&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.h Thu Jan 17 14:05:50 2019
@@ -1836,6 +1836,9 @@ public:
   void EmitLambdaBlockInvokeBody();
   void EmitLambdaDelegatingInvokeBody(const CXXMethodDecl *MD);
   void EmitLambdaStaticInvokeBody(const CXXMethodDecl *MD);
+  void EmitLambdaVLACapture(const VariableArrayType *VAT, LValue LV) {
+    EmitStoreThroughLValue(RValue::get(VLASizeMap[VAT->getSizeExpr()]), LV);
+  }
   void EmitAsanPrologueOrEpilogue(bool Prologue);
 
   /// Emit the unified return block, trying to avoid its emission when
@@ -3560,7 +3563,6 @@ public:
 
   LValue EmitCXXConstructLValue(const CXXConstructExpr *E);
   LValue EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E);
-  LValue EmitLambdaLValue(const LambdaExpr *E);
   LValue EmitCXXTypeidLValue(const CXXTypeidExpr *E);
   LValue EmitCXXUuidofLValue(const CXXUuidofExpr *E);
 
@@ -3982,8 +3984,6 @@ public:
 
   void EmitCXXThrowExpr(const CXXThrowExpr *E, bool KeepInsertionPoint = true);
 
-  void EmitLambdaExpr(const LambdaExpr *E, AggValueSlot Dest);
-
   RValue EmitAtomicExpr(AtomicExpr *E);
 
   //===--------------------------------------------------------------------===//

Added: cfe/trunk/test/CodeGenCXX/cxx1y-init-captures-eh.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/cxx1y-init-captures-eh.cpp?rev=351487&view=auto
==============================================================================
--- cfe/trunk/test/CodeGenCXX/cxx1y-init-captures-eh.cpp (added)
+++ cfe/trunk/test/CodeGenCXX/cxx1y-init-captures-eh.cpp Thu Jan 17 14:05:50 2019
@@ -0,0 +1,104 @@
+// RUN: %clang_cc1 -std=c++1y -triple x86_64-linux-gnu -fexceptions -fcxx-exceptions -emit-llvm %s -o - | FileCheck %s
+
+struct S {
+  S();
+  ~S();
+};
+
+struct T {
+  T() noexcept;
+  ~T();
+  int n;
+};
+
+// CHECK-LABEL: define void @_Z1fv(
+void f() {
+  // CHECK: call void @_ZN1SC1Ev(
+  // CHECK: invoke void @__cxa_throw
+  //
+  // Ensure we call the lambda destructor here, and do not call the destructor
+  // for the capture.
+  // CHECK: landingpad
+  // CHECK-NOT: _ZN1SD
+  // CHECK: call void @"_ZZ1fvEN3$_0D1Ev"(
+  // CHECK-NOT: _ZN1SD
+  // CHECK: resume
+  [s = S()] {}, throw 0;
+
+  // CHECK: }
+}
+
+// CHECK-LABEL: define void @_Z1gv(
+void g() {
+  // CHECK: call void @_ZN1SC1Ev(
+  // CHECK: invoke void @__cxa_throw
+  //
+  // Ensure we call the lambda destructor here, and do not call the destructor
+  // for the capture.
+  // CHECK: landingpad
+  // CHECK-NOT: @"_ZZ1gvEN3$_0D1Ev"(
+  // CHECK: call void @_ZN1SD1Ev(
+  // CHECK-NOT: @"_ZZ1gvEN3$_0D1Ev"(
+  // CHECK: resume
+  [s = S(), t = (throw 0, 1)] {};
+
+  // CHECK: }
+}
+
+void x() noexcept;
+void y() noexcept;
+
+// CHECK-LABEL: define void @_Z1hbb(
+void h(bool b1, bool b2) {
+  // CHECK: {{.*}} = alloca i1,
+  // CHECK: %[[S_ISACTIVE:.*]] = alloca i1,
+  // CHECK: {{.*}} = alloca i1,
+
+  // lambda init: s and t, branch on b1
+  // CHECK: call void @_ZN1SC1Ev(
+  // CHECK: store i1 true, i1* %[[S_ISACTIVE]], align 1
+  // CHECK: call void @_ZN1TC1Ev(
+  // CHECK: br i1
+
+  // throw 1
+  // CHECK: invoke void @__cxa_throw
+
+  // completion of lambda init, branch on b2
+  // CHECK: store i32 42,
+  // CHECK: store i1 false, i1* %[[S_ISACTIVE]], align 1
+  // CHECK: br i1
+
+  // throw 2
+  // CHECK: invoke void @__cxa_throw
+
+  // end of full-expression
+  // CHECK: call void @_Z1xv(
+  // CHECK: call void @"_ZZ1hbbEN3$_2D1Ev"(
+  // CHECK: call void @_ZN1TD1Ev(
+  // CHECK: call void @_Z1yv(
+  // CHECK: ret void
+
+  // cleanups for throw 1
+  // CHECK: landingpad
+  // CHECK-NOT: @"_ZZ1hbbEN3$_2D1Ev"(
+  // CHECK: br
+
+  // cleanups for throw 2
+  // CHECK: landingpad
+  // CHECK: call void @"_ZZ1hbbEN3$_2D1Ev"(
+  // CHECK: br
+
+  // common cleanup code
+  // CHECK: call void @_ZN1TD1Ev(
+  // CHECK: load i1, i1* %[[S_ISACTIVE]],
+  // CHECK: br i1
+
+  // CHECK: call void @_ZN1SD1Ev(
+  // CHECK: br
+
+  // CHECK: resume
+  [s = S(), t = T().n, u = (b1 ? throw 1 : 42)] {}, (b2 ? throw 2 : 0), x();
+  y();
+
+  // CHECK: }
+}




More information about the cfe-commits mailing list