clang 24.0.0git
CIRGenCoroutine.cpp
Go to the documentation of this file.
1//===----- CGCoroutine.cpp - Emit CIR Code for C++ coroutines -------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This contains code dealing with C++ code generation of coroutines.
10//
11//===----------------------------------------------------------------------===//
12
13#include "CIRGenFunction.h"
14#include "mlir/Support/LLVM.h"
15#include "clang/AST/StmtCXX.h"
21
22using namespace clang;
23using namespace clang::CIRGen;
24
26 // What is the current await expression kind and how many
27 // await/yield expressions were encountered so far.
28 // These are used to generate pretty labels for await expressions in LLVM IR.
29 cir::AwaitKind currentAwaitKind = cir::AwaitKind::Init;
30 // Stores the __builtin_coro_id emitted in the function so that we can supply
31 // it as the first argument to other builtins.
32 cir::CoroIdOp coroId = nullptr;
33
34 // Stores the result of __builtin_coro_begin call.
35 mlir::Value coroBegin = nullptr;
36
37 // How many co_return statements are in the coroutine. Used to decide whether
38 // we need to add co_return; equivalent at the end of the user authored body.
39 unsigned coreturnCount = 0;
40
41 // The promise type's 'unhandled_exception' handler, if it defines one.
43
44 // Stores the last emitted coro.free for the deallocate expressions, we use it
45 // to wrap dealloc code with if(auto mem = coro.free) dealloc(mem).
46 cir::CoroFreeOp lastCoroFree = nullptr;
47
48 // A temporary bool alloca that stores whether 'await_resume' threw an
49 // exception. If it did, 'true' is stored in this variable, and the coroutine
50 // body must be skipped. If the promise type does not define an exception
51 // handler, this is null.
53
54 // If coro.id came from the builtin, remember the expression to give better
55 // diagnostic. If CoroIdExpr is nullptr, the coro.id was created by
56 // EmitCoroutineBody.
57 CallExpr const *coroIdExpr = nullptr;
58};
59
60// Defining these here allows to keep CGCoroData private to this file.
63
64namespace {
65// FIXME: both GetParamRef and ParamReferenceReplacerRAII are good template
66// candidates to be shared among LLVM / CIR codegen.
67
68// Hunts for the parameter reference in the parameter copy/move declaration.
69struct GetParamRef : public StmtVisitor<GetParamRef> {
70public:
71 DeclRefExpr *expr = nullptr;
72 GetParamRef() {}
73 void VisitDeclRefExpr(DeclRefExpr *e) {
74 assert(expr == nullptr && "multilple declref in param move");
75 expr = e;
76 }
77 void VisitStmt(Stmt *s) {
78 for (Stmt *c : s->children()) {
79 if (c)
80 Visit(c);
81 }
82 }
83};
84
85// This class replaces references to parameters to their copies by changing
86// the addresses in CGF.LocalDeclMap and restoring back the original values in
87// its destructor.
88struct ParamReferenceReplacerRAII {
89 CIRGenFunction::DeclMapTy savedLocals;
90 CIRGenFunction::DeclMapTy &localDeclMap;
91
92 ParamReferenceReplacerRAII(CIRGenFunction::DeclMapTy &localDeclMap)
93 : localDeclMap(localDeclMap) {}
94
95 void addCopy(const DeclStmt *pm) {
96 // Figure out what param it refers to.
97
98 assert(pm->isSingleDecl());
99 const VarDecl *vd = static_cast<const VarDecl *>(pm->getSingleDecl());
100 const Expr *initExpr = vd->getInit();
101 GetParamRef visitor;
102 visitor.Visit(const_cast<Expr *>(initExpr));
103 assert(visitor.expr);
104 DeclRefExpr *dreOrig = visitor.expr;
105 auto *pd = dreOrig->getDecl();
106
107 auto it = localDeclMap.find(pd);
108 assert(it != localDeclMap.end() && "parameter is not found");
109 savedLocals.insert({pd, it->second});
110
111 auto copyIt = localDeclMap.find(vd);
112 assert(copyIt != localDeclMap.end() && "parameter copy is not found");
113 it->second = copyIt->getSecond();
114 }
115
116 ~ParamReferenceReplacerRAII() {
117 for (auto &&savedLocal : savedLocals) {
118 localDeclMap.insert({savedLocal.first, savedLocal.second});
119 }
120 }
121};
122} // namespace
123
124namespace {
125// Make sure to call coro.delete on scope exit.
126struct CallCoroDelete final : public EHScopeStack::Cleanup {
127 Stmt *deallocate;
128
129 // Emit "if (coro.free(CoroId, CoroBegin)) Deallocate;"
130
131 // Note: That deallocation will be emitted twice: once for a normal exit and
132 // once for exceptional exit. This usage is safe because Deallocate does not
133 // contain any declarations. The SubStmtBuilder::makeNewAndDeleteExpr()
134 // builds a single call to a deallocation function which is safe to emit
135 // multiple times.
136 void emit(CIRGenFunction &cgf, Flags) override {
137 // Remember the current point, as we are going to emit deallocation code
138 // first to get to coro.free instruction that is an argument to a delete
139 // call.
140
141 if (cgf.emitStmt(deallocate, /*useCurrentScope=*/true).failed()) {
142 cgf.cgm.error(deallocate->getBeginLoc(),
143 "failed to emit coroutine deallocation expression");
144 return;
145 }
146
147 CIRGenBuilderTy &builder = cgf.getBuilder();
148 cir::CoroFreeOp coroFree = cgf.curCoro.data->lastCoroFree;
149
150 if (!coroFree) {
151 cgf.cgm.error(deallocate->getBeginLoc(),
152 "Deallocation expression does not refer to coro.free");
153 return;
154 }
155
156 builder.setInsertionPointAfter(coroFree);
157 mlir::Value isPtrNotNull = builder.createPtrIsNotNull(coroFree.getResult());
158
159 llvm::SmallVector<mlir::Operation *> opsToMove;
160 mlir::Block *block = builder.getInsertionBlock();
161 mlir::Block::iterator it(isPtrNotNull.getDefiningOp());
162
163 for (++it; it != block->end(); ++it)
164 opsToMove.push_back(&*it);
165
166 auto ifOp =
167 cir::IfOp::create(builder, cgf.getLoc(deallocate->getSourceRange()),
168 isPtrNotNull, /*withElseRegion*/ false,
169 [&](mlir::OpBuilder &builder, mlir::Location loc) {
170 cir::YieldOp::create(builder, loc);
171 });
172
173 mlir::Operation *yieldOp = ifOp.getThenRegion().back().getTerminator();
174 for (auto *op : opsToMove)
175 op->moveBefore(yieldOp);
176 }
177 explicit CallCoroDelete(Stmt *deallocStmt) : deallocate(deallocStmt) {}
178};
179} // namespace
180
182 if (curCoro.data && curCoro.data->coroBegin) {
183 return RValue::get(curCoro.data->coroBegin);
184 }
185 cgm.errorNYI("NYI");
186 return RValue();
187}
188
191 cir::CoroIdOp coroId,
192 CallExpr const *coroIdExpr = nullptr) {
193
194 if (curCoro.data) {
195 if (curCoro.data->coroIdExpr)
196 cgf.cgm.error(coroIdExpr->getBeginLoc(),
197 "only one __builtin_coro_id can be used in a function");
198 else if (coroIdExpr)
199 cgf.cgm.error(coroIdExpr->getBeginLoc(),
200 "__builtin_coro_id shall not be used in a C++ coroutine");
201 else
202 llvm_unreachable("EmitCoroutineBodyStatement called twice?");
203
204 return;
205 }
206
207 curCoro.data = std::make_unique<CGCoroData>();
208 curCoro.data->coroId = coroId;
209 curCoro.data->coroIdExpr = coroIdExpr;
210}
211
212static mlir::LogicalResult
214 Stmt *body,
215 const CIRGenFunction::LexicalScope *currLexScope) {
216 if (cgf.emitStmt(body, /*useCurrentScope=*/true).failed())
217 return mlir::failure();
218 // Note that classic codegen checks CanFallthrough by looking into the
219 // availability of the insert block which is kinda brittle and unintuitive,
220 // seems to be related with how landing pads are handled.
221 //
222 // CIRGen handles this by checking pre-existing co_returns in the current
223 // scope instead.
224
225 // From LLVM IR Gen: const bool CanFallthrough = Builder.GetInsertBlock();
226 const bool canFallthrough = !currLexScope->hasCoreturn();
227 if (canFallthrough)
228 if (Stmt *onFallthrough = s.getFallthroughHandler())
229 if (cgf.emitStmt(onFallthrough, /*useCurrentScope=*/true).failed())
230 return mlir::failure();
231
232 return mlir::success();
233}
234
236 mlir::Location loc = getLoc(e->getBeginLoc());
237
239 for (const Expr *arg : e->arguments())
240 args.push_back(emitScalarExpr(arg));
241
242 auto coroId = cir::CoroIdOp::create(cgm.getBuilder(), loc, args);
243 createCoroData(*this, curCoro, coroId, e);
244 return coroId;
245}
246
248 mlir::Location loc = getLoc(e->getBeginLoc());
249 if (!curCoro.data || !curCoro.data->coroId) {
250 cgm.error(e->getBeginLoc(), "this builtin expect that __builtin_coro_id has"
251 " been used earlier in this function");
252 return {};
253 }
254
255 return cir::CoroAllocOp::create(
256 cgm.getBuilder(), loc,
257 mlir::ValueRange{curCoro.data->coroId.getResult()});
258}
259
261
262 mlir::Location loc = getLoc(e->getBeginLoc());
263 if (!curCoro.data || !curCoro.data->coroId) {
264 cgm.error(e->getBeginLoc(), "this builtin expect that __builtin_coro_id has"
265 " been used earlier in this function");
266 return {};
267 }
269 args.push_back(curCoro.data->coroId.getResult());
270 for (const Expr *arg : e->arguments())
271 args.push_back(emitScalarExpr(arg));
272
273 auto coroBegin = cir::CoroBeginOp::create(cgm.getBuilder(), loc, args);
274 curCoro.data->coroBegin = coroBegin;
275 return coroBegin;
276}
277
279
280 mlir::Location loc = getLoc(e->getBeginLoc());
281 CIRGenBuilderTy &builder = cgm.getBuilder();
283 for (const Expr *arg : e->arguments())
284 args.push_back(emitScalarExpr(arg));
285 args.push_back(cir::TokenNoneOp::create(builder, loc));
286 return cir::CoroEndOp::create(builder, loc, {cgm.voidTy}, args);
287}
288
290 mlir::Location loc = getLoc(e->getBeginLoc());
291
292 if (!curCoro.data || !curCoro.data->coroId) {
293 cgm.error(e->getBeginLoc(), "this builtin expect that __builtin_coro_id has"
294 " been used earlier in this function");
295 return {};
296 }
297
298 auto coroFree =
299 cir::CoroFreeOp::create(cgm.getBuilder(), loc,
300 mlir::ValueRange{curCoro.data->coroId.getResult(),
301 curCoro.data->coroBegin});
302
303 curCoro.data->lastCoroFree = coroFree;
304 return coroFree;
305}
306
308 mlir::Location loc = getLoc(e->getBeginLoc());
309 return cir::CoroSizeOp::create(cgm.getBuilder(), loc);
310}
311
312cir::CoroPromiseOp
314 mlir::Location loc = getLoc(e->getBeginLoc());
315
317 for (const Expr *arg : e->arguments())
318 args.push_back(emitScalarExpr(arg));
319
320 auto coroPromise = cir::CoroPromiseOp::create(cgm.getBuilder(), loc, args);
321 return coroPromise;
322}
323
324static mlir::LogicalResult
326
327 CXXCatchStmt catchStmt(s.getBeginLoc(), /*exDecl=*/nullptr,
328 cgf.curCoro.data->exceptionHandler);
329 auto *tryStmt = CXXTryStmt::Create(cgf.getContext(), s.getBeginLoc(),
330 s.getBody(), &catchStmt);
331 struct handlerEmitter final : CIRGenFunction::cxxTryBodyEmitter {
332 const CoroutineBodyStmt &s;
333
334 handlerEmitter(const CoroutineBodyStmt &s) : s(s) /*, scope(scope)*/ {}
335 mlir::LogicalResult operator()(CIRGenFunction &cgf) override {
336 return emitBodyAndFallthrough(cgf, s, s.getBody(), cgf.curLexScope);
337 }
338 ~handlerEmitter() override = default;
339 } emitter{s};
340
341 mlir::LogicalResult res = cgf.emitCXXTryStmt(*tryStmt, emitter);
342
343 return res;
344}
345
346mlir::LogicalResult
348 mlir::Location openCurlyLoc = getLoc(s.getBeginLoc());
349 cir::ConstantOp nullPtrCst = builder.getNullPtr(voidPtrTy, openCurlyLoc);
350
351 auto fn = mlir::cast<cir::FuncOp>(curFn);
352 fn.setCoroutine(true);
353 const TargetInfo &ti = cgm.getASTContext().getTargetInfo();
354 unsigned newAlign = ti.getNewAlign() / ti.getCharWidth();
355
356 cir::CoroIdOp coroId = cir::CoroIdOp::create(
357 cgm.getBuilder(), openCurlyLoc,
358 mlir::ValueRange{builder.getUInt32(newAlign, openCurlyLoc), nullPtrCst,
359 nullPtrCst, nullPtrCst});
360 createCoroData(*this, curCoro, coroId);
361
362 // Backend is allowed to elide memory allocations, to help it, emit
363 // auto mem = coro.alloc() ? 0 : ... allocation code ...;
364 cir::CoroAllocOp coroAlloc = cir::CoroAllocOp::create(
365 cgm.getBuilder(), openCurlyLoc,
366 mlir::ValueRange{curCoro.data->coroId.getResult()});
367
368 // Initialize address of coroutine frame to null
369 CanQualType astVoidPtrTy = cgm.getASTContext().VoidPtrTy;
370 mlir::Type allocaTy = convertTypeForMem(astVoidPtrTy);
371 Address coroFrame =
372 createTempAlloca(allocaTy, getContext().getTypeAlignInChars(astVoidPtrTy),
373 openCurlyLoc, "__coro_frame_addr",
374 /*ArraySize=*/nullptr);
375
376 mlir::Value storeAddr = coroFrame.getPointer();
377 builder.CIRBaseBuilderTy::createStore(openCurlyLoc, nullPtrCst, storeAddr);
378 mlir::LogicalResult res = mlir::success();
379 cir::IfOp::create(
380 builder, openCurlyLoc, coroAlloc.getResult(),
381 /*withElseRegion=*/false,
382 /*thenBuilder=*/[&](mlir::OpBuilder &b, mlir::Location loc) {
383 mlir::Value allocatedPtr = emitScalarExpr(s.getAllocate());
384 builder.CIRBaseBuilderTy::createStore(loc, allocatedPtr, storeAddr);
385 // Handle allocation failure if 'ReturnStmtOnAllocFailure' was provided.
386 if (Stmt *retOnAllocFailure = s.getReturnStmtOnAllocFailure()) {
387 mlir::Value isPtrNull = builder.createPtrIsNull(allocatedPtr);
388 assert(!cir::MissingFeatures::emitCondLikelihoodViaExpectIntrinsic());
389 cir::IfOp::create(builder, loc, isPtrNull, /*withElseRegion=*/false,
390 [&](mlir::OpBuilder &b, mlir::Location loc) {
391 res = emitStmt(retOnAllocFailure,
392 /*useCurrentScope=*/true);
393 cir::UnreachableOp::create(builder, loc);
394 });
395 }
396 cir::YieldOp::create(builder, loc);
397 });
398
399 if (res.failed())
400 return res;
401
402 curCoro.data->coroBegin = cir::CoroBeginOp::create(
403 cgm.getBuilder(), openCurlyLoc,
404 mlir::ValueRange{
405 curCoro.data->coroId.getResult(),
406 cir::LoadOp::create(builder, openCurlyLoc, allocaTy, storeAddr)});
407
408 {
410 ParamReferenceReplacerRAII paramReplacer(localDeclMap);
411 RunCleanupsScope resumeScope(*this);
412 ehStack.pushCleanup<CallCoroDelete>(NormalAndEHCleanup, s.getDeallocate());
413 // Create mapping between parameters and copy-params for coroutine
414 // function.
415 llvm::ArrayRef<const Stmt *> paramMoves = s.getParamMoves();
416 assert((paramMoves.size() == 0 || (paramMoves.size() == fnArgs.size())) &&
417 "ParamMoves and FnArgs should be the same size for coroutine "
418 "function");
419 // For zipping the arg map into debug info.
421
422 // Create parameter copies. We do it before creating a promise, since an
423 // evolution of coroutine TS may allow promise constructor to observe
424 // parameter copies.
426 for (auto *pm : paramMoves) {
427 if (emitStmt(pm, /*useCurrentScope=*/true).failed())
428 return mlir::failure();
429 paramReplacer.addCopy(cast<DeclStmt>(pm));
430 }
431
432 if (emitStmt(s.getPromiseDeclStmt(), /*useCurrentScope=*/true).failed())
433 return mlir::failure();
434 // returnValue should be valid as long as the coroutine's return type
435 // is not void. The assertion could help us to reduce the check later.
436 assert(returnValue.isValid() == (bool)s.getReturnStmt());
437 // Now we have the promise, initialize the GRO.
438 // We need to emit `get_return_object` first. According to:
439 // [dcl.fct.def.coroutine]p7
440 // The call to get_return_­object is sequenced before the call to
441 // initial_suspend and is invoked at most once.
442 //
443 // So we couldn't emit return value when we emit return statment,
444 // otherwise the call to get_return_object wouldn't be in front
445 // of initial_suspend.
446 if (returnValue.isValid())
447 emitAnyExprToMem(s.getReturnValue(), returnValue,
448 s.getReturnValue()->getType().getQualifiers(),
449 /*isInit*/ true);
450
452
453 curCoro.data->currentAwaitKind = cir::AwaitKind::Init;
454 curCoro.data->exceptionHandler = s.getExceptionHandler();
455
456 if (emitStmt(s.getInitSuspendStmt(), /*useCurrentScope=*/true).failed())
457 return mlir::failure();
458
459 curCoro.data->currentAwaitKind = cir::AwaitKind::User;
460
461 mlir::OpBuilder::InsertPoint userBody;
462 auto coroBodyOp =
463 cir::CoroBodyOp::create(builder, openCurlyLoc, /*scopeBuilder=*/
464 [&](mlir::OpBuilder &b, mlir::Location loc) {
465 userBody = b.saveInsertionPoint();
466 });
467 {
468 mlir::OpBuilder::InsertionGuard guard(builder);
469 builder.restoreInsertionPoint(userBody);
470 if (curCoro.data->exceptionHandler) {
471 // This bit of code is supposed to do:
472 //
473 // if (await-resume-didnt-throw-exception) {
474 // try {
475 // coroutine-body
476 // } catch (...) {
477 // unhandled_exception();
478 // }
479 // }
480 //
481 // IF resume couldn't have thrown an exception(await_resume is
482 // noexcept), we skip the 'if'.
483 //
484 // Note that we've reversed the condition of the 'if' from classic
485 // codegen so that we don't need an 'else' block.
486 if (curCoro.data->resumeEHVar.isValid()) {
487 mlir::Value shouldSkip = builder.createFlagLoad(
488 openCurlyLoc, curCoro.data->resumeEHVar.getPointer());
489 mlir::LogicalResult res = mlir::success();
490 cir::IfOp::create(builder, openCurlyLoc, shouldSkip,
491 /*withElseRegion=*/false,
492 [&](mlir::OpBuilder &b, mlir::Location loc) {
493 res = coroutineBodyExceptionHelper(*this, s);
494 builder.createYield(openCurlyLoc);
495 });
496
497 if (res.failed())
498 return mlir::failure();
499
500 } else if (coroutineBodyExceptionHelper(*this, s).failed()) {
501 return mlir::failure();
502 }
503 } else if (emitBodyAndFallthrough(*this, s, s.getBody(), curLexScope)
504 .failed()) {
505 return mlir::failure();
506 }
507 }
508
509 mlir::Block &coroBodyBlock = coroBodyOp.getBody().back();
510 if (!coroBodyBlock.mightHaveTerminator()) {
511 mlir::OpBuilder::InsertionGuard guard(builder);
512 builder.setInsertionPointToEnd(&coroBodyBlock);
513 cir::YieldOp::create(builder, openCurlyLoc);
514 }
515
516 // Note that LLVM checks CanFallthrough by looking into the availability
517 // of the insert block which is kinda brittle and unintuitive, seems to be
518 // related with how landing pads are handled.
519 //
520 // CIRGen handles this by checking pre-existing co_returns in the current
521 // scope instead.
522 //
523 // From LLVM IR Gen: const bool CanFallthrough = Builder.GetInsertBlock();
524 const bool canFallthrough = curLexScope->hasCoreturn();
525 const bool hasCoreturns = curCoro.data->coreturnCount > 0;
526 if (canFallthrough || hasCoreturns) {
527 curCoro.data->currentAwaitKind = cir::AwaitKind::Final;
528 {
529 mlir::OpBuilder::InsertionGuard guard(builder);
530 if (emitStmt(s.getFinalSuspendStmt(), /*useCurrentScope=*/true)
531 .failed())
532 return mlir::failure();
533 }
534 }
535 }
536
537 cir::ConstantOp nullHandler =
538 builder.getNullPtr(builder.getVoidPtrTy(), openCurlyLoc);
539 cir::ConstantOp noUnwind = builder.getBool(false, openCurlyLoc);
540 auto tkNone = cir::TokenNoneOp::create(builder, openCurlyLoc);
541 cir::CoroEndOp::create(builder, openCurlyLoc, nullHandler, noUnwind, tkNone);
542
543 if (auto *ret = cast_or_null<ReturnStmt>(s.getReturnStmt())) {
544 // Since we already emitted the return value above, so we shouldn't
545 // emit it again here.
546 Expr *previousRetValue = ret->getRetValue();
547 ret->setRetValue(nullptr);
548 if (emitStmt(ret, /*useCurrentScope=*/true).failed())
549 return mlir::failure();
550 // Set the return value back. The code generator, as the AST **Consumer**,
551 // shouldn't change the AST.
552 ret->setRetValue(previousRetValue);
553 }
554 return mlir::success();
555}
556
557static bool memberCallExpressionCanThrow(const Expr *e) {
558 if (const auto *ce = dyn_cast<CXXMemberCallExpr>(e))
559 if (const auto *proto =
560 ce->getMethodDecl()->getType()->getAs<FunctionProtoType>())
561 if (isNoexceptExceptionSpec(proto->getExceptionSpecType()) &&
562 proto->canThrow() == CT_Cannot)
563 return false;
564 return true;
565}
566
567// Given a suspend expression which roughly looks like:
568//
569// auto && x = CommonExpr();
570// if (!x.await_ready()) {
571// x.await_suspend(...); (*)
572// }
573// x.await_resume();
574//
575// where the result of the entire expression is the result of x.await_resume()
576//
577// (*) If x.await_suspend return type is bool, it allows to veto a suspend:
578// if (x.await_suspend(...))
579// llvm_coro_suspend();
580//
581// This is more higher level than LLVM codegen, for that one see llvm's
582// docs/Coroutines.md for more details.
583namespace {
584struct LValueOrRValue {
585 LValue lv;
586 RValue rv;
587};
588} // namespace
589
590static LValueOrRValue
592 CoroutineSuspendExpr const &s, cir::AwaitKind kind,
593 AggValueSlot aggSlot, bool ignoreResult,
594 mlir::Block *scopeParentBlock,
595 mlir::Value &tmpResumeRValAddr, bool forLValue) {
596 [[maybe_unused]] mlir::LogicalResult awaitBuild = mlir::success();
597 LValueOrRValue awaitRes;
598
601 CIRGenBuilderTy &builder = cgf.getBuilder();
602 [[maybe_unused]] cir::AwaitOp awaitOp = cir::AwaitOp::create(
603 builder, cgf.getLoc(s.getSourceRange()), kind,
604 /*readyBuilder=*/
605 [&](mlir::OpBuilder &b, mlir::Location loc) {
606 Expr *condExpr = s.getReadyExpr()->IgnoreParens();
607 builder.createCondition(cgf.evaluateExprAsBool(condExpr));
608 },
609 /*suspendBuilder=*/
610 [&](mlir::OpBuilder &b, mlir::Location loc) {
611 // Note that differently from LLVM codegen we do not emit coro.save
612 // and coro.suspend here, that should be done as part of lowering this
613 // to LLVM dialect (or some other MLIR dialect)
614
615 // A invalid suspendRet indicates "void returning await_suspend"
616 mlir::Value suspendRet = cgf.emitScalarExpr(s.getSuspendExpr());
617
618 // Veto suspension if requested by bool returning await_suspend.
619 if (suspendRet) {
620 cgf.cgm.errorNYI("Veto await_suspend");
621 }
622
623 // Signals the parent that execution flows to next region.
624 cir::YieldOp::create(builder, loc);
625 },
626 /*resumeBuilder=*/
627 [&](mlir::OpBuilder &b, mlir::Location loc) {
628 // Exception handling requires additional IR. If the 'await_resume'
629 // function is marked as 'noexcept', we avoid generating this additional
630 // IR.
631 if (coro.exceptionHandler && kind == cir::AwaitKind::Init &&
632 memberCallExpressionCanThrow(s.getResumeExpr())) {
633 // we are basically just emitting:
634 // resumeEh = false;
635 // try {
636 // resumeExpr();
637 // resumeEh = true;
638 // } catch(...) {
639 // exceptionHandler();
640 // }
641 // Note the values of resumeEh are reversed from classic codegen,
642 // simply so we can use an 'IfOp' without a 'else' later.
643 ASTContext &ctx = cgf.getContext();
644 SourceLocation resumeLoc = s.getResumeExpr()->getExprLoc();
645 mlir::Location mlirLoc = cgf.getLoc(resumeLoc);
646 coro.resumeEHVar = cgf.createTempAlloca(
647 builder.getBoolTy(), ctx.getTypeAlignInChars(ctx.BoolTy), mlirLoc,
648 "resume.eh");
649 builder.createFlagStore(mlirLoc, false,
650 coro.resumeEHVar.getPointer());
651
652 CXXCatchStmt catchStmt(resumeLoc,
653 /*exDecl=*/nullptr, coro.exceptionHandler);
654 auto *tryBody =
655 CompoundStmt::Create(ctx, s.getResumeExpr(), FPOptionsOverride(),
656 resumeLoc, resumeLoc);
657 CXXTryStmt *tryStmt =
658 CXXTryStmt::Create(ctx, resumeLoc, tryBody, &catchStmt);
659
660 struct resumeEmitter final : CIRGenFunction::cxxTryBodyEmitter {
661 const CXXTryStmt &tryStmt;
662 mlir::Location loc;
663 mlir::Value resumeEHVar;
664 resumeEmitter(const CXXTryStmt &tryStmt, mlir::Location loc,
665 Address resumeEHVar)
666 : tryStmt(tryStmt), loc(loc),
667 resumeEHVar(resumeEHVar.getPointer()) {}
668
669 mlir::LogicalResult operator()(CIRGenFunction &cgf) override {
670 mlir::LogicalResult res =
671 cgf.emitStmt(tryStmt.getTryBlock(), /*useCurrentScope=*/true);
672 cgf.getBuilder().createFlagStore(loc, true, resumeEHVar);
673 return res;
674 }
675
676 ~resumeEmitter() override = default;
677 } emitter{*tryStmt, mlirLoc, coro.resumeEHVar};
678
679 awaitBuild = cgf.emitCXXTryStmt(*tryStmt, emitter);
680 // We are not supposed to obtain the value from init suspend
681 // await_resume().
682 awaitRes.rv = RValue::getIgnored();
683 } else if (forLValue) {
684 // FIXME(cir): the alloca for the resume expr should be placed in the
685 // enclosing cir.scope instead.
686 awaitRes.lv = cgf.emitLValue(s.getResumeExpr());
687 } else {
688 awaitRes.rv =
689 cgf.emitAnyExpr(s.getResumeExpr(), aggSlot, ignoreResult);
690 if (!awaitRes.rv.isIgnored()) {
691 // Create the alloca in the block before the scope wrapping
692 // cir.await.
693 mlir::Value value;
694 RValue rv = awaitRes.rv;
695 if (rv.isScalar()) {
696 value = rv.getValue();
697 } else if (rv.isComplex()) {
698 value = rv.getComplexValue();
699 } else {
700 cgf.cgm.errorNYI("emitSuspendExpression: Aggregate value");
701 return;
702 }
703
704 tmpResumeRValAddr = cgf.emitAlloca(
705 "__coawait_resume_rval", value.getType(), loc, CharUnits::One(),
706 builder.getBestAllocaInsertPoint(scopeParentBlock));
707 // Store the rvalue so we can reload it before the promise call.
708 builder.CIRBaseBuilderTy::createStore(loc, value,
709 tmpResumeRValAddr);
710 }
711 }
712
713 // Returns control back to parent.
714 cir::YieldOp::create(builder, loc);
715 });
716
717 assert(awaitBuild.succeeded() && "Should know how to codegen");
718 return awaitRes;
719}
720
722 const CoroutineSuspendExpr &e,
723 cir::AwaitKind kind, AggValueSlot aggSlot,
724 bool ignoreResult) {
725 RValue rval;
726 mlir::Location scopeLoc = cgf.getLoc(e.getSourceRange());
727
728 // Since we model suspend / resume as an inner region, we must store
729 // resume scalar results in a tmp alloca, and load it after we build the
730 // suspend expression. An alternative way to do this would be to make
731 // every region return a value when promise.return_value() is used, but
732 // it's a bit awkward given that resume is the only region that actually
733 // returns a value.
734 mlir::Block *currEntryBlock = cgf.curLexScope->getEntryBlock();
735 [[maybe_unused]] mlir::Value tmpResumeRValAddr;
736
737 // No need to explicitly wrap this into a scope since the AST already uses a
738 // ExprWithCleanups, which will wrap this into a cir.scope anyways.
739 rval = emitSuspendExpression(cgf, *cgf.curCoro.data, e, kind, aggSlot,
740 ignoreResult, currEntryBlock, tmpResumeRValAddr,
741 /*forLValue*/ false)
742 .rv;
743
744 if (ignoreResult || rval.isIgnored())
745 return rval;
746
747 if (rval.isScalar()) {
748 rval = RValue::get(cir::LoadOp::create(cgf.getBuilder(), scopeLoc,
749 rval.getValue().getType(),
750 tmpResumeRValAddr));
751 } else if (rval.isAggregate()) {
752 // This is probably already handled via AggSlot, remove this assertion
753 // once we have a testcase and prove all pieces work.
754 cgf.cgm.errorNYI("emitSuspendExpr Aggregate");
755 } else { // complex
756 rval = RValue::getComplex(cir::LoadOp::create(
757 cgf.getBuilder(), scopeLoc, rval.getComplexValue().getType(),
758 tmpResumeRValAddr));
759 }
760 return rval;
761}
762
764 AggValueSlot aggSlot,
765 bool ignoreResult) {
766 return emitSuspendExpr(*this, e, curCoro.data->currentAwaitKind, aggSlot,
767 ignoreResult);
768}
769
771 AggValueSlot aggSlot,
772 bool ignoreResult) {
773 return emitSuspendExpr(*this, e, cir::AwaitKind::Yield, aggSlot,
774 ignoreResult);
775}
776
777mlir::LogicalResult CIRGenFunction::emitCoreturnStmt(CoreturnStmt const &s) {
778 ++curCoro.data->coreturnCount;
779 curLexScope->setCoreturn();
780
781 const Expr *rv = s.getOperand();
782 if (rv && rv->getType()->isVoidType() && !isa<InitListExpr>(rv)) {
783 // Make sure to evaluate the non initlist expression of a co_return
784 // with a void expression for side effects.
785 RunCleanupsScope cleanupScope(*this);
786 emitIgnoredExpr(rv);
787 }
788
789 if (emitStmt(s.getPromiseCall(), /*useCurrentScope=*/true).failed())
790 return mlir::failure();
791 // Create a new return block (if not existent) and add a branch to
792 // it. The actual return instruction is only inserted during current
793 // scope cleanup handling.
794 mlir::Location loc = getLoc(s.getSourceRange());
795 cir::CoReturnOp::create(builder, loc);
796
797 return mlir::success();
798}
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 void createCoroData(CIRGenFunction &cgf, CIRGenFunction::CGCoroInfo &curCoro, cir::CoroIdOp coroId, CallExpr const *coroIdExpr=nullptr)
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)
*collection of selector each with an associated kind and an ordered *collection of selectors A selector has a kind
cir::StoreOp createFlagStore(mlir::Location loc, bool val, mlir::Value dst)
mlir::Value createPtrIsNotNull(mlir::Value ptr)
cir::YieldOp createYield(mlir::Location loc, mlir::ValueRange value={})
Create a yield operation.
cir::LoadOp createFlagLoad(mlir::Location loc, mlir::Value addr)
Emit a load from an boolean flag variable.
CharUnits getTypeAlignInChars(QualType T) const
Return the ABI-specified alignment of a (complete) type T, in characters.
CanQualType BoolTy
mlir::Value getPointer() const
Definition Address.h:98
static Address invalid()
Definition Address.h:76
bool isValid() const
Definition Address.h:77
An aggregate value slot.
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.
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,...
cir::CoroAllocOp emitCoroAllocBuiltinCall(const CallExpr *e)
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.
cir::CoroBeginOp emitCoroBeginBuiltinCall(const CallExpr *e)
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.
RValue emitCoyieldExpr(const CoyieldExpr &e, AggValueSlot aggSlot=AggValueSlot::ignored(), bool ignoreResult=false)
cir::CoroFreeOp emitCoroFreeBuiltin(const CallExpr *e)
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)
mlir::Value emitAlloca(llvm::StringRef name, mlir::Type ty, mlir::Location loc, clang::CharUnits alignment, bool insertIntoFnEntryBlock, mlir::Value arraySize=nullptr)
cir::CoroPromiseOp emitCoroPromiseBuiltinCall(const CallExpr *e)
Address returnValue
The temporary alloca to hold the return value.
cir::CoroEndOp emitCoroEndBuiltinCall(const CallExpr *e)
mlir::Value emitScalarExpr(const clang::Expr *e, bool ignoreResultAssign=false)
Emit the computation of the specified expression of scalar type.
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.
cir::CoroSizeOp emitCoroSizeBuiltinCall(const CallExpr *e)
cir::CoroIdOp emitCoroIDBuiltinCall(const CallExpr *e)
clang::ASTContext & getContext() const
mlir::LogicalResult emitCoroutineBody(const CoroutineBodyStmt &s)
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.
CIRGenBuilderTy & getBuilder()
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.
Definition CIRGenValue.h:33
bool isAggregate() const
Definition CIRGenValue.h:51
static RValue get(mlir::Value v)
Definition CIRGenValue.h:83
static RValue getComplex(mlir::Value v)
Definition CIRGenValue.h:91
bool isComplex() const
Definition CIRGenValue.h:50
mlir::Value getValue() const
Return the value of this scalar value.
Definition CIRGenValue.h:57
bool isScalar() const
Definition CIRGenValue.h:49
bool isIgnored() const
Definition CIRGenValue.h:52
mlir::Value getComplexValue() const
Return the value of this complex value.
Definition CIRGenValue.h:63
static RValue getIgnored()
Definition CIRGenValue.h:78
CXXCatchStmt - This represents a C++ catch block.
Definition StmtCXX.h:29
static CXXTryStmt * Create(const ASTContext &C, SourceLocation tryLoc, CompoundStmt *tryBlock, ArrayRef< Stmt * > handlers)
Definition StmtCXX.cpp:26
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:2987
SourceLocation getBeginLoc() const
Definition Expr.h:3321
arg_range arguments()
Definition Expr.h:3239
static CharUnits One()
One - Construct a CharUnits quantity of one.
Definition CharUnits.h:58
Represents a 'co_await' expression.
Definition ExprCXX.h:5422
static CompoundStmt * Create(const ASTContext &C, ArrayRef< Stmt * > Stmts, FPOptionsOverride FPFeatures, SourceLocation LB, SourceLocation RB)
Definition Stmt.cpp:399
Represents a 'co_return' statement in the C++ Coroutines TS.
Definition StmtCXX.h:474
Expr * getOperand() const
Retrieve the operand of the 'co_return' statement.
Definition StmtCXX.h:498
Expr * getPromiseCall() const
Retrieve the promise call that results from this 'co_return' statement.
Definition StmtCXX.h:503
Represents the body of a coroutine.
Definition StmtCXX.h:321
CompoundStmt * getBody() const
Retrieve the body of the coroutine as written.
Definition StmtCXX.h:381
Stmt * getFallthroughHandler() const
Definition StmtCXX.h:402
SourceLocation getBeginLoc() const LLVM_READONLY
Definition StmtCXX.h:428
Represents an expression that might suspend coroutine execution; either a co_await or co_yield expres...
Definition ExprCXX.h:5308
OpaqueValueExpr * getOpaqueValue() const
getOpaqueValue - Return the opaque value placeholder.
Definition ExprCXX.h:5362
Represents a 'co_yield' expression.
Definition ExprCXX.h:5503
A reference to a declared variable, function, enum, etc.
Definition Expr.h:1290
ValueDecl * getDecl()
Definition Expr.h:1358
bool isSingleDecl() const
isSingleDecl - This method returns true if this DeclStmt refers to a single Decl.
Definition Stmt.h:1656
const Decl * getSingleDecl() const
Definition Stmt.h:1658
This represents one expression.
Definition Expr.h:113
QualType getType() const
Definition Expr.h:145
Represents a prototype with parameter type info, e.g.
Definition TypeBase.h:5421
StmtVisitor - This class implements a simple visitor for Stmt subclasses.
Stmt - This represents one statement.
Definition Stmt.h:85
child_range children()
Definition Stmt.cpp:304
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition Stmt.cpp:343
Exposes information about the current target.
Definition TargetInfo.h:226
unsigned getNewAlign() const
Return the largest alignment for which a suitably-sized allocation with 'operator new(size_t)' is gua...
Definition TargetInfo.h:760
unsigned getCharWidth() const
Definition TargetInfo.h:526
bool isVoidType() const
Definition TypeBase.h:9110
const Expr * getInit() const
Definition Decl.h:1392
Defines the clang::TargetInfo interface.
const internal::VariadicDynCastAllOfMatcher< Stmt, Expr > expr
Matches expressions.
Top level wrappers for InstallAPI frontend operations.
CanQual< Type > CanQualType
Represents a canonical, potentially-qualified type.
bool isa(CodeGen::Address addr)
Definition Address.h:330
bool isNoexceptExceptionSpec(ExceptionSpecificationType ESpecType)
U cast(CodeGen::Address addr)
Definition Address.h:327
static bool ehCleanupScope()
static bool coroOutsideFrameMD()
static bool generateDebugInfo()
std::unique_ptr< CGCoroData > data
Represents a scope, including function bodies, compound statements, and the substatements of if/while...
cir::PointerType voidPtrTy
void* in address space 0