https://github.com/xakep8 updated https://github.com/llvm/llvm-project/pull/240737
>From 77c8779e4eb3b652086693577ff460124686671a Mon Sep 17 00:00:00 2001 From: Kunal Dubey <[email protected]> Date: Tue, 10 Nov 2026 12:50:08 +0530 Subject: [PATCH 1/5] [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 0e540588b45f97..8ee263aa5377e1 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIROps.td +++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td @@ -5387,6 +5387,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 3ebbaecaecba54..157c8248d48f67 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 263ec4cedef8928874f71d9f3e76ae732ce2de0f Mon Sep 17 00:00:00 2001 From: Kunal Dubey <[email protected]> Date: Tue, 10 Nov 2026 15:46:45 +0530 Subject: [PATCH 2/5] [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); >From ef09857d9ef636767d12fbec64def9949f42a61a Mon Sep 17 00:00:00 2001 From: Kunal Dubey <[email protected]> Date: Wed, 11 Nov 2026 23:21:15 +0530 Subject: [PATCH 3/5] [CIR] Generalized Await Suspend Intrinsic Moved the wrapper generation above the switch case so that the wrapper could be used by suspend bool and void cases too and expanded the new await.suspend intrinsic to handle the bool and void cases. Also updated the tests to handle the new cases and fixed old test shaped in light of the new intrinsic. --- clang/include/clang/CIR/Dialect/IR/CIROps.td | 22 ++- clang/lib/CIR/CodeGen/CIRGenCoroutine.cpp | 46 +++--- clang/lib/CodeGen/CGCoroutine.cpp | 155 +++++++++--------- clang/test/CIR/CodeGenCoroutines/coro-agg.cpp | 14 +- .../coro-await-suspend-bool.cpp | 4 +- .../coro-await-suspend-handle.cpp | 35 ---- .../CodeGenCoroutines/coro-await-suspend.cpp | 68 ++++++++ .../test/CIR/CodeGenCoroutines/coro-await.cpp | 8 +- .../test/CIR/CodeGenCoroutines/coro-yield.cpp | 10 +- 9 files changed, 211 insertions(+), 151 deletions(-) delete mode 100644 clang/test/CIR/CodeGenCoroutines/coro-await-suspend-handle.cpp create mode 100644 clang/test/CIR/CodeGenCoroutines/coro-await-suspend.cpp diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td index 8ee263aa5377e1..bdd692f9cafedf 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIROps.td +++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td @@ -5388,9 +5388,29 @@ def CIR_CoroSuspendOp : CIR_CoroIntrinsicOp<"suspend", } //===----------------------------------------------------------------------===// -// Coroutine intrinsic AwaitSuspendHandleOp +// Coroutine intrinsic AwaitSuspendOp //===----------------------------------------------------------------------===// +def CIR_CoroAwaitSuspendVoidOp : + CIR_CoroIntrinsicOp<"await.suspend.void", + (ins CIR_AnyPtrType:$awaiter, + CIR_VoidPtrType:$frame, + CIR_PtrToFunc:$wrapper), + (outs)> { + let summary = "Represents llvm.coro.await.suspend.void"; + let llvmOp = "CoroAwaitSuspendVoidOp"; +} + +def CIR_CoroAwaitSuspendBoolOp : + CIR_CoroIntrinsicOp<"await.suspend.bool", + (ins CIR_AnyPtrType:$awaiter, + CIR_VoidPtrType:$frame, + CIR_PtrToFunc:$wrapper), + (outs CIR_BoolType:$result)> { + let summary = "Represents llvm.coro.await.suspend.bool"; + let llvmOp = "CoroAwaitSuspendBoolOp"; +} + def CIR_CoroAwaitSuspendHandleOp : CIR_CoroIntrinsicOp<"await.suspend.handle", (ins CIR_AnyPtrType:$awaiter, diff --git a/clang/lib/CIR/CodeGen/CIRGenCoroutine.cpp b/clang/lib/CIR/CodeGen/CIRGenCoroutine.cpp index 06de98075bbedb..d36965ca1af3a7 100644 --- a/clang/lib/CIR/CodeGen/CIRGenCoroutine.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenCoroutine.cpp @@ -877,38 +877,40 @@ emitSuspendExpression(CIRGenFunction &cgf, CGCoroData &coro, // and coro.suspend here, that should be done as part of lowering this // to LLVM dialect (or some other MLIR dialect) + 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()); + switch (s.getSuspendReturnType()) { case CoroutineSuspendExpr::SuspendReturnType::SuspendBool: { - mlir::Value suspendRet = cgf.evaluateExprAsBool(s.getSuspendExpr()); + cir::CoroAwaitSuspendBoolOp suspendRet = + cir::CoroAwaitSuspendBoolOp::create( + builder, loc, awaiterPointer, coro.coroBegin, wrapperPointer); // Veto suspension if requested by bool returning await_suspend. - cir::CoroSuspendPoint::create(builder, loc, suspendRet); + cir::CoroSuspendPoint::create(builder, loc, suspendRet.getResult()); break; } case CoroutineSuspendExpr::SuspendReturnType::SuspendVoid: - cgf.emitScalarExpr(s.getSuspendExpr()); + cir::CoroAwaitSuspendVoidOp::create(builder, loc, awaiterPointer, + coro.coroBegin, wrapperPointer); // Signals the parent that execution flows to next region. cir::CoroSuspendPoint::create(builder, loc); break; case CoroutineSuspendExpr::SuspendReturnType::SuspendHandle: - 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()); - // TODO(cir): Handle exceptions when resuming the returned coroutine. cir::CoroAwaitSuspendHandleOp::create(builder, loc, awaiterPointer, coro.coroBegin, wrapperPointer); diff --git a/clang/lib/CodeGen/CGCoroutine.cpp b/clang/lib/CodeGen/CGCoroutine.cpp index 60f6e2d60c9548..2f162519139e0d 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::BasicBlock; using llvm::Value; +using llvm::BasicBlock; 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; -}; -} // namespace -static LValueOrRValue -emitSuspendExpression(CodeGenFunction &CGF, CGCoroData &Coro, - CoroutineSuspendExpr const &S, AwaitKind Kind, - AggValueSlot aggSlot, bool ignoreResult, bool forLValue) { + struct LValueOrRValue { + LValue LV; + RValue RV; + }; +} +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,15 +380,13 @@ 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) { @@ -404,10 +402,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? @@ -477,27 +475,24 @@ 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. @@ -517,51 +512,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; @@ -600,7 +595,7 @@ struct CallCoroEnd final : public EHScopeStack::Cleanup { } } }; -} // namespace +} namespace { // Make sure to call coro.delete on scope exit. @@ -654,7 +649,7 @@ struct CallCoroDelete final : public EHScopeStack::Cleanup { } explicit CallCoroDelete(Stmt *DeallocStmt) : Deallocate(DeallocStmt) {} }; -} // namespace +} namespace { struct GetReturnObjectManager { @@ -961,7 +956,8 @@ void CodeGenFunction::EmitCoroutineBody(const CoroutineBodyStmt &S) { // If not, return OnAllocFailure object. EmitBlock(RetOnFailureBB); EmitStmt(RetOnAllocFailure); - } else { + } + else { Builder.CreateBr(InitBB); } @@ -1065,7 +1061,8 @@ void CodeGenFunction::EmitCoroutineBody(const CoroutineBodyStmt &S) { if (ContBB) EmitBlock(ContBB); - } else { + } + else { emitBodyAndFallthrough(*this, S, S.getBody()); } @@ -1188,10 +1185,12 @@ 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/coro-agg.cpp b/clang/test/CIR/CodeGenCoroutines/coro-agg.cpp index e1b5d1b0eef8b0..791ec17e273e0c 100644 --- a/clang/test/CIR/CodeGenCoroutines/coro-agg.cpp +++ b/clang/test/CIR/CodeGenCoroutines/coro-agg.cpp @@ -1179,13 +1179,17 @@ struct ReadySuspendArgs { // RESUME-EH: } cleanup all { // RESUME-NEXT: cir.call @_ZN1SD1Ev(%[[READY]]) // RESUME: }, suspend : { +// RESUME: %[[SUSPEND_WRAPPER:.*]] = cir.get_global @ready_suspend_in_conditional.__await_suspend_wrapper__await : +// RESUME-NEXT: cir.coro.intrinsic.await.suspend.void({{.*}}, {{.*}}, %[[SUSPEND_WRAPPER]]) +// RESUME-NEXT: cir.coro.suspend_point +// RESUME: }, resume : { +// RESUME: cir.call @_ZN16ReadySuspendArgs12await_resumeEv( + +extern "C" Task ready_suspend_in_conditional(bool b) { resume_use(b ? co_await ReadySuspendArgs{} : 0); } + +// RESUME-LABEL: cir.func {{.*}} @ready_suspend_in_conditional.__await_suspend_wrapper__await( // RESUME: cir.call @_ZN1SC1Ei(%[[SUSPEND:[^,]+]], // RESUME: cir.call @_ZN16ReadySuspendArgs13await_suspendESt16coroutine_handleIvERK1S( // RESUME-NOEH: } cleanup normal { // RESUME-EH: } cleanup all { // RESUME-NEXT: cir.call @_ZN1SD1Ev(%[[SUSPEND]]) -// RESUME: cir.coro.suspend_point -// RESUME: }, resume : { -// RESUME: cir.call @_ZN16ReadySuspendArgs12await_resumeEv( - -extern "C" Task ready_suspend_in_conditional(bool b) { resume_use(b ? co_await ReadySuspendArgs{} : 0); } diff --git a/clang/test/CIR/CodeGenCoroutines/coro-await-suspend-bool.cpp b/clang/test/CIR/CodeGenCoroutines/coro-await-suspend-bool.cpp index 21577e5202bbfe..299f07efa9bd9b 100644 --- a/clang/test/CIR/CodeGenCoroutines/coro-await-suspend-bool.cpp +++ b/clang/test/CIR/CodeGenCoroutines/coro-await-suspend-bool.cpp @@ -34,7 +34,7 @@ Task await_bool_veto() { // CIR: %[[READY:.*]] = cir.call @_ZN11BoolAwaiter11await_readyEv(%{{.*}}) : (!cir.ptr<!rec_BoolAwaiter>{{.*}}) -> (!cir.bool{{.*}}) // CIR: cir.condition(%[[READY]]) // CIR: }, suspend : { - // CIR: %[[SUSPEND_RET:.*]] = cir.call @_ZN11BoolAwaiter13await_suspendESt16coroutine_handleIvE(%{{.*}}) : (!cir.ptr<!rec_BoolAwaiter>{{.*}}) -> (!cir.bool{{.*}}) + // CIR: %[[SUSPEND_RET:.*]] = cir.coro.intrinsic.await.suspend.bool // CIR: cir.coro.suspend_point(%[[SUSPEND_RET]]) // CIR: }, resume : { // CIR: cir.call @_ZN11BoolAwaiter12await_resumeEv(%{{.*}}) : (!cir.ptr<!rec_BoolAwaiter>{{.*}}) -> () @@ -44,7 +44,7 @@ Task await_bool_veto() { // CIR-FLAT: %[[READY:.*]] = cir.call @_ZN11BoolAwaiter11await_readyEv(%[[AWAITER:[0-9]+]]) // CIR-FLAT-NEXT: cir.brcond %[[READY]] ^[[RESUME:bb[0-9]+]], ^[[SUSPEND:.*]] // CIR-FLAT: ^[[SUSPEND]]: - // CIR-FLAT: %[[SUSPEND_RET:.*]] = cir.call @_ZN11BoolAwaiter13await_suspendESt16coroutine_handleIvE(%[[AWAITER]]) + // CIR-FLAT: %[[SUSPEND_RET:.*]] = cir.coro.intrinsic.await.suspend.bool // CIR-FLAT-NEXT: cir.brcond %[[SUSPEND_RET]] ^[[DO_SUSPEND:.*]], ^[[RESUME]] // CIR-FLAT: ^[[DO_SUSPEND]]: // CIR-FLAT: %[[RES:.*]] = cir.coro.intrinsic.suspend diff --git a/clang/test/CIR/CodeGenCoroutines/coro-await-suspend-handle.cpp b/clang/test/CIR/CodeGenCoroutines/coro-await-suspend-handle.cpp deleted file mode 100644 index dafb9ba587de50..00000000000000 --- a/clang/test/CIR/CodeGenCoroutines/coro-await-suspend-handle.cpp +++ /dev/null @@ -1,35 +0,0 @@ -// 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 diff --git a/clang/test/CIR/CodeGenCoroutines/coro-await-suspend.cpp b/clang/test/CIR/CodeGenCoroutines/coro-await-suspend.cpp new file mode 100644 index 00000000000000..86980ce49a791e --- /dev/null +++ b/clang/test/CIR/CodeGenCoroutines/coro-await-suspend.cpp @@ -0,0 +1,68 @@ +// 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; +}; + +struct VoidAwaiter { + bool await_ready() noexcept; + void await_suspend(std::coroutine_handle<>) noexcept; + void await_resume() noexcept; +}; + +struct BoolAwaiter { + bool await_ready() noexcept; + bool await_suspend(std::coroutine_handle<>) noexcept; + void await_resume() noexcept; +}; + +// CIR-LABEL: cir.func coroutine {{.*}} @_Z13await_suspendv + +// CIR: %[[HANDLE_WRAPPER:[0-9]+]] = cir.get_global @_Z13await_suspendv.__await_suspend_wrapper__await : +// CIR-NEXT: cir.coro.intrinsic.await.suspend.handle(%[[HANDLE_AWAITER:[0-9]+]], %[[FRAME:[0-9]+]], %[[HANDLE_WRAPPER]]) +// CIR-NEXT: cir.coro.suspend_point + +// CIR: %[[VOID_WRAPPER:[0-9]+]] = cir.get_global @_Z13await_suspendv.__await_suspend_wrapper__await2 : +// CIR-NEXT: cir.coro.intrinsic.await.suspend.void(%[[VOID_AWAITER:[0-9]+]], %[[FRAME]], %[[VOID_WRAPPER]]) +// CIR-NEXT: cir.coro.suspend_point + +// CIR: %[[BOOL_WRAPPER:[0-9]+]] = cir.get_global @_Z13await_suspendv.__await_suspend_wrapper__await3 : +// CIR-NEXT: %[[SUSPEND_RET:[0-9]+]] = cir.coro.intrinsic.await.suspend.bool(%[[BOOL_AWAITER:[0-9]+]], %[[FRAME]], %[[BOOL_WRAPPER]]) +// CIR-NEXT: cir.coro.suspend_point(%[[SUSPEND_RET]]) + +Task await_suspend() { + co_await HandleAwaiter{}; + co_await VoidAwaiter{}; + co_await BoolAwaiter{}; +} + +// CIR-LABEL: cir.func {{.*}} @_Z13await_suspendv.__await_suspend_wrapper__await( +// CIR: cir.call @_ZN13HandleAwaiter13await_suspendESt16coroutine_handleIvE +// CIR: cir.call @_ZNKSt16coroutine_handleIvE7addressEv +// CIR: cir.return + +// CIR-LABEL: cir.func {{.*}} @_Z13await_suspendv.__await_suspend_wrapper__await2( +// CIR: cir.call @_ZN11VoidAwaiter13await_suspendESt16coroutine_handleIvE +// CIR: cir.return + +// CIR-LABEL: cir.func {{.*}} @_Z13await_suspendv.__await_suspend_wrapper__await3( +// CIR: cir.call @_ZN11BoolAwaiter13await_suspendESt16coroutine_handleIvE +// CIR: cir.return diff --git a/clang/test/CIR/CodeGenCoroutines/coro-await.cpp b/clang/test/CIR/CodeGenCoroutines/coro-await.cpp index c5c3057df33113..1eb785e3df1904 100644 --- a/clang/test/CIR/CodeGenCoroutines/coro-await.cpp +++ b/clang/test/CIR/CodeGenCoroutines/coro-await.cpp @@ -37,8 +37,8 @@ Task test_await_assign() { // CIR: %[[READY:.*]] = cir.call @_ZN7Awaiter11await_readyEv(%{{.*}}) : (!cir.ptr<!rec_Awaiter>{{.*}}) -> (!cir.bool{{.*}}) // CIR: cir.condition(%[[READY]]) // CIR: }, suspend : { - // CIR: cir.call @_ZN7Awaiter13await_suspendESt16coroutine_handleIvE(%{{.*}}) - // CIR: cir.coro.suspend_point + // CIR: cir.coro.intrinsic.await.suspend.void + // CIR-NEXT: cir.coro.suspend_point // CIR: }, resume : { // CIR: %[[RES:.*]] = cir.call @_ZN7Awaiter12await_resumeEv(%{{.*}}) nothrow : (!cir.ptr<!rec_Awaiter>{{.*}}) -> (!cir.ptr<!s32i>{{.*}}) // CIR: cir.store %[[RES]], %[[TMP_ADDR:.*]] : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>> @@ -68,8 +68,8 @@ Task test_await_ref() { // CIR: %[[READY:.*]] = cir.call @_ZN7Awaiter11await_readyEv(%{{.*}}) : (!cir.ptr<!rec_Awaiter>{{.*}}) -> (!cir.bool{{.*}}) // CIR: cir.condition(%[[READY]]) // CIR: }, suspend : { - // CIR: cir.call @_ZN7Awaiter13await_suspendESt16coroutine_handleIvE(%{{.*}}) - // CIR: cir.coro.suspend_point + // CIR: cir.coro.intrinsic.await.suspend.void + // CIR-NEXT: cir.coro.suspend_point // CIR: }, resume : { // CIR: %[[RES:.*]] = cir.call @_ZN7Awaiter12await_resumeEv(%{{.*}}) nothrow : (!cir.ptr<!rec_Awaiter>{{.*}}) -> (!cir.ptr<!s32i>{{.*}}) // CIR: cir.store %[[RES]], %[[TMP_ADDR:.*]] : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>> diff --git a/clang/test/CIR/CodeGenCoroutines/coro-yield.cpp b/clang/test/CIR/CodeGenCoroutines/coro-yield.cpp index cc3b8a74c6867c..b1ab5bbfe14e3b 100644 --- a/clang/test/CIR/CodeGenCoroutines/coro-yield.cpp +++ b/clang/test/CIR/CodeGenCoroutines/coro-yield.cpp @@ -34,8 +34,9 @@ Task test_yield_assign() { // CIR: %[[READY:.*]] = cir.call @_ZN12YieldAwaiter11await_readyEv(%{{.*}}) : (!cir.ptr<!rec_YieldAwaiter>{{.*}}) -> (!cir.bool{{.*}}) // CIR: cir.condition(%[[READY]]) // CIR: }, suspend : { - // CIR: cir.call @_ZN12YieldAwaiter13await_suspendESt16coroutine_handleIvE(%{{.*}}) - // CIR: cir.coro.suspend_point + // CIR: %[[ASSIGN_WRAPPER:.*]] = cir.get_global @_Z17test_yield_assignv.__await_suspend_wrapper__yield : + // CIR-NEXT: cir.coro.intrinsic.await.suspend.void({{.*}}, {{.*}}, %[[ASSIGN_WRAPPER]]) + // CIR-NEXT: cir.coro.suspend_point // CIR: }, resume : { // CIR: %[[RES:.*]] = cir.call @_ZN12YieldAwaiter12await_resumeEv(%{{.*}}) : (!cir.ptr<!rec_YieldAwaiter>{{.*}}) -> (!cir.ptr<!s32i>{{.*}}) // CIR: cir.store %[[RES]], %[[TMP_ADDR:.*]] : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>> @@ -65,8 +66,9 @@ Task test_yield_ref() { // CIR: %[[READY:.*]] = cir.call @_ZN12YieldAwaiter11await_readyEv(%{{.*}}) : (!cir.ptr<!rec_YieldAwaiter>{{.*}}) -> (!cir.bool{{.*}}) // CIR: cir.condition(%[[READY]]) // CIR: }, suspend : { - // CIR: cir.call @_ZN12YieldAwaiter13await_suspendESt16coroutine_handleIvE(%{{.*}}) - // CIR: cir.coro.suspend_point + // CIR: %[[REF_WRAPPER:.*]] = cir.get_global @_Z14test_yield_refv.__await_suspend_wrapper__yield : + // CIR-NEXT: cir.coro.intrinsic.await.suspend.void({{.*}}, {{.*}}, %[[REF_WRAPPER]]) + // CIR-NEXT: cir.coro.suspend_point // CIR: }, resume : { // CIR: %[[RES:.*]] = cir.call @_ZN12YieldAwaiter12await_resumeEv(%{{.*}}) : (!cir.ptr<!rec_YieldAwaiter>{{.*}}) -> (!cir.ptr<!s32i>{{.*}}) // CIR: cir.store %[[RES]], %[[TMP_ADDR:.*]] : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>> >From 1ffe30266682de1bb708f6d98b8ea9636e9ea33e Mon Sep 17 00:00:00 2001 From: Kunal Dubey <[email protected]> Date: Wed, 11 Nov 2026 23:28:35 +0530 Subject: [PATCH 4/5] [CIR] Added CIR-FLAT and LLVM tests to new test file --- .../CodeGenCoroutines/coro-await-suspend.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/clang/test/CIR/CodeGenCoroutines/coro-await-suspend.cpp b/clang/test/CIR/CodeGenCoroutines/coro-await-suspend.cpp index 86980ce49a791e..e5304da86c87a7 100644 --- a/clang/test/CIR/CodeGenCoroutines/coro-await-suspend.cpp +++ b/clang/test/CIR/CodeGenCoroutines/coro-await-suspend.cpp @@ -2,6 +2,15 @@ // RUN: -fclangir -Wno-coroutine-missing-unhandled-exception \ // RUN: -emit-cir %s -o %t.cir // RUN: FileCheck --input-file=%t.cir %s --check-prefix=CIR +// RUN: cir-opt --cir-flatten-cfg %t.cir -o %t.flat.cir +// RUN: FileCheck --input-file=%t.flat.cir %s --check-prefix=CIR-FLAT \ +// RUN: --implicit-check-not=cir.coroutine --implicit-check-not=cir.await \ +// RUN: --implicit-check-not=cir.co_return --implicit-check-not=cir.coro.exit \ +// RUN: --implicit-check-not=cir.coro.suspend_point +// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu \ +// RUN: -fclangir -emit-llvm -disable-llvm-passes \ +// RUN: -Wno-coroutine-missing-unhandled-exception %s -o %t.ll +// RUN: FileCheck --input-file=%t.ll %s --check-prefix=LLVM #include "Inputs/coroutine.h" @@ -48,6 +57,24 @@ struct BoolAwaiter { // CIR-NEXT: %[[SUSPEND_RET:[0-9]+]] = cir.coro.intrinsic.await.suspend.bool(%[[BOOL_AWAITER:[0-9]+]], %[[FRAME]], %[[BOOL_WRAPPER]]) // CIR-NEXT: cir.coro.suspend_point(%[[SUSPEND_RET]]) +// CIR-FLAT-LABEL: cir.func flattened_coroutine {{.*}} @_Z13await_suspendv + +// CIR-FLAT: %[[FLAT_HANDLE_WRAPPER:[0-9]+]] = cir.get_global @_Z13await_suspendv.__await_suspend_wrapper__await : +// CIR-FLAT-NEXT: cir.coro.intrinsic.await.suspend.handle({{.*}}, {{.*}}, %[[FLAT_HANDLE_WRAPPER]]) + +// CIR-FLAT: %[[FLAT_VOID_WRAPPER:[0-9]+]] = cir.get_global @_Z13await_suspendv.__await_suspend_wrapper__await2 : +// CIR-FLAT-NEXT: cir.coro.intrinsic.await.suspend.void({{.*}}, {{.*}}, %[[FLAT_VOID_WRAPPER]]) + +// CIR-FLAT: %[[FLAT_BOOL_WRAPPER:[0-9]+]] = cir.get_global @_Z13await_suspendv.__await_suspend_wrapper__await3 : +// CIR-FLAT-NEXT: %[[FLAT_SUSPEND_RET:[0-9]+]] = cir.coro.intrinsic.await.suspend.bool({{.*}}, {{.*}}, %[[FLAT_BOOL_WRAPPER]]) +// CIR-FLAT-NEXT: cir.brcond %[[FLAT_SUSPEND_RET]] + +// LLVM-LABEL: define {{.*}} @_Z13await_suspendv( +// LLVM: call void @llvm.coro.await.suspend.handle(ptr %{{[0-9]+}}, ptr %[[LLVM_FRAME:[0-9]+]], ptr @_Z13await_suspendv.__await_suspend_wrapper__await) +// LLVM: call void @llvm.coro.await.suspend.void(ptr %{{[0-9]+}}, ptr %[[LLVM_FRAME]], ptr @_Z13await_suspendv.__await_suspend_wrapper__await2) +// LLVM: %[[LLVM_SUSPEND_RET:[0-9]+]] = call i1 @llvm.coro.await.suspend.bool(ptr %{{[0-9]+}}, ptr %[[LLVM_FRAME]], ptr @_Z13await_suspendv.__await_suspend_wrapper__await3) +// LLVM-NEXT: br i1 %[[LLVM_SUSPEND_RET]] + Task await_suspend() { co_await HandleAwaiter{}; co_await VoidAwaiter{}; >From 84384b8c4daea5ef92d9a539a935f6ae5f86355d Mon Sep 17 00:00:00 2001 From: Kunal Dubey <[email protected]> Date: Thu, 12 Nov 2026 12:38:52 +0530 Subject: [PATCH 5/5] [CIR] Fixed failing tests --- .../coro-await-suspend-bool.cpp | 2 +- .../test/CIR/CodeGenCoroutines/coro-task.cpp | 100 ++++++------------ 2 files changed, 36 insertions(+), 66 deletions(-) diff --git a/clang/test/CIR/CodeGenCoroutines/coro-await-suspend-bool.cpp b/clang/test/CIR/CodeGenCoroutines/coro-await-suspend-bool.cpp index 299f07efa9bd9b..a32e94e26bf246 100644 --- a/clang/test/CIR/CodeGenCoroutines/coro-await-suspend-bool.cpp +++ b/clang/test/CIR/CodeGenCoroutines/coro-await-suspend-bool.cpp @@ -59,7 +59,7 @@ Task await_bool_veto() { // LLVM-NEXT: br i1 %[[READY]], label %[[RESUME:[0-9]+]], label %[[SUSPEND:[0-9]+]] // LLVM: [[SUSPEND]]: // LLVM-NEXT: %[[SAVE:.*]] = call token @llvm.coro.save(ptr null) - // LLVM: %[[SUSPEND_RET:.*]] = call noundef zeroext i1 @_ZN11BoolAwaiter13await_suspendESt16coroutine_handleIvE(ptr {{.*}} %[[AWAITER]]) + // LLVM: %[[SUSPEND_RET:.*]] = call i1 @llvm.coro.await.suspend.bool(ptr %[[AWAITER]], ptr {{.*}}, ptr @_Z15await_bool_vetov.__await_suspend_wrapper__await) // LLVM-NEXT: br i1 %[[SUSPEND_RET]], label %[[DO_SUSPEND:[0-9]+]], label %[[RESUME]] // LLVM: [[DO_SUSPEND]]: // LLVM-NEXT: %[[RES:.*]] = call i8 @llvm.coro.suspend(token %[[SAVE]], i1 false) diff --git a/clang/test/CIR/CodeGenCoroutines/coro-task.cpp b/clang/test/CIR/CodeGenCoroutines/coro-task.cpp index b83a1d722fd820..ef4a4095b7f3fb 100644 --- a/clang/test/CIR/CodeGenCoroutines/coro-task.cpp +++ b/clang/test/CIR/CodeGenCoroutines/coro-task.cpp @@ -41,8 +41,8 @@ VoidTask silly_task() { // CIR-NEXT: %[[SavedFrameAddr:.*]] = cir.alloca "__coro_frame_addr" {{.*}} : !cir.ptr<!cir.ptr<!void>> // CIR-NEXT: %[[VoidPromisseAddr:.*]] = cir.alloca "__promise" {{.*}} : !cir.ptr<![[VoidPromisse]]> // CIR-NEXT: %[[SuspendAlwaysAddr:.*]] = cir.alloca "ref.tmp0" {{.*}} : !cir.ptr<![[SuspendAlways]]> -// CIR-NEXT: %[[CoroHandleVoidAddr:.*]] = cir.alloca "agg.tmp0" {{.*}} : !cir.ptr<![[CoroHandleVoid]]> -// CIR-NEXT: %[[CoroHandlePromiseAddr:.*]] = cir.alloca "agg.tmp1" {{.*}} : !cir.ptr<![[CoroHandlePromiseVoid]]> +// CIR-NEXT: %[[UserSuspendAlwaysAddr:.*]] = cir.alloca "ref.tmp1" {{.*}} : !cir.ptr<![[SuspendAlways]]> +// CIR-NEXT: %[[FinalSuspendAlwaysAddr:.*]] = cir.alloca "ref.tmp2" {{.*}} : !cir.ptr<![[SuspendAlways]]> // LLVM: define dso_local void @_Z10silly_taskv() #[[CoroAttrs:[0-9]+]] { // LLVM: %[[VoidTaskAddr:.*]] = alloca %[[VoidTask]], align 1 @@ -152,32 +152,18 @@ VoidTask silly_task() { // OGCG: %[[Tmp0:.*]] = call noundef zeroext i1 @_ZNSt14suspend_always11await_readyEv(ptr noundef nonnull align 1 dereferenceable(1) %[[SuspendAlwaysAddr]]) // OGCG: br i1 %[[Tmp0]], label %init.ready, label %init.suspend -// Second region `suspend` contains the actual suspend logic. -// -// - Start by getting the coroutine handle using from_address(). -// - Implicit convert coroutine handle from task specific promisse -// specialization to a void one. -// - Call suspend_always::await_suspend() passing the handle. -// -// FIXME: add veto support for non-void await_suspends. - -// The handle arguments are ignored too, so only the object argument is passed. +// The second region calls the await-suspend intrinsic with the awaiter, frame, +// and wrapper. The wrapper contains the handle construction and await_suspend +// call checked below. // CIR-NEXT: }, suspend : { -// CIR-NEXT: cir.call @_ZNSt16coroutine_handleIN5folly4coro4TaskIvE12promise_typeEE12from_addressEPv(%[[CoroFrameAddr]]) -// CIR-NEXT: %[[FromAddrRes:.*]] = cir.const #cir.poison : ![[CoroHandlePromiseVoid]] -// CIR-NEXT: cir.store{{.*}} %[[FromAddrRes]], %[[CoroHandlePromiseAddr]] : ![[CoroHandlePromiseVoid]] -// CIR-NEXT: %[[CoroHandlePromiseReload:.*]] = cir.load{{.*}} %[[CoroHandlePromiseAddr]] -// CIR-NEXT: cir.call @_ZNSt16coroutine_handleIvEC1IN5folly4coro4TaskIvE12promise_typeEEES_IT_E(%[[CoroHandleVoidAddr]]) -// CIR-NEXT: %[[CoroHandleVoidReload:.*]] = cir.load{{.*}} %[[CoroHandleVoidAddr]] : !cir.ptr<![[CoroHandleVoid]]>, ![[CoroHandleVoid]] -// CIR-NEXT: cir.call @_ZNSt14suspend_always13await_suspendESt16coroutine_handleIvE(%[[SuspendAlwaysAddr]]) +// CIR-NEXT: %[[InitWrapper:.*]] = cir.get_global @_Z10silly_taskv.__await_suspend_wrapper__init +// CIR-NEXT: cir.coro.intrinsic.await.suspend.void(%[[SuspendAlwaysAddr]], %[[CoroFrameAddr]], %[[InitWrapper]]) // CIR-NEXT: cir.coro.suspend_point // LLVM: [[InitSuspend]]: // LLVM: %[[Save:.*]] = call token @llvm.coro.save(ptr null) -// LLVM: call void @_ZNSt16coroutine_handleIN5folly4coro4TaskIvE12promise_typeEE12from_addressEPv(ptr noundef %[[CoroFrameAddr]]) -// LLVM: call void @_ZNSt16coroutine_handleIvEC1IN5folly4coro4TaskIvE12promise_typeEEES_IT_E( -// LLVM: call void @_ZNSt14suspend_always13await_suspendESt16coroutine_handleIvE(ptr noundef nonnull align 1 dereferenceable(1) %[[SuspendAlwaysAddr]]) +// LLVM: call void @llvm.coro.await.suspend.void(ptr %[[SuspendAlwaysAddr]], ptr %[[CoroFrameAddr]], ptr @_Z10silly_taskv.__await_suspend_wrapper__init) // LLVM: %[[SuspendRes:.*]] = call i8 @llvm.coro.suspend(token %[[Save]], i1 false) // LLVM: switch i8 %[[SuspendRes]], label %[[InitRet:[0-9]+]] [ // LLVM: i8 0, label %[[InitReady]] @@ -314,6 +300,14 @@ VoidTask silly_task() { // CIR: cir.return // CIR: } +// CIR-LABEL: cir.func internal private @_Z10silly_taskv.__await_suspend_wrapper__init( +// CIR: %[[CoroHandleVoidAddr:.*]] = cir.alloca "agg.tmp0" {{.*}} : !cir.ptr<![[CoroHandleVoid]]> +// CIR: %[[CoroHandlePromiseAddr:.*]] = cir.alloca "agg.tmp1" {{.*}} : !cir.ptr<![[CoroHandlePromiseVoid]]> +// CIR: cir.call @_ZNSt16coroutine_handleIN5folly4coro4TaskIvE12promise_typeEE12from_addressEPv +// CIR: cir.call @_ZNSt16coroutine_handleIvEC1IN5folly4coro4TaskIvE12promise_typeEEES_IT_E(%[[CoroHandleVoidAddr]]) +// CIR: cir.call @_ZNSt14suspend_always13await_suspendESt16coroutine_handleIvE +// CIR: cir.return + // LLVM: [[CoroRet]]: // LLVM-NEXT: call void @llvm.coro.end(ptr null, i1 false, token none) // LLVM-NEXT: %{{.*}} = load %[[VoidTask]], ptr %[[VoidTaskAddr]] @@ -380,8 +374,8 @@ VoidTask silly_task() { // CIR-FLAT: ^[[INIT_SUSPEND]]: // CIR-FLAT: %[[INIT_SAVE_ARG:.*]] = cir.const #cir.ptr<null> : !cir.ptr<!void> // CIR-FLAT: %[[INIT_SAVE:.*]] = cir.coro.intrinsic.save(%[[INIT_SAVE_ARG]]) : (!cir.ptr<!void>) -> token -// CIR-FLAT: cir.call @_ZNSt16coroutine_handleIN5folly4coro4TaskIvE12promise_typeEE12from_addressEPv(%[[FRAME]]) -// CIR-FLAT: cir.call @_ZNSt14suspend_always13await_suspendESt16coroutine_handleIvE(%[[INIT_AWAITER]]) +// CIR-FLAT: %[[INIT_WRAPPER:.*]] = cir.get_global @_Z10silly_taskv.__await_suspend_wrapper__init +// CIR-FLAT-NEXT: cir.coro.intrinsic.await.suspend.void(%[[INIT_AWAITER]], %[[FRAME]], %[[INIT_WRAPPER]]) // CIR-FLAT: %[[INIT_IS_FINAL:.*]] = cir.const #false // CIR-FLAT: %[[INIT_SUSPEND_RES:.*]] = cir.coro.intrinsic.suspend(%[[INIT_SAVE]], %[[INIT_IS_FINAL]]) : (token, !cir.bool) -> !s8i // CIR-FLAT: cir.switch.flat %[[INIT_SUSPEND_RES]] : !s8i, ^[[INIT_SUSPENDED:bb[0-9]+]] [ @@ -401,7 +395,8 @@ VoidTask silly_task() { // CIR-FLAT: cir.brcond %[[USER_IS_READY]] ^[[USER_RESUME:bb[0-9]+]], ^[[USER_SUSPEND:bb[0-9]+]] // CIR-FLAT: ^[[USER_SUSPEND]]: // CIR-FLAT: %[[USER_SAVE:.*]] = cir.coro.intrinsic.save( -// CIR-FLAT: cir.call @_ZNSt14suspend_always13await_suspendESt16coroutine_handleIvE(%[[USER_AWAITER]]) +// CIR-FLAT: %[[USER_WRAPPER:.*]] = cir.get_global @_Z10silly_taskv.__await_suspend_wrapper__await +// CIR-FLAT-NEXT: cir.coro.intrinsic.await.suspend.void(%[[USER_AWAITER]], %[[FRAME]], %[[USER_WRAPPER]]) // CIR-FLAT: %[[USER_IS_FINAL:.*]] = cir.const #false // CIR-FLAT: %[[USER_SUSPEND_RES:.*]] = cir.coro.intrinsic.suspend(%[[USER_SAVE]], %[[USER_IS_FINAL]]) // CIR-FLAT: cir.switch.flat %[[USER_SUSPEND_RES]] : !s8i, ^[[USER_SUSPENDED:bb[0-9]+]] [ @@ -428,7 +423,8 @@ VoidTask silly_task() { // CIR-FLAT: cir.brcond %[[FINAL_IS_READY]] ^[[FINAL_RESUME:bb[0-9]+]], ^[[FINAL_SUSPEND:bb[0-9]+]] // CIR-FLAT: ^[[FINAL_SUSPEND]]: // CIR-FLAT: %[[FINAL_SAVE:.*]] = cir.coro.intrinsic.save( -// CIR-FLAT: cir.call @_ZNSt14suspend_always13await_suspendESt16coroutine_handleIvE(%[[FINAL_AWAITER]]) +// CIR-FLAT: %[[FINAL_WRAPPER:.*]] = cir.get_global @_Z10silly_taskv.__await_suspend_wrapper__final +// CIR-FLAT-NEXT: cir.coro.intrinsic.await.suspend.void(%[[FINAL_AWAITER]], %[[FRAME]], %[[FINAL_WRAPPER]]) // CIR-FLAT: %[[FINAL_IS_FINAL:.*]] = cir.const #true // CIR-FLAT: %[[FINAL_SUSPEND_RES:.*]] = cir.coro.intrinsic.suspend(%[[FINAL_SAVE]], %[[FINAL_IS_FINAL]]) // CIR-FLAT: cir.switch.flat %[[FINAL_SUSPEND_RES]] : !s8i, ^[[FINAL_SUSPENDED:bb[0-9]+]] [ @@ -483,8 +479,6 @@ folly::coro::Task<int> byRef(const std::string& s) { // CIR: %[[AllocaFnUse:.*]] = cir.alloca "s" {{.*}} init const : !cir.ptr<!cir.ptr<![[StdString]]>> // CIR: %[[IntPromisseAddr:.*]] = cir.alloca "__promise" {{.*}} : !cir.ptr<![[IntPromisse]]> // CIR: %[[SuspendAlwaysAddr:.*]] = cir.alloca "ref.tmp0" {{.*}} : !cir.ptr<![[SuspendAlways]]> -// CIR: %[[CoroHandleVoidAddr:.*]] = cir.alloca "agg.tmp0" {{.*}} : !cir.ptr<![[CoroHandleVoid]]> -// CIR: %[[CoroHandlePromiseAddr:.*]] = cir.alloca "agg.tmp1" {{.*}} : !cir.ptr<![[CoroHandlePromiseInt]]> // CIR: cir.store %[[ARG]], %[[AllocaParam]] : !cir.ptr<![[StdString]]>, {{.*}} // Call promise.get_return_object() to retrieve the task object. @@ -503,13 +497,8 @@ folly::coro::Task<int> byRef(const std::string& s) { // CIR: %[[TmpCallRes:.*]] = cir.call @_ZNSt14suspend_always11await_readyEv(%[[SuspendAlwaysAddr]]) // CIR: cir.condition(%[[TmpCallRes]]) // CIR: }, suspend : { -// CIR: cir.call @_ZNSt16coroutine_handleIN5folly4coro4TaskIiE12promise_typeEE12from_addressEPv(%[[CoroFrameAddr:.*]]) -// CIR: %[[FromAddrRes:.*]] = cir.const #cir.poison : ![[CoroHandlePromiseInt]] -// CIR: cir.store{{.*}} %[[FromAddrRes]], %[[CoroHandlePromiseAddr]] : ![[CoroHandlePromiseInt]] -// CIR: %[[CoroHandlePromiseReload:.*]] = cir.load{{.*}} %[[CoroHandlePromiseAddr]] -// CIR: cir.call @_ZNSt16coroutine_handleIvEC1IN5folly4coro4TaskIiE12promise_typeEEES_IT_E(%[[CoroHandleVoidAddr]]) -// CIR: %[[CoroHandleVoidReload:.*]] = cir.load{{.*}} %[[CoroHandleVoidAddr]] : !cir.ptr<![[CoroHandleVoid]]>, ![[CoroHandleVoid]] -// CIR: cir.call @_ZNSt14suspend_always13await_suspendESt16coroutine_handleIvE(%[[SuspendAlwaysAddr]]) +// CIR: %[[InitWrapper:.*]] = cir.get_global @_Z5byRefRKSt6string.__await_suspend_wrapper__init +// CIR: cir.coro.intrinsic.await.suspend.void(%[[SuspendAlwaysAddr]], %[[CoroFrameAddr:.*]], %[[InitWrapper]]) // CIR: cir.coro.suspend_point // CIR: }, resume : { // CIR: cir.call @_ZNSt14suspend_always12await_resumeEv(%[[SuspendAlwaysAddr]]) @@ -655,16 +644,10 @@ folly::coro::Task<void> yield1() { // CIR-DAG: %[[FRAME:.*]] = cir.alloca "__coro_frame_addr" {{.*}} : !cir.ptr<!cir.ptr<!void>> // CIR-DAG: %[[PROMISE:.*]] = cir.alloca "__promise" {{.*}} : !cir.ptr<![[VoidPromisse]]> // CIR-DAG: %[[SUSP0:.*]] = cir.alloca "ref.tmp0" {{.*}} : !cir.ptr<![[SuspendAlways]]> -// CIR-DAG: %[[CH_VOID0:.*]] = cir.alloca "agg.tmp0" {{.*}} : !cir.ptr<![[CoroHandleVoid]]> -// CIR-DAG: %[[CH_PROM0:.*]] = cir.alloca "agg.tmp1" {{.*}} : !cir.ptr<![[CoroHandlePromiseVoid]]> // CIR-DAG: %[[T_ADDR:.*]] = cir.alloca "t" {{.*}} init : !cir.ptr<![[VoidTask]]> // CIR-DAG: %[[SUSP1:.*]] = cir.alloca "ref.tmp1" {{.*}} : !cir.ptr<![[SuspendAlways]]> -// CIR-DAG: %[[AWAITER_COPY_ADDR:.*]] = cir.alloca "agg.tmp2" {{.*}} : !cir.ptr<![[VoidTask]]> -// CIR-DAG: %[[CH_VOID1:.*]] = cir.alloca "agg.tmp3" {{.*}} : !cir.ptr<![[CoroHandleVoid]]> -// CIR-DAG: %[[CH_PROM1:.*]] = cir.alloca "agg.tmp4" {{.*}} : !cir.ptr<![[CoroHandlePromiseVoid]]> +// CIR-DAG: %[[AWAITER_COPY_ADDR:.*]] = cir.alloca "agg.tmp0" {{.*}} : !cir.ptr<![[VoidTask]]> // CIR-DAG: %[[SUSP2:.*]] = cir.alloca "ref.tmp2" {{.*}} : !cir.ptr<![[SuspendAlways]]> -// CIR-DAG: %[[CH_VOID2:.*]] = cir.alloca "agg.tmp5" {{.*}} : !cir.ptr<![[CoroHandleVoid]]> -// CIR-DAG: %[[CH_PROM2:.*]] = cir.alloca "agg.tmp6" {{.*}} : !cir.ptr<![[CoroHandlePromiseVoid]]> // CIR: cir.coroutine initialSuspend : { // initial_suspend + await(init) @@ -675,13 +658,8 @@ folly::coro::Task<void> yield1() { // CIR: %[[READY0:.*]] = cir.call @_ZNSt14suspend_always11await_readyEv(%[[SUSP0]]){{.*}} // CIR: cir.condition(%[[READY0]]) // CIR: }, suspend : { -// CIR: cir.call @_ZNSt16coroutine_handleIN5folly4coro4TaskIvE12promise_typeEE12from_addressEPv(%{{.*}}){{.*}} -// CIR: %[[FROMADDR0:.*]] = cir.const #cir.poison : ![[CoroHandlePromiseVoid]] -// CIR: cir.store{{.*}} %[[FROMADDR0]], %[[CH_PROM0]] -// CIR: %[[PROM_RELOAD0:.*]] = cir.load{{.*}} %[[CH_PROM0]] -// CIR: cir.call @_ZNSt16coroutine_handleIvEC1IN5folly4coro4TaskIvE12promise_typeEEES_IT_E(%[[CH_VOID0]]){{.*}} -// CIR: %[[VOID_RELOAD0:.*]] = cir.load{{.*}} %[[CH_VOID0]] -// CIR: cir.call @_ZNSt14suspend_always13await_suspendESt16coroutine_handleIvE(%[[SUSP0]]){{.*}} +// CIR: %[[INIT_WRAPPER:.*]] = cir.get_global @_Z6yield1v.__await_suspend_wrapper__init +// CIR: cir.coro.intrinsic.await.suspend.void(%[[SUSP0]], %{{.*}}, %[[INIT_WRAPPER]]) // CIR: cir.coro.suspend_point // CIR: }, resume : { // CIR: cir.call @_ZNSt14suspend_always12await_resumeEv(%[[SUSP0]]){{.*}} @@ -702,13 +680,8 @@ folly::coro::Task<void> yield1() { // CIR: %[[READY1:.*]] = cir.call @_ZNSt14suspend_always11await_readyEv(%[[SUSP1]]){{.*}} // CIR: cir.condition(%[[READY1]]) // CIR: }, suspend : { -// CIR: cir.call @_ZNSt16coroutine_handleIN5folly4coro4TaskIvE12promise_typeEE12from_addressEPv(%{{.*}}){{.*}} -// CIR: %[[FROMADDR1:.*]] = cir.const #cir.poison : ![[CoroHandlePromiseVoid]] -// CIR: cir.store{{.*}} %[[FROMADDR1]], %[[CH_PROM1]] -// CIR: %[[PROM_RELOAD1:.*]] = cir.load{{.*}} %[[CH_PROM1]] -// CIR: cir.call @_ZNSt16coroutine_handleIvEC1IN5folly4coro4TaskIvE12promise_typeEEES_IT_E(%[[CH_VOID1]]){{.*}} -// CIR: %[[VOID_RELOAD1:.*]] = cir.load{{.*}} %[[CH_VOID1]] -// CIR: cir.call @_ZNSt14suspend_always13await_suspendESt16coroutine_handleIvE(%[[SUSP1]]){{.*}} +// CIR: %[[YIELD_WRAPPER:.*]] = cir.get_global @_Z6yield1v.__await_suspend_wrapper__yield +// CIR: cir.coro.intrinsic.await.suspend.void(%[[SUSP1]], %{{.*}}, %[[YIELD_WRAPPER]]) // CIR: cir.coro.suspend_point // CIR: }, resume : { // CIR: cir.call @_ZNSt14suspend_always12await_resumeEv(%[[SUSP1]]){{.*}} @@ -726,13 +699,8 @@ folly::coro::Task<void> yield1() { // CIR: %[[READY2:.*]] = cir.call @_ZNSt14suspend_always11await_readyEv(%[[SUSP2]]){{.*}} // CIR: cir.condition(%[[READY2]]) // CIR: }, suspend : { -// CIR: cir.call @_ZNSt16coroutine_handleIN5folly4coro4TaskIvE12promise_typeEE12from_addressEPv(%{{.*}}){{.*}} -// CIR: %[[FROMADDR2:.*]] = cir.const #cir.poison : ![[CoroHandlePromiseVoid]] -// CIR: cir.store{{.*}} %[[FROMADDR2]], %[[CH_PROM2]] -// CIR: %[[PROM_RELOAD2:.*]] = cir.load{{.*}} %[[CH_PROM2]] -// CIR: cir.call @_ZNSt16coroutine_handleIvEC1IN5folly4coro4TaskIvE12promise_typeEEES_IT_E(%[[CH_VOID2]]){{.*}} -// CIR: %[[VOID_RELOAD2:.*]] = cir.load{{.*}} %[[CH_VOID2]] -// CIR: cir.call @_ZNSt14suspend_always13await_suspendESt16coroutine_handleIvE(%[[SUSP2]]){{.*}} +// CIR: %[[FINAL_WRAPPER:.*]] = cir.get_global @_Z6yield1v.__await_suspend_wrapper__final +// CIR: cir.coro.intrinsic.await.suspend.void(%[[SUSP2]], %{{.*}}, %[[FINAL_WRAPPER]]) // CIR: cir.coro.suspend_point // CIR: }, resume : { // CIR: cir.call @_ZNSt14suspend_always12await_resumeEv(%[[SUSP2]]){{.*}} @@ -757,7 +725,8 @@ folly::coro::Task<void> yield1() { // CIR-FLAT: cir.brcond %[[READY]] ^[[YIELD_RESUME:bb[0-9]+]], ^[[YIELD_SUSPEND:bb[0-9]+]] // CIR-FLAT: ^[[YIELD_SUSPEND]]: // CIR-FLAT: %[[SAVE:.*]] = cir.coro.intrinsic.save( -// CIR-FLAT: cir.call @_ZNSt14suspend_always13await_suspendESt16coroutine_handleIvE(%[[YIELD_AWAITER]]) +// CIR-FLAT: %[[YIELD_WRAPPER:.*]] = cir.get_global @_Z6yield1v.__await_suspend_wrapper__yield +// CIR-FLAT-NEXT: cir.coro.intrinsic.await.suspend.void(%[[YIELD_AWAITER]], %{{.*}}, %[[YIELD_WRAPPER]]) // CIR-FLAT: %[[IS_FINAL:.*]] = cir.const #false // CIR-FLAT: %[[RES:.*]] = cir.coro.intrinsic.suspend(%[[SAVE]], %[[IS_FINAL]]) // CIR-FLAT: cir.switch.flat %[[RES]] : !s8i, ^[[YIELD_SUSPENDED:bb[0-9]+]] [ @@ -884,7 +853,8 @@ folly::coro::Task<int> go1() { // CIR-FLAT: %[[READY:.*]] = cir.call @_ZN5folly4coro4TaskIiE11await_readyEv(%[[TASK]]) // CIR-FLAT: cir.brcond %[[READY]] ^[[RESUME:bb[0-9]+]], ^[[SUSPEND:bb[0-9]+]] // CIR-FLAT: ^[[SUSPEND]]: -// CIR-FLAT: cir.call @_ZN5folly4coro4TaskIiE13await_suspendESt16coroutine_handleIvE(%[[TASK]]) +// CIR-FLAT: %[[AWAIT_WRAPPER:.*]] = cir.get_global @_Z3go1v.__await_suspend_wrapper__await +// CIR-FLAT-NEXT: cir.coro.intrinsic.await.suspend.void(%[[TASK]], %{{.*}}, %[[AWAIT_WRAPPER]]) // CIR-FLAT: cir.const #false // CIR-FLAT: cir.coro.intrinsic.suspend // CIR-FLAT: ^[[RESUME]]: _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
