Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Coroutines] Set presplit attribute in Clang instead of CoroEarly pass
Presplit coroutines cannot be inlined. During AlwaysInliner we check if a function is a presplit coroutine, if so we skip inlining. The presplit coroutine attributes are set in CoroEarly pass. However in O0 pipeline, AlwaysInliner runs before CoroEarly, so the attribute isn't set yet and will still inline the coroutine. This causes Clang to crash: https://bugs.llvm.org/show_bug.cgi?id=49920 To fix this, we set the attributes in the Clang front-end instead of in CoroEarly pass. Reviewed By: rjmccall, ChuanqiXu Differential Revision: https://reviews.llvm.org/D100282
- Loading branch information
Showing
16 changed files
with
132 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
clang/test/CodeGenCoroutines/coro-always-inline-resume.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm -fcoroutines-ts \ | ||
// RUN: -fexperimental-new-pass-manager -O0 %s -o - | FileCheck %s | ||
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm -fcoroutines-ts \ | ||
// RUN: -fexperimental-new-pass-manager -fno-inline -O0 %s -o - | FileCheck %s | ||
|
||
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm -fcoroutines-ts \ | ||
// RUN: -O0 %s -o - | FileCheck %s | ||
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm -fcoroutines-ts \ | ||
// RUN: -fno-inline -O0 %s -o - | FileCheck %s | ||
|
||
namespace std { | ||
namespace experimental { | ||
|
||
struct handle {}; | ||
|
||
struct awaitable { | ||
bool await_ready() noexcept { return true; } | ||
// CHECK-NOT: await_suspend | ||
inline void __attribute__((__always_inline__)) await_suspend(handle) noexcept {} | ||
bool await_resume() noexcept { return true; } | ||
}; | ||
|
||
template <typename T> | ||
struct coroutine_handle { | ||
static handle from_address(void *address) noexcept { return {}; } | ||
}; | ||
|
||
template <typename T = void> | ||
struct coroutine_traits { | ||
struct promise_type { | ||
awaitable initial_suspend() { return {}; } | ||
awaitable final_suspend() noexcept { return {}; } | ||
void return_void() {} | ||
T get_return_object() { return T(); } | ||
void unhandled_exception() {} | ||
}; | ||
}; | ||
} // namespace experimental | ||
} // namespace std | ||
|
||
// CHECK-LABEL: @_Z3foov | ||
// CHECK-LABEL: entry: | ||
// CHECK-NEXT: %this.addr.i{{[0-9]*}} = alloca %"struct.std::experimental::awaitable"*, align 8 | ||
// CHECK-NEXT: %this.addr.i{{[0-9]*}} = alloca %"struct.std::experimental::awaitable"*, align 8 | ||
// CHECK: [[CAST0:%[0-9]+]] = bitcast %"struct.std::experimental::awaitable"** %this.addr.i{{[0-9]*}} to i8* | ||
// CHECK-NEXT: call void @llvm.lifetime.start.p0i8(i64 8, i8* [[CAST0]]) | ||
// CHECK: [[CAST1:%[0-9]+]] = bitcast %"struct.std::experimental::awaitable"** %this.addr.i{{[0-9]*}} to i8* | ||
// CHECK-NEXT: call void @llvm.lifetime.end.p0i8(i64 8, i8* [[CAST1]]) | ||
|
||
// CHECK: [[CAST2:%[0-9]+]] = bitcast %"struct.std::experimental::awaitable"** %this.addr.i{{[0-9]*}} to i8* | ||
// CHECK-NEXT: call void @llvm.lifetime.start.p0i8(i64 8, i8* [[CAST2]]) | ||
// CHECK: [[CAST3:%[0-9]+]] = bitcast %"struct.std::experimental::awaitable"** %this.addr.i{{[0-9]*}} to i8* | ||
// CHECK-NEXT: call void @llvm.lifetime.end.p0i8(i64 8, i8* [[CAST3]]) | ||
void foo() { co_return; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,68 @@ | ||
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm -fcoroutines-ts \ | ||
// RUN: -fexperimental-new-pass-manager -O0 %s -o - | FileCheck %s | ||
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm -fcoroutines-ts \ | ||
// RUN: -fexperimental-new-pass-manager -fno-inline -O0 %s -o - | FileCheck %s | ||
|
||
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm -fcoroutines-ts \ | ||
// RUN: -O0 %s -o - | FileCheck %s | ||
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm -fcoroutines-ts \ | ||
// RUN: -fno-inline -O0 %s -o - | FileCheck %s | ||
|
||
namespace std { | ||
namespace experimental { | ||
|
||
struct handle {}; | ||
|
||
struct awaitable { | ||
bool await_ready() noexcept { return true; } | ||
// CHECK-NOT: await_suspend | ||
inline void __attribute__((__always_inline__)) await_suspend(handle) noexcept {} | ||
bool await_resume() noexcept { return true; } | ||
}; | ||
// RUN: %clang -cc1 -triple x86_64-unknown-linux-gnu -std=c++2a %s -emit-llvm -disable-llvm-passes -o - | FileCheck %s | ||
// RUN: %clang -cc1 -triple x86_64-unknown-linux-gnu -std=c++2a %s -emit-llvm -disable-llvm-passes -o - | opt -always-inline -S | FileCheck --check-prefix=INLINE %s | ||
|
||
template <typename T> | ||
struct coroutine_handle { | ||
static handle from_address(void *address) noexcept { return {}; } | ||
}; | ||
#include "Inputs/coroutine.h" | ||
|
||
namespace coro = std::experimental::coroutines_v1; | ||
|
||
class task { | ||
public: | ||
class promise_type { | ||
public: | ||
task get_return_object() noexcept; | ||
coro::suspend_always initial_suspend() noexcept; | ||
void return_void() noexcept; | ||
void unhandled_exception() noexcept; | ||
|
||
struct final_awaiter { | ||
bool await_ready() noexcept; | ||
void await_suspend(coro::coroutine_handle<promise_type> h) noexcept; | ||
void await_resume() noexcept; | ||
}; | ||
|
||
template <typename T = void> | ||
struct coroutine_traits { | ||
struct promise_type { | ||
awaitable initial_suspend() { return {}; } | ||
awaitable final_suspend() noexcept { return {}; } | ||
void return_void() {} | ||
T get_return_object() { return T(); } | ||
void unhandled_exception() {} | ||
final_awaiter final_suspend() noexcept; | ||
|
||
coro::coroutine_handle<> continuation; | ||
}; | ||
|
||
task(task &&t) noexcept; | ||
~task(); | ||
|
||
class awaiter { | ||
public: | ||
bool await_ready() noexcept; | ||
void await_suspend(coro::coroutine_handle<> continuation) noexcept; | ||
void await_resume() noexcept; | ||
|
||
private: | ||
friend task; | ||
explicit awaiter(coro::coroutine_handle<promise_type> h) noexcept; | ||
coro::coroutine_handle<promise_type> coro_; | ||
}; | ||
|
||
awaiter operator co_await() &&noexcept; | ||
|
||
private: | ||
explicit task(coro::coroutine_handle<promise_type> h) noexcept; | ||
coro::coroutine_handle<promise_type> coro_; | ||
}; | ||
} // namespace experimental | ||
} // namespace std | ||
|
||
// CHECK-LABEL: @_Z3foov | ||
// CHECK-LABEL: entry: | ||
// CHECK-NEXT: %this.addr.i{{[0-9]*}} = alloca %"struct.std::experimental::awaitable"*, align 8 | ||
// CHECK-NEXT: %this.addr.i{{[0-9]*}} = alloca %"struct.std::experimental::awaitable"*, align 8 | ||
// CHECK: [[CAST0:%[0-9]+]] = bitcast %"struct.std::experimental::awaitable"** %this.addr.i{{[0-9]*}} to i8* | ||
// CHECK-NEXT: call void @llvm.lifetime.start.p0i8(i64 8, i8* [[CAST0]]) | ||
// CHECK: [[CAST1:%[0-9]+]] = bitcast %"struct.std::experimental::awaitable"** %this.addr.i{{[0-9]*}} to i8* | ||
// CHECK-NEXT: call void @llvm.lifetime.end.p0i8(i64 8, i8* [[CAST1]]) | ||
|
||
// CHECK: [[CAST2:%[0-9]+]] = bitcast %"struct.std::experimental::awaitable"** %this.addr.i{{[0-9]*}} to i8* | ||
// CHECK-NEXT: call void @llvm.lifetime.start.p0i8(i64 8, i8* [[CAST2]]) | ||
// CHECK: [[CAST3:%[0-9]+]] = bitcast %"struct.std::experimental::awaitable"** %this.addr.i{{[0-9]*}} to i8* | ||
// CHECK-NEXT: call void @llvm.lifetime.end.p0i8(i64 8, i8* [[CAST3]]) | ||
void foo() { co_return; } | ||
|
||
task cee(); | ||
|
||
__attribute__((always_inline)) inline task bar() { | ||
co_await cee(); | ||
co_return; | ||
} | ||
|
||
task foo() { | ||
co_await bar(); | ||
co_return; | ||
} | ||
|
||
// check that Clang front-end will tag bar with both alwaysinline and coroutine presplit | ||
// CHECK: define linkonce_odr void @_Z3barv({{.*}}) #[[ATTR:[0-9]+]] {{.*}} | ||
// CHECK: attributes #[[ATTR]] = { alwaysinline {{.*}} "coroutine.presplit"="0" {{.*}}} | ||
|
||
// check that bar is not inlined even it's marked as always_inline | ||
// INLINE-LABEL: define dso_local void @_Z3foov( | ||
// INLINE: call void @_Z3barv( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters