[clang] [llvm] [Clang][Coroutines] Improve CoroElide with [[clang::coro_structured_concurrency]] attribute for C++ (PR #94693)

Yuxuan Chen via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 14 16:17:28 PDT 2024


================
@@ -8015,6 +8015,26 @@ but do not pass them to the underlying coroutine or pass them by value.
 }];
 }
 
+def CoroStructuredConcurrencyDoc : Documentation {
+  let Category = DocCatDecl;
+  let Content = [{
+The ``[[clang::coro_structured_concurrency]]`` is a class attribute which can be applied
+to a coroutine return type.
+
+When a coroutine function that returns such a type calls another coroutine function,
+the compiler performs heap allocation elision when the following conditions are all met:
+- callee coroutine function returns a type that is annotated with
+  ``[[clang::coro_structured_concurrency]]``.
+- The callee coroutine function is inlined.
+- In caller coroutine, the return value of the callee is a prvalue or an xvalue, and
+- The temporary expression containing the callee coroutine object is immediately co_awaited.
+
+The behavior is undefined if any of the following condition was met:
+- the caller coroutine is destroyed earlier than the callee coroutine.
----------------
yuxuanchen1997 wrote:

> To make sure we're clear about exactly which case we're talking about, can you write an example that triggers undefined behavior?

Sure. Though the UB needs to be triggered from a place that's either:
1) you have access to the handle to the callee after the caller has been destroyed. 
2) you destroy the caller of a currently running callee (potentially from another thread of execution).

An example would be
```
std::coroutine_handle<> await_suspend(std::coroutine_handle<> caller_handle) {
  caller_handle.destroy();
  return this->handle;
}
```
Such `task` type should not be attributed structured concurrency.

> are you saying that a well-behaved Task will ensure destruction always happens in the right order, regardless of how it's used?
Yes. This is the assumption. The `Task`/`Promise` and even `Awaiter` types holding this attribute should not save/allow extraction of callee handle for the purpose of resumption. When such a way to break the structuredness is provided, the `Task` type should not be attributed as `coro_structured_concurrency`. 

> I'd still like an answer to my question about sanitizers.
Missed this one in my prior response. The UB triggered from violation of the contract is effectively a use-after-free. Not a sanitizers expert, but ASan sounds like able to catch this?

https://github.com/llvm/llvm-project/pull/94693


More information about the cfe-commits mailing list