diff --git a/src/compiler/reverse.jl b/src/compiler/reverse.jl index 0583b3da6..88e86c848 100644 --- a/src/compiler/reverse.jl +++ b/src/compiler/reverse.jl @@ -132,6 +132,10 @@ function instrument(ir::IR) elseif isexpr(ex, :(=)) @assert ex.args[1] isa GlobalRef pr[v] = xcall(Zygote, :global_set, QuoteNode(ex.args[1]), ex.args[2]) + elseif isexpr(ex, :boundscheck) + # Expr(:boundscheck) now appears in common Julia code paths, so we need to handle it. + # For correctness sake, fix to true like https://github.com/dfdx/Umlaut.jl/issues/34. + pr[v] = true else ex = instrument_new!(pr, v, ex) ex = instrument_literals!(pr, v, ex) diff --git a/test/compiler.jl b/test/compiler.jl index 4f8776c90..3d7497ec7 100644 --- a/test/compiler.jl +++ b/test/compiler.jl @@ -226,6 +226,15 @@ end # issue 897 @test gradient(x -> sum(norm, collect(eachcol(x))), ones(3, 400))[1] ≈ fill(0.5773502691896258, 3, 400) +# Tests adapted from https://github.com/dfdx/Umlaut.jl/pull/35 +@eval _has_boundscheck(x) = ifelse($(Expr(:boundscheck)), 2x, x) + +@testset "Meta Expr handling" begin + y, (dx,) = withgradient(_has_boundscheck, 1) + @test y == 2 + @test dx == 2 +end + # issue 1118 & 1380 function f_1380(x) if rand(Bool)