Commit 0c6345f
committed
inlining: relax
Eager `finalizer` inlining (#45272) currently has a restriction that
requires all the def/uses are in a same basic block.
This commit relaxes that restriction a bit by allowing def/uses to
involve control flow when all of them are dominated by a `finalizer`
call to be inlined, since in that case it is safe to insert the body of
`finalizer` at the end of all the def/uses, e.g.
```julia
Base.@assume_effects :nothrow safeprint(@nospecialize x...) = print(x...)
@noinline nothrow_side_effect(x) =
Base.@assume_effects :total !:effect_free @CCall jl_(x::Any)::Cvoid
mutable struct DoAllocWithFieldInter
x
end
function register_finalizer!(obj::DoAllocWithFieldInter)
finalizer(obj) do this
nothrow_side_effect(nothing)
end
end
let src = code_typed1() do
for i = -1000:1000
o = DoAllocWithFieldInter(i)
register_finalizer!(o)
if i == 1000
safeprint(o.x, '\n')
elseif i > 0
safeprint(o.x)
end
end
end
# the finalzier call gets inlined
@test count(isinvoke(:nothrow_side_effect), src.code) == 1
end
```
There is a room for further improvement though -- if we have a way to
compute the post-domination, we can also inline a `finalizer` call
in a case when a `finalizer` block is post-dominated by all the def/uses
e.g.
```
let src = code_typed1() do
for i = -1000:1000
o = DoAllocWithFieldInter(i)
if i == 1000
safeprint(o.x, '\n')
elseif i > 0
safeprint(o.x)
end
register_finalizer!(o)
end
end
# currently this is broken
@test_broken count(isinvoke(:nothrow_side_effect), src.code) == 1
end
```finalizer inlining control-flow restriction1 parent d7b4dbe commit 0c6345f
File tree
3 files changed
+123
-23
lines changed- base/compiler/ssair
- test/compiler
3 files changed
+123
-23
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1100 | 1100 | | |
1101 | 1101 | | |
1102 | 1102 | | |
1103 | | - | |
1104 | | - | |
1105 | | - | |
1106 | | - | |
| 1103 | + | |
| 1104 | + | |
| 1105 | + | |
1107 | 1106 | | |
1108 | 1107 | | |
1109 | | - | |
1110 | | - | |
1111 | | - | |
1112 | | - | |
| 1108 | + | |
| 1109 | + | |
| 1110 | + | |
| 1111 | + | |
| 1112 | + | |
| 1113 | + | |
| 1114 | + | |
| 1115 | + | |
| 1116 | + | |
| 1117 | + | |
| 1118 | + | |
| 1119 | + | |
| 1120 | + | |
| 1121 | + | |
| 1122 | + | |
| 1123 | + | |
| 1124 | + | |
| 1125 | + | |
| 1126 | + | |
| 1127 | + | |
| 1128 | + | |
| 1129 | + | |
| 1130 | + | |
| 1131 | + | |
| 1132 | + | |
1113 | 1133 | | |
1114 | | - | |
1115 | | - | |
1116 | | - | |
1117 | | - | |
1118 | | - | |
| 1134 | + | |
1119 | 1135 | | |
1120 | | - | |
1121 | | - | |
1122 | | - | |
1123 | | - | |
1124 | 1136 | | |
1125 | | - | |
1126 | | - | |
1127 | | - | |
1128 | | - | |
| 1137 | + | |
| 1138 | + | |
1129 | 1139 | | |
1130 | 1140 | | |
1131 | 1141 | | |
| |||
1184 | 1194 | | |
1185 | 1195 | | |
1186 | 1196 | | |
1187 | | - | |
| 1197 | + | |
1188 | 1198 | | |
1189 | 1199 | | |
1190 | 1200 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1376 | 1376 | | |
1377 | 1377 | | |
1378 | 1378 | | |
| 1379 | + | |
| 1380 | + | |
| 1381 | + | |
| 1382 | + | |
| 1383 | + | |
| 1384 | + | |
| 1385 | + | |
| 1386 | + | |
| 1387 | + | |
| 1388 | + | |
| 1389 | + | |
| 1390 | + | |
| 1391 | + | |
| 1392 | + | |
| 1393 | + | |
| 1394 | + | |
| 1395 | + | |
| 1396 | + | |
| 1397 | + | |
| 1398 | + | |
| 1399 | + | |
| 1400 | + | |
| 1401 | + | |
| 1402 | + | |
| 1403 | + | |
| 1404 | + | |
| 1405 | + | |
| 1406 | + | |
| 1407 | + | |
| 1408 | + | |
| 1409 | + | |
| 1410 | + | |
| 1411 | + | |
| 1412 | + | |
| 1413 | + | |
| 1414 | + | |
| 1415 | + | |
| 1416 | + | |
| 1417 | + | |
| 1418 | + | |
| 1419 | + | |
| 1420 | + | |
| 1421 | + | |
| 1422 | + | |
| 1423 | + | |
| 1424 | + | |
| 1425 | + | |
| 1426 | + | |
| 1427 | + | |
| 1428 | + | |
| 1429 | + | |
| 1430 | + | |
| 1431 | + | |
| 1432 | + | |
| 1433 | + | |
| 1434 | + | |
| 1435 | + | |
| 1436 | + | |
| 1437 | + | |
| 1438 | + | |
| 1439 | + | |
| 1440 | + | |
| 1441 | + | |
| 1442 | + | |
| 1443 | + | |
| 1444 | + | |
| 1445 | + | |
| 1446 | + | |
| 1447 | + | |
| 1448 | + | |
| 1449 | + | |
| 1450 | + | |
| 1451 | + | |
| 1452 | + | |
| 1453 | + | |
| 1454 | + | |
| 1455 | + | |
| 1456 | + | |
| 1457 | + | |
| 1458 | + | |
| 1459 | + | |
| 1460 | + | |
| 1461 | + | |
| 1462 | + | |
| 1463 | + | |
1379 | 1464 | | |
1380 | 1465 | | |
1381 | 1466 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | | - | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
21 | 26 | | |
22 | 27 | | |
23 | 28 | | |
| |||
0 commit comments