Do NOT use code_llvm
as your first tool to identify performance issue. I really don’t know who started the tradition of using overkilling tools to confuse themselves (certainly not you) but this has cause too many people to look at low level representation and trying to guess what it means and confuse more people in that process (and I expect you to be one of the victims too).
In particular, do not use code_llvm
if you have not used LLVM IR before and do not suggest it to anyone unless you can understand it yourself or at least identify the issue in a particular IR. It’s much better than code_native
(which is almost never useful for performance issues) but we still have much better tools in most of the case.
In almost all cases, the right starting point is code_warntype
. In this case,
julia> @code_warntype compute(1)
Variables:
#self#::#compute
k::Int64
i::Int64
#temp#::Int64
s::Int64
Body:
begin
s::Int64 = 2 # line 5:
SSAValue(3) = (Base.select_value)((Base.sle_int)(1, 1000)::Bool, 1000, (Base.sub_int)(1, 1)::Int64)::Int64
#temp#::Int64 = 1
5:
unless (Base.not_int)((#temp#::Int64 === (Base.add_int)(SSAValue(3), 1)::Int64)::Bool)::Bool goto 14
SSAValue(4) = #temp#::Int64
SSAValue(5) = (Base.add_int)(#temp#::Int64, 1)::Int64
#temp#::Int64 = SSAValue(5) # line 6:
s::Int64 = (Base.add_int)(s::Int64, k::Int64)::Int64
12:
goto 5
14: # line 8:
SSAValue(2) = s::Int64
(Core.setfield!)(Core.Box(0), :contents, SSAValue(2))::Int64 # line 9:
return
end::Void
julia> @code_warntype compute2(1)
Variables:
#self#::#local_compute#1
k::Int64
i::Int64
#temp#::Int64
s::Int64
Body:
begin
s::Int64 = 2 # line 13:
SSAValue(3) = (Base.select_value)((Base.sle_int)(1, 1000)::Bool, 1000, (Base.sub_int)(1, 1)::Int64)::Int64
#temp#::Int64 = 1
5:
unless (Base.not_int)((#temp#::Int64 === (Base.add_int)(SSAValue(3), 1)::Int64)::Bool)::Bool goto 14
SSAValue(4) = #temp#::Int64
SSAValue(5) = (Base.add_int)(#temp#::Int64, 1)::Int64
#temp#::Int64 = SSAValue(5) # line 14:
s::Int64 = (Base.add_int)(s::Int64, k::Int64)::Int64
12:
goto 5
14: # line 16:
SSAValue(2) = s::Int64
(Core.setfield!)((Core.getfield)(#self#::#local_compute#1, :result)::Any, :contents, SSAValue(2))::Int64 # line 17:
return
end::Void
I’ll not expect all users to identify this is related to closure lowering but it should be very clear what the difference is in the IR (the last expression) and the slower version also warns you about a ::Any
type.