14#include "mlir/Support/LLVM.h"
49struct GetParamRef :
public StmtVisitor<GetParamRef> {
54 assert(
expr ==
nullptr &&
"multilple declref in param move");
57 void VisitStmt(
Stmt *
s) {
58 for (
Stmt *
c :
s->children()) {
68struct ParamReferenceReplacerRAII {
73 : localDeclMap(localDeclMap) {}
75 void addCopy(
const DeclStmt *pm) {
79 const VarDecl *vd =
static_cast<const VarDecl *
>(pm->
getSingleDecl());
80 const Expr *initExpr = vd->
getInit();
82 visitor.Visit(
const_cast<Expr *
>(initExpr));
84 DeclRefExpr *dreOrig = visitor.expr;
87 auto it = localDeclMap.find(pd);
88 assert(it != localDeclMap.end() &&
"parameter is not found");
89 savedLocals.insert({pd, it->second});
91 auto copyIt = localDeclMap.find(vd);
92 assert(copyIt != localDeclMap.end() &&
"parameter copy is not found");
93 it->second = copyIt->getSecond();
96 ~ParamReferenceReplacerRAII() {
97 for (
auto &&savedLocal : savedLocals) {
98 localDeclMap.insert({savedLocal.first, savedLocal.second});
114 cir::CallOp coroId) {
115 assert(!
curCoro.
data &&
"EmitCoroutineBodyStatement called twice?");
122 mlir::Value nullPtr) {
123 cir::IntType int32Ty = builder.getUInt32Ty();
128 mlir::Operation *builtin =
cgm.getGlobalValue(
cgm.builtinCoroId);
132 fnOp =
cgm.createCIRBuiltinFunction(
133 loc,
cgm.builtinCoroId,
134 cir::FuncType::get({int32Ty, voidPtrTy, voidPtrTy, voidPtrTy}, int32Ty),
136 assert(fnOp &&
"should always succeed");
141 return builder.createCallOp(loc, fnOp,
142 mlir::ValueRange{builder.getUInt32(newAlign, loc),
143 nullPtr, nullPtr, nullPtr});
147 cir::BoolType boolTy = builder.getBoolTy();
149 mlir::Operation *builtin =
cgm.getGlobalValue(
cgm.builtinCoroAlloc);
153 fnOp =
cgm.createCIRBuiltinFunction(loc,
cgm.builtinCoroAlloc,
154 cir::FuncType::get({uInt32Ty}, boolTy),
156 assert(fnOp &&
"should always succeed");
161 return builder.createCallOp(
162 loc, fnOp, mlir::ValueRange{
curCoro.data->coroId.getResult()});
167 mlir::Value coroframeAddr) {
168 mlir::Operation *builtin =
cgm.getGlobalValue(
cgm.builtinCoroBegin);
172 fnOp =
cgm.createCIRBuiltinFunction(
173 loc,
cgm.builtinCoroBegin,
174 cir::FuncType::get({uInt32Ty, voidPtrTy},
voidPtrTy),
176 assert(fnOp &&
"should always succeed");
181 return builder.createCallOp(
183 mlir::ValueRange{
curCoro.data->coroId.getResult(), coroframeAddr});
188 mlir::Location openCurlyLoc =
getLoc(
s.getBeginLoc());
189 cir::ConstantOp nullPtrCst = builder.getNullPtr(
voidPtrTy, openCurlyLoc);
191 auto fn = mlir::cast<cir::FuncOp>(
curFn);
192 fn.setCoroutine(
true);
205 openCurlyLoc,
"__coro_frame_addr",
208 mlir::Value storeAddr = coroFrame.
getPointer();
209 builder.CIRBaseBuilderTy::createStore(openCurlyLoc, nullPtrCst, storeAddr);
211 builder, openCurlyLoc, coroAlloc.getResult(),
213 [&](mlir::OpBuilder &
b, mlir::Location loc) {
214 builder.CIRBaseBuilderTy::createStore(
215 loc, emitScalarExpr(s.getAllocate()), storeAddr);
216 cir::YieldOp::create(builder, loc);
221 cir::LoadOp::create(builder, openCurlyLoc, allocaTy, storeAddr))
225 if (
s.getReturnStmtOnAllocFailure())
226 cgm.errorNYI(
"handle coroutine return alloc failure");
234 assert((paramMoves.size() == 0 || (paramMoves.size() ==
fnArgs.size())) &&
235 "ParamMoves and FnArgs should be the same size for coroutine "
244 for (
auto *pm : paramMoves) {
246 return mlir::failure();
250 if (
emitStmt(
s.getPromiseDeclStmt(),
true).failed())
251 return mlir::failure();
254 assert(
returnValue.isValid() == (
bool)
s.getReturnStmt());
266 s.getReturnValue()->getType().getQualifiers(),
271 curCoro.data->currentAwaitKind = cir::AwaitKind::Init;
272 if (
emitStmt(
s.getInitSuspendStmt(),
true).failed())
273 return mlir::failure();
276 return mlir::success();
280 if (
const auto *ce = dyn_cast<CXXMemberCallExpr>(e))
281 if (
const auto *proto =
306struct LValueOrRValue {
316 mlir::Block *scopeParentBlock,
317 mlir::Value &tmpResumeRValAddr,
bool forLValue) {
318 [[maybe_unused]] mlir::LogicalResult awaitBuild = mlir::success();
319 LValueOrRValue awaitRes;
324 [[maybe_unused]] cir::AwaitOp awaitOp = cir::AwaitOp::create(
325 builder, cgf.
getLoc(
s.getSourceRange()), kind,
327 [&](mlir::OpBuilder &
b, mlir::Location loc) {
328 Expr *condExpr = s.getReadyExpr()->IgnoreParens();
329 builder.createCondition(cgf.evaluateExprAsBool(condExpr));
332 [&](mlir::OpBuilder &
b, mlir::Location loc) {
338 mlir::Value suspendRet = cgf.emitScalarExpr(s.getSuspendExpr());
342 cgf.cgm.errorNYI(
"Veto await_suspend");
346 cir::YieldOp::create(builder, loc);
349 [&](mlir::OpBuilder &
b, mlir::Location loc) {
353 CXXTryStmt *tryStmt =
nullptr;
354 if (coro.exceptionHandler && kind == cir::AwaitKind::Init &&
356 cgf.cgm.errorNYI(
"Coro resume Exception");
364 cgf.emitAnyExpr(
s.getResumeExpr(), aggSlot, ignoreResult);
365 if (!awaitRes.rv.isIgnored())
372 cgf.cgm.errorNYI(
"Coro tryStmt");
375 cir::YieldOp::create(builder, loc);
378 assert(awaitBuild.succeeded() &&
"Should know how to codegen");
396 [[maybe_unused]] mlir::Value tmpResumeRValAddr;
401 ignoreResult, currEntryBlock, tmpResumeRValAddr,
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 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.
cir::CallOp emitCoroIDBuiltinCall(mlir::Location loc, mlir::Value nullPtr)
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={})
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 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 isNoexceptExceptionSpec(ExceptionSpecificationType ESpecType)
U cast(CodeGen::Address addr)
static bool ehCleanupScope()
static bool coroCoReturn()
static bool emitBodyAndFallthrough()
static bool coroOutsideFrameMD()
static bool coroCoYield()
static bool generateDebugInfo()
cir::AwaitKind currentAwaitKind
std::unique_ptr< CGCoroData > data
mlir::Block * getEntryBlock()
cir::PointerType voidPtrTy
void* in address space 0