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

propagate inbounds in a few more places for memoryref #56151

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions base/Base_compiler.jl
Original file line number Diff line number Diff line change
@@ -40,6 +40,7 @@ macro inline() Expr(:meta, :inline) end
macro noinline() Expr(:meta, :noinline) end

macro _boundscheck() Expr(:boundscheck) end
macro _propagate_inbounds_meta() Expr(:meta, :inline, :propagate_inbounds) end

# Try to help prevent users from shooting them-selves in the foot
# with ambiguities by defining a few common and critical operations
2 changes: 1 addition & 1 deletion base/array.jl
Original file line number Diff line number Diff line change
@@ -281,7 +281,7 @@ the same manner as C.
"""
function unsafe_copyto!(dest::Array, doffs, src::Array, soffs, n)
n == 0 && return dest
unsafe_copyto!(memoryref(dest.ref, doffs), memoryref(src.ref, soffs), n)
@inbounds unsafe_copyto!(memoryref(dest.ref, doffs), memoryref(src.ref, soffs), n)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is correct.

return dest
end

5 changes: 3 additions & 2 deletions base/boot.jl
Original file line number Diff line number Diff line change
@@ -328,6 +328,7 @@ macro inline() Expr(:meta, :inline) end
macro noinline() Expr(:meta, :noinline) end

macro _boundscheck() Expr(:boundscheck) end
macro _propagate_inbounds_meta() Expr(:meta, :inline, :propagate_inbounds) end

# n.b. the effects and model of these is refined in inference abstractinterpretation.jl
TypeVar(@nospecialize(n)) = _typevar(n::Symbol, Union{}, Any)
@@ -590,8 +591,8 @@ memoryref(mem::GenericMemory) = memoryrefnew(mem)
memoryref(mem::GenericMemory, i::Integer) = memoryrefnew(memoryrefnew(mem), Int(i), @_boundscheck)
memoryref(ref::GenericMemoryRef, i::Integer) = memoryrefnew(ref, Int(i), @_boundscheck)
GenericMemoryRef(mem::GenericMemory) = memoryref(mem)
GenericMemoryRef(mem::GenericMemory, i::Integer) = memoryref(mem, i)
GenericMemoryRef(mem::GenericMemoryRef, i::Integer) = memoryref(mem, i)
GenericMemoryRef(mem::GenericMemory, i::Integer) = (@_propagate_inbounds_meta; memoryref(mem, i))
GenericMemoryRef(mem::GenericMemoryRef, i::Integer) = (@_propagate_inbounds_meta; memoryref(mem, i))

const AtomicMemory{T} = GenericMemory{:atomic, T, CPU}
const AtomicMemoryRef{T} = GenericMemoryRef{:atomic, T, CPU}
4 changes: 0 additions & 4 deletions base/essentials.jl
Original file line number Diff line number Diff line change
@@ -369,10 +369,6 @@ macro _noub_if_noinbounds_meta()
#=:nortcall=#false))
end

# another version of inlining that propagates an inbounds context
macro _propagate_inbounds_meta()
return Expr(:meta, :inline, :propagate_inbounds)
end
macro _nospecializeinfer_meta()
return Expr(:meta, :nospecializeinfer)
end
5 changes: 3 additions & 2 deletions base/genericmemory.jl
Original file line number Diff line number Diff line change
@@ -250,7 +250,8 @@ getindex(A::Memory, c::Colon) = copy(A)

function _setindex!(A::Memory{T}, x::T, i1::Int) where {T}
ref = memoryrefnew(memoryref(A), i1, @_boundscheck)
memoryrefset!(ref, x, :not_atomic, @_boundscheck)
# boundscheck emitted by `memoryrefnew` also checks the index for memoryrefset!
memoryrefset!(ref, x, :not_atomic, false)
return A
end

@@ -261,7 +262,7 @@ function setindex!(A::Memory{T}, x, i1::Int) where {T}
end

function setindex!(A::Memory{T}, x, i1::Int, i2::Int, I::Int...) where {T}
@inline
@_propagate_inbounds_meta
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this is a silly question, but does the @inbounds if destp < srcp || destp > endp above (in unsafe_copyto!) apply @inbounds to the else branch, too? Also, shouldn't the inbounds/prop inbounds pattern be applied to getindex, too?

@boundscheck (i2 == 1 && all(==(1), I)) || throw_boundserror(A, (i1, i2, I...))
setindex!(A, x, i1)
end