Skip to content

Commit 392a1f9

Browse files
authored
Merge pull request #1462 from FluxML/bc/rm-boundscheck
Handle `Expr(:boundscheck)`
2 parents 9df7226 + bc78d43 commit 392a1f9

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/compiler/reverse.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ function instrument(ir::IR)
132132
elseif isexpr(ex, :(=))
133133
@assert ex.args[1] isa GlobalRef
134134
pr[v] = xcall(Zygote, :global_set, QuoteNode(ex.args[1]), ex.args[2])
135+
elseif isexpr(ex, :boundscheck)
136+
# Expr(:boundscheck) now appears in common Julia code paths, so we need to handle it.
137+
# For correctness sake, fix to true like https://github.com/dfdx/Umlaut.jl/issues/34.
138+
pr[v] = true
135139
else
136140
ex = instrument_new!(pr, v, ex)
137141
ex = instrument_literals!(pr, v, ex)

test/compiler.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,15 @@ end
226226
# issue 897
227227
@test gradient(x -> sum(norm, collect(eachcol(x))), ones(3, 400))[1] fill(0.5773502691896258, 3, 400)
228228

229+
# Tests adapted from https://github.com/dfdx/Umlaut.jl/pull/35
230+
@eval _has_boundscheck(x) = ifelse($(Expr(:boundscheck)), 2x, x)
231+
232+
@testset "Meta Expr handling" begin
233+
y, (dx,) = withgradient(_has_boundscheck, 1)
234+
@test y == 2
235+
@test dx == 2
236+
end
237+
229238
# issue 1118 & 1380
230239
function f_1380(x)
231240
if rand(Bool)

0 commit comments

Comments
 (0)