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
@@ -21,8 +21,8 @@ digraph inAnonymousObject_kt {
4 [label="Exit anonymous object expression"];
5 [label="Variable declaration: lval obj: R|<anonymous>|"];
6 [label="Access variable R|<local>/obj|"];
7 [label="Function call: R|<local>/obj|.R|/<anonymous>.run|()"];
8 [label="Function call: R|<local>/d|.R|SubstitutionOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
7 [label="Function call: R|<local>/obj|.R|/<anonymous>.run|()" style="filled" fillcolor=yellow];
8 [label="Function call: R|<local>/d|.R|SubstitutionOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()" style="filled" fillcolor=yellow];
9 [label="Exit block"];
}
10 [label="Exit function foo" style="filled" fillcolor=red];
@@ -54,7 +54,7 @@ digraph inAnonymousObject_kt {
subgraph cluster_3 {
color=red
15 [label="Enter function <init>" style="filled" fillcolor=red];
16 [label="Delegated constructor call: super<R|kotlin/Any|>()"];
16 [label="Delegated constructor call: super<R|kotlin/Any|>()" style="filled" fillcolor=yellow];
17 [label="Exit function <init>" style="filled" fillcolor=red];
}
15 -> {16};
@@ -95,7 +95,7 @@ digraph inAnonymousObject_kt {
subgraph cluster_8 {
color=blue
28 [label="Enter block"];
29 [label="Function call: R|<local>/c|.R|SubstitutionOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
29 [label="Function call: R|<local>/c|.R|SubstitutionOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()" style="filled" fillcolor=yellow];
30 [label="Exit block"];
}
31 [label="Exit function run" style="filled" fillcolor=red];
@@ -10,9 +10,9 @@ digraph inLocalClass_kt {
color=blue
1 [label="Enter block"];
2 [label="Exit local class foo"];
3 [label="Function call: R|/LocalClass.LocalClass|()"];
4 [label="Function call: R|/LocalClass.LocalClass|().R|<local>/run|()"];
5 [label="Function call: R|<local>/e|.R|SubstitutionOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
3 [label="Function call: R|/LocalClass.LocalClass|()" style="filled" fillcolor=yellow];
4 [label="Function call: R|/LocalClass.LocalClass|().R|<local>/run|()" style="filled" fillcolor=yellow];
5 [label="Function call: R|<local>/e|.R|SubstitutionOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()" style="filled" fillcolor=yellow];
6 [label="Exit block"];
}
7 [label="Exit function foo" style="filled" fillcolor=red];
@@ -55,11 +55,11 @@ digraph inLocalClass_kt {
subgraph cluster_4 {
color=red
15 [label="Enter function <init>" style="filled" fillcolor=red];
16 [label="Delegated constructor call: super<R|kotlin/Any|>()"];
16 [label="Delegated constructor call: super<R|kotlin/Any|>()" style="filled" fillcolor=yellow];
subgraph cluster_5 {
color=blue
17 [label="Enter block"];
18 [label="Function call: R|<local>/b|.R|SubstitutionOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
18 [label="Function call: R|<local>/b|.R|SubstitutionOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()" style="filled" fillcolor=yellow];
19 [label="Exit block"];
}
20 [label="Exit function <init>" style="filled" fillcolor=red];
@@ -95,7 +95,7 @@ digraph inLocalClass_kt {
subgraph cluster_9 {
color=blue
28 [label="Enter block"];
29 [label="Function call: R|<local>/d|.R|SubstitutionOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
29 [label="Function call: R|<local>/d|.R|SubstitutionOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()" style="filled" fillcolor=yellow];
30 [label="Exit block"];
}
31 [label="Exit function run" style="filled" fillcolor=red];
@@ -10,8 +10,8 @@ digraph inLocalFunction_kt {
color=blue
1 [label="Enter block"];
2 [label="Local function declaration foo"];
3 [label="Function call: R|<local>/localFun|()"];
4 [label="Function call: R|<local>/b|.R|SubstitutionOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
3 [label="Function call: R|<local>/localFun|()" style="filled" fillcolor=yellow];
4 [label="Function call: R|<local>/b|.R|SubstitutionOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()" style="filled" fillcolor=yellow];
5 [label="Exit block"];
}
6 [label="Exit function foo" style="filled" fillcolor=red];
@@ -23,8 +23,8 @@ digraph inLocalFunction_kt {
color=blue
8 [label="Enter block"];
9 [label="Access variable R|<local>/a|"];
10 [label="Function call: R|<local>/a|.R|SubstitutionOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
11 [label="Function call: R|<local>/a|.R|SubstitutionOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
10 [label="Function call: R|<local>/a|.R|SubstitutionOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()" style="filled" fillcolor=yellow];
11 [label="Function call: R|<local>/a|.R|SubstitutionOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()" style="filled" fillcolor=yellow];
12 [label="Exit block"];
}
13 [label="Exit function localFun" style="filled" fillcolor=red];
@@ -43,7 +43,7 @@ digraph toLocalVariables_kt {
13 [label="Enter block"];
14 [label="Access variable R|<local>/y|"];
15 [label="Variable declaration: lval yCopy: R|() -> kotlin/Unit|"];
16 [label="Function call: R|<local>/yCopy|.R|SubstitutionOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
16 [label="Function call: R|<local>/yCopy|.R|SubstitutionOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()" style="filled" fillcolor=yellow];
17 [label="Exit block"];
}
18 [label="Exit when branch result"];
@@ -52,7 +52,7 @@ digraph toLocalVariables_kt {
color=blue
20 [label="Enter block"];
21 [label="Access variable R|<local>/x|"];
22 [label="Function call: R|/bar|(...)"];
22 [label="Function call: R|/bar|(...)" style="filled" fillcolor=yellow];
23 [label="Exit block"];
}
24 [label="Exit when branch result"];
@@ -61,7 +61,7 @@ digraph toLocalVariables_kt {
26 [label="Variable declaration: lval zCopy: R|() -> kotlin/Unit|"];
27 [label="Access variable R|<local>/z|"];
28 [label="Assignment: R|<local>/zCopy|"];
29 [label="Function call: R|<local>/zCopy|.R|SubstitutionOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
29 [label="Function call: R|<local>/zCopy|.R|SubstitutionOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()" style="filled" fillcolor=yellow];
30 [label="Exit block"];
}
31 [label="Exit function foo" style="filled" fillcolor=red];