1 //===----- CGCoroutine.cpp - Emit LLVM Code for C++ coroutines ------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This contains code dealing with C++ code generation of coroutines.
11 //===----------------------------------------------------------------------===//
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"
19 using namespace clang
;
20 using namespace CodeGen
;
23 using llvm::BasicBlock
;
26 enum class AwaitKind
{ Init
, Normal
, Yield
, Final
};
27 static constexpr llvm::StringLiteral AwaitKindStr
[] = {"init", "await", "yield",
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;
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;
43 // A branch to this block is emitted when coroutine needs to suspend.
44 llvm::BasicBlock
*SuspendBB
= nullptr;
46 // The promise type's 'unhandled_exception' handler, if it defines one.
47 Stmt
*ExceptionHandler
= nullptr;
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;
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
58 CodeGenFunction::JumpDest CleanupJD
;
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
;
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
68 llvm::CallInst
*CoroId
= nullptr;
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;
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;
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
82 CallExpr
const *CoroIdExpr
= nullptr;
85 // Defining these here allows to keep CGCoroData private to this file.
86 clang::CodeGen::CodeGenFunction::CGCoroInfo::CGCoroInfo() {}
87 CodeGenFunction::CGCoroInfo::~CGCoroInfo() {}
89 static void createCoroData(CodeGenFunction
&CGF
,
90 CodeGenFunction::CGCoroInfo
&CurCoro
,
91 llvm::CallInst
*CoroId
,
92 CallExpr
const *CoroIdExpr
= nullptr) {
94 if (CurCoro
.Data
->CoroIdExpr
)
95 CGF
.CGM
.Error(CoroIdExpr
->getBeginLoc(),
96 "only one __builtin_coro_id can be used in a function");
98 CGF
.CGM
.Error(CoroIdExpr
->getBeginLoc(),
99 "__builtin_coro_id shall not be used in a C++ coroutine");
101 llvm_unreachable("EmitCoroutineBodyStatement called twice?");
106 CurCoro
.Data
= std::unique_ptr
<CGCoroData
>(new CGCoroData
);
107 CurCoro
.Data
->CoroId
= CoroId
;
108 CurCoro
.Data
->CoroIdExpr
= CoroIdExpr
;
111 // Synthesize a pretty name for a suspend point.
112 static SmallString
<32> buildSuspendPrefixStr(CGCoroData
&Coro
, AwaitKind Kind
) {
115 case AwaitKind::Init
:
116 case AwaitKind::Final
:
118 case AwaitKind::Normal
:
119 No
= ++Coro
.AwaitNum
;
121 case AwaitKind::Yield
:
122 No
= ++Coro
.YieldNum
;
125 SmallString
<32> Prefix(AwaitKindStr
[static_cast<unsigned>(Kind
)]);
127 Twine(No
).toVector(Prefix
);
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
)
142 // Emit suspend expression which roughly looks like:
144 // auto && x = CommonExpr();
145 // if (!x.await_ready()) {
147 // x.await_suspend(...); (*)
148 // llvm_coro_suspend(); (**)
152 // where the result of the entire expression is the result of x.await_resume()
154 // (*) If x.await_suspend return type is bool, it allows to veto a suspend:
155 // if (x.await_suspend(...))
156 // llvm_coro_suspend();
158 // (**) llvm_coro_suspend() encodes three possible continuations as
159 // a switch instruction:
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
167 // See llvm's docs/Coroutines.rst for more details.
170 struct LValueOrRValue
{
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();
182 CodeGenFunction::OpaqueValueMappingData::bind(CGF
, S
.getOpaqueValue(), E
);
183 auto UnbindOnExit
= llvm::make_scope_exit([&] { Binder
.unbind(CGF
); });
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"));
190 // If expression is ready, no need to suspend.
191 CGF
.EmitBranchOnBoolExpr(S
.getReadyExpr(), ReadyBlock
, SuspendBlock
, 0);
193 // Otherwise, emit suspend logic.
194 CGF
.EmitBlock(SuspendBlock
);
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
});
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
);
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
)});
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
);
224 // Emit cleanup for this suspend point.
225 CGF
.EmitBlock(CleanupBlock
);
226 CGF
.EmitBranchThroughCleanup(Coro
.CleanupJD
);
228 // Emit await_resume expression.
229 CGF
.EmitBlock(ReadyBlock
);
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())) {
237 CGF
.CreateTempAlloca(Builder
.getInt1Ty(), Prefix
+ Twine("resume.eh"));
238 Builder
.CreateFlagStore(true, Coro
.ResumeEHVar
);
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
);
251 Res
.LV
= CGF
.EmitLValue(S
.getResumeExpr());
253 Res
.RV
= CGF
.EmitAnyExpr(S
.getResumeExpr(), aggSlot
, ignoreResult
);
256 Builder
.CreateFlagStore(false, Coro
.ResumeEHVar
);
257 CGF
.ExitCXXTryStmt(*TryStmt
);
263 RValue
CodeGenFunction::EmitCoawaitExpr(const CoawaitExpr
&E
,
264 AggValueSlot aggSlot
,
266 return emitSuspendExpression(*this, *CurCoro
.Data
, E
,
267 CurCoro
.Data
->CurrentAwaitKind
, aggSlot
,
268 ignoreResult
, /*forLValue*/false).RV
;
270 RValue
CodeGenFunction::EmitCoyieldExpr(const CoyieldExpr
&E
,
271 AggValueSlot aggSlot
,
273 return emitSuspendExpression(*this, *CurCoro
.Data
, E
, AwaitKind::Yield
,
274 aggSlot
, ignoreResult
, /*forLValue*/false).RV
;
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);
286 EmitStmt(S
.getPromiseCall());
287 EmitBranchThroughCleanup(CurCoro
.Data
->FinalJD
);
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
297 assert(isa
<CallExpr
>(RE
) && "unexpected suspend expression type");
298 return cast
<CallExpr
>(RE
)->getCallReturnType(Ctx
);
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 "
307 return emitSuspendExpression(*this, *CurCoro
.Data
, *E
,
308 CurCoro
.Data
->CurrentAwaitKind
, AggValueSlot::ignored(),
309 /*ignoreResult*/false, /*forLValue*/true).LV
;
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 "
317 return emitSuspendExpression(*this, *CurCoro
.Data
, *E
,
318 AwaitKind::Yield
, AggValueSlot::ignored(),
319 /*ignoreResult*/false, /*forLValue*/true).LV
;
322 // Hunts for the parameter reference in the parameter copy/move declaration.
324 struct GetParamRef
: public StmtVisitor
<GetParamRef
> {
326 DeclRefExpr
*Expr
= nullptr;
328 void VisitDeclRefExpr(DeclRefExpr
*E
) {
329 assert(Expr
== nullptr && "multilple declref in param move");
332 void VisitStmt(Stmt
*S
) {
333 for (auto *C
: S
->children()) {
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
346 struct ParamReferenceReplacerRAII
{
347 CodeGenFunction::DeclMapTy SavedLocals
;
348 CodeGenFunction::DeclMapTy
& LocalDeclMap
;
350 ParamReferenceReplacerRAII(CodeGenFunction::DeclMapTy
&LocalDeclMap
)
351 : LocalDeclMap(LocalDeclMap
) {}
353 void addCopy(DeclStmt
const *PM
) {
354 // Figure out what param it refers to.
356 assert(PM
->isSingleDecl());
357 VarDecl
const*VD
= static_cast<VarDecl
const*>(PM
->getSingleDecl());
358 Expr
const *InitExpr
= VD
->getInit();
360 Visitor
.Visit(const_cast<Expr
*>(InitExpr
));
361 assert(Visitor
.Expr
);
362 DeclRefExpr
*DREOrig
= Visitor
.Expr
;
363 auto *PD
= DREOrig
->getDecl();
365 auto it
= LocalDeclMap
.find(PD
);
366 assert(it
!= LocalDeclMap
.end() && "parameter is not found");
367 SavedLocals
.insert({ PD
, it
->second
});
369 auto copyIt
= LocalDeclMap
.find(VD
);
370 assert(copyIt
!= LocalDeclMap
.end() && "parameter copy is not found");
371 it
->second
= copyIt
->getSecond();
374 ~ParamReferenceReplacerRAII() {
375 for (auto&& SavedLocal
: SavedLocals
) {
376 LocalDeclMap
.insert({SavedLocal
.first
, SavedLocal
.second
});
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
;
388 if (llvm::Instruction
*EHPad
= CGF
.CurrentFuncletPad
)
389 BundleList
.emplace_back("funclet", EHPad
);
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
{
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
);
420 // Make sure to call coro.delete on scope exit.
421 struct CallCoroDelete final
: public EHScopeStack::Cleanup
{
424 // Emit "if (coro.free(CoroId, CoroBegin)) Deallocate;"
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
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
435 BasicBlock
*SaveInsertBlock
= CGF
.Builder
.GetInsertBlock();
437 auto *FreeBB
= CGF
.createBasicBlock("coro.free");
438 CGF
.EmitBlock(FreeBB
);
439 CGF
.EmitStmt(Deallocate
);
441 auto *AfterFreeBB
= CGF
.createBasicBlock("after.coro.free");
442 CGF
.EmitBlock(AfterFreeBB
);
444 // We should have captured coro.free from the emission of deallocate.
445 auto *CoroFree
= CGF
.CurCoro
.Data
->LastCoroFree
;
447 CGF
.CGM
.Error(Deallocate
->getBeginLoc(),
448 "Deallocation expressoin does not refer to coro.free");
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
);
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
);
462 // No longer need old terminator.
463 InsertPt
->eraseFromParent();
464 CGF
.Builder
.SetInsertPoint(AfterFreeBB
);
466 explicit CallCoroDelete(Stmt
*DeallocStmt
) : Deallocate(DeallocStmt
) {}
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;
478 Address GroActiveFlag
;
479 CodeGenFunction::AutoVarEmission GroEmission
;
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.
489 // More info at https://github.com/cplusplus/papers/issues/1414
491 // The general cases:
492 // 1. Same type of get_return_object and coroutine return type (direct
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.
502 // Direct emission for void returning coroutines or GROs.
504 auto *RVI
= S
.getReturnValueInit();
505 assert(RVI
&& "expected RVI");
506 auto GroType
= RVI
->getType();
507 return CGF
.getContext().hasSameType(GroType
, CGF
.FnRetTy
);
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() {
520 auto *GroDeclStmt
= dyn_cast_or_null
<DeclStmt
>(S
.getResultDecl());
522 // If get_return_object returns void, no need to do an alloca.
526 auto *GroVarDecl
= cast
<VarDecl
>(GroDeclStmt
->getSingleDecl());
528 // Set GRO flag that it is not initialized yet
529 GroActiveFlag
= CGF
.CreateTempAlloca(Builder
.getInt1Ty(), CharUnits::One(),
531 Builder
.CreateStore(Builder
.getFalse(), GroActiveFlag
);
533 GroEmission
= CGF
.EmitAutoVarAlloca(*GroVarDecl
);
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();
540 // Make the cleanup conditional on gro.active
541 for (auto b
= CGF
.EHStack
.find(top
), e
= CGF
.EHStack
.find(old_top
); b
!= e
;
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();
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.
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(),
574 if (!GroActiveFlag
.isValid()) {
575 // No Gro variable was allocated. Simply emit the call to
576 // get_return_object.
577 CGF
.EmitStmt(S
.getResultDecl());
581 CGF
.EmitAutoVarInit(GroEmission
);
582 Builder
.CreateStore(Builder
.getTrue(), GroActiveFlag
);
587 static void emitBodyAndFallthrough(CodeGenFunction
&CGF
,
588 const CoroutineBodyStmt
&S
, Stmt
*Body
) {
590 const bool CanFallthrough
= CGF
.Builder
.GetInsertBlock();
592 if (Stmt
*OnFallthrough
= S
.getFallthroughHandler())
593 CGF
.EmitStmt(OnFallthrough
);
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();
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");
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");
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
});
620 Builder
.CreateCondBr(CoroAlloc
, AllocBB
, InitBB
);
623 auto *AllocateCall
= EmitScalarExpr(S
.getAllocate());
624 auto *AllocOrInvokeContBB
= Builder
.GetInsertBlock();
626 // Handle allocation failure if 'ReturnStmtOnAllocFailure' was provided.
627 if (auto *RetOnAllocFailure
= S
.getReturnStmtOnAllocFailure()) {
628 auto *RetOnFailureBB
= createBasicBlock("coro.ret.on.failure");
630 // See if allocation was successful.
631 auto *NullPtr
= llvm::ConstantPointerNull::get(Int8PtrTy
);
632 auto *Cond
= Builder
.CreateICmpNE(AllocateCall
, NullPtr
);
633 Builder
.CreateCondBr(Cond
, InitBB
, RetOnFailureBB
);
635 // If not, return OnAllocFailure object.
636 EmitBlock(RetOnFailureBB
);
637 EmitStmt(RetOnAllocFailure
);
640 Builder
.CreateBr(InitBB
);
645 // Pass the result of the allocation to coro.begin.
646 auto *Phi
= Builder
.CreatePHI(VoidPtrTy
, 2);
647 Phi
->addIncoming(NullPtr
, EntryBB
);
648 Phi
->addIncoming(AllocateCall
, AllocOrInvokeContBB
);
649 auto *CoroBegin
= Builder
.CreateCall(
650 CGM
.getIntrinsic(llvm::Intrinsic::coro_begin
), {CoroId
, Phi
});
651 CurCoro
.Data
->CoroBegin
= CoroBegin
;
653 GetReturnObjectManager
GroManager(*this, S
);
654 GroManager
.EmitGroAlloca();
656 CurCoro
.Data
->CleanupJD
= getJumpDestInCurrentScope(RetBB
);
658 CGDebugInfo
*DI
= getDebugInfo();
659 ParamReferenceReplacerRAII
ParamReplacer(LocalDeclMap
);
660 CodeGenFunction::RunCleanupsScope
ResumeScope(*this);
661 EHStack
.pushCleanup
<CallCoroDelete
>(NormalAndEHCleanup
, S
.getDeallocate());
663 // Create mapping between parameters and copy-params for coroutine function.
664 llvm::ArrayRef
<const Stmt
*> ParamMoves
= S
.getParamMoves();
666 (ParamMoves
.size() == 0 || (ParamMoves
.size() == FnArgs
.size())) &&
667 "ParamMoves and FnArgs should be the same size for coroutine function");
668 if (ParamMoves
.size() == FnArgs
.size() && DI
)
669 for (const auto Pair
: llvm::zip(FnArgs
, ParamMoves
))
670 DI
->getCoroutineParameterMappings().insert(
671 {std::get
<0>(Pair
), std::get
<1>(Pair
)});
673 // Create parameter copies. We do it before creating a promise, since an
674 // evolution of coroutine TS may allow promise constructor to observe
676 for (auto *PM
: S
.getParamMoves()) {
678 ParamReplacer
.addCopy(cast
<DeclStmt
>(PM
));
679 // TODO: if(CoroParam(...)) need to surround ctor and dtor
680 // for the copy, so that llvm can elide it if the copy is
684 EmitStmt(S
.getPromiseDeclStmt());
686 Address PromiseAddr
= GetAddrOfLocalVar(S
.getPromiseDecl());
687 auto *PromiseAddrVoidPtr
=
688 new llvm::BitCastInst(PromiseAddr
.getPointer(), VoidPtrTy
, "", CoroId
);
689 // Update CoroId to refer to the promise. We could not do it earlier because
690 // promise local variable was not emitted yet.
691 CoroId
->setArgOperand(1, PromiseAddrVoidPtr
);
693 // Now we have the promise, initialize the GRO
694 GroManager
.EmitGroInit();
696 EHStack
.pushCleanup
<CallCoroEnd
>(EHCleanup
);
698 CurCoro
.Data
->CurrentAwaitKind
= AwaitKind::Init
;
699 CurCoro
.Data
->ExceptionHandler
= S
.getExceptionHandler();
700 EmitStmt(S
.getInitSuspendStmt());
701 CurCoro
.Data
->FinalJD
= getJumpDestInCurrentScope(FinalBB
);
703 CurCoro
.Data
->CurrentAwaitKind
= AwaitKind::Normal
;
705 if (CurCoro
.Data
->ExceptionHandler
) {
706 // If we generated IR to record whether an exception was thrown from
707 // 'await_resume', then use that IR to determine whether the coroutine
708 // body should be skipped.
709 // If we didn't generate the IR (perhaps because 'await_resume' was marked
710 // as 'noexcept'), then we skip this check.
711 BasicBlock
*ContBB
= nullptr;
712 if (CurCoro
.Data
->ResumeEHVar
) {
713 BasicBlock
*BodyBB
= createBasicBlock("coro.resumed.body");
714 ContBB
= createBasicBlock("coro.resumed.cont");
715 Value
*SkipBody
= Builder
.CreateFlagLoad(CurCoro
.Data
->ResumeEHVar
,
717 Builder
.CreateCondBr(SkipBody
, ContBB
, BodyBB
);
721 auto Loc
= S
.getBeginLoc();
722 CXXCatchStmt
Catch(Loc
, /*exDecl=*/nullptr,
723 CurCoro
.Data
->ExceptionHandler
);
725 CXXTryStmt::Create(getContext(), Loc
, S
.getBody(), &Catch
);
727 EnterCXXTryStmt(*TryStmt
);
728 emitBodyAndFallthrough(*this, S
, TryStmt
->getTryBlock());
729 ExitCXXTryStmt(*TryStmt
);
735 emitBodyAndFallthrough(*this, S
, S
.getBody());
738 // See if we need to generate final suspend.
739 const bool CanFallthrough
= Builder
.GetInsertBlock();
740 const bool HasCoreturns
= CurCoro
.Data
->CoreturnCount
> 0;
741 if (CanFallthrough
|| HasCoreturns
) {
743 CurCoro
.Data
->CurrentAwaitKind
= AwaitKind::Final
;
744 EmitStmt(S
.getFinalSuspendStmt());
746 // We don't need FinalBB. Emit it to make sure the block is deleted.
747 EmitBlock(FinalBB
, /*IsFinished=*/true);
752 // Emit coro.end before getReturnStmt (and parameter destructors), since
753 // resume and destroy parts of the coroutine should not include them.
754 llvm::Function
*CoroEnd
= CGM
.getIntrinsic(llvm::Intrinsic::coro_end
);
755 Builder
.CreateCall(CoroEnd
, {NullPtr
, Builder
.getFalse()});
757 if (Stmt
*Ret
= S
.getReturnStmt()) {
758 // Since we already emitted the return value above, so we shouldn't
759 // emit it again here.
760 if (GroManager
.DirectEmit
)
761 cast
<ReturnStmt
>(Ret
)->setRetValue(nullptr);
765 // LLVM require the frontend to mark the coroutine.
766 CurFn
->setPresplitCoroutine();
769 // Emit coroutine intrinsic and patch up arguments of the token type.
770 RValue
CodeGenFunction::EmitCoroutineIntrinsic(const CallExpr
*E
,
772 SmallVector
<llvm::Value
*, 8> Args
;
776 // The coro.frame builtin is replaced with an SSA value of the coro.begin
778 case llvm::Intrinsic::coro_frame
: {
779 if (CurCoro
.Data
&& CurCoro
.Data
->CoroBegin
) {
780 return RValue::get(CurCoro
.Data
->CoroBegin
);
782 CGM
.Error(E
->getBeginLoc(), "this builtin expect that __builtin_coro_begin "
783 "has been used earlier in this function");
784 auto *NullPtr
= llvm::ConstantPointerNull::get(Builder
.getInt8PtrTy());
785 return RValue::get(NullPtr
);
787 case llvm::Intrinsic::coro_size
: {
788 auto &Context
= getContext();
789 CanQualType SizeTy
= Context
.getSizeType();
790 llvm::IntegerType
*T
= Builder
.getIntNTy(Context
.getTypeSize(SizeTy
));
791 llvm::Function
*F
= CGM
.getIntrinsic(llvm::Intrinsic::coro_size
, T
);
792 return RValue::get(Builder
.CreateCall(F
));
794 case llvm::Intrinsic::coro_align
: {
795 auto &Context
= getContext();
796 CanQualType SizeTy
= Context
.getSizeType();
797 llvm::IntegerType
*T
= Builder
.getIntNTy(Context
.getTypeSize(SizeTy
));
798 llvm::Function
*F
= CGM
.getIntrinsic(llvm::Intrinsic::coro_align
, T
);
799 return RValue::get(Builder
.CreateCall(F
));
801 // The following three intrinsics take a token parameter referring to a token
802 // returned by earlier call to @llvm.coro.id. Since we cannot represent it in
803 // builtins, we patch it up here.
804 case llvm::Intrinsic::coro_alloc
:
805 case llvm::Intrinsic::coro_begin
:
806 case llvm::Intrinsic::coro_free
: {
807 if (CurCoro
.Data
&& CurCoro
.Data
->CoroId
) {
808 Args
.push_back(CurCoro
.Data
->CoroId
);
811 CGM
.Error(E
->getBeginLoc(), "this builtin expect that __builtin_coro_id has"
812 " been used earlier in this function");
813 // Fallthrough to the next case to add TokenNone as the first argument.
816 // @llvm.coro.suspend takes a token parameter. Add token 'none' as the first
818 case llvm::Intrinsic::coro_suspend
:
819 Args
.push_back(llvm::ConstantTokenNone::get(getLLVMContext()));
822 for (const Expr
*Arg
: E
->arguments())
823 Args
.push_back(EmitScalarExpr(Arg
));
825 llvm::Function
*F
= CGM
.getIntrinsic(IID
);
826 llvm::CallInst
*Call
= Builder
.CreateCall(F
, Args
);
828 // Note: The following code is to enable to emit coro.id and coro.begin by
829 // hand to experiment with coroutines in C.
830 // If we see @llvm.coro.id remember it in the CoroData. We will update
831 // coro.alloc, coro.begin and coro.free intrinsics to refer to it.
832 if (IID
== llvm::Intrinsic::coro_id
) {
833 createCoroData(*this, CurCoro
, Call
, E
);
835 else if (IID
== llvm::Intrinsic::coro_begin
) {
837 CurCoro
.Data
->CoroBegin
= Call
;
839 else if (IID
== llvm::Intrinsic::coro_free
) {
840 // Remember the last coro_free as we need it to build the conditional
841 // deletion of the coroutine frame.
843 CurCoro
.Data
->LastCoroFree
= Call
;
845 return RValue::get(Call
);