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
@@ -24,11 +24,11 @@ digraph bangbang_kt {
color=blue
5 [label="Enter block"];
6 [label="Access variable R|<local>/a|"];
7 [label="Check not null: R|<local>/a|!!"];
8 [label="Function call: R|<local>/a|!!.R|/A.foo|()"];
7 [label="Check not null: R|<local>/a|!!" style="filled" fillcolor=yellow];
8 [label="Function call: R|<local>/a|!!.R|/A.foo|()" style="filled" fillcolor=yellow];
9 [label="Access variable R|<local>/a|"];
10 [label="Smart cast: R|<local>/a|"];
11 [label="Function call: R|<local>/a|.R|/A.foo|()"];
11 [label="Function call: R|<local>/a|.R|/A.foo|()" style="filled" fillcolor=yellow];
12 [label="Exit block"];
}
13 [label="Exit function test_0" style="filled" fillcolor=red];
@@ -56,8 +56,8 @@ digraph bangbang_kt {
color=blue
17 [label="Enter when branch condition "];
18 [label="Access variable R|<local>/a|"];
19 [label="Check not null: R|<local>/a|!!"];
20 [label="Function call: R|<local>/a|!!.R|/A.foo|()"];
19 [label="Check not null: R|<local>/a|!!" style="filled" fillcolor=yellow];
20 [label="Function call: R|<local>/a|!!.R|/A.foo|()" style="filled" fillcolor=yellow];
21 [label="Exit when branch condition"];
}
22 [label="Synthetic else branch"];
@@ -67,7 +67,7 @@ digraph bangbang_kt {
24 [label="Enter block"];
25 [label="Access variable R|<local>/a|"];
26 [label="Smart cast: R|<local>/a|"];
27 [label="Function call: R|<local>/a|.R|/A.foo|()"];
27 [label="Function call: R|<local>/a|.R|/A.foo|()" style="filled" fillcolor=yellow];
28 [label="Exit block"];
}
29 [label="Exit when branch result"];
@@ -75,7 +75,7 @@ digraph bangbang_kt {
}
31 [label="Access variable R|<local>/a|"];
32 [label="Smart cast: R|<local>/a|"];
33 [label="Function call: R|<local>/a|.R|/A.foo|()"];
33 [label="Function call: R|<local>/a|.R|/A.foo|()" style="filled" fillcolor=yellow];
34 [label="Exit block"];
}
35 [label="Exit function test_1" style="filled" fillcolor=red];
@@ -118,8 +118,8 @@ digraph bangbang_kt {
color=blue
40 [label="Enter &&"];
41 [label="Access variable R|<local>/a|"];
42 [label="Check not null: R|<local>/a|!!"];
43 [label="Function call: R|<local>/a|!!.R|/A.foo|()"];
42 [label="Check not null: R|<local>/a|!!" style="filled" fillcolor=yellow];
43 [label="Function call: R|<local>/a|!!.R|/A.foo|()" style="filled" fillcolor=yellow];
44 [label="Exit left part of &&"];
45 [label="Enter right part of &&"];
46 [label="Access variable R|<local>/b|"];
@@ -134,7 +134,7 @@ digraph bangbang_kt {
51 [label="Enter block"];
52 [label="Access variable R|<local>/a|"];
53 [label="Smart cast: R|<local>/a|"];
54 [label="Function call: R|<local>/a|.R|/A.foo|()"];
54 [label="Function call: R|<local>/a|.R|/A.foo|()" style="filled" fillcolor=yellow];
55 [label="Exit block"];
}
56 [label="Exit when branch result"];
@@ -142,7 +142,7 @@ digraph bangbang_kt {
}
58 [label="Access variable R|<local>/a|"];
59 [label="Smart cast: R|<local>/a|"];
60 [label="Function call: R|<local>/a|.R|/A.foo|()"];
60 [label="Function call: R|<local>/a|.R|/A.foo|()" style="filled" fillcolor=yellow];
61 [label="Exit block"];
}
62 [label="Exit function test_2" style="filled" fillcolor=red];
@@ -193,8 +193,8 @@ digraph bangbang_kt {
69 [label="Exit left part of &&"];
70 [label="Enter right part of &&"];
71 [label="Access variable R|<local>/a|"];
72 [label="Check not null: R|<local>/a|!!"];
73 [label="Function call: R|<local>/a|!!.R|/A.foo|()"];
72 [label="Check not null: R|<local>/a|!!" style="filled" fillcolor=yellow];
73 [label="Function call: R|<local>/a|!!.R|/A.foo|()" style="filled" fillcolor=yellow];
74 [label="Exit &&"];
}
75 [label="Exit when branch condition"];
@@ -206,14 +206,14 @@ digraph bangbang_kt {
78 [label="Enter block"];
79 [label="Access variable R|<local>/a|"];
80 [label="Smart cast: R|<local>/a|"];
81 [label="Function call: R|<local>/a|.R|/A.foo|()"];
81 [label="Function call: R|<local>/a|.R|/A.foo|()" style="filled" fillcolor=yellow];
82 [label="Exit block"];
}
83 [label="Exit when branch result"];
84 [label="Exit when"];
}
85 [label="Access variable R|<local>/a|"];
86 [label="Function call: R|<local>/a|.<Inapplicable(UNSAFE_CALL): /A.foo>#()"];
86 [label="Function call: R|<local>/a|.<Inapplicable(UNSAFE_CALL): /A.foo>#()" style="filled" fillcolor=yellow];
87 [label="Exit block"];
}
88 [label="Exit function test_3" style="filled" fillcolor=red];
@@ -260,8 +260,8 @@ digraph bangbang_kt {
color=blue
93 [label="Enter ||"];
94 [label="Access variable R|<local>/a|"];
95 [label="Check not null: R|<local>/a|!!"];
96 [label="Function call: R|<local>/a|!!.R|/A.foo|()"];
95 [label="Check not null: R|<local>/a|!!" style="filled" fillcolor=yellow];
96 [label="Function call: R|<local>/a|!!.R|/A.foo|()" style="filled" fillcolor=yellow];
97 [label="Exit left part of ||"];
98 [label="Enter right part of ||"];
99 [label="Access variable R|<local>/b|"];
@@ -276,7 +276,7 @@ digraph bangbang_kt {
104 [label="Enter block"];
105 [label="Access variable R|<local>/a|"];
106 [label="Smart cast: R|<local>/a|"];
107 [label="Function call: R|<local>/a|.R|/A.foo|()"];
107 [label="Function call: R|<local>/a|.R|/A.foo|()" style="filled" fillcolor=yellow];
108 [label="Exit block"];
}
109 [label="Exit when branch result"];
@@ -284,7 +284,7 @@ digraph bangbang_kt {
}
111 [label="Access variable R|<local>/a|"];
112 [label="Smart cast: R|<local>/a|"];
113 [label="Function call: R|<local>/a|.R|/A.foo|()"];
113 [label="Function call: R|<local>/a|.R|/A.foo|()" style="filled" fillcolor=yellow];
114 [label="Exit block"];
}
115 [label="Exit function test_4" style="filled" fillcolor=red];
@@ -335,8 +335,8 @@ digraph bangbang_kt {
122 [label="Exit left part of ||"];
123 [label="Enter right part of ||"];
124 [label="Access variable R|<local>/a|"];
125 [label="Check not null: R|<local>/a|!!"];
126 [label="Function call: R|<local>/a|!!.R|/A.foo|()"];
125 [label="Check not null: R|<local>/a|!!" style="filled" fillcolor=yellow];
126 [label="Function call: R|<local>/a|!!.R|/A.foo|()" style="filled" fillcolor=yellow];
127 [label="Exit ||"];
}
128 [label="Exit when branch condition"];
@@ -347,14 +347,14 @@ digraph bangbang_kt {
color=blue
131 [label="Enter block"];
132 [label="Access variable R|<local>/a|"];
133 [label="Function call: R|<local>/a|.<Inapplicable(UNSAFE_CALL): /A.foo>#()"];
133 [label="Function call: R|<local>/a|.<Inapplicable(UNSAFE_CALL): /A.foo>#()" style="filled" fillcolor=yellow];
134 [label="Exit block"];
}
135 [label="Exit when branch result"];
136 [label="Exit when"];
}
137 [label="Access variable R|<local>/a|"];
138 [label="Function call: R|<local>/a|.<Inapplicable(UNSAFE_CALL): /A.foo>#()"];
138 [label="Function call: R|<local>/a|.<Inapplicable(UNSAFE_CALL): /A.foo>#()" style="filled" fillcolor=yellow];
139 [label="Exit block"];
}
140 [label="Exit function test_5" style="filled" fillcolor=red];
@@ -391,8 +391,8 @@ digraph bangbang_kt {
color=blue
142 [label="Enter block"];
143 [label="Access variable R|<local>/x|"];
144 [label="Check not null: R|<local>/x|!!"];
145 [label="Function call: R|<local>/x|!!.R|/A.foo|()"];
144 [label="Check not null: R|<local>/x|!!" style="filled" fillcolor=yellow];
145 [label="Function call: R|<local>/x|!!.R|/A.foo|()" style="filled" fillcolor=yellow];
146 [label="Exit block"];
}
147 [label="Exit function test_6" style="filled" fillcolor=red];
@@ -411,8 +411,8 @@ digraph bangbang_kt {
color=blue
149 [label="Enter block"];
150 [label="Access variable R|<local>/x|"];
151 [label="Check not null: R|<local>/x|!!"];
152 [label="Function call: R|<local>/x|!!.R|/A.foo|()"];
151 [label="Check not null: R|<local>/x|!!" style="filled" fillcolor=yellow];
152 [label="Function call: R|<local>/x|!!.R|/A.foo|()" style="filled" fillcolor=yellow];
153 [label="Exit block"];
}
154 [label="Exit function test_7" style="filled" fillcolor=red];