1 | //===----- CGCoroutine.cpp - Emit LLVM Code for C++ coroutines ------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This contains code dealing with C++ code generation of coroutines. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #include "CGCleanup.h" |
14 | #include "CodeGenFunction.h" |
15 | #include "llvm/ADT/ScopeExit.h" |
16 | #include "clang/AST/StmtCXX.h" |
17 | #include "clang/AST/StmtVisitor.h" |
18 | |
19 | using namespace clang; |
20 | using namespace CodeGen; |
21 | |
22 | using llvm::Value; |
23 | using llvm::BasicBlock; |
24 | |
25 | namespace { |
26 | enum class AwaitKind { Init, Normal, Yield, Final }; |
27 | static constexpr llvm::StringLiteral AwaitKindStr[] = {"init" , "await" , "yield" , |
28 | "final" }; |
29 | } |
30 | |
31 | struct clang::CodeGen::CGCoroData { |
32 | // What is the current await expression kind and how many |
33 | // await/yield expressions were encountered so far. |
34 | // These are used to generate pretty labels for await expressions in LLVM IR. |
35 | AwaitKind CurrentAwaitKind = AwaitKind::Init; |
36 | unsigned AwaitNum = 0; |
37 | unsigned YieldNum = 0; |
38 | |
39 | // How many co_return statements are in the coroutine. Used to decide whether |
40 | // we need to add co_return; equivalent at the end of the user authored body. |
41 | unsigned CoreturnCount = 0; |
42 | |
43 | // A branch to this block is emitted when coroutine needs to suspend. |
44 | llvm::BasicBlock *SuspendBB = nullptr; |
45 | |
46 | // The promise type's 'unhandled_exception' handler, if it defines one. |
47 | Stmt *ExceptionHandler = nullptr; |
48 | |
49 | // A temporary i1 alloca that stores whether 'await_resume' threw an |
50 | // exception. If it did, 'true' is stored in this variable, and the coroutine |
51 | // body must be skipped. If the promise type does not define an exception |
52 | // handler, this is null. |
53 | llvm::Value *ResumeEHVar = nullptr; |
54 | |
55 | // Stores the jump destination just before the coroutine memory is freed. |
56 | // This is the destination that every suspend point jumps to for the cleanup |
57 | // branch. |
58 | CodeGenFunction::JumpDest CleanupJD; |
59 | |
60 | // Stores the jump destination just before the final suspend. The co_return |
61 | // statements jumps to this point after calling return_xxx promise member. |
62 | CodeGenFunction::JumpDest FinalJD; |
63 | |
64 | // Stores the llvm.coro.id emitted in the function so that we can supply it |
65 | // as the first argument to coro.begin, coro.alloc and coro.free intrinsics. |
66 | // Note: llvm.coro.id returns a token that cannot be directly expressed in a |
67 | // builtin. |
68 | llvm::CallInst *CoroId = nullptr; |
69 | |
70 | // Stores the llvm.coro.begin emitted in the function so that we can replace |
71 | // all coro.frame intrinsics with direct SSA value of coro.begin that returns |
72 | // the address of the coroutine frame of the current coroutine. |
73 | llvm::CallInst *CoroBegin = nullptr; |
74 | |
75 | // Stores the last emitted coro.free for the deallocate expressions, we use it |
76 | // to wrap dealloc code with if(auto mem = coro.free) dealloc(mem). |
77 | llvm::CallInst *LastCoroFree = nullptr; |
78 | |
79 | // If coro.id came from the builtin, remember the expression to give better |
80 | // diagnostic. If CoroIdExpr is nullptr, the coro.id was created by |
81 | // EmitCoroutineBody. |
82 | CallExpr const *CoroIdExpr = nullptr; |
83 | }; |
84 | |
85 | // Defining these here allows to keep CGCoroData private to this file. |
86 | clang::CodeGen::CodeGenFunction::CGCoroInfo::CGCoroInfo() {} |
87 | CodeGenFunction::CGCoroInfo::~CGCoroInfo() {} |
88 | |
89 | static void createCoroData(CodeGenFunction &CGF, |
90 | CodeGenFunction::CGCoroInfo &CurCoro, |
91 | llvm::CallInst *CoroId, |
92 | CallExpr const *CoroIdExpr = nullptr) { |
93 | if (CurCoro.Data) { |
94 | if (CurCoro.Data->CoroIdExpr) |
95 | CGF.CGM.Error(loc: CoroIdExpr->getBeginLoc(), |
96 | error: "only one __builtin_coro_id can be used in a function" ); |
97 | else if (CoroIdExpr) |
98 | CGF.CGM.Error(loc: CoroIdExpr->getBeginLoc(), |
99 | error: "__builtin_coro_id shall not be used in a C++ coroutine" ); |
100 | else |
101 | llvm_unreachable("EmitCoroutineBodyStatement called twice?" ); |
102 | |
103 | return; |
104 | } |
105 | |
106 | CurCoro.Data = std::make_unique<CGCoroData>(); |
107 | CurCoro.Data->CoroId = CoroId; |
108 | CurCoro.Data->CoroIdExpr = CoroIdExpr; |
109 | } |
110 | |
111 | // Synthesize a pretty name for a suspend point. |
112 | static SmallString<32> buildSuspendPrefixStr(CGCoroData &Coro, AwaitKind Kind) { |
113 | unsigned No = 0; |
114 | switch (Kind) { |
115 | case AwaitKind::Init: |
116 | case AwaitKind::Final: |
117 | break; |
118 | case AwaitKind::Normal: |
119 | No = ++Coro.AwaitNum; |
120 | break; |
121 | case AwaitKind::Yield: |
122 | No = ++Coro.YieldNum; |
123 | break; |
124 | } |
125 | SmallString<32> Prefix(AwaitKindStr[static_cast<unsigned>(Kind)]); |
126 | if (No > 1) { |
127 | Twine(No).toVector(Out&: Prefix); |
128 | } |
129 | return Prefix; |
130 | } |
131 | |
132 | // Check if function can throw based on prototype noexcept, also works for |
133 | // destructors which are implicitly noexcept but can be marked noexcept(false). |
134 | static bool FunctionCanThrow(const FunctionDecl *D) { |
135 | const auto *Proto = D->getType()->getAs<FunctionProtoType>(); |
136 | if (!Proto) { |
137 | // Function proto is not found, we conservatively assume throwing. |
138 | return true; |
139 | } |
140 | return !isNoexceptExceptionSpec(ESpecType: Proto->getExceptionSpecType()) || |
141 | Proto->canThrow() != CT_Cannot; |
142 | } |
143 | |
144 | static bool StmtCanThrow(const Stmt *S) { |
145 | if (const auto *CE = dyn_cast<CallExpr>(Val: S)) { |
146 | const auto *Callee = CE->getDirectCallee(); |
147 | if (!Callee) |
148 | // We don't have direct callee. Conservatively assume throwing. |
149 | return true; |
150 | |
151 | if (FunctionCanThrow(D: Callee)) |
152 | return true; |
153 | |
154 | // Fall through to visit the children. |
155 | } |
156 | |
157 | if (const auto *TE = dyn_cast<CXXBindTemporaryExpr>(Val: S)) { |
158 | // Special handling of CXXBindTemporaryExpr here as calling of Dtor of the |
159 | // temporary is not part of `children()` as covered in the fall through. |
160 | // We need to mark entire statement as throwing if the destructor of the |
161 | // temporary throws. |
162 | const auto *Dtor = TE->getTemporary()->getDestructor(); |
163 | if (FunctionCanThrow(D: Dtor)) |
164 | return true; |
165 | |
166 | // Fall through to visit the children. |
167 | } |
168 | |
169 | for (const auto *child : S->children()) |
170 | if (StmtCanThrow(S: child)) |
171 | return true; |
172 | |
173 | return false; |
174 | } |
175 | |
176 | // Emit suspend expression which roughly looks like: |
177 | // |
178 | // auto && x = CommonExpr(); |
179 | // if (!x.await_ready()) { |
180 | // llvm_coro_save(); |
181 | // llvm_coro_await_suspend(&x, frame, wrapper) (*) (**) |
182 | // llvm_coro_suspend(); (***) |
183 | // } |
184 | // x.await_resume(); |
185 | // |
186 | // where the result of the entire expression is the result of x.await_resume() |
187 | // |
188 | // (*) llvm_coro_await_suspend_{void, bool, handle} is lowered to |
189 | // wrapper(&x, frame) when it's certain not to interfere with |
190 | // coroutine transform. await_suspend expression is |
191 | // asynchronous to the coroutine body and not all analyses |
192 | // and transformations can handle it correctly at the moment. |
193 | // |
194 | // Wrapper function encapsulates x.await_suspend(...) call and looks like: |
195 | // |
196 | // auto __await_suspend_wrapper(auto& awaiter, void* frame) { |
197 | // std::coroutine_handle<> handle(frame); |
198 | // return awaiter.await_suspend(handle); |
199 | // } |
200 | // |
201 | // (**) If x.await_suspend return type is bool, it allows to veto a suspend: |
202 | // if (x.await_suspend(...)) |
203 | // llvm_coro_suspend(); |
204 | // |
205 | // (***) llvm_coro_suspend() encodes three possible continuations as |
206 | // a switch instruction: |
207 | // |
208 | // %where-to = call i8 @llvm.coro.suspend(...) |
209 | // switch i8 %where-to, label %coro.ret [ ; jump to epilogue to suspend |
210 | // i8 0, label %yield.ready ; go here when resumed |
211 | // i8 1, label %yield.cleanup ; go here when destroyed |
212 | // ] |
213 | // |
214 | // See llvm's docs/Coroutines.rst for more details. |
215 | // |
216 | namespace { |
217 | struct LValueOrRValue { |
218 | LValue LV; |
219 | RValue RV; |
220 | }; |
221 | } |
222 | static LValueOrRValue emitSuspendExpression(CodeGenFunction &CGF, CGCoroData &Coro, |
223 | CoroutineSuspendExpr const &S, |
224 | AwaitKind Kind, AggValueSlot aggSlot, |
225 | bool ignoreResult, bool forLValue) { |
226 | auto *E = S.getCommonExpr(); |
227 | |
228 | auto CommonBinder = |
229 | CodeGenFunction::OpaqueValueMappingData::bind(CGF, ov: S.getOpaqueValue(), e: E); |
230 | auto UnbindCommonOnExit = |
231 | llvm::make_scope_exit(F: [&] { CommonBinder.unbind(CGF); }); |
232 | |
233 | auto Prefix = buildSuspendPrefixStr(Coro, Kind); |
234 | BasicBlock *ReadyBlock = CGF.createBasicBlock(name: Prefix + Twine(".ready" )); |
235 | BasicBlock *SuspendBlock = CGF.createBasicBlock(name: Prefix + Twine(".suspend" )); |
236 | BasicBlock *CleanupBlock = CGF.createBasicBlock(name: Prefix + Twine(".cleanup" )); |
237 | |
238 | // If expression is ready, no need to suspend. |
239 | CGF.EmitBranchOnBoolExpr(Cond: S.getReadyExpr(), TrueBlock: ReadyBlock, FalseBlock: SuspendBlock, TrueCount: 0); |
240 | |
241 | // Otherwise, emit suspend logic. |
242 | CGF.EmitBlock(BB: SuspendBlock); |
243 | |
244 | auto &Builder = CGF.Builder; |
245 | llvm::Function *CoroSave = CGF.CGM.getIntrinsic(IID: llvm::Intrinsic::coro_save); |
246 | auto *NullPtr = llvm::ConstantPointerNull::get(T: CGF.CGM.Int8PtrTy); |
247 | auto *SaveCall = Builder.CreateCall(Callee: CoroSave, Args: {NullPtr}); |
248 | |
249 | auto SuspendWrapper = CodeGenFunction(CGF.CGM).generateAwaitSuspendWrapper( |
250 | CoroName: CGF.CurFn->getName(), SuspendPointName: Prefix, S); |
251 | |
252 | CGF.CurCoro.InSuspendBlock = true; |
253 | |
254 | assert(CGF.CurCoro.Data && CGF.CurCoro.Data->CoroBegin && |
255 | "expected to be called in coroutine context" ); |
256 | |
257 | SmallVector<llvm::Value *, 3> SuspendIntrinsicCallArgs; |
258 | SuspendIntrinsicCallArgs.push_back( |
259 | Elt: CGF.getOrCreateOpaqueLValueMapping(e: S.getOpaqueValue()).getPointer(CGF)); |
260 | |
261 | SuspendIntrinsicCallArgs.push_back(Elt: CGF.CurCoro.Data->CoroBegin); |
262 | SuspendIntrinsicCallArgs.push_back(Elt: SuspendWrapper); |
263 | |
264 | const auto SuspendReturnType = S.getSuspendReturnType(); |
265 | llvm::Intrinsic::ID AwaitSuspendIID; |
266 | |
267 | switch (SuspendReturnType) { |
268 | case CoroutineSuspendExpr::SuspendReturnType::SuspendVoid: |
269 | AwaitSuspendIID = llvm::Intrinsic::coro_await_suspend_void; |
270 | break; |
271 | case CoroutineSuspendExpr::SuspendReturnType::SuspendBool: |
272 | AwaitSuspendIID = llvm::Intrinsic::coro_await_suspend_bool; |
273 | break; |
274 | case CoroutineSuspendExpr::SuspendReturnType::SuspendHandle: |
275 | AwaitSuspendIID = llvm::Intrinsic::coro_await_suspend_handle; |
276 | break; |
277 | } |
278 | |
279 | llvm::Function *AwaitSuspendIntrinsic = CGF.CGM.getIntrinsic(IID: AwaitSuspendIID); |
280 | |
281 | // SuspendHandle might throw since it also resumes the returned handle. |
282 | const bool AwaitSuspendCanThrow = |
283 | SuspendReturnType == |
284 | CoroutineSuspendExpr::SuspendReturnType::SuspendHandle || |
285 | StmtCanThrow(S: S.getSuspendExpr()); |
286 | |
287 | llvm::CallBase *SuspendRet = nullptr; |
288 | // FIXME: add call attributes? |
289 | if (AwaitSuspendCanThrow) |
290 | SuspendRet = |
291 | CGF.EmitCallOrInvoke(Callee: AwaitSuspendIntrinsic, Args: SuspendIntrinsicCallArgs); |
292 | else |
293 | SuspendRet = CGF.EmitNounwindRuntimeCall(callee: AwaitSuspendIntrinsic, |
294 | args: SuspendIntrinsicCallArgs); |
295 | |
296 | assert(SuspendRet); |
297 | CGF.CurCoro.InSuspendBlock = false; |
298 | |
299 | switch (SuspendReturnType) { |
300 | case CoroutineSuspendExpr::SuspendReturnType::SuspendVoid: |
301 | assert(SuspendRet->getType()->isVoidTy()); |
302 | break; |
303 | case CoroutineSuspendExpr::SuspendReturnType::SuspendBool: { |
304 | assert(SuspendRet->getType()->isIntegerTy()); |
305 | |
306 | // Veto suspension if requested by bool returning await_suspend. |
307 | BasicBlock *RealSuspendBlock = |
308 | CGF.createBasicBlock(name: Prefix + Twine(".suspend.bool" )); |
309 | CGF.Builder.CreateCondBr(Cond: SuspendRet, True: RealSuspendBlock, False: ReadyBlock); |
310 | CGF.EmitBlock(BB: RealSuspendBlock); |
311 | break; |
312 | } |
313 | case CoroutineSuspendExpr::SuspendReturnType::SuspendHandle: { |
314 | assert(SuspendRet->getType()->isVoidTy()); |
315 | break; |
316 | } |
317 | } |
318 | |
319 | // Emit the suspend point. |
320 | const bool IsFinalSuspend = (Kind == AwaitKind::Final); |
321 | llvm::Function *CoroSuspend = |
322 | CGF.CGM.getIntrinsic(IID: llvm::Intrinsic::coro_suspend); |
323 | auto *SuspendResult = Builder.CreateCall( |
324 | Callee: CoroSuspend, Args: {SaveCall, Builder.getInt1(V: IsFinalSuspend)}); |
325 | |
326 | // Create a switch capturing three possible continuations. |
327 | auto *Switch = Builder.CreateSwitch(V: SuspendResult, Dest: Coro.SuspendBB, NumCases: 2); |
328 | Switch->addCase(OnVal: Builder.getInt8(C: 0), Dest: ReadyBlock); |
329 | Switch->addCase(OnVal: Builder.getInt8(C: 1), Dest: CleanupBlock); |
330 | |
331 | // Emit cleanup for this suspend point. |
332 | CGF.EmitBlock(BB: CleanupBlock); |
333 | CGF.EmitBranchThroughCleanup(Dest: Coro.CleanupJD); |
334 | |
335 | // Emit await_resume expression. |
336 | CGF.EmitBlock(BB: ReadyBlock); |
337 | |
338 | // Exception handling requires additional IR. If the 'await_resume' function |
339 | // is marked as 'noexcept', we avoid generating this additional IR. |
340 | CXXTryStmt *TryStmt = nullptr; |
341 | if (Coro.ExceptionHandler && Kind == AwaitKind::Init && |
342 | StmtCanThrow(S: S.getResumeExpr())) { |
343 | Coro.ResumeEHVar = |
344 | CGF.CreateTempAlloca(Ty: Builder.getInt1Ty(), Name: Prefix + Twine("resume.eh" )); |
345 | Builder.CreateFlagStore(Value: true, Addr: Coro.ResumeEHVar); |
346 | |
347 | auto Loc = S.getResumeExpr()->getExprLoc(); |
348 | auto *Catch = new (CGF.getContext()) |
349 | CXXCatchStmt(Loc, /*exDecl=*/nullptr, Coro.ExceptionHandler); |
350 | auto *TryBody = CompoundStmt::Create(C: CGF.getContext(), Stmts: S.getResumeExpr(), |
351 | FPFeatures: FPOptionsOverride(), LB: Loc, RB: Loc); |
352 | TryStmt = CXXTryStmt::Create(C: CGF.getContext(), tryLoc: Loc, tryBlock: TryBody, handlers: Catch); |
353 | CGF.EnterCXXTryStmt(S: *TryStmt); |
354 | CGF.EmitStmt(S: TryBody); |
355 | // We don't use EmitCXXTryStmt here. We need to store to ResumeEHVar that |
356 | // doesn't exist in the body. |
357 | Builder.CreateFlagStore(Value: false, Addr: Coro.ResumeEHVar); |
358 | CGF.ExitCXXTryStmt(S: *TryStmt); |
359 | LValueOrRValue Res; |
360 | // We are not supposed to obtain the value from init suspend await_resume(). |
361 | Res.RV = RValue::getIgnored(); |
362 | return Res; |
363 | } |
364 | |
365 | LValueOrRValue Res; |
366 | if (forLValue) |
367 | Res.LV = CGF.EmitLValue(E: S.getResumeExpr()); |
368 | else |
369 | Res.RV = CGF.EmitAnyExpr(E: S.getResumeExpr(), aggSlot, ignoreResult); |
370 | |
371 | return Res; |
372 | } |
373 | |
374 | RValue CodeGenFunction::EmitCoawaitExpr(const CoawaitExpr &E, |
375 | AggValueSlot aggSlot, |
376 | bool ignoreResult) { |
377 | return emitSuspendExpression(CGF&: *this, Coro&: *CurCoro.Data, S: E, |
378 | Kind: CurCoro.Data->CurrentAwaitKind, aggSlot, |
379 | ignoreResult, /*forLValue*/false).RV; |
380 | } |
381 | RValue CodeGenFunction::EmitCoyieldExpr(const CoyieldExpr &E, |
382 | AggValueSlot aggSlot, |
383 | bool ignoreResult) { |
384 | return emitSuspendExpression(CGF&: *this, Coro&: *CurCoro.Data, S: E, Kind: AwaitKind::Yield, |
385 | aggSlot, ignoreResult, /*forLValue*/false).RV; |
386 | } |
387 | |
388 | void CodeGenFunction::EmitCoreturnStmt(CoreturnStmt const &S) { |
389 | ++CurCoro.Data->CoreturnCount; |
390 | const Expr *RV = S.getOperand(); |
391 | if (RV && RV->getType()->isVoidType() && !isa<InitListExpr>(Val: RV)) { |
392 | // Make sure to evaluate the non initlist expression of a co_return |
393 | // with a void expression for side effects. |
394 | RunCleanupsScope cleanupScope(*this); |
395 | EmitIgnoredExpr(E: RV); |
396 | } |
397 | EmitStmt(S: S.getPromiseCall()); |
398 | EmitBranchThroughCleanup(Dest: CurCoro.Data->FinalJD); |
399 | } |
400 | |
401 | |
402 | #ifndef NDEBUG |
403 | static QualType getCoroutineSuspendExprReturnType(const ASTContext &Ctx, |
404 | const CoroutineSuspendExpr *E) { |
405 | const auto *RE = E->getResumeExpr(); |
406 | // Is it possible for RE to be a CXXBindTemporaryExpr wrapping |
407 | // a MemberCallExpr? |
408 | assert(isa<CallExpr>(RE) && "unexpected suspend expression type" ); |
409 | return cast<CallExpr>(RE)->getCallReturnType(Ctx); |
410 | } |
411 | #endif |
412 | |
413 | llvm::Function * |
414 | CodeGenFunction::generateAwaitSuspendWrapper(Twine const &CoroName, |
415 | Twine const &SuspendPointName, |
416 | CoroutineSuspendExpr const &S) { |
417 | std::string FuncName = |
418 | (CoroName + ".__await_suspend_wrapper__" + SuspendPointName).str(); |
419 | |
420 | ASTContext &C = getContext(); |
421 | |
422 | FunctionArgList args; |
423 | |
424 | ImplicitParamDecl AwaiterDecl(C, C.VoidPtrTy, ImplicitParamKind::Other); |
425 | ImplicitParamDecl FrameDecl(C, C.VoidPtrTy, ImplicitParamKind::Other); |
426 | QualType ReturnTy = S.getSuspendExpr()->getType(); |
427 | |
428 | args.push_back(Elt: &AwaiterDecl); |
429 | args.push_back(Elt: &FrameDecl); |
430 | |
431 | const CGFunctionInfo &FI = |
432 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(resultType: ReturnTy, args); |
433 | |
434 | llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(Info: FI); |
435 | |
436 | llvm::Function *Fn = llvm::Function::Create( |
437 | Ty: LTy, Linkage: llvm::GlobalValue::PrivateLinkage, N: FuncName, M: &CGM.getModule()); |
438 | |
439 | Fn->addParamAttr(ArgNo: 0, Kind: llvm::Attribute::AttrKind::NonNull); |
440 | Fn->addParamAttr(ArgNo: 0, Kind: llvm::Attribute::AttrKind::NoUndef); |
441 | |
442 | Fn->addParamAttr(ArgNo: 1, Kind: llvm::Attribute::AttrKind::NoUndef); |
443 | |
444 | Fn->setMustProgress(); |
445 | Fn->addFnAttr(Kind: llvm::Attribute::AttrKind::AlwaysInline); |
446 | |
447 | StartFunction(GD: GlobalDecl(), RetTy: ReturnTy, Fn, FnInfo: FI, Args: args); |
448 | |
449 | // FIXME: add TBAA metadata to the loads |
450 | llvm::Value *AwaiterPtr = Builder.CreateLoad(Addr: GetAddrOfLocalVar(VD: &AwaiterDecl)); |
451 | auto AwaiterLValue = |
452 | MakeNaturalAlignAddrLValue(V: AwaiterPtr, T: AwaiterDecl.getType()); |
453 | |
454 | CurAwaitSuspendWrapper.FramePtr = |
455 | Builder.CreateLoad(Addr: GetAddrOfLocalVar(VD: &FrameDecl)); |
456 | |
457 | auto AwaiterBinder = CodeGenFunction::OpaqueValueMappingData::bind( |
458 | CGF&: *this, ov: S.getOpaqueValue(), lv: AwaiterLValue); |
459 | |
460 | auto *SuspendRet = EmitScalarExpr(E: S.getSuspendExpr()); |
461 | |
462 | auto UnbindCommonOnExit = |
463 | llvm::make_scope_exit(F: [&] { AwaiterBinder.unbind(CGF&: *this); }); |
464 | if (SuspendRet != nullptr) { |
465 | Fn->addRetAttr(Kind: llvm::Attribute::AttrKind::NoUndef); |
466 | Builder.CreateStore(Val: SuspendRet, Addr: ReturnValue); |
467 | } |
468 | |
469 | CurAwaitSuspendWrapper.FramePtr = nullptr; |
470 | FinishFunction(); |
471 | return Fn; |
472 | } |
473 | |
474 | LValue |
475 | CodeGenFunction::EmitCoawaitLValue(const CoawaitExpr *E) { |
476 | assert(getCoroutineSuspendExprReturnType(getContext(), E)->isReferenceType() && |
477 | "Can't have a scalar return unless the return type is a " |
478 | "reference type!" ); |
479 | return emitSuspendExpression(CGF&: *this, Coro&: *CurCoro.Data, S: *E, |
480 | Kind: CurCoro.Data->CurrentAwaitKind, aggSlot: AggValueSlot::ignored(), |
481 | /*ignoreResult*/false, /*forLValue*/true).LV; |
482 | } |
483 | |
484 | LValue |
485 | CodeGenFunction::EmitCoyieldLValue(const CoyieldExpr *E) { |
486 | assert(getCoroutineSuspendExprReturnType(getContext(), E)->isReferenceType() && |
487 | "Can't have a scalar return unless the return type is a " |
488 | "reference type!" ); |
489 | return emitSuspendExpression(CGF&: *this, Coro&: *CurCoro.Data, S: *E, |
490 | Kind: AwaitKind::Yield, aggSlot: AggValueSlot::ignored(), |
491 | /*ignoreResult*/false, /*forLValue*/true).LV; |
492 | } |
493 | |
494 | // Hunts for the parameter reference in the parameter copy/move declaration. |
495 | namespace { |
496 | struct GetParamRef : public StmtVisitor<GetParamRef> { |
497 | public: |
498 | DeclRefExpr *Expr = nullptr; |
499 | GetParamRef() {} |
500 | void VisitDeclRefExpr(DeclRefExpr *E) { |
501 | assert(Expr == nullptr && "multilple declref in param move" ); |
502 | Expr = E; |
503 | } |
504 | void VisitStmt(Stmt *S) { |
505 | for (auto *C : S->children()) { |
506 | if (C) |
507 | Visit(S: C); |
508 | } |
509 | } |
510 | }; |
511 | } |
512 | |
513 | // This class replaces references to parameters to their copies by changing |
514 | // the addresses in CGF.LocalDeclMap and restoring back the original values in |
515 | // its destructor. |
516 | |
517 | namespace { |
518 | struct ParamReferenceReplacerRAII { |
519 | CodeGenFunction::DeclMapTy SavedLocals; |
520 | CodeGenFunction::DeclMapTy& LocalDeclMap; |
521 | |
522 | ParamReferenceReplacerRAII(CodeGenFunction::DeclMapTy &LocalDeclMap) |
523 | : LocalDeclMap(LocalDeclMap) {} |
524 | |
525 | void addCopy(DeclStmt const *PM) { |
526 | // Figure out what param it refers to. |
527 | |
528 | assert(PM->isSingleDecl()); |
529 | VarDecl const*VD = static_cast<VarDecl const*>(PM->getSingleDecl()); |
530 | Expr const *InitExpr = VD->getInit(); |
531 | GetParamRef Visitor; |
532 | Visitor.Visit(S: const_cast<Expr*>(InitExpr)); |
533 | assert(Visitor.Expr); |
534 | DeclRefExpr *DREOrig = Visitor.Expr; |
535 | auto *PD = DREOrig->getDecl(); |
536 | |
537 | auto it = LocalDeclMap.find(Val: PD); |
538 | assert(it != LocalDeclMap.end() && "parameter is not found" ); |
539 | SavedLocals.insert(KV: { PD, it->second }); |
540 | |
541 | auto copyIt = LocalDeclMap.find(Val: VD); |
542 | assert(copyIt != LocalDeclMap.end() && "parameter copy is not found" ); |
543 | it->second = copyIt->getSecond(); |
544 | } |
545 | |
546 | ~ParamReferenceReplacerRAII() { |
547 | for (auto&& SavedLocal : SavedLocals) { |
548 | LocalDeclMap.insert(KV: {SavedLocal.first, SavedLocal.second}); |
549 | } |
550 | } |
551 | }; |
552 | } |
553 | |
554 | // For WinEH exception representation backend needs to know what funclet coro.end |
555 | // belongs to. That information is passed in a funclet bundle. |
556 | static SmallVector<llvm::OperandBundleDef, 1> |
557 | getBundlesForCoroEnd(CodeGenFunction &CGF) { |
558 | SmallVector<llvm::OperandBundleDef, 1> BundleList; |
559 | |
560 | if (llvm::Instruction *EHPad = CGF.CurrentFuncletPad) |
561 | BundleList.emplace_back(Args: "funclet" , Args&: EHPad); |
562 | |
563 | return BundleList; |
564 | } |
565 | |
566 | namespace { |
567 | // We will insert coro.end to cut any of the destructors for objects that |
568 | // do not need to be destroyed once the coroutine is resumed. |
569 | // See llvm/docs/Coroutines.rst for more details about coro.end. |
570 | struct CallCoroEnd final : public EHScopeStack::Cleanup { |
571 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
572 | auto &CGM = CGF.CGM; |
573 | auto *NullPtr = llvm::ConstantPointerNull::get(T: CGF.Int8PtrTy); |
574 | llvm::Function *CoroEndFn = CGM.getIntrinsic(IID: llvm::Intrinsic::coro_end); |
575 | // See if we have a funclet bundle to associate coro.end with. (WinEH) |
576 | auto Bundles = getBundlesForCoroEnd(CGF); |
577 | auto *CoroEnd = |
578 | CGF.Builder.CreateCall(Callee: CoroEndFn, |
579 | Args: {NullPtr, CGF.Builder.getTrue(), |
580 | llvm::ConstantTokenNone::get(Context&: CoroEndFn->getContext())}, |
581 | OpBundles: Bundles); |
582 | if (Bundles.empty()) { |
583 | // Otherwise, (landingpad model), create a conditional branch that leads |
584 | // either to a cleanup block or a block with EH resume instruction. |
585 | auto *ResumeBB = CGF.getEHResumeBlock(/*isCleanup=*/true); |
586 | auto *CleanupContBB = CGF.createBasicBlock(name: "cleanup.cont" ); |
587 | CGF.Builder.CreateCondBr(Cond: CoroEnd, True: ResumeBB, False: CleanupContBB); |
588 | CGF.EmitBlock(BB: CleanupContBB); |
589 | } |
590 | } |
591 | }; |
592 | } |
593 | |
594 | namespace { |
595 | // Make sure to call coro.delete on scope exit. |
596 | struct CallCoroDelete final : public EHScopeStack::Cleanup { |
597 | Stmt *Deallocate; |
598 | |
599 | // Emit "if (coro.free(CoroId, CoroBegin)) Deallocate;" |
600 | |
601 | // Note: That deallocation will be emitted twice: once for a normal exit and |
602 | // once for exceptional exit. This usage is safe because Deallocate does not |
603 | // contain any declarations. The SubStmtBuilder::makeNewAndDeleteExpr() |
604 | // builds a single call to a deallocation function which is safe to emit |
605 | // multiple times. |
606 | void Emit(CodeGenFunction &CGF, Flags) override { |
607 | // Remember the current point, as we are going to emit deallocation code |
608 | // first to get to coro.free instruction that is an argument to a delete |
609 | // call. |
610 | BasicBlock *SaveInsertBlock = CGF.Builder.GetInsertBlock(); |
611 | |
612 | auto *FreeBB = CGF.createBasicBlock(name: "coro.free" ); |
613 | CGF.EmitBlock(BB: FreeBB); |
614 | CGF.EmitStmt(S: Deallocate); |
615 | |
616 | auto *AfterFreeBB = CGF.createBasicBlock(name: "after.coro.free" ); |
617 | CGF.EmitBlock(BB: AfterFreeBB); |
618 | |
619 | // We should have captured coro.free from the emission of deallocate. |
620 | auto *CoroFree = CGF.CurCoro.Data->LastCoroFree; |
621 | if (!CoroFree) { |
622 | CGF.CGM.Error(loc: Deallocate->getBeginLoc(), |
623 | error: "Deallocation expressoin does not refer to coro.free" ); |
624 | return; |
625 | } |
626 | |
627 | // Get back to the block we were originally and move coro.free there. |
628 | auto *InsertPt = SaveInsertBlock->getTerminator(); |
629 | CoroFree->moveBefore(MovePos: InsertPt); |
630 | CGF.Builder.SetInsertPoint(InsertPt); |
631 | |
632 | // Add if (auto *mem = coro.free) Deallocate; |
633 | auto *NullPtr = llvm::ConstantPointerNull::get(T: CGF.Int8PtrTy); |
634 | auto *Cond = CGF.Builder.CreateICmpNE(LHS: CoroFree, RHS: NullPtr); |
635 | CGF.Builder.CreateCondBr(Cond, True: FreeBB, False: AfterFreeBB); |
636 | |
637 | // No longer need old terminator. |
638 | InsertPt->eraseFromParent(); |
639 | CGF.Builder.SetInsertPoint(AfterFreeBB); |
640 | } |
641 | explicit CallCoroDelete(Stmt *DeallocStmt) : Deallocate(DeallocStmt) {} |
642 | }; |
643 | } |
644 | |
645 | namespace { |
646 | struct GetReturnObjectManager { |
647 | CodeGenFunction &CGF; |
648 | CGBuilderTy &Builder; |
649 | const CoroutineBodyStmt &S; |
650 | // When true, performs RVO for the return object. |
651 | bool DirectEmit = false; |
652 | |
653 | Address GroActiveFlag; |
654 | CodeGenFunction::AutoVarEmission GroEmission; |
655 | |
656 | GetReturnObjectManager(CodeGenFunction &CGF, const CoroutineBodyStmt &S) |
657 | : CGF(CGF), Builder(CGF.Builder), S(S), GroActiveFlag(Address::invalid()), |
658 | GroEmission(CodeGenFunction::AutoVarEmission::invalid()) { |
659 | // The call to get_Âreturn_Âobject is sequenced before the call to |
660 | // initial_Âsuspend and is invoked at most once, but there are caveats |
661 | // regarding on whether the prvalue result object may be initialized |
662 | // directly/eager or delayed, depending on the types involved. |
663 | // |
664 | // More info at https://github.com/cplusplus/papers/issues/1414 |
665 | // |
666 | // The general cases: |
667 | // 1. Same type of get_return_object and coroutine return type (direct |
668 | // emission): |
669 | // - Constructed in the return slot. |
670 | // 2. Different types (delayed emission): |
671 | // - Constructed temporary object prior to initial suspend initialized with |
672 | // a call to get_return_object() |
673 | // - When coroutine needs to to return to the caller and needs to construct |
674 | // return value for the coroutine it is initialized with expiring value of |
675 | // the temporary obtained above. |
676 | // |
677 | // Direct emission for void returning coroutines or GROs. |
678 | DirectEmit = [&]() { |
679 | auto *RVI = S.getReturnValueInit(); |
680 | assert(RVI && "expected RVI" ); |
681 | auto GroType = RVI->getType(); |
682 | return CGF.getContext().hasSameType(T1: GroType, T2: CGF.FnRetTy); |
683 | }(); |
684 | } |
685 | |
686 | // The gro variable has to outlive coroutine frame and coroutine promise, but, |
687 | // it can only be initialized after coroutine promise was created, thus, we |
688 | // split its emission in two parts. EmitGroAlloca emits an alloca and sets up |
689 | // cleanups. Later when coroutine promise is available we initialize the gro |
690 | // and sets the flag that the cleanup is now active. |
691 | void EmitGroAlloca() { |
692 | if (DirectEmit) |
693 | return; |
694 | |
695 | auto *GroDeclStmt = dyn_cast_or_null<DeclStmt>(Val: S.getResultDecl()); |
696 | if (!GroDeclStmt) { |
697 | // If get_return_object returns void, no need to do an alloca. |
698 | return; |
699 | } |
700 | |
701 | auto *GroVarDecl = cast<VarDecl>(Val: GroDeclStmt->getSingleDecl()); |
702 | |
703 | // Set GRO flag that it is not initialized yet |
704 | GroActiveFlag = CGF.CreateTempAlloca(Ty: Builder.getInt1Ty(), align: CharUnits::One(), |
705 | Name: "gro.active" ); |
706 | Builder.CreateStore(Val: Builder.getFalse(), Addr: GroActiveFlag); |
707 | |
708 | GroEmission = CGF.EmitAutoVarAlloca(var: *GroVarDecl); |
709 | auto *GroAlloca = dyn_cast_or_null<llvm::AllocaInst>( |
710 | Val: GroEmission.getOriginalAllocatedAddress().getPointer()); |
711 | assert(GroAlloca && "expected alloca to be emitted" ); |
712 | GroAlloca->setMetadata(KindID: llvm::LLVMContext::MD_coro_outside_frame, |
713 | Node: llvm::MDNode::get(Context&: CGF.CGM.getLLVMContext(), MDs: {})); |
714 | |
715 | // Remember the top of EHStack before emitting the cleanup. |
716 | auto old_top = CGF.EHStack.stable_begin(); |
717 | CGF.EmitAutoVarCleanups(emission: GroEmission); |
718 | auto top = CGF.EHStack.stable_begin(); |
719 | |
720 | // Make the cleanup conditional on gro.active |
721 | for (auto b = CGF.EHStack.find(sp: top), e = CGF.EHStack.find(sp: old_top); b != e; |
722 | b++) { |
723 | if (auto *Cleanup = dyn_cast<EHCleanupScope>(Val: &*b)) { |
724 | assert(!Cleanup->hasActiveFlag() && "cleanup already has active flag?" ); |
725 | Cleanup->setActiveFlag(GroActiveFlag); |
726 | Cleanup->setTestFlagInEHCleanup(); |
727 | Cleanup->setTestFlagInNormalCleanup(); |
728 | } |
729 | } |
730 | } |
731 | |
732 | void EmitGroInit() { |
733 | if (DirectEmit) { |
734 | // ReturnValue should be valid as long as the coroutine's return type |
735 | // is not void. The assertion could help us to reduce the check later. |
736 | assert(CGF.ReturnValue.isValid() == (bool)S.getReturnStmt()); |
737 | // Now we have the promise, initialize the GRO. |
738 | // We need to emit `get_return_object` first. According to: |
739 | // [dcl.fct.def.coroutine]p7 |
740 | // The call to get_return_Âobject is sequenced before the call to |
741 | // initial_suspend and is invoked at most once. |
742 | // |
743 | // So we couldn't emit return value when we emit return statment, |
744 | // otherwise the call to get_return_object wouldn't be in front |
745 | // of initial_suspend. |
746 | if (CGF.ReturnValue.isValid()) { |
747 | CGF.EmitAnyExprToMem(E: S.getReturnValue(), Location: CGF.ReturnValue, |
748 | Quals: S.getReturnValue()->getType().getQualifiers(), |
749 | /*IsInit*/ IsInitializer: true); |
750 | } |
751 | return; |
752 | } |
753 | |
754 | if (!GroActiveFlag.isValid()) { |
755 | // No Gro variable was allocated. Simply emit the call to |
756 | // get_return_object. |
757 | CGF.EmitStmt(S: S.getResultDecl()); |
758 | return; |
759 | } |
760 | |
761 | CGF.EmitAutoVarInit(emission: GroEmission); |
762 | Builder.CreateStore(Val: Builder.getTrue(), Addr: GroActiveFlag); |
763 | } |
764 | }; |
765 | } // namespace |
766 | |
767 | static void emitBodyAndFallthrough(CodeGenFunction &CGF, |
768 | const CoroutineBodyStmt &S, Stmt *Body) { |
769 | CGF.EmitStmt(S: Body); |
770 | const bool CanFallthrough = CGF.Builder.GetInsertBlock(); |
771 | if (CanFallthrough) |
772 | if (Stmt *OnFallthrough = S.getFallthroughHandler()) |
773 | CGF.EmitStmt(S: OnFallthrough); |
774 | } |
775 | |
776 | void CodeGenFunction::EmitCoroutineBody(const CoroutineBodyStmt &S) { |
777 | auto *NullPtr = llvm::ConstantPointerNull::get(T: Builder.getPtrTy()); |
778 | auto &TI = CGM.getContext().getTargetInfo(); |
779 | unsigned NewAlign = TI.getNewAlign() / TI.getCharWidth(); |
780 | |
781 | auto *EntryBB = Builder.GetInsertBlock(); |
782 | auto *AllocBB = createBasicBlock(name: "coro.alloc" ); |
783 | auto *InitBB = createBasicBlock(name: "coro.init" ); |
784 | auto *FinalBB = createBasicBlock(name: "coro.final" ); |
785 | auto *RetBB = createBasicBlock(name: "coro.ret" ); |
786 | |
787 | auto *CoroId = Builder.CreateCall( |
788 | Callee: CGM.getIntrinsic(IID: llvm::Intrinsic::coro_id), |
789 | Args: {Builder.getInt32(C: NewAlign), NullPtr, NullPtr, NullPtr}); |
790 | createCoroData(CGF&: *this, CurCoro, CoroId); |
791 | CurCoro.Data->SuspendBB = RetBB; |
792 | assert(ShouldEmitLifetimeMarkers && |
793 | "Must emit lifetime intrinsics for coroutines" ); |
794 | |
795 | // Backend is allowed to elide memory allocations, to help it, emit |
796 | // auto mem = coro.alloc() ? 0 : ... allocation code ...; |
797 | auto *CoroAlloc = Builder.CreateCall( |
798 | Callee: CGM.getIntrinsic(IID: llvm::Intrinsic::coro_alloc), Args: {CoroId}); |
799 | |
800 | Builder.CreateCondBr(Cond: CoroAlloc, True: AllocBB, False: InitBB); |
801 | |
802 | EmitBlock(BB: AllocBB); |
803 | auto *AllocateCall = EmitScalarExpr(E: S.getAllocate()); |
804 | auto *AllocOrInvokeContBB = Builder.GetInsertBlock(); |
805 | |
806 | // Handle allocation failure if 'ReturnStmtOnAllocFailure' was provided. |
807 | if (auto *RetOnAllocFailure = S.getReturnStmtOnAllocFailure()) { |
808 | auto *RetOnFailureBB = createBasicBlock(name: "coro.ret.on.failure" ); |
809 | |
810 | // See if allocation was successful. |
811 | auto *NullPtr = llvm::ConstantPointerNull::get(T: Int8PtrTy); |
812 | auto *Cond = Builder.CreateICmpNE(LHS: AllocateCall, RHS: NullPtr); |
813 | // Expect the allocation to be successful. |
814 | emitCondLikelihoodViaExpectIntrinsic(Cond, LH: Stmt::LH_Likely); |
815 | Builder.CreateCondBr(Cond, True: InitBB, False: RetOnFailureBB); |
816 | |
817 | // If not, return OnAllocFailure object. |
818 | EmitBlock(BB: RetOnFailureBB); |
819 | EmitStmt(S: RetOnAllocFailure); |
820 | } |
821 | else { |
822 | Builder.CreateBr(Dest: InitBB); |
823 | } |
824 | |
825 | EmitBlock(BB: InitBB); |
826 | |
827 | // Pass the result of the allocation to coro.begin. |
828 | auto *Phi = Builder.CreatePHI(Ty: VoidPtrTy, NumReservedValues: 2); |
829 | Phi->addIncoming(V: NullPtr, BB: EntryBB); |
830 | Phi->addIncoming(V: AllocateCall, BB: AllocOrInvokeContBB); |
831 | auto *CoroBegin = Builder.CreateCall( |
832 | Callee: CGM.getIntrinsic(IID: llvm::Intrinsic::coro_begin), Args: {CoroId, Phi}); |
833 | CurCoro.Data->CoroBegin = CoroBegin; |
834 | |
835 | GetReturnObjectManager GroManager(*this, S); |
836 | GroManager.EmitGroAlloca(); |
837 | |
838 | CurCoro.Data->CleanupJD = getJumpDestInCurrentScope(Target: RetBB); |
839 | { |
840 | CGDebugInfo *DI = getDebugInfo(); |
841 | ParamReferenceReplacerRAII ParamReplacer(LocalDeclMap); |
842 | CodeGenFunction::RunCleanupsScope ResumeScope(*this); |
843 | EHStack.pushCleanup<CallCoroDelete>(Kind: NormalAndEHCleanup, A: S.getDeallocate()); |
844 | |
845 | // Create mapping between parameters and copy-params for coroutine function. |
846 | llvm::ArrayRef<const Stmt *> ParamMoves = S.getParamMoves(); |
847 | assert( |
848 | (ParamMoves.size() == 0 || (ParamMoves.size() == FnArgs.size())) && |
849 | "ParamMoves and FnArgs should be the same size for coroutine function" ); |
850 | if (ParamMoves.size() == FnArgs.size() && DI) |
851 | for (const auto Pair : llvm::zip(t&: FnArgs, u&: ParamMoves)) |
852 | DI->getCoroutineParameterMappings().insert( |
853 | KV: {std::get<0>(t: Pair), std::get<1>(t: Pair)}); |
854 | |
855 | // Create parameter copies. We do it before creating a promise, since an |
856 | // evolution of coroutine TS may allow promise constructor to observe |
857 | // parameter copies. |
858 | for (auto *PM : S.getParamMoves()) { |
859 | EmitStmt(S: PM); |
860 | ParamReplacer.addCopy(PM: cast<DeclStmt>(Val: PM)); |
861 | // TODO: if(CoroParam(...)) need to surround ctor and dtor |
862 | // for the copy, so that llvm can elide it if the copy is |
863 | // not needed. |
864 | } |
865 | |
866 | EmitStmt(S: S.getPromiseDeclStmt()); |
867 | |
868 | Address PromiseAddr = GetAddrOfLocalVar(VD: S.getPromiseDecl()); |
869 | auto *PromiseAddrVoidPtr = new llvm::BitCastInst( |
870 | PromiseAddr.emitRawPointer(CGF&: *this), VoidPtrTy, "" , CoroId); |
871 | // Update CoroId to refer to the promise. We could not do it earlier because |
872 | // promise local variable was not emitted yet. |
873 | CoroId->setArgOperand(i: 1, v: PromiseAddrVoidPtr); |
874 | |
875 | // Now we have the promise, initialize the GRO |
876 | GroManager.EmitGroInit(); |
877 | |
878 | EHStack.pushCleanup<CallCoroEnd>(Kind: EHCleanup); |
879 | |
880 | CurCoro.Data->CurrentAwaitKind = AwaitKind::Init; |
881 | CurCoro.Data->ExceptionHandler = S.getExceptionHandler(); |
882 | EmitStmt(S: S.getInitSuspendStmt()); |
883 | CurCoro.Data->FinalJD = getJumpDestInCurrentScope(Target: FinalBB); |
884 | |
885 | CurCoro.Data->CurrentAwaitKind = AwaitKind::Normal; |
886 | |
887 | if (CurCoro.Data->ExceptionHandler) { |
888 | // If we generated IR to record whether an exception was thrown from |
889 | // 'await_resume', then use that IR to determine whether the coroutine |
890 | // body should be skipped. |
891 | // If we didn't generate the IR (perhaps because 'await_resume' was marked |
892 | // as 'noexcept'), then we skip this check. |
893 | BasicBlock *ContBB = nullptr; |
894 | if (CurCoro.Data->ResumeEHVar) { |
895 | BasicBlock *BodyBB = createBasicBlock(name: "coro.resumed.body" ); |
896 | ContBB = createBasicBlock(name: "coro.resumed.cont" ); |
897 | Value *SkipBody = Builder.CreateFlagLoad(Addr: CurCoro.Data->ResumeEHVar, |
898 | Name: "coro.resumed.eh" ); |
899 | Builder.CreateCondBr(Cond: SkipBody, True: ContBB, False: BodyBB); |
900 | EmitBlock(BB: BodyBB); |
901 | } |
902 | |
903 | auto Loc = S.getBeginLoc(); |
904 | CXXCatchStmt Catch(Loc, /*exDecl=*/nullptr, |
905 | CurCoro.Data->ExceptionHandler); |
906 | auto *TryStmt = |
907 | CXXTryStmt::Create(C: getContext(), tryLoc: Loc, tryBlock: S.getBody(), handlers: &Catch); |
908 | |
909 | EnterCXXTryStmt(S: *TryStmt); |
910 | emitBodyAndFallthrough(CGF&: *this, S, Body: TryStmt->getTryBlock()); |
911 | ExitCXXTryStmt(S: *TryStmt); |
912 | |
913 | if (ContBB) |
914 | EmitBlock(BB: ContBB); |
915 | } |
916 | else { |
917 | emitBodyAndFallthrough(CGF&: *this, S, Body: S.getBody()); |
918 | } |
919 | |
920 | // See if we need to generate final suspend. |
921 | const bool CanFallthrough = Builder.GetInsertBlock(); |
922 | const bool HasCoreturns = CurCoro.Data->CoreturnCount > 0; |
923 | if (CanFallthrough || HasCoreturns) { |
924 | EmitBlock(BB: FinalBB); |
925 | CurCoro.Data->CurrentAwaitKind = AwaitKind::Final; |
926 | EmitStmt(S: S.getFinalSuspendStmt()); |
927 | } else { |
928 | // We don't need FinalBB. Emit it to make sure the block is deleted. |
929 | EmitBlock(BB: FinalBB, /*IsFinished=*/true); |
930 | } |
931 | } |
932 | |
933 | EmitBlock(BB: RetBB); |
934 | // Emit coro.end before getReturnStmt (and parameter destructors), since |
935 | // resume and destroy parts of the coroutine should not include them. |
936 | llvm::Function *CoroEnd = CGM.getIntrinsic(IID: llvm::Intrinsic::coro_end); |
937 | Builder.CreateCall(Callee: CoroEnd, |
938 | Args: {NullPtr, Builder.getFalse(), |
939 | llvm::ConstantTokenNone::get(Context&: CoroEnd->getContext())}); |
940 | |
941 | if (Stmt *Ret = S.getReturnStmt()) { |
942 | // Since we already emitted the return value above, so we shouldn't |
943 | // emit it again here. |
944 | if (GroManager.DirectEmit) |
945 | cast<ReturnStmt>(Val: Ret)->setRetValue(nullptr); |
946 | EmitStmt(S: Ret); |
947 | } |
948 | |
949 | // LLVM require the frontend to mark the coroutine. |
950 | CurFn->setPresplitCoroutine(); |
951 | |
952 | if (CXXRecordDecl *RD = FnRetTy->getAsCXXRecordDecl(); |
953 | RD && RD->hasAttr<CoroOnlyDestroyWhenCompleteAttr>()) |
954 | CurFn->setCoroDestroyOnlyWhenComplete(); |
955 | } |
956 | |
957 | // Emit coroutine intrinsic and patch up arguments of the token type. |
958 | RValue CodeGenFunction::EmitCoroutineIntrinsic(const CallExpr *E, |
959 | unsigned int IID) { |
960 | SmallVector<llvm::Value *, 8> Args; |
961 | switch (IID) { |
962 | default: |
963 | break; |
964 | // The coro.frame builtin is replaced with an SSA value of the coro.begin |
965 | // intrinsic. |
966 | case llvm::Intrinsic::coro_frame: { |
967 | if (CurCoro.Data && CurCoro.Data->CoroBegin) { |
968 | return RValue::get(V: CurCoro.Data->CoroBegin); |
969 | } |
970 | |
971 | if (CurAwaitSuspendWrapper.FramePtr) { |
972 | return RValue::get(V: CurAwaitSuspendWrapper.FramePtr); |
973 | } |
974 | |
975 | CGM.Error(loc: E->getBeginLoc(), error: "this builtin expect that __builtin_coro_begin " |
976 | "has been used earlier in this function" ); |
977 | auto *NullPtr = llvm::ConstantPointerNull::get(T: Builder.getPtrTy()); |
978 | return RValue::get(V: NullPtr); |
979 | } |
980 | case llvm::Intrinsic::coro_size: { |
981 | auto &Context = getContext(); |
982 | CanQualType SizeTy = Context.getSizeType(); |
983 | llvm::IntegerType *T = Builder.getIntNTy(N: Context.getTypeSize(T: SizeTy)); |
984 | llvm::Function *F = CGM.getIntrinsic(IID: llvm::Intrinsic::coro_size, Tys: T); |
985 | return RValue::get(V: Builder.CreateCall(Callee: F)); |
986 | } |
987 | case llvm::Intrinsic::coro_align: { |
988 | auto &Context = getContext(); |
989 | CanQualType SizeTy = Context.getSizeType(); |
990 | llvm::IntegerType *T = Builder.getIntNTy(N: Context.getTypeSize(T: SizeTy)); |
991 | llvm::Function *F = CGM.getIntrinsic(IID: llvm::Intrinsic::coro_align, Tys: T); |
992 | return RValue::get(V: Builder.CreateCall(Callee: F)); |
993 | } |
994 | // The following three intrinsics take a token parameter referring to a token |
995 | // returned by earlier call to @llvm.coro.id. Since we cannot represent it in |
996 | // builtins, we patch it up here. |
997 | case llvm::Intrinsic::coro_alloc: |
998 | case llvm::Intrinsic::coro_begin: |
999 | case llvm::Intrinsic::coro_free: { |
1000 | if (CurCoro.Data && CurCoro.Data->CoroId) { |
1001 | Args.push_back(Elt: CurCoro.Data->CoroId); |
1002 | break; |
1003 | } |
1004 | CGM.Error(loc: E->getBeginLoc(), error: "this builtin expect that __builtin_coro_id has" |
1005 | " been used earlier in this function" ); |
1006 | // Fallthrough to the next case to add TokenNone as the first argument. |
1007 | [[fallthrough]]; |
1008 | } |
1009 | // @llvm.coro.suspend takes a token parameter. Add token 'none' as the first |
1010 | // argument. |
1011 | case llvm::Intrinsic::coro_suspend: |
1012 | Args.push_back(Elt: llvm::ConstantTokenNone::get(Context&: getLLVMContext())); |
1013 | break; |
1014 | } |
1015 | for (const Expr *Arg : E->arguments()) |
1016 | Args.push_back(Elt: EmitScalarExpr(E: Arg)); |
1017 | // @llvm.coro.end takes a token parameter. Add token 'none' as the last |
1018 | // argument. |
1019 | if (IID == llvm::Intrinsic::coro_end) |
1020 | Args.push_back(Elt: llvm::ConstantTokenNone::get(Context&: getLLVMContext())); |
1021 | |
1022 | llvm::Function *F = CGM.getIntrinsic(IID); |
1023 | llvm::CallInst *Call = Builder.CreateCall(Callee: F, Args); |
1024 | |
1025 | // Note: The following code is to enable to emit coro.id and coro.begin by |
1026 | // hand to experiment with coroutines in C. |
1027 | // If we see @llvm.coro.id remember it in the CoroData. We will update |
1028 | // coro.alloc, coro.begin and coro.free intrinsics to refer to it. |
1029 | if (IID == llvm::Intrinsic::coro_id) { |
1030 | createCoroData(CGF&: *this, CurCoro, CoroId: Call, CoroIdExpr: E); |
1031 | } |
1032 | else if (IID == llvm::Intrinsic::coro_begin) { |
1033 | if (CurCoro.Data) |
1034 | CurCoro.Data->CoroBegin = Call; |
1035 | } |
1036 | else if (IID == llvm::Intrinsic::coro_free) { |
1037 | // Remember the last coro_free as we need it to build the conditional |
1038 | // deletion of the coroutine frame. |
1039 | if (CurCoro.Data) |
1040 | CurCoro.Data->LastCoroFree = Call; |
1041 | } |
1042 | return RValue::get(V: Call); |
1043 | } |
1044 | |