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(CoroIdExpr->getBeginLoc(),
96                     "only one __builtin_coro_id can be used in a function");
97     else if (CoroIdExpr)
98       CGF.CGM.Error(CoroIdExpr->getBeginLoc(),
99                     "__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.
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(Prefix);
128   }
129   return Prefix;
130 }
131 
132 static bool memberCallExpressionCanThrow(const Expr *E) {
133   if (const auto *CE = dyn_cast<CXXMemberCallExpr>(E))
134     if (const auto *Proto =
135             CE->getMethodDecl()->getType()->getAs<FunctionProtoType>())
136       if (isNoexceptExceptionSpec(Proto->getExceptionSpecType()) &&
137           Proto->canThrow() == CT_Cannot)
138         return false;
139   return true;
140 }
141 
142 // Emit suspend expression which roughly looks like:
143 //
144 //   auto && x = CommonExpr();
145 //   if (!x.await_ready()) {
146 //      llvm_coro_save();
147 //      x.await_suspend(...);     (*)
148 //      llvm_coro_suspend(); (**)
149 //   }
150 //   x.await_resume();
151 //
152 // where the result of the entire expression is the result of x.await_resume()
153 //
154 //   (*) If x.await_suspend return type is bool, it allows to veto a suspend:
155 //      if (x.await_suspend(...))
156 //        llvm_coro_suspend();
157 //
158 //  (**) llvm_coro_suspend() encodes three possible continuations as
159 //       a switch instruction:
160 //
161 //  %where-to = call i8 @llvm.coro.suspend(...)
162 //  switch i8 %where-to, label %coro.ret [ ; jump to epilogue to suspend
163 //    i8 0, label %yield.ready   ; go here when resumed
164 //    i8 1, label %yield.cleanup ; go here when destroyed
165 //  ]
166 //
167 //  See llvm's docs/Coroutines.rst for more details.
168 //
169 namespace {
170   struct LValueOrRValue {
171     LValue LV;
172     RValue RV;
173   };
174 }
175 static LValueOrRValue emitSuspendExpression(CodeGenFunction &CGF, CGCoroData &Coro,
176                                     CoroutineSuspendExpr const &S,
177                                     AwaitKind Kind, AggValueSlot aggSlot,
178                                     bool ignoreResult, bool forLValue) {
179   auto *E = S.getCommonExpr();
180 
181   auto Binder =
182       CodeGenFunction::OpaqueValueMappingData::bind(CGF, S.getOpaqueValue(), E);
183   auto UnbindOnExit = llvm::make_scope_exit([&] { Binder.unbind(CGF); });
184 
185   auto Prefix = buildSuspendPrefixStr(Coro, Kind);
186   BasicBlock *ReadyBlock = CGF.createBasicBlock(Prefix + Twine(".ready"));
187   BasicBlock *SuspendBlock = CGF.createBasicBlock(Prefix + Twine(".suspend"));
188   BasicBlock *CleanupBlock = CGF.createBasicBlock(Prefix + Twine(".cleanup"));
189 
190   // If expression is ready, no need to suspend.
191   CGF.EmitBranchOnBoolExpr(S.getReadyExpr(), ReadyBlock, SuspendBlock, 0);
192 
193   // Otherwise, emit suspend logic.
194   CGF.EmitBlock(SuspendBlock);
195 
196   auto &Builder = CGF.Builder;
197   llvm::Function *CoroSave = CGF.CGM.getIntrinsic(llvm::Intrinsic::coro_save);
198   auto *NullPtr = llvm::ConstantPointerNull::get(CGF.CGM.Int8PtrTy);
199   auto *SaveCall = Builder.CreateCall(CoroSave, {NullPtr});
200 
201   CGF.CurCoro.InSuspendBlock = true;
202   auto *SuspendRet = CGF.EmitScalarExpr(S.getSuspendExpr());
203   CGF.CurCoro.InSuspendBlock = false;
204   if (SuspendRet != nullptr && SuspendRet->getType()->isIntegerTy(1)) {
205     // Veto suspension if requested by bool returning await_suspend.
206     BasicBlock *RealSuspendBlock =
207         CGF.createBasicBlock(Prefix + Twine(".suspend.bool"));
208     CGF.Builder.CreateCondBr(SuspendRet, RealSuspendBlock, ReadyBlock);
209     CGF.EmitBlock(RealSuspendBlock);
210   }
211 
212   // Emit the suspend point.
213   const bool IsFinalSuspend = (Kind == AwaitKind::Final);
214   llvm::Function *CoroSuspend =
215       CGF.CGM.getIntrinsic(llvm::Intrinsic::coro_suspend);
216   auto *SuspendResult = Builder.CreateCall(
217       CoroSuspend, {SaveCall, Builder.getInt1(IsFinalSuspend)});
218 
219   // Create a switch capturing three possible continuations.
220   auto *Switch = Builder.CreateSwitch(SuspendResult, Coro.SuspendBB, 2);
221   Switch->addCase(Builder.getInt8(0), ReadyBlock);
222   Switch->addCase(Builder.getInt8(1), CleanupBlock);
223 
224   // Emit cleanup for this suspend point.
225   CGF.EmitBlock(CleanupBlock);
226   CGF.EmitBranchThroughCleanup(Coro.CleanupJD);
227 
228   // Emit await_resume expression.
229   CGF.EmitBlock(ReadyBlock);
230 
231   // Exception handling requires additional IR. If the 'await_resume' function
232   // is marked as 'noexcept', we avoid generating this additional IR.
233   CXXTryStmt *TryStmt = nullptr;
234   if (Coro.ExceptionHandler && Kind == AwaitKind::Init &&
235       memberCallExpressionCanThrow(S.getResumeExpr())) {
236     Coro.ResumeEHVar =
237         CGF.CreateTempAlloca(Builder.getInt1Ty(), Prefix + Twine("resume.eh"));
238     Builder.CreateFlagStore(true, Coro.ResumeEHVar);
239 
240     auto Loc = S.getResumeExpr()->getExprLoc();
241     auto *Catch = new (CGF.getContext())
242         CXXCatchStmt(Loc, /*exDecl=*/nullptr, Coro.ExceptionHandler);
243     auto *TryBody = CompoundStmt::Create(CGF.getContext(), S.getResumeExpr(),
244                                          FPOptionsOverride(), Loc, Loc);
245     TryStmt = CXXTryStmt::Create(CGF.getContext(), Loc, TryBody, Catch);
246     CGF.EnterCXXTryStmt(*TryStmt);
247   }
248 
249   LValueOrRValue Res;
250   if (forLValue)
251     Res.LV = CGF.EmitLValue(S.getResumeExpr());
252   else
253     Res.RV = CGF.EmitAnyExpr(S.getResumeExpr(), aggSlot, ignoreResult);
254 
255   if (TryStmt) {
256     Builder.CreateFlagStore(false, Coro.ResumeEHVar);
257     CGF.ExitCXXTryStmt(*TryStmt);
258   }
259 
260   return Res;
261 }
262 
263 RValue CodeGenFunction::EmitCoawaitExpr(const CoawaitExpr &E,
264                                         AggValueSlot aggSlot,
265                                         bool ignoreResult) {
266   return emitSuspendExpression(*this, *CurCoro.Data, E,
267                                CurCoro.Data->CurrentAwaitKind, aggSlot,
268                                ignoreResult, /*forLValue*/false).RV;
269 }
270 RValue CodeGenFunction::EmitCoyieldExpr(const CoyieldExpr &E,
271                                         AggValueSlot aggSlot,
272                                         bool ignoreResult) {
273   return emitSuspendExpression(*this, *CurCoro.Data, E, AwaitKind::Yield,
274                                aggSlot, ignoreResult, /*forLValue*/false).RV;
275 }
276 
277 void CodeGenFunction::EmitCoreturnStmt(CoreturnStmt const &S) {
278   ++CurCoro.Data->CoreturnCount;
279   const Expr *RV = S.getOperand();
280   if (RV && RV->getType()->isVoidType() && !isa<InitListExpr>(RV)) {
281     // Make sure to evaluate the non initlist expression of a co_return
282     // with a void expression for side effects.
283     RunCleanupsScope cleanupScope(*this);
284     EmitIgnoredExpr(RV);
285   }
286   EmitStmt(S.getPromiseCall());
287   EmitBranchThroughCleanup(CurCoro.Data->FinalJD);
288 }
289 
290 
291 #ifndef NDEBUG
292 static QualType getCoroutineSuspendExprReturnType(const ASTContext &Ctx,
293   const CoroutineSuspendExpr *E) {
294   const auto *RE = E->getResumeExpr();
295   // Is it possible for RE to be a CXXBindTemporaryExpr wrapping
296   // a MemberCallExpr?
297   assert(isa<CallExpr>(RE) && "unexpected suspend expression type");
298   return cast<CallExpr>(RE)->getCallReturnType(Ctx);
299 }
300 #endif
301 
302 LValue
303 CodeGenFunction::EmitCoawaitLValue(const CoawaitExpr *E) {
304   assert(getCoroutineSuspendExprReturnType(getContext(), E)->isReferenceType() &&
305          "Can't have a scalar return unless the return type is a "
306          "reference type!");
307   return emitSuspendExpression(*this, *CurCoro.Data, *E,
308                                CurCoro.Data->CurrentAwaitKind, AggValueSlot::ignored(),
309                                /*ignoreResult*/false, /*forLValue*/true).LV;
310 }
311 
312 LValue
313 CodeGenFunction::EmitCoyieldLValue(const CoyieldExpr *E) {
314   assert(getCoroutineSuspendExprReturnType(getContext(), E)->isReferenceType() &&
315          "Can't have a scalar return unless the return type is a "
316          "reference type!");
317   return emitSuspendExpression(*this, *CurCoro.Data, *E,
318                                AwaitKind::Yield, AggValueSlot::ignored(),
319                                /*ignoreResult*/false, /*forLValue*/true).LV;
320 }
321 
322 // Hunts for the parameter reference in the parameter copy/move declaration.
323 namespace {
324 struct GetParamRef : public StmtVisitor<GetParamRef> {
325 public:
326   DeclRefExpr *Expr = nullptr;
327   GetParamRef() {}
328   void VisitDeclRefExpr(DeclRefExpr *E) {
329     assert(Expr == nullptr && "multilple declref in param move");
330     Expr = E;
331   }
332   void VisitStmt(Stmt *S) {
333     for (auto *C : S->children()) {
334       if (C)
335         Visit(C);
336     }
337   }
338 };
339 }
340 
341 // This class replaces references to parameters to their copies by changing
342 // the addresses in CGF.LocalDeclMap and restoring back the original values in
343 // its destructor.
344 
345 namespace {
346   struct ParamReferenceReplacerRAII {
347     CodeGenFunction::DeclMapTy SavedLocals;
348     CodeGenFunction::DeclMapTy& LocalDeclMap;
349 
350     ParamReferenceReplacerRAII(CodeGenFunction::DeclMapTy &LocalDeclMap)
351         : LocalDeclMap(LocalDeclMap) {}
352 
353     void addCopy(DeclStmt const *PM) {
354       // Figure out what param it refers to.
355 
356       assert(PM->isSingleDecl());
357       VarDecl const*VD = static_cast<VarDecl const*>(PM->getSingleDecl());
358       Expr const *InitExpr = VD->getInit();
359       GetParamRef Visitor;
360       Visitor.Visit(const_cast<Expr*>(InitExpr));
361       assert(Visitor.Expr);
362       DeclRefExpr *DREOrig = Visitor.Expr;
363       auto *PD = DREOrig->getDecl();
364 
365       auto it = LocalDeclMap.find(PD);
366       assert(it != LocalDeclMap.end() && "parameter is not found");
367       SavedLocals.insert({ PD, it->second });
368 
369       auto copyIt = LocalDeclMap.find(VD);
370       assert(copyIt != LocalDeclMap.end() && "parameter copy is not found");
371       it->second = copyIt->getSecond();
372     }
373 
374     ~ParamReferenceReplacerRAII() {
375       for (auto&& SavedLocal : SavedLocals) {
376         LocalDeclMap.insert({SavedLocal.first, SavedLocal.second});
377       }
378     }
379   };
380 }
381 
382 // For WinEH exception representation backend needs to know what funclet coro.end
383 // belongs to. That information is passed in a funclet bundle.
384 static SmallVector<llvm::OperandBundleDef, 1>
385 getBundlesForCoroEnd(CodeGenFunction &CGF) {
386   SmallVector<llvm::OperandBundleDef, 1> BundleList;
387 
388   if (llvm::Instruction *EHPad = CGF.CurrentFuncletPad)
389     BundleList.emplace_back("funclet", EHPad);
390 
391   return BundleList;
392 }
393 
394 namespace {
395 // We will insert coro.end to cut any of the destructors for objects that
396 // do not need to be destroyed once the coroutine is resumed.
397 // See llvm/docs/Coroutines.rst for more details about coro.end.
398 struct CallCoroEnd final : public EHScopeStack::Cleanup {
399   void Emit(CodeGenFunction &CGF, Flags flags) override {
400     auto &CGM = CGF.CGM;
401     auto *NullPtr = llvm::ConstantPointerNull::get(CGF.Int8PtrTy);
402     llvm::Function *CoroEndFn = CGM.getIntrinsic(llvm::Intrinsic::coro_end);
403     // See if we have a funclet bundle to associate coro.end with. (WinEH)
404     auto Bundles = getBundlesForCoroEnd(CGF);
405     auto *CoroEnd = CGF.Builder.CreateCall(
406         CoroEndFn, {NullPtr, CGF.Builder.getTrue()}, Bundles);
407     if (Bundles.empty()) {
408       // Otherwise, (landingpad model), create a conditional branch that leads
409       // either to a cleanup block or a block with EH resume instruction.
410       auto *ResumeBB = CGF.getEHResumeBlock(/*isCleanup=*/true);
411       auto *CleanupContBB = CGF.createBasicBlock("cleanup.cont");
412       CGF.Builder.CreateCondBr(CoroEnd, ResumeBB, CleanupContBB);
413       CGF.EmitBlock(CleanupContBB);
414     }
415   }
416 };
417 }
418 
419 namespace {
420 // Make sure to call coro.delete on scope exit.
421 struct CallCoroDelete final : public EHScopeStack::Cleanup {
422   Stmt *Deallocate;
423 
424   // Emit "if (coro.free(CoroId, CoroBegin)) Deallocate;"
425 
426   // Note: That deallocation will be emitted twice: once for a normal exit and
427   // once for exceptional exit. This usage is safe because Deallocate does not
428   // contain any declarations. The SubStmtBuilder::makeNewAndDeleteExpr()
429   // builds a single call to a deallocation function which is safe to emit
430   // multiple times.
431   void Emit(CodeGenFunction &CGF, Flags) override {
432     // Remember the current point, as we are going to emit deallocation code
433     // first to get to coro.free instruction that is an argument to a delete
434     // call.
435     BasicBlock *SaveInsertBlock = CGF.Builder.GetInsertBlock();
436 
437     auto *FreeBB = CGF.createBasicBlock("coro.free");
438     CGF.EmitBlock(FreeBB);
439     CGF.EmitStmt(Deallocate);
440 
441     auto *AfterFreeBB = CGF.createBasicBlock("after.coro.free");
442     CGF.EmitBlock(AfterFreeBB);
443 
444     // We should have captured coro.free from the emission of deallocate.
445     auto *CoroFree = CGF.CurCoro.Data->LastCoroFree;
446     if (!CoroFree) {
447       CGF.CGM.Error(Deallocate->getBeginLoc(),
448                     "Deallocation expressoin does not refer to coro.free");
449       return;
450     }
451 
452     // Get back to the block we were originally and move coro.free there.
453     auto *InsertPt = SaveInsertBlock->getTerminator();
454     CoroFree->moveBefore(InsertPt);
455     CGF.Builder.SetInsertPoint(InsertPt);
456 
457     // Add if (auto *mem = coro.free) Deallocate;
458     auto *NullPtr = llvm::ConstantPointerNull::get(CGF.Int8PtrTy);
459     auto *Cond = CGF.Builder.CreateICmpNE(CoroFree, NullPtr);
460     CGF.Builder.CreateCondBr(Cond, FreeBB, AfterFreeBB);
461 
462     // No longer need old terminator.
463     InsertPt->eraseFromParent();
464     CGF.Builder.SetInsertPoint(AfterFreeBB);
465   }
466   explicit CallCoroDelete(Stmt *DeallocStmt) : Deallocate(DeallocStmt) {}
467 };
468 }
469 
470 namespace {
471 struct GetReturnObjectManager {
472   CodeGenFunction &CGF;
473   CGBuilderTy &Builder;
474   const CoroutineBodyStmt &S;
475   // When true, performs RVO for the return object.
476   bool DirectEmit = false;
477 
478   Address GroActiveFlag;
479   CodeGenFunction::AutoVarEmission GroEmission;
480 
481   GetReturnObjectManager(CodeGenFunction &CGF, const CoroutineBodyStmt &S)
482       : CGF(CGF), Builder(CGF.Builder), S(S), GroActiveFlag(Address::invalid()),
483         GroEmission(CodeGenFunction::AutoVarEmission::invalid()) {
484     // The call to get_­return_­object is sequenced before the call to
485     // initial_­suspend and is invoked at most once, but there are caveats
486     // regarding on whether the prvalue result object may be initialized
487     // directly/eager or delayed, depending on the types involved.
488     //
489     // More info at https://github.com/cplusplus/papers/issues/1414
490     //
491     // The general cases:
492     // 1. Same type of get_return_object and coroutine return type (direct
493     // emission):
494     //  - Constructed in the return slot.
495     // 2. Different types (delayed emission):
496     //  - Constructed temporary object prior to initial suspend initialized with
497     //  a call to get_return_object()
498     //  - When coroutine needs to to return to the caller and needs to construct
499     //  return value for the coroutine it is initialized with expiring value of
500     //  the temporary obtained above.
501     //
502     // Direct emission for void returning coroutines or GROs.
503     DirectEmit = [&]() {
504       auto *RVI = S.getReturnValueInit();
505       assert(RVI && "expected RVI");
506       auto GroType = RVI->getType();
507       return CGF.getContext().hasSameType(GroType, CGF.FnRetTy);
508     }();
509   }
510 
511   // The gro variable has to outlive coroutine frame and coroutine promise, but,
512   // it can only be initialized after coroutine promise was created, thus, we
513   // split its emission in two parts. EmitGroAlloca emits an alloca and sets up
514   // cleanups. Later when coroutine promise is available we initialize the gro
515   // and sets the flag that the cleanup is now active.
516   void EmitGroAlloca() {
517     if (DirectEmit)
518       return;
519 
520     auto *GroDeclStmt = dyn_cast_or_null<DeclStmt>(S.getResultDecl());
521     if (!GroDeclStmt) {
522       // If get_return_object returns void, no need to do an alloca.
523       return;
524     }
525 
526     auto *GroVarDecl = cast<VarDecl>(GroDeclStmt->getSingleDecl());
527 
528     // Set GRO flag that it is not initialized yet
529     GroActiveFlag = CGF.CreateTempAlloca(Builder.getInt1Ty(), CharUnits::One(),
530                                          "gro.active");
531     Builder.CreateStore(Builder.getFalse(), GroActiveFlag);
532 
533     GroEmission = CGF.EmitAutoVarAlloca(*GroVarDecl);
534 
535     // Remember the top of EHStack before emitting the cleanup.
536     auto old_top = CGF.EHStack.stable_begin();
537     CGF.EmitAutoVarCleanups(GroEmission);
538     auto top = CGF.EHStack.stable_begin();
539 
540     // Make the cleanup conditional on gro.active
541     for (auto b = CGF.EHStack.find(top), e = CGF.EHStack.find(old_top); b != e;
542          b++) {
543       if (auto *Cleanup = dyn_cast<EHCleanupScope>(&*b)) {
544         assert(!Cleanup->hasActiveFlag() && "cleanup already has active flag?");
545         Cleanup->setActiveFlag(GroActiveFlag);
546         Cleanup->setTestFlagInEHCleanup();
547         Cleanup->setTestFlagInNormalCleanup();
548       }
549     }
550   }
551 
552   void EmitGroInit() {
553     if (DirectEmit) {
554       // ReturnValue should be valid as long as the coroutine's return type
555       // is not void. The assertion could help us to reduce the check later.
556       assert(CGF.ReturnValue.isValid() == (bool)S.getReturnStmt());
557       // Now we have the promise, initialize the GRO.
558       // We need to emit `get_return_object` first. According to:
559       // [dcl.fct.def.coroutine]p7
560       // The call to get_return_­object is sequenced before the call to
561       // initial_suspend and is invoked at most once.
562       //
563       // So we couldn't emit return value when we emit return statment,
564       // otherwise the call to get_return_object wouldn't be in front
565       // of initial_suspend.
566       if (CGF.ReturnValue.isValid()) {
567         CGF.EmitAnyExprToMem(S.getReturnValue(), CGF.ReturnValue,
568                              S.getReturnValue()->getType().getQualifiers(),
569                              /*IsInit*/ true);
570       }
571       return;
572     }
573 
574     if (!GroActiveFlag.isValid()) {
575       // No Gro variable was allocated. Simply emit the call to
576       // get_return_object.
577       CGF.EmitStmt(S.getResultDecl());
578       return;
579     }
580 
581     CGF.EmitAutoVarInit(GroEmission);
582     Builder.CreateStore(Builder.getTrue(), GroActiveFlag);
583   }
584 };
585 } // namespace
586 
587 static void emitBodyAndFallthrough(CodeGenFunction &CGF,
588                                    const CoroutineBodyStmt &S, Stmt *Body) {
589   CGF.EmitStmt(Body);
590   const bool CanFallthrough = CGF.Builder.GetInsertBlock();
591   if (CanFallthrough)
592     if (Stmt *OnFallthrough = S.getFallthroughHandler())
593       CGF.EmitStmt(OnFallthrough);
594 }
595 
596 void CodeGenFunction::EmitCoroutineBody(const CoroutineBodyStmt &S) {
597   auto *NullPtr = llvm::ConstantPointerNull::get(Builder.getInt8PtrTy());
598   auto &TI = CGM.getContext().getTargetInfo();
599   unsigned NewAlign = TI.getNewAlign() / TI.getCharWidth();
600 
601   auto *EntryBB = Builder.GetInsertBlock();
602   auto *AllocBB = createBasicBlock("coro.alloc");
603   auto *InitBB = createBasicBlock("coro.init");
604   auto *FinalBB = createBasicBlock("coro.final");
605   auto *RetBB = createBasicBlock("coro.ret");
606 
607   auto *CoroId = Builder.CreateCall(
608       CGM.getIntrinsic(llvm::Intrinsic::coro_id),
609       {Builder.getInt32(NewAlign), NullPtr, NullPtr, NullPtr});
610   createCoroData(*this, CurCoro, CoroId);
611   CurCoro.Data->SuspendBB = RetBB;
612   assert(ShouldEmitLifetimeMarkers &&
613          "Must emit lifetime intrinsics for coroutines");
614 
615   // Backend is allowed to elide memory allocations, to help it, emit
616   // auto mem = coro.alloc() ? 0 : ... allocation code ...;
617   auto *CoroAlloc = Builder.CreateCall(
618       CGM.getIntrinsic(llvm::Intrinsic::coro_alloc), {CoroId});
619 
620   Builder.CreateCondBr(CoroAlloc, AllocBB, InitBB);
621 
622   EmitBlock(AllocBB);
623   auto *AllocateCall = EmitScalarExpr(S.getAllocate());
624   auto *AllocOrInvokeContBB = Builder.GetInsertBlock();
625 
626   // Handle allocation failure if 'ReturnStmtOnAllocFailure' was provided.
627   if (auto *RetOnAllocFailure = S.getReturnStmtOnAllocFailure()) {
628     auto *RetOnFailureBB = createBasicBlock("coro.ret.on.failure");
629 
630     // See if allocation was successful.
631     auto *NullPtr = llvm::ConstantPointerNull::get(Int8PtrTy);
632     auto *Cond = Builder.CreateICmpNE(AllocateCall, NullPtr);
633     // Expect the allocation to be successful.
634     emitCondLikelihoodViaExpectIntrinsic(Cond, Stmt::LH_Likely);
635     Builder.CreateCondBr(Cond, InitBB, RetOnFailureBB);
636 
637     // If not, return OnAllocFailure object.
638     EmitBlock(RetOnFailureBB);
639     EmitStmt(RetOnAllocFailure);
640   }
641   else {
642     Builder.CreateBr(InitBB);
643   }
644 
645   EmitBlock(InitBB);
646 
647   // Pass the result of the allocation to coro.begin.
648   auto *Phi = Builder.CreatePHI(VoidPtrTy, 2);
649   Phi->addIncoming(NullPtr, EntryBB);
650   Phi->addIncoming(AllocateCall, AllocOrInvokeContBB);
651   auto *CoroBegin = Builder.CreateCall(
652       CGM.getIntrinsic(llvm::Intrinsic::coro_begin), {CoroId, Phi});
653   CurCoro.Data->CoroBegin = CoroBegin;
654 
655   GetReturnObjectManager GroManager(*this, S);
656   GroManager.EmitGroAlloca();
657 
658   CurCoro.Data->CleanupJD = getJumpDestInCurrentScope(RetBB);
659   {
660     CGDebugInfo *DI = getDebugInfo();
661     ParamReferenceReplacerRAII ParamReplacer(LocalDeclMap);
662     CodeGenFunction::RunCleanupsScope ResumeScope(*this);
663     EHStack.pushCleanup<CallCoroDelete>(NormalAndEHCleanup, S.getDeallocate());
664 
665     // Create mapping between parameters and copy-params for coroutine function.
666     llvm::ArrayRef<const Stmt *> ParamMoves = S.getParamMoves();
667     assert(
668         (ParamMoves.size() == 0 || (ParamMoves.size() == FnArgs.size())) &&
669         "ParamMoves and FnArgs should be the same size for coroutine function");
670     if (ParamMoves.size() == FnArgs.size() && DI)
671       for (const auto Pair : llvm::zip(FnArgs, ParamMoves))
672         DI->getCoroutineParameterMappings().insert(
673             {std::get<0>(Pair), std::get<1>(Pair)});
674 
675     // Create parameter copies. We do it before creating a promise, since an
676     // evolution of coroutine TS may allow promise constructor to observe
677     // parameter copies.
678     for (auto *PM : S.getParamMoves()) {
679       EmitStmt(PM);
680       ParamReplacer.addCopy(cast<DeclStmt>(PM));
681       // TODO: if(CoroParam(...)) need to surround ctor and dtor
682       // for the copy, so that llvm can elide it if the copy is
683       // not needed.
684     }
685 
686     EmitStmt(S.getPromiseDeclStmt());
687 
688     Address PromiseAddr = GetAddrOfLocalVar(S.getPromiseDecl());
689     auto *PromiseAddrVoidPtr =
690         new llvm::BitCastInst(PromiseAddr.getPointer(), VoidPtrTy, "", CoroId);
691     // Update CoroId to refer to the promise. We could not do it earlier because
692     // promise local variable was not emitted yet.
693     CoroId->setArgOperand(1, PromiseAddrVoidPtr);
694 
695     // Now we have the promise, initialize the GRO
696     GroManager.EmitGroInit();
697 
698     EHStack.pushCleanup<CallCoroEnd>(EHCleanup);
699 
700     CurCoro.Data->CurrentAwaitKind = AwaitKind::Init;
701     CurCoro.Data->ExceptionHandler = S.getExceptionHandler();
702     EmitStmt(S.getInitSuspendStmt());
703     CurCoro.Data->FinalJD = getJumpDestInCurrentScope(FinalBB);
704 
705     CurCoro.Data->CurrentAwaitKind = AwaitKind::Normal;
706 
707     if (CurCoro.Data->ExceptionHandler) {
708       // If we generated IR to record whether an exception was thrown from
709       // 'await_resume', then use that IR to determine whether the coroutine
710       // body should be skipped.
711       // If we didn't generate the IR (perhaps because 'await_resume' was marked
712       // as 'noexcept'), then we skip this check.
713       BasicBlock *ContBB = nullptr;
714       if (CurCoro.Data->ResumeEHVar) {
715         BasicBlock *BodyBB = createBasicBlock("coro.resumed.body");
716         ContBB = createBasicBlock("coro.resumed.cont");
717         Value *SkipBody = Builder.CreateFlagLoad(CurCoro.Data->ResumeEHVar,
718                                                  "coro.resumed.eh");
719         Builder.CreateCondBr(SkipBody, ContBB, BodyBB);
720         EmitBlock(BodyBB);
721       }
722 
723       auto Loc = S.getBeginLoc();
724       CXXCatchStmt Catch(Loc, /*exDecl=*/nullptr,
725                          CurCoro.Data->ExceptionHandler);
726       auto *TryStmt =
727           CXXTryStmt::Create(getContext(), Loc, S.getBody(), &Catch);
728 
729       EnterCXXTryStmt(*TryStmt);
730       emitBodyAndFallthrough(*this, S, TryStmt->getTryBlock());
731       ExitCXXTryStmt(*TryStmt);
732 
733       if (ContBB)
734         EmitBlock(ContBB);
735     }
736     else {
737       emitBodyAndFallthrough(*this, S, S.getBody());
738     }
739 
740     // See if we need to generate final suspend.
741     const bool CanFallthrough = Builder.GetInsertBlock();
742     const bool HasCoreturns = CurCoro.Data->CoreturnCount > 0;
743     if (CanFallthrough || HasCoreturns) {
744       EmitBlock(FinalBB);
745       CurCoro.Data->CurrentAwaitKind = AwaitKind::Final;
746       EmitStmt(S.getFinalSuspendStmt());
747     } else {
748       // We don't need FinalBB. Emit it to make sure the block is deleted.
749       EmitBlock(FinalBB, /*IsFinished=*/true);
750     }
751   }
752 
753   EmitBlock(RetBB);
754   // Emit coro.end before getReturnStmt (and parameter destructors), since
755   // resume and destroy parts of the coroutine should not include them.
756   llvm::Function *CoroEnd = CGM.getIntrinsic(llvm::Intrinsic::coro_end);
757   Builder.CreateCall(CoroEnd, {NullPtr, Builder.getFalse()});
758 
759   if (Stmt *Ret = S.getReturnStmt()) {
760     // Since we already emitted the return value above, so we shouldn't
761     // emit it again here.
762     if (GroManager.DirectEmit)
763       cast<ReturnStmt>(Ret)->setRetValue(nullptr);
764     EmitStmt(Ret);
765   }
766 
767   // LLVM require the frontend to mark the coroutine.
768   CurFn->setPresplitCoroutine();
769 }
770 
771 // Emit coroutine intrinsic and patch up arguments of the token type.
772 RValue CodeGenFunction::EmitCoroutineIntrinsic(const CallExpr *E,
773                                                unsigned int IID) {
774   SmallVector<llvm::Value *, 8> Args;
775   switch (IID) {
776   default:
777     break;
778   // The coro.frame builtin is replaced with an SSA value of the coro.begin
779   // intrinsic.
780   case llvm::Intrinsic::coro_frame: {
781     if (CurCoro.Data && CurCoro.Data->CoroBegin) {
782       return RValue::get(CurCoro.Data->CoroBegin);
783     }
784     CGM.Error(E->getBeginLoc(), "this builtin expect that __builtin_coro_begin "
785                                 "has been used earlier in this function");
786     auto *NullPtr = llvm::ConstantPointerNull::get(Builder.getInt8PtrTy());
787     return RValue::get(NullPtr);
788   }
789   case llvm::Intrinsic::coro_size: {
790     auto &Context = getContext();
791     CanQualType SizeTy = Context.getSizeType();
792     llvm::IntegerType *T = Builder.getIntNTy(Context.getTypeSize(SizeTy));
793     llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::coro_size, T);
794     return RValue::get(Builder.CreateCall(F));
795   }
796   case llvm::Intrinsic::coro_align: {
797     auto &Context = getContext();
798     CanQualType SizeTy = Context.getSizeType();
799     llvm::IntegerType *T = Builder.getIntNTy(Context.getTypeSize(SizeTy));
800     llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::coro_align, T);
801     return RValue::get(Builder.CreateCall(F));
802   }
803   // The following three intrinsics take a token parameter referring to a token
804   // returned by earlier call to @llvm.coro.id. Since we cannot represent it in
805   // builtins, we patch it up here.
806   case llvm::Intrinsic::coro_alloc:
807   case llvm::Intrinsic::coro_begin:
808   case llvm::Intrinsic::coro_free: {
809     if (CurCoro.Data && CurCoro.Data->CoroId) {
810       Args.push_back(CurCoro.Data->CoroId);
811       break;
812     }
813     CGM.Error(E->getBeginLoc(), "this builtin expect that __builtin_coro_id has"
814                                 " been used earlier in this function");
815     // Fallthrough to the next case to add TokenNone as the first argument.
816     [[fallthrough]];
817   }
818   // @llvm.coro.suspend takes a token parameter. Add token 'none' as the first
819   // argument.
820   case llvm::Intrinsic::coro_suspend:
821     Args.push_back(llvm::ConstantTokenNone::get(getLLVMContext()));
822     break;
823   }
824   for (const Expr *Arg : E->arguments())
825     Args.push_back(EmitScalarExpr(Arg));
826 
827   llvm::Function *F = CGM.getIntrinsic(IID);
828   llvm::CallInst *Call = Builder.CreateCall(F, Args);
829 
830   // Note: The following code is to enable to emit coro.id and coro.begin by
831   // hand to experiment with coroutines in C.
832   // If we see @llvm.coro.id remember it in the CoroData. We will update
833   // coro.alloc, coro.begin and coro.free intrinsics to refer to it.
834   if (IID == llvm::Intrinsic::coro_id) {
835     createCoroData(*this, CurCoro, Call, E);
836   }
837   else if (IID == llvm::Intrinsic::coro_begin) {
838     if (CurCoro.Data)
839       CurCoro.Data->CoroBegin = Call;
840   }
841   else if (IID == llvm::Intrinsic::coro_free) {
842     // Remember the last coro_free as we need it to build the conditional
843     // deletion of the coroutine frame.
844     if (CurCoro.Data)
845       CurCoro.Data->LastCoroFree = Call;
846   }
847   return RValue::get(Call);
848 }
849