[clang] [Clang] Propagate elide safe context through [[clang::coro_must_await]] (PR #108474)

Adrian Vogelsgesang via cfe-commits cfe-commits at lists.llvm.org
Sat Sep 14 05:00:29 PDT 2024


================
@@ -8261,12 +8261,19 @@ def CoroAwaitElidableDoc : Documentation {
 The ``[[clang::coro_await_elidable]]`` 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 call to the coroutine function
-is immediately co_awaited as a prvalue. In this case, the coroutine frame for the
-callee will be a local variable within the enclosing braces in the caller's stack
-frame. And the local variable, like other variables in coroutines, may be collected
-into the coroutine frame, which may be allocated on the heap.
+When a coroutine function returns such a type, a direct call expression therein
+that returns a prvalue of a type attributed ``[[clang::coro_await_elidable]]``
+is said to be under a safe elide context if one of the following is true:
----------------
vogelsgesang wrote:

> When a coroutine function returns such a type, a direct call expression therein that returns a prvalue of a type attributed ``[[clang::coro_await_elidable]]`` [...]

If I am reading the code correctly, it is not actually a requirement that the containing / callee coroutine itself is also annotated as `coro_await_elidable`. Only the called coroutine needs to be annotated as `coro_await_elidable`.

I.e., we would also apply HALO for something like

```
  class [[clang::coro_await_elidable]] ElidableTask { ... };
  class NonElidableTask { ... };

  ElidableTask foo();
  NonElidableTask bar() {
    co_await foo(); // foo()'s coroutine frame on this line is elidable
  }
```

Or did I misread the code?

Assuming I understood code correctly, I would propose something like

```
A call to a coroutine function returning such a type is said to be safe-to-elide
if one of the following is true:
- it is the immediate right-hand side operand to a co_await expression.
- it is an argument to a [[clang::coro_must_await]] parameter or parameter pack
   of another safe-to-elide function call.

Do note that the safe-to-elide context applies only to the call expression itself,
and the context does not transitively include any of its subexpressions unless
``[[clang::coro_must_await]]`` is used to opt-in to transivitely applying safe-to-elide.

The compiler performs heap allocation elision on call expressions on safe-to-elide
calls, if the callee is a coroutine.
```

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


More information about the cfe-commits mailing list