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

assignment out of an inner function leads to unnecessary boxing #47539

Closed
aviatesk opened this issue Nov 11, 2022 · 8 comments
Closed

assignment out of an inner function leads to unnecessary boxing #47539

aviatesk opened this issue Nov 11, 2022 · 8 comments
Labels
compiler:lowering Syntax lowering (compiler front end, 2nd stage)

Comments

@aviatesk
Copy link
Sponsor Member

This may have been filed already, but let me file it here anyway as I couldn't find existing issue:

julia> function f(c, x)
           local var = nothing
           if c
               var = Any[]
           end
           function usevar(x)
               if var !== nothing
                   push!(var, x)
                   return var
               else
                   return nothing
               end
           end
           return usevar(x)
       end
f (generic function with 1 method)

julia> code_lowered(f, (Bool,Int))
1-element Vector{Core.CodeInfo}:
 CodeInfo(
1 ─      var = Core.Box()
│        Core.NewvarNode(:(usevar))
│   %3 = Main.nothing
│        Core.setfield!(var, :contents, %3)
└──      goto #3 if not c
2%6 = Base.getindex(Main.Any)
└──      Core.setfield!(var, :contents, %6)
3 ┄      usevar = %new(Main.:(var"#usevar#3"), var)
│   %9 = (usevar)(x)
└──      return %9
)

I'm willing to look at improving the lowering implementation if anyone kindly tell me which part of our codebase should be modified.

@aviatesk aviatesk added the compiler:lowering Syntax lowering (compiler front end, 2nd stage) label Nov 11, 2022
@jakobnissen
Copy link
Contributor

Isn't this an instance of the infamous "slow closure bug"? #15276

@aviatesk
Copy link
Sponsor Member Author

aviatesk commented Nov 11, 2022

I'm kind of interested in what actual changes are needed to fix those examples.
Not all outer assignment causes boxing, e.g.:

julia> function f(c, x)
           # local var = nothing
           # if c
           #     var = Any[]
           # end
           var = Any[]
           function usevar(x)
               if var !== nothing
                   push!(var, x)
                   return var
               else
                   return nothing
               end
           end
           return usevar(x)
       end
f (generic function with 1 method)

julia> code_lowered(f, (Bool,Int))
1-element Vector{Core.CodeInfo}:
 CodeInfo(
1 ─      var = Base.getindex(Main.Any)
│   %2 = Main.:(var"#usevar#3")
│   %3 = Core.typeof(var)
│   %4 = Core.apply_type(%2, %3)
│        usevar = %new(%4, var)
│   %6 = (usevar)(x)
└──      return %6
)

so it seems that some general fix is needed here, but rather we want to fix these example one by one?

@jakobnissen
Copy link
Contributor

There is a section on this in the Julia docs performance tips. I'm not deeply into the codebase, but as far as I have heard other people talk about it, the problem is that at the lowering stage, there is no type info, so Julia does not know if var can change type in the if statement. Therefore, it emits a box.

The fix is supposedly to rewrite the lowering step in Julia - but I'm sure the other compiler devs have thought quite a lot about this bug

@aviatesk
Copy link
Sponsor Member Author

the problem is that at the lowering stage, there is no type info, so Julia does not know if var can change type in the if statement. Therefore, it emits a box.

Well, as far as all the assignments happens out side of the scope of inner function, we can safely unbox it? Maybe I should better read through the code base though. This seems to be very much an implementation detail.

@KristofferC
Copy link
Sponsor Member

There is a section on this in the Julia docs performance tips. I'm not deeply into the codebase, but as far as I have heard other people talk about it, the problem is that at the lowering stage, there is no type info, so Julia does not know if var can change type in the if statement. Therefore, it emits a box.

Just pointing it out explicitly, there is no re-assignment to the variable inside the closure in this case.

@CameronBieganek
Copy link
Contributor

The fix is supposedly to rewrite the lowering step in Julia

IIUC, rewriting parsing and lowering in Julia (i.e. JuliaSyntax) is just a (preferred) prerequisite for solving this problem. Doing the same lowering in Julia that FemtoLisp does will not of course solve the problem.

@maleadt
Copy link
Member

maleadt commented Nov 14, 2022

parsing and lowering in Julia (i.e. JuliaSyntax)

FWIW, JuliaSyntax is only parsing, not lowering. There are no plans to port lowering to Julia.

@vtjnash
Copy link
Sponsor Member

vtjnash commented Nov 14, 2022

Duplicate of #15276

@vtjnash vtjnash marked this as a duplicate of #15276 Nov 14, 2022
@vtjnash vtjnash closed this as completed Nov 14, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler:lowering Syntax lowering (compiler front end, 2nd stage)
Projects
None yet
Development

No branches or pull requests

6 participants