Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimizer: eliminate isdefined check #44708

Merged
merged 1 commit into from
Mar 24, 2022
Merged

optimizer: eliminate isdefined check #44708

merged 1 commit into from
Mar 24, 2022

Commits on Mar 24, 2022

  1. optimizer: eliminate isdefined in sroa_mutables!

    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}
    ```
    aviatesk committed Mar 24, 2022
    Configuration menu
    Copy the full SHA
    10cf29d View commit details
    Browse the repository at this point in the history