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
@@ -55,28 +55,27 @@ digraph inPlaceLambdas_kt {
18 [label="Postponed enter to lambda"];
subgraph cluster_9 {
color=blue
27 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
26 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
subgraph cluster_10 {
color=blue
28 [label="Enter block"];
29 [label="Access variable R|<local>/x|"];
30 [label="Smart cast: R|<local>/x|"];
31 [label="Function call: R|<local>/x|.R|/A.foo|()"];
32 [label="Exit block"];
27 [label="Enter block"];
28 [label="Access variable R|<local>/x|"];
29 [label="Smart cast: R|<local>/x|"];
30 [label="Function call: R|<local>/x|.R|/A.foo|()" style="filled" fillcolor=yellow];
31 [label="Exit block"];
}
33 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
32 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
}
19 [label="Call arguments union" style="filled" fillcolor=yellow];
20 [label="Postponed exit from lambda"];
21 [label="Function call: R|kotlin/run|<R|kotlin/Unit|>(...)"];
22 [label="Exit block"];
19 [label="Postponed exit from lambda"];
20 [label="Function call: R|kotlin/run|<R|kotlin/Unit|>(...)" style="filled" fillcolor=yellow];
21 [label="Exit block"];
}
23 [label="Exit when branch result"];
24 [label="Exit when"];
22 [label="Exit when branch result"];
23 [label="Exit when"];
}
25 [label="Exit block"];
24 [label="Exit block"];
}
26 [label="Exit function test_1" style="filled" fillcolor=red];
25 [label="Exit function test_1" style="filled" fillcolor=red];
}
8 -> {9};
9 -> {10};
@@ -85,163 +84,158 @@ digraph inPlaceLambdas_kt {
12 -> {13};
13 -> {14};
14 -> {16 15};
15 -> {24};
15 -> {23};
16 -> {17};
17 -> {18};
18 -> {27};
18 -> {20} [color=red];
18 -> {27} [style=dashed];
19 -> {21} [color=red];
20 -> {21} [color=green];
18 -> {26};
18 -> {19} [color=red];
18 -> {26} [style=dashed];
19 -> {20};
20 -> {21};
21 -> {22};
22 -> {23};
23 -> {24};
24 -> {25};
25 -> {26};
26 -> {27};
27 -> {28};
28 -> {29};
29 -> {30};
30 -> {31};
31 -> {32};
32 -> {33};
33 -> {19} [color=red];
33 -> {20} [color=green];
32 -> {20} [color=red];
32 -> {19} [color=green];
subgraph cluster_11 {
color=red
34 [label="Enter function test_2" style="filled" fillcolor=red];
33 [label="Enter function test_2" style="filled" fillcolor=red];
subgraph cluster_12 {
color=blue
35 [label="Enter block"];
36 [label="Postponed enter to lambda"];
34 [label="Enter block"];
35 [label="Postponed enter to lambda"];
subgraph cluster_13 {
color=blue
45 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
43 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
subgraph cluster_14 {
color=blue
46 [label="Enter block"];
47 [label="Access variable R|<local>/x|"];
48 [label="Type operator: (R|<local>/x| as R|B|)"];
49 [label="Exit block"];
44 [label="Enter block"];
45 [label="Access variable R|<local>/x|"];
46 [label="Type operator: (R|<local>/x| as R|B|)"];
47 [label="Exit block"];
}
50 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
48 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
}
37 [label="Call arguments union" style="filled" fillcolor=yellow];
38 [label="Postponed exit from lambda"];
39 [label="Function call: R|kotlin/run|<R|B|>(...)"];
40 [label="Access variable R|<local>/x|"];
41 [label="Smart cast: R|<local>/x|"];
42 [label="Function call: R|<local>/x|.R|/B.bar|()"];
43 [label="Exit block"];
36 [label="Postponed exit from lambda"];
37 [label="Function call: R|kotlin/run|<R|B|>(...)" style="filled" fillcolor=yellow];
38 [label="Access variable R|<local>/x|"];
39 [label="Smart cast: R|<local>/x|"];
40 [label="Function call: R|<local>/x|.R|/B.bar|()" style="filled" fillcolor=yellow];
41 [label="Exit block"];
}
44 [label="Exit function test_2" style="filled" fillcolor=red];
42 [label="Exit function test_2" style="filled" fillcolor=red];
}
33 -> {34};
34 -> {35};
35 -> {36};
36 -> {45};
36 -> {38} [color=red];
36 -> {45} [style=dashed];
37 -> {39} [color=red];
38 -> {39} [color=green];
35 -> {43};
35 -> {36} [color=red];
35 -> {43} [style=dashed];
36 -> {37};
37 -> {38};
38 -> {39};
39 -> {40};
40 -> {41};
41 -> {42};
42 -> {43};
43 -> {44};
44 -> {45};
45 -> {46};
46 -> {47};
47 -> {48};
48 -> {49};
49 -> {50};
50 -> {37} [color=red];
50 -> {38} [color=green];
48 -> {37} [color=red];
48 -> {36} [color=green];
subgraph cluster_15 {
color=red
51 [label="Enter function test_3" style="filled" fillcolor=red];
49 [label="Enter function test_3" style="filled" fillcolor=red];
subgraph cluster_16 {
color=blue
52 [label="Enter block"];
50 [label="Enter block"];
subgraph cluster_17 {
color=blue
53 [label="Enter when"];
51 [label="Enter when"];
subgraph cluster_18 {
color=blue
54 [label="Enter when branch condition "];
55 [label="Access variable R|<local>/x|"];
56 [label="Type operator: (R|<local>/x| is R|A|)"];
57 [label="Exit when branch condition"];
52 [label="Enter when branch condition "];
53 [label="Access variable R|<local>/x|"];
54 [label="Type operator: (R|<local>/x| is R|A|)"];
55 [label="Exit when branch condition"];
}
58 [label="Synthetic else branch"];
59 [label="Enter when branch result"];
56 [label="Synthetic else branch"];
57 [label="Enter when branch result"];
subgraph cluster_19 {
color=blue
60 [label="Enter block"];
61 [label="Postponed enter to lambda"];
58 [label="Enter block"];
59 [label="Postponed enter to lambda"];
subgraph cluster_20 {
color=blue
73 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
70 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
subgraph cluster_21 {
color=blue
74 [label="Enter block"];
71 [label="Enter block"];
72 [label="Access variable R|<local>/x|"];
73 [label="Smart cast: R|<local>/x|"];
74 [label="Function call: R|<local>/x|.R|/A.foo|()" style="filled" fillcolor=yellow];
75 [label="Access variable R|<local>/x|"];
76 [label="Smart cast: R|<local>/x|"];
77 [label="Function call: R|<local>/x|.R|/A.foo|()"];
78 [label="Access variable R|<local>/x|"];
79 [label="Smart cast: R|<local>/x|"];
80 [label="Type operator: (R|<local>/x| as R|B|)"];
81 [label="Exit block"];
77 [label="Type operator: (R|<local>/x| as R|B|)"];
78 [label="Exit block"];
}
82 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
79 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
}
62 [label="Call arguments union" style="filled" fillcolor=yellow];
63 [label="Postponed exit from lambda"];
64 [label="Function call: R|kotlin/run|<R|B|>(...)"];
65 [label="Access variable R|<local>/x|"];
66 [label="Smart cast: R|<local>/x|"];
67 [label="Function call: R|<local>/x|.R|/B.bar|()"];
68 [label="Exit block"];
60 [label="Postponed exit from lambda"];
61 [label="Function call: R|kotlin/run|<R|B|>(...)" style="filled" fillcolor=yellow];
62 [label="Access variable R|<local>/x|"];
63 [label="Smart cast: R|<local>/x|"];
64 [label="Function call: R|<local>/x|.R|/B.bar|()" style="filled" fillcolor=yellow];
65 [label="Exit block"];
}
69 [label="Exit when branch result"];
70 [label="Exit when"];
66 [label="Exit when branch result"];
67 [label="Exit when"];
}
71 [label="Exit block"];
68 [label="Exit block"];
}
72 [label="Exit function test_3" style="filled" fillcolor=red];
69 [label="Exit function test_3" style="filled" fillcolor=red];
}
49 -> {50};
50 -> {51};
51 -> {52};
52 -> {53};
53 -> {54};
54 -> {55};
55 -> {56};
56 -> {57};
57 -> {59 58};
58 -> {70};
59 -> {60};
55 -> {57 56};
56 -> {67};
57 -> {58};
58 -> {59};
59 -> {70};
59 -> {60} [color=red];
59 -> {70} [style=dashed];
60 -> {61};
61 -> {73};
61 -> {63} [color=red];
61 -> {73} [style=dashed];
62 -> {64} [color=red];
63 -> {64} [color=green];
61 -> {62};
62 -> {63};
63 -> {64};
64 -> {65};
65 -> {66};
66 -> {67};
67 -> {68};
68 -> {69};
69 -> {70};
70 -> {71};
71 -> {72};
72 -> {73};
73 -> {74};
74 -> {75};
75 -> {76};
76 -> {77};
77 -> {78};
78 -> {79};
79 -> {80};
80 -> {81};
81 -> {82};
82 -> {62} [color=red];
82 -> {63} [color=green];
79 -> {61} [color=red];
79 -> {60} [color=green];
}
@@ -6,7 +6,7 @@ digraph lambdaInWhenBranch_kt {
subgraph cluster_0 {
color=red
0 [label="Enter function <init>" style="filled" fillcolor=red];
1 [label="Delegated constructor call: super<R|kotlin/Any|>()"];
1 [label="Delegated constructor call: super<R|kotlin/Any|>()" style="filled" fillcolor=yellow];
2 [label="Exit function <init>" style="filled" fillcolor=red];
}
0 -> {1};
@@ -22,7 +22,7 @@ digraph lambdaInWhenBranch_kt {
subgraph cluster_2 {
color=red
5 [label="Enter function <init>" style="filled" fillcolor=red];
6 [label="Delegated constructor call: super<R|Sealed|>()"];
6 [label="Delegated constructor call: super<R|Sealed|>()" style="filled" fillcolor=yellow];
7 [label="Exit function <init>" style="filled" fillcolor=red];
}
5 -> {6};
@@ -75,7 +75,7 @@ digraph lambdaInWhenBranch_kt {
subgraph cluster_8 {
color=red
21 [label="Enter function <init>" style="filled" fillcolor=red];
22 [label="Delegated constructor call: super<R|Sealed|>()"];
22 [label="Delegated constructor call: super<R|Sealed|>()" style="filled" fillcolor=yellow];
23 [label="Exit function <init>" style="filled" fillcolor=red];
}
21 -> {22};
@@ -135,17 +135,17 @@ digraph lambdaInWhenBranch_kt {
48 [label="Postponed enter to lambda"];
subgraph cluster_18 {
color=blue
83 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
82 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
subgraph cluster_19 {
color=blue
84 [label="Enter block"];
85 [label="Access variable R|<local>/it|"];
86 [label="Exit block"];
83 [label="Enter block"];
84 [label="Access variable R|<local>/it|"];
85 [label="Exit block"];
}
87 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
86 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
}
49 [label="Postponed exit from lambda"];
50 [label="Function call: String().R|kotlin/let|<R|kotlin/String|, R|kotlin/String|>(...)"];
50 [label="Function call: String().R|kotlin/let|<R|kotlin/String|, R|kotlin/String|>(...)" style="filled" fillcolor=yellow];
51 [label="Exit block"];
}
52 [label="Exit when branch result"];
@@ -196,7 +196,6 @@ digraph lambdaInWhenBranch_kt {
}
81 [label="Exit function foo" style="filled" fillcolor=red];
}
82 [label="Merge postponed lambda exits"];
28 -> {29};
29 -> {30};
30 -> {31};
@@ -217,14 +216,14 @@ digraph lambdaInWhenBranch_kt {
45 -> {46};
46 -> {47};
47 -> {48};
48 -> {83};
48 -> {82};
48 -> {49} [color=red];
48 -> {83} [style=dashed];
48 -> {82} [style=dashed];
49 -> {50};
50 -> {51};
51 -> {52};
52 -> {53};
53 -> {82 54};
53 -> {54};
54 -> {55};
55 -> {56};
56 -> {57};
@@ -252,11 +251,11 @@ digraph lambdaInWhenBranch_kt {
78 -> {79};
79 -> {80};
80 -> {81};
82 -> {83};
83 -> {84};
84 -> {85};
85 -> {86};
86 -> {87};
87 -> {82} [color=red];
87 -> {49} [color=green];
86 -> {53} [color=red];
86 -> {49} [color=green];
}
@@ -25,7 +25,7 @@ digraph smartcastOnLambda_kt {
subgraph cluster_4 {
color=blue
10 [label="Enter block"];
11 [label="Function call: R|<local>/func|.R|SubstitutionOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
11 [label="Function call: R|<local>/func|.R|SubstitutionOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()" style="filled" fillcolor=yellow];
12 [label="Exit block"];
}
13 [label="Exit when branch result"];