@@ -105,10 +105,32 @@ void LLVMCoroutine::createSuspend()
105105 m_builder->SetInsertPoint (resumeBranch);
106106}
107107
108- llvm::Value *LLVMCoroutine::createResume (llvm::Value *coroHandle)
108+ llvm::Value *LLVMCoroutine::createResume (llvm::Function *function, llvm:: Value *coroHandle)
109109{
110+ llvm::LLVMContext &ctx = m_builder->getContext ();
111+ llvm::Function *coroDone = llvm::Intrinsic::getDeclaration (m_module, llvm::Intrinsic::coro_done);
112+
113+ llvm::Value *ret = m_builder->CreateAlloca (m_builder->getInt1Ty ());
114+ llvm::Value *done = m_builder->CreateCall (coroDone, { coroHandle });
115+ done = m_builder->CreateCall (coroDone, { coroHandle });
116+
117+ llvm::BasicBlock *destroyBranch = llvm::BasicBlock::Create (ctx, " " , function);
118+ llvm::BasicBlock *resumeBranch = llvm::BasicBlock::Create (ctx, " " , function);
119+ llvm::BasicBlock *nextBranch = llvm::BasicBlock::Create (ctx, " " , function);
120+ m_builder->CreateCondBr (done, destroyBranch, resumeBranch);
121+
122+ m_builder->SetInsertPoint (destroyBranch);
123+ m_builder->CreateCall (llvm::Intrinsic::getDeclaration (m_module, llvm::Intrinsic::coro_destroy), { coroHandle });
124+ m_builder->CreateBr (nextBranch);
125+
126+ m_builder->SetInsertPoint (resumeBranch);
110127 m_builder->CreateCall (llvm::Intrinsic::getDeclaration (m_module, llvm::Intrinsic::coro_resume), { coroHandle });
111- return m_builder->CreateCall (llvm::Intrinsic::getDeclaration (m_module, llvm::Intrinsic::coro_done), { coroHandle });
128+ done = m_builder->CreateCall (coroDone, { coroHandle });
129+ m_builder->CreateStore (done, ret);
130+ m_builder->CreateCondBr (done, destroyBranch, nextBranch);
131+
132+ m_builder->SetInsertPoint (nextBranch);
133+ return m_builder->CreateLoad (m_builder->getInt1Ty (), ret);
112134}
113135
114136void LLVMCoroutine::end ()
0 commit comments