EnzymeAD / Enzyme.jl

Julia bindings for the Enzyme automatic differentiator
https://enzyme.mit.edu
MIT License
455 stars 64 forks source link

`make_zero` fails on a mutable type with `const` fields #1925

Closed mofeing closed 1 month ago

mofeing commented 1 month ago
mutable struct A
    x::Float64
    const y::Float64
end

make_zero(A, IdDict(), A(0.0, 0.0), Val(false))

fails with the following stacktrace

setfield!: const field .y of type B cannot be changed

Stacktrace:
 [1] make_zero(::Type{B}, seen::IdDict{Any, Any}, prev::B, ::Val{false})
   @ Enzyme.Compiler ~/.julia/packages/Enzyme/HXYPU/src/compiler.jl:1948

make_zero is not checking whether the field is const (which can be done using isconst(A, :y) or isconst(A, 2))

wsmoses commented 1 month ago

This fix it? https://github.com/EnzymeAD/Enzyme.jl/pull/1926