Skip to content

Commit a814f87

Browse files
committed
Merge branch 'kb-impl/Enzyme-snag.native-reverse-p-1a4ac06d'
KB-Committer-Id: Enzyme:snag.native-reverse-p-1a4ac06d
2 parents 0147c6f + a914bd3 commit a814f87

3 files changed

Lines changed: 97 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@
4848
dependent loads (`idx.data`, `idx[j]`, `idx[j].data`), which the same loop in
4949
a function of its own gets as arguments. This was the remaining 6 to 7% of
5050
Reverse of a ragged nest over that outlined spelling on Julia 1.12 and 1.13.
51+
- Fix an LLVM verifier abort ("Call parameter type does not match function
52+
signature!") on Julia 1.10 when a function differentiated as a callee, or in
53+
split mode, takes `view` of a `SubArray` of a custom array type it holds on
54+
the stack (dogfood libEnzyme build `979b679a`). `view` calls `objectid` of
55+
the parent through Base's aliasing check, and Julia passes that stack slot to
56+
the runtime as an `i8` pointer. When Enzyme moved the slot to the heap, the
57+
call kept the slot's own pointer type; it now gets a bitcast to `i8`.
5158
- Do not recompute a call on a path the forward pass skipped (dogfood
5259
libEnzyme build `045d93e1`). When the reverse pass recomputes a value that
5360
three paths merge, one of which skips a call while the other two branch on

‎deps/dogfood_libenzyme.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
# for the consumer's LLVM version, and refuses to resolve when that build is
88
# missing. Every jll pin move rebases the branch, and every new repair is one
99
# more cherry-pick; either way `commit` changes here in the same landing.
10-
commit = "3a825a54e3ed955cb1ad31639fbcfa7a26b05453"
10+
commit = "979b679afd12d0f7d829f3925ba2d3c453b9d385"
1111
branch = "dogfood-jll"
1212
repository = "https://github.com/nsiccha/Enzyme"

‎test/heap_slot_object_id.jl‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using Enzyme, Test
2+
3+
# A function Enzyme differentiates as a callee, or in split mode, moves its
4+
# stack slots to the heap. On Julia 1.10 (typed pointers) Julia passes such a
5+
# slot to `jl_object_id_` through an addrspacecast that also changes its pointee
6+
# type to `i8`. `objectid` of an immutable value that holds references is how
7+
# `view` reaches it: `unalias`, `mightalias`, then Base's default `dataids`. The
8+
# moved slot kept its own pointer type there, and the derivative failed LLVM's
9+
# verifier ("Call parameter type does not match function signature!").
10+
11+
struct SlotPoint{B, A} <: AbstractVector{Float64}
12+
base::B
13+
active::A
14+
end
15+
Base.size(p::SlotPoint) = size(p.base)
16+
Base.IndexStyle(::Type{<:SlotPoint}) = IndexLinear()
17+
@inline function Base.getindex(p::SlotPoint, i::Int)
18+
@boundscheck checkbounds(p.base, i)
19+
return i <= length(p.active) ? @inbounds(p.active[i]) : @inbounds(p.base[i])
20+
end
21+
22+
# Holds its arguments in one tuple, a stack slot, as ReactiveKernels' statement
23+
# wrapper does.
24+
@noinline @generated slot_call(f::F, args::Vararg{Any, N}) where {F, N} =
25+
:(f($((:(args[$i]) for i in 1:N)...)))
26+
27+
function slot_sumsq(v)
28+
t = 0.0
29+
for x in v
30+
t += x * x
31+
end
32+
return t
33+
end
34+
35+
slot_window(active, base) = view(SlotPoint(base, active), 1:length(base))
36+
37+
slot_view_loss(active, base, rows) =
38+
slot_sumsq(slot_call(view, slot_window(active, base), rows))
39+
40+
function slot_copy_loss(active, base, rows)
41+
out = Vector{Float64}(undef, length(rows))
42+
slot_call(copyto!, out, view(slot_window(active, base), rows))
43+
return slot_sumsq(out)
44+
end
45+
46+
# The slot in the function that is differentiated, in split mode.
47+
function slot_view_direct(active, base, rows)
48+
args = (slot_window(active, base), rows)
49+
return slot_sumsq(view(args...))
50+
end
51+
52+
function slot_expected(active, base, rows)
53+
g = zero(active)
54+
for r in rows
55+
r <= length(active) && (g[r] += 2 * active[r])
56+
end
57+
return g
58+
end
59+
60+
@testset "A moved stack slot passed to jl_object_id_" begin
61+
base = [3.0, 4.0, 2.0, 1.0, -0.5]
62+
for rows in ([1, 2, 4], [2, 2, 5, 1], Int[])
63+
active = [0.5, -1.0]
64+
expected = slot_expected(active, base, rows)
65+
primal = slot_sumsq([SlotPoint(base, active)[r] for r in rows])
66+
for f in (slot_view_loss, slot_copy_loss)
67+
dactive = zero(active)
68+
b = copy(base)
69+
res = autodiff(ReverseWithPrimal, f, Active, Duplicated(active, dactive), Const(b), Const(rows))
70+
@test res[2] ≈ primal
71+
@test dactive ≈ expected
72+
@test b == base
73+
dactive = zero(active)
74+
autodiff(set_runtime_activity(Reverse), f, Active, Duplicated(active, dactive), Const(b), Const(rows))
75+
@test dactive ≈ expected
76+
end
77+
78+
dactive = zero(active)
79+
args = (Duplicated(active, dactive), Const(copy(base)), Const(rows))
80+
fwd, rev = autodiff_thunk(
81+
ReverseSplitWithPrimal, Const{typeof(slot_view_direct)}, Active,
82+
typeof.(args)...,
83+
)
84+
tape, value, _ = fwd(Const(slot_view_direct), args...)
85+
@test value ≈ primal
86+
rev(Const(slot_view_direct), args..., 1.0, tape)
87+
@test dactive ≈ expected
88+
end
89+
end

0 commit comments

Comments
 (0)