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",
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) {
186 assert(SuspendRet->getType()->isIntegerTy(1) &&
187 "Sema should have already checked that it is void or bool");
188 BasicBlock *RealSuspendBlock =
190 CGF.
Builder.CreateCondBr(SuspendRet, RealSuspendBlock, ReadyBlock);
191 SuspendBlock = RealSuspendBlock;
196 const bool IsFinalSuspend = (Kind == AwaitKind::Final);
197 llvm::Function *CoroSuspend =
199 auto *SuspendResult =
Builder.CreateCall(
200 CoroSuspend, {SaveCall,
Builder.getInt1(IsFinalSuspend)});
204 Switch->addCase(
Builder.getInt8(0), ReadyBlock);
205 Switch->addCase(
Builder.getInt8(1), CleanupBlock);
226 ignoreResult,
false).RV;
232 aggSlot, ignoreResult,
false).RV;
248 assert(isa<CallExpr>(RE) &&
"unexpected suspend expression type");
249 return cast<CallExpr>(RE)->getCallReturnType(Ctx);
256 "Can't have a scalar return unless the return type is a "
266 "Can't have a scalar return unless the return type is a "
275 struct GetParamRef :
public StmtVisitor<GetParamRef> {
280 assert(
Expr ==
nullptr &&
"multilple declref in param move");
283 void VisitStmt(
Stmt *
S) {
297 struct ParamReferenceReplacerRAII {
302 : LocalDeclMap(LocalDeclMap) {}
309 Expr const *InitExpr = VD->getInit();
311 Visitor.Visit(const_cast<Expr*>(InitExpr));
312 assert(Visitor.Expr);
313 auto *DREOrig = cast<DeclRefExpr>(Visitor.Expr);
314 auto *PD = DREOrig->getDecl();
316 auto it = LocalDeclMap.find(PD);
317 assert(it != LocalDeclMap.end() &&
"parameter is not found");
318 SavedLocals.insert({ PD, it->second });
320 auto copyIt = LocalDeclMap.find(VD);
321 assert(copyIt != LocalDeclMap.end() &&
"parameter copy is not found");
322 it->second = copyIt->getSecond();
325 ~ParamReferenceReplacerRAII() {
326 for (
auto&& SavedLocal : SavedLocals) {
327 LocalDeclMap.insert({SavedLocal.first, SavedLocal.second});
340 BundleList.emplace_back(
"funclet", EHPad);
352 auto *NullPtr = llvm::ConstantPointerNull::get(CGF.
Int8PtrTy);
353 llvm::Function *CoroEndFn =
CGM.
getIntrinsic(llvm::Intrinsic::coro_end);
356 auto *CoroEnd = CGF.
Builder.CreateCall(
357 CoroEndFn, {NullPtr, CGF.
Builder.getTrue()}, Bundles);
358 if (Bundles.empty()) {
363 CGF.
Builder.CreateCondBr(CoroEnd, ResumeBB, CleanupContBB);
386 BasicBlock *SaveInsertBlock = CGF.
Builder.GetInsertBlock();
398 CGF.
CGM.
Error(Deallocate->getLocStart(),
399 "Deallocation expressoin does not refer to coro.free");
404 auto *InsertPt = SaveInsertBlock->getTerminator();
405 CoroFree->moveBefore(InsertPt);
406 CGF.
Builder.SetInsertPoint(InsertPt);
409 auto *NullPtr = llvm::ConstantPointerNull::get(CGF.
Int8PtrTy);
410 auto *Cond = CGF.
Builder.CreateICmpNE(CoroFree, NullPtr);
411 CGF.
Builder.CreateCondBr(Cond, FreeBB, AfterFreeBB);
414 InsertPt->eraseFromParent();
415 CGF.
Builder.SetInsertPoint(AfterFreeBB);
417 explicit CallCoroDelete(
Stmt *DeallocStmt) : Deallocate(DeallocStmt) {}
422 struct GetReturnObjectManager {
440 void EmitGroAlloca() {
441 auto *GroDeclStmt = dyn_cast<
DeclStmt>(
S.getResultDecl());
447 auto *GroVarDecl = cast<VarDecl>(GroDeclStmt->getSingleDecl());
464 if (
auto *Cleanup = dyn_cast<EHCleanupScope>(&*b)) {
465 assert(!Cleanup->hasActiveFlag() &&
"cleanup already has active flag?");
466 Cleanup->setActiveFlag(GroActiveFlag);
467 Cleanup->setTestFlagInEHCleanup();
468 Cleanup->setTestFlagInNormalCleanup();
474 if (!GroActiveFlag.isValid()) {
490 const bool CanFallthrough = CGF.
Builder.GetInsertBlock();
497 auto *NullPtr = llvm::ConstantPointerNull::get(
Builder.getInt8PtrTy());
499 unsigned NewAlign = TI.
getNewAlign() / TI.getCharWidth();
501 auto *EntryBB =
Builder.GetInsertBlock();
507 auto *CoroId =
Builder.CreateCall(
509 {
Builder.getInt32(NewAlign), NullPtr, NullPtr, NullPtr});
515 auto *CoroAlloc =
Builder.CreateCall(
518 Builder.CreateCondBr(CoroAlloc, AllocBB, InitBB);
522 auto *AllocOrInvokeContBB =
Builder.GetInsertBlock();
529 auto *NullPtr = llvm::ConstantPointerNull::get(
Int8PtrTy);
530 auto *Cond =
Builder.CreateICmpNE(AllocateCall, NullPtr);
531 Builder.CreateCondBr(Cond, InitBB, RetOnFailureBB);
545 Phi->addIncoming(NullPtr, EntryBB);
546 Phi->addIncoming(AllocateCall, AllocOrInvokeContBB);
547 auto *CoroBegin =
Builder.CreateCall(
551 GetReturnObjectManager GroManager(*
this, S);
552 GroManager.EmitGroAlloca();
556 ParamReferenceReplacerRAII ParamReplacer(LocalDeclMap);
565 ParamReplacer.addCopy(cast<DeclStmt>(PM));
574 auto *PromiseAddrVoidPtr =
575 new llvm::BitCastInst(PromiseAddr.getPointer(),
VoidPtrTy,
"", CoroId);
578 CoroId->setArgOperand(1, PromiseAddrVoidPtr);
581 GroManager.EmitGroInit();
589 CurCoro.
Data->CurrentAwaitKind = AwaitKind::Normal;
605 const bool CanFallthrough =
Builder.GetInsertBlock();
606 const bool HasCoreturns =
CurCoro.
Data->CoreturnCount > 0;
607 if (CanFallthrough || HasCoreturns) {
636 case llvm::Intrinsic::coro_frame: {
641 "has been used earlier in this function");
642 auto NullPtr = llvm::ConstantPointerNull::get(
Builder.getInt8PtrTy());
648 case llvm::Intrinsic::coro_alloc:
649 case llvm::Intrinsic::coro_begin:
650 case llvm::Intrinsic::coro_free: {
656 " been used earlier in this function");
662 case llvm::Intrinsic::coro_suspend:
670 llvm::CallInst *Call =
Builder.CreateCall(F, Args);
676 if (IID == llvm::Intrinsic::coro_id) {
679 else if (IID == llvm::Intrinsic::coro_begin) {
683 else if (IID == llvm::Intrinsic::coro_free) {
void EmitCoroutineBody(const CoroutineBodyStmt &S)
Stmt * getReturnStmt() const
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.
Expr * getResumeExpr() const
RValue EmitCoawaitExpr(const CoawaitExpr &E, AggValueSlot aggSlot=AggValueSlot::ignored(), bool ignoreResult=false)
Address GetAddrOfLocalVar(const VarDecl *VD)
GetAddrOfLocalVar - Return the address of a local variable.
VarDecl - An instance of this class is created to represent a variable declaration or definition...
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 ...
Stmt * getFinalSuspendStmt() const
OpaqueValueExpr * getOpaqueValue() const
getOpaqueValue - Return the opaque value placeholder.
LValue EmitCoyieldLValue(const CoyieldExpr *E)
Stmt * getFallthroughHandler() const
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.
const TargetInfo & getTargetInfo() const
unsigned getNewAlign() const
Return the largest alignment for which a suitably-sized allocation with '::operator new(size_t)' is g...
llvm::PointerType * VoidPtrTy
static CXXTryStmt * Create(const ASTContext &C, SourceLocation tryLoc, Stmt *tryBlock, ArrayRef< Stmt * > handlers)
void EmitStmt(const Stmt *S)
EmitStmt - Emit the code for the statement.
llvm::BasicBlock * createBasicBlock(const Twine &name="", llvm::Function *parent=nullptr, llvm::BasicBlock *before=nullptr)
createBasicBlock - Create an LLVM basic block.
VarDecl * getPromiseDecl() 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...
iterator find(stable_iterator save) const
Turn a stable reference to a scope depth into a unstable pointer to the EH stack. ...
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.
Stmt * getPromiseDeclStmt() const
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 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...
ASTContext & getContext() const
stable_iterator stable_begin() const
Create a stable reference to the top of the EH stack.
llvm::LLVMContext & getLLVMContext()
SourceLocation getLocStart() const LLVM_READONLY
Stmt * getInitSuspendStmt() const
llvm::Function * getIntrinsic(unsigned IID, ArrayRef< llvm::Type * > Tys=None)
Stmt * getReturnStmtOnAllocFailure() const
Expr * getReadyExpr() const
ArrayRef< Stmt const * > getParamMoves() const
CodeGenFunction::JumpDest FinalJD
ASTContext & getContext() const
std::unique_ptr< CGCoroData > Data
bool isSingleDecl() const
isSingleDecl - This method returns true if this DeclStmt refers to a single Decl. ...
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. ...
Expr * getSuspendExpr() const
CodeGenFunction::JumpDest CleanupJD
StmtVisitor - This class implements a simple visitor for Stmt subclasses.
llvm::BasicBlock * SuspendBB
llvm::BasicBlock * getEHResumeBlock(bool isCleanup)
llvm::Instruction * CurrentFuncletPad
JumpDest getJumpDestInCurrentScope(llvm::BasicBlock *Target)
The given basic block lies in the current EH scope, but may be a target of a potentially scope-crossi...
Expr * getDeallocate() const
Expr * getCommonExpr() const
Expr * getAllocate() const
void Error(SourceLocation loc, StringRef error)
Emit a general error that something can't be done.
AwaitKind CurrentAwaitKind
const Decl * getSingleDecl() const
llvm::Value * EmitScalarExpr(const Expr *E, bool IgnoreResultAssign=false)
EmitScalarExpr - Emit the computation of the specified expression of LLVM scalar type, returning the result.
Stmt * getBody() const
Retrieve the body of the coroutine as written.
static AggValueSlot ignored()
ignored - Returns an aggregate value slot indicating that the aggregate value is being ignored...
Represents a 'co_yield' expression.
detail::InMemoryDirectory::const_iterator E
llvm::StoreInst * CreateStore(llvm::Value *Val, Address Addr, bool IsVolatile=false)
CallExpr const * CoroIdExpr
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.
Stmt * getExceptionHandler() const
llvm::CallInst * LastCoroFree
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.
void EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock=false)
Represents an expression that might suspend coroutine execution; either a co_await or co_yield expres...
static OpaqueValueMappingData bind(CodeGenFunction &CGF, const OpaqueValueExpr *ov, const Expr *e)
LValue EmitCoawaitLValue(const CoawaitExpr *E)
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]).
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.
Information for lazily generating a cleanup.
llvm::CallInst * CoroBegin