14#include "mlir/Support/LLVM.h"
63struct GetParamRef :
public StmtVisitor<GetParamRef> {
68 assert(
expr ==
nullptr &&
"multilple declref in param move");
71 void VisitStmt(
Stmt *
s) {
72 for (
Stmt *
c :
s->children()) {
82struct ParamReferenceReplacerRAII {
87 : localDeclMap(localDeclMap) {}
89 void addCopy(
const DeclStmt *pm) {
93 const VarDecl *vd =
static_cast<const VarDecl *
>(pm->
getSingleDecl());
94 const Expr *initExpr = vd->
getInit();
96 visitor.Visit(
const_cast<Expr *
>(initExpr));
98 DeclRefExpr *dreOrig = visitor.expr;
101 auto it = localDeclMap.find(pd);
102 assert(it != localDeclMap.end() &&
"parameter is not found");
103 savedLocals.insert({pd, it->second});
105 auto copyIt = localDeclMap.find(vd);
106 assert(copyIt != localDeclMap.end() &&
"parameter copy is not found");
107 it->second = copyIt->getSecond();
110 ~ParamReferenceReplacerRAII() {
111 for (
auto &&savedLocal : savedLocals) {
112 localDeclMap.insert({savedLocal.first, savedLocal.second});
120struct CallCoroDelete final :
public EHScopeStack::Cleanup {
130 void emit(CIRGenFunction &cgf, Flags)
override {
135 if (cgf.
emitStmt(deallocate,
true).failed()) {
136 cgf.
cgm.
error(deallocate->getBeginLoc(),
137 "failed to emit coroutine deallocation expression");
142 cir::CallOp coroFree = cgf.
curCoro.
data->lastCoroFree;
145 cgf.
cgm.
error(deallocate->getBeginLoc(),
146 "Deallocation expression does not refer to coro.free");
150 builder.setInsertionPointAfter(coroFree);
153 llvm::SmallVector<mlir::Operation *> opsToMove;
154 mlir::Block *block = builder.getInsertionBlock();
155 mlir::Block::iterator it(isPtrNotNull.getDefiningOp());
157 for (++it; it != block->end(); ++it)
158 opsToMove.push_back(&*it);
161 cir::IfOp::create(builder, cgf.
getLoc(deallocate->getSourceRange()),
163 [&](mlir::OpBuilder &builder, mlir::Location loc) {
164 cir::YieldOp::create(builder, loc);
167 mlir::Operation *yieldOp = ifOp.getThenRegion().back().getTerminator();
168 for (
auto *op : opsToMove)
169 op->moveBefore(yieldOp);
171 explicit CallCoroDelete(Stmt *deallocStmt) : deallocate(deallocStmt) {}
185 cir::CallOp coroId) {
186 assert(!
curCoro.
data &&
"EmitCoroutineBodyStatement called twice?");
192static mlir::LogicalResult
196 if (cgf.
emitStmt(body,
true).failed())
197 return mlir::failure();
206 const bool canFallthrough = !currLexScope->
hasCoreturn();
208 if (
Stmt *onFallthrough =
s.getFallthroughHandler())
209 if (cgf.
emitStmt(onFallthrough,
true).failed())
210 return mlir::failure();
212 return mlir::success();
216 mlir::Value nullPtr) {
217 cir::IntType int32Ty = builder.getUInt32Ty();
222 mlir::Operation *builtin =
cgm.getGlobalValue(
cgm.builtinCoroId);
226 fnOp =
cgm.createCIRBuiltinFunction(
227 loc,
cgm.builtinCoroId,
228 cir::FuncType::get({int32Ty, voidPtrTy, voidPtrTy, voidPtrTy}, int32Ty),
230 assert(fnOp &&
"should always succeed");
235 return builder.createCallOp(loc, fnOp,
236 mlir::ValueRange{builder.getUInt32(newAlign, loc),
237 nullPtr, nullPtr, nullPtr});
241 cir::BoolType boolTy = builder.getBoolTy();
243 mlir::Operation *builtin =
cgm.getGlobalValue(
cgm.builtinCoroAlloc);
247 fnOp =
cgm.createCIRBuiltinFunction(loc,
cgm.builtinCoroAlloc,
248 cir::FuncType::get({uInt32Ty}, boolTy),
250 assert(fnOp &&
"should always succeed");
255 return builder.createCallOp(
256 loc, fnOp, mlir::ValueRange{
curCoro.data->coroId.getResult()});
261 mlir::Value coroframeAddr) {
262 mlir::Operation *builtin =
cgm.getGlobalValue(
cgm.builtinCoroBegin);
266 fnOp =
cgm.createCIRBuiltinFunction(
267 loc,
cgm.builtinCoroBegin,
268 cir::FuncType::get({uInt32Ty, voidPtrTy},
voidPtrTy),
270 assert(fnOp &&
"should always succeed");
275 return builder.createCallOp(
277 mlir::ValueRange{
curCoro.data->coroId.getResult(), coroframeAddr});
281 mlir::Value nullPtr) {
282 cir::BoolType boolTy = builder.getBoolTy();
283 mlir::Operation *builtin =
cgm.getGlobalValue(
cgm.builtinCoroEnd);
287 fnOp =
cgm.createCIRBuiltinFunction(
288 loc,
cgm.builtinCoroEnd,
289 cir::FuncType::get({voidPtrTy, boolTy}, boolTy),
291 assert(fnOp &&
"should always succeed");
296 return builder.createCallOp(
297 loc, fnOp, mlir::ValueRange{nullPtr, builder.getBool(
false, loc)});
301 mlir::Operation *builtin =
cgm.getGlobalValue(
cgm.builtinCoroFree);
305 fnOp =
cgm.createCIRBuiltinFunction(
306 loc,
cgm.builtinCoroFree,
307 cir::FuncType::get({uInt32Ty, voidPtrTy},
voidPtrTy),
309 assert(fnOp &&
"should always succeed");
313 cir::CallOp coroFree =
314 builder.createCallOp(loc, fnOp,
315 mlir::ValueRange{
curCoro.data->coroId.getResult(),
318 curCoro.data->lastCoroFree = coroFree;
322static mlir::LogicalResult
328 s.getBody(), &catchStmt);
336 ~handlerEmitter()
override =
default;
346 mlir::Location openCurlyLoc =
getLoc(
s.getBeginLoc());
347 cir::ConstantOp nullPtrCst = builder.getNullPtr(
voidPtrTy, openCurlyLoc);
349 auto fn = mlir::cast<cir::FuncOp>(
curFn);
350 fn.setCoroutine(
true);
363 openCurlyLoc,
"__coro_frame_addr",
366 mlir::Value storeAddr = coroFrame.
getPointer();
367 builder.CIRBaseBuilderTy::createStore(openCurlyLoc, nullPtrCst, storeAddr);
369 builder, openCurlyLoc, coroAlloc.getResult(),
371 [&](mlir::OpBuilder &
b, mlir::Location loc) {
372 builder.CIRBaseBuilderTy::createStore(
373 loc, emitScalarExpr(s.getAllocate()), storeAddr);
374 cir::YieldOp::create(builder, loc);
379 cir::LoadOp::create(builder, openCurlyLoc, allocaTy, storeAddr))
383 if (
s.getReturnStmtOnAllocFailure())
384 cgm.errorNYI(
"handle coroutine return alloc failure");
394 assert((paramMoves.size() == 0 || (paramMoves.size() ==
fnArgs.size())) &&
395 "ParamMoves and FnArgs should be the same size for coroutine "
404 for (
auto *pm : paramMoves) {
406 return mlir::failure();
410 if (
emitStmt(
s.getPromiseDeclStmt(),
true).failed())
411 return mlir::failure();
414 assert(
returnValue.isValid() == (
bool)
s.getReturnStmt());
426 s.getReturnValue()->getType().getQualifiers(),
431 curCoro.data->currentAwaitKind = cir::AwaitKind::Init;
432 curCoro.data->exceptionHandler =
s.getExceptionHandler();
434 if (
emitStmt(
s.getInitSuspendStmt(),
true).failed())
435 return mlir::failure();
437 curCoro.data->currentAwaitKind = cir::AwaitKind::User;
439 mlir::OpBuilder::InsertPoint userBody;
441 cir::CoroBodyOp::create(builder, openCurlyLoc,
442 [&](mlir::OpBuilder &
b, mlir::Location loc) {
443 userBody =
b.saveInsertionPoint();
446 mlir::OpBuilder::InsertionGuard guard(builder);
447 builder.restoreInsertionPoint(userBody);
448 if (
curCoro.data->exceptionHandler) {
464 if (
curCoro.data->resumeEHVar.isValid()) {
465 mlir::Value shouldSkip = builder.createFlagLoad(
466 openCurlyLoc,
curCoro.data->resumeEHVar.getPointer());
467 mlir::LogicalResult res = mlir::success();
468 cir::IfOp::create(builder, openCurlyLoc, shouldSkip,
470 [&](mlir::OpBuilder &
b, mlir::Location loc) {
472 builder.createYield(openCurlyLoc);
476 return mlir::failure();
479 return mlir::failure();
483 return mlir::failure();
487 mlir::Block &coroBodyBlock = coroBodyOp.getBody().back();
488 if (!coroBodyBlock.mightHaveTerminator()) {
489 mlir::OpBuilder::InsertionGuard guard(builder);
490 builder.setInsertionPointToEnd(&coroBodyBlock);
491 cir::YieldOp::create(builder, openCurlyLoc);
502 const bool canFallthrough =
curLexScope->hasCoreturn();
503 const bool hasCoreturns =
curCoro.data->coreturnCount > 0;
504 if (canFallthrough || hasCoreturns) {
505 curCoro.data->currentAwaitKind = cir::AwaitKind::Final;
507 mlir::OpBuilder::InsertionGuard guard(builder);
508 if (
emitStmt(
s.getFinalSuspendStmt(),
true)
510 return mlir::failure();
516 openCurlyLoc, builder.getNullPtr(builder.getVoidPtrTy(), openCurlyLoc));
517 if (
auto *ret = cast_or_null<ReturnStmt>(
s.getReturnStmt())) {
520 Expr *previousRetValue = ret->getRetValue();
521 ret->setRetValue(
nullptr);
523 return mlir::failure();
526 ret->setRetValue(previousRetValue);
528 return mlir::success();
532 if (
const auto *ce = dyn_cast<CXXMemberCallExpr>(e))
533 if (
const auto *proto =
558struct LValueOrRValue {
568 mlir::Block *scopeParentBlock,
569 mlir::Value &tmpResumeRValAddr,
bool forLValue) {
570 [[maybe_unused]] mlir::LogicalResult awaitBuild = mlir::success();
571 LValueOrRValue awaitRes;
576 [[maybe_unused]] cir::AwaitOp awaitOp = cir::AwaitOp::create(
579 [&](mlir::OpBuilder &
b, mlir::Location loc) {
580 Expr *condExpr = s.getReadyExpr()->IgnoreParens();
581 builder.createCondition(cgf.evaluateExprAsBool(condExpr));
584 [&](mlir::OpBuilder &
b, mlir::Location loc) {
590 mlir::Value suspendRet = cgf.emitScalarExpr(s.getSuspendExpr());
594 cgf.cgm.errorNYI(
"Veto await_suspend");
598 cir::YieldOp::create(builder, loc);
601 [&](mlir::OpBuilder &
b, mlir::Location loc) {
605 if (coro.exceptionHandler &&
kind == cir::AwaitKind::Init &&
618 SourceLocation resumeLoc =
s.getResumeExpr()->getExprLoc();
619 mlir::Location mlirLoc = cgf.
getLoc(resumeLoc);
623 builder.createFlagStore(mlirLoc,
false,
624 coro.resumeEHVar.getPointer());
626 CXXCatchStmt catchStmt(resumeLoc,
627 nullptr, coro.exceptionHandler);
630 resumeLoc, resumeLoc);
631 CXXTryStmt *tryStmt =
634 struct resumeEmitter final : CIRGenFunction::cxxTryBodyEmitter {
635 const CXXTryStmt &tryStmt;
637 mlir::Value resumeEHVar;
638 resumeEmitter(
const CXXTryStmt &tryStmt, mlir::Location loc,
640 : tryStmt(tryStmt), loc(loc),
641 resumeEHVar(resumeEHVar.getPointer()) {}
643 mlir::LogicalResult operator()(CIRGenFunction &cgf)
override {
644 mlir::LogicalResult res =
645 cgf.
emitStmt(tryStmt.getTryBlock(),
true);
650 ~resumeEmitter()
override =
default;
651 } emitter{*tryStmt, mlirLoc, coro.resumeEHVar};
657 }
else if (forLValue) {
663 cgf.
emitAnyExpr(
s.getResumeExpr(), aggSlot, ignoreResult);
664 if (!awaitRes.rv.isIgnored()) {
668 RValue rv = awaitRes.rv;
674 cgf.
cgm.
errorNYI(
"emitSuspendExpression: Aggregate value");
680 builder.getBestAllocaInsertPoint(scopeParentBlock));
682 builder.CIRBaseBuilderTy::createStore(loc, value,
688 cir::YieldOp::create(builder, loc);
691 assert(awaitBuild.succeeded() &&
"Should know how to codegen");
709 [[maybe_unused]] mlir::Value tmpResumeRValAddr;
714 ignoreResult, currEntryBlock, tmpResumeRValAddr,
755 const Expr *rv =
s.getOperand();
763 if (
emitStmt(
s.getPromiseCall(),
true).failed())
764 return mlir::failure();
768 mlir::Location loc =
getLoc(
s.getSourceRange());
769 cir::CoReturnOp::create(builder, loc);
771 return mlir::success();
static void emit(Program &P, llvm::SmallVectorImpl< std::byte > &Code, const T &Val, bool &Success)
Helper to write bytecode and bail out if 32-bit offsets become invalid.
static LValueOrRValue emitSuspendExpression(CIRGenFunction &cgf, CGCoroData &coro, CoroutineSuspendExpr const &s, cir::AwaitKind kind, AggValueSlot aggSlot, bool ignoreResult, mlir::Block *scopeParentBlock, mlir::Value &tmpResumeRValAddr, bool forLValue)
static RValue emitSuspendExpr(CIRGenFunction &cgf, const CoroutineSuspendExpr &e, cir::AwaitKind kind, AggValueSlot aggSlot, bool ignoreResult)
static bool memberCallExpressionCanThrow(const Expr *e)
static mlir::LogicalResult coroutineBodyExceptionHelper(CIRGenFunction &cgf, const CoroutineBodyStmt &s)
static mlir::LogicalResult emitBodyAndFallthrough(CIRGenFunction &cgf, const CoroutineBodyStmt &s, Stmt *body, const CIRGenFunction::LexicalScope *currLexScope)
static void createCoroData(CIRGenFunction &cgf, CIRGenFunction::CGCoroInfo &curCoro, cir::CallOp coroId)
*collection of selector each with an associated kind and an ordered *collection of selectors A selector has a kind
__device__ __2f16 float __ockl_bool s
__device__ __2f16 float c
cir::StoreOp createFlagStore(mlir::Location loc, bool val, mlir::Value dst)
mlir::Value createPtrIsNotNull(mlir::Value ptr)
CharUnits getTypeAlignInChars(QualType T) const
Return the ABI-specified alignment of a (complete) type T, in characters.
mlir::Value getPointer() const
An RAII object to set (and then clear) a mapping for an OpaqueValueExpr.
Enters a new scope for capturing cleanups, all of which will be executed once the scope is exited.
cir::CallOp emitCoroEndBuiltinCall(mlir::Location loc, mlir::Value nullPtr)
cir::CallOp emitCoroIDBuiltinCall(mlir::Location loc, mlir::Value nullPtr)
mlir::LogicalResult emitCoreturnStmt(const CoreturnStmt &s)
cir::AllocaOp createTempAlloca(mlir::Type ty, mlir::Location loc, const Twine &name="tmp", mlir::Value arraySize=nullptr, bool insertIntoFnEntryBlock=false)
This creates an alloca and inserts it into the entry block if ArraySize is nullptr,...
llvm::DenseMap< const clang::Decl *, Address > DeclMapTy
LValue emitLValue(const clang::Expr *e)
Emit code to compute a designator that specifies the location of the expression.
mlir::Location getLoc(clang::SourceLocation srcLoc)
Helpers to convert Clang's SourceLocation to a MLIR Location.
void emitAnyExprToMem(const Expr *e, Address location, Qualifiers quals, bool isInitializer)
Emits the code necessary to evaluate an arbitrary expression into the given memory location.
cir::CallOp emitCoroFreeBuiltin(const CallExpr *e)
RValue emitCoyieldExpr(const CoyieldExpr &e, AggValueSlot aggSlot=AggValueSlot::ignored(), bool ignoreResult=false)
mlir::Operation * curFn
The current function or global initializer that is generated code for.
EHScopeStack ehStack
Tracks function scope overall cleanup handling.
llvm::SmallVector< const ParmVarDecl * > fnArgs
Save Parameter Decl for coroutine.
mlir::Type convertTypeForMem(QualType t)
mlir::LogicalResult emitCXXTryStmt(const clang::CXXTryStmt &s, cxxTryBodyEmitter &bodyCallback)
cir::CallOp emitCoroAllocBuiltinCall(mlir::Location loc)
RValue emitCoroutineFrame()
mlir::Value emitAlloca(llvm::StringRef name, mlir::Type ty, mlir::Location loc, clang::CharUnits alignment, bool insertIntoFnEntryBlock, mlir::Value arraySize=nullptr)
Address returnValue
The temporary alloca to hold the return value.
CIRGenBuilderTy & getBuilder()
DeclMapTy localDeclMap
This keeps track of the CIR allocas or globals for local C declarations.
RValue emitCoawaitExpr(const CoawaitExpr &e, AggValueSlot aggSlot=AggValueSlot::ignored(), bool ignoreResult=false)
RValue emitAnyExpr(const clang::Expr *e, AggValueSlot aggSlot=AggValueSlot::ignored(), bool ignoreResult=false)
Emit code to compute the specified expression which can have any type.
LexicalScope * curLexScope
clang::ASTContext & getContext() const
mlir::LogicalResult emitCoroutineBody(const CoroutineBodyStmt &s)
cir::CallOp emitCoroBeginBuiltinCall(mlir::Location loc, mlir::Value coroframeAddr)
mlir::LogicalResult emitStmt(const clang::Stmt *s, bool useCurrentScope, llvm::ArrayRef< const Attr * > attrs={})
void emitIgnoredExpr(const clang::Expr *e)
Emit code to compute the specified expression, ignoring the result.
DiagnosticBuilder errorNYI(SourceLocation, llvm::StringRef)
Helpers to emit "not yet implemented" error diagnostics.
void error(SourceLocation loc, llvm::StringRef error)
Emit a general error that something can't be done.
This trivial value class is used to represent the result of an expression that is evaluated.
static RValue get(mlir::Value v)
static RValue getComplex(mlir::Value v)
mlir::Value getValue() const
Return the value of this scalar value.
mlir::Value getComplexValue() const
Return the value of this complex value.
static RValue getIgnored()
CXXCatchStmt - This represents a C++ catch block.
static CXXTryStmt * Create(const ASTContext &C, SourceLocation tryLoc, CompoundStmt *tryBlock, ArrayRef< Stmt * > handlers)
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
SourceLocation getBeginLoc() const
static CharUnits One()
One - Construct a CharUnits quantity of one.
Represents a 'co_await' expression.
static CompoundStmt * Create(const ASTContext &C, ArrayRef< Stmt * > Stmts, FPOptionsOverride FPFeatures, SourceLocation LB, SourceLocation RB)
Represents a 'co_return' statement in the C++ Coroutines TS.
Represents the body of a coroutine.
Represents an expression that might suspend coroutine execution; either a co_await or co_yield expres...
Represents a 'co_yield' expression.
A reference to a declared variable, function, enum, etc.
bool isSingleDecl() const
isSingleDecl - This method returns true if this DeclStmt refers to a single Decl.
const Decl * getSingleDecl() const
This represents one expression.
Represents a prototype with parameter type info, e.g.
StmtVisitor - This class implements a simple visitor for Stmt subclasses.
Stmt - This represents one statement.
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Exposes information about the current target.
unsigned getNewAlign() const
Return the largest alignment for which a suitably-sized allocation with 'operator new(size_t)' is gua...
unsigned getCharWidth() const
const Expr * getInit() const
Defines the clang::TargetInfo interface.
const internal::VariadicDynCastAllOfMatcher< Stmt, Expr > expr
Matches expressions.
The JSON file list parser is used to communicate input to InstallAPI.
CanQual< Type > CanQualType
Represents a canonical, potentially-qualified type.
bool isa(CodeGen::Address addr)
bool isNoexceptExceptionSpec(ExceptionSpecificationType ESpecType)
U cast(CodeGen::Address addr)
static bool ehCleanupScope()
static bool coroOutsideFrameMD()
static bool generateDebugInfo()
cir::AwaitKind currentAwaitKind
std::unique_ptr< CGCoroData > data
Represents a scope, including function bodies, compound statements, and the substatements of if/while...
mlir::Block * getEntryBlock()
cir::PointerType voidPtrTy
void* in address space 0