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

Support struct types in create_result #6

Merged
merged 16 commits into from
May 28, 2024
16 changes: 15 additions & 1 deletion src/Reactant.jl
Original file line number Diff line number Diff line change
Expand Up @@ -720,10 +720,24 @@ function generate_jlfunc(concrete_result, client, mod, Nargs, linear_args, linea
end)
return
end
if T <: Int || T <: AbstractFloat || T <: AbstractString || T <: Nothing
mofeing marked this conversation as resolved.
Show resolved Hide resolved
if T ∈ [Int, AbstractFloat, AbstractString, Nothing, Symbol]
push!(concrete_result_maker, :($resname = $tocopy))
return
end
if isstructtype(T)
elems = Symbol[]
nf = fieldcount(T)
for i in 1:nf
sym = Symbol(resname, :_, i)
create_result(getfield(tocopy, i), sym, (path..., i))
push!(elems, sym)
end
push!(concrete_result_maker, quote
Copy link
Member

Choose a reason for hiding this comment

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

I think this depends on whether the structure is mutable (Enzyme.make_zero should have some relevant code to look at).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added a test on a mutable struct type and seems to work... although it might be too naive?

flds = Any[$(elems...)]
$resname = ccall(:jl_new_structv, Any, (Any, Ptr{Cvoid}, UInt32), $T, flds, $nf)
end)
return
end

error("canot copy $T")
end
Expand Down