Index: lib/CodeGen/CGCoroutine.cpp =================================================================== --- lib/CodeGen/CGCoroutine.cpp +++ lib/CodeGen/CGCoroutine.cpp @@ -350,6 +350,16 @@ } #endif +static SmallVector +getBundlesForCoroEnd(CodeGenFunction &CGF) { + SmallVector BundleList; + + if (Instruction *EHPad = CGF.CurrentFuncletPad) + BundleList.emplace_back("funclet", EHPad); + + return BundleList; +} + void CodeGenFunction::EmitCoroutineBody(const CoroutineBodyStmt &S) { auto *NullPtr = llvm::ConstantPointerNull::get(Builder.getInt8PtrTy()); @@ -424,8 +434,16 @@ void Emit(CodeGenFunction &CGF, Flags flags) override { auto &CGM = CGF.CGM; auto NullPtr = llvm::ConstantPointerNull::get(CGF.Int8PtrTy); - llvm::Function *CoroEnd = CGM.getIntrinsic(llvm::Intrinsic::coro_end); - CGF.Builder.CreateCall(CoroEnd, {NullPtr, CGF.Builder.getInt1(true)}); + llvm::Function *CoroEndFn = CGM.getIntrinsic(llvm::Intrinsic::coro_end); + auto Bundles = getBundlesForCoroEnd(CGF); + auto *CoroEnd = CGF.Builder.CreateCall( + CoroEndFn, {NullPtr, CGF.Builder.getInt1(true)}, Bundles); + if (Bundles.empty()) { + auto *ResumeBB = CGF.getEHResumeBlock(/*cleanup=*/true); + auto *CleanupContBB = CGF.createBasicBlock("cleanup.cont"); + CGF.Builder.CreateCondBr(CoroEnd, ResumeBB, CleanupContBB); + CGF.EmitBlock(CleanupContBB); + } } }; EHStack.pushCleanup(EHCleanup);