-
Notifications
You must be signed in to change notification settings - Fork 2
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
Fix inlining of generated functions #13
Conversation
@@ -1,39 +1,55 @@ | |||
@inline @generated _gen_unrolled_any(::Val{N}, f, itr) where {N} = | |||
Expr(:||, (:(f(generic_getindex(itr, $n))) for n in 1:N)...) | |||
@generated _gen_unrolled_any(::Val{N}, f, itr) where {N} = Expr( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks fine to me.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #13 +/- ##
==========================================
+ Coverage 56.57% 57.48% +0.91%
==========================================
Files 8 8
Lines 251 247 -4
==========================================
Hits 142 142
+ Misses 109 105 -4 ☔ View full report in Codecov by Sentry. |
8443ddf
to
57ae250
Compare
57ae250
to
7a8e9f2
Compare
82a9b04
to
09c72fe
Compare
09c72fe
to
13523ac
Compare
I have tried this out in CliMA/ClimaAtmos.jl#3313, and unfortunately this does not actually solve the strange compilation issue mentioned above. I originally defined the first method of the @generated val_unrolled_reduce(op, ::Val{N}, init) where {N} = Expr(
:block,
Expr(:meta, :inline),
foldl((prev_op_expr, item_expr) -> :(op($prev_op_expr, $item_expr)), (:init, 1:N...)),
) This caused compilation to hang for the non-orographic gravity wave parameterization tests in Buildkite Run 20622. I then tried rewriting this using @generated function val_unrolled_reduce(op, ::Val{N}, init) where {N}
return Expr(
:block,
Expr(:meta, :inline),
foldl((prev_op_expr, item_expr) -> :(op($prev_op_expr, $item_expr)), (:init, 1:N...)),
)
end This also caused compilation to hang for the non-orographic gravity wave parameterization tests in Buildkite Run 20623. Finally, I returned this function to the implementation it had before this PR (but without the @generated function val_unrolled_reduce(op, ::Val{N}, init) where {N}
return foldl((:init, 1:N...)) do prev_op_expr, item_expr
:(op($prev_op_expr, $item_expr))
end
end This version took less than a minute to compile in Buildkite Run 20624. Although this PR does not resolve the compilation mystery, this is still a change that we should make in order to correctly inline generated functions. I'll go ahead and merge this in. |
This PR fixes the inlining of generated functions, which will hopefully prevent compilation from hanging in the non-orographic gravity wave parametrization test in ClimaAtmos.
Normally, inlining works by inserting an
inline
annotation at the beginning of a function definition:When the function is generated, though, the
@inline
macro inserts two such annotations (one at the start of the generated function and one in the expression that generates it):This type of expression is probably not supported by the compiler, and I think it is responsible for the strange behavior we have been seeing in ClimaAtmos. Specifically, we have observed that replacing the definition
with an equivalent version that uses assignment syntax
causes compilation to hang. Having the double
inline
annotation here is probably forcing the compiler to enter some unhandled infinite loop orwait
condition for this complex test (though I don't think we're seeing a compiler bug here because such a use case probably isn't supported).In order to correctly inline generated functions, we need to only insert the
inline
annotation at the start of the generated function expression. For example,