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
@@ -6,7 +6,7 @@ digraph exhaustiveWhenAndDNNType_kt {
subgraph cluster_0 {
color=red
0 [label="Enter function <init>" style="filled" fillcolor=red];
1 [label="Delegated constructor call: super<R|kotlin/Enum<SomeEnum>|>()"];
1 [label="Delegated constructor call: super<R|kotlin/Enum<SomeEnum>|>()" style="filled" fillcolor=yellow];
2 [label="Exit function <init>" style="filled" fillcolor=red];
}
0 -> {1};
@@ -22,7 +22,7 @@ digraph exhaustiveWhenAndDNNType_kt {
subgraph cluster_2 {
color=red
5 [label="Enter function <init>" style="filled" fillcolor=red];
6 [label="Delegated constructor call: super<R|kotlin/Any|>()"];
6 [label="Delegated constructor call: super<R|kotlin/Any|>()" style="filled" fillcolor=yellow];
7 [label="Exit function <init>" style="filled" fillcolor=red];
}
5 -> {6};
@@ -62,7 +62,7 @@ digraph exhaustiveWhenAndDNNType_kt {
color=blue
19 [label="Enter when"];
20 [label="Access variable R|<local>/flag|"];
21 [label="Check not null: R|<local>/flag|!!"];
21 [label="Check not null: R|<local>/flag|!!" style="filled" fillcolor=yellow];
subgraph cluster_9 {
color=blue
22 [label="Enter when branch condition "];
@@ -85,7 +85,7 @@ digraph exhaustiveWhenAndDNNType_kt {
subgraph cluster_11 {
color=blue
35 [label="Enter block"];
36 [label="Function call: R|/B.B|()"];
36 [label="Function call: R|/B.B|()" style="filled" fillcolor=yellow];
37 [label="Exit block"];
}
38 [label="Exit when branch result"];
@@ -93,7 +93,7 @@ digraph exhaustiveWhenAndDNNType_kt {
subgraph cluster_12 {
color=blue
40 [label="Enter block"];
41 [label="Function call: R|/B.B|()"];
41 [label="Function call: R|/B.B|()" style="filled" fillcolor=yellow];
42 [label="Exit block"];
}
43 [label="Exit when branch result"];
@@ -101,7 +101,7 @@ digraph exhaustiveWhenAndDNNType_kt {
}
45 [label="Variable declaration: lval b: R|B|"];
46 [label="Access variable R|<local>/b|"];
47 [label="Function call: R|/takeB|(...)"];
47 [label="Function call: R|/takeB|(...)" style="filled" fillcolor=yellow];
48 [label="Exit block"];
}
49 [label="Exit function test_1" style="filled" fillcolor=red];
@@ -155,7 +155,7 @@ digraph exhaustiveWhenAndDNNType_kt {
color=blue
55 [label="Enter when"];
56 [label="Access variable R|<local>/flag|"];
57 [label="Check not null: R|<local>/flag|!!"];
57 [label="Check not null: R|<local>/flag|!!" style="filled" fillcolor=yellow];
subgraph cluster_16 {
color=blue
58 [label="Enter when branch condition "];
@@ -178,7 +178,7 @@ digraph exhaustiveWhenAndDNNType_kt {
subgraph cluster_18 {
color=blue
71 [label="Enter block"];
72 [label="Function call: R|/B.B|()"];
72 [label="Function call: R|/B.B|()" style="filled" fillcolor=yellow];
73 [label="Exit block"];
}
74 [label="Exit when branch result"];
@@ -186,7 +186,7 @@ digraph exhaustiveWhenAndDNNType_kt {
subgraph cluster_19 {
color=blue
76 [label="Enter block"];
77 [label="Function call: R|/B.B|()"];
77 [label="Function call: R|/B.B|()" style="filled" fillcolor=yellow];
78 [label="Exit block"];
}
79 [label="Exit when branch result"];
@@ -194,7 +194,7 @@ digraph exhaustiveWhenAndDNNType_kt {
}
81 [label="Variable declaration: lval b: R|B|"];
82 [label="Access variable R|<local>/b|"];
83 [label="Function call: R|/takeB|(...)"];
83 [label="Function call: R|/takeB|(...)" style="filled" fillcolor=yellow];
84 [label="Exit block"];
}
85 [label="Exit function test_2" style="filled" fillcolor=red];
@@ -270,7 +270,7 @@ digraph exhaustiveWhenAndDNNType_kt {
subgraph cluster_25 {
color=blue
106 [label="Enter block"];
107 [label="Function call: R|/B.B|()"];
107 [label="Function call: R|/B.B|()" style="filled" fillcolor=yellow];
108 [label="Exit block"];
}
109 [label="Exit when branch result"];
@@ -278,7 +278,7 @@ digraph exhaustiveWhenAndDNNType_kt {
subgraph cluster_26 {
color=blue
111 [label="Enter block"];
112 [label="Function call: R|/B.B|()"];
112 [label="Function call: R|/B.B|()" style="filled" fillcolor=yellow];
113 [label="Exit block"];
}
114 [label="Exit when branch result"];
@@ -286,7 +286,7 @@ digraph exhaustiveWhenAndDNNType_kt {
}
116 [label="Variable declaration: lval b: R|B|"];
117 [label="Access variable R|<local>/b|"];
118 [label="Function call: R|/takeB|(...)"];
118 [label="Function call: R|/takeB|(...)" style="filled" fillcolor=yellow];
119 [label="Exit block"];
}
120 [label="Exit function test_3" style="filled" fillcolor=red];