https://github.com/xakep8 updated https://github.com/llvm/llvm-project/pull/240737
>From c81c83be4e842ffbc3cd7c3612e1826704850084 Mon Sep 17 00:00:00 2001 From: Kunal Dubey <[email protected]> Date: Tue, 10 Nov 2026 12:50:08 +0530 Subject: [PATCH 1/2] [CIR] Support for the handle-returning await_suspend Add CIR generation for await_suspend methods that return a coroutine handle. This enables one coroutine to resume another. --- clang/include/clang/CIR/Dialect/IR/CIROps.td | 21 +++ clang/lib/CIR/CodeGen/CIRGenCoroutine.cpp | 129 ++++++++++++++- clang/lib/CIR/CodeGen/CIRGenFunction.h | 11 ++ clang/lib/CIR/CodeGen/CIRGenTypes.cpp | 12 ++ clang/lib/CIR/CodeGen/CIRGenTypes.h | 4 + clang/lib/CodeGen/CGCoroutine.cpp | 155 +++++++++--------- .../CIR/CodeGenCoroutines/Inputs/coroutine.h | 2 + .../coro-await-suspend-handle.cpp | 35 ++++ 8 files changed, 290 insertions(+), 79 deletions(-) create mode 100644 clang/test/CIR/CodeGenCoroutines/coro-await-suspend-handle.cpp diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td index b6f35179be1d64..992a1adf802895 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIROps.td +++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td @@ -5331,6 +5331,27 @@ def CIR_CoroSuspendOp : CIR_CoroIntrinsicOp<"suspend", let llvmOp = "CoroSuspendOp"; } +//===----------------------------------------------------------------------===// +// Coroutine intrinsic AwaitSuspendHandleOp +//===----------------------------------------------------------------------===// + +def CIR_CoroAwaitSuspendHandleOp : + CIR_CoroIntrinsicOp<"await.suspend.handle", + (ins CIR_AnyPtrType:$awaiter, + CIR_VoidPtrType:$frame, + CIR_PtrToFunc:$wrapper), + (outs)> { + let summary = "Represents llvm.coro.await.suspend.handle"; + let description = [{ + Performs an await-suspend operation that returns another coroutine handle. + The wrapper invokes await_suspend using the awaiter and current coroutine + frame. The returned handle identifies the coroutine resumed by symmetric + transfer. + }]; + + let llvmOp = "CoroAwaitSuspendHandleOp"; +} + //===----------------------------------------------------------------------===// // Coroutine intrinsic SaveOp //===----------------------------------------------------------------------===// diff --git a/clang/lib/CIR/CodeGen/CIRGenCoroutine.cpp b/clang/lib/CIR/CodeGen/CIRGenCoroutine.cpp index 162fc61bbcf93b..a7d8066da0fe00 100644 --- a/clang/lib/CIR/CodeGen/CIRGenCoroutine.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenCoroutine.cpp @@ -17,6 +17,8 @@ #include "clang/Basic/TargetInfo.h" #include "clang/CIR/Dialect/IR/CIRDialect.h" #include "clang/CIR/MissingFeatures.h" +#include "llvm/ADT/SmallString.h" +#include "llvm/ADT/Twine.h" using namespace clang; using namespace clang::CIRGen; @@ -26,6 +28,8 @@ struct clang::CIRGen::CGCoroData { // await/yield expressions were encountered so far. // These are used to generate pretty labels for await expressions in LLVM IR. cir::AwaitKind currentAwaitKind = cir::AwaitKind::Init; + unsigned awaitNum = 0; + unsigned yieldNum = 0; // Stores the __builtin_coro_id emitted in the function so that we can supply // it as the first argument to other builtins. cir::CoroIdOp coroId = nullptr; @@ -61,6 +65,34 @@ CIRGenFunction::CGCoroInfo::CGCoroInfo() {} CIRGenFunction::CGCoroInfo::~CGCoroInfo() {} namespace { +static llvm::SmallString<32> buildSuspendPointName(CGCoroData &coro, + cir::AwaitKind kind) { + llvm::StringRef name; + unsigned number = 0; + + switch (kind) { + case cir::AwaitKind::Init: + name = "init"; + break; + case cir::AwaitKind::User: + name = "await"; + number = ++coro.awaitNum; + break; + case cir::AwaitKind::Yield: + name = "yield"; + number = ++coro.yieldNum; + break; + case cir::AwaitKind::Final: + name = "final"; + break; + } + + llvm::SmallString<32> result(name); + if (number > 1) + llvm::Twine(number).toVector(result); + + return result; +} // FIXME: both GetParamRef and ParamReferenceReplacerRAII are good template // candidates to be shared among LLVM / CIR codegen. @@ -124,10 +156,83 @@ RValue CIRGenFunction::emitCoroutineFrame() { if (curCoro.data && curCoro.data->coroBegin) { return RValue::get(curCoro.data->coroBegin); } + + if (curAwaitSuspendWrapper.framePtr) + return RValue::get(curAwaitSuspendWrapper.framePtr); + cgm.errorNYI("NYI"); return RValue(); } +cir::FuncOp +CIRGenFunction::generateAwaitSuspendWrapper(llvm::StringRef coroutineName, + llvm::StringRef suspendPointName, + const CoroutineSuspendExpr &s) { + llvm::SmallString<128> functionName(coroutineName); + functionName += ".__await_suspend_wrapper__"; + functionName += suspendPointName; + + ASTContext &context = getContext(); + + auto *awaiterDecl = ImplicitParamDecl::Create(context, context.VoidPtrTy, + ImplicitParamKind::Other); + auto *frameDecl = ImplicitParamDecl::Create(context, context.VoidPtrTy, + ImplicitParamKind::Other); + + QualType returnTy = s.getSuspendExpr()->getType(); + + FunctionArgList args; + args.push_back(awaiterDecl); + args.push_back(frameDecl); + + const CIRGenFunctionInfo &functionInfo = + cgm.getTypes().arrangeBuiltinFunctionDeclaration(returnTy, args); + + cir::FuncType functionType = cgm.getTypes().getFunctionType(functionInfo); + + cir::FuncOp function = + cgm.createCIRFunction(getLoc(s.getSourceRange()), functionName, + functionType, /*funcDecl=*/nullptr); + + function.setLinkage(cir::GlobalLinkageKind::InternalLinkage); + + cgm.setCIRFunctionAttributes(GlobalDecl(), functionInfo, function, + /*isThunk=*/false); + + mlir::OpBuilder::InsertionGuard insertionGuard(builder); + SourceLocRAIIObject functionLocation(*this, s.getSourceRange()); + + mlir::Location location = getLoc(s.getSourceRange()); + mlir::Block *entryBlock = function.addEntryBlock(); + + SymTableScopeTy variableScope(symbolTable); + { + LexicalScope lexicalScope(*this, location, entryBlock); + startFunction(GlobalDecl(), returnTy, function, functionType, args, + s.getBeginLoc(), s.getBeginLoc()); + mlir::Value awaiterPtr = + builder.createLoad(location, getAddrOfLocalVar(awaiterDecl)); + + QualType awaiterTy = s.getOpaqueValue()->getType(); + mlir::Type awaiterType = convertTypeForMem(awaiterTy); + awaiterPtr = builder.createPtrBitcast(awaiterPtr, awaiterType); + + LValue awaiterLValue = makeNaturalAlignAddrLValue(awaiterPtr, awaiterTy); + curAwaitSuspendWrapper.framePtr = + builder.createLoad(location, getAddrOfLocalVar(frameDecl)); + + OpaqueValueMapping awaiterBinder(*this, s.getOpaqueValue(), awaiterLValue); + + mlir::Value suspendResult = emitScalarExpr(s.getSuspendExpr()); + if (suspendResult) + builder.createStore(location, suspendResult, returnValue); + + curAwaitSuspendWrapper.framePtr = nullptr; + finishFunction(s.getEndLoc()); + } + return function; +} + static void createCoroData(CIRGenFunction &cgf, CIRGenFunction::CGCoroInfo &curCoro, cir::CoroIdOp coroId, @@ -718,6 +823,7 @@ emitSuspendExpression(CIRGenFunction &cgf, CGCoroData &coro, CIRGenFunction::OpaqueValueMapping binder = CIRGenFunction::OpaqueValueMapping(cgf, s.getOpaqueValue()); CIRGenBuilderTy &builder = cgf.getBuilder(); + llvm::SmallString<32> suspendPointName = buildSuspendPointName(coro, kind); // Exception handling requires additional IR. We avoid generating it when // neither the await_resume() call nor the destruction of its result can @@ -784,8 +890,27 @@ emitSuspendExpression(CIRGenFunction &cgf, CGCoroData &coro, cir::CoroSuspendPoint::create(builder, loc); break; case CoroutineSuspendExpr::SuspendReturnType::SuspendHandle: - cgf.cgm.errorNYI(s.getSourceRange(), - "await_suspend returning handle"); + cir::FuncOp coroutineFunction = mlir::cast<cir::FuncOp>(cgf.curFn); + + CIRGenFunction wrapperCGF(cgf.cgm, builder); + + cir::FuncOp suspendWrapper = wrapperCGF.generateAwaitSuspendWrapper( + coroutineFunction.getSymName(), suspendPointName, s); + + mlir::Value awaiterPointer = + cgf.getOrCreateOpaqueLValueMapping(s.getOpaqueValue()) + .getPointer(); + + assert(coro.coroBegin && "expected coroutine frame"); + + cir::PointerType wrapperPointerType = + cir::PointerType::get(suspendWrapper.getFunctionType()); + + mlir::Value wrapperPointer = cir::GetGlobalOp::create( + builder, loc, wrapperPointerType, suspendWrapper.getSymName()); + + cir::CoroAwaitSuspendHandleOp::create(builder, loc, awaiterPointer, + coro.coroBegin, wrapperPointer); cir::CoroSuspendPoint::create(builder, loc); break; } diff --git a/clang/lib/CIR/CodeGen/CIRGenFunction.h b/clang/lib/CIR/CodeGen/CIRGenFunction.h index f9483d9aac8ce1..1930ad435a2bac 100644 --- a/clang/lib/CIR/CodeGen/CIRGenFunction.h +++ b/clang/lib/CIR/CodeGen/CIRGenFunction.h @@ -118,6 +118,14 @@ class CIRGenFunction : public CIRGenTypeCache { }; CGCoroInfo curCoro; + // Holds the coroutine frame parameter while generating an await_suspend + // wrapper. The wrapper is not itself a coroutine, so it has no coro.begin. + struct AwaitSuspendWrapperInfo { + mlir::Value framePtr; + }; + + AwaitSuspendWrapperInfo curAwaitSuspendWrapper; + bool isCoroutine() const { return curCoro.data != nullptr; } /// The temporary alloca to hold the return value. This is @@ -2119,6 +2127,9 @@ class CIRGenFunction : public CIRGenTypeCache { LValue emitCompoundLiteralLValue(const CompoundLiteralExpr *e); void emitConstructorBody(FunctionArgList &args); + cir::FuncOp generateAwaitSuspendWrapper(llvm::StringRef coroutineName, + llvm::StringRef suspendPointName, + const CoroutineSuspendExpr &s); mlir::LogicalResult emitCoroutineBody(const CoroutineBodyStmt &s); cir::CoroEndOp emitCoroEndBuiltinCall(const CallExpr *e); diff --git a/clang/lib/CIR/CodeGen/CIRGenTypes.cpp b/clang/lib/CIR/CodeGen/CIRGenTypes.cpp index a030d5dc39eff1..515145a77526f5 100644 --- a/clang/lib/CIR/CodeGen/CIRGenTypes.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenTypes.cpp @@ -871,6 +871,18 @@ const CIRGenFunctionInfo &CIRGenTypes::arrangeGlobalDeclaration(GlobalDecl gd) { return arrangeFunctionDeclaration(fd); } +const CIRGenFunctionInfo & +CIRGenTypes::arrangeBuiltinFunctionDeclaration(QualType resultType, + const FunctionArgList &args) { + SmallVector<CanQualType, 16> argTypes; + for (const VarDecl *arg : args) + argTypes.push_back(astContext.getCanonicalParamType(arg->getType())); + + return arrangeCIRFunctionInfo(resultType->getCanonicalTypeUnqualified(), + /*isInstanceMethod=*/false, argTypes, + FunctionType::ExtInfo(), RequiredArgs::All); +} + // When we find the full definition for a TagDecl, replace the 'opaque' type we // previously made for it if applicable. void CIRGenTypes::updateCompletedType(const TagDecl *td) { diff --git a/clang/lib/CIR/CodeGen/CIRGenTypes.h b/clang/lib/CIR/CodeGen/CIRGenTypes.h index 852e1f4ab66993..db36797a1f742d 100644 --- a/clang/lib/CIR/CodeGen/CIRGenTypes.h +++ b/clang/lib/CIR/CodeGen/CIRGenTypes.h @@ -198,6 +198,10 @@ class CIRGenTypes { const CIRGenFunctionInfo &arrangeBuiltinFunctionCall(QualType resultType, const CallArgList &args); + const CIRGenFunctionInfo & + arrangeBuiltinFunctionDeclaration(QualType resultType, + const FunctionArgList &args); + /// Return whether a type can be zero-initialized (in the C++ sense) with an /// LLVM zeroinitializer. bool isZeroInitializable(clang::QualType ty); diff --git a/clang/lib/CodeGen/CGCoroutine.cpp b/clang/lib/CodeGen/CGCoroutine.cpp index 2f162519139e0d..60f6e2d60c9548 100644 --- a/clang/lib/CodeGen/CGCoroutine.cpp +++ b/clang/lib/CodeGen/CGCoroutine.cpp @@ -20,14 +20,14 @@ using namespace clang; using namespace CodeGen; -using llvm::Value; using llvm::BasicBlock; +using llvm::Value; namespace { enum class AwaitKind { Init, Normal, Yield, Final }; static constexpr llvm::StringLiteral AwaitKindStr[] = {"init", "await", "yield", "final"}; -} +} // namespace struct clang::CodeGen::CGCoroData { // What is the current await expression kind and how many @@ -217,15 +217,15 @@ static bool StmtCanThrow(const Stmt *S) { // See llvm's docs/Coroutines.md for more details. // namespace { - struct LValueOrRValue { - LValue LV; - RValue RV; - }; -} -static LValueOrRValue emitSuspendExpression(CodeGenFunction &CGF, CGCoroData &Coro, - CoroutineSuspendExpr const &S, - AwaitKind Kind, AggValueSlot aggSlot, - bool ignoreResult, bool forLValue) { +struct LValueOrRValue { + LValue LV; + RValue RV; +}; +} // namespace +static LValueOrRValue +emitSuspendExpression(CodeGenFunction &CGF, CGCoroData &Coro, + CoroutineSuspendExpr const &S, AwaitKind Kind, + AggValueSlot aggSlot, bool ignoreResult, bool forLValue) { auto *E = S.getCommonExpr(); auto CommonBinder = @@ -380,13 +380,15 @@ RValue CodeGenFunction::EmitCoawaitExpr(const CoawaitExpr &E, bool ignoreResult) { return emitSuspendExpression(*this, *CurCoro.Data, E, CurCoro.Data->CurrentAwaitKind, aggSlot, - ignoreResult, /*forLValue*/false).RV; + ignoreResult, /*forLValue*/ false) + .RV; } RValue CodeGenFunction::EmitCoyieldExpr(const CoyieldExpr &E, AggValueSlot aggSlot, bool ignoreResult) { return emitSuspendExpression(*this, *CurCoro.Data, E, AwaitKind::Yield, - aggSlot, ignoreResult, /*forLValue*/false).RV; + aggSlot, ignoreResult, /*forLValue*/ false) + .RV; } void CodeGenFunction::EmitCoreturnStmt(CoreturnStmt const &S) { @@ -402,10 +404,10 @@ void CodeGenFunction::EmitCoreturnStmt(CoreturnStmt const &S) { EmitBranchThroughCleanup(CurCoro.Data->FinalJD); } - #ifndef NDEBUG -static QualType getCoroutineSuspendExprReturnType(const ASTContext &Ctx, - const CoroutineSuspendExpr *E) { +static QualType +getCoroutineSuspendExprReturnType(const ASTContext &Ctx, + const CoroutineSuspendExpr *E) { const auto *RE = E->getResumeExpr(); // Is it possible for RE to be a CXXBindTemporaryExpr wrapping // a MemberCallExpr? @@ -475,24 +477,27 @@ CodeGenFunction::generateAwaitSuspendWrapper(Twine const &CoroName, return Fn; } -LValue -CodeGenFunction::EmitCoawaitLValue(const CoawaitExpr *E) { - assert(getCoroutineSuspendExprReturnType(getContext(), E)->isReferenceType() && - "Can't have a scalar return unless the return type is a " - "reference type!"); +LValue CodeGenFunction::EmitCoawaitLValue(const CoawaitExpr *E) { + assert( + getCoroutineSuspendExprReturnType(getContext(), E)->isReferenceType() && + "Can't have a scalar return unless the return type is a " + "reference type!"); return emitSuspendExpression(*this, *CurCoro.Data, *E, - CurCoro.Data->CurrentAwaitKind, AggValueSlot::ignored(), - /*ignoreResult*/false, /*forLValue*/true).LV; + CurCoro.Data->CurrentAwaitKind, + AggValueSlot::ignored(), + /*ignoreResult*/ false, /*forLValue*/ true) + .LV; } -LValue -CodeGenFunction::EmitCoyieldLValue(const CoyieldExpr *E) { - assert(getCoroutineSuspendExprReturnType(getContext(), E)->isReferenceType() && - "Can't have a scalar return unless the return type is a " - "reference type!"); - return emitSuspendExpression(*this, *CurCoro.Data, *E, - AwaitKind::Yield, AggValueSlot::ignored(), - /*ignoreResult*/false, /*forLValue*/true).LV; +LValue CodeGenFunction::EmitCoyieldLValue(const CoyieldExpr *E) { + assert( + getCoroutineSuspendExprReturnType(getContext(), E)->isReferenceType() && + "Can't have a scalar return unless the return type is a " + "reference type!"); + return emitSuspendExpression(*this, *CurCoro.Data, *E, AwaitKind::Yield, + AggValueSlot::ignored(), + /*ignoreResult*/ false, /*forLValue*/ true) + .LV; } // Hunts for the parameter reference in the parameter copy/move declaration. @@ -512,51 +517,51 @@ struct GetParamRef : public StmtVisitor<GetParamRef> { } } }; -} +} // namespace // This class replaces references to parameters to their copies by changing // the addresses in CGF.LocalDeclMap and restoring back the original values in // its destructor. namespace { - struct ParamReferenceReplacerRAII { - CodeGenFunction::DeclMapTy SavedLocals; - CodeGenFunction::DeclMapTy& LocalDeclMap; - - ParamReferenceReplacerRAII(CodeGenFunction::DeclMapTy &LocalDeclMap) - : LocalDeclMap(LocalDeclMap) {} - - void addCopy(DeclStmt const *PM) { - // Figure out what param it refers to. - - assert(PM->isSingleDecl()); - VarDecl const*VD = static_cast<VarDecl const*>(PM->getSingleDecl()); - Expr const *InitExpr = VD->getInit(); - GetParamRef Visitor; - Visitor.Visit(const_cast<Expr*>(InitExpr)); - assert(Visitor.Expr); - DeclRefExpr *DREOrig = Visitor.Expr; - auto *PD = DREOrig->getDecl(); - - auto it = LocalDeclMap.find(PD); - assert(it != LocalDeclMap.end() && "parameter is not found"); - SavedLocals.insert({ PD, it->second }); - - auto copyIt = LocalDeclMap.find(VD); - assert(copyIt != LocalDeclMap.end() && "parameter copy is not found"); - it->second = copyIt->getSecond(); - } +struct ParamReferenceReplacerRAII { + CodeGenFunction::DeclMapTy SavedLocals; + CodeGenFunction::DeclMapTy &LocalDeclMap; + + ParamReferenceReplacerRAII(CodeGenFunction::DeclMapTy &LocalDeclMap) + : LocalDeclMap(LocalDeclMap) {} + + void addCopy(DeclStmt const *PM) { + // Figure out what param it refers to. + + assert(PM->isSingleDecl()); + VarDecl const *VD = static_cast<VarDecl const *>(PM->getSingleDecl()); + Expr const *InitExpr = VD->getInit(); + GetParamRef Visitor; + Visitor.Visit(const_cast<Expr *>(InitExpr)); + assert(Visitor.Expr); + DeclRefExpr *DREOrig = Visitor.Expr; + auto *PD = DREOrig->getDecl(); + + auto it = LocalDeclMap.find(PD); + assert(it != LocalDeclMap.end() && "parameter is not found"); + SavedLocals.insert({PD, it->second}); + + auto copyIt = LocalDeclMap.find(VD); + assert(copyIt != LocalDeclMap.end() && "parameter copy is not found"); + it->second = copyIt->getSecond(); + } - ~ParamReferenceReplacerRAII() { - for (auto&& SavedLocal : SavedLocals) { - LocalDeclMap.insert({SavedLocal.first, SavedLocal.second}); - } + ~ParamReferenceReplacerRAII() { + for (auto &&SavedLocal : SavedLocals) { + LocalDeclMap.insert({SavedLocal.first, SavedLocal.second}); } - }; -} + } +}; +} // namespace -// For WinEH exception representation backend needs to know what funclet coro.end -// belongs to. That information is passed in a funclet bundle. +// For WinEH exception representation backend needs to know what funclet +// coro.end belongs to. That information is passed in a funclet bundle. static SmallVector<llvm::OperandBundleDef, 1> getBundlesForCoroEnd(CodeGenFunction &CGF) { SmallVector<llvm::OperandBundleDef, 1> BundleList; @@ -595,7 +600,7 @@ struct CallCoroEnd final : public EHScopeStack::Cleanup { } } }; -} +} // namespace namespace { // Make sure to call coro.delete on scope exit. @@ -649,7 +654,7 @@ struct CallCoroDelete final : public EHScopeStack::Cleanup { } explicit CallCoroDelete(Stmt *DeallocStmt) : Deallocate(DeallocStmt) {} }; -} +} // namespace namespace { struct GetReturnObjectManager { @@ -956,8 +961,7 @@ void CodeGenFunction::EmitCoroutineBody(const CoroutineBodyStmt &S) { // If not, return OnAllocFailure object. EmitBlock(RetOnFailureBB); EmitStmt(RetOnAllocFailure); - } - else { + } else { Builder.CreateBr(InitBB); } @@ -1061,8 +1065,7 @@ void CodeGenFunction::EmitCoroutineBody(const CoroutineBodyStmt &S) { if (ContBB) EmitBlock(ContBB); - } - else { + } else { emitBodyAndFallthrough(*this, S, S.getBody()); } @@ -1185,12 +1188,10 @@ RValue CodeGenFunction::EmitCoroutineIntrinsic(const CallExpr *E, // coro.alloc, coro.begin and coro.free intrinsics to refer to it. if (IID == llvm::Intrinsic::coro_id) { createCoroData(*this, CurCoro, Call, E); - } - else if (IID == llvm::Intrinsic::coro_begin) { + } else if (IID == llvm::Intrinsic::coro_begin) { if (CurCoro.Data) CurCoro.Data->CoroBegin = Call; - } - else if (IID == llvm::Intrinsic::coro_free) { + } else if (IID == llvm::Intrinsic::coro_free) { // Remember the last coro_free as we need it to build the conditional // deletion of the coroutine frame. if (CurCoro.Data) diff --git a/clang/test/CIR/CodeGenCoroutines/Inputs/coroutine.h b/clang/test/CIR/CodeGenCoroutines/Inputs/coroutine.h index bddd2b590c901e..e0a0d13811cd4d 100644 --- a/clang/test/CIR/CodeGenCoroutines/Inputs/coroutine.h +++ b/clang/test/CIR/CodeGenCoroutines/Inputs/coroutine.h @@ -14,12 +14,14 @@ struct coroutine_traits { using promise_type = typename Ret::promise_type; }; template <class Promise = void> struct coroutine_handle { static coroutine_handle from_address(void *) noexcept; + void *address() const noexcept; }; template <> struct coroutine_handle<void> { template <class PromiseType> coroutine_handle(coroutine_handle<PromiseType>) noexcept; static coroutine_handle from_address(void *); + void *address() const noexcept; }; struct suspend_always { diff --git a/clang/test/CIR/CodeGenCoroutines/coro-await-suspend-handle.cpp b/clang/test/CIR/CodeGenCoroutines/coro-await-suspend-handle.cpp new file mode 100644 index 00000000000000..dafb9ba587de50 --- /dev/null +++ b/clang/test/CIR/CodeGenCoroutines/coro-await-suspend-handle.cpp @@ -0,0 +1,35 @@ +// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu \ +// RUN: -fclangir -Wno-coroutine-missing-unhandled-exception \ +// RUN: -emit-cir %s -o %t.cir +// RUN: FileCheck --input-file=%t.cir %s --check-prefix=CIR + +#include "Inputs/coroutine.h" + +struct Task { + struct promise_type { + Task get_return_object() { return {}; } + std::suspend_never initial_suspend() noexcept { return {}; } + std::suspend_never final_suspend() noexcept { return {}; } + void return_void() noexcept {} + void unhandled_exception() {} + }; +}; + +struct HandleAwaiter { + bool await_ready() noexcept; + std::coroutine_handle<> + await_suspend(std::coroutine_handle<>) noexcept; + void await_resume() noexcept; +}; + +// CIR-LABEL: cir.func coroutine {{.*}} @_Z12await_handlev +// CIR: cir.coro.intrinsic.await.suspend.handle +// CIR: cir.coro.suspend_point +Task await_handle() { + co_await HandleAwaiter{}; +} + +// CIR-LABEL: cir.func {{.*}} @_Z12await_handlev.__await_suspend_wrapper__await +// CIR: cir.call {{.*}}await_suspend +// CIR: cir.call {{.*}}address +// CIR: cir.return >From 9f48db43ed079a1efb0d3477c2486ef3d0592c30 Mon Sep 17 00:00:00 2001 From: Kunal Dubey <[email protected]> Date: Tue, 10 Nov 2026 15:46:45 +0530 Subject: [PATCH 2/2] [CIR] Added TODO for exception handling --- clang/lib/CIR/CodeGen/CIRGenCoroutine.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/clang/lib/CIR/CodeGen/CIRGenCoroutine.cpp b/clang/lib/CIR/CodeGen/CIRGenCoroutine.cpp index a7d8066da0fe00..06de98075bbedb 100644 --- a/clang/lib/CIR/CodeGen/CIRGenCoroutine.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenCoroutine.cpp @@ -909,6 +909,7 @@ emitSuspendExpression(CIRGenFunction &cgf, CGCoroData &coro, mlir::Value wrapperPointer = cir::GetGlobalOp::create( builder, loc, wrapperPointerType, suspendWrapper.getSymName()); + // TODO(cir): Handle exceptions when resuming the returned coroutine. cir::CoroAwaitSuspendHandleOp::create(builder, loc, awaiterPointer, coro.coroBegin, wrapperPointer); cir::CoroSuspendPoint::create(builder, loc); _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
