Author: Aman Maurya Date: 2026-09-29T08:22:57+05:30 New Revision: dae88f2450b03f9a123cd681a7f698f67bde9ece
URL: https://github.com/llvm/llvm-project/commit/dae88f2450b03f9a123cd681a7f698f67bde9ece DIFF: https://github.com/llvm/llvm-project/commit/dae88f2450b03f9a123cd681a7f698f67bde9ece.diff LOG: [CIR] Support aggregate co_await / co_yield in AggExprEmitter (#225412) Support evaluating `co_await` and `co_yield` expressions whose result is an aggregate type in `AggExprEmitter`. Fixes #225317 Added: clang/test/CIR/CodeGenCoroutines/coro-agg.cpp Modified: clang/lib/CIR/CodeGen/CIRGenCoroutine.cpp clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp Removed: ################################################################################ diff --git a/clang/lib/CIR/CodeGen/CIRGenCoroutine.cpp b/clang/lib/CIR/CodeGen/CIRGenCoroutine.cpp index 6f2c0e33ce1d9..b0e03ce10d5a6 100644 --- a/clang/lib/CIR/CodeGen/CIRGenCoroutine.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenCoroutine.cpp @@ -707,25 +707,20 @@ emitSuspendExpression(CIRGenFunction &cgf, CGCoroData &coro, awaitRes.rv = cgf.emitAnyExpr(s.getResumeExpr(), aggSlot, ignoreResult); if (!awaitRes.rv.isIgnored()) { - // Create the alloca in the block before the scope wrapping - // cir.await. - mlir::Value value; RValue rv = awaitRes.rv; - if (rv.isScalar()) { - value = rv.getValue(); - } else if (rv.isComplex()) { - value = rv.getComplexValue(); + if (rv.isScalar() || rv.isComplex()) { + mlir::Value value = + rv.isScalar() ? rv.getValue() : rv.getComplexValue(); + tmpResumeRValAddr = cgf.emitAlloca( + "__coawait_resume_rval", value.getType(), loc, + CharUnits::One(), + builder.getBestAllocaInsertPoint(scopeParentBlock)); + // Store the rvalue so we can reload it before the promise call. + builder.CIRBaseBuilderTy::createStore(loc, value, + tmpResumeRValAddr); } else { - cgf.cgm.errorNYI("emitSuspendExpression: Aggregate value"); - return; + assert(rv.isAggregate() && "unexpected rvalue kind"); } - - tmpResumeRValAddr = cgf.emitAlloca( - "__coawait_resume_rval", value.getType(), loc, CharUnits::One(), - builder.getBestAllocaInsertPoint(scopeParentBlock)); - // Store the rvalue so we can reload it before the promise call. - builder.CIRBaseBuilderTy::createStore(loc, value, - tmpResumeRValAddr); } } @@ -768,9 +763,7 @@ static RValue emitSuspendExpr(CIRGenFunction &cgf, rval.getValue().getType(), tmpResumeRValAddr)); } else if (rval.isAggregate()) { - // This is probably already handled via AggSlot, remove this assertion - // once we have a testcase and prove all pieces work. - cgf.cgm.errorNYI("emitSuspendExpr Aggregate"); + return rval; } else { // complex rval = RValue::getComplex(cir::LoadOp::create( cgf.getBuilder(), scopeLoc, rval.getComplexValue().getType(), diff --git a/clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp b/clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp index e90d075f76500..9cdb57a80deb2 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp @@ -325,14 +325,12 @@ class AggExprEmitter : public StmtVisitor<AggExprEmitter> { Visit(ge->getResultExpr()); } void VisitCoawaitExpr(CoawaitExpr *e) { - cgf.cgm.errorNYI(e->getSourceRange(), "AggExprEmitter: VisitCoawaitExpr"); + cgf.emitCoawaitExpr(*e, dest, dest.isIgnored()); } void VisitCoyieldExpr(CoyieldExpr *e) { - cgf.cgm.errorNYI(e->getSourceRange(), "AggExprEmitter: VisitCoyieldExpr"); - } - void VisitUnaryCoawait(UnaryOperator *e) { - cgf.cgm.errorNYI(e->getSourceRange(), "AggExprEmitter: VisitUnaryCoawait"); + cgf.emitCoyieldExpr(*e, dest, dest.isIgnored()); } + void VisitUnaryCoawait(UnaryOperator *e) { Visit(e->getSubExpr()); } void VisitUnaryExtension(UnaryOperator *e) { Visit(e->getSubExpr()); } void VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *e) { Visit(e->getReplacement()); diff --git a/clang/test/CIR/CodeGenCoroutines/coro-agg.cpp b/clang/test/CIR/CodeGenCoroutines/coro-agg.cpp new file mode 100644 index 0000000000000..816b647c98ff2 --- /dev/null +++ b/clang/test/CIR/CodeGenCoroutines/coro-agg.cpp @@ -0,0 +1,101 @@ +// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -Wno-coroutine-missing-unhandled-exception -emit-cir %s -o %t.cir +// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR +// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -emit-llvm -disable-llvm-passes -Wno-coroutine-missing-unhandled-exception %s -o %t.ll +// RUN: FileCheck --input-file=%t.ll %s -check-prefix=OGCG + +#include "Inputs/coroutine.h" + +struct B { + int x; + int y; + bool await_ready() { return true; } + B await_resume() { return {}; } + template <typename F> void await_suspend(F) {} +}; + +struct coro_t { + struct promise_type { + coro_t get_return_object() { return {}; } + std::suspend_never initial_suspend() { return {}; } + std::suspend_never final_suspend() noexcept { return {}; } + void return_void() {} + static void unhandled_exception() {} + B yield_value(int) { return {}; } + }; +}; + +// CIR-LABEL: cir.func coroutine {{.*}} @_Z22aggregate_coawait_exprv +// OGCG-LABEL: define dso_local void @_Z22aggregate_coawait_exprv +coro_t aggregate_coawait_expr() { + // CIR: %[[VAL:.*]] = cir.alloca "val" align(4) init : !cir.ptr<!rec_B> + // CIR: cir.await(user, ready : { + // CIR: }, suspend : { + // CIR: }, resume : { + // CIR: %[[RES:.*]] = cir.call @_ZN1B12await_resumeEv(%{{.*}}) + // CIR: cir.store %[[RES]], %[[TMP:.*]] : !u64i, !cir.ptr<!u64i> + // CIR: %[[CAST:.*]] = cir.cast bitcast %[[TMP]] : !cir.ptr<!u64i> -> !cir.ptr<!rec_B> + // CIR: %[[LOAD:.*]] = cir.load %[[CAST]] : !cir.ptr<!rec_B>, !rec_B + // CIR: cir.store align(4) %[[LOAD]], %[[VAL]] : !rec_B, !cir.ptr<!rec_B> + // CIR: cir.yield + // CIR: },) + + // OGCG: %[[VAL:.*]] = alloca %struct.B, align 4 + // OGCG: await.ready: + // OGCG: %[[RES:.*]] = call i64 @_ZN1B12await_resumeEv(ptr {{.*}}) + // OGCG: store i64 %[[RES]], ptr %[[VAL]], align 4 + B val = co_await B{}; +} + +// CIR-LABEL: cir.func coroutine {{.*}} @_Z29aggregate_coawait_expr_unusedv +// OGCG-LABEL: define dso_local void @_Z29aggregate_coawait_expr_unusedv +coro_t aggregate_coawait_expr_unused() { + // CIR: cir.await(user, ready : { + // CIR: }, suspend : { + // CIR: }, resume : { + // CIR: %[[RES:.*]] = cir.call @_ZN1B12await_resumeEv(%{{.*}}) + // CIR: cir.yield + // CIR: },) + + // OGCG: await.ready: + // OGCG: %[[RES:.*]] = call i64 @_ZN1B12await_resumeEv(ptr {{.*}}) + // OGCG: store i64 %[[RES]], ptr %{{.*}}, align 4 + co_await B{}; +} + +// CIR-LABEL: cir.func coroutine {{.*}} @_Z22aggregate_coyield_exprv +// OGCG-LABEL: define dso_local void @_Z22aggregate_coyield_exprv +coro_t aggregate_coyield_expr() { + // CIR: %[[VAL:.*]] = cir.alloca "val" align(4) init : !cir.ptr<!rec_B> + // CIR: cir.await(yield, ready : { + // CIR: }, suspend : { + // CIR: }, resume : { + // CIR: %[[RES:.*]] = cir.call @_ZN1B12await_resumeEv(%{{.*}}) + // CIR: cir.store %[[RES]], %[[TMP:.*]] : !u64i, !cir.ptr<!u64i> + // CIR: %[[CAST:.*]] = cir.cast bitcast %[[TMP]] : !cir.ptr<!u64i> -> !cir.ptr<!rec_B> + // CIR: %[[LOAD:.*]] = cir.load %[[CAST]] : !cir.ptr<!rec_B>, !rec_B + // CIR: cir.store align(4) %[[LOAD]], %[[VAL]] : !rec_B, !cir.ptr<!rec_B> + // CIR: cir.yield + // CIR: },) + + // OGCG: %[[VAL:.*]] = alloca %struct.B, align 4 + // OGCG: yield.ready: + // OGCG: %[[RES:.*]] = call i64 @_ZN1B12await_resumeEv(ptr {{.*}}) + // OGCG: store i64 %[[RES]], ptr %[[VAL]], align 4 + B val = co_yield 42; +} + +// CIR-LABEL: cir.func coroutine {{.*}} @_Z29aggregate_coyield_expr_unusedv +// OGCG-LABEL: define dso_local void @_Z29aggregate_coyield_expr_unusedv +coro_t aggregate_coyield_expr_unused() { + // CIR: cir.await(yield, ready : { + // CIR: }, suspend : { + // CIR: }, resume : { + // CIR: %[[RES:.*]] = cir.call @_ZN1B12await_resumeEv(%{{.*}}) + // CIR: cir.yield + // CIR: },) + + // OGCG: yield.ready: + // OGCG: %[[RES:.*]] = call i64 @_ZN1B12await_resumeEv(ptr {{.*}}) + // OGCG: store i64 %[[RES]], ptr %{{.*}}, align 4 + co_yield 42; +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
