Skip to content

Commit 0061f2a

Browse files
authored
Merge pull request #22246 from JuliaLang/yyc/isassigned-boundscheck
Treat bounds check in isassigned as, well, bounds check
2 parents 50fa8d5 + c574949 commit 0061f2a

File tree

4 files changed

+13
-3
lines changed

4 files changed

+13
-3
lines changed

base/array.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ sizeof(a::Array) = elsize(a) * length(a)
9696
function isassigned(a::Array, i::Int...)
9797
@_inline_meta
9898
ii = (sub2ind(size(a), i...) % UInt) - 1
99-
ii < length(a) % UInt || return false
99+
@boundscheck ii < length(a) % UInt || return false
100100
ccall(:jl_array_isassigned, Cint, (Any, UInt), a, ii) == 1
101101
end
102102

base/essentials.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ false
294294
function isassigned end
295295

296296
function isassigned(v::SimpleVector, i::Int)
297-
1 <= i <= length(v) || return false
297+
@boundscheck 1 <= i <= length(v) || return false
298298
x = unsafe_load(convert(Ptr{Ptr{Void}},data_pointer_from_objref(v)) + i*sizeof(Ptr))
299299
return x != C_NULL
300300
end

base/multidimensional.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1229,7 +1229,7 @@ end
12291229
@nexprs $N d->begin
12301230
l = size(B,d)
12311231
stride *= l
1232-
1 <= I_{d-1} <= l || return false
1232+
@boundscheck 1 <= I_{d-1} <= l || return false
12331233
index += (I_d - 1) * stride
12341234
end
12351235
return isassigned(B, index)

test/boundscheck_exec.jl

+10
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,14 @@ else
209209
@test_throws BoundsError V1()
210210
end
211211

212+
# This tests both the bounds check elision and the behavior of `jl_array_isassigned`
213+
# For `isbits` array the `ccall` should return a constant `true` and does not access
214+
# the array
215+
inbounds_isassigned(a, i) = @inbounds return isassigned(a, i)
216+
if bc_opt == bc_default || bc_opt == bc_off
217+
@test inbounds_isassigned(Int[], 2) == true
218+
else
219+
@test inbounds_isassigned(Int[], 2) == false
220+
end
221+
212222
end

0 commit comments

Comments
 (0)