@@ -1298,7 +1298,7 @@ end
12981298 xi = getfield (prev, i)
12991299 T = Core. Typeof (xi)
13001300 xi = EnzymeCore. make_zero (T, seen, xi, Val (copy_if_inactive))
1301- ccall ( :jl_set_nth_field , Cvoid, (Any, Csize_t, Any), y, i- 1 , xi)
1301+ setfield! ( y, i, xi)
13021302 end
13031303 end
13041304 return y
@@ -1324,6 +1324,204 @@ end
13241324 return y
13251325end
13261326
1327+ function make_zero_immutable! (prev:: T , seen:: S ):: T where {T <: AbstractFloat , S}
1328+ zero (T)
1329+ end
1330+
1331+ function make_zero_immutable! (prev:: Complex{T} , seen:: S ):: Complex{T} where {T <: AbstractFloat , S}
1332+ zero (T)
1333+ end
1334+
1335+ function make_zero_immutable! (prev:: T , seen:: S ):: T where {T <: Tuple , S}
1336+ ntuple (Val (length (T. parameters))) do i
1337+ Base. @_inline_meta
1338+ make_zero_immutable! (prev[i], seen)
1339+ end
1340+ end
1341+
1342+ function make_zero_immutable! (prev:: NamedTuple{a, b} , seen:: S ):: NamedTuple{a, b} where {a,b, S}
1343+ NamedTuple {a, b} (
1344+ ntuple (Val (length (T. parameters))) do i
1345+ Base. @_inline_meta
1346+ make_zero_immutable! (prev[a[i]], seen)
1347+ end
1348+ )
1349+ end
1350+
1351+
1352+ function make_zero_immutable! (prev:: T , seen:: S ):: T where {T, S}
1353+ if guaranteed_const_nongen (T, nothing )
1354+ return prev
1355+ end
1356+ @assert ! ismutable (T)
1357+
1358+ @assert ! Base. isabstracttype (RT)
1359+ @assert Base. isconcretetype (RT)
1360+ nf = fieldcount (RT)
1361+
1362+ flds = Vector {Any} (undef, nf)
1363+ for i in 1 : nf
1364+ if isdefined (prev, i)
1365+ xi = getfield (prev, i)
1366+ ST = Core. Typeof (xi)
1367+ flds[i] = if active_reg_inner (ST, (), nothing , #= justActive=# Val (true )) == ActiveState
1368+ make_zero_immutable! (xi, seen)
1369+ else
1370+ EnzymeCore. make_zero! (xi, seen)
1371+ xi
1372+ end
1373+ else
1374+ nf = i - 1 # rest of tail must be undefined values
1375+ break
1376+ end
1377+ end
1378+ ccall (:jl_new_structv , Any, (Any, Ptr{Any}, UInt32), RT, flds, nf):: T
1379+ end
1380+
1381+ @inline function EnzymeCore. make_zero! (prev:: Base.RefValue{T} , seen:: ST ):: Nothing where {T <: AbstractFloat , ST}
1382+ T[] = zero (T)
1383+ nothing
1384+ end
1385+
1386+ @inline function EnzymeCore. make_zero! (prev:: Base.RefValue{Complex{T}} , seen:: ST ):: Nothing where {T <: AbstractFloat , ST}
1387+ T[] = zero (Complex{T})
1388+ nothing
1389+ end
1390+
1391+ @inline function EnzymeCore. make_zero! (prev:: Array{T, N} , seen:: ST ):: Nothing where {T <: AbstractFloat , N, ST}
1392+ fill! (prev, zero (T))
1393+ nothing
1394+ end
1395+
1396+ @inline function EnzymeCore. make_zero! (prev:: Array{Complex{T}, N} , seen:: ST ):: Nothing where {T <: AbstractFloat , N, ST}
1397+ fill! (prev, zero (Complex{T}))
1398+ nothing
1399+ end
1400+
1401+ @inline function EnzymeCore. make_zero! (prev:: Base.RefValue{T} ):: Nothing where {T <: AbstractFloat }
1402+ EnzymeCore. make_zero! (prev, nothing )
1403+ nothing
1404+ end
1405+
1406+ @inline function EnzymeCore. make_zero! (prev:: Base.RefValue{Complex{T}} ):: Nothing where {T <: AbstractFloat }
1407+ EnzymeCore. make_zero! (prev, nothing )
1408+ nothing
1409+ end
1410+
1411+ @inline function EnzymeCore. make_zero! (prev:: Array{T, N} ):: Nothing where {T <: AbstractFloat , N}
1412+ EnzymeCore. make_zero! (prev, nothing )
1413+ nothing
1414+ end
1415+
1416+ @inline function EnzymeCore. make_zero! (prev:: Array{Complex{T}, N} ):: Nothing where {T <: AbstractFloat , N}
1417+ EnzymeCore. make_zero! (prev, nothing )
1418+ nothing
1419+ end
1420+
1421+ @inline function EnzymeCore. make_zero! (prev:: Array{T, N} , seen:: ST ):: Nothing where {T, N, ST}
1422+ if guaranteed_const_nongen (T, nothing )
1423+ return
1424+ end
1425+ if in (seen, prev)
1426+ return
1427+ end
1428+ push! (seen, prev)
1429+
1430+ for I in eachindex (prev)
1431+ if isassigned (prev, I)
1432+ pv = prev[I]
1433+ SBT = Core. Typeof (pv)
1434+ if active_reg_inner (SBT, (), nothing , #= justActive=# Val (true )) == ActiveState
1435+ @inbounds prev[I] = make_zero_immutable! (pv, seen)
1436+ nothing
1437+ else
1438+ EnzymeCore. make_zero! (pv, seen)
1439+ nothing
1440+ end
1441+ end
1442+ end
1443+ nothing
1444+ end
1445+
1446+ @inline function EnzymeCore. make_zero! (prev:: Base.RefValue{T} , seen:: ST ):: Nothing where {T, ST}
1447+ if guaranteed_const_nongen (T, nothing )
1448+ return
1449+ end
1450+ if in (seen, prev)
1451+ return
1452+ end
1453+ push! (seen, prev)
1454+
1455+ pv = prev[]
1456+ SBT = Core. Typeof (pv)
1457+ if active_reg_inner (SBT, (), nothing , #= justActive=# Val (true )) == ActiveState
1458+ prev[] = make_zero_immutable! (pv, seen)
1459+ nothing
1460+ else
1461+ EnzymeCore. make_zero! (pv, seen)
1462+ nothing
1463+ end
1464+ nothing
1465+ end
1466+
1467+ @inline function EnzymeCore. make_zero! (prev:: Core.Box , seen:: ST ):: Nothing where {ST}
1468+ pv = prev. contents
1469+ T = Core. Typeof (pv)
1470+ if guaranteed_const_nongen (T, nothing )
1471+ return
1472+ end
1473+ if in (seen, prev)
1474+ return
1475+ end
1476+ push! (seen, prev)
1477+ SBT = Core. Typeof (pv)
1478+ if active_reg_inner (SBT, (), nothing , #= justActive=# Val (true )) == ActiveState
1479+ prev. contents = EnzymeCore. make_zero_immutable! (pv, seen)
1480+ nothing
1481+ else
1482+ EnzymeCore. make_zero! (pv, seen)
1483+ nothing
1484+ end
1485+ nothing
1486+ end
1487+
1488+ @inline function EnzymeCore. make_zero! (prev:: T , seen:: S = Base. IdSet {Any} ()):: Nothing where {T, S}
1489+ if guaranteed_const_nongen (T, nothing )
1490+ return
1491+ end
1492+ if in (seen, prev)
1493+ return
1494+ end
1495+ @assert ! Base. isabstracttype (T)
1496+ @assert Base. isconcretetype (T)
1497+ nf = fieldcount (T)
1498+
1499+
1500+ if nf == 0
1501+ return
1502+ end
1503+
1504+ push! (seen, prev)
1505+
1506+ for i in 1 : nf
1507+ if isdefined (prev, i)
1508+ xi = getfield (prev, i)
1509+ SBT = Core. Typeof (xi)
1510+ if guaranteed_const_nongen (SBT, nothing )
1511+ continue
1512+ end
1513+ if active_reg_inner (SBT, (), nothing , #= justActive=# Val (true )) == ActiveState
1514+ setfield! (prev, i, make_zero_immutable! (xi, seen))
1515+ nothing
1516+ else
1517+ EnzymeCore. make_zero! (xi, seen)
1518+ nothing
1519+ end
1520+ end
1521+ end
1522+ return
1523+ end
1524+
13271525struct EnzymeRuntimeException <: Base.Exception
13281526 msg:: Cstring
13291527end
@@ -5536,7 +5734,7 @@ end
55365734 @assert ismutable (x)
55375735 yi = getfield (y, i)
55385736 nexti = recursive_add (xi, yi, f, mutable_register)
5539- ccall ( :jl_set_nth_field , Cvoid, (Any, Csize_t, Any), x, i- 1 , nexti)
5737+ setfield! ( x, i, nexti)
55405738 end
55415739 end
55425740 end
0 commit comments