16 #include "llvm/ADT/ScopeExit.h" 20 using namespace clang;
21 using namespace CodeGen;
24 using llvm::BasicBlock;
27 enum class AwaitKind { Init, Normal, Yield, Final };
28 static constexpr llvm::StringLiteral AwaitKindStr[] = {
"init",
"await",
"yield",
37 unsigned AwaitNum = 0;
38 unsigned YieldNum = 0;
42 unsigned CoreturnCount = 0;
45 llvm::BasicBlock *SuspendBB =
nullptr;
48 Stmt *ExceptionHandler =
nullptr;
69 llvm::CallInst *CoroId =
nullptr;
74 llvm::CallInst *CoroBegin =
nullptr;
78 llvm::CallInst *LastCoroFree =
nullptr;
92 llvm::CallInst *CoroId,
93 CallExpr const *CoroIdExpr =
nullptr) {
95 if (CurCoro.
Data->CoroIdExpr)
96 CGF.
CGM.
Error(CoroIdExpr->getBeginLoc(),
97 "only one __builtin_coro_id can be used in a function");
99 CGF.
CGM.
Error(CoroIdExpr->getBeginLoc(),
100 "__builtin_coro_id shall not be used in a C++ coroutine");
102 llvm_unreachable(
"EmitCoroutineBodyStatement called twice?");
108 CurCoro.
Data->CoroId = CoroId;
109 CurCoro.
Data->CoroIdExpr = CoroIdExpr;
116 case AwaitKind::Init:
117 case AwaitKind::Final:
119 case AwaitKind::Normal:
122 case AwaitKind::Yield:
126 SmallString<32> Prefix(AwaitKindStr[static_cast<unsigned>(Kind)]);
128 Twine(No).toVector(Prefix);
134 if (
const auto *CE = dyn_cast<CXXMemberCallExpr>(E))
135 if (
const auto *Proto =
171 struct LValueOrRValue {
179 bool ignoreResult,
bool forLValue) {
184 auto UnbindOnExit = llvm::make_scope_exit([&] { Binder.unbind(CGF); });
188 BasicBlock *SuspendBlock = CGF.
createBasicBlock(Prefix + Twine(
".suspend"));
189 BasicBlock *CleanupBlock = CGF.
createBasicBlock(Prefix + Twine(
".cleanup"));
198 llvm::Function *CoroSave = CGF.
CGM.
getIntrinsic(llvm::Intrinsic::coro_save);
199 auto *NullPtr = llvm::ConstantPointerNull::get(CGF.
CGM.
Int8PtrTy);
200 auto *SaveCall = Builder.CreateCall(CoroSave, {NullPtr});
203 if (SuspendRet !=
nullptr && SuspendRet->getType()->isIntegerTy(1)) {
205 BasicBlock *RealSuspendBlock =
207 CGF.
Builder.CreateCondBr(SuspendRet, RealSuspendBlock, ReadyBlock);
208 SuspendBlock = RealSuspendBlock;
213 const bool IsFinalSuspend = (Kind == AwaitKind::Final);
214 llvm::Function *CoroSuspend =
216 auto *SuspendResult = Builder.CreateCall(
217 CoroSuspend, {SaveCall, Builder.getInt1(IsFinalSuspend)});
220 auto *Switch = Builder.CreateSwitch(SuspendResult, Coro.
SuspendBB, 2);
221 Switch->addCase(Builder.getInt8(0), ReadyBlock);
222 Switch->addCase(Builder.getInt8(1), CleanupBlock);
267 CurCoro.Data->CurrentAwaitKind, aggSlot,
268 ignoreResult,
false).RV;
274 aggSlot, ignoreResult,
false).RV;
278 ++CurCoro.Data->CoreturnCount;
287 EmitBranchThroughCleanup(CurCoro.Data->FinalJD);
297 assert(isa<CallExpr>(RE) &&
"unexpected suspend expression type");
298 return cast<CallExpr>(RE)->getCallReturnType(Ctx);
305 "Can't have a scalar return unless the return type is a " 315 "Can't have a scalar return unless the return type is a " 324 struct GetParamRef :
public StmtVisitor<GetParamRef> {
329 assert(Expr ==
nullptr &&
"multilple declref in param move");
332 void VisitStmt(
Stmt *S) {
346 struct ParamReferenceReplacerRAII {
351 : LocalDeclMap(LocalDeclMap) {}
358 Expr const *InitExpr = VD->getInit();
360 Visitor.Visit(const_cast<Expr*>(InitExpr));
361 assert(Visitor.Expr);
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});
389 BundleList.emplace_back(
"funclet", EHPad);
401 auto *NullPtr = llvm::ConstantPointerNull::get(CGF.
Int8PtrTy);
402 llvm::Function *CoroEndFn = CGM.getIntrinsic(llvm::Intrinsic::coro_end);
405 auto *CoroEnd = CGF.
Builder.CreateCall(
406 CoroEndFn, {NullPtr, CGF.
Builder.getTrue()}, Bundles);
407 if (Bundles.empty()) {
412 CGF.
Builder.CreateCondBr(CoroEnd, ResumeBB, CleanupContBB);
435 BasicBlock *SaveInsertBlock = CGF.
Builder.GetInsertBlock();
447 CGF.
CGM.
Error(Deallocate->getBeginLoc(),
448 "Deallocation expressoin does not refer to coro.free");
453 auto *InsertPt = SaveInsertBlock->getTerminator();
454 CoroFree->moveBefore(InsertPt);
455 CGF.
Builder.SetInsertPoint(InsertPt);
458 auto *NullPtr = llvm::ConstantPointerNull::get(CGF.
Int8PtrTy);
459 auto *Cond = CGF.
Builder.CreateICmpNE(CoroFree, NullPtr);
460 CGF.
Builder.CreateCondBr(Cond, FreeBB, AfterFreeBB);
463 InsertPt->eraseFromParent();
464 CGF.
Builder.SetInsertPoint(AfterFreeBB);
466 explicit CallCoroDelete(
Stmt *DeallocStmt) : Deallocate(DeallocStmt) {}
471 struct GetReturnObjectManager {
489 void EmitGroAlloca() {
496 auto *GroVarDecl = cast<VarDecl>(GroDeclStmt->getSingleDecl());
501 Builder.
CreateStore(Builder.getFalse(), GroActiveFlag);
513 if (
auto *Cleanup = dyn_cast<EHCleanupScope>(&*b)) {
514 assert(!Cleanup->hasActiveFlag() &&
"cleanup already has active flag?");
515 Cleanup->setActiveFlag(GroActiveFlag);
516 Cleanup->setTestFlagInEHCleanup();
517 Cleanup->setTestFlagInNormalCleanup();
523 if (!GroActiveFlag.
isValid()) {
531 Builder.
CreateStore(Builder.getTrue(), GroActiveFlag);
539 const bool CanFallthrough = CGF.
Builder.GetInsertBlock();
546 auto *NullPtr = llvm::ConstantPointerNull::get(Builder.getInt8PtrTy());
547 auto &TI = CGM.getContext().getTargetInfo();
548 unsigned NewAlign = TI.getNewAlign() / TI.getCharWidth();
550 auto *EntryBB = Builder.GetInsertBlock();
551 auto *AllocBB = createBasicBlock(
"coro.alloc");
552 auto *InitBB = createBasicBlock(
"coro.init");
553 auto *FinalBB = createBasicBlock(
"coro.final");
554 auto *RetBB = createBasicBlock(
"coro.ret");
556 auto *CoroId = Builder.CreateCall(
557 CGM.getIntrinsic(llvm::Intrinsic::coro_id),
558 {Builder.getInt32(NewAlign), NullPtr, NullPtr, NullPtr});
560 CurCoro.Data->SuspendBB = RetBB;
564 auto *CoroAlloc = Builder.CreateCall(
565 CGM.getIntrinsic(llvm::Intrinsic::coro_alloc), {CoroId});
567 Builder.CreateCondBr(CoroAlloc, AllocBB, InitBB);
570 auto *AllocateCall = EmitScalarExpr(S.
getAllocate());
571 auto *AllocOrInvokeContBB = Builder.GetInsertBlock();
575 auto *RetOnFailureBB = createBasicBlock(
"coro.ret.on.failure");
578 auto *NullPtr = llvm::ConstantPointerNull::get(Int8PtrTy);
579 auto *Cond = Builder.CreateICmpNE(AllocateCall, NullPtr);
580 Builder.CreateCondBr(Cond, InitBB, RetOnFailureBB);
583 EmitBlock(RetOnFailureBB);
584 EmitStmt(RetOnAllocFailure);
587 Builder.CreateBr(InitBB);
593 auto *Phi = Builder.CreatePHI(VoidPtrTy, 2);
594 Phi->addIncoming(NullPtr, EntryBB);
595 Phi->addIncoming(AllocateCall, AllocOrInvokeContBB);
596 auto *CoroBegin = Builder.CreateCall(
597 CGM.getIntrinsic(llvm::Intrinsic::coro_begin), {CoroId, Phi});
598 CurCoro.Data->CoroBegin = CoroBegin;
600 GetReturnObjectManager GroManager(*
this, S);
601 GroManager.EmitGroAlloca();
603 CurCoro.Data->CleanupJD = getJumpDestInCurrentScope(RetBB);
605 ParamReferenceReplacerRAII ParamReplacer(LocalDeclMap);
614 ParamReplacer.addCopy(cast<DeclStmt>(PM));
623 auto *PromiseAddrVoidPtr =
624 new llvm::BitCastInst(PromiseAddr.getPointer(), VoidPtrTy,
"", CoroId);
627 CoroId->setArgOperand(1, PromiseAddrVoidPtr);
630 GroManager.EmitGroInit();
632 EHStack.pushCleanup<CallCoroEnd>(
EHCleanup);
634 CurCoro.Data->CurrentAwaitKind = AwaitKind::Init;
637 CurCoro.Data->FinalJD = getJumpDestInCurrentScope(FinalBB);
639 CurCoro.Data->CurrentAwaitKind = AwaitKind::Normal;
641 if (CurCoro.Data->ExceptionHandler) {
647 BasicBlock *ContBB =
nullptr;
648 if (CurCoro.Data->ResumeEHVar) {
649 BasicBlock *BodyBB = createBasicBlock(
"coro.resumed.body");
650 ContBB = createBasicBlock(
"coro.resumed.cont");
651 Value *SkipBody = Builder.CreateFlagLoad(CurCoro.Data->ResumeEHVar,
653 Builder.CreateCondBr(SkipBody, ContBB, BodyBB);
659 CurCoro.Data->ExceptionHandler);
663 EnterCXXTryStmt(*TryStmt);
665 ExitCXXTryStmt(*TryStmt);
675 const bool CanFallthrough = Builder.GetInsertBlock();
676 const bool HasCoreturns = CurCoro.Data->CoreturnCount > 0;
677 if (CanFallthrough || HasCoreturns) {
679 CurCoro.Data->CurrentAwaitKind = AwaitKind::Final;
683 EmitBlock(FinalBB,
true);
690 llvm::Function *CoroEnd = CGM.getIntrinsic(llvm::Intrinsic::coro_end);
691 Builder.CreateCall(CoroEnd, {NullPtr, Builder.getFalse()});
706 case llvm::Intrinsic::coro_frame: {
707 if (CurCoro.Data && CurCoro.Data->CoroBegin) {
710 CGM.Error(E->
getBeginLoc(),
"this builtin expect that __builtin_coro_begin " 711 "has been used earlier in this function");
712 auto NullPtr = llvm::ConstantPointerNull::get(Builder.getInt8PtrTy());
718 case llvm::Intrinsic::coro_alloc:
719 case llvm::Intrinsic::coro_begin:
720 case llvm::Intrinsic::coro_free: {
721 if (CurCoro.Data && CurCoro.Data->CoroId) {
722 Args.push_back(CurCoro.Data->CoroId);
725 CGM.Error(E->
getBeginLoc(),
"this builtin expect that __builtin_coro_id has" 726 " been used earlier in this function");
732 case llvm::Intrinsic::coro_suspend:
733 Args.push_back(llvm::ConstantTokenNone::get(getLLVMContext()));
737 Args.push_back(EmitScalarExpr(Arg));
740 llvm::CallInst *Call = Builder.CreateCall(F, Args);
746 if (IID == llvm::Intrinsic::coro_id) {
749 else if (IID == llvm::Intrinsic::coro_begin) {
751 CurCoro.Data->CoroBegin = Call;
753 else if (IID == llvm::Intrinsic::coro_free) {
757 CurCoro.Data->LastCoroFree = Call;
Expr * getReadyExpr() const
void EmitCoroutineBody(const CoroutineBodyStmt &S)
A (possibly-)qualified type.
void EmitBranchOnBoolExpr(const Expr *Cond, llvm::BasicBlock *TrueBlock, llvm::BasicBlock *FalseBlock, uint64_t TrueCount)
EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g.
RValue EmitCoyieldExpr(const CoyieldExpr &E, AggValueSlot aggSlot=AggValueSlot::ignored(), bool ignoreResult=false)
Represents a 'co_return' statement in the C++ Coroutines TS.
Stmt - This represents one statement.
static bool memberCallExpressionCanThrow(const Expr *E)
RValue EmitCoawaitExpr(const CoawaitExpr &E, AggValueSlot aggSlot=AggValueSlot::ignored(), bool ignoreResult=false)
Stmt * getPromiseDeclStmt() const
iterator find(stable_iterator save) const
Turn a stable reference to a scope depth into a unstable pointer to the EH stack. ...
stable_iterator stable_begin() const
Create a stable reference to the top of the EH stack.
Stmt * getExceptionHandler() const
Expr * getDeallocate() const
Represents a variable declaration or definition.
Stmt * getResultDecl() const
A jump destination is an abstract label, branching to which may require a jump out through normal cle...
bool isNoexceptExceptionSpec(ExceptionSpecificationType ESpecType)
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
VarDecl * getPromiseDecl() const
LValue EmitCoyieldLValue(const CoyieldExpr *E)
Denotes a cleanup that should run when a scope is exited using exceptional control flow (a throw stat...
RValue EmitAnyExpr(const Expr *E, AggValueSlot aggSlot=AggValueSlot::ignored(), bool ignoreResult=false)
EmitAnyExpr - Emit code to compute the specified expression which can have any type.
ArrayRef< Stmt const * > getParamMoves() const
static CompoundStmt * Create(const ASTContext &C, ArrayRef< Stmt *> Stmts, SourceLocation LB, SourceLocation RB)
llvm::BasicBlock * createBasicBlock(const Twine &name="", llvm::Function *parent=nullptr, llvm::BasicBlock *before=nullptr)
createBasicBlock - Create an LLVM basic block.
OpaqueValueExpr * getOpaqueValue() const
getOpaqueValue - Return the opaque value placeholder.
Expr * getCommonExpr() const
llvm::AllocaInst * CreateTempAlloca(llvm::Type *Ty, const Twine &Name="tmp", llvm::Value *ArraySize=nullptr)
CreateTempAlloca - This creates an alloca and inserts it into the entry block if ArraySize is nullptr...
Stmt * getReturnStmt() const
static QualType getCoroutineSuspendExprReturnType(const ASTContext &Ctx, const CoroutineSuspendExpr *E)
static CharUnits One()
One - Construct a CharUnits quantity of one.
AutoVarEmission EmitAutoVarAlloca(const VarDecl &var)
EmitAutoVarAlloca - Emit the alloca and debug information for a local variable.
ASTContext & getContext() const
Represents a prototype with parameter type info, e.g.
RValue - This trivial value class is used to represent the result of an expression that is evaluated...
static SmallVector< llvm::OperandBundleDef, 1 > getBundlesForCoroEnd(CodeGenFunction &CGF)
static AutoVarEmission invalid()
static SmallString< 32 > buildSuspendPrefixStr(CGCoroData &Coro, AwaitKind Kind)
This represents one expression.
void EmitAutoVarInit(const AutoVarEmission &emission)
Enters a new scope for capturing cleanups, all of which will be executed once the scope is exited...
CXXTryStmt - A C++ try block, including all handlers.
CodeGenFunction::JumpDest FinalJD
llvm::Value * ResumeEHVar
std::unique_ptr< CGCoroData > Data
static LValueOrRValue emitSuspendExpression(CodeGenFunction &CGF, CGCoroData &Coro, CoroutineSuspendExpr const &S, AwaitKind Kind, AggValueSlot aggSlot, bool ignoreResult, bool forLValue)
RValue EmitCoroutineIntrinsic(const CallExpr *E, unsigned int IID)
DeclStmt - Adaptor class for mixing declarations with statements and expressions. ...
SourceLocation getBeginLoc() const LLVM_READONLY
CodeGenFunction::JumpDest CleanupJD
void EmitStmt(const Stmt *S, ArrayRef< const Attr *> Attrs=None)
EmitStmt - Emit the code for the statement.
StmtVisitor - This class implements a simple visitor for Stmt subclasses.
Expr * getSuspendExpr() const
llvm::BasicBlock * SuspendBB
llvm::BasicBlock * getEHResumeBlock(bool isCleanup)
llvm::Instruction * CurrentFuncletPad
void Error(SourceLocation loc, StringRef error)
Emit a general error that something can't be done.
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
llvm::Value * EmitScalarExpr(const Expr *E, bool IgnoreResultAssign=false)
EmitScalarExpr - Emit the computation of the specified expression of LLVM scalar type, returning the result.
Dataflow Directional Tag Classes.
Stmt * getReturnStmtOnAllocFailure() const
Expr * getAllocate() const
static AggValueSlot ignored()
ignored - Returns an aggregate value slot indicating that the aggregate value is being ignored...
Represents a 'co_yield' expression.
Expr * getOperand() const
Retrieve the operand of the 'co_return' statement.
const Decl * getSingleDecl() const
llvm::Function * getIntrinsic(unsigned IID, ArrayRef< llvm::Type *> Tys=None)
Stmt * getInitSuspendStmt() const
llvm::StoreInst * CreateStore(llvm::Value *Val, Address Addr, bool IsVolatile=false)
bool isSingleDecl() const
isSingleDecl - This method returns true if this DeclStmt refers to a single Decl. ...
void EmitCoreturnStmt(const CoreturnStmt &S)
static void emitBodyAndFallthrough(CodeGenFunction &CGF, const CoroutineBodyStmt &S, Stmt *Body)
llvm::DenseMap< const Decl *, Address > DeclMapTy
void EmitAutoVarCleanups(const AutoVarEmission &emission)
static void createCoroData(CodeGenFunction &CGF, CodeGenFunction::CGCoroInfo &CurCoro, llvm::CallInst *CoroId, CallExpr const *CoroIdExpr=nullptr)
Represents the body of a coroutine.
Represents a 'co_await' expression.
llvm::PointerType * Int8PtrTy
void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false)
EmitBlock - Emit the given block.
void EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock=false)
Represents an expression that might suspend coroutine execution; either a co_await or co_yield expres...
Expr * getResumeExpr() const
static OpaqueValueMappingData bind(CodeGenFunction &CGF, const OpaqueValueExpr *ov, const Expr *e)
Expr * getPromiseCall() const
Retrieve the promise call that results from this 'co_return' statement.
LValue EmitCoawaitLValue(const CoawaitExpr *E)
Stmt * getFallthroughHandler() const
static CXXTryStmt * Create(const ASTContext &C, SourceLocation tryLoc, Stmt *tryBlock, ArrayRef< Stmt *> handlers)
CXXCatchStmt - This represents a C++ catch block.
LValue EmitLValue(const Expr *E)
EmitLValue - Emit code to compute a designator that specifies the location of the expression...
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Stmt * getBody() const
Retrieve the body of the coroutine as written.
A reference to a declared variable, function, enum, etc.
static RValue get(llvm::Value *V)
void ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock=false)
void EmitBranchThroughCleanup(JumpDest Dest)
EmitBranchThroughCleanup - Emit a branch from the current insert block through the normal cleanup han...
LValue - This represents an lvalue references.
SourceLocation getBeginLoc() const LLVM_READONLY
Information for lazily generating a cleanup.
Stmt * getFinalSuspendStmt() const