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;
60 llvm::CallInst *CoroId =
nullptr;
65 llvm::CallInst *CoroBegin =
nullptr;
69 llvm::CallInst *LastCoroFree =
nullptr;
83 llvm::CallInst *CoroId,
84 CallExpr const *CoroIdExpr =
nullptr) {
86 if (CurCoro.
Data->CoroIdExpr)
87 CGF.
CGM.
Error(CoroIdExpr->getLocStart(),
88 "only one __builtin_coro_id can be used in a function");
90 CGF.
CGM.
Error(CoroIdExpr->getLocStart(),
91 "__builtin_coro_id shall not be used in a C++ coroutine");
93 llvm_unreachable(
"EmitCoroutineBodyStatement called twice?");
99 CurCoro.
Data->CoroId = CoroId;
100 CurCoro.
Data->CoroIdExpr = CoroIdExpr;
107 case AwaitKind::Init:
108 case AwaitKind::Final:
110 case AwaitKind::Normal:
113 case AwaitKind::Yield:
119 Twine(No).toVector(Prefix);
152 struct LValueOrRValue {
160 bool ignoreResult,
bool forLValue) {
165 auto UnbindOnExit = llvm::make_scope_exit([&] { Binder.unbind(CGF); });
169 BasicBlock *SuspendBlock = CGF.
createBasicBlock(Prefix + Twine(
".suspend"));
170 BasicBlock *CleanupBlock = CGF.
createBasicBlock(Prefix + Twine(
".cleanup"));
179 llvm::Function *CoroSave = CGF.
CGM.
getIntrinsic(llvm::Intrinsic::coro_save);
180 auto *NullPtr = llvm::ConstantPointerNull::get(CGF.
CGM.
Int8PtrTy);
181 auto *SaveCall = Builder.CreateCall(CoroSave, {NullPtr});
184 if (SuspendRet !=
nullptr && SuspendRet->getType()->isIntegerTy(1)) {
186 BasicBlock *RealSuspendBlock =
188 CGF.
Builder.CreateCondBr(SuspendRet, RealSuspendBlock, ReadyBlock);
189 SuspendBlock = RealSuspendBlock;
194 const bool IsFinalSuspend = (Kind == AwaitKind::Final);
195 llvm::Function *CoroSuspend =
197 auto *SuspendResult = Builder.CreateCall(
198 CoroSuspend, {SaveCall, Builder.getInt1(IsFinalSuspend)});
201 auto *Switch = Builder.CreateSwitch(SuspendResult, Coro.
SuspendBB, 2);
202 Switch->addCase(Builder.getInt8(0), ReadyBlock);
203 Switch->addCase(Builder.getInt8(1), CleanupBlock);
223 CurCoro.Data->CurrentAwaitKind, aggSlot,
224 ignoreResult,
false).RV;
230 aggSlot, ignoreResult,
false).RV;
234 ++CurCoro.Data->CoreturnCount;
243 EmitBranchThroughCleanup(CurCoro.Data->FinalJD);
253 assert(isa<CallExpr>(RE) &&
"unexpected suspend expression type");
254 return cast<CallExpr>(RE)->getCallReturnType(Ctx);
261 "Can't have a scalar return unless the return type is a " 271 "Can't have a scalar return unless the return type is a " 280 struct GetParamRef :
public StmtVisitor<GetParamRef> {
285 assert(Expr ==
nullptr &&
"multilple declref in param move");
288 void VisitStmt(
Stmt *S) {
302 struct ParamReferenceReplacerRAII {
307 : LocalDeclMap(LocalDeclMap) {}
314 Expr const *InitExpr = VD->getInit();
316 Visitor.Visit(const_cast<Expr*>(InitExpr));
317 assert(Visitor.Expr);
318 auto *DREOrig = cast<DeclRefExpr>(Visitor.Expr);
319 auto *PD = DREOrig->getDecl();
321 auto it = LocalDeclMap.find(PD);
322 assert(it != LocalDeclMap.end() &&
"parameter is not found");
323 SavedLocals.insert({ PD, it->second });
325 auto copyIt = LocalDeclMap.find(VD);
326 assert(copyIt != LocalDeclMap.end() &&
"parameter copy is not found");
327 it->second = copyIt->getSecond();
330 ~ParamReferenceReplacerRAII() {
331 for (
auto&& SavedLocal : SavedLocals) {
332 LocalDeclMap.insert({SavedLocal.first, SavedLocal.second});
345 BundleList.emplace_back(
"funclet", EHPad);
357 auto *NullPtr = llvm::ConstantPointerNull::get(CGF.
Int8PtrTy);
358 llvm::Function *CoroEndFn = CGM.getIntrinsic(llvm::Intrinsic::coro_end);
361 auto *CoroEnd = CGF.
Builder.CreateCall(
362 CoroEndFn, {NullPtr, CGF.
Builder.getTrue()}, Bundles);
363 if (Bundles.empty()) {
368 CGF.
Builder.CreateCondBr(CoroEnd, ResumeBB, CleanupContBB);
391 BasicBlock *SaveInsertBlock = CGF.
Builder.GetInsertBlock();
403 CGF.
CGM.
Error(Deallocate->getLocStart(),
404 "Deallocation expressoin does not refer to coro.free");
409 auto *InsertPt = SaveInsertBlock->getTerminator();
410 CoroFree->moveBefore(InsertPt);
411 CGF.
Builder.SetInsertPoint(InsertPt);
414 auto *NullPtr = llvm::ConstantPointerNull::get(CGF.
Int8PtrTy);
415 auto *Cond = CGF.
Builder.CreateICmpNE(CoroFree, NullPtr);
416 CGF.
Builder.CreateCondBr(Cond, FreeBB, AfterFreeBB);
419 InsertPt->eraseFromParent();
420 CGF.
Builder.SetInsertPoint(AfterFreeBB);
422 explicit CallCoroDelete(
Stmt *DeallocStmt) : Deallocate(DeallocStmt) {}
427 struct GetReturnObjectManager {
445 void EmitGroAlloca() {
452 auto *GroVarDecl = cast<VarDecl>(GroDeclStmt->getSingleDecl());
457 Builder.
CreateStore(Builder.getFalse(), GroActiveFlag);
469 if (
auto *Cleanup = dyn_cast<EHCleanupScope>(&*b)) {
470 assert(!Cleanup->hasActiveFlag() &&
"cleanup already has active flag?");
471 Cleanup->setActiveFlag(GroActiveFlag);
472 Cleanup->setTestFlagInEHCleanup();
473 Cleanup->setTestFlagInNormalCleanup();
479 if (!GroActiveFlag.
isValid()) {
487 Builder.
CreateStore(Builder.getTrue(), GroActiveFlag);
495 const bool CanFallthrough = CGF.
Builder.GetInsertBlock();
502 auto *NullPtr = llvm::ConstantPointerNull::get(Builder.getInt8PtrTy());
503 auto &TI = CGM.getContext().getTargetInfo();
504 unsigned NewAlign = TI.getNewAlign() / TI.getCharWidth();
506 auto *EntryBB = Builder.GetInsertBlock();
507 auto *AllocBB = createBasicBlock(
"coro.alloc");
508 auto *InitBB = createBasicBlock(
"coro.init");
509 auto *FinalBB = createBasicBlock(
"coro.final");
510 auto *RetBB = createBasicBlock(
"coro.ret");
512 auto *CoroId = Builder.CreateCall(
513 CGM.getIntrinsic(llvm::Intrinsic::coro_id),
514 {Builder.getInt32(NewAlign), NullPtr, NullPtr, NullPtr});
516 CurCoro.Data->SuspendBB = RetBB;
520 auto *CoroAlloc = Builder.CreateCall(
521 CGM.getIntrinsic(llvm::Intrinsic::coro_alloc), {CoroId});
523 Builder.CreateCondBr(CoroAlloc, AllocBB, InitBB);
526 auto *AllocateCall = EmitScalarExpr(S.
getAllocate());
527 auto *AllocOrInvokeContBB = Builder.GetInsertBlock();
531 auto *RetOnFailureBB = createBasicBlock(
"coro.ret.on.failure");
534 auto *NullPtr = llvm::ConstantPointerNull::get(Int8PtrTy);
535 auto *Cond = Builder.CreateICmpNE(AllocateCall, NullPtr);
536 Builder.CreateCondBr(Cond, InitBB, RetOnFailureBB);
539 EmitBlock(RetOnFailureBB);
540 EmitStmt(RetOnAllocFailure);
543 Builder.CreateBr(InitBB);
549 auto *Phi = Builder.CreatePHI(VoidPtrTy, 2);
550 Phi->addIncoming(NullPtr, EntryBB);
551 Phi->addIncoming(AllocateCall, AllocOrInvokeContBB);
552 auto *CoroBegin = Builder.CreateCall(
553 CGM.getIntrinsic(llvm::Intrinsic::coro_begin), {CoroId, Phi});
554 CurCoro.Data->CoroBegin = CoroBegin;
556 GetReturnObjectManager GroManager(*
this, S);
557 GroManager.EmitGroAlloca();
559 CurCoro.Data->CleanupJD = getJumpDestInCurrentScope(RetBB);
561 ParamReferenceReplacerRAII ParamReplacer(LocalDeclMap);
570 ParamReplacer.addCopy(cast<DeclStmt>(PM));
579 auto *PromiseAddrVoidPtr =
580 new llvm::BitCastInst(PromiseAddr.getPointer(), VoidPtrTy,
"", CoroId);
583 CoroId->setArgOperand(1, PromiseAddrVoidPtr);
586 GroManager.EmitGroInit();
588 EHStack.pushCleanup<CallCoroEnd>(
EHCleanup);
590 CurCoro.Data->CurrentAwaitKind = AwaitKind::Init;
592 CurCoro.Data->FinalJD = getJumpDestInCurrentScope(FinalBB);
594 CurCoro.Data->CurrentAwaitKind = AwaitKind::Normal;
601 EnterCXXTryStmt(*TryStmt);
603 ExitCXXTryStmt(*TryStmt);
610 const bool CanFallthrough = Builder.GetInsertBlock();
611 const bool HasCoreturns = CurCoro.Data->CoreturnCount > 0;
612 if (CanFallthrough || HasCoreturns) {
614 CurCoro.Data->CurrentAwaitKind = AwaitKind::Final;
618 EmitBlock(FinalBB,
true);
625 llvm::Function *CoroEnd = CGM.getIntrinsic(llvm::Intrinsic::coro_end);
626 Builder.CreateCall(CoroEnd, {NullPtr, Builder.getFalse()});
641 case llvm::Intrinsic::coro_frame: {
642 if (CurCoro.Data && CurCoro.Data->CoroBegin) {
645 CGM.Error(E->
getLocStart(),
"this builtin expect that __builtin_coro_begin " 646 "has been used earlier in this function");
647 auto NullPtr = llvm::ConstantPointerNull::get(Builder.getInt8PtrTy());
653 case llvm::Intrinsic::coro_alloc:
654 case llvm::Intrinsic::coro_begin:
655 case llvm::Intrinsic::coro_free: {
656 if (CurCoro.Data && CurCoro.Data->CoroId) {
657 Args.push_back(CurCoro.Data->CoroId);
660 CGM.Error(E->
getLocStart(),
"this builtin expect that __builtin_coro_id has" 661 " been used earlier in this function");
667 case llvm::Intrinsic::coro_suspend:
668 Args.push_back(llvm::ConstantTokenNone::get(getLLVMContext()));
672 Args.push_back(EmitScalarExpr(Arg));
675 llvm::CallInst *Call = Builder.CreateCall(F, Args);
681 if (IID == llvm::Intrinsic::coro_id) {
684 else if (IID == llvm::Intrinsic::coro_begin) {
686 CurCoro.Data->CoroBegin = Call;
688 else if (IID == llvm::Intrinsic::coro_free) {
692 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)
Stmt - This represents one statement.
Represents a 'co_return' statement in the C++ Coroutines TS.
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
VarDecl - An instance of this class is created to represent 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...
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
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.
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)
Expr - 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...
SourceLocation getLocStart() const LLVM_READONLY
CodeGenFunction::JumpDest FinalJD
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. ...
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.
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
SourceLocation getLocStart() const LLVM_READONLY
void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false)
EmitBlock - Emit the given block.
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 EmitBranchThroughCleanup(JumpDest Dest)
EmitBranchThroughCleanup - Emit a branch from the current insert block through the normal cleanup han...
LValue - This represents an lvalue references.
Information for lazily generating a cleanup.
Stmt * getFinalSuspendStmt() const