Skip to content

Commit 1f72d75

Browse files
committedMay 24, 2017
[coroutines] Allow rematerialization upto 4 times. Remove incorrect assert
Reviewers: majnemer Subscribers: EricWF, llvm-commits Differential Revision: https://reviews.llvm.org/D33524 llvm-svn: 303819
1 parent 07b1ba5 commit 1f72d75

File tree

2 files changed

+71
-15
lines changed

2 files changed

+71
-15
lines changed
 

‎llvm/lib/Transforms/Coroutines/CoroFrame.cpp

+19-15
Original file line numberDiff line numberDiff line change
@@ -799,9 +799,10 @@ void coro::buildCoroutineFrame(Function &F, Shape &Shape) {
799799
splitAround(CSI, "CoroSuspend");
800800
}
801801

802-
// Put fallthrough CoroEnd into its own block. Note: Shape::buildFrom places
803-
// the fallthrough coro.end as the first element of CoroEnds array.
804-
splitAround(Shape.CoroEnds.front(), "CoroEnd");
802+
// Put CoroEnds into their own blocks.
803+
for (CoroEndInst *CE : Shape.CoroEnds) {
804+
splitAround(CE, "CoroEnd");
805+
}
805806

806807
// Transforms multi-edge PHI Nodes, so that any value feeding into a PHI will
807808
// never has its definition separated from the PHI by the suspend point.
@@ -813,19 +814,24 @@ void coro::buildCoroutineFrame(Function &F, Shape &Shape) {
813814
IRBuilder<> Builder(F.getContext());
814815
SpillInfo Spills;
815816

816-
// See if there are materializable instructions across suspend points.
817-
for (Instruction &I : instructions(F))
818-
if (materializable(I))
819-
for (User *U : I.users())
820-
if (Checker.isDefinitionAcrossSuspend(I, U))
821-
Spills.emplace_back(&I, U);
817+
for (int repeat = 0; repeat < 4; ++repeat) {
818+
// See if there are materializable instructions across suspend points.
819+
for (Instruction &I : instructions(F))
820+
if (materializable(I))
821+
for (User *U : I.users())
822+
if (Checker.isDefinitionAcrossSuspend(I, U))
823+
Spills.emplace_back(&I, U);
824+
825+
if (Spills.empty())
826+
break;
822827

823-
// Rewrite materializable instructions to be materialized at the use point.
824-
DEBUG(dump("Materializations", Spills));
825-
rewriteMaterializableInstructions(Builder, Spills);
828+
// Rewrite materializable instructions to be materialized at the use point.
829+
DEBUG(dump("Materializations", Spills));
830+
rewriteMaterializableInstructions(Builder, Spills);
831+
Spills.clear();
832+
}
826833

827834
// Collect the spills for arguments and other not-materializable values.
828-
Spills.clear();
829835
for (Argument &A : F.args())
830836
for (User *U : A.users())
831837
if (Checker.isDefinitionAcrossSuspend(A, U))
@@ -847,8 +853,6 @@ void coro::buildCoroutineFrame(Function &F, Shape &Shape) {
847853
if (I.getType()->isTokenTy())
848854
report_fatal_error(
849855
"token definition is separated from the use by a suspend point");
850-
assert(!materializable(I) &&
851-
"rewriteMaterializable did not do its job");
852856
Spills.emplace_back(&I, U);
853857
}
854858
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
; Verifies that we materialize instruction across suspend points
2+
; RUN: opt < %s -coro-split -S | FileCheck %s
3+
4+
define i8* @f(i32 %n) "coroutine.presplit"="1" {
5+
entry:
6+
%id = call token @llvm.coro.id(i32 0, i8* null, i8* null, i8* null)
7+
%size = call i32 @llvm.coro.size.i32()
8+
%alloc = call i8* @malloc(i32 %size)
9+
%hdl = call i8* @llvm.coro.begin(token %id, i8* %alloc)
10+
11+
%inc1 = add i32 %n, 1
12+
%sp1 = call i8 @llvm.coro.suspend(token none, i1 false)
13+
switch i8 %sp1, label %suspend [i8 0, label %resume1
14+
i8 1, label %cleanup]
15+
resume1:
16+
%inc2 = add i32 %inc1, 1
17+
%sp2 = call i8 @llvm.coro.suspend(token none, i1 false)
18+
switch i8 %sp1, label %suspend [i8 0, label %resume2
19+
i8 1, label %cleanup]
20+
21+
resume2:
22+
call void @print(i32 %inc1)
23+
call void @print(i32 %inc2)
24+
br label %cleanup
25+
26+
cleanup:
27+
%mem = call i8* @llvm.coro.free(token %id, i8* %hdl)
28+
call void @free(i8* %mem)
29+
br label %suspend
30+
suspend:
31+
call i1 @llvm.coro.end(i8* %hdl, i1 0)
32+
ret i8* %hdl
33+
}
34+
35+
; See that we only spilled one value
36+
; CHECK: %f.Frame = type { void (%f.Frame*)*, void (%f.Frame*)*, i1, i1, i32 }
37+
; CHECK-LABEL: @f(
38+
39+
declare i8* @llvm.coro.free(token, i8*)
40+
declare i32 @llvm.coro.size.i32()
41+
declare i8 @llvm.coro.suspend(token, i1)
42+
declare void @llvm.coro.resume(i8*)
43+
declare void @llvm.coro.destroy(i8*)
44+
45+
declare token @llvm.coro.id(i32, i8*, i8*, i8*)
46+
declare i1 @llvm.coro.alloc(token)
47+
declare i8* @llvm.coro.begin(token, i8*)
48+
declare i1 @llvm.coro.end(i8*, i1)
49+
50+
declare noalias i8* @malloc(i32)
51+
declare void @print(i32)
52+
declare void @free(i8*)

0 commit comments

Comments
 (0)
Please sign in to comment.