Gor Nishanov | 97e3b6d | 2016-10-03 22:44:48 +0000 | [diff] [blame] | 1 | //===----- CGCoroutine.cpp - Emit LLVM Code for C++ coroutines ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This contains code dealing with C++ code generation of coroutines. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenFunction.h" |
Gor Nishanov | 5eb5858 | 2017-03-26 02:18:05 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/ScopeExit.h" |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 16 | #include "clang/AST/StmtCXX.h" |
Gor Nishanov | 97e3b6d | 2016-10-03 22:44:48 +0000 | [diff] [blame] | 17 | |
| 18 | using namespace clang; |
| 19 | using namespace CodeGen; |
| 20 | |
Gor Nishanov | 5eb5858 | 2017-03-26 02:18:05 +0000 | [diff] [blame] | 21 | using llvm::Value; |
| 22 | using llvm::BasicBlock; |
Gor Nishanov | 97e3b6d | 2016-10-03 22:44:48 +0000 | [diff] [blame] | 23 | |
Gor Nishanov | 5eb5858 | 2017-03-26 02:18:05 +0000 | [diff] [blame] | 24 | namespace { |
| 25 | enum class AwaitKind { Init, Normal, Yield, Final }; |
| 26 | static constexpr llvm::StringLiteral AwaitKindStr[] = {"init", "await", "yield", |
| 27 | "final"}; |
| 28 | } |
Gor Nishanov | 90be121 | 2017-03-06 21:12:54 +0000 | [diff] [blame] | 29 | |
Gor Nishanov | 5eb5858 | 2017-03-26 02:18:05 +0000 | [diff] [blame] | 30 | struct clang::CodeGen::CGCoroData { |
| 31 | // What is the current await expression kind and how many |
| 32 | // await/yield expressions were encountered so far. |
| 33 | // These are used to generate pretty labels for await expressions in LLVM IR. |
| 34 | AwaitKind CurrentAwaitKind = AwaitKind::Init; |
| 35 | unsigned AwaitNum = 0; |
| 36 | unsigned YieldNum = 0; |
| 37 | |
| 38 | // How many co_return statements are in the coroutine. Used to decide whether |
| 39 | // we need to add co_return; equivalent at the end of the user authored body. |
| 40 | unsigned CoreturnCount = 0; |
| 41 | |
| 42 | // A branch to this block is emitted when coroutine needs to suspend. |
| 43 | llvm::BasicBlock *SuspendBB = nullptr; |
| 44 | |
| 45 | // Stores the jump destination just before the coroutine memory is freed. |
| 46 | // This is the destination that every suspend point jumps to for the cleanup |
| 47 | // branch. |
| 48 | CodeGenFunction::JumpDest CleanupJD; |
| 49 | |
| 50 | // Stores the jump destination just before the final suspend. The co_return |
Gor Nishanov | 90be121 | 2017-03-06 21:12:54 +0000 | [diff] [blame] | 51 | // statements jumps to this point after calling return_xxx promise member. |
| 52 | CodeGenFunction::JumpDest FinalJD; |
| 53 | |
Gor Nishanov | 97e3b6d | 2016-10-03 22:44:48 +0000 | [diff] [blame] | 54 | // Stores the llvm.coro.id emitted in the function so that we can supply it |
| 55 | // as the first argument to coro.begin, coro.alloc and coro.free intrinsics. |
| 56 | // Note: llvm.coro.id returns a token that cannot be directly expressed in a |
| 57 | // builtin. |
| 58 | llvm::CallInst *CoroId = nullptr; |
Gor Nishanov | 5eb5858 | 2017-03-26 02:18:05 +0000 | [diff] [blame] | 59 | |
Gor Nishanov | 68fe6ee | 2017-05-23 03:46:59 +0000 | [diff] [blame^] | 60 | // Stores the llvm.coro.begin emitted in the function so that we can replace |
| 61 | // all coro.frame intrinsics with direct SSA value of coro.begin that returns |
| 62 | // the address of the coroutine frame of the current coroutine. |
| 63 | llvm::CallInst *CoroBegin = nullptr; |
| 64 | |
Gor Nishanov | 97e3b6d | 2016-10-03 22:44:48 +0000 | [diff] [blame] | 65 | // If coro.id came from the builtin, remember the expression to give better |
| 66 | // diagnostic. If CoroIdExpr is nullptr, the coro.id was created by |
| 67 | // EmitCoroutineBody. |
| 68 | CallExpr const *CoroIdExpr = nullptr; |
| 69 | }; |
Gor Nishanov | 97e3b6d | 2016-10-03 22:44:48 +0000 | [diff] [blame] | 70 | |
Gor Nishanov | 5eb5858 | 2017-03-26 02:18:05 +0000 | [diff] [blame] | 71 | // Defining these here allows to keep CGCoroData private to this file. |
Gor Nishanov | 97e3b6d | 2016-10-03 22:44:48 +0000 | [diff] [blame] | 72 | clang::CodeGen::CodeGenFunction::CGCoroInfo::CGCoroInfo() {} |
| 73 | CodeGenFunction::CGCoroInfo::~CGCoroInfo() {} |
| 74 | |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 75 | static void createCoroData(CodeGenFunction &CGF, |
Gor Nishanov | 97e3b6d | 2016-10-03 22:44:48 +0000 | [diff] [blame] | 76 | CodeGenFunction::CGCoroInfo &CurCoro, |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 77 | llvm::CallInst *CoroId, |
| 78 | CallExpr const *CoroIdExpr = nullptr) { |
Gor Nishanov | 97e3b6d | 2016-10-03 22:44:48 +0000 | [diff] [blame] | 79 | if (CurCoro.Data) { |
| 80 | if (CurCoro.Data->CoroIdExpr) |
| 81 | CGF.CGM.Error(CoroIdExpr->getLocStart(), |
| 82 | "only one __builtin_coro_id can be used in a function"); |
| 83 | else if (CoroIdExpr) |
| 84 | CGF.CGM.Error(CoroIdExpr->getLocStart(), |
| 85 | "__builtin_coro_id shall not be used in a C++ coroutine"); |
| 86 | else |
| 87 | llvm_unreachable("EmitCoroutineBodyStatement called twice?"); |
| 88 | |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 89 | return; |
Gor Nishanov | 97e3b6d | 2016-10-03 22:44:48 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | CurCoro.Data = std::unique_ptr<CGCoroData>(new CGCoroData); |
| 93 | CurCoro.Data->CoroId = CoroId; |
| 94 | CurCoro.Data->CoroIdExpr = CoroIdExpr; |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Gor Nishanov | 5eb5858 | 2017-03-26 02:18:05 +0000 | [diff] [blame] | 97 | // Synthesize a pretty name for a suspend point. |
| 98 | static SmallString<32> buildSuspendPrefixStr(CGCoroData &Coro, AwaitKind Kind) { |
| 99 | unsigned No = 0; |
| 100 | switch (Kind) { |
| 101 | case AwaitKind::Init: |
| 102 | case AwaitKind::Final: |
| 103 | break; |
| 104 | case AwaitKind::Normal: |
| 105 | No = ++Coro.AwaitNum; |
| 106 | break; |
| 107 | case AwaitKind::Yield: |
| 108 | No = ++Coro.YieldNum; |
| 109 | break; |
| 110 | } |
| 111 | SmallString<32> Prefix(AwaitKindStr[static_cast<unsigned>(Kind)]); |
| 112 | if (No > 1) { |
| 113 | Twine(No).toVector(Prefix); |
| 114 | } |
| 115 | return Prefix; |
| 116 | } |
| 117 | |
| 118 | // Emit suspend expression which roughly looks like: |
| 119 | // |
| 120 | // auto && x = CommonExpr(); |
| 121 | // if (!x.await_ready()) { |
| 122 | // llvm_coro_save(); |
| 123 | // x.await_suspend(...); (*) |
| 124 | // llvm_coro_suspend(); (**) |
| 125 | // } |
| 126 | // x.await_resume(); |
| 127 | // |
| 128 | // where the result of the entire expression is the result of x.await_resume() |
| 129 | // |
| 130 | // (*) If x.await_suspend return type is bool, it allows to veto a suspend: |
| 131 | // if (x.await_suspend(...)) |
| 132 | // llvm_coro_suspend(); |
| 133 | // |
| 134 | // (**) llvm_coro_suspend() encodes three possible continuations as |
| 135 | // a switch instruction: |
| 136 | // |
| 137 | // %where-to = call i8 @llvm.coro.suspend(...) |
| 138 | // switch i8 %where-to, label %coro.ret [ ; jump to epilogue to suspend |
| 139 | // i8 0, label %yield.ready ; go here when resumed |
| 140 | // i8 1, label %yield.cleanup ; go here when destroyed |
| 141 | // ] |
| 142 | // |
| 143 | // See llvm's docs/Coroutines.rst for more details. |
| 144 | // |
| 145 | static RValue emitSuspendExpression(CodeGenFunction &CGF, CGCoroData &Coro, |
| 146 | CoroutineSuspendExpr const &S, |
| 147 | AwaitKind Kind, AggValueSlot aggSlot, |
| 148 | bool ignoreResult) { |
| 149 | auto *E = S.getCommonExpr(); |
| 150 | auto Binder = |
| 151 | CodeGenFunction::OpaqueValueMappingData::bind(CGF, S.getOpaqueValue(), E); |
| 152 | auto UnbindOnExit = llvm::make_scope_exit([&] { Binder.unbind(CGF); }); |
| 153 | |
| 154 | auto Prefix = buildSuspendPrefixStr(Coro, Kind); |
| 155 | BasicBlock *ReadyBlock = CGF.createBasicBlock(Prefix + Twine(".ready")); |
| 156 | BasicBlock *SuspendBlock = CGF.createBasicBlock(Prefix + Twine(".suspend")); |
| 157 | BasicBlock *CleanupBlock = CGF.createBasicBlock(Prefix + Twine(".cleanup")); |
| 158 | |
| 159 | // If expression is ready, no need to suspend. |
| 160 | CGF.EmitBranchOnBoolExpr(S.getReadyExpr(), ReadyBlock, SuspendBlock, 0); |
| 161 | |
| 162 | // Otherwise, emit suspend logic. |
| 163 | CGF.EmitBlock(SuspendBlock); |
| 164 | |
| 165 | auto &Builder = CGF.Builder; |
| 166 | llvm::Function *CoroSave = CGF.CGM.getIntrinsic(llvm::Intrinsic::coro_save); |
| 167 | auto *NullPtr = llvm::ConstantPointerNull::get(CGF.CGM.Int8PtrTy); |
| 168 | auto *SaveCall = Builder.CreateCall(CoroSave, {NullPtr}); |
| 169 | |
| 170 | auto *SuspendRet = CGF.EmitScalarExpr(S.getSuspendExpr()); |
| 171 | if (SuspendRet != nullptr) { |
| 172 | // Veto suspension if requested by bool returning await_suspend. |
| 173 | assert(SuspendRet->getType()->isIntegerTy(1) && |
| 174 | "Sema should have already checked that it is void or bool"); |
| 175 | BasicBlock *RealSuspendBlock = |
| 176 | CGF.createBasicBlock(Prefix + Twine(".suspend.bool")); |
| 177 | CGF.Builder.CreateCondBr(SuspendRet, RealSuspendBlock, ReadyBlock); |
| 178 | SuspendBlock = RealSuspendBlock; |
| 179 | CGF.EmitBlock(RealSuspendBlock); |
| 180 | } |
| 181 | |
| 182 | // Emit the suspend point. |
| 183 | const bool IsFinalSuspend = (Kind == AwaitKind::Final); |
| 184 | llvm::Function *CoroSuspend = |
| 185 | CGF.CGM.getIntrinsic(llvm::Intrinsic::coro_suspend); |
| 186 | auto *SuspendResult = Builder.CreateCall( |
| 187 | CoroSuspend, {SaveCall, Builder.getInt1(IsFinalSuspend)}); |
| 188 | |
| 189 | // Create a switch capturing three possible continuations. |
| 190 | auto *Switch = Builder.CreateSwitch(SuspendResult, Coro.SuspendBB, 2); |
| 191 | Switch->addCase(Builder.getInt8(0), ReadyBlock); |
| 192 | Switch->addCase(Builder.getInt8(1), CleanupBlock); |
| 193 | |
| 194 | // Emit cleanup for this suspend point. |
| 195 | CGF.EmitBlock(CleanupBlock); |
| 196 | CGF.EmitBranchThroughCleanup(Coro.CleanupJD); |
| 197 | |
| 198 | // Emit await_resume expression. |
| 199 | CGF.EmitBlock(ReadyBlock); |
| 200 | return CGF.EmitAnyExpr(S.getResumeExpr(), aggSlot, ignoreResult); |
| 201 | } |
| 202 | |
| 203 | RValue CodeGenFunction::EmitCoawaitExpr(const CoawaitExpr &E, |
| 204 | AggValueSlot aggSlot, |
| 205 | bool ignoreResult) { |
| 206 | return emitSuspendExpression(*this, *CurCoro.Data, E, |
| 207 | CurCoro.Data->CurrentAwaitKind, aggSlot, |
| 208 | ignoreResult); |
| 209 | } |
| 210 | RValue CodeGenFunction::EmitCoyieldExpr(const CoyieldExpr &E, |
| 211 | AggValueSlot aggSlot, |
| 212 | bool ignoreResult) { |
| 213 | return emitSuspendExpression(*this, *CurCoro.Data, E, AwaitKind::Yield, |
| 214 | aggSlot, ignoreResult); |
| 215 | } |
| 216 | |
Gor Nishanov | 90be121 | 2017-03-06 21:12:54 +0000 | [diff] [blame] | 217 | void CodeGenFunction::EmitCoreturnStmt(CoreturnStmt const &S) { |
| 218 | ++CurCoro.Data->CoreturnCount; |
| 219 | EmitStmt(S.getPromiseCall()); |
| 220 | EmitBranchThroughCleanup(CurCoro.Data->FinalJD); |
| 221 | } |
| 222 | |
Gor Nishanov | 818a776 | 2017-04-05 04:55:03 +0000 | [diff] [blame] | 223 | // For WinEH exception representation backend need to know what funclet coro.end |
| 224 | // belongs to. That information is passed in a funclet bundle. |
| 225 | static SmallVector<llvm::OperandBundleDef, 1> |
| 226 | getBundlesForCoroEnd(CodeGenFunction &CGF) { |
| 227 | SmallVector<llvm::OperandBundleDef, 1> BundleList; |
| 228 | |
| 229 | if (llvm::Instruction *EHPad = CGF.CurrentFuncletPad) |
| 230 | BundleList.emplace_back("funclet", EHPad); |
| 231 | |
| 232 | return BundleList; |
| 233 | } |
| 234 | |
| 235 | namespace { |
| 236 | // We will insert coro.end to cut any of the destructors for objects that |
| 237 | // do not need to be destroyed once the coroutine is resumed. |
| 238 | // See llvm/docs/Coroutines.rst for more details about coro.end. |
| 239 | struct CallCoroEnd final : public EHScopeStack::Cleanup { |
| 240 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
| 241 | auto &CGM = CGF.CGM; |
| 242 | auto *NullPtr = llvm::ConstantPointerNull::get(CGF.Int8PtrTy); |
| 243 | llvm::Function *CoroEndFn = CGM.getIntrinsic(llvm::Intrinsic::coro_end); |
| 244 | // See if we have a funclet bundle to associate coro.end with. (WinEH) |
| 245 | auto Bundles = getBundlesForCoroEnd(CGF); |
| 246 | auto *CoroEnd = CGF.Builder.CreateCall( |
| 247 | CoroEndFn, {NullPtr, CGF.Builder.getTrue()}, Bundles); |
| 248 | if (Bundles.empty()) { |
| 249 | // Otherwise, (landingpad model), create a conditional branch that leads |
| 250 | // either to a cleanup block or a block with EH resume instruction. |
| 251 | auto *ResumeBB = CGF.getEHResumeBlock(/*cleanup=*/true); |
| 252 | auto *CleanupContBB = CGF.createBasicBlock("cleanup.cont"); |
| 253 | CGF.Builder.CreateCondBr(CoroEnd, ResumeBB, CleanupContBB); |
| 254 | CGF.EmitBlock(CleanupContBB); |
| 255 | } |
| 256 | } |
| 257 | }; |
| 258 | } |
| 259 | |
Gor Nishanov | 63b6df4 | 2017-04-01 00:22:47 +0000 | [diff] [blame] | 260 | namespace { |
| 261 | // Make sure to call coro.delete on scope exit. |
| 262 | struct CallCoroDelete final : public EHScopeStack::Cleanup { |
| 263 | Stmt *Deallocate; |
| 264 | |
| 265 | // TODO: Wrap deallocate in if(coro.free(...)) Deallocate. |
| 266 | void Emit(CodeGenFunction &CGF, Flags) override { |
| 267 | // Note: That deallocation will be emitted twice: once for a normal exit and |
| 268 | // once for exceptional exit. This usage is safe because Deallocate does not |
| 269 | // contain any declarations. The SubStmtBuilder::makeNewAndDeleteExpr() |
| 270 | // builds a single call to a deallocation function which is safe to emit |
| 271 | // multiple times. |
| 272 | CGF.EmitStmt(Deallocate); |
| 273 | } |
| 274 | explicit CallCoroDelete(Stmt *DeallocStmt) : Deallocate(DeallocStmt) {} |
| 275 | }; |
| 276 | } |
| 277 | |
Gor Nishanov | 5b050e4 | 2017-05-22 22:33:17 +0000 | [diff] [blame] | 278 | static void emitBodyAndFallthrough(CodeGenFunction &CGF, |
| 279 | const CoroutineBodyStmt &S, Stmt *Body) { |
| 280 | CGF.EmitStmt(Body); |
| 281 | const bool CanFallthrough = CGF.Builder.GetInsertBlock(); |
| 282 | if (CanFallthrough) |
| 283 | if (Stmt *OnFallthrough = S.getFallthroughHandler()) |
| 284 | CGF.EmitStmt(OnFallthrough); |
| 285 | } |
| 286 | |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 287 | void CodeGenFunction::EmitCoroutineBody(const CoroutineBodyStmt &S) { |
| 288 | auto *NullPtr = llvm::ConstantPointerNull::get(Builder.getInt8PtrTy()); |
| 289 | auto &TI = CGM.getContext().getTargetInfo(); |
| 290 | unsigned NewAlign = TI.getNewAlign() / TI.getCharWidth(); |
| 291 | |
Gor Nishanov | aa6e9a9 | 2017-05-23 01:13:17 +0000 | [diff] [blame] | 292 | auto *EntryBB = Builder.GetInsertBlock(); |
| 293 | auto *AllocBB = createBasicBlock("coro.alloc"); |
| 294 | auto *InitBB = createBasicBlock("coro.init"); |
Gor Nishanov | 90be121 | 2017-03-06 21:12:54 +0000 | [diff] [blame] | 295 | auto *FinalBB = createBasicBlock("coro.final"); |
Gor Nishanov | 5eb5858 | 2017-03-26 02:18:05 +0000 | [diff] [blame] | 296 | auto *RetBB = createBasicBlock("coro.ret"); |
Gor Nishanov | 90be121 | 2017-03-06 21:12:54 +0000 | [diff] [blame] | 297 | |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 298 | auto *CoroId = Builder.CreateCall( |
| 299 | CGM.getIntrinsic(llvm::Intrinsic::coro_id), |
| 300 | {Builder.getInt32(NewAlign), NullPtr, NullPtr, NullPtr}); |
| 301 | createCoroData(*this, CurCoro, CoroId); |
Gor Nishanov | 5eb5858 | 2017-03-26 02:18:05 +0000 | [diff] [blame] | 302 | CurCoro.Data->SuspendBB = RetBB; |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 303 | |
Gor Nishanov | aa6e9a9 | 2017-05-23 01:13:17 +0000 | [diff] [blame] | 304 | // Backend is allowed to elide memory allocations, to help it, emit |
| 305 | // auto mem = coro.alloc() ? 0 : ... allocation code ...; |
| 306 | auto *CoroAlloc = Builder.CreateCall( |
| 307 | CGM.getIntrinsic(llvm::Intrinsic::coro_alloc), {CoroId}); |
| 308 | |
| 309 | Builder.CreateCondBr(CoroAlloc, AllocBB, InitBB); |
| 310 | |
| 311 | EmitBlock(AllocBB); |
Gor Nishanov | 3aa9eb3 | 2017-03-27 23:36:59 +0000 | [diff] [blame] | 312 | auto *AllocateCall = EmitScalarExpr(S.getAllocate()); |
Gor Nishanov | aa6e9a9 | 2017-05-23 01:13:17 +0000 | [diff] [blame] | 313 | auto *AllocOrInvokeContBB = Builder.GetInsertBlock(); |
Gor Nishanov | 3aa9eb3 | 2017-03-27 23:36:59 +0000 | [diff] [blame] | 314 | |
| 315 | // Handle allocation failure if 'ReturnStmtOnAllocFailure' was provided. |
| 316 | if (auto *RetOnAllocFailure = S.getReturnStmtOnAllocFailure()) { |
| 317 | auto *RetOnFailureBB = createBasicBlock("coro.ret.on.failure"); |
Gor Nishanov | 3aa9eb3 | 2017-03-27 23:36:59 +0000 | [diff] [blame] | 318 | |
| 319 | // See if allocation was successful. |
| 320 | auto *NullPtr = llvm::ConstantPointerNull::get(Int8PtrTy); |
| 321 | auto *Cond = Builder.CreateICmpNE(AllocateCall, NullPtr); |
| 322 | Builder.CreateCondBr(Cond, InitBB, RetOnFailureBB); |
| 323 | |
| 324 | // If not, return OnAllocFailure object. |
| 325 | EmitBlock(RetOnFailureBB); |
| 326 | EmitStmt(RetOnAllocFailure); |
Gor Nishanov | 3aa9eb3 | 2017-03-27 23:36:59 +0000 | [diff] [blame] | 327 | } |
Gor Nishanov | aa6e9a9 | 2017-05-23 01:13:17 +0000 | [diff] [blame] | 328 | else { |
| 329 | Builder.CreateBr(InitBB); |
| 330 | } |
| 331 | |
| 332 | EmitBlock(InitBB); |
| 333 | |
| 334 | // Pass the result of the allocation to coro.begin. |
| 335 | auto *Phi = Builder.CreatePHI(VoidPtrTy, 2); |
| 336 | Phi->addIncoming(NullPtr, EntryBB); |
| 337 | Phi->addIncoming(AllocateCall, AllocOrInvokeContBB); |
Gor Nishanov | 68fe6ee | 2017-05-23 03:46:59 +0000 | [diff] [blame^] | 338 | auto *CoroBegin = Builder.CreateCall( |
Gor Nishanov | aa6e9a9 | 2017-05-23 01:13:17 +0000 | [diff] [blame] | 339 | CGM.getIntrinsic(llvm::Intrinsic::coro_begin), {CoroId, Phi}); |
Gor Nishanov | 68fe6ee | 2017-05-23 03:46:59 +0000 | [diff] [blame^] | 340 | CurCoro.Data->CoroBegin = CoroBegin; |
Gor Nishanov | 90be121 | 2017-03-06 21:12:54 +0000 | [diff] [blame] | 341 | |
Gor Nishanov | 5eb5858 | 2017-03-26 02:18:05 +0000 | [diff] [blame] | 342 | CurCoro.Data->CleanupJD = getJumpDestInCurrentScope(RetBB); |
Gor Nishanov | 63b6df4 | 2017-04-01 00:22:47 +0000 | [diff] [blame] | 343 | { |
| 344 | CodeGenFunction::RunCleanupsScope ResumeScope(*this); |
| 345 | EHStack.pushCleanup<CallCoroDelete>(NormalAndEHCleanup, S.getDeallocate()); |
Gor Nishanov | 90be121 | 2017-03-06 21:12:54 +0000 | [diff] [blame] | 346 | |
Gor Nishanov | 63b6df4 | 2017-04-01 00:22:47 +0000 | [diff] [blame] | 347 | EmitStmt(S.getPromiseDeclStmt()); |
Gor Nishanov | 6a47068 | 2017-05-22 20:22:23 +0000 | [diff] [blame] | 348 | EmitStmt(S.getResultDecl()); // FIXME: Gro lifetime is wrong. |
Gor Nishanov | 90be121 | 2017-03-06 21:12:54 +0000 | [diff] [blame] | 349 | |
Gor Nishanov | 818a776 | 2017-04-05 04:55:03 +0000 | [diff] [blame] | 350 | EHStack.pushCleanup<CallCoroEnd>(EHCleanup); |
| 351 | |
Gor Nishanov | 63b6df4 | 2017-04-01 00:22:47 +0000 | [diff] [blame] | 352 | CurCoro.Data->FinalJD = getJumpDestInCurrentScope(FinalBB); |
Gor Nishanov | 90be121 | 2017-03-06 21:12:54 +0000 | [diff] [blame] | 353 | |
Gor Nishanov | 63b6df4 | 2017-04-01 00:22:47 +0000 | [diff] [blame] | 354 | // FIXME: Emit initial suspend and more before the body. |
| 355 | |
| 356 | CurCoro.Data->CurrentAwaitKind = AwaitKind::Normal; |
Gor Nishanov | 5b050e4 | 2017-05-22 22:33:17 +0000 | [diff] [blame] | 357 | |
| 358 | if (auto *OnException = S.getExceptionHandler()) { |
| 359 | auto Loc = S.getLocStart(); |
| 360 | CXXCatchStmt Catch(Loc, /*exDecl=*/nullptr, OnException); |
| 361 | auto *TryStmt = CXXTryStmt::Create(getContext(), Loc, S.getBody(), &Catch); |
| 362 | |
| 363 | EnterCXXTryStmt(*TryStmt); |
| 364 | emitBodyAndFallthrough(*this, S, TryStmt->getTryBlock()); |
| 365 | ExitCXXTryStmt(*TryStmt); |
| 366 | } |
| 367 | else { |
| 368 | emitBodyAndFallthrough(*this, S, S.getBody()); |
| 369 | } |
Gor Nishanov | 63b6df4 | 2017-04-01 00:22:47 +0000 | [diff] [blame] | 370 | |
| 371 | // See if we need to generate final suspend. |
| 372 | const bool CanFallthrough = Builder.GetInsertBlock(); |
| 373 | const bool HasCoreturns = CurCoro.Data->CoreturnCount > 0; |
| 374 | if (CanFallthrough || HasCoreturns) { |
| 375 | EmitBlock(FinalBB); |
| 376 | // FIXME: Emit final suspend. |
| 377 | } |
Gor Nishanov | 90be121 | 2017-03-06 21:12:54 +0000 | [diff] [blame] | 378 | } |
Gor Nishanov | 90be121 | 2017-03-06 21:12:54 +0000 | [diff] [blame] | 379 | |
Gor Nishanov | 5eb5858 | 2017-03-26 02:18:05 +0000 | [diff] [blame] | 380 | EmitBlock(RetBB); |
Gor Nishanov | 6a47068 | 2017-05-22 20:22:23 +0000 | [diff] [blame] | 381 | // Emit coro.end before getReturnStmt (and parameter destructors), since |
| 382 | // resume and destroy parts of the coroutine should not include them. |
Gor Nishanov | 818a776 | 2017-04-05 04:55:03 +0000 | [diff] [blame] | 383 | llvm::Function *CoroEnd = CGM.getIntrinsic(llvm::Intrinsic::coro_end); |
| 384 | Builder.CreateCall(CoroEnd, {NullPtr, Builder.getFalse()}); |
Gor Nishanov | 5eb5858 | 2017-03-26 02:18:05 +0000 | [diff] [blame] | 385 | |
Gor Nishanov | 6a47068 | 2017-05-22 20:22:23 +0000 | [diff] [blame] | 386 | if (Stmt *Ret = S.getReturnStmt()) |
| 387 | EmitStmt(Ret); |
Gor Nishanov | 97e3b6d | 2016-10-03 22:44:48 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | // Emit coroutine intrinsic and patch up arguments of the token type. |
| 391 | RValue CodeGenFunction::EmitCoroutineIntrinsic(const CallExpr *E, |
| 392 | unsigned int IID) { |
| 393 | SmallVector<llvm::Value *, 8> Args; |
| 394 | switch (IID) { |
| 395 | default: |
| 396 | break; |
Gor Nishanov | 68fe6ee | 2017-05-23 03:46:59 +0000 | [diff] [blame^] | 397 | // The coro.frame builtin is replaced with an SSA value of the coro.begin |
| 398 | // intrinsic. |
| 399 | case llvm::Intrinsic::coro_frame: { |
| 400 | if (CurCoro.Data && CurCoro.Data->CoroBegin) { |
| 401 | return RValue::get(CurCoro.Data->CoroBegin); |
| 402 | } |
| 403 | CGM.Error(E->getLocStart(), "this builtin expect that __builtin_coro_begin " |
| 404 | "has been used earlier in this function"); |
| 405 | auto NullPtr = llvm::ConstantPointerNull::get(Builder.getInt8PtrTy()); |
| 406 | return RValue::get(NullPtr); |
| 407 | } |
Gor Nishanov | 97e3b6d | 2016-10-03 22:44:48 +0000 | [diff] [blame] | 408 | // The following three intrinsics take a token parameter referring to a token |
| 409 | // returned by earlier call to @llvm.coro.id. Since we cannot represent it in |
| 410 | // builtins, we patch it up here. |
| 411 | case llvm::Intrinsic::coro_alloc: |
| 412 | case llvm::Intrinsic::coro_begin: |
| 413 | case llvm::Intrinsic::coro_free: { |
| 414 | if (CurCoro.Data && CurCoro.Data->CoroId) { |
| 415 | Args.push_back(CurCoro.Data->CoroId); |
| 416 | break; |
| 417 | } |
| 418 | CGM.Error(E->getLocStart(), "this builtin expect that __builtin_coro_id has" |
| 419 | " been used earlier in this function"); |
| 420 | // Fallthrough to the next case to add TokenNone as the first argument. |
| 421 | } |
| 422 | // @llvm.coro.suspend takes a token parameter. Add token 'none' as the first |
| 423 | // argument. |
| 424 | case llvm::Intrinsic::coro_suspend: |
| 425 | Args.push_back(llvm::ConstantTokenNone::get(getLLVMContext())); |
| 426 | break; |
| 427 | } |
| 428 | for (auto &Arg : E->arguments()) |
| 429 | Args.push_back(EmitScalarExpr(Arg)); |
| 430 | |
| 431 | llvm::Value *F = CGM.getIntrinsic(IID); |
| 432 | llvm::CallInst *Call = Builder.CreateCall(F, Args); |
| 433 | |
Gor Nishanov | 68fe6ee | 2017-05-23 03:46:59 +0000 | [diff] [blame^] | 434 | // Note: The following code is to enable to emit coroutine intrinsics by |
| 435 | // hand to experiment with coroutines in C. |
Gor Nishanov | 97e3b6d | 2016-10-03 22:44:48 +0000 | [diff] [blame] | 436 | // If we see @llvm.coro.id remember it in the CoroData. We will update |
| 437 | // coro.alloc, coro.begin and coro.free intrinsics to refer to it. |
| 438 | if (IID == llvm::Intrinsic::coro_id) { |
| 439 | createCoroData(*this, CurCoro, Call, E); |
| 440 | } |
Gor Nishanov | 68fe6ee | 2017-05-23 03:46:59 +0000 | [diff] [blame^] | 441 | else if (IID == llvm::Intrinsic::coro_begin) { |
| 442 | if (CurCoro.Data) |
| 443 | CurCoro.Data->CoroBegin = Call; |
| 444 | } |
Gor Nishanov | 97e3b6d | 2016-10-03 22:44:48 +0000 | [diff] [blame] | 445 | return RValue::get(Call); |
| 446 | } |