Skip to content

Commit 1f9c27a

Browse files
yuxuanchen1997Groverkss
authored andcommitted
[Coroutines] Respect noinline attributes when eliding heap allocation (llvm#115384)
1 parent 879e343 commit 1f9c27a

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

llvm/lib/Transforms/Coroutines/CoroSplit.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,9 +2088,9 @@ static void doSplitCoroutine(Function &F, SmallVectorImpl<Function *> &Clones,
20882088

20892089
bool isNoSuspendCoroutine = Shape.CoroSuspends.empty();
20902090

2091-
bool shouldCreateNoAllocVariant = !isNoSuspendCoroutine &&
2092-
Shape.ABI == coro::ABI::Switch &&
2093-
hasSafeElideCaller(F);
2091+
bool shouldCreateNoAllocVariant =
2092+
!isNoSuspendCoroutine && Shape.ABI == coro::ABI::Switch &&
2093+
hasSafeElideCaller(F) && !F.hasFnAttribute(llvm::Attribute::NoInline);
20942094

20952095
// If there are no suspend points, no split required, just remove
20962096
// the allocation and deallocation blocks, they are not needed.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
; Tests that coro-split does not generate noinline variant for noinline functions
2+
; RUN: opt < %s -passes='cgscc(coro-split),simplifycfg,early-cse' -S | FileCheck %s
3+
4+
; CHECK-LABEL: @cannotinline()
5+
define ptr @cannotinline() presplitcoroutine noinline {
6+
entry:
7+
%id = call token @llvm.coro.id(i32 0, ptr null, ptr null, ptr null)
8+
%need.alloc = call i1 @llvm.coro.alloc(token %id)
9+
br i1 %need.alloc, label %dyn.alloc, label %begin
10+
11+
dyn.alloc:
12+
%size = call i32 @llvm.coro.size.i32()
13+
%alloc = call ptr @malloc(i32 %size)
14+
br label %begin
15+
16+
begin:
17+
%phi = phi ptr [ null, %entry ], [ %alloc, %dyn.alloc ]
18+
%hdl = call ptr @llvm.coro.begin(token %id, ptr %phi)
19+
call void @print(i32 0)
20+
%0 = call i8 @llvm.coro.suspend(token none, i1 false)
21+
switch i8 %0, label %suspend [i8 0, label %resume
22+
i8 1, label %cleanup]
23+
resume:
24+
call void @print(i32 1)
25+
br label %cleanup
26+
27+
cleanup:
28+
%mem = call ptr @llvm.coro.free(token %id, ptr %hdl)
29+
call void @free(ptr %mem)
30+
br label %suspend
31+
suspend:
32+
call i1 @llvm.coro.end(ptr %hdl, i1 0, token none)
33+
ret ptr %hdl
34+
}
35+
36+
; CHECK-NOT-LABEL: @cannotinline.noalloc()
37+
38+
39+
; Make a safe_elide call to cannotinline
40+
define void @caller() presplitcoroutine {
41+
entry:
42+
%ptr = call ptr @cannotinline() #1
43+
ret void
44+
}
45+
46+
47+
declare ptr @llvm.coro.free(token, ptr)
48+
declare i32 @llvm.coro.size.i32()
49+
declare i8 @llvm.coro.suspend(token, i1)
50+
declare void @llvm.coro.resume(ptr)
51+
declare void @llvm.coro.destroy(ptr)
52+
53+
declare token @llvm.coro.id(i32, ptr, ptr, ptr)
54+
declare i1 @llvm.coro.alloc(token)
55+
declare ptr @llvm.coro.begin(token, ptr)
56+
declare i1 @llvm.coro.end(ptr, i1, token)
57+
58+
declare noalias ptr @malloc(i32) allockind("alloc,uninitialized") "alloc-family"="malloc"
59+
declare void @print(i32)
60+
declare void @free(ptr) willreturn allockind("free") "alloc-family"="malloc"
61+
62+
attributes #1 = { coro_elide_safe }

0 commit comments

Comments
 (0)