FIR CFG: add union nodes

Quick quiz:

 Q: In a CFG, what does `a -> b -> c -> d` mean?
 A: `a`, then `b`, then `c`, then `d`.

 Q: In a CFG, what does `a -> b -> d; a -> c -> d` mean?
 A: `a`, then `b` or `c`, then `d`.

 Q: So how do you encode "a, then (b, then c) or (c, then b), then d`?
 A: You can't.

Problem is, you need to, because that's what `a; run2({ b }, { c }); d`
does when `run2` has a contract that it calls both its lambda arguments
in-place: `shuffle(listOf(block1, block2)).forEach { it() }` is a
perfectly valid implementation for it, as little sense as that makes.

So that's what union nodes solve. When a node implements
`UnionNodeMarker`, its inputs are interpreted as "all visited in some
order" instead of the normal "one of the inputs is visited".

Currently this is used for data flow. It *should* also be used for
control flow, but it isn't. But it should be. But that's not so easy.

BTW, `try` exit is NOT a union node; although lambdas in one branch can
be completed according to types' of lambdas in another, data does not
flow between the branches anyway (since we don't know how much of the
`try` executed before jumping into `catch`, and `catch`es are mutually
exclusive) so a `try` expression is more like `when` than a function
call with called-in-place-exactly-once arguments. The fact that
`exitTryExpression` used `processUnionOfArguments` in a weird way
should've hinted at that, but now we know for certain.
This commit is contained in:
pyos
2022-11-17 12:53:35 +01:00
committed by teamcity
parent 99bebfa183
commit a9be27e330
108 changed files with 4050 additions and 4239 deletions
+96 -98
View File
@@ -9,7 +9,7 @@ digraph lambdas_kt {
subgraph cluster_1 {
color=blue
1 [label="Enter block"];
2 [label="Function call: R|<local>/block|.R|SubstitutionOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
2 [label="Function call: R|<local>/block|.R|SubstitutionOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()" style="filled" fillcolor=yellow];
3 [label="Exit block"];
}
4 [label="Exit function run" style="filled" fillcolor=red];
@@ -49,13 +49,13 @@ digraph lambdas_kt {
24 [label="Enter block"];
25 [label="Access variable R|<local>/x|"];
26 [label="Smart cast: R|<local>/x|"];
27 [label="Function call: R|<local>/x|.R|kotlin/Int.inc|()"];
27 [label="Function call: R|<local>/x|.R|kotlin/Int.inc|()" style="filled" fillcolor=yellow];
28 [label="Exit block"];
}
29 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
}
16 [label="Postponed exit from lambda"];
17 [label="Function call: R|/run|(...)"];
17 [label="Function call: R|/run|(...)" style="filled" fillcolor=yellow];
18 [label="Exit block"];
}
19 [label="Exit when branch result"];
@@ -78,19 +78,19 @@ digraph lambdas_kt {
15 -> {16 23};
15 -> {23} [style=dashed];
16 -> {17};
16 -> {15} [color=green style=dashed];
17 -> {18};
18 -> {19};
19 -> {20};
20 -> {21};
21 -> {22};
23 -> {29 24};
23 -> {24};
24 -> {25};
25 -> {26};
26 -> {27};
27 -> {28};
28 -> {29};
29 -> {16};
29 -> {23} [color=green style=dashed];
subgraph cluster_9 {
color=red
@@ -114,30 +114,29 @@ digraph lambdas_kt {
color=blue
39 [label="Enter block"];
40 [label="Postponed enter to lambda"];
subgraph cluster_14 {
color=blue
49 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
subgraph cluster_15 {
color=blue
50 [label="Enter block"];
51 [label="Access variable R|<local>/x|"];
52 [label="Smart cast: R|<local>/x|"];
53 [label="Function call: R|<local>/x|.R|kotlin/Int.inc|()"];
54 [label="Exit block"];
}
55 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
}
41 [label="Postponed exit from lambda"];
42 [label="Exit anonymous function expression"];
43 [label="Variable declaration: lval lambda: R|() -> kotlin/Int|"];
44 [label="Exit block"];
41 [label="Exit anonymous function expression"];
42 [label="Variable declaration: lval lambda: R|() -> kotlin/Int|"];
43 [label="Exit block"];
}
45 [label="Exit when branch result"];
46 [label="Exit when"];
44 [label="Exit when branch result"];
45 [label="Exit when"];
}
47 [label="Exit block"];
46 [label="Exit block"];
}
48 [label="Exit function test_2" style="filled" fillcolor=red];
47 [label="Exit function test_2" style="filled" fillcolor=red];
}
subgraph cluster_14 {
color=blue
48 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
subgraph cluster_15 {
color=blue
49 [label="Enter block"];
50 [label="Access variable R|<local>/x|"];
51 [label="Smart cast: R|<local>/x|"];
52 [label="Function call: R|<local>/x|.R|kotlin/Int.inc|()" style="filled" fillcolor=yellow];
53 [label="Exit block"];
}
54 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
}
30 -> {31};
31 -> {32};
@@ -146,148 +145,147 @@ digraph lambdas_kt {
34 -> {35};
35 -> {36};
36 -> {38 37};
37 -> {46};
37 -> {45};
38 -> {39};
39 -> {40};
40 -> {41 49};
40 -> {49} [style=dashed];
40 -> {48 41};
40 -> {48} [style=dashed];
41 -> {42};
42 -> {43};
43 -> {44};
44 -> {45};
45 -> {46};
46 -> {47};
47 -> {48};
48 -> {49};
49 -> {50};
50 -> {51};
51 -> {52};
52 -> {53};
53 -> {54};
54 -> {55};
subgraph cluster_16 {
color=red
56 [label="Enter function getInt" style="filled" fillcolor=red];
55 [label="Enter function getInt" style="filled" fillcolor=red];
subgraph cluster_17 {
color=blue
57 [label="Enter block"];
58 [label="Function call: R|<local>/block|.R|SubstitutionOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
59 [label="Const: Int(1)"];
60 [label="Jump: ^getInt Int(1)"];
61 [label="Stub" style="filled" fillcolor=gray];
62 [label="Exit block" style="filled" fillcolor=gray];
56 [label="Enter block"];
57 [label="Function call: R|<local>/block|.R|SubstitutionOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()" style="filled" fillcolor=yellow];
58 [label="Const: Int(1)"];
59 [label="Jump: ^getInt Int(1)"];
60 [label="Stub" style="filled" fillcolor=gray];
61 [label="Exit block" style="filled" fillcolor=gray];
}
63 [label="Exit function getInt" style="filled" fillcolor=red];
62 [label="Exit function getInt" style="filled" fillcolor=red];
}
55 -> {56};
56 -> {57};
57 -> {58};
58 -> {59};
59 -> {60};
60 -> {63};
59 -> {62};
59 -> {60} [style=dotted];
60 -> {61} [style=dotted];
61 -> {62} [style=dotted];
62 -> {63} [style=dotted];
subgraph cluster_18 {
color=red
64 [label="Enter function test_3" style="filled" fillcolor=red];
63 [label="Enter function test_3" style="filled" fillcolor=red];
subgraph cluster_19 {
color=blue
65 [label="Enter block"];
66 [label="Postponed enter to lambda"];
64 [label="Enter block"];
65 [label="Postponed enter to lambda"];
subgraph cluster_20 {
color=blue
73 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
72 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
subgraph cluster_21 {
color=blue
74 [label="Enter block"];
75 [label="Const: Int(1)"];
76 [label="Jump: ^test_3 Int(1)"];
77 [label="Stub" style="filled" fillcolor=gray];
78 [label="Exit block" style="filled" fillcolor=gray];
73 [label="Enter block"];
74 [label="Const: Int(1)"];
75 [label="Jump: ^test_3 Int(1)"];
76 [label="Stub" style="filled" fillcolor=gray];
77 [label="Exit block" style="filled" fillcolor=gray];
}
79 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
78 [label="Exit function anonymousFunction" style="filled" fillcolor=red style="filled" fillcolor=gray];
}
67 [label="Postponed exit from lambda"];
68 [label="Function call: R|/getInt|(...)"];
69 [label="Jump: ^test_3 R|/getInt|(<L> = getInt@fun <anonymous>(): R|kotlin/Unit| <inline=Inline, kind=UNKNOWN> {
66 [label="Postponed exit from lambda"];
67 [label="Function call: R|/getInt|(...)" style="filled" fillcolor=yellow];
68 [label="Jump: ^test_3 R|/getInt|(<L> = getInt@fun <anonymous>(): R|kotlin/Unit| <inline=Inline, kind=UNKNOWN> {
^test_3 Int(1)
}
)"];
70 [label="Stub" style="filled" fillcolor=gray];
71 [label="Exit block" style="filled" fillcolor=gray];
69 [label="Stub" style="filled" fillcolor=gray];
70 [label="Exit block" style="filled" fillcolor=gray];
}
72 [label="Exit function test_3" style="filled" fillcolor=red];
71 [label="Exit function test_3" style="filled" fillcolor=red];
}
63 -> {64};
64 -> {65};
65 -> {66};
66 -> {67 73};
66 -> {73} [style=dashed];
65 -> {66 72};
65 -> {72} [style=dashed];
66 -> {67};
66 -> {65} [color=green style=dashed];
67 -> {68};
68 -> {69};
69 -> {72};
68 -> {71};
68 -> {69} [style=dotted];
69 -> {70} [style=dotted];
70 -> {71} [style=dotted];
71 -> {72} [style=dotted];
73 -> {79 74};
72 -> {73};
73 -> {74};
74 -> {75};
75 -> {76};
76 -> {72};
75 -> {71};
75 -> {76} [style=dotted];
76 -> {77} [style=dotted];
77 -> {78} [style=dotted];
78 -> {79} [style=dotted];
79 -> {67};
79 -> {73} [color=green style=dashed];
78 -> {66} [style=dotted];
subgraph cluster_22 {
color=red
80 [label="Enter function test_4" style="filled" fillcolor=red];
79 [label="Enter function test_4" style="filled" fillcolor=red];
subgraph cluster_23 {
color=blue
81 [label="Enter block"];
82 [label="Postponed enter to lambda"];
80 [label="Enter block"];
81 [label="Postponed enter to lambda"];
subgraph cluster_24 {
color=blue
89 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
88 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
subgraph cluster_25 {
color=blue
90 [label="Enter block"];
91 [label="Const: Int(1)"];
92 [label="Jump: ^test_4 Int(1)"];
93 [label="Stub" style="filled" fillcolor=gray];
94 [label="Exit block" style="filled" fillcolor=gray];
89 [label="Enter block"];
90 [label="Const: Int(1)"];
91 [label="Jump: ^test_4 Int(1)"];
92 [label="Stub" style="filled" fillcolor=gray];
93 [label="Exit block" style="filled" fillcolor=gray];
}
95 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
94 [label="Exit function anonymousFunction" style="filled" fillcolor=red style="filled" fillcolor=gray];
}
83 [label="Postponed exit from lambda"];
84 [label="Function call: R|/getInt|(...)"];
85 [label="Jump: ^test_4 R|/getInt|(block = getInt@fun <anonymous>(): R|kotlin/Unit| <inline=Inline, kind=UNKNOWN> {
82 [label="Postponed exit from lambda"];
83 [label="Function call: R|/getInt|(...)" style="filled" fillcolor=yellow];
84 [label="Jump: ^test_4 R|/getInt|(block = getInt@fun <anonymous>(): R|kotlin/Unit| <inline=Inline, kind=UNKNOWN> {
^test_4 Int(1)
}
)"];
86 [label="Stub" style="filled" fillcolor=gray];
87 [label="Exit block" style="filled" fillcolor=gray];
85 [label="Stub" style="filled" fillcolor=gray];
86 [label="Exit block" style="filled" fillcolor=gray];
}
88 [label="Exit function test_4" style="filled" fillcolor=red];
87 [label="Exit function test_4" style="filled" fillcolor=red];
}
79 -> {80};
80 -> {81};
81 -> {82};
82 -> {83 89};
82 -> {89} [style=dashed];
81 -> {82 88};
81 -> {88} [style=dashed];
82 -> {83};
82 -> {81} [color=green style=dashed];
83 -> {84};
84 -> {85};
85 -> {88};
84 -> {87};
84 -> {85} [style=dotted];
85 -> {86} [style=dotted];
86 -> {87} [style=dotted];
87 -> {88} [style=dotted];
89 -> {95 90};
88 -> {89};
89 -> {90};
90 -> {91};
91 -> {92};
92 -> {88};
91 -> {87};
91 -> {92} [style=dotted];
92 -> {93} [style=dotted];
93 -> {94} [style=dotted];
94 -> {95} [style=dotted];
95 -> {83};
95 -> {89} [color=green style=dashed];
94 -> {82} [style=dotted];
}