Skip to content

Commit a94fa2c

Browse files
committed
[Coroutines 1/2] Improve symmetric control transfer feature
Differential Revision: https://reviews.llvm.org/D76911
1 parent 42dc667 commit a94fa2c

File tree

2 files changed

+101
-5
lines changed

2 files changed

+101
-5
lines changed

llvm/lib/Transforms/Coroutines/CoroSplit.cpp

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,37 @@ static bool simplifyTerminatorLeadingToRet(Instruction *InitialInst) {
975975
return false;
976976
}
977977

978+
// Check whether CI obeys the rules of musttail attribute.
979+
static bool shouldBeMustTail(const CallInst &CI, const Function &F) {
980+
if (CI.isInlineAsm())
981+
return false;
982+
983+
// Match prototypes and calling conventions of resume function.
984+
FunctionType *CalleeTy = CI.getFunctionType();
985+
if (!CalleeTy->getReturnType()->isVoidTy() || (CalleeTy->getNumParams() != 1))
986+
return false;
987+
988+
Type *CalleeParmTy = CalleeTy->getParamType(0);
989+
if (!CalleeParmTy->isPointerTy() ||
990+
(CalleeParmTy->getPointerAddressSpace() != 0))
991+
return false;
992+
993+
if (CI.getCallingConv() != F.getCallingConv())
994+
return false;
995+
996+
// CI should not has any ABI-impacting function attributes.
997+
static const Attribute::AttrKind ABIAttrs[] = {
998+
Attribute::StructRet, Attribute::ByVal, Attribute::InAlloca,
999+
Attribute::InReg, Attribute::Returned, Attribute::SwiftSelf,
1000+
Attribute::SwiftError, Attribute::Alignment};
1001+
AttributeList Attrs = CI.getAttributes();
1002+
for (auto AK : ABIAttrs)
1003+
if (Attrs.hasParamAttribute(0, AK))
1004+
return false;
1005+
1006+
return true;
1007+
}
1008+
9781009
// Add musttail to any resume instructions that is immediately followed by a
9791010
// suspend (i.e. ret). We do this even in -O0 to support guaranteed tail call
9801011
// for symmetrical coroutine control transfer (C++ Coroutines TS extension).
@@ -987,11 +1018,8 @@ static void addMustTailToCoroResumes(Function &F) {
9871018
SmallVector<CallInst *, 4> Resumes;
9881019
for (auto &I : instructions(F))
9891020
if (auto *Call = dyn_cast<CallInst>(&I))
990-
if (auto *CalledValue = Call->getCalledValue())
991-
// CoroEarly pass replaced coro resumes with indirect calls to an
992-
// address return by CoroSubFnInst intrinsic. See if it is one of those.
993-
if (isa<CoroSubFnInst>(CalledValue->stripPointerCasts()))
994-
Resumes.push_back(Call);
1021+
if (shouldBeMustTail(*Call, F))
1022+
Resumes.push_back(Call);
9951023

9961024
// Set musttail on those that are followed by a ret instruction.
9971025
for (CallInst *Call : Resumes)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
; Tests that coro-split will convert coro.resume followed by a suspend to a
2+
; musttail call.
3+
; RUN: opt < %s -coro-split -S | FileCheck %s
4+
; RUN: opt < %s -passes=coro-split -S | FileCheck %s
5+
6+
define void @fakeresume1(i8*) {
7+
entry:
8+
ret void;
9+
}
10+
11+
define void @fakeresume2(i64*) {
12+
entry:
13+
ret void;
14+
}
15+
16+
define void @g() #0 {
17+
entry:
18+
%id = call token @llvm.coro.id(i32 0, i8* null, i8* null, i8* null)
19+
%alloc = call i8* @malloc(i64 16) #3
20+
%vFrame = call noalias nonnull i8* @llvm.coro.begin(token %id, i8* %alloc)
21+
22+
%save = call token @llvm.coro.save(i8* null)
23+
call fastcc void @fakeresume1(i8* null)
24+
25+
%suspend = call i8 @llvm.coro.suspend(token %save, i1 false)
26+
switch i8 %suspend, label %exit [
27+
i8 0, label %await.ready
28+
i8 1, label %exit
29+
]
30+
await.ready:
31+
%save2 = call token @llvm.coro.save(i8* null)
32+
call fastcc void @fakeresume2(i64* null)
33+
34+
%suspend2 = call i8 @llvm.coro.suspend(token %save2, i1 false)
35+
switch i8 %suspend2, label %exit [
36+
i8 0, label %exit
37+
i8 1, label %exit
38+
]
39+
exit:
40+
call i1 @llvm.coro.end(i8* null, i1 false)
41+
ret void
42+
}
43+
44+
; Verify that in the initial function resume is not marked with musttail.
45+
; CHECK-LABEL: @g(
46+
; CHECK-NOT: musttail call fastcc void @fakeresume1(i8* null)
47+
48+
; Verify that in the resume part resume call is marked with musttail.
49+
; CHECK-LABEL: @g.resume(
50+
; CHECK: musttail call fastcc void @fakeresume2(i64* null)
51+
; CHECK-NEXT: ret void
52+
53+
declare token @llvm.coro.id(i32, i8* readnone, i8* nocapture readonly, i8*) #1
54+
declare i1 @llvm.coro.alloc(token) #2
55+
declare i64 @llvm.coro.size.i64() #3
56+
declare i8* @llvm.coro.begin(token, i8* writeonly) #2
57+
declare token @llvm.coro.save(i8*) #2
58+
declare i8* @llvm.coro.frame() #3
59+
declare i8 @llvm.coro.suspend(token, i1) #2
60+
declare i8* @llvm.coro.free(token, i8* nocapture readonly) #1
61+
declare i1 @llvm.coro.end(i8*, i1) #2
62+
declare i8* @llvm.coro.subfn.addr(i8* nocapture readonly, i8) #1
63+
declare i8* @malloc(i64)
64+
65+
attributes #0 = { "coroutine.presplit"="1" }
66+
attributes #1 = { argmemonly nounwind readonly }
67+
attributes #2 = { nounwind }
68+
attributes #3 = { nounwind readnone }

0 commit comments

Comments
 (0)