Commit e205e50
committed
optimizer: eliminate
This commit implements a simple optimization within `sroa_mutables!` to
eliminate `isdefined` call by checking load-forwardability of the field.
This optimization may be especially useful to eliminate extra allocation
of `Core.Box` involved with a capturing closure, e.g.:
```julia
julia> callit(f, args...) = f(args...);
julia> function isdefined_elim()
local arr::Vector{Any}
callit() do
arr = Any[]
end
return arr
end;
julia> code_typed(isdefined_elim)
```
```diff
diff --git a/_master.jl b/_pr.jl
index 3aa40ba20e5..11eccf65f32 100644
--- a/_master.jl
+++ b/_pr.jl
@@ -1,15 +1,8 @@
1-element Vector{Any}:
CodeInfo(
-1 ─ %1 = Core.Box::Type{Core.Box}
-│ %2 = %new(%1)::Core.Box
-│ %3 = $(Expr(:foreigncall, :(:jl_alloc_array_1d), Vector{Any}, svec(Any, Int64), 0, :(:ccall), Vector{Any}, 0, 0))::Vector{Any}
-│ Core.setfield!(%2, :contents, %3)::Vector{Any}
-│ %5 = Core.isdefined(%2, :contents)::Bool
-└── goto #3 if not %5
-2 ─ goto #4
-3 ─ $(Expr(:throw_undef_if_not, :arr, false))::Any
-4 ┄ %9 = Core.getfield(%2, :contents)::Any
-│ Core.typeassert(%9, Vector{Any})::Vector{Any}
-│ %11 = π (%9, Vector{Any})
-└── return %11
+1 ─ %1 = $(Expr(:foreigncall, :(:jl_alloc_array_1d), Vector{Any}, svec(Any, Int64), 0, :(:ccall), Vector{Any}, 0, 0))::Vector{Any}
+└── goto #3 if not true
+2 ─ goto #4
+3 ─ $(Expr(:throw_undef_if_not, :arr, false))::Any
+4 ┄ return %1
) => Vector{Any}
```isdefined in sroa_mutables!
1 parent d9f2074 commit e205e50
2 files changed
+83
-14
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
30 | | - | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
31 | 34 | | |
32 | 35 | | |
33 | 36 | | |
| |||
725 | 728 | | |
726 | 729 | | |
727 | 730 | | |
728 | | - | |
| 731 | + | |
729 | 732 | | |
730 | 733 | | |
731 | 734 | | |
| |||
741 | 744 | | |
742 | 745 | | |
743 | 746 | | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
744 | 754 | | |
745 | 755 | | |
746 | 756 | | |
| |||
795 | 805 | | |
796 | 806 | | |
797 | 807 | | |
798 | | - | |
799 | | - | |
800 | 808 | | |
801 | 809 | | |
802 | 810 | | |
803 | 811 | | |
804 | | - | |
| 812 | + | |
805 | 813 | | |
806 | 814 | | |
807 | 815 | | |
| |||
812 | 820 | | |
813 | 821 | | |
814 | 822 | | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
815 | 827 | | |
816 | 828 | | |
817 | 829 | | |
818 | | - | |
| 830 | + | |
| 831 | + | |
819 | 832 | | |
820 | 833 | | |
821 | 834 | | |
| |||
836 | 849 | | |
837 | 850 | | |
838 | 851 | | |
| 852 | + | |
| 853 | + | |
839 | 854 | | |
840 | 855 | | |
841 | 856 | | |
| |||
844 | 859 | | |
845 | 860 | | |
846 | 861 | | |
| 862 | + | |
| 863 | + | |
847 | 864 | | |
848 | 865 | | |
849 | 866 | | |
| |||
927 | 944 | | |
928 | 945 | | |
929 | 946 | | |
930 | | - | |
| 947 | + | |
931 | 948 | | |
932 | | - | |
| 949 | + | |
933 | 950 | | |
934 | 951 | | |
935 | 952 | | |
| |||
969 | 986 | | |
970 | 987 | | |
971 | 988 | | |
972 | | - | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
| 992 | + | |
| 993 | + | |
| 994 | + | |
| 995 | + | |
| 996 | + | |
| 997 | + | |
973 | 998 | | |
974 | 999 | | |
975 | 1000 | | |
| |||
991 | 1016 | | |
992 | 1017 | | |
993 | 1018 | | |
994 | | - | |
995 | | - | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
996 | 1025 | | |
997 | 1026 | | |
998 | 1027 | | |
| |||
1010 | 1039 | | |
1011 | 1040 | | |
1012 | 1041 | | |
1013 | | - | |
1014 | | - | |
1015 | | - | |
| 1042 | + | |
| 1043 | + | |
| 1044 | + | |
| 1045 | + | |
| 1046 | + | |
1016 | 1047 | | |
1017 | 1048 | | |
1018 | 1049 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
425 | 425 | | |
426 | 426 | | |
427 | 427 | | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
428 | 466 | | |
429 | 467 | | |
430 | 468 | | |
| |||
0 commit comments