diff --git a/clang/lib/CodeGen/CGClass.cpp b/clang/lib/CodeGen/CGClass.cpp --- a/clang/lib/CodeGen/CGClass.cpp +++ b/clang/lib/CodeGen/CGClass.cpp @@ -2969,9 +2969,10 @@ // Start building arguments for forwarding call CallArgList CallArgs; - QualType ThisType = getContext().getPointerType(getContext().getRecordType(Lambda)); - llvm::Value *ThisPtr = llvm::UndefValue::get(getTypes().ConvertType(ThisType)); - CallArgs.add(RValue::get(ThisPtr), ThisType); + QualType LambdaType = getContext().getRecordType(Lambda); + QualType ThisType = getContext().getPointerType(LambdaType); + Address ThisPtr = CreateMemTemp(LambdaType, "unused.capture"); + CallArgs.add(RValue::get(ThisPtr.getPointer()), ThisType); // Add the rest of the parameters. for (auto Param : MD->parameters()) diff --git a/clang/test/CodeGenCXX/lambda-to-function-pointer-conversion.cpp b/clang/test/CodeGenCXX/lambda-to-function-pointer-conversion.cpp --- a/clang/test/CodeGenCXX/lambda-to-function-pointer-conversion.cpp +++ b/clang/test/CodeGenCXX/lambda-to-function-pointer-conversion.cpp @@ -2,10 +2,11 @@ // This code used to cause an assertion failure in EmitDelegateCallArg. -// CHECK: define internal void @"?__invoke@@?0??test@@YAXXZ@CA@UTrivial@@@Z"( -// CHECK: call void @"??R@?0??test@@YAXXZ@QEBA@UTrivial@@@Z"( +// CHECK-LABEL: define internal void @"?__invoke@@?0??test@@YAXXZ@CA@UTrivial@@@Z"( +// CHECK: %unused.capture = alloca %class.anon, align 1 +// CHECK: call void @"??R@?0??test@@YAXXZ@QEBA@UTrivial@@@Z"(ptr noundef nonnull align 1 dereferenceable(1) %unused.capture, -// CHECK: define internal void @"??R@?0??test@@YAXXZ@QEBA@UTrivial@@@Z"( +// CHECK: define internal void @"??R@?0??test@@YAXXZ@QEBA@UTrivial@@@Z"(ptr noundef nonnull align 1 dereferenceable(1) %this, struct Trivial { int x; @@ -16,3 +17,15 @@ void test() { fnptr = [](Trivial a){ (void)a; }; } + +// CHECK-LABEL: define internal i32 @"?__invoke@@?0??test2@@YAXXZ@CA@H@Z"( +// CHECK: %unused.capture = alloca %class.anon.0, align 1 +// CHECK: call void @"??R@?0??test2@@YAXXZ@QEBA@H@Z"(ptr noundef nonnull align 1 dereferenceable(1) %unused.capture, + +// CHECK: define internal void @"??R@?0??test2@@YAXXZ@QEBA@H@Z"(ptr noundef nonnull align 1 dereferenceable(1) %this, + +Trivial (*fnptr2)(int); + +void test2() { + fnptr2 = [](int) -> Trivial { return {}; }; +}