Fix incorrect coroutines codegen behavior

If all the suspension calls in a suspend function were "hidden"
under the for-convention (iterator/next/hasNext) calls,
control-flow didn't find them, thus supposing that there is no
suspension points and there is no need to generate a coroutine state machine

The solution is to add relevant calls to CFG

 #KT-15824 Fixed
This commit is contained in:
Denis Zharkov
2017-01-20 15:25:21 +03:00
parent 8cbea903f4
commit 02b40326cc
22 changed files with 609 additions and 291 deletions
@@ -16,28 +16,34 @@ L0:
r(10) -> <v2>
mark(1..10)
call(1..10, rangeTo|<v1>, <v2>) -> <v3>
mark(1..10)
call(1..10, iterator|<v3>) -> <v4>
v(i) INIT: in: {} out: {i=D}
L2 [loop entry point]:
L6 [condition entry point]:
jmp?(L3) INIT: in: {i=I?D} out: {i=I?D}
magic[LOOP_RANGE_ITERATION](1..10|<v3>) -> <v4>
w(i|<v4>) INIT: in: {i=I?D} out: {i=ID}
mark(1..10) INIT: in: {i=I?D} out: {i=I?D}
call(1..10, hasNext) -> <v5>
jmp?(L3)
mark(1..10)
call(1..10, next) -> <v6>
magic[LOOP_RANGE_ITERATION](1..10|<v6>) -> <v7>
w(i|<v7>) INIT: in: {i=I?D} out: {i=ID}
mark(for (i in 1..10) { val a = i }) INIT: in: {i=ID} out: {i=ID}
L4 [body entry point]:
4 mark({ val a = i })
v(val a = i) INIT: in: {i=ID} out: {a=D, i=ID}
r(i) -> <v5> INIT: in: {a=D, i=ID} out: {a=D, i=ID}
w(a|<v5>) INIT: in: {a=D, i=ID} out: {a=ID, i=ID}
r(i) -> <v8> INIT: in: {a=D, i=ID} out: {a=D, i=ID}
w(a|<v8>) INIT: in: {a=D, i=ID} out: {a=ID, i=ID}
3 jmp(L2) INIT: in: {i=ID} out: {i=ID} USE: in: {i=READ} out: {i=READ}
L3 [loop exit point]:
L5 [body exit point]:
read (Unit) INIT: in: {i=I?D} out: {i=I?D}
2 mark("after") INIT: in: {} out: {}
r("after") -> <v6>
r("after") -> <v9>
L1:
1 <END>
error:
<ERROR>
sink:
<SINK> USE: in: {} out: {}
=====================
=====================
@@ -7,12 +7,12 @@ fun foo() {
"after"
}
---------------------
<v4>: Int NEW: magic[LOOP_RANGE_ITERATION](1..10|<v3>) -> <v4>
"before" <v0>: * NEW: r("before") -> <v0>
1 <v1>: Int NEW: r(1) -> <v1>
10 <v2>: Int NEW: r(10) -> <v2>
1..10 <v3>: {<: Iterable<Int>} NEW: call(1..10, rangeTo|<v1>, <v2>) -> <v3>
i <v5>: Int NEW: r(i) -> <v5>
"after" <v6>: * NEW: r("after") -> <v6>
{ "before" for (i in 1..10) { val a = i } "after" } <v6>: * COPY
<v7>: Int NEW: magic[LOOP_RANGE_ITERATION](1..10|<v6>) -> <v7>
"before" <v0>: * NEW: r("before") -> <v0>
1 <v1>: Int NEW: r(1) -> <v1>
10 <v2>: Int NEW: r(10) -> <v2>
1..10 <v6>: {<: Iterable<Int>} NEW: call(1..10, next) -> <v6>
i <v8>: Int NEW: r(i) -> <v8>
"after" <v9>: * NEW: r("after") -> <v9>
{ "before" for (i in 1..10) { val a = i } "after" } <v9>: * COPY
=====================