Skip to content

Commit

Permalink
Compile Time Ref Prep (#438)
Browse files Browse the repository at this point in the history
* Move ref spec to compile time

* Bump patch

* Add comment
  • Loading branch information
willtebbutt authored Jan 7, 2025
1 parent f9618e5 commit 06834aa
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Mooncake"
uuid = "da2b9cff-9c12-43a0-ae48-6db2b0edb7d6"
authors = ["Will Tebbutt, Hong Ge, and contributors"]
version = "0.4.72"
version = "0.4.73"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
Expand Down
31 changes: 9 additions & 22 deletions src/interpreter/s2s_reverse_mode_ad.jl
Original file line number Diff line number Diff line change
Expand Up @@ -253,38 +253,25 @@ get_rev_data_id(::ADInfo, ::Any) = nothing
"""
reverse_data_ref_stmts(info::ADInfo)
Create the statements which initialise the reverse-data `Ref`s.
Create the `:new` statements which initialise the reverse-data `Ref`s. Interpolates the
initial rdata directly into the statement, which is safe because it is always a bits type.
"""
function reverse_data_ref_stmts(info::ADInfo)
function make_ref_stmt(id, P)
ref_type = Base.RefValue{P <: Type ? NoRData : zero_like_rdata_type(P)}
init_ref_val = P <: Type ? NoRData() : Mooncake.zero_like_rdata_from_type(P)
return (id, new_inst(Expr(:new, ref_type, QuoteNode(init_ref_val))))
end
return vcat(
map(collect(info.arg_rdata_ref_ids)) do (k, id)
(id, new_inst(Expr(:call, __make_ref, CC.widenconst(info.arg_types[k]))))
return make_ref_stmt(id, CC.widenconst(info.arg_types[k]))
end,
map(collect(info.ssa_rdata_ref_ids)) do (k, id)
(id, new_inst(Expr(:call, __make_ref, CC.widenconst(info.ssa_insts[k].type))))
return make_ref_stmt(id, CC.widenconst(info.ssa_insts[k].type))
end,
)
end

"""
__make_ref(p::Type{P}) where {P}
Helper for [`reverse_data_ref_stmts`](@ref). Constructs a `Ref` whose element type is the
[`zero_like_rdata_type`](@ref) for `P`, and whose element is the zero-like rdata for `P`.
"""
@inline function __make_ref(p::Type{P}) where {P}
_P = @isdefined(P) ? P : _typeof(p)
return Ref{zero_like_rdata_type(_P)}(Mooncake.zero_like_rdata_from_type(_P))
end

# This specialised method is necessary to ensure that `__make_ref` works properly for
# `DataType`s with unbound type parameters. See `TestResources.typevar_tester` for an
# example. The above method requires that `P` be a type in which all parameters are fully-
# bound. Strange errors occur if this property does not hold.
@inline __make_ref(::Type{<:Type}) = Ref{NoRData}(NoRData())

@inline __make_ref(::Type{Union{}}) = nothing

# Returns the number of arguments that the primal function has.
num_args(info::ADInfo) = length(info.arg_types)

Expand Down

2 comments on commit 06834aa

@willtebbutt
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/122526

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.4.73 -m "<description of version>" 06834aa0e061ed46b790b7281019fae133950f28
git push origin v0.4.73

Please sign in to comment.