Skip to content

Commit dbe0760

Browse files
authored
Merge pull request #235 from JuliaDiff/ox/isdefined
handle Expr(:isdefined,) in forwards mode
2 parents cbcc0f3 + 02a5bbd commit dbe0760

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/codegen/forward.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ function fwd_transform!(ci, mi, nargs, N)
1010
ssa_mapping = Int[]
1111
loc_mapping = Int[]
1212

13-
function emit!(@nospecialize stmt)
14-
(isexpr(stmt, :call) || isexpr(stmt, :(=)) || isexpr(stmt, :new)) || return stmt
13+
emit!(@nospecialize stmt) = stmt
14+
function emit!(stmt::Expr)
15+
stmt.head (:call, :(=), :new, :isdefined) || return stmt
1516
push!(new_code, stmt)
1617
push!(new_codelocs, isempty(new_codelocs) ? 0 : new_codelocs[end])
1718
return SSAValue(length(new_code))
@@ -58,7 +59,8 @@ function fwd_transform!(ci, mi, nargs, N)
5859
# Can't trust that meta annotations are still valid in the AD'd
5960
# version.
6061
return nothing
61-
62+
elseif isexpr(stmt, :isdefined)
63+
return Expr(:call, ZeroBundle{N}, emit!(stmt))
6264
# Always disable `@inbounds`, as we don't actually know if the AD'd
6365
# code is truly `@inbounds` or not.
6466
elseif isexpr(stmt, :boundscheck)

test/forward.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,19 @@ end
147147
end
148148
end
149149

150+
@testset "isdefined" begin
151+
function foo_isdefined(x)
152+
if (@noinline rand()) < 2
153+
a=1 # always happens
154+
end
155+
156+
2*@isdefined(a)*x + @isdefined(_thing_that_is_not_defined)
157+
end
158+
let var"'" = Diffractor.PrimeDerivativeFwd
159+
@test foo_isdefined'(100.0) == 2.0
160+
end
161+
end
162+
150163

151164
@testset "taylor_compatible" begin
152165
taylor_compatible = Diffractor.taylor_compatible

0 commit comments

Comments
 (0)