Skip to content

Commit

Permalink
Broadcast shouldn't use inbounds when calling f (#19188)
Browse files Browse the repository at this point in the history
* Broadcast shouldn't use inbounds when calling f

Previously, `broadcast(getindex, A, I...)` would call `getindex` within an
at-inbounds context. This patch simply moves the function call so that it
will throw a BoundsError appropriately.

* Add inbounds elsewhere in broadcast

Try to restore some performance by allowing inbounds propagation into _broadcast_getindex.
  • Loading branch information
mbauman authored and stevengj committed Nov 4, 2016
1 parent 421f079 commit 89d9f16
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
9 changes: 5 additions & 4 deletions base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ map_newindexer(shape, ::Tuple{}) = (), ()
(keep, keeps...), (Idefault, Idefaults...)
end

@inline _broadcast_getindex(A, I) = _broadcast_getindex(containertype(A), A, I)
@inline _broadcast_getindex(::Type{Any}, A, I) = A
@inline _broadcast_getindex(::Any, A, I) = A[I]
Base.@propagate_inbounds _broadcast_getindex(A, I) = _broadcast_getindex(containertype(A), A, I)
Base.@propagate_inbounds _broadcast_getindex(::Type{Any}, A, I) = A
Base.@propagate_inbounds _broadcast_getindex(::Any, A, I) = A[I]

## Broadcasting core
# nargs encodes the number of As arguments (which matches the number
Expand All @@ -140,7 +140,8 @@ end
# extract array values
@nexprs $nargs i->(@inbounds val_i = _broadcast_getindex(A_i, I_i))
# call the function and store the result
@inbounds B[I] = @ncall $nargs f val
result = @ncall $nargs f val
@inbounds B[I] = result
end
end
end
Expand Down
7 changes: 7 additions & 0 deletions test/boundscheck_exec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,11 @@ if bc_opt != bc_off
@test_throws BoundsError k1(Int[])
end

# Ensure that broadcast doesn't use @inbounds when calling the function
if bc_opt != bc_off
let A = zeros(3,3)
@test_throws BoundsError broadcast(getindex, A, 1:3, 1:3)
end
end

end

0 comments on commit 89d9f16

Please sign in to comment.