Skip to content

Improving broadcasting performance by working around recursion limits of inlining #41139

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

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 43 additions & 6 deletions base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,47 @@ cat_nested(t::Broadcasted, rest...) = (cat_nested(t.args...)..., cat_nested(rest
cat_nested(t::Any, rest...) = (t, cat_nested(rest...)...)
cat_nested() = ()

"""
RecursiveInliningEnforcerA

A helper type for the `make_makeargs` function that is used to break
method recursion that sometimes prevents inlining which results in increased
compilation and execution times. See issue
https://github.com/JuliaLang/julia/issues/41090 for details.
"""
struct RecursiveInliningEnforcerA{T}
makeargs::T
end

"""
RecursiveInliningEnforcerB

A helper type for the `make_makeargs` function. See `RecursiveInliningEnforcerA`
for details.
"""
struct RecursiveInliningEnforcerB{TMT,TMH,TT,TH,TF}
makeargs_tail::TMT
makeargs_head::TMH
headargs::TH
tailargs::TT
f::TF
end

for UB in [Any, RecursiveInliningEnforcerA]
@eval @inline function (bb::RecursiveInliningEnforcerB{TMT,TMH,TT,TH,TF})(args::Vararg{Any,N}) where {N,TMT,TMH<:$UB,TT,TH,TF}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this need two methods? Doesn't the Any bound cover everything?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC the compiler currently has problems with inlining recursive method calls, so by having two different methods doing the same thing there is less recursion to handle. There is no other reason to having two methods there. I don't know how exactly this works so I may be wrong though -- but it works.

args1 = bb.makeargs_head(args...)
a = bb.headargs(args1...)
b = bb.makeargs_tail(bb.tailargs(args1...)...)
return (bb.f(a...), b...)
end
end

for UB in [Any, RecursiveInliningEnforcerB]
@eval @inline function (a::RecursiveInliningEnforcerA{TTA})(head::TH, tail::Vararg{Any,N}) where {TTA<:$UB,TH,N}
return (head, a.makeargs(tail...)...)
end
end

"""
make_makeargs(makeargs_tail::Function, t::Tuple) -> Function

Expand All @@ -358,7 +399,7 @@ by `t`).
@inline make_makeargs(makeargs_tail, t::Tuple{}) = makeargs_tail
@inline function make_makeargs(makeargs_tail, t::Tuple)
makeargs = make_makeargs(makeargs_tail, tail(t))
(head, tail...)->(head, makeargs(tail...)...)
return RecursiveInliningEnforcerA(makeargs)
end
function make_makeargs(makeargs_tail, t::Tuple{<:Broadcasted, Vararg{Any}})
bc = t[1]
Expand All @@ -377,11 +418,7 @@ function make_makeargs(makeargs_tail, t::Tuple{<:Broadcasted, Vararg{Any}})
# args is flattened (i.e. our children have not been evaluated
# yet).
headargs, tailargs = make_headargs(bc.args), make_tailargs(bc.args)
return @inline function(args::Vararg{Any,N}) where N
args1 = makeargs_head(args...)
a, b = headargs(args1...), makeargs_tail(tailargs(args1...)...)
(f(a...), b...)
end
return RecursiveInliningEnforcerB(makeargs_tail, makeargs_head, headargs, tailargs, f)
end
end

Expand Down