Skip to content

Commit 20e6515

Browse files
committed
[Coroutines] Mark 'coroutine_handle<>::address' as always-inline
Close #65054 The direct issue is still the call to coroutine_handle<>::address() after await_suspend(). Without optimizations, the current logic will put the temporary result of await_suspend() to the coroutine frame since the middle end feel the temporary is escaped from coroutine_handle<>::address. To fix this fundamentally, we should wrap the whole logic about await-suspend into a standalone function. See #64945 And as a short-term workaround, we probably can mark coroutine_handle<>::address() as always-inline so that the temporary result may not be thought to be escaped then it won't be put on the coroutine frame. Although it looks dirty, it is probably do-able since the compiler are allowed to do special tricks to standard library components.
1 parent bbf0733 commit 20e6515

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

clang/lib/Sema/SemaCoroutine.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,28 @@ static Expr *maybeTailCall(Sema &S, QualType RetType, Expr *E,
344344

345345
Expr *JustAddress = AddressExpr.get();
346346

347+
// FIXME: Without optimizations, the temporary result from `await_suspend()`
348+
// may be put on the coroutine frame since the coroutine frame constructor
349+
// will think the temporary variable will escape from the
350+
// `coroutine_handle<>::address()` call. This is problematic since the
351+
// coroutine should be considered to be suspended after it enters
352+
// `await_suspend` so it shouldn't access/update the coroutine frame after
353+
// that.
354+
//
355+
// See https://github.com/llvm/llvm-project/issues/65054 for the report.
356+
//
357+
// The long term solution may wrap the whole logic about `await-suspend`
358+
// into a standalone function. This is similar to the proposed solution
359+
// in tryMarkAwaitSuspendNoInline. See the comments there for details.
360+
//
361+
// The short term solution here is to mark `coroutine_handle<>::address()`
362+
// function as always-inline so that the coroutine frame constructor won't
363+
// think the temporary result is escaped incorrectly.
364+
if (auto *FD = cast<CallExpr>(JustAddress)->getDirectCallee())
365+
if (!FD->hasAttr<AlwaysInlineAttr>() && !FD->hasAttr<NoInlineAttr>())
366+
FD->addAttr(AlwaysInlineAttr::CreateImplicit(S.getASTContext(),
367+
FD->getLocation()));
368+
347369
// Check that the type of AddressExpr is void*
348370
if (!JustAddress->getType().getTypePtr()->isVoidPointerType())
349371
S.Diag(cast<CallExpr>(JustAddress)->getCalleeDecl()->getLocation(),
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 \
2+
// RUN: -O0 -disable-llvm-passes -emit-llvm %s -o - \
3+
// RUN: | FileCheck %s --check-prefix=FRONTEND
4+
5+
// The output of O0 is highly redundant and hard to test. Also it is not good
6+
// limit the output of O0. So we test the optimized output from O0. The idea
7+
// is the optimizations shouldn't change the semantics of the program.
8+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 \
9+
// RUN: -O0 -emit-llvm %s -o - -disable-O0-optnone \
10+
// RUN: | opt -passes='sroa,mem2reg,simplifycfg' -S | FileCheck %s --check-prefix=CHECK-O0
11+
12+
#include "Inputs/coroutine.h"
13+
14+
// A simple awaiter type with an await_suspend method that can't be
15+
// inlined.
16+
struct Awaiter {
17+
const int& x;
18+
19+
bool await_ready() { return false; }
20+
std::coroutine_handle<> await_suspend(const std::coroutine_handle<> h);
21+
void await_resume() {}
22+
};
23+
24+
struct MyTask {
25+
// A lazy promise with an await_transform method that supports awaiting
26+
// integer references using the Awaiter struct above.
27+
struct promise_type {
28+
MyTask get_return_object() {
29+
return MyTask{
30+
std::coroutine_handle<promise_type>::from_promise(*this),
31+
};
32+
}
33+
34+
std::suspend_always initial_suspend() { return {}; }
35+
std::suspend_always final_suspend() noexcept { return {}; }
36+
void unhandled_exception();
37+
38+
auto await_transform(const int& x) { return Awaiter{x}; }
39+
};
40+
41+
std::coroutine_handle<> h;
42+
};
43+
44+
// A global array of integers.
45+
int g_array[32];
46+
47+
// A coroutine that awaits each integer in the global array.
48+
MyTask FooBar() {
49+
for (const int& x : g_array) {
50+
co_await x;
51+
}
52+
}
53+
54+
// FRONTEND: define{{.*}}@_ZNKSt16coroutine_handleIvE7addressEv{{.*}}#[[address_attr:[0-9]+]]
55+
// FRONTEND: attributes #[[address_attr]] = {{.*}}alwaysinline
56+
57+
// CHECK-O0: define{{.*}}@_Z6FooBarv.resume
58+
// CHECK-O0: call{{.*}}@_ZN7Awaiter13await_suspendESt16coroutine_handleIvE
59+
// CHECK-O0-NOT: store
60+
// CHECK-O0: ret void

0 commit comments

Comments
 (0)