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
19using namespace clang;
20using namespace CodeGen;
21
22using llvm::Value;
23using llvm::BasicBlock;
24
25namespace {
26enum class AwaitKind { Init, Normal, Yield, Final };
27static constexpr llvm::StringLiteral AwaitKindStr[] = {"init", "await", "yield",
28 "final"};
29}
30
31struct 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.
86clang::CodeGen::CodeGenFunction::CGCoroInfo::CGCoroInfo() {}
87CodeGenFunction::CGCoroInfo::~CGCoroInfo() {}
88
89static 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::unique_ptr<CGCoroData>(new CGCoroData);
107 CurCoro.Data->CoroId = CoroId;
108 CurCoro.Data->CoroIdExpr = CoroIdExpr;
109}
110
111// Synthesize a pretty name for a suspend point.
112static 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).
134static 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(Proto->getExceptionSpecType()) ||
141 Proto->canThrow() != CT_Cannot;
142}
143
144static 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(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//
216namespace {
217 struct LValueOrRValue {
218 LValue LV;
219 RValue RV;
220 };
221}
222static 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(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 const auto AwaitSuspendCanThrow = StmtCanThrow(S.getSuspendExpr());
282
283 llvm::CallBase *SuspendRet = nullptr;
284 // FIXME: add call attributes?
285 if (AwaitSuspendCanThrow)
286 SuspendRet =
287 CGF.EmitCallOrInvoke(Callee: AwaitSuspendIntrinsic, Args: SuspendIntrinsicCallArgs);
288 else
289 SuspendRet = CGF.EmitNounwindRuntimeCall(callee: AwaitSuspendIntrinsic,
290 args: SuspendIntrinsicCallArgs);
291
292 assert(SuspendRet);
293 CGF.CurCoro.InSuspendBlock = false;
294
295 switch (SuspendReturnType) {
296 case CoroutineSuspendExpr::SuspendReturnType::SuspendVoid:
297 assert(SuspendRet->getType()->isVoidTy());
298 break;
299 case CoroutineSuspendExpr::SuspendReturnType::SuspendBool: {
300 assert(SuspendRet->getType()->isIntegerTy());
301
302 // Veto suspension if requested by bool returning await_suspend.
303 BasicBlock *RealSuspendBlock =
304 CGF.createBasicBlock(name: Prefix + Twine(".suspend.bool"));
305 CGF.Builder.CreateCondBr(Cond: SuspendRet, True: RealSuspendBlock, False: ReadyBlock);
306 CGF.EmitBlock(BB: RealSuspendBlock);
307 break;
308 }
309 case CoroutineSuspendExpr::SuspendReturnType::SuspendHandle: {
310 assert(SuspendRet->getType()->isPointerTy());
311
312 auto ResumeIntrinsic = CGF.CGM.getIntrinsic(llvm::Intrinsic::coro_resume);
313 Builder.CreateCall(ResumeIntrinsic, SuspendRet);
314 break;
315 }
316 }
317
318 // Emit the suspend point.
319 const bool IsFinalSuspend = (Kind == AwaitKind::Final);
320 llvm::Function *CoroSuspend =
321 CGF.CGM.getIntrinsic(llvm::Intrinsic::coro_suspend);
322 auto *SuspendResult = Builder.CreateCall(
323 Callee: CoroSuspend, Args: {SaveCall, Builder.getInt1(V: IsFinalSuspend)});
324
325 // Create a switch capturing three possible continuations.
326 auto *Switch = Builder.CreateSwitch(V: SuspendResult, Dest: Coro.SuspendBB, NumCases: 2);
327 Switch->addCase(OnVal: Builder.getInt8(C: 0), Dest: ReadyBlock);
328 Switch->addCase(OnVal: Builder.getInt8(C: 1), Dest: CleanupBlock);
329
330 // Emit cleanup for this suspend point.
331 CGF.EmitBlock(BB: CleanupBlock);
332 CGF.EmitBranchThroughCleanup(Dest: Coro.CleanupJD);
333
334 // Emit await_resume expression.
335 CGF.EmitBlock(BB: ReadyBlock);
336
337 // Exception handling requires additional IR. If the 'await_resume' function
338 // is marked as 'noexcept', we avoid generating this additional IR.
339 CXXTryStmt *TryStmt = nullptr;
340 if (Coro.ExceptionHandler && Kind == AwaitKind::Init &&
341 StmtCanThrow(S.getResumeExpr())) {
342 Coro.ResumeEHVar =
343 CGF.CreateTempAlloca(Ty: Builder.getInt1Ty(), Name: Prefix + Twine("resume.eh"));
344 Builder.CreateFlagStore(Value: true, Addr: Coro.ResumeEHVar);
345
346 auto Loc = S.getResumeExpr()->getExprLoc();
347 auto *Catch = new (CGF.getContext())
348 CXXCatchStmt(Loc, /*exDecl=*/nullptr, Coro.ExceptionHandler);
349 auto *TryBody = CompoundStmt::Create(CGF.getContext(), S.getResumeExpr(),
350 FPOptionsOverride(), Loc, Loc);
351 TryStmt = CXXTryStmt::Create(CGF.getContext(), Loc, TryBody, Catch);
352 CGF.EnterCXXTryStmt(S: *TryStmt);
353 CGF.EmitStmt(S: TryBody);
354 // We don't use EmitCXXTryStmt here. We need to store to ResumeEHVar that
355 // doesn't exist in the body.
356 Builder.CreateFlagStore(Value: false, Addr: Coro.ResumeEHVar);
357 CGF.ExitCXXTryStmt(S: *TryStmt);
358 LValueOrRValue Res;
359 // We are not supposed to obtain the value from init suspend await_resume().
360 Res.RV = RValue::getIgnored();
361 return Res;
362 }
363
364 LValueOrRValue Res;
365 if (forLValue)
366 Res.LV = CGF.EmitLValue(E: S.getResumeExpr());
367 else
368 Res.RV = CGF.EmitAnyExpr(E: S.getResumeExpr(), aggSlot, ignoreResult);
369
370 return Res;
371}
372
373RValue CodeGenFunction::EmitCoawaitExpr(const CoawaitExpr &E,
374 AggValueSlot aggSlot,
375 bool ignoreResult) {
376 return emitSuspendExpression(*this, *CurCoro.Data, E,
377 CurCoro.Data->CurrentAwaitKind, aggSlot,
378 ignoreResult, /*forLValue*/false).RV;
379}
380RValue CodeGenFunction::EmitCoyieldExpr(const CoyieldExpr &E,
381 AggValueSlot aggSlot,
382 bool ignoreResult) {
383 return emitSuspendExpression(*this, *CurCoro.Data, E, AwaitKind::Yield,
384 aggSlot, ignoreResult, /*forLValue*/false).RV;
385}
386
387void CodeGenFunction::EmitCoreturnStmt(CoreturnStmt const &S) {
388 ++CurCoro.Data->CoreturnCount;
389 const Expr *RV = S.getOperand();
390 if (RV && RV->getType()->isVoidType() && !isa<InitListExpr>(Val: RV)) {
391 // Make sure to evaluate the non initlist expression of a co_return
392 // with a void expression for side effects.
393 RunCleanupsScope cleanupScope(*this);
394 EmitIgnoredExpr(E: RV);
395 }
396 EmitStmt(S.getPromiseCall());
397 EmitBranchThroughCleanup(Dest: CurCoro.Data->FinalJD);
398}
399
400
401#ifndef NDEBUG
402static QualType getCoroutineSuspendExprReturnType(const ASTContext &Ctx,
403 const CoroutineSuspendExpr *E) {
404 const auto *RE = E->getResumeExpr();
405 // Is it possible for RE to be a CXXBindTemporaryExpr wrapping
406 // a MemberCallExpr?
407 assert(isa<CallExpr>(RE) && "unexpected suspend expression type");
408 return cast<CallExpr>(Val: RE)->getCallReturnType(Ctx);
409}
410#endif
411
412llvm::Function *
413CodeGenFunction::generateAwaitSuspendWrapper(Twine const &CoroName,
414 Twine const &SuspendPointName,
415 CoroutineSuspendExpr const &S) {
416 std::string FuncName =
417 (CoroName + ".__await_suspend_wrapper__" + SuspendPointName).str();
418
419 ASTContext &C = getContext();
420
421 FunctionArgList args;
422
423 ImplicitParamDecl AwaiterDecl(C, C.VoidPtrTy, ImplicitParamKind::Other);
424 ImplicitParamDecl FrameDecl(C, C.VoidPtrTy, ImplicitParamKind::Other);
425 QualType ReturnTy = S.getSuspendExpr()->getType();
426
427 args.push_back(&AwaiterDecl);
428 args.push_back(&FrameDecl);
429
430 const CGFunctionInfo &FI =
431 CGM.getTypes().arrangeBuiltinFunctionDeclaration(resultType: ReturnTy, args);
432
433 llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(Info: FI);
434
435 llvm::Function *Fn = llvm::Function::Create(
436 Ty: LTy, Linkage: llvm::GlobalValue::PrivateLinkage, N: FuncName, M: &CGM.getModule());
437
438 Fn->addParamAttr(0, llvm::Attribute::AttrKind::NonNull);
439 Fn->addParamAttr(0, llvm::Attribute::AttrKind::NoUndef);
440
441 Fn->addParamAttr(1, llvm::Attribute::AttrKind::NoUndef);
442
443 Fn->setMustProgress();
444 Fn->addFnAttr(llvm::Attribute::AttrKind::AlwaysInline);
445
446 StartFunction(GD: GlobalDecl(), RetTy: ReturnTy, Fn, FnInfo: FI, Args: args);
447
448 // FIXME: add TBAA metadata to the loads
449 llvm::Value *AwaiterPtr = Builder.CreateLoad(Addr: GetAddrOfLocalVar(&AwaiterDecl));
450 auto AwaiterLValue =
451 MakeNaturalAlignAddrLValue(V: AwaiterPtr, T: AwaiterDecl.getType());
452
453 CurAwaitSuspendWrapper.FramePtr =
454 Builder.CreateLoad(Addr: GetAddrOfLocalVar(&FrameDecl));
455
456 auto AwaiterBinder = CodeGenFunction::OpaqueValueMappingData::bind(
457 *this, S.getOpaqueValue(), AwaiterLValue);
458
459 auto *SuspendRet = EmitScalarExpr(E: S.getSuspendExpr());
460
461 auto UnbindCommonOnExit =
462 llvm::make_scope_exit(F: [&] { AwaiterBinder.unbind(*this); });
463 if (SuspendRet != nullptr) {
464 Fn->addRetAttr(llvm::Attribute::AttrKind::NoUndef);
465 Builder.CreateStore(Val: SuspendRet, Addr: ReturnValue);
466 }
467
468 CurAwaitSuspendWrapper.FramePtr = nullptr;
469 FinishFunction();
470 return Fn;
471}
472
473LValue
474CodeGenFunction::EmitCoawaitLValue(const CoawaitExpr *E) {
475 assert(getCoroutineSuspendExprReturnType(getContext(), E)->isReferenceType() &&
476 "Can't have a scalar return unless the return type is a "
477 "reference type!");
478 return emitSuspendExpression(*this, *CurCoro.Data, *E,
479 CurCoro.Data->CurrentAwaitKind, AggValueSlot::ignored(),
480 /*ignoreResult*/false, /*forLValue*/true).LV;
481}
482
483LValue
484CodeGenFunction::EmitCoyieldLValue(const CoyieldExpr *E) {
485 assert(getCoroutineSuspendExprReturnType(getContext(), E)->isReferenceType() &&
486 "Can't have a scalar return unless the return type is a "
487 "reference type!");
488 return emitSuspendExpression(*this, *CurCoro.Data, *E,
489 AwaitKind::Yield, AggValueSlot::ignored(),
490 /*ignoreResult*/false, /*forLValue*/true).LV;
491}
492
493// Hunts for the parameter reference in the parameter copy/move declaration.
494namespace {
495struct GetParamRef : public StmtVisitor<GetParamRef> {
496public:
497 DeclRefExpr *Expr = nullptr;
498 GetParamRef() {}
499 void VisitDeclRefExpr(DeclRefExpr *E) {
500 assert(Expr == nullptr && "multilple declref in param move");
501 Expr = E;
502 }
503 void VisitStmt(Stmt *S) {
504 for (auto *C : S->children()) {
505 if (C)
506 Visit(C);
507 }
508 }
509};
510}
511
512// This class replaces references to parameters to their copies by changing
513// the addresses in CGF.LocalDeclMap and restoring back the original values in
514// its destructor.
515
516namespace {
517 struct ParamReferenceReplacerRAII {
518 CodeGenFunction::DeclMapTy SavedLocals;
519 CodeGenFunction::DeclMapTy& LocalDeclMap;
520
521 ParamReferenceReplacerRAII(CodeGenFunction::DeclMapTy &LocalDeclMap)
522 : LocalDeclMap(LocalDeclMap) {}
523
524 void addCopy(DeclStmt const *PM) {
525 // Figure out what param it refers to.
526
527 assert(PM->isSingleDecl());
528 VarDecl const*VD = static_cast<VarDecl const*>(PM->getSingleDecl());
529 Expr const *InitExpr = VD->getInit();
530 GetParamRef Visitor;
531 Visitor.Visit(const_cast<Expr*>(InitExpr));
532 assert(Visitor.Expr);
533 DeclRefExpr *DREOrig = Visitor.Expr;
534 auto *PD = DREOrig->getDecl();
535
536 auto it = LocalDeclMap.find(PD);
537 assert(it != LocalDeclMap.end() && "parameter is not found");
538 SavedLocals.insert({ PD, it->second });
539
540 auto copyIt = LocalDeclMap.find(VD);
541 assert(copyIt != LocalDeclMap.end() && "parameter copy is not found");
542 it->second = copyIt->getSecond();
543 }
544
545 ~ParamReferenceReplacerRAII() {
546 for (auto&& SavedLocal : SavedLocals) {
547 LocalDeclMap.insert(KV: {SavedLocal.first, SavedLocal.second});
548 }
549 }
550 };
551}
552
553// For WinEH exception representation backend needs to know what funclet coro.end
554// belongs to. That information is passed in a funclet bundle.
555static SmallVector<llvm::OperandBundleDef, 1>
556getBundlesForCoroEnd(CodeGenFunction &CGF) {
557 SmallVector<llvm::OperandBundleDef, 1> BundleList;
558
559 if (llvm::Instruction *EHPad = CGF.CurrentFuncletPad)
560 BundleList.emplace_back(Args: "funclet", Args&: EHPad);
561
562 return BundleList;
563}
564
565namespace {
566// We will insert coro.end to cut any of the destructors for objects that
567// do not need to be destroyed once the coroutine is resumed.
568// See llvm/docs/Coroutines.rst for more details about coro.end.
569struct CallCoroEnd final : public EHScopeStack::Cleanup {
570 void Emit(CodeGenFunction &CGF, Flags flags) override {
571 auto &CGM = CGF.CGM;
572 auto *NullPtr = llvm::ConstantPointerNull::get(T: CGF.Int8PtrTy);
573 llvm::Function *CoroEndFn = CGM.getIntrinsic(llvm::Intrinsic::coro_end);
574 // See if we have a funclet bundle to associate coro.end with. (WinEH)
575 auto Bundles = getBundlesForCoroEnd(CGF);
576 auto *CoroEnd =
577 CGF.Builder.CreateCall(Callee: CoroEndFn,
578 Args: {NullPtr, CGF.Builder.getTrue(),
579 llvm::ConstantTokenNone::get(Context&: CoroEndFn->getContext())},
580 OpBundles: Bundles);
581 if (Bundles.empty()) {
582 // Otherwise, (landingpad model), create a conditional branch that leads
583 // either to a cleanup block or a block with EH resume instruction.
584 auto *ResumeBB = CGF.getEHResumeBlock(/*isCleanup=*/true);
585 auto *CleanupContBB = CGF.createBasicBlock(name: "cleanup.cont");
586 CGF.Builder.CreateCondBr(Cond: CoroEnd, True: ResumeBB, False: CleanupContBB);
587 CGF.EmitBlock(BB: CleanupContBB);
588 }
589 }
590};
591}
592
593namespace {
594// Make sure to call coro.delete on scope exit.
595struct CallCoroDelete final : public EHScopeStack::Cleanup {
596 Stmt *Deallocate;
597
598 // Emit "if (coro.free(CoroId, CoroBegin)) Deallocate;"
599
600 // Note: That deallocation will be emitted twice: once for a normal exit and
601 // once for exceptional exit. This usage is safe because Deallocate does not
602 // contain any declarations. The SubStmtBuilder::makeNewAndDeleteExpr()
603 // builds a single call to a deallocation function which is safe to emit
604 // multiple times.
605 void Emit(CodeGenFunction &CGF, Flags) override {
606 // Remember the current point, as we are going to emit deallocation code
607 // first to get to coro.free instruction that is an argument to a delete
608 // call.
609 BasicBlock *SaveInsertBlock = CGF.Builder.GetInsertBlock();
610
611 auto *FreeBB = CGF.createBasicBlock(name: "coro.free");
612 CGF.EmitBlock(BB: FreeBB);
613 CGF.EmitStmt(S: Deallocate);
614
615 auto *AfterFreeBB = CGF.createBasicBlock(name: "after.coro.free");
616 CGF.EmitBlock(BB: AfterFreeBB);
617
618 // We should have captured coro.free from the emission of deallocate.
619 auto *CoroFree = CGF.CurCoro.Data->LastCoroFree;
620 if (!CoroFree) {
621 CGF.CGM.Error(loc: Deallocate->getBeginLoc(),
622 error: "Deallocation expressoin does not refer to coro.free");
623 return;
624 }
625
626 // Get back to the block we were originally and move coro.free there.
627 auto *InsertPt = SaveInsertBlock->getTerminator();
628 CoroFree->moveBefore(MovePos: InsertPt);
629 CGF.Builder.SetInsertPoint(InsertPt);
630
631 // Add if (auto *mem = coro.free) Deallocate;
632 auto *NullPtr = llvm::ConstantPointerNull::get(T: CGF.Int8PtrTy);
633 auto *Cond = CGF.Builder.CreateICmpNE(LHS: CoroFree, RHS: NullPtr);
634 CGF.Builder.CreateCondBr(Cond, True: FreeBB, False: AfterFreeBB);
635
636 // No longer need old terminator.
637 InsertPt->eraseFromParent();
638 CGF.Builder.SetInsertPoint(AfterFreeBB);
639 }
640 explicit CallCoroDelete(Stmt *DeallocStmt) : Deallocate(DeallocStmt) {}
641};
642}
643
644namespace {
645struct GetReturnObjectManager {
646 CodeGenFunction &CGF;
647 CGBuilderTy &Builder;
648 const CoroutineBodyStmt &S;
649 // When true, performs RVO for the return object.
650 bool DirectEmit = false;
651
652 Address GroActiveFlag;
653 CodeGenFunction::AutoVarEmission GroEmission;
654
655 GetReturnObjectManager(CodeGenFunction &CGF, const CoroutineBodyStmt &S)
656 : CGF(CGF), Builder(CGF.Builder), S(S), GroActiveFlag(Address::invalid()),
657 GroEmission(CodeGenFunction::AutoVarEmission::invalid()) {
658 // The call to get_­return_­object is sequenced before the call to
659 // initial_­suspend and is invoked at most once, but there are caveats
660 // regarding on whether the prvalue result object may be initialized
661 // directly/eager or delayed, depending on the types involved.
662 //
663 // More info at https://github.com/cplusplus/papers/issues/1414
664 //
665 // The general cases:
666 // 1. Same type of get_return_object and coroutine return type (direct
667 // emission):
668 // - Constructed in the return slot.
669 // 2. Different types (delayed emission):
670 // - Constructed temporary object prior to initial suspend initialized with
671 // a call to get_return_object()
672 // - When coroutine needs to to return to the caller and needs to construct
673 // return value for the coroutine it is initialized with expiring value of
674 // the temporary obtained above.
675 //
676 // Direct emission for void returning coroutines or GROs.
677 DirectEmit = [&]() {
678 auto *RVI = S.getReturnValueInit();
679 assert(RVI && "expected RVI");
680 auto GroType = RVI->getType();
681 return CGF.getContext().hasSameType(GroType, CGF.FnRetTy);
682 }();
683 }
684
685 // The gro variable has to outlive coroutine frame and coroutine promise, but,
686 // it can only be initialized after coroutine promise was created, thus, we
687 // split its emission in two parts. EmitGroAlloca emits an alloca and sets up
688 // cleanups. Later when coroutine promise is available we initialize the gro
689 // and sets the flag that the cleanup is now active.
690 void EmitGroAlloca() {
691 if (DirectEmit)
692 return;
693
694 auto *GroDeclStmt = dyn_cast_or_null<DeclStmt>(Val: S.getResultDecl());
695 if (!GroDeclStmt) {
696 // If get_return_object returns void, no need to do an alloca.
697 return;
698 }
699
700 auto *GroVarDecl = cast<VarDecl>(Val: GroDeclStmt->getSingleDecl());
701
702 // Set GRO flag that it is not initialized yet
703 GroActiveFlag = CGF.CreateTempAlloca(Ty: Builder.getInt1Ty(), align: CharUnits::One(),
704 Name: "gro.active");
705 Builder.CreateStore(Val: Builder.getFalse(), Addr: GroActiveFlag);
706
707 GroEmission = CGF.EmitAutoVarAlloca(var: *GroVarDecl);
708 auto *GroAlloca = dyn_cast_or_null<llvm::AllocaInst>(
709 Val: GroEmission.getOriginalAllocatedAddress().getPointer());
710 assert(GroAlloca && "expected alloca to be emitted");
711 GroAlloca->setMetadata(KindID: llvm::LLVMContext::MD_coro_outside_frame,
712 Node: llvm::MDNode::get(Context&: CGF.CGM.getLLVMContext(), MDs: {}));
713
714 // Remember the top of EHStack before emitting the cleanup.
715 auto old_top = CGF.EHStack.stable_begin();
716 CGF.EmitAutoVarCleanups(emission: GroEmission);
717 auto top = CGF.EHStack.stable_begin();
718
719 // Make the cleanup conditional on gro.active
720 for (auto b = CGF.EHStack.find(sp: top), e = CGF.EHStack.find(sp: old_top); b != e;
721 b++) {
722 if (auto *Cleanup = dyn_cast<EHCleanupScope>(Val: &*b)) {
723 assert(!Cleanup->hasActiveFlag() && "cleanup already has active flag?");
724 Cleanup->setActiveFlag(GroActiveFlag);
725 Cleanup->setTestFlagInEHCleanup();
726 Cleanup->setTestFlagInNormalCleanup();
727 }
728 }
729 }
730
731 void EmitGroInit() {
732 if (DirectEmit) {
733 // ReturnValue should be valid as long as the coroutine's return type
734 // is not void. The assertion could help us to reduce the check later.
735 assert(CGF.ReturnValue.isValid() == (bool)S.getReturnStmt());
736 // Now we have the promise, initialize the GRO.
737 // We need to emit `get_return_object` first. According to:
738 // [dcl.fct.def.coroutine]p7
739 // The call to get_return_­object is sequenced before the call to
740 // initial_suspend and is invoked at most once.
741 //
742 // So we couldn't emit return value when we emit return statment,
743 // otherwise the call to get_return_object wouldn't be in front
744 // of initial_suspend.
745 if (CGF.ReturnValue.isValid()) {
746 CGF.EmitAnyExprToMem(E: S.getReturnValue(), Location: CGF.ReturnValue,
747 Quals: S.getReturnValue()->getType().getQualifiers(),
748 /*IsInit*/ IsInitializer: true);
749 }
750 return;
751 }
752
753 if (!GroActiveFlag.isValid()) {
754 // No Gro variable was allocated. Simply emit the call to
755 // get_return_object.
756 CGF.EmitStmt(S: S.getResultDecl());
757 return;
758 }
759
760 CGF.EmitAutoVarInit(emission: GroEmission);
761 Builder.CreateStore(Val: Builder.getTrue(), Addr: GroActiveFlag);
762 }
763};
764} // namespace
765
766static void emitBodyAndFallthrough(CodeGenFunction &CGF,
767 const CoroutineBodyStmt &S, Stmt *Body) {
768 CGF.EmitStmt(S: Body);
769 const bool CanFallthrough = CGF.Builder.GetInsertBlock();
770 if (CanFallthrough)
771 if (Stmt *OnFallthrough = S.getFallthroughHandler())
772 CGF.EmitStmt(S: OnFallthrough);
773}
774
775void CodeGenFunction::EmitCoroutineBody(const CoroutineBodyStmt &S) {
776 auto *NullPtr = llvm::ConstantPointerNull::get(T: Builder.getPtrTy());
777 auto &TI = CGM.getContext().getTargetInfo();
778 unsigned NewAlign = TI.getNewAlign() / TI.getCharWidth();
779
780 auto *EntryBB = Builder.GetInsertBlock();
781 auto *AllocBB = createBasicBlock(name: "coro.alloc");
782 auto *InitBB = createBasicBlock(name: "coro.init");
783 auto *FinalBB = createBasicBlock(name: "coro.final");
784 auto *RetBB = createBasicBlock(name: "coro.ret");
785
786 auto *CoroId = Builder.CreateCall(
787 CGM.getIntrinsic(llvm::Intrinsic::coro_id),
788 {Builder.getInt32(NewAlign), NullPtr, NullPtr, NullPtr});
789 createCoroData(*this, CurCoro, CoroId);
790 CurCoro.Data->SuspendBB = RetBB;
791 assert(ShouldEmitLifetimeMarkers &&
792 "Must emit lifetime intrinsics for coroutines");
793
794 // Backend is allowed to elide memory allocations, to help it, emit
795 // auto mem = coro.alloc() ? 0 : ... allocation code ...;
796 auto *CoroAlloc = Builder.CreateCall(
797 CGM.getIntrinsic(llvm::Intrinsic::coro_alloc), {CoroId});
798
799 Builder.CreateCondBr(CoroAlloc, AllocBB, InitBB);
800
801 EmitBlock(BB: AllocBB);
802 auto *AllocateCall = EmitScalarExpr(E: S.getAllocate());
803 auto *AllocOrInvokeContBB = Builder.GetInsertBlock();
804
805 // Handle allocation failure if 'ReturnStmtOnAllocFailure' was provided.
806 if (auto *RetOnAllocFailure = S.getReturnStmtOnAllocFailure()) {
807 auto *RetOnFailureBB = createBasicBlock(name: "coro.ret.on.failure");
808
809 // See if allocation was successful.
810 auto *NullPtr = llvm::ConstantPointerNull::get(T: Int8PtrTy);
811 auto *Cond = Builder.CreateICmpNE(LHS: AllocateCall, RHS: NullPtr);
812 // Expect the allocation to be successful.
813 emitCondLikelihoodViaExpectIntrinsic(Cond, LH: Stmt::LH_Likely);
814 Builder.CreateCondBr(Cond, True: InitBB, False: RetOnFailureBB);
815
816 // If not, return OnAllocFailure object.
817 EmitBlock(BB: RetOnFailureBB);
818 EmitStmt(S: RetOnAllocFailure);
819 }
820 else {
821 Builder.CreateBr(Dest: InitBB);
822 }
823
824 EmitBlock(BB: InitBB);
825
826 // Pass the result of the allocation to coro.begin.
827 auto *Phi = Builder.CreatePHI(Ty: VoidPtrTy, NumReservedValues: 2);
828 Phi->addIncoming(V: NullPtr, BB: EntryBB);
829 Phi->addIncoming(V: AllocateCall, BB: AllocOrInvokeContBB);
830 auto *CoroBegin = Builder.CreateCall(
831 CGM.getIntrinsic(llvm::Intrinsic::coro_begin), {CoroId, Phi});
832 CurCoro.Data->CoroBegin = CoroBegin;
833
834 GetReturnObjectManager GroManager(*this, S);
835 GroManager.EmitGroAlloca();
836
837 CurCoro.Data->CleanupJD = getJumpDestInCurrentScope(Target: RetBB);
838 {
839 CGDebugInfo *DI = getDebugInfo();
840 ParamReferenceReplacerRAII ParamReplacer(LocalDeclMap);
841 CodeGenFunction::RunCleanupsScope ResumeScope(*this);
842 EHStack.pushCleanup<CallCoroDelete>(Kind: NormalAndEHCleanup, A: S.getDeallocate());
843
844 // Create mapping between parameters and copy-params for coroutine function.
845 llvm::ArrayRef<const Stmt *> ParamMoves = S.getParamMoves();
846 assert(
847 (ParamMoves.size() == 0 || (ParamMoves.size() == FnArgs.size())) &&
848 "ParamMoves and FnArgs should be the same size for coroutine function");
849 if (ParamMoves.size() == FnArgs.size() && DI)
850 for (const auto Pair : llvm::zip(t&: FnArgs, u&: ParamMoves))
851 DI->getCoroutineParameterMappings().insert(
852 KV: {std::get<0>(t: Pair), std::get<1>(t: Pair)});
853
854 // Create parameter copies. We do it before creating a promise, since an
855 // evolution of coroutine TS may allow promise constructor to observe
856 // parameter copies.
857 for (auto *PM : S.getParamMoves()) {
858 EmitStmt(S: PM);
859 ParamReplacer.addCopy(PM: cast<DeclStmt>(Val: PM));
860 // TODO: if(CoroParam(...)) need to surround ctor and dtor
861 // for the copy, so that llvm can elide it if the copy is
862 // not needed.
863 }
864
865 EmitStmt(S: S.getPromiseDeclStmt());
866
867 Address PromiseAddr = GetAddrOfLocalVar(VD: S.getPromiseDecl());
868 auto *PromiseAddrVoidPtr = new llvm::BitCastInst(
869 PromiseAddr.emitRawPointer(CGF&: *this), VoidPtrTy, "", CoroId);
870 // Update CoroId to refer to the promise. We could not do it earlier because
871 // promise local variable was not emitted yet.
872 CoroId->setArgOperand(1, PromiseAddrVoidPtr);
873
874 // Now we have the promise, initialize the GRO
875 GroManager.EmitGroInit();
876
877 EHStack.pushCleanup<CallCoroEnd>(Kind: EHCleanup);
878
879 CurCoro.Data->CurrentAwaitKind = AwaitKind::Init;
880 CurCoro.Data->ExceptionHandler = S.getExceptionHandler();
881 EmitStmt(S: S.getInitSuspendStmt());
882 CurCoro.Data->FinalJD = getJumpDestInCurrentScope(Target: FinalBB);
883
884 CurCoro.Data->CurrentAwaitKind = AwaitKind::Normal;
885
886 if (CurCoro.Data->ExceptionHandler) {
887 // If we generated IR to record whether an exception was thrown from
888 // 'await_resume', then use that IR to determine whether the coroutine
889 // body should be skipped.
890 // If we didn't generate the IR (perhaps because 'await_resume' was marked
891 // as 'noexcept'), then we skip this check.
892 BasicBlock *ContBB = nullptr;
893 if (CurCoro.Data->ResumeEHVar) {
894 BasicBlock *BodyBB = createBasicBlock(name: "coro.resumed.body");
895 ContBB = createBasicBlock(name: "coro.resumed.cont");
896 Value *SkipBody = Builder.CreateFlagLoad(Addr: CurCoro.Data->ResumeEHVar,
897 Name: "coro.resumed.eh");
898 Builder.CreateCondBr(Cond: SkipBody, True: ContBB, False: BodyBB);
899 EmitBlock(BB: BodyBB);
900 }
901
902 auto Loc = S.getBeginLoc();
903 CXXCatchStmt Catch(Loc, /*exDecl=*/nullptr,
904 CurCoro.Data->ExceptionHandler);
905 auto *TryStmt =
906 CXXTryStmt::Create(C: getContext(), tryLoc: Loc, tryBlock: S.getBody(), handlers: &Catch);
907
908 EnterCXXTryStmt(S: *TryStmt);
909 emitBodyAndFallthrough(*this, S, TryStmt->getTryBlock());
910 ExitCXXTryStmt(S: *TryStmt);
911
912 if (ContBB)
913 EmitBlock(BB: ContBB);
914 }
915 else {
916 emitBodyAndFallthrough(*this, S, S.getBody());
917 }
918
919 // See if we need to generate final suspend.
920 const bool CanFallthrough = Builder.GetInsertBlock();
921 const bool HasCoreturns = CurCoro.Data->CoreturnCount > 0;
922 if (CanFallthrough || HasCoreturns) {
923 EmitBlock(BB: FinalBB);
924 CurCoro.Data->CurrentAwaitKind = AwaitKind::Final;
925 EmitStmt(S: S.getFinalSuspendStmt());
926 } else {
927 // We don't need FinalBB. Emit it to make sure the block is deleted.
928 EmitBlock(BB: FinalBB, /*IsFinished=*/true);
929 }
930 }
931
932 EmitBlock(BB: RetBB);
933 // Emit coro.end before getReturnStmt (and parameter destructors), since
934 // resume and destroy parts of the coroutine should not include them.
935 llvm::Function *CoroEnd = CGM.getIntrinsic(llvm::Intrinsic::coro_end);
936 Builder.CreateCall(Callee: CoroEnd,
937 Args: {NullPtr, Builder.getFalse(),
938 llvm::ConstantTokenNone::get(Context&: CoroEnd->getContext())});
939
940 if (Stmt *Ret = S.getReturnStmt()) {
941 // Since we already emitted the return value above, so we shouldn't
942 // emit it again here.
943 if (GroManager.DirectEmit)
944 cast<ReturnStmt>(Val: Ret)->setRetValue(nullptr);
945 EmitStmt(S: Ret);
946 }
947
948 // LLVM require the frontend to mark the coroutine.
949 CurFn->setPresplitCoroutine();
950
951 if (CXXRecordDecl *RD = FnRetTy->getAsCXXRecordDecl();
952 RD && RD->hasAttr<CoroOnlyDestroyWhenCompleteAttr>())
953 CurFn->setCoroDestroyOnlyWhenComplete();
954}
955
956// Emit coroutine intrinsic and patch up arguments of the token type.
957RValue CodeGenFunction::EmitCoroutineIntrinsic(const CallExpr *E,
958 unsigned int IID) {
959 SmallVector<llvm::Value *, 8> Args;
960 switch (IID) {
961 default:
962 break;
963 // The coro.frame builtin is replaced with an SSA value of the coro.begin
964 // intrinsic.
965 case llvm::Intrinsic::coro_frame: {
966 if (CurCoro.Data && CurCoro.Data->CoroBegin) {
967 return RValue::get(V: CurCoro.Data->CoroBegin);
968 }
969
970 if (CurAwaitSuspendWrapper.FramePtr) {
971 return RValue::get(V: CurAwaitSuspendWrapper.FramePtr);
972 }
973
974 CGM.Error(loc: E->getBeginLoc(), error: "this builtin expect that __builtin_coro_begin "
975 "has been used earlier in this function");
976 auto *NullPtr = llvm::ConstantPointerNull::get(T: Builder.getPtrTy());
977 return RValue::get(V: NullPtr);
978 }
979 case llvm::Intrinsic::coro_size: {
980 auto &Context = getContext();
981 CanQualType SizeTy = Context.getSizeType();
982 llvm::IntegerType *T = Builder.getIntNTy(N: Context.getTypeSize(T: SizeTy));
983 llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::coro_size, T);
984 return RValue::get(V: Builder.CreateCall(Callee: F));
985 }
986 case llvm::Intrinsic::coro_align: {
987 auto &Context = getContext();
988 CanQualType SizeTy = Context.getSizeType();
989 llvm::IntegerType *T = Builder.getIntNTy(N: Context.getTypeSize(T: SizeTy));
990 llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::coro_align, T);
991 return RValue::get(V: Builder.CreateCall(Callee: F));
992 }
993 // The following three intrinsics take a token parameter referring to a token
994 // returned by earlier call to @llvm.coro.id. Since we cannot represent it in
995 // builtins, we patch it up here.
996 case llvm::Intrinsic::coro_alloc:
997 case llvm::Intrinsic::coro_begin:
998 case llvm::Intrinsic::coro_free: {
999 if (CurCoro.Data && CurCoro.Data->CoroId) {
1000 Args.push_back(Elt: CurCoro.Data->CoroId);
1001 break;
1002 }
1003 CGM.Error(loc: E->getBeginLoc(), error: "this builtin expect that __builtin_coro_id has"
1004 " been used earlier in this function");
1005 // Fallthrough to the next case to add TokenNone as the first argument.
1006 [[fallthrough]];
1007 }
1008 // @llvm.coro.suspend takes a token parameter. Add token 'none' as the first
1009 // argument.
1010 case llvm::Intrinsic::coro_suspend:
1011 Args.push_back(Elt: llvm::ConstantTokenNone::get(Context&: getLLVMContext()));
1012 break;
1013 }
1014 for (const Expr *Arg : E->arguments())
1015 Args.push_back(Elt: EmitScalarExpr(E: Arg));
1016 // @llvm.coro.end takes a token parameter. Add token 'none' as the last
1017 // argument.
1018 if (IID == llvm::Intrinsic::coro_end)
1019 Args.push_back(Elt: llvm::ConstantTokenNone::get(Context&: getLLVMContext()));
1020
1021 llvm::Function *F = CGM.getIntrinsic(IID);
1022 llvm::CallInst *Call = Builder.CreateCall(Callee: F, Args);
1023
1024 // Note: The following code is to enable to emit coro.id and coro.begin by
1025 // hand to experiment with coroutines in C.
1026 // If we see @llvm.coro.id remember it in the CoroData. We will update
1027 // coro.alloc, coro.begin and coro.free intrinsics to refer to it.
1028 if (IID == llvm::Intrinsic::coro_id) {
1029 createCoroData(CGF&: *this, CurCoro, CoroId: Call, CoroIdExpr: E);
1030 }
1031 else if (IID == llvm::Intrinsic::coro_begin) {
1032 if (CurCoro.Data)
1033 CurCoro.Data->CoroBegin = Call;
1034 }
1035 else if (IID == llvm::Intrinsic::coro_free) {
1036 // Remember the last coro_free as we need it to build the conditional
1037 // deletion of the coroutine frame.
1038 if (CurCoro.Data)
1039 CurCoro.Data->LastCoroFree = Call;
1040 }
1041 return RValue::get(V: Call);
1042}
1043

source code of clang/lib/CodeGen/CGCoroutine.cpp