14#include "mlir/Support/LLVM.h"
58struct GetParamRef :
public StmtVisitor<GetParamRef> {
63 assert(
expr ==
nullptr &&
"multilple declref in param move");
66 void VisitStmt(
Stmt *
s) {
67 for (
Stmt *
c :
s->children()) {
77struct ParamReferenceReplacerRAII {
82 : localDeclMap(localDeclMap) {}
84 void addCopy(
const DeclStmt *pm) {
88 const VarDecl *vd =
static_cast<const VarDecl *
>(pm->
getSingleDecl());
89 const Expr *initExpr = vd->
getInit();
91 visitor.Visit(
const_cast<Expr *
>(initExpr));
93 DeclRefExpr *dreOrig = visitor.expr;
96 auto it = localDeclMap.find(pd);
97 assert(it != localDeclMap.end() &&
"parameter is not found");
98 savedLocals.insert({pd, it->second});
100 auto copyIt = localDeclMap.find(vd);
101 assert(copyIt != localDeclMap.end() &&
"parameter copy is not found");
102 it->second = copyIt->getSecond();
105 ~ParamReferenceReplacerRAII() {
106 for (
auto &&savedLocal : savedLocals) {
107 localDeclMap.insert({savedLocal.first, savedLocal.second});
123 cir::CallOp coroId) {
124 assert(!
curCoro.
data &&
"EmitCoroutineBodyStatement called twice?");
130static mlir::LogicalResult
134 if (cgf.
emitStmt(body,
true).failed())
135 return mlir::failure();
144 const bool canFallthrough = !currLexScope->
hasCoreturn();
146 if (
Stmt *onFallthrough =
s.getFallthroughHandler())
147 if (cgf.
emitStmt(onFallthrough,
true).failed())
148 return mlir::failure();
150 return mlir::success();
154 mlir::Value nullPtr) {
155 cir::IntType int32Ty = builder.getUInt32Ty();
160 mlir::Operation *builtin =
cgm.getGlobalValue(
cgm.builtinCoroId);
164 fnOp =
cgm.createCIRBuiltinFunction(
165 loc,
cgm.builtinCoroId,
166 cir::FuncType::get({int32Ty, voidPtrTy, voidPtrTy, voidPtrTy}, int32Ty),
168 assert(fnOp &&
"should always succeed");
173 return builder.createCallOp(loc, fnOp,
174 mlir::ValueRange{builder.getUInt32(newAlign, loc),
175 nullPtr, nullPtr, nullPtr});
179 cir::BoolType boolTy = builder.getBoolTy();
181 mlir::Operation *builtin =
cgm.getGlobalValue(
cgm.builtinCoroAlloc);
185 fnOp =
cgm.createCIRBuiltinFunction(loc,
cgm.builtinCoroAlloc,
186 cir::FuncType::get({uInt32Ty}, boolTy),
188 assert(fnOp &&
"should always succeed");
193 return builder.createCallOp(
194 loc, fnOp, mlir::ValueRange{
curCoro.data->coroId.getResult()});
199 mlir::Value coroframeAddr) {
200 mlir::Operation *builtin =
cgm.getGlobalValue(
cgm.builtinCoroBegin);
204 fnOp =
cgm.createCIRBuiltinFunction(
205 loc,
cgm.builtinCoroBegin,
206 cir::FuncType::get({uInt32Ty, voidPtrTy},
voidPtrTy),
208 assert(fnOp &&
"should always succeed");
213 return builder.createCallOp(
215 mlir::ValueRange{
curCoro.data->coroId.getResult(), coroframeAddr});
220 mlir::Location openCurlyLoc =
getLoc(
s.getBeginLoc());
221 cir::ConstantOp nullPtrCst = builder.getNullPtr(
voidPtrTy, openCurlyLoc);
223 auto fn = mlir::cast<cir::FuncOp>(
curFn);
224 fn.setCoroutine(
true);
237 openCurlyLoc,
"__coro_frame_addr",
240 mlir::Value storeAddr = coroFrame.
getPointer();
241 builder.CIRBaseBuilderTy::createStore(openCurlyLoc, nullPtrCst, storeAddr);
243 builder, openCurlyLoc, coroAlloc.getResult(),
245 [&](mlir::OpBuilder &
b, mlir::Location loc) {
246 builder.CIRBaseBuilderTy::createStore(
247 loc, emitScalarExpr(s.getAllocate()), storeAddr);
248 cir::YieldOp::create(builder, loc);
253 cir::LoadOp::create(builder, openCurlyLoc, allocaTy, storeAddr))
257 if (
s.getReturnStmtOnAllocFailure())
258 cgm.errorNYI(
"handle coroutine return alloc failure");
266 assert((paramMoves.size() == 0 || (paramMoves.size() ==
fnArgs.size())) &&
267 "ParamMoves and FnArgs should be the same size for coroutine "
276 for (
auto *pm : paramMoves) {
278 return mlir::failure();
282 if (
emitStmt(
s.getPromiseDeclStmt(),
true).failed())
283 return mlir::failure();
286 assert(
returnValue.isValid() == (
bool)
s.getReturnStmt());
298 s.getReturnValue()->getType().getQualifiers(),
303 curCoro.data->currentAwaitKind = cir::AwaitKind::Init;
304 if (
emitStmt(
s.getInitSuspendStmt(),
true).failed())
305 return mlir::failure();
307 curCoro.data->currentAwaitKind = cir::AwaitKind::User;
310 if (
s.getExceptionHandler())
313 return mlir::failure();
323 const bool canFallthrough =
curLexScope->hasCoreturn();
324 const bool hasCoreturns =
curCoro.data->coreturnCount > 0;
325 if (canFallthrough || hasCoreturns) {
326 curCoro.data->currentAwaitKind = cir::AwaitKind::Final;
328 mlir::OpBuilder::InsertionGuard guard(builder);
329 builder.setInsertionPoint(
curCoro.data->finalSuspendInsPoint);
330 if (
emitStmt(
s.getFinalSuspendStmt(),
true)
332 return mlir::failure();
336 return mlir::success();
340 if (
const auto *ce = dyn_cast<CXXMemberCallExpr>(e))
341 if (
const auto *proto =
366struct LValueOrRValue {
376 mlir::Block *scopeParentBlock,
377 mlir::Value &tmpResumeRValAddr,
bool forLValue) {
378 [[maybe_unused]] mlir::LogicalResult awaitBuild = mlir::success();
379 LValueOrRValue awaitRes;
384 [[maybe_unused]] cir::AwaitOp awaitOp = cir::AwaitOp::create(
385 builder, cgf.
getLoc(
s.getSourceRange()), kind,
387 [&](mlir::OpBuilder &
b, mlir::Location loc) {
388 Expr *condExpr = s.getReadyExpr()->IgnoreParens();
389 builder.createCondition(cgf.evaluateExprAsBool(condExpr));
392 [&](mlir::OpBuilder &
b, mlir::Location loc) {
398 mlir::Value suspendRet = cgf.emitScalarExpr(s.getSuspendExpr());
402 cgf.cgm.errorNYI(
"Veto await_suspend");
406 cir::YieldOp::create(builder, loc);
409 [&](mlir::OpBuilder &
b, mlir::Location loc) {
413 CXXTryStmt *tryStmt =
nullptr;
414 if (coro.exceptionHandler && kind == cir::AwaitKind::Init &&
416 cgf.cgm.errorNYI(
"Coro resume Exception");
424 cgf.emitAnyExpr(
s.getResumeExpr(), aggSlot, ignoreResult);
425 if (!awaitRes.rv.isIgnored())
432 cgf.cgm.errorNYI(
"Coro tryStmt");
435 cir::YieldOp::create(builder, loc);
438 assert(awaitBuild.succeeded() &&
"Should know how to codegen");
456 [[maybe_unused]] mlir::Value tmpResumeRValAddr;
461 ignoreResult, currEntryBlock, tmpResumeRValAddr,
493 const Expr *rv =
s.getOperand();
501 if (
emitStmt(
s.getPromiseCall(),
true).failed())
502 return mlir::failure();
506 mlir::Location loc =
getLoc(
s.getSourceRange());
507 mlir::Block *retBlock =
curLexScope->getOrCreateRetBlock(*
this, loc);
508 curCoro.data->finalSuspendInsPoint =
509 cir::BrOp::create(builder, loc, retBlock);
513 builder.createBlock(builder.getBlock()->getParent());
515 return mlir::success();
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 emitBodyAndFallthrough(CIRGenFunction &cgf, const CoroutineBodyStmt &s, Stmt *body, const CIRGenFunction::LexicalScope *currLexScope)
static void createCoroData(CIRGenFunction &cgf, CIRGenFunction::CGCoroInfo &curCoro, cir::CallOp coroId)
__device__ __2f16 float __ockl_bool s
__device__ __2f16 float c
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 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
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.
mlir::Operation * curFn
The current function or global initializer that is generated code for.
llvm::SmallVector< const ParmVarDecl * > fnArgs
Save Parameter Decl for coroutine.
mlir::Type convertTypeForMem(QualType t)
cir::CallOp emitCoroAllocBuiltinCall(mlir::Location loc)
RValue emitCoroutineFrame()
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)
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.
This trivial value class is used to represent the result of an expression that is evaluated.
static RValue get(mlir::Value v)
mlir::Value getValue() const
Return the value of this scalar value.
Represents a 'co_await' expression.
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...
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 coroCoReturn()
static bool coroutineExceptions()
static bool coroOutsideFrameMD()
static bool coroCoYield()
static bool generateDebugInfo()
cir::AwaitKind currentAwaitKind
mlir::Operation * finalSuspendInsPoint
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