[clang] [CIR] Pass the promise address to cir.coro.intrinsic.id (PR #236293)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Oct 27 18:45:07 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clangir
Author: Andres-Salamanca
<details>
<summary>Changes</summary>
CIRGen was emitting `cir.coro.intrinsic.id` with a null promise operand. Classic codegen fills it in with the promise alloca once the promise variable has been emitted. Do the same in CIRGen, bitcasting the promise address to `!cir.ptr<!void>` to match the expected type.
---
Full diff: https://github.com/llvm/llvm-project/pull/236293.diff
2 Files Affected:
- (modified) clang/lib/CIR/CodeGen/CIRGenCoroutine.cpp (+10)
- (modified) clang/test/CIR/CodeGenCoroutines/coro-task.cpp (+4-2)
``````````diff
diff --git a/clang/lib/CIR/CodeGen/CIRGenCoroutine.cpp b/clang/lib/CIR/CodeGen/CIRGenCoroutine.cpp
index f0421572773b11..162fc61bbcf93b 100644
--- a/clang/lib/CIR/CodeGen/CIRGenCoroutine.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenCoroutine.cpp
@@ -423,6 +423,16 @@ CIRGenFunction::emitCoroutineBody(const CoroutineBodyStmt &s) {
auto initialSuspendBuilder = [&]() -> mlir::LogicalResult {
if (emitStmt(s.getPromiseDeclStmt(), /*useCurrentScope=*/true).failed())
return mlir::failure();
+ // Update coro.id to refer to the promise. We could not do it earlier
+ // because the promise local variable was not emitted yet.
+ Address promiseAddr = getAddrOfLocalVar(s.getPromiseDecl());
+ {
+ mlir::OpBuilder::InsertionGuard guard(builder);
+ builder.setInsertionPoint(coroId);
+ mlir::Value promisePtr = builder.createBitcast(
+ promiseAddr.emitRawPointer(), builder.getVoidPtrTy());
+ coroId.getPromiseMutable().assign(promisePtr);
+ }
// returnValue should be valid as long as the coroutine's return type
// is not void. The assertion could help us to reduce the check later.
assert(returnValue.isValid() == (bool)s.getReturnStmt());
diff --git a/clang/test/CIR/CodeGenCoroutines/coro-task.cpp b/clang/test/CIR/CodeGenCoroutines/coro-task.cpp
index 2cbc625cc1ac29..d655f82d50798a 100644
--- a/clang/test/CIR/CodeGenCoroutines/coro-task.cpp
+++ b/clang/test/CIR/CodeGenCoroutines/coro-task.cpp
@@ -46,7 +46,8 @@ VoidTask silly_task() {
// CIR: %[[NullPtr:.*]] = cir.const #cir.ptr<null> : !cir.ptr<!void>
// CIR-NEXT: %[[Align:.*]] = cir.const #cir.int<16> : !u32i
-// CIR-NEXT: %[[CoroId:.*]] = cir.coro.intrinsic.id(%[[Align]], %[[NullPtr]], %[[NullPtr]], %[[NullPtr]]) : (!u32i, !cir.ptr<!void>, !cir.ptr<!void>, !cir.ptr<!void>) -> token
+// CIR-NEXT: %[[PromiseVoidPtr:.*]] = cir.cast bitcast %[[VoidPromisseAddr]] : !cir.ptr<![[VoidPromisse]]> -> !cir.ptr<!void>
+// CIR-NEXT: %[[CoroId:.*]] = cir.coro.intrinsic.id(%[[Align]], %[[PromiseVoidPtr]], %[[NullPtr]], %[[NullPtr]]) : (!u32i, !cir.ptr<!void>, !cir.ptr<!void>, !cir.ptr<!void>) -> token
// OGCG: %[[CoroId:.*]] = call token @llvm.coro.id(i32 16, ptr %[[VoidPromisseAddr]], ptr null, ptr null)
// Perform allocation calling operator 'new' depending on __builtin_coro_alloc and
@@ -267,7 +268,8 @@ VoidTask silly_task() {
// CIR-FLAT: %[[NULL:.*]] = cir.const #cir.ptr<null> : !cir.ptr<!void>
// CIR-FLAT: %[[ALIGN:.*]] = cir.const #cir.int<16> : !u32i
-// CIR-FLAT: %[[CORO_ID:.*]] = cir.coro.intrinsic.id(%[[ALIGN]], %[[NULL]], %[[NULL]], %[[NULL]])
+// CIR-FLAT: %[[PROMISE_VOID_PTR:.*]] = cir.cast bitcast %[[PROMISE]] : {{.*}} -> !cir.ptr<!void>
+// CIR-FLAT: %[[CORO_ID:.*]] = cir.coro.intrinsic.id(%[[ALIGN]], %[[PROMISE_VOID_PTR]], %[[NULL]], %[[NULL]])
// CIR-FLAT: %[[SHOULD_ALLOC:.*]] = cir.coro.intrinsic.alloc(%[[CORO_ID]])
// CIR-FLAT: cir.store %[[NULL]], %[[FRAME_ADDR]]
// CIR-FLAT: cir.brcond %[[SHOULD_ALLOC]] ^[[CORO_ALLOC:bb[0-9]+]], ^[[CORO_INIT:bb[0-9]+]]
``````````
</details>
https://github.com/llvm/llvm-project/pull/236293
More information about the cfe-commits
mailing list