Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Expr(:boundscheck) #1462

Merged
merged 2 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/compiler/reverse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions test/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading