Commit b34cb17
adce_pass: Drop phinode edges that can be proved unused (#43922)
* optimizer: lift more comparisons
This commit implements more comparison liftings.
Especially, this change enables the compiler to lift `isa`/`isdefined`
checks (i.e. replace a comparison call with ϕ-node by CFG
union-splitting).
For example, the code snippet below will run 500x faster:
```julia
function compute(n)
s = 0
itr = 1:n
st = iterate(itr)
while isdefined(st, 2) # mimic our iteration protocol with
`isdefined`
v, st = st
s += v
st = iterate(itr, st)
end
s
end
```
Although it seems like the codegen for `isa` is fairly optimized already
and so I could not find any performance benefit for `isa`-lifting
(`code_llvm` emits mostly equivalent code), but I hope it's more ideal
if we can do the equivalent optimization on Julia level so that we can
just consult to `code_typed` for performance optimization.
* adce_pass: Drop phinode uses that can be proved unused
Union splitting introduces patterns like:
Consider a case like:
```
f(x::Float64) = println(x)
f(x::SomeBigStruct) = nothing
```
```
goto 2 if not ...
1: a = ::Float64
goto 3
2: b = new(SomeBigStruct, ...)::SomeBigStruct
3: c = phi(a, b)
if !isa(c, Float64) goto 5
4: d = PiNode(c, Float64)
println(d)
goto 6
5: nothing
6: return nothing
```
Now, #43227 will turn this into:
```
goto 2 if not ...
1: a = ::Float64
goto 3
2: b = new(SomeBigStruct, ...)::SomeBigStruct
3: c = phi(a, b)
cond = phi(true, false)
if !cond goto 5
4: d = PiNode(c, Float64)
println(d)
goto 6
5: nothing
6: return nothing
```
But even though dynamically `b` is never used,
it doesn't get deleted, because there's still a
use in the PhiNode `c`.
This PR teaches adce to recognize this situation and,
for PhiNodes that are only ever used by PiNodes, drop
any edges that are known to be unused. E.g. in the above
case, the adce improvements would turn it into:
```
goto 2 if not ...
1: a = ::Float64
goto 3
2: b = new(SomeBigStruct, ...)::SomeBigStruct
3: c = phi(a)
cond = phi(true, false)
if !cond goto 5
4: d = PiNode(c, Float64)
println(d)
goto 6
5: nothing
6: return nothing
```
Which in turn would let regular DCE drop the allocation:
```
goto 2 if not ...
1: a = ::Float64
goto 3
2: nothing
3: c = phi(a)
cond = phi(true, false)
if !cond goto 5
4: d = PiNode(c, Float64)
println(d)
goto 6
5: nothing
6: return nothing
```
Co-authored-by: Shuhei Kadowaki <aviatesk@gmail.com>
Co-authored-by: Ian Atol <ian.atol@juliacomputing.com>1 parent 40bceb2 commit b34cb17
2 files changed
+82
-5
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1060 | 1060 | | |
1061 | 1061 | | |
1062 | 1062 | | |
| 1063 | + | |
| 1064 | + | |
| 1065 | + | |
| 1066 | + | |
| 1067 | + | |
1063 | 1068 | | |
1064 | 1069 | | |
1065 | 1070 | | |
| |||
1082 | 1087 | | |
1083 | 1088 | | |
1084 | 1089 | | |
| 1090 | + | |
| 1091 | + | |
1085 | 1092 | | |
1086 | 1093 | | |
1087 | 1094 | | |
1088 | 1095 | | |
1089 | | - | |
1090 | | - | |
1091 | | - | |
1092 | | - | |
1093 | | - | |
| 1096 | + | |
| 1097 | + | |
| 1098 | + | |
| 1099 | + | |
| 1100 | + | |
| 1101 | + | |
| 1102 | + | |
| 1103 | + | |
| 1104 | + | |
| 1105 | + | |
| 1106 | + | |
| 1107 | + | |
| 1108 | + | |
| 1109 | + | |
| 1110 | + | |
| 1111 | + | |
| 1112 | + | |
| 1113 | + | |
| 1114 | + | |
| 1115 | + | |
| 1116 | + | |
| 1117 | + | |
| 1118 | + | |
| 1119 | + | |
| 1120 | + | |
| 1121 | + | |
| 1122 | + | |
| 1123 | + | |
| 1124 | + | |
| 1125 | + | |
1094 | 1126 | | |
1095 | 1127 | | |
1096 | 1128 | | |
1097 | 1129 | | |
1098 | 1130 | | |
1099 | 1131 | | |
1100 | 1132 | | |
| 1133 | + | |
| 1134 | + | |
| 1135 | + | |
| 1136 | + | |
| 1137 | + | |
| 1138 | + | |
| 1139 | + | |
| 1140 | + | |
| 1141 | + | |
| 1142 | + | |
| 1143 | + | |
| 1144 | + | |
| 1145 | + | |
| 1146 | + | |
| 1147 | + | |
| 1148 | + | |
| 1149 | + | |
| 1150 | + | |
| 1151 | + | |
| 1152 | + | |
| 1153 | + | |
| 1154 | + | |
| 1155 | + | |
| 1156 | + | |
| 1157 | + | |
| 1158 | + | |
| 1159 | + | |
| 1160 | + | |
1101 | 1161 | | |
1102 | 1162 | | |
1103 | 1163 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
838 | 838 | | |
839 | 839 | | |
840 | 840 | | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
| 848 | + | |
| 849 | + | |
| 850 | + | |
| 851 | + | |
| 852 | + | |
| 853 | + | |
| 854 | + | |
| 855 | + | |
| 856 | + | |
| 857 | + | |
0 commit comments